Mercurial > dropbear
comparison gendss.c @ 188:c9483550701b
- refactored random mp_int generation and byte->mp_int code
- added RSA blinding
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 05 May 2005 03:58:21 +0000 |
parents | 6571b480fa04 |
children | 740e782679be b50f0107e505 76097ec1a29a |
comparison
equal
deleted
inserted
replaced
187:c44df7123b0a | 188:c9483550701b |
---|---|
75 /* 160 bit prime */ | 75 /* 160 bit prime */ |
76 genrandom(buf, QSIZE); | 76 genrandom(buf, QSIZE); |
77 buf[0] |= 0x80; /* top bit high */ | 77 buf[0] |= 0x80; /* top bit high */ |
78 buf[QSIZE-1] |= 0x01; /* bottom bit high */ | 78 buf[QSIZE-1] |= 0x01; /* bottom bit high */ |
79 | 79 |
80 if (mp_read_unsigned_bin(key->q, buf, QSIZE) != MP_OKAY) { | 80 bytes_to_mp(key->q, buf, QSIZE); |
81 fprintf(stderr, "dss key generation failed\n"); | |
82 exit(1); | |
83 } | |
84 | 81 |
85 /* 18 rounds are required according to HAC */ | 82 /* 18 rounds are required according to HAC */ |
86 if (mp_prime_next_prime(key->q, 18, 0) != MP_OKAY) { | 83 if (mp_prime_next_prime(key->q, 18, 0) != MP_OKAY) { |
87 fprintf(stderr, "dss key generation failed\n"); | 84 fprintf(stderr, "dss key generation failed\n"); |
88 exit(1); | 85 exit(1); |
114 | 111 |
115 genrandom(buf, size); | 112 genrandom(buf, size); |
116 buf[0] |= 0x80; /* set the top bit high */ | 113 buf[0] |= 0x80; /* set the top bit high */ |
117 | 114 |
118 /* X is a random mp_int */ | 115 /* X is a random mp_int */ |
119 if (mp_read_unsigned_bin(&tempX, buf, size) != MP_OKAY) { | 116 bytes_to_mp(&tempX, buf, size); |
120 fprintf(stderr, "dss key generation failed\n"); | |
121 exit(1); | |
122 } | |
123 | 117 |
124 /* C = X mod 2q */ | 118 /* C = X mod 2q */ |
125 if (mp_mod(&tempX, &temp2q, &tempC) != MP_OKAY) { | 119 if (mp_mod(&tempX, &temp2q, &tempC) != MP_OKAY) { |
126 fprintf(stderr, "dss key generation failed\n"); | 120 fprintf(stderr, "dss key generation failed\n"); |
127 exit(1); | 121 exit(1); |
145 exit(1); | 139 exit(1); |
146 } | 140 } |
147 } while (!result); | 141 } while (!result); |
148 | 142 |
149 mp_clear_multi(&tempX, &tempC, &tempP, &temp2q, NULL); | 143 mp_clear_multi(&tempX, &tempC, &tempP, &temp2q, NULL); |
144 m_burn(buf, size); | |
150 m_free(buf); | 145 m_free(buf); |
151 } | 146 } |
152 | 147 |
153 static void getg(dss_key * key) { | 148 static void getg(dss_key * key) { |
154 | 149 |
187 mp_clear_multi(&div, &h, &val, NULL); | 182 mp_clear_multi(&div, &h, &val, NULL); |
188 } | 183 } |
189 | 184 |
190 static void getx(dss_key *key) { | 185 static void getx(dss_key *key) { |
191 | 186 |
192 DEF_MP_INT(val); | 187 gen_random_mpint(key->q, key->x); |
193 char buf[QSIZE]; | |
194 | |
195 m_mp_init(&val); | |
196 | |
197 do { | |
198 genrandom(buf, QSIZE); | |
199 | |
200 if (mp_read_unsigned_bin(&val, buf, QSIZE) != MP_OKAY) { | |
201 fprintf(stderr, "dss key generation failed\n"); | |
202 } | |
203 } while ((mp_cmp_d(&val, 1) == MP_GT) && (mp_cmp(&val, key->q) == MP_LT)); | |
204 | |
205 mp_copy(&val, key->x); | |
206 mp_clear(&val); | |
207 | |
208 } | 188 } |
209 | 189 |
210 static void gety(dss_key *key) { | 190 static void gety(dss_key *key) { |
211 | 191 |
212 if (mp_exptmod(key->g, key->x, key->p, key->y) != MP_OKAY) { | 192 if (mp_exptmod(key->g, key->x, key->p, key->y) != MP_OKAY) { |