comparison genrsa.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
51 51
52 key = m_malloc(sizeof(*key)); 52 key = m_malloc(sizeof(*key));
53 m_mp_alloc_init_multi(&key->e, &key->n, &key->d, &key->p, &key->q, NULL); 53 m_mp_alloc_init_multi(&key->e, &key->n, &key->d, &key->p, &key->q, NULL);
54 m_mp_init_multi(&pminus, &lcm, &qminus, NULL); 54 m_mp_init_multi(&pminus, &lcm, &qminus, NULL);
55 55
56 if (mp_set_int(key->e, RSA_E) != MP_OKAY) { 56 mp_set_ul(key->e, RSA_E);
57 fprintf(stderr, "RSA generation failed\n");
58 exit(1);
59 }
60 57
61 while (1) { 58 while (1) {
62 getrsaprime(key->p, &pminus, key->e, size/16); 59 getrsaprime(key->p, &pminus, key->e, size/16);
63 getrsaprime(key->q, &qminus, key->e, size/16); 60 getrsaprime(key->q, &qminus, key->e, size/16);
64 61
93 /* return a prime suitable for p or q */ 90 /* return a prime suitable for p or q */
94 static void getrsaprime(mp_int* prime, mp_int *primeminus, 91 static void getrsaprime(mp_int* prime, mp_int *primeminus,
95 mp_int* rsa_e, unsigned int size_bytes) { 92 mp_int* rsa_e, unsigned int size_bytes) {
96 93
97 unsigned char *buf; 94 unsigned char *buf;
95 int trials;
98 DEF_MP_INT(temp_gcd); 96 DEF_MP_INT(temp_gcd);
99 97
100 buf = (unsigned char*)m_malloc(size_bytes); 98 buf = (unsigned char*)m_malloc(size_bytes);
101 99
102 m_mp_init(&temp_gcd); 100 m_mp_init(&temp_gcd);
106 genrandom(buf, size_bytes); 104 genrandom(buf, size_bytes);
107 buf[0] |= 0x80; 105 buf[0] |= 0x80;
108 106
109 bytes_to_mp(prime, buf, size_bytes); 107 bytes_to_mp(prime, buf, size_bytes);
110 108
111 /* find the next integer which is prime, 8 round of miller-rabin */ 109 /* find the next integer which is prime */
112 if (mp_prime_next_prime(prime, 8, 0) != MP_OKAY) { 110 trials = mp_prime_rabin_miller_trials(mp_count_bits(prime));
111 if (mp_prime_next_prime(prime, trials, 0) != MP_OKAY) {
113 fprintf(stderr, "RSA generation failed\n"); 112 fprintf(stderr, "RSA generation failed\n");
114 exit(1); 113 exit(1);
115 } 114 }
116 115
117 /* subtract one to get p-1 */ 116 /* subtract one to get p-1 */