Deleted Added
full compact
bn_rand.c (72613) bn_rand.c (76866)
1/* crypto/bn/bn_rand.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *

--- 62 unchanged lines hidden (view full) ---

71 if (bits == 0)
72 {
73 BN_zero(rnd);
74 return 1;
75 }
76
77 bytes=(bits+7)/8;
78 bit=(bits-1)%8;
1/* crypto/bn/bn_rand.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *

--- 62 unchanged lines hidden (view full) ---

71 if (bits == 0)
72 {
73 BN_zero(rnd);
74 return 1;
75 }
76
77 bytes=(bits+7)/8;
78 bit=(bits-1)%8;
79 mask=0xff<<bit;
79 mask=0xff<<(bit+1);
80
81 buf=(unsigned char *)OPENSSL_malloc(bytes);
82 if (buf == NULL)
83 {
84 BNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);
85 goto err;
86 }
87

--- 7 unchanged lines hidden (view full) ---

95 goto err;
96 }
97 else
98 {
99 if (RAND_bytes(buf, bytes) <= 0)
100 goto err;
101 }
102
80
81 buf=(unsigned char *)OPENSSL_malloc(bytes);
82 if (buf == NULL)
83 {
84 BNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);
85 goto err;
86 }
87

--- 7 unchanged lines hidden (view full) ---

95 goto err;
96 }
97 else
98 {
99 if (RAND_bytes(buf, bytes) <= 0)
100 goto err;
101 }
102
103 if (top)
103#if 1
104 if (pseudorand == 2)
104 {
105 {
105 if (bit == 0)
106 /* generate patterns that are more likely to trigger BN
107 library bugs */
108 int i;
109 unsigned char c;
110
111 for (i = 0; i < bytes; i++)
106 {
112 {
107 buf[0]=1;
108 buf[1]|=0x80;
113 RAND_pseudo_bytes(&c, 1);
114 if (c >= 128 && i > 0)
115 buf[i] = buf[i-1];
116 else if (c < 42)
117 buf[i] = 0;
118 else if (c < 84)
119 buf[i] = 255;
109 }
120 }
121 }
122#endif
123
124 if (top != -1)
125 {
126 if (top)
127 {
128 if (bit == 0)
129 {
130 buf[0]=1;
131 buf[1]|=0x80;
132 }
133 else
134 {
135 buf[0]|=(3<<(bit-1));
136 }
137 }
110 else
111 {
138 else
139 {
112 buf[0]|=(3<<(bit-1));
113 buf[0]&= ~(mask<<1);
140 buf[0]|=(1<<bit);
114 }
115 }
141 }
142 }
116 else
117 {
118 buf[0]|=(1<<bit);
119 buf[0]&= ~(mask<<1);
120 }
121 if (bottom) /* set bottom bits to whatever odd is */
143 buf[0] &= ~mask;
144 if (bottom) /* set bottom bit if requested */
122 buf[bytes-1]|=1;
123 if (!BN_bin2bn(buf,bytes,rnd)) goto err;
124 ret=1;
125err:
126 if (buf != NULL)
127 {
128 memset(buf,0,bytes);
129 OPENSSL_free(buf);

--- 6 unchanged lines hidden (view full) ---

136 return bnrand(0, rnd, bits, top, bottom);
137 }
138
139int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
140 {
141 return bnrand(1, rnd, bits, top, bottom);
142 }
143
145 buf[bytes-1]|=1;
146 if (!BN_bin2bn(buf,bytes,rnd)) goto err;
147 ret=1;
148err:
149 if (buf != NULL)
150 {
151 memset(buf,0,bytes);
152 OPENSSL_free(buf);

--- 6 unchanged lines hidden (view full) ---

159 return bnrand(0, rnd, bits, top, bottom);
160 }
161
162int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
163 {
164 return bnrand(1, rnd, bits, top, bottom);
165 }
166
167#if 1
168int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
169 {
170 return bnrand(2, rnd, bits, top, bottom);
171 }
172#endif
173
144/* random number r: 0 <= r < range */
145int BN_rand_range(BIGNUM *r, BIGNUM *range)
146 {
147 int n;
148
149 if (range->neg || BN_is_zero(range))
150 {
151 BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
152 return 0;
153 }
154
155 n = BN_num_bits(range); /* n > 0 */
174/* random number r: 0 <= r < range */
175int BN_rand_range(BIGNUM *r, BIGNUM *range)
176 {
177 int n;
178
179 if (range->neg || BN_is_zero(range))
180 {
181 BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
182 return 0;
183 }
184
185 n = BN_num_bits(range); /* n > 0 */
156
186
157 if (n == 1)
158 {
159 if (!BN_zero(r)) return 0;
160 }
161 else if (BN_is_bit_set(range, n - 2))
162 {
163 do
164 {
165 /* range = 11..._2, so each iteration succeeds with probability >= .75 */
187 if (n == 1)
188 {
189 if (!BN_zero(r)) return 0;
190 }
191 else if (BN_is_bit_set(range, n - 2))
192 {
193 do
194 {
195 /* range = 11..._2, so each iteration succeeds with probability >= .75 */
166 if (!BN_rand(r, n, 0, 0)) return 0;
196 if (!BN_rand(r, n, -1, 0)) return 0;
167 }
168 while (BN_cmp(r, range) >= 0);
169 }
170 else
171 {
172 /* range = 10..._2,
173 * so 3*range (= 11..._2) is exactly one bit longer than range */
174 do
175 {
197 }
198 while (BN_cmp(r, range) >= 0);
199 }
200 else
201 {
202 /* range = 10..._2,
203 * so 3*range (= 11..._2) is exactly one bit longer than range */
204 do
205 {
176 if (!BN_rand(r, n + 1, 0, 0)) return 0;
206 if (!BN_rand(r, n + 1, -1, 0)) return 0;
177 /* If r < 3*range, use r := r MOD range
178 * (which is either r, r - range, or r - 2*range).
179 * Otherwise, iterate once more.
180 * Since 3*range = 11..._2, each iteration succeeds with
181 * probability >= .75. */
182 if (BN_cmp(r ,range) >= 0)
183 {
184 if (!BN_sub(r, r, range)) return 0;
185 if (BN_cmp(r, range) >= 0)
186 if (!BN_sub(r, r, range)) return 0;
187 }
188 }
189 while (BN_cmp(r, range) >= 0);
190 }
191
192 return 1;
193 }
207 /* If r < 3*range, use r := r MOD range
208 * (which is either r, r - range, or r - 2*range).
209 * Otherwise, iterate once more.
210 * Since 3*range = 11..._2, each iteration succeeds with
211 * probability >= .75. */
212 if (BN_cmp(r ,range) >= 0)
213 {
214 if (!BN_sub(r, r, range)) return 0;
215 if (BN_cmp(r, range) >= 0)
216 if (!BN_sub(r, r, range)) return 0;
217 }
218 }
219 while (BN_cmp(r, range) >= 0);
220 }
221
222 return 1;
223 }
194