Mercurial > dropbear
comparison ecc.c @ 1487:b0c3b46372dc
simplify error handling, check mp_copy return value
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 10 Feb 2018 19:25:00 +0800 |
parents | 06d52bcb8094 |
children | d68d61e7056a |
comparison
equal
deleted
inserted
replaced
1486:cf43bbb6b8ff | 1487:b0c3b46372dc |
---|---|
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 } |