comparison ecc.c @ 1511:5916af64acd4 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Sat, 17 Feb 2018 19:29:51 +0800
parents b0c3b46372dc
children d68d61e7056a
comparison
equal deleted inserted replaced
1457:32f990cc96b1 1511:5916af64acd4
80 return key; 80 return key;
81 } 81 }
82 82
83 /* Copied from libtomcrypt ecc_import.c (version there is static), modified 83 /* Copied from libtomcrypt ecc_import.c (version there is static), modified
84 for different mp_int pointer without LTC_SOURCE */ 84 for different mp_int pointer without LTC_SOURCE */
85 static int ecc_is_point(ecc_key *key) 85 static int ecc_is_point(const ecc_key *key)
86 { 86 {
87 mp_int *prime, *b, *t1, *t2; 87 mp_int *prime, *b, *t1, *t2;
88 int err; 88 int err;
89 89
90 m_mp_alloc_init_multi(&prime, &b, &t1, &t2, NULL); 90 m_mp_alloc_init_multi(&prime, &b, &t1, &t2, NULL);
211 211
212 } 212 }
213 213
214 /* a modified version of libtomcrypt's "ecc_shared_secret" to output 214 /* a modified version of libtomcrypt's "ecc_shared_secret" to output
215 a mp_int instead. */ 215 a mp_int instead. */
216 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key) 216 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, const ecc_key *private_key)
217 { 217 {
218 ecc_point *result = NULL; 218 ecc_point *result = NULL;
219 mp_int *prime = NULL, *shared_secret = NULL; 219 mp_int *prime = NULL, *shared_secret = NULL;
220 int err = DROPBEAR_FAILURE; 220 int err = DROPBEAR_FAILURE;
221 221
222 /* type valid? */ 222 /* type valid? */
223 if (private_key->type != PK_PRIVATE) { 223 if (private_key->type != PK_PRIVATE) {
224 goto done; 224 goto out;
225 } 225 }
226 226
227 if (private_key->dp != public_key->dp) { 227 if (private_key->dp != public_key->dp) {
228 goto done; 228 goto out;
229 } 229 }
230 230
231 /* make new point */ 231 /* make new point */
232 result = ltc_ecc_new_point(); 232 result = ltc_ecc_new_point();
233 if (result == NULL) { 233 if (result == NULL) {
234 goto done; 234 goto out;
235 } 235 }
236 236
237 prime = m_malloc(sizeof(*prime)); 237 prime = m_malloc(sizeof(*prime));
238 m_mp_init(prime); 238 m_mp_init(prime);
239 239
240 if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) { 240 if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) {
241 goto done; 241 goto out;
242 } 242 }
243 if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) { 243 if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) {
244 goto done; 244 goto out;
245 } 245 }
246
247 shared_secret = m_malloc(sizeof(*shared_secret));
248 m_mp_init(shared_secret);
249 if (mp_copy(result->x, shared_secret) != CRYPT_OK) {
250 goto out;
251 }
252
253 mp_clear(prime);
254 m_free(prime);
255 ltc_ecc_del_point(result);
246 256
247 err = DROPBEAR_SUCCESS; 257 err = DROPBEAR_SUCCESS;
248 done: 258 out:
249 if (err == DROPBEAR_SUCCESS) {
250 shared_secret = m_malloc(sizeof(*shared_secret));
251 m_mp_init(shared_secret);
252 mp_copy(result->x, shared_secret);
253 }
254
255 if (prime) {
256 mp_clear(prime);
257 m_free(prime);
258 }
259 if (result)
260 {
261 ltc_ecc_del_point(result);
262 }
263
264 if (err == DROPBEAR_FAILURE) { 259 if (err == DROPBEAR_FAILURE) {
265 dropbear_exit("ECC error"); 260 dropbear_exit("ECC error");
266 } 261 }
267 return shared_secret; 262 return shared_secret;
268 } 263 }