comparison random.c @ 640:76097ec1a29a dropbear-tfm

- Bring in original tomsfastmath patch against 0.52 from Peter Turczak in 2008
author Matt Johnston <matt@ucc.asn.au>
date Mon, 21 Nov 2011 19:19:57 +0800
parents 2cd2edfa11ee
children 2b1bb792cd4d
comparison
equal deleted inserted replaced
518:ce104c8b0be1 640:76097ec1a29a
203 buf += copylen; 203 buf += copylen;
204 } 204 }
205 m_burn(hash, sizeof(hash)); 205 m_burn(hash, sizeof(hash));
206 } 206 }
207 207
208 /* Generates a random mp_int. 208 /* Generates a random fp_int.
209 * max is a *mp_int specifying an upper bound. 209 * max is a *fp_int specifying an upper bound.
210 * rand must be an initialised *mp_int for the result. 210 * rand must be an initialised *fp_int for the result.
211 * the result rand satisfies: 0 < rand < max 211 * the result rand satisfies: 0 < rand < max
212 * */ 212 * */
213 void gen_random_mpint(mp_int *max, mp_int *rand) { 213 void gen_random_fpint(fp_int *max, fp_int *rand) {
214 214
215 unsigned char *randbuf = NULL; 215 unsigned char *randbuf = NULL;
216 unsigned int len = 0; 216 unsigned int len = 0;
217 const unsigned char masks[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f}; 217 const unsigned char masks[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};
218 218
219 const int size_bits = mp_count_bits(max); 219 const int size_bits = fp_count_bits(max);
220 220
221 len = size_bits / 8; 221 len = size_bits / 8;
222 if ((size_bits % 8) != 0) { 222 if ((size_bits % 8) != 0) {
223 len += 1; 223 len += 1;
224 } 224 }
225 225
226 randbuf = (unsigned char*)m_malloc(len); 226 randbuf = (unsigned char*)m_malloc(len);
227 do { 227 do {
228 genrandom(randbuf, len); 228 genrandom(randbuf, len);
229 /* Mask out the unrequired bits - mp_read_unsigned_bin expects 229 /* Mask out the unrequired bits - fp_read_unsigned_bin expects
230 * MSB first.*/ 230 * MSB first.*/
231 randbuf[0] &= masks[size_bits % 8]; 231 randbuf[0] &= masks[size_bits % 8];
232 232
233 bytes_to_mp(rand, randbuf, len); 233 bytes_to_fp(rand, randbuf, len);
234 234
235 /* keep regenerating until we get one satisfying 235 /* keep regenerating until we get one satisfying
236 * 0 < rand < max */ 236 * 0 < rand < max */
237 } while (mp_cmp(rand, max) != MP_LT); 237 } while (fp_cmp(rand, max) != FP_LT);
238 m_burn(randbuf, len); 238 m_burn(randbuf, len);
239 m_free(randbuf); 239 m_free(randbuf);
240 } 240 }