Mercurial > dropbear
annotate ecc.c @ 765:5503e05ab3a4 ecc
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
- Reindent ecc.c properly
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 08 Apr 2013 23:56:31 +0800 |
parents | f744321ac048 |
children | d1575fdc29a6 |
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 |
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 | 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, |
762
a78a38e402d1
- Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents:
761
diff
changeset
|
15 .name = "nistp256" |
756 | 16 }; |
17 #endif | |
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 | 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, |
762
a78a38e402d1
- Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents:
761
diff
changeset
|
22 .name = "nistp384" |
756 | 23 }; |
24 #endif | |
757 | 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 | 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, |
762
a78a38e402d1
- Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents:
761
diff
changeset
|
29 .name = "nistp521" |
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) { |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
34 ecc_key *key = m_malloc(sizeof(*key)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
35 key->pubkey.x = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
36 key->pubkey.y = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
37 key->pubkey.z = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
38 key->k = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
39 m_mp_init_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
40 return 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
|
41 } |
756 | 42 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
43 // Copied from libtomcrypt ecc_import.c (version there is static), modified |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
44 // for different mp_int pointer without LTC_SOURCE |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
45 static int ecc_is_point(ecc_key *key) |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
46 { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
47 mp_int *prime, *b, *t1, *t2; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
48 int err; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
49 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
50 prime = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
51 b = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
52 t1 = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
53 t2 = m_malloc(sizeof(mp_int)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
54 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
55 m_mp_init_multi(prime, b, t1, t2, NULL); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
56 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
57 /* load prime and b */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
58 if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
59 if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
60 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
61 /* compute y^2 */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
62 if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
63 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
64 /* compute x^3 */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
65 if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
66 if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
67 if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
68 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
69 /* compute y^2 - x^3 */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
70 if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
71 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
72 /* compute y^2 - x^3 + 3x */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
73 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
74 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
75 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
76 if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
77 while (mp_cmp_d(t1, 0) == LTC_MP_LT) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
78 if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
79 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
80 while (mp_cmp(t1, prime) != LTC_MP_LT) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
81 if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
82 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
83 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
84 /* compare to b */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
85 if (mp_cmp(t1, b) != LTC_MP_EQ) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
86 err = CRYPT_INVALID_PACKET; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
87 } else { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
88 err = CRYPT_OK; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
89 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
90 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
91 error: |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
92 mp_clear_multi(prime, b, t1, t2, NULL); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
93 m_free(prime); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
94 m_free(b); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
95 m_free(t1); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
96 m_free(t2); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
97 return err; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
98 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
99 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
100 /* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
101 void buf_put_ecc_raw_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
|
102 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
|
103 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
|
104 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
|
105 if (err != CRYPT_OK) { |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
106 dropbear_exit("ECC error"); |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
107 } |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
108 buf_incrwritepos(buf, len); |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
109 } |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
110 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
111 /* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */ |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
112 ecc_key * buf_get_ecc_raw_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
113 ecc_key *key = NULL; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
114 int ret = DROPBEAR_FAILURE; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
115 const unsigned int size = curve->dp->size; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
116 buf_setpos(buf, 0); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
117 unsigned int len = buf->len; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
118 unsigned char first = buf_getbyte(buf); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
119 if (first == 2 || first == 3) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
120 dropbear_log(LOG_WARNING, "Dropbear doesn't support ECC point compression"); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
121 return NULL; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
122 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
123 if (first != 4 || len != 1+2*size) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
124 return NULL; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
125 } |
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
|
126 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
127 key = new_ecc_key(); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
128 key->dp = curve->dp; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
129 |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
130 if (mp_read_unsigned_bin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
131 goto out; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
132 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
133 buf_incrpos(buf, 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
|
134 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
135 if (mp_read_unsigned_bin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
136 goto out; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
137 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
138 buf_incrpos(buf, size); |
757 | 139 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
140 mp_set(key->pubkey.z, 1); |
757 | 141 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
142 if (ecc_is_point(key) != CRYPT_OK) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
143 goto out; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
144 } |
757 | 145 |
146 // SEC1 3.2.3.1 Check that Q != 0 | |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
147 if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
148 goto out; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
149 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
150 if (mp_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
151 goto out; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
152 } |
757 | 153 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
154 ret = DROPBEAR_SUCCESS; |
757 | 155 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
156 out: |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
157 if (ret == DROPBEAR_FAILURE) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
158 if (key) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
159 ecc_free(key); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
160 m_free(key); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
161 key = NULL; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
162 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
163 } |
757 | 164 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
165 return key; |
757 | 166 |
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
167 } |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
168 |
756 | 169 // a modified version of libtomcrypt's "ecc_shared_secret" to output |
170 // a mp_int instead. | |
171 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key) | |
172 { | |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
173 ecc_point *result = NULL; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
174 mp_int *prime = NULL, *shared_secret = NULL; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
175 int err = DROPBEAR_FAILURE; |
756 | 176 |
177 /* type valid? */ | |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
178 if (private_key->type != PK_PRIVATE) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
179 goto done; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
180 } |
756 | 181 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
182 if (private_key->dp != public_key->dp) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
183 goto done; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
184 } |
756 | 185 |
186 /* make new point */ | |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
187 result = ltc_ecc_new_point(); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
188 if (result == NULL) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
189 goto done; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
190 } |
756 | 191 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
192 prime = m_malloc(sizeof(*prime)); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
193 m_mp_init(prime); |
756 | 194 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
195 if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
196 goto done; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
197 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
198 if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
199 goto done; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
200 } |
756 | 201 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
202 err = DROPBEAR_SUCCESS; |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
203 done: |
756 | 204 if (err == DROPBEAR_SUCCESS) { |
763 | 205 shared_secret = m_malloc(sizeof(*shared_secret)); |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
206 m_mp_init(shared_secret); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
207 mp_copy(result->x, shared_secret); |
756 | 208 } |
209 | |
210 if (prime) { | |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
211 mp_clear(prime); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
212 m_free(prime); |
756 | 213 } |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
214 if (result) |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
215 { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
216 ltc_ecc_del_point(result); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
217 } |
756 | 218 |
765
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
219 if (err == DROPBEAR_FAILURE) { |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
220 dropbear_exit("ECC error"); |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
221 } |
5503e05ab3a4
- Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents:
763
diff
changeset
|
222 return shared_secret; |
756 | 223 } |
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
224 |
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
225 #endif |