1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/mpi/utils/bbs_rand.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,63 @@ 1.4 +/* 1.5 + * Blum, Blum & Shub PRNG using the MPI library 1.6 + * 1.7 + * This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include "bbs_rand.h" 1.12 + 1.13 +#define SEED 1 1.14 +#define MODULUS 2 1.15 + 1.16 +/* This modulus is the product of two randomly generated 512-bit 1.17 + prime integers, each of which is congruent to 3 (mod 4). */ 1.18 +static char *bbs_modulus = 1.19 +"75A2A6E1D27393B86562B9CE7279A8403CB4258A637DAB5233465373E37837383EDC" 1.20 +"332282B8575927BC4172CE8C147B4894050EE9D2BDEED355C121037270CA2570D127" 1.21 +"7D2390CD1002263326635CC6B259148DE3A1A03201980A925E395E646A5E9164B0EC" 1.22 +"28559EBA58C87447245ADD0651EDA507056A1129E3A3E16E903D64B437"; 1.23 + 1.24 +static int bbs_init = 0; /* flag set when library is initialized */ 1.25 +static mp_int bbs_state; /* the current state of the generator */ 1.26 + 1.27 +/* Suggested size of random seed data */ 1.28 +int bbs_seed_size = (sizeof(bbs_modulus) / 2); 1.29 + 1.30 +void bbs_srand(unsigned char *data, int len) 1.31 +{ 1.32 + if((bbs_init & SEED) == 0) { 1.33 + mp_init(&bbs_state); 1.34 + bbs_init |= SEED; 1.35 + } 1.36 + 1.37 + mp_read_raw(&bbs_state, (char *)data, len); 1.38 + 1.39 +} /* end bbs_srand() */ 1.40 + 1.41 +unsigned int bbs_rand(void) 1.42 +{ 1.43 + static mp_int modulus; 1.44 + unsigned int result = 0, ix; 1.45 + 1.46 + if((bbs_init & MODULUS) == 0) { 1.47 + mp_init(&modulus); 1.48 + mp_read_radix(&modulus, bbs_modulus, 16); 1.49 + bbs_init |= MODULUS; 1.50 + } 1.51 + 1.52 + for(ix = 0; ix < sizeof(unsigned int); ix++) { 1.53 + mp_digit d; 1.54 + 1.55 + mp_sqrmod(&bbs_state, &modulus, &bbs_state); 1.56 + d = DIGIT(&bbs_state, 0); 1.57 + 1.58 + result = (result << CHAR_BIT) | (d & UCHAR_MAX); 1.59 + } 1.60 + 1.61 + return result; 1.62 + 1.63 +} /* end bbs_rand() */ 1.64 + 1.65 +/*------------------------------------------------------------------------*/ 1.66 +/* HERE THERE BE DRAGONS */