unsigned char * mk_session_key(uint32_t * len) { // We will generate it completely at random, // which may not be the best idea (see weak keys // for GCM mode). int i; unsigned char * key; if (!HSinitted) { havege_init(&HS); HSinitted++; } key = malloc(4 * sizeof(int)); if (key == NULL) return NULL; for (i = 0; i < 4; i++) { ((int*) key)[i] = havege_rand(&HS); } *len = 4 * sizeof(uint32_t); return key; }
int main( int argc, char *argv[] ) { FILE *f; time_t t; int i, j, k; havege_state hs; unsigned char buf[1024]; if( argc < 2 ) { fprintf( stderr, "usage: %s <output filename>\n", argv[0] ); return( 1 ); } if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) { printf( "failed to open '%s' for writing.\n", argv[0] ); return( 1 ); } havege_init( &hs ); t = time( NULL ); for( i = 0, k = 32768; i < k; i++ ) { for( j = 0; j < sizeof( buf ); j++ ) buf[j] = havege_rand( &hs ); fwrite( buf, sizeof( buf ), 1, f ); printf( "Generating 32Mb of data in file '%s'... %04.1f" \ "%% done\r", argv[1], (100 * (float) (i + 1)) / k ); fflush( stdout ); } if( t == time( NULL ) ) t--; fclose( f ); return( 0 ); }
extern uint32_t encrypt(unsigned char * key, uint32_t keylen, unsigned char * in, uint32_t inlen, unsigned char * out) { gcm_ctx_256b ctx; int i; if (!HSinitted) { havege_init(&HS); HSinitted++; } for (i = 0; i < 4; i++) { ((int*) (out + inlen + 16))[i] = havege_rand(&HS); } gcm_init_256b(&ctx,key,keylen * 8); gcm_encrypt_256b(&ctx, out + inlen + 16,16,in,inlen,NULL,0,out,out + inlen); gcm_destroy_256b(&ctx); return 32 + inlen; }