autosurf.c (1259B)
1 /* ISC license. */ 2 3 #include <unistd.h> 4 #include <stdint.h> 5 6 #include <skalibs/uint32.h> 7 #include <skalibs/tai.h> 8 #include <skalibs/sha1.h> 9 #include <skalibs/surf.h> 10 11 /* 12 Writes 160 bytes of crap into s. 13 Certainly not cryptographically secure or 100% unpredictable, 14 but we're only using this to seed a fallback internal PRNG. 15 */ 16 17 static void makeseed (char *s) 18 { 19 SHA1Schedule bak = SHA1_INIT() ; 20 { 21 tain now ; 22 char tmp[256] ; 23 uint32_t x = getpid() ; 24 uint32_pack(tmp, x) ; /* if valgrind sends you here, tell it to stfu */ 25 x = getppid() ; 26 uint32_pack(tmp + 4, x) ; 27 tain_now(&now) ; 28 tain_pack(tmp + 8, &now) ; 29 sha1_update(&bak, tmp, 8 + TAIN_PACK) ; 30 gethostname(tmp, 256) ; 31 sha1_update(&bak, tmp, 256) ; 32 sha1_final(&bak, tmp) ; 33 sha1_init(&bak) ; 34 sha1_update(&bak, tmp, 20) ; 35 } 36 { 37 char i = 0 ; 38 for (; i < 8 ; i++) 39 { 40 SHA1Schedule ctx = bak ; 41 sha1_update(&ctx, &i, 1) ; 42 sha1_final(&ctx, s + 20*i) ; 43 } 44 } 45 } 46 47 void autosurf (char *s, size_t n) 48 { 49 static SURFSchedule ctx = SURFSCHEDULE_ZERO ; 50 static int need4seed = 1 ; 51 if (need4seed) 52 { 53 char seed[160] ; 54 makeseed(seed) ; 55 surf_init(&ctx, seed) ; 56 need4seed = 0 ; 57 } 58 surf(&ctx, s, n) ; 59 }