Mercurial > dropbear
annotate ecc.c @ 757:230666086711 ecc
ecc key import function
author  Matt Johnston <matt@ucc.asn.au> 

date  Wed, 27 Mar 2013 23:50:52 +0800 
parents  bf9dc2d9c2b1 
children  76fba0856749 
rev  line source 

756  1 #include "includes.h" 
2 #include "options.h"  
3 #include "ecc.h"  
4  
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

5 #ifdef DROPBEAR_ECC 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

6 
757  7 // TODO: use raw bytes for the dp rather than the hex strings in libtomcrypt's ecc.c 
8  
756  9 #ifdef DROPBEAR_ECC_256 
757  10 const struct dropbear_ecc_curve ecc_curve_secp256r1 { 
11 .dp = <c_ecc_sets[0],  
756  12 .hash_desc = sha256_desc, 
13 .name = "secp256r1"  
14 };  
15 #endif  
16  
17  
18 #ifdef DROPBEAR_ECC_384  
757  19 const struct dropbear_ecc_curve ecc_curve_secp384r1 { 
20 .dp = <c_ecc_sets[1],  
756  21 .hash_desc = sha384_desc, 
22 .name = "secp384r1"  
23 };  
24 #endif  
25  
757  26 #ifdef DROPBEAR_ECC_521 
27 const struct dropbear_ecc_curve ecc_curve_secp521r1 {  
28 .dp = <c_ecc_sets[2],  
29 .hash_desc = sha521_desc,  
30 .name = "secp521r1"  
756  31 }; 
32 #endif  
33  
34  
757  35 void buf_put_ecc_pubkey_string(buffer *buf, ecc_key *key) { 
756  36 // XXX point compression 
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

37 int len = key>dp>size*2 + 1; 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

38 buf_putint(len); 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

39 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

40 if (err != CRYPT_OK) { 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

41 dropbear_exit("ECC error"); 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

42 } 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

43 buf_incrwritepos(buf, len); 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

44 } 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

45 
757  46 ecc_key * buf_get_ecc_key_string(buffer *buf, const struct dropbear_ecc_curve *curve) { 
47 ecc_key *key = NULL;  
48 int ret = DROPBEAR_FAILURE;  
49 const int size = curve>dp>size;  
50 unsigned int len = buf_get_string(buf);  
51 unsigned char first = buf_get_char(buf);  
52 if (first == 2  first == 3) {  
53 dropbear_log("Dropbear doesn't support ECC point compression");  
54 return NULL;  
55 }  
56 if (first != 4  len != 1+2*size) {  
57 return NULL;  
58 }  
59  
60 key = m_malloc(sizeof(*key));  
61 m_mp_init_multi(&key>pubkey.x, &key>pubkey.y, &key>pubkey.z, &key>k, NULL);  
62  
63 if (mp_read_unsigned_bin(&key>pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) {  
64 goto out;  
65 }  
66 buf_incrpos(buf, size);  
67  
68 if (mp_read_unsigned_bin(&key>pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) {  
69 goto out;  
70 }  
71 buf_incrpos(buf, size);  
72  
73 if (mp_set(key>pubkey.z, 1) != MP_OKAY) {  
74 goto out;  
75 }  
76  
77 if (is_point(key) != CRYPT_OK) {  
78 goto out;  
79 }  
80  
81 // SEC1 3.2.3.1 Check that Q != 0  
82 if (mp_cmp_d(key>pubkey.x, 0) == LTC_MP_EQ) {  
83 goto out;  
84 }  
85 if (mp_cmp_d(key>pubkey.y, 0) == LTC_MP_EQ) {  
86 goto out;  
87 }  
88  
89 ret = DROPBEAR_SUCCESS;  
90  
91 out:  
92 if (ret == DROPBEAR_FAILURE) {  
93 if (key) {  
94 mp_free_multi(&key>pubkey.x, &key>pubkey.y, &key>pubkey.z, &key>k, NULL);  
95 m_free(key);  
96 key = NULL;  
97 }  
98 }  
99  
100 return key;  
101  
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

102 } 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

103 
756  104 // a modified version of libtomcrypt's "ecc_shared_secret" to output 
105 // a mp_int instead.  
106 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key)  
107 {  
108 ecc_point *result = NULL  
109 mp_int *prime = NULL, *shared_secret = NULL;  
110 int ret = DROPBEAR_FAILURE;  
111  
112 /* type valid? */  
113 if (private_key>type != PK_PRIVATE) {  
114 goto done;  
115 }  
116  
117 if (private_key>dp != public_key>dp) {  
118 goto done;  
119 }  
120  
121 #if 0  
122 // XXX  possibly not neccessary tests?  
123 if (ltc_ecc_is_valid_idx(private_key>idx) == 0  ltc_ecc_is_valid_idx(public_key>idx) == 0) {  
124 goto done;  
125 }  
126  
127 if (XSTRCMP(private_key>dp>name, public_key>dp>name) != 0) {  
128 goto done;  
129 }  
130 #endif  
131  
132 /* make new point */  
133 result = ltc_ecc_new_point();  
134 if (result == NULL) {  
135 goto done;  
136 }  
137  
138 prime = m_malloc(sizeof(*prime));  
139 m_mp_init(prime);  
140  
141 if (mp_read_radix(prime, (char *)private_key>dp>prime, 16) != CRYPT_OK) {  
142 goto done;  
143 }  
144 if (ltc_mp.ecc_ptmul(private_key>k, &public_key>pubkey, result, prime, 1) != CRYPT_OK) {  
145 goto done;  
146 }  
147  
148 err = DROPBEAR_SUCCESS;  
149 done:  
150 if (err == DROPBEAR_SUCCESS) {  
151 shared_secret = prime;  
152 prime = NULL;  
153 }  
154  
155 if (prime) {  
156 mp_clear(prime);  
157 m_free(prime);  
158 }  
159 ltc_ecc_del_point(result);  
160  
161 if (err == DROPBEAR_FAILURE) {  
162 dropbear_exit("ECC error");  
163 }  
164  
165 return shared_secret;  
166 return err;  
167 }  
168  
169 }  
755
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

170 
b07eb3dc23ec
refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset

171 #endif 