comparison 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
comparison
equal deleted inserted replaced
756:bf9dc2d9c2b1 757:230666086711
2 #include "options.h" 2 #include "options.h"
3 #include "ecc.h" 3 #include "ecc.h"
4 4
5 #ifdef DROPBEAR_ECC 5 #ifdef DROPBEAR_ECC
6 6
7 // TODO: use raw bytes for the dp rather than the hex strings in libtomcrypt's ecc.c
8
7 #ifdef DROPBEAR_ECC_256 9 #ifdef DROPBEAR_ECC_256
8 const struct ecc_curve_secp256r1 { 10 const struct dropbear_ecc_curve ecc_curve_secp256r1 {
9 .ltc_set = &ltc_ecc_sets[0], 11 .dp = &ltc_ecc_sets[0],
10 .hash_desc = sha256_desc, 12 .hash_desc = sha256_desc,
11 .name = "secp256r1" 13 .name = "secp256r1"
12 }; 14 };
13 #endif 15 #endif
14 16
15 17
16 #ifdef DROPBEAR_ECC_384 18 #ifdef DROPBEAR_ECC_384
17 const struct ecc_curve_secp384r1 { 19 const struct dropbear_ecc_curve ecc_curve_secp384r1 {
18 .ltc_set = &ltc_ecc_sets[1], 20 .dp = &ltc_ecc_sets[1],
19 .hash_desc = sha384_desc, 21 .hash_desc = sha384_desc,
20 .name = "secp384r1" 22 .name = "secp384r1"
21 }; 23 };
22 #endif 24 #endif
23 25
24 #ifdef DROPBEAR_ECC_256 26 #ifdef DROPBEAR_ECC_521
25 const struct ecc_curve_secp256r1 { 27 const struct dropbear_ecc_curve ecc_curve_secp521r1 {
26 .ltc_set = &ltc_ecc_sets[0], 28 .dp = &ltc_ecc_sets[2],
27 .hash_desc = sha256_desc, 29 .hash_desc = sha521_desc,
28 .name = "secp256r1" 30 .name = "secp521r1"
29 }; 31 };
30 #endif 32 #endif
31 33
32 34
33 void buf_put_ecc_key_string(buffer *buf, ecc_key *key) { 35 void buf_put_ecc_pubkey_string(buffer *buf, ecc_key *key) {
34 // XXX point compression 36 // XXX point compression
35 int len = key->dp->size*2 + 1; 37 int len = key->dp->size*2 + 1;
36 buf_putint(len); 38 buf_putint(len);
37 int err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len); 39 int err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len);
38 if (err != CRYPT_OK) { 40 if (err != CRYPT_OK) {
39 dropbear_exit("ECC error"); 41 dropbear_exit("ECC error");
40 } 42 }
41 buf_incrwritepos(buf, len); 43 buf_incrwritepos(buf, len);
42 } 44 }
43 45
44 int buf_get_ecc_key_string(buffer *buf, ecc_key *key) { 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
45 } 102 }
46 103
47 // a modified version of libtomcrypt's "ecc_shared_secret" to output 104 // a modified version of libtomcrypt's "ecc_shared_secret" to output
48 // a mp_int instead. 105 // a mp_int instead.
49 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key) 106 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key)