/* * Seed PRNG using randomness from file * Assume file contains at least RANDOM_BYTES_NEEDED 'random' bytes */ u32 SeedPRNGfromFile (const u8 *rfile, RANDOM_STRUCT *r) { FILE *fp; u32 n=RANDOM_BYTES_NEEDED; u32 bytes; u8 *buffer; if (r == NULL) return PRNG_NOT_INIT; if ( (fp = (FILE *)open_input(rfile)) == NULL) { exit(1); } if ( (buffer = (u8 *)malloc(RANDOM_BYTES_NEEDED)) == NULL) { fprintf(stderr, "error in SeedPRNGfromFile: out of memory.\n"); exit (1); } while (GetRandomBytesNeeded (&n, r) > 0) { if ( (bytes = fread (buffer, 1, n, fp)) < n ) { if ( ferror(fp) ) { fprintf(stderr, "error while reading randomness file '%s' : %s\n", rfile, strerror(errno)); exit (1); } if ( feof(fp) ) { fprintf(stderr, "error: not enough bytes in file '%s' to seed PRNG.\n", rfile); fprintf(stderr, "(bytes expected: %d, bytes read: %d).\n", RANDOM_BYTES_NEEDED, bytes); exit (1); } fprintf(stderr, "error in SeedPRNGfromFile: unkown error\n"); exit (1); } RandomUpdate (r, buffer, bytes); } /* zeroize sensitive information */ memset (buffer, 0, RANDOM_BYTES_NEEDED); free (buffer); fclose(fp); }
/*lint -e525 -e438*/ int GeneratePublicKey(DH_KEY *params, unsigned char *publicValue, unsigned int pubValueLen) { rand_t *rd; int rv; time_t seed; unsigned int bytesNeeded; unsigned char seedByte[20] = {0}; R_RANDOM_STRUCT randomStruct; bytesNeeded = pubValueLen; randomStruct.bytesNeeded = bytesNeeded; memset ((POINTER)randomStruct.state, 0, sizeof (randomStruct.state)); randomStruct.outputAvailable = 0; time(&seed); rd = rand_open(); if (rd == NULL) return -1; rand_set(rd,&seed,4); if (rd == NULL) return -1; while (1) { bytesNeeded = randomStruct.bytesNeeded; if (bytesNeeded == 0) break; rand_get(rd,seedByte,16); if (rd == NULL) return -1; RandomUpdate(&randomStruct, seedByte, 16); } rd = rand_close(rd); params->priVallen = pubValueLen; rv = SetupDHAgreement(publicValue,params->privateValue,pubValueLen, params,&randomStruct); return rv; }