comparison ecc.c @ 767:e465ed10c51d ecc

Be safer with how we handle ltc_ecc_sets[] (particularly with system libtomcrypt) A bit of progress with ecdsa code
author Matt Johnston <matt@ucc.asn.au>
date Tue, 09 Apr 2013 22:44:19 +0800
parents d1575fdc29a6
children 6e6ce39da2fc
comparison
equal deleted inserted replaced
766:d1575fdc29a6 767:e465ed10c51d
4 #include "dbutil.h" 4 #include "dbutil.h"
5 #include "bignum.h" 5 #include "bignum.h"
6 6
7 #ifdef DROPBEAR_ECC 7 #ifdef DROPBEAR_ECC
8 8
9 // TODO: use raw bytes for the dp rather than the hex strings in libtomcrypt's ecc.c 9 // .dp members are filled out by dropbear_ecc_fill_dp() at startup
10 #ifdef DROPBEAR_ECC_256 10 #ifdef DROPBEAR_ECC_256
11 const struct dropbear_ecc_curve ecc_curve_nistp256 = { 11 struct dropbear_ecc_curve ecc_curve_nistp256 = {
12 .dp = &ltc_ecc_sets[0], 12 .ltc_size = 32,
13 .hashdesc = &sha256_desc, 13 .hashdesc = &sha256_desc,
14 .name = "nistp256" 14 .name = "nistp256"
15 }; 15 };
16 #endif 16 #endif
17 #ifdef DROPBEAR_ECC_384 17 #ifdef DROPBEAR_ECC_384
18 const struct dropbear_ecc_curve ecc_curve_nistp384 = { 18 struct dropbear_ecc_curve ecc_curve_nistp384 = {
19 .dp = &ltc_ecc_sets[1], 19 .ltc_size = 48,
20 .hashdesc = &sha384_desc, 20 .hashdesc = &sha384_desc,
21 .name = "nistp384" 21 .name = "nistp384"
22 }; 22 };
23 #endif 23 #endif
24 #ifdef DROPBEAR_ECC_521 24 #ifdef DROPBEAR_ECC_521
25 const struct dropbear_ecc_curve ecc_curve_nistp521 = { 25 struct dropbear_ecc_curve ecc_curve_nistp521 = {
26 .dp = &ltc_ecc_sets[2], 26 .ltc_size = 66,
27 .hashdesc = &sha512_desc, 27 .hashdesc = &sha512_desc,
28 .name = "nistp521" 28 .name = "nistp521"
29 }; 29 };
30 #endif 30 #endif
31 31
32 static ecc_key * new_ecc_key(void) { 32 struct dropbear_ecc_curve *dropbear_ecc_curves[] = {
33 #ifdef DROPBEAR_ECC_256
34 &ecc_curve_nistp256,
35 #endif
36 #ifdef DROPBEAR_ECC_384
37 &ecc_curve_nistp384,
38 #endif
39 #ifdef DROPBEAR_ECC_521
40 &ecc_curve_nistp521,
41 #endif
42 NULL
43 };
44
45 void dropbear_ecc_fill_dp() {
46 struct dropbear_ecc_curve **curve;
47 // libtomcrypt guarantees they're ordered by size
48 const ltc_ecc_set_type *dp = ltc_ecc_sets;
49 for (curve = dropbear_ecc_curves; *curve; curve++) {
50 for (;dp->size > 0; dp++) {
51 if (dp->size == (*curve)->ltc_size) {
52 (*curve)->dp = dp;
53 break;
54 }
55 }
56 if (!(*curve)->dp) {
57 dropbear_exit("Missing ECC params %s", (*curve)->name);
58 }
59 }
60 }
61
62 ecc_key * new_ecc_key(void) {
33 ecc_key *key = m_malloc(sizeof(*key)); 63 ecc_key *key = m_malloc(sizeof(*key));
34 key->pubkey.x = m_malloc(sizeof(mp_int)); 64 key->pubkey.x = m_malloc(sizeof(mp_int));
35 key->pubkey.y = m_malloc(sizeof(mp_int)); 65 key->pubkey.y = m_malloc(sizeof(mp_int));
36 key->pubkey.z = m_malloc(sizeof(mp_int)); 66 key->pubkey.z = m_malloc(sizeof(mp_int));
37 key->k = m_malloc(sizeof(mp_int)); 67 key->k = m_malloc(sizeof(mp_int));