annotate ecc.c @ 763:f744321ac048 ecc

ecdh works against OpenSSH
author Matt Johnston <matt@ucc.asn.au>
date Mon, 08 Apr 2013 23:12:20 +0800
parents a78a38e402d1
children 5503e05ab3a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
1 #include "includes.h"
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
2 #include "options.h"
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
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
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
9 // TODO: use raw bytes for the dp rather than the hex strings in libtomcrypt's ecc.c
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
10
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
11 #ifdef DROPBEAR_ECC_256
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
12 const struct dropbear_ecc_curve ecc_curve_nistp256 = {
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
13 .dp = &ltc_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,
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
15 .name = "nistp256"
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
16 };
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
17 #endif
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
18 #ifdef DROPBEAR_ECC_384
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
19 const struct dropbear_ecc_curve ecc_curve_nistp384 = {
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
20 .dp = &ltc_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,
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
22 .name = "nistp384"
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
23 };
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
24 #endif
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
25 #ifdef DROPBEAR_ECC_521
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
26 const struct dropbear_ecc_curve ecc_curve_nistp521 = {
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
27 .dp = &ltc_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,
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
29 .name = "nistp521"
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
30 };
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
31 #endif
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
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));
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
38 key->k = m_malloc(sizeof(mp_int));
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
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
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
42
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
111 ecc_key *key = NULL;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
119 return NULL;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
120 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
121 if (first != 4 || len != 1+2*size) {
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
122 return NULL;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
123 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
129 goto out;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
130 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
131 buf_incrpos(buf, size);
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
134 goto out;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
135 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
136 buf_incrpos(buf, size);
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
141 goto out;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
142 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
143
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
144 // SEC1 3.2.3.1 Check that Q != 0
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
145 if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) {
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
146 goto out;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
147 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
148 if (mp_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) {
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
149 goto out;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
150 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
151
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
152 ret = DROPBEAR_SUCCESS;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
153
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
154 out:
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
155 if (ret == DROPBEAR_FAILURE) {
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
158 m_free(key);
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
159 key = NULL;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
160 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
161 }
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
162
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
163 return key;
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
167 // a modified version of libtomcrypt's "ecc_shared_secret" to output
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
168 // a mp_int instead.
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
169 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key)
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
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
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
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
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
174
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
175 /* type valid? */
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
176 if (private_key->type != PK_PRIVATE) {
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
177 goto done;
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
178 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
179
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
180 if (private_key->dp != public_key->dp) {
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
181 goto done;
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
182 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
183
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
184 /* make new point */
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
185 result = ltc_ecc_new_point();
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
186 if (result == NULL) {
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
187 goto done;
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
188 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
189
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
190 prime = m_malloc(sizeof(*prime));
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
191 m_mp_init(prime);
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
192
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
193 if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) {
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
194 goto done;
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
195 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
196 if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) {
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
197 goto done;
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
198 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
199
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
200 err = DROPBEAR_SUCCESS;
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
201 done:
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
202 if (err == DROPBEAR_SUCCESS) {
763
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
203 shared_secret = m_malloc(sizeof(*shared_secret));
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
204 m_mp_init(shared_secret);
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
205 mp_copy(result->x, shared_secret);
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
206 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
207
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
208 if (prime) {
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
209 mp_clear(prime);
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
210 m_free(prime);
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
211 }
763
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
212 if (result)
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
213 {
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
214 ltc_ecc_del_point(result);
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
215 }
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
216
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
217 if (err == DROPBEAR_FAILURE) {
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
218 dropbear_exit("ECC error");
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
219 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
220 return shared_secret;
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
221 }
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 #endif