Mercurial > dropbear
annotate ecc.c @ 761:ac2158e3e403 ecc
ecc kind of works, needs fixing/testing
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 07 Apr 2013 01:36:42 +0800 |
parents | 76fba0856749 |
children | a78a38e402d1 |
rev | line source |
---|---|
756 | 1 #include "includes.h" |
2 #include "options.h" | |
3 #include "ecc.h" | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
4 #include "dbutil.h" |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
5 #include "bignum.h" |
756 | 6 |
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 #ifdef DROPBEAR_ECC |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 |
757 | 9 // TODO: use raw bytes for the dp rather than the hex strings in libtomcrypt's ecc.c |
10 | |
756 | 11 #ifdef DROPBEAR_ECC_256 |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
12 const struct dropbear_ecc_curve ecc_curve_secp256r1 = { |
757 | 13 .dp = <c_ecc_sets[0], |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
14 .hash_desc = &sha256_desc, |
756 | 15 .name = "secp256r1" |
16 }; | |
17 #endif | |
18 #ifdef DROPBEAR_ECC_384 | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
19 const struct dropbear_ecc_curve ecc_curve_secp384r1 = { |
757 | 20 .dp = <c_ecc_sets[1], |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
21 .hash_desc = &sha384_desc, |
756 | 22 .name = "secp384r1" |
23 }; | |
24 #endif | |
757 | 25 #ifdef DROPBEAR_ECC_521 |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
26 const struct dropbear_ecc_curve ecc_curve_secp521r1 = { |
757 | 27 .dp = <c_ecc_sets[2], |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
28 .hash_desc = &sha512_desc, |
757 | 29 .name = "secp521r1" |
756 | 30 }; |
31 #endif | |
32 | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
33 static ecc_key * new_ecc_key(void) { |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
34 ecc_key *key = m_malloc(sizeof(*key)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
35 key->pubkey.x = m_malloc(sizeof(mp_int)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
36 key->pubkey.y = m_malloc(sizeof(mp_int)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
37 key->pubkey.z = m_malloc(sizeof(mp_int)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
38 key->k = m_malloc(sizeof(mp_init)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
39 m_mp_init_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
40 return key; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
41 } |
756 | 42 |
757 | 43 void buf_put_ecc_pubkey_string(buffer *buf, ecc_key *key) { |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
44 unsigned long len = key->dp->size*2 + 1; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
45 buf_putint(buf, len); |
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
46 int err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len); |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 if (err != CRYPT_OK) { |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 dropbear_exit("ECC error"); |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 } |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 buf_incrwritepos(buf, len); |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 } |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
52 |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
53 // Copied from libtomcrypt ecc_import.c (version there is static), modified |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
54 // for different mp_int pointer without LTC_SOURCE |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
55 static int ecc_is_point(ecc_key *key) |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
56 { |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
57 mp_int *prime, *b, *t1, *t2; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
58 int err; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
59 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
60 prime = m_malloc(sizeof(mp_int)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
61 b = m_malloc(sizeof(mp_int)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
62 t1 = m_malloc(sizeof(mp_int)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
63 t2 = m_malloc(sizeof(mp_int)); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
64 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
65 m_mp_init_multi(prime, b, t1, t2, NULL); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
66 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
67 /* load prime and b */ |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
68 if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
69 if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
70 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
71 /* compute y^2 */ |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
72 if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
73 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
74 /* compute x^3 */ |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
75 if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
76 if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
77 if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
78 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
79 /* compute y^2 - x^3 */ |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
80 if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
81 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
82 /* compute y^2 - x^3 + 3x */ |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
83 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
84 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
85 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
86 if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
87 while (mp_cmp_d(t1, 0) == LTC_MP_LT) { |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
88 if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
89 } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
90 while (mp_cmp(t1, prime) != LTC_MP_LT) { |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
91 if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
92 } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
93 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
94 /* compare to b */ |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
95 if (mp_cmp(t1, b) != LTC_MP_EQ) { |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
96 err = CRYPT_INVALID_PACKET; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
97 } else { |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
98 err = CRYPT_OK; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
99 } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
100 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
101 error: |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
102 mp_clear_multi(prime, b, t1, t2, NULL); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
103 m_free(prime); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
104 m_free(b); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
105 m_free(t1); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
106 m_free(t2); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
107 return err; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
108 } |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
109 |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
110 ecc_key * buf_get_ecc_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve) { |
757 | 111 ecc_key *key = NULL; |
112 int ret = DROPBEAR_FAILURE; | |
761
ac2158e3e403
ecc kind of works, needs fixing/testing
Matt Johnston <matt@ucc.asn.au>
parents:
759
diff
changeset
|
113 const unsigned int size = curve->dp->size; |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
114 buf_setpos(buf, 0); |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
115 unsigned int len = buf->len; |
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
116 unsigned char first = buf_getbyte(buf); |
757 | 117 if (first == 2 || first == 3) { |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
118 dropbear_log(LOG_WARNING, "Dropbear doesn't support ECC point compression"); |
757 | 119 return NULL; |
120 } | |
121 if (first != 4 || len != 1+2*size) { | |
122 return NULL; | |
123 } | |
124 | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
125 key = new_ecc_key(); |
761
ac2158e3e403
ecc kind of works, needs fixing/testing
Matt Johnston <matt@ucc.asn.au>
parents:
759
diff
changeset
|
126 key->dp = curve->dp; |
757 | 127 |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
128 if (mp_read_unsigned_bin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) { |
757 | 129 goto out; |
130 } | |
131 buf_incrpos(buf, size); | |
132 | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
133 if (mp_read_unsigned_bin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) { |
757 | 134 goto out; |
135 } | |
136 buf_incrpos(buf, size); | |
137 | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
138 mp_set(key->pubkey.z, 1); |
757 | 139 |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
140 if (ecc_is_point(key) != CRYPT_OK) { |
757 | 141 goto out; |
142 } | |
143 | |
144 // SEC1 3.2.3.1 Check that Q != 0 | |
145 if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) { | |
146 goto out; | |
147 } | |
148 if (mp_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) { | |
149 goto out; | |
150 } | |
151 | |
152 ret = DROPBEAR_SUCCESS; | |
153 | |
154 out: | |
155 if (ret == DROPBEAR_FAILURE) { | |
156 if (key) { | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
157 ecc_free(key); |
757 | 158 m_free(key); |
159 key = NULL; | |
160 } | |
161 } | |
162 | |
163 return key; | |
164 | |
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
165 } |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
166 |
756 | 167 // a modified version of libtomcrypt's "ecc_shared_secret" to output |
168 // a mp_int instead. | |
169 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key) | |
170 { | |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
171 ecc_point *result = NULL; |
756 | 172 mp_int *prime = NULL, *shared_secret = NULL; |
759
76fba0856749
More changes for KEX and ECDH. Set up hash descriptors, make ECC code work,
Matt Johnston <matt@ucc.asn.au>
parents:
757
diff
changeset
|
173 int err = DROPBEAR_FAILURE; |
756 | 174 |
175 /* type valid? */ | |
176 if (private_key->type != PK_PRIVATE) { | |
177 goto done; | |
178 } | |
179 | |
180 if (private_key->dp != public_key->dp) { | |
181 goto done; | |
182 } | |
183 | |
184 #if 0 | |
185 // XXX - possibly not neccessary tests? | |
186 if (ltc_ecc_is_valid_idx(private_key->idx) == 0 || ltc_ecc_is_valid_idx(public_key->idx) == 0) { | |
187 goto done; | |
188 } | |
189 | |
190 if (XSTRCMP(private_key->dp->name, public_key->dp->name) != 0) { | |
191 goto done; | |
192 } | |
193 #endif | |
194 | |
195 /* make new point */ | |
196 result = ltc_ecc_new_point(); | |
197 if (result == NULL) { | |
198 goto done; | |
199 } | |
200 | |
201 prime = m_malloc(sizeof(*prime)); | |
202 m_mp_init(prime); | |
203 | |
204 if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) { | |
205 goto done; | |
206 } | |
207 if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) { | |
208 goto done; | |
209 } | |
210 | |
211 err = DROPBEAR_SUCCESS; | |
212 done: | |
213 if (err == DROPBEAR_SUCCESS) { | |
214 shared_secret = prime; | |
215 prime = NULL; | |
216 } | |
217 | |
218 if (prime) { | |
219 mp_clear(prime); | |
220 m_free(prime); | |
221 } | |
222 ltc_ecc_del_point(result); | |
223 | |
224 if (err == DROPBEAR_FAILURE) { | |
225 dropbear_exit("ECC error"); | |
226 } | |
227 | |
228 return shared_secret; | |
229 } | |
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
230 |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
231 #endif |