security/nss/lib/freebl/mpi/utils/prng.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  *  prng.c
     3  *
     4  *  Command-line pseudo-random number generator
     5  *
     6  * This Source Code Form is subject to the terms of the Mozilla Public
     7  * License, v. 2.0. If a copy of the MPL was not distributed with this
     8  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    10 #include <stdio.h>
    11 #include <stdlib.h>
    12 #include <limits.h>
    13 #include <time.h>
    15 #ifdef __OS2__
    16 #include <types.h>
    17 #include <process.h>
    18 #else
    19 #include <unistd.h>
    20 #endif
    22 #include "bbs_rand.h"
    24 int main(int argc, char *argv[])
    25 {
    26   unsigned char *seed;
    27   unsigned int   ix, num = 1;
    28   pid_t          pid;
    30   if(argc > 1) {
    31     num = atoi(argv[1]);
    32     if(num <= 0) 
    33       num = 1;
    34   }
    36   pid = getpid();
    37   srand(time(NULL) * (unsigned int)pid);
    39   /* Not a perfect seed, but not bad */
    40   seed = malloc(bbs_seed_size);
    41   for(ix = 0; ix < bbs_seed_size; ix++) {
    42     seed[ix] = rand() % UCHAR_MAX;
    43   }
    45   bbs_srand(seed, bbs_seed_size);
    46   memset(seed, 0, bbs_seed_size);
    47   free(seed);
    49   while(num-- > 0) {
    50     ix = bbs_rand();
    52     printf("%u\n", ix);
    53   }
    55   return 0;
    57 }

mercurial