annotate ecc.c @ 805:724c3e0c8734 ecc

Add m_mp_alloc_init_multi() helper
author Matt Johnston <matt@ucc.asn.au>
date Thu, 23 May 2013 22:18:16 +0800
parents 70625eed40c9
children 7540c0822374
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
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
9 // .dp members are filled out by dropbear_ecc_fill_dp() at startup
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
10 #ifdef DROPBEAR_ECC_256
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
11 struct dropbear_ecc_curve ecc_curve_nistp256 = {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
12 .ltc_size = 32,
793
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
13 .hash_desc = &sha256_desc,
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
14 .name = "nistp256"
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
15 };
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
16 #endif
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
17 #ifdef DROPBEAR_ECC_384
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
18 struct dropbear_ecc_curve ecc_curve_nistp384 = {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
19 .ltc_size = 48,
793
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
20 .hash_desc = &sha384_desc,
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
21 .name = "nistp384"
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
22 };
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
23 #endif
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
24 #ifdef DROPBEAR_ECC_521
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
25 struct dropbear_ecc_curve ecc_curve_nistp521 = {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
26 .ltc_size = 66,
793
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
27 .hash_desc = &sha512_desc,
762
a78a38e402d1 - Fix various hardcoded uses of SHA1
Matt Johnston <matt@ucc.asn.au>
parents: 761
diff changeset
28 .name = "nistp521"
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
29 };
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
30 #endif
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
31
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
32 struct dropbear_ecc_curve *dropbear_ecc_curves[] = {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
33 #ifdef DROPBEAR_ECC_256
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
34 &ecc_curve_nistp256,
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
35 #endif
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
36 #ifdef DROPBEAR_ECC_384
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
37 &ecc_curve_nistp384,
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
38 #endif
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
39 #ifdef DROPBEAR_ECC_521
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
40 &ecc_curve_nistp521,
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
41 #endif
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
42 NULL
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
43 };
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
44
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
45 void dropbear_ecc_fill_dp() {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
46 struct dropbear_ecc_curve **curve;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
47 // libtomcrypt guarantees they're ordered by size
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
48 const ltc_ecc_set_type *dp = ltc_ecc_sets;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
49 for (curve = dropbear_ecc_curves; *curve; curve++) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
50 for (;dp->size > 0; dp++) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
51 if (dp->size == (*curve)->ltc_size) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
52 (*curve)->dp = dp;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
53 break;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
54 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
55 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
56 if (!(*curve)->dp) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
57 dropbear_exit("Missing ECC params %s", (*curve)->name);
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
58 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
59 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
60 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
61
793
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
62 struct dropbear_ecc_curve* curve_for_dp(const ltc_ecc_set_type *dp) {
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
63 struct dropbear_ecc_curve **curve = NULL;
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
64 for (curve = dropbear_ecc_curves; *curve; curve++) {
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
65 if ((*curve)->dp == dp) {
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
66 break;
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
67 }
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
68 }
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
69 assert(*curve);
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
70 return *curve;
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
71 }
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 768
diff changeset
72
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
73 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
74 ecc_key *key = m_malloc(sizeof(*key));
805
724c3e0c8734 Add m_mp_alloc_init_multi() helper
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
75 m_mp_alloc_init_multi(&key->pubkey.x, &key->pubkey.y,
724c3e0c8734 Add m_mp_alloc_init_multi() helper
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
76 &key->pubkey.z, &key->k, NULL);
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
77 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
78 }
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
79
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
80 // 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
81 // 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
82 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
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 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
85 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
86
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 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
88 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
89 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
90 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
91
805
724c3e0c8734 Add m_mp_alloc_init_multi() helper
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
92 m_mp_alloc_init_multi(&prime, &b, &t1, &t2, NULL);
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
93
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 /* 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
95 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
96 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
97
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 /* 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
99 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
100
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 /* 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
102 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
103 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
104 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
105
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
106 /* 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
107 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
108
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
109 /* 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
110 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
111 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
112 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
113 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
114 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
115 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
116 }
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 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
118 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
119 }
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
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 /* 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
122 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
123 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
124 } 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
125 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
126 }
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
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 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
129 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
130 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
131 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
132 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
133 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
134 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
135 }
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
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 /* 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
138 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
139 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
140 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
141 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
142 if (err != CRYPT_OK) {
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 dropbear_exit("ECC error");
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 }
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 buf_incrwritepos(buf, len);
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 }
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147
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
148 /* 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
149 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
150 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
151 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
152 const unsigned int size = curve->dp->size;
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
153 unsigned char first;
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
154
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
155 TRACE(("enter buf_get_ecc_raw_pubkey"))
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
156
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
157 buf_setpos(buf, 0);
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
158 first = buf_getbyte(buf);
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
159 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
160 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
161 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
162 }
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
163 if (first != 4 || buf->len != 1+2*size) {
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
164 TRACE(("leave, wrong size"))
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 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
166 }
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
167
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
168 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
169 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
170
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
171 if (mp_read_unsigned_bin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) {
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
172 TRACE(("failed to read x"))
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 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
174 }
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 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
176
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
177 if (mp_read_unsigned_bin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) {
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
178 TRACE(("failed to read y"))
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
179 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
180 }
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
181 buf_incrpos(buf, size);
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
182
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
183 mp_set(key->pubkey.z, 1);
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
184
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
185 if (ecc_is_point(key) != CRYPT_OK) {
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
186 TRACE(("failed, not a 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 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
188 }
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
189
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
190 // 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
191 if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) {
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
192 TRACE(("failed, x == 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
193 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
194 }
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_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) {
768
6e6ce39da2fc A bit of debugging output
Matt Johnston <matt@ucc.asn.au>
parents: 767
diff changeset
196 TRACE(("failed, y == 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
197 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
198 }
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
199
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
200 ret = DROPBEAR_SUCCESS;
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
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 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
203 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
204 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
205 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
206 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
207 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
208 }
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
209 }
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
210
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 return key;
757
230666086711 ecc key import function
Matt Johnston <matt@ucc.asn.au>
parents: 756
diff changeset
212
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 }
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
215 // 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
216 // a mp_int instead.
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
217 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
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 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
220 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
221 int err = DROPBEAR_FAILURE;
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
222
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
223 /* 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
224 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
225 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
226 }
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
227
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
228 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
229 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
230 }
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
231
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
232 /* 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
233 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
234 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
235 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
236 }
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
237
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
238 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
239 m_mp_init(prime);
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
240
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
241 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
242 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
243 }
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
244 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
245 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
246 }
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
247
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
248 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
249 done:
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
250 if (err == DROPBEAR_SUCCESS) {
763
f744321ac048 ecdh works against OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 762
diff changeset
251 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
252 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
253 mp_copy(result->x, shared_secret);
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
254 }
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
255
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
256 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
257 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
258 m_free(prime);
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
259 }
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
260 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
261 {
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
262 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
263 }
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
264
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
265 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
266 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
267 }
5503e05ab3a4 - Rename buf_put_ecc_pubkey_string() to buf_put_ecc_raw_pubkey_string()
Matt Johnston <matt@ucc.asn.au>
parents: 763
diff changeset
268 return shared_secret;
756
bf9dc2d9c2b1 more bits on ecc branch
Matt Johnston <matt@ucc.asn.au>
parents: 755
diff changeset
269 }
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 #endif