Mercurial > dropbear
comparison genrsa.c @ 892:ae766a2c8fa7
generate RSA keys of exact length
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 14 Feb 2014 23:18:45 +0800 |
parents | 220f55d540ae |
children | 750ec4ec4cbe |
comparison
equal
deleted
inserted
replaced
891:e78f5ce6e7bb | 892:ae766a2c8fa7 |
---|---|
56 if (mp_set_int(key->e, RSA_E) != MP_OKAY) { | 56 if (mp_set_int(key->e, RSA_E) != MP_OKAY) { |
57 fprintf(stderr, "RSA generation failed\n"); | 57 fprintf(stderr, "RSA generation failed\n"); |
58 exit(1); | 58 exit(1); |
59 } | 59 } |
60 | 60 |
61 getrsaprime(key->p, &pminus, key->e, size/16); | 61 while (1) { |
62 getrsaprime(key->q, &qminus, key->e, size/16); | 62 getrsaprime(key->p, &pminus, key->e, size/16); |
63 getrsaprime(key->q, &qminus, key->e, size/16); | |
63 | 64 |
64 if (mp_mul(key->p, key->q, key->n) != MP_OKAY) { | 65 if (mp_mul(key->p, key->q, key->n) != MP_OKAY) { |
65 fprintf(stderr, "RSA generation failed\n"); | 66 fprintf(stderr, "RSA generation failed\n"); |
66 exit(1); | 67 exit(1); |
68 } | |
69 | |
70 if ((unsigned int)mp_count_bits(key->n) == size) { | |
71 break; | |
72 } | |
67 } | 73 } |
68 | 74 |
69 /* lcm(p-1, q-1) */ | 75 /* lcm(p-1, q-1) */ |
70 if (mp_lcm(&pminus, &qminus, &lcm) != MP_OKAY) { | 76 if (mp_lcm(&pminus, &qminus, &lcm) != MP_OKAY) { |
71 fprintf(stderr, "RSA generation failed\n"); | 77 fprintf(stderr, "RSA generation failed\n"); |
89 mp_int* rsa_e, unsigned int size_bytes) { | 95 mp_int* rsa_e, unsigned int size_bytes) { |
90 | 96 |
91 unsigned char *buf; | 97 unsigned char *buf; |
92 DEF_MP_INT(temp_gcd); | 98 DEF_MP_INT(temp_gcd); |
93 | 99 |
94 buf = (unsigned char*)m_malloc(size_bytes+1); | 100 buf = (unsigned char*)m_malloc(size_bytes); |
95 | 101 |
96 m_mp_init(&temp_gcd); | 102 m_mp_init(&temp_gcd); |
97 do { | 103 do { |
98 /* generate a random odd number with MSB set, then find the | 104 /* generate a random odd number with MSB set, then find the |
99 the next prime above it */ | 105 the next prime above it */ |
100 genrandom(buf, size_bytes+1); | 106 genrandom(buf, size_bytes); |
101 buf[0] |= 0x80; /* MSB set */ | 107 buf[0] |= 0x80; |
102 | 108 |
103 bytes_to_mp(prime, buf, size_bytes+1); | 109 bytes_to_mp(prime, buf, size_bytes); |
104 | 110 |
105 /* find the next integer which is prime, 8 round of miller-rabin */ | 111 /* find the next integer which is prime, 8 round of miller-rabin */ |
106 if (mp_prime_next_prime(prime, 8, 0) != MP_OKAY) { | 112 if (mp_prime_next_prime(prime, 8, 0) != MP_OKAY) { |
107 fprintf(stderr, "RSA generation failed\n"); | 113 fprintf(stderr, "RSA generation failed\n"); |
108 exit(1); | 114 exit(1); |
120 } | 126 } |
121 } while (mp_cmp_d(&temp_gcd, 1) != MP_EQ); /* while gcd(p-1, e) != 1 */ | 127 } while (mp_cmp_d(&temp_gcd, 1) != MP_EQ); /* while gcd(p-1, e) != 1 */ |
122 | 128 |
123 /* now we have a good value for result */ | 129 /* now we have a good value for result */ |
124 mp_clear(&temp_gcd); | 130 mp_clear(&temp_gcd); |
125 m_burn(buf, size_bytes+1); | 131 m_burn(buf, size_bytes); |
126 m_free(buf); | 132 m_free(buf); |
127 } | 133 } |
128 | 134 |
129 #endif /* DROPBEAR_RSA */ | 135 #endif /* DROPBEAR_RSA */ |