Mercurial > dropbear
comparison ecc.c @ 756:bf9dc2d9c2b1 ecc
more bits on ecc branch
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 27 Mar 2013 00:38:03 +0800 |
parents | b07eb3dc23ec |
children | 230666086711 |
comparison
equal
deleted
inserted
replaced
755:b07eb3dc23ec | 756:bf9dc2d9c2b1 |
---|---|
1 #include "includes.h" | |
2 #include "options.h" | |
3 #include "ecc.h" | |
4 | |
1 #ifdef DROPBEAR_ECC | 5 #ifdef DROPBEAR_ECC |
2 | 6 |
7 #ifdef DROPBEAR_ECC_256 | |
8 const struct ecc_curve_secp256r1 { | |
9 .ltc_set = <c_ecc_sets[0], | |
10 .hash_desc = sha256_desc, | |
11 .name = "secp256r1" | |
12 }; | |
13 #endif | |
14 | |
15 | |
16 #ifdef DROPBEAR_ECC_384 | |
17 const struct ecc_curve_secp384r1 { | |
18 .ltc_set = <c_ecc_sets[1], | |
19 .hash_desc = sha384_desc, | |
20 .name = "secp384r1" | |
21 }; | |
22 #endif | |
23 | |
24 #ifdef DROPBEAR_ECC_256 | |
25 const struct ecc_curve_secp256r1 { | |
26 .ltc_set = <c_ecc_sets[0], | |
27 .hash_desc = sha256_desc, | |
28 .name = "secp256r1" | |
29 }; | |
30 #endif | |
31 | |
32 | |
3 void buf_put_ecc_key_string(buffer *buf, ecc_key *key) { | 33 void buf_put_ecc_key_string(buffer *buf, ecc_key *key) { |
34 // XXX point compression | |
4 int len = key->dp->size*2 + 1; | 35 int len = key->dp->size*2 + 1; |
5 buf_putint(len); | 36 buf_putint(len); |
6 int err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len); | 37 int err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len); |
7 if (err != CRYPT_OK) { | 38 if (err != CRYPT_OK) { |
8 dropbear_exit("ECC error"); | 39 dropbear_exit("ECC error"); |
11 } | 42 } |
12 | 43 |
13 int buf_get_ecc_key_string(buffer *buf, ecc_key *key) { | 44 int buf_get_ecc_key_string(buffer *buf, ecc_key *key) { |
14 } | 45 } |
15 | 46 |
47 // a modified version of libtomcrypt's "ecc_shared_secret" to output | |
48 // a mp_int instead. | |
49 mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, ecc_key *private_key) | |
50 { | |
51 ecc_point *result = NULL | |
52 mp_int *prime = NULL, *shared_secret = NULL; | |
53 int ret = DROPBEAR_FAILURE; | |
54 | |
55 /* type valid? */ | |
56 if (private_key->type != PK_PRIVATE) { | |
57 goto done; | |
58 } | |
59 | |
60 if (private_key->dp != public_key->dp) { | |
61 goto done; | |
62 } | |
63 | |
64 #if 0 | |
65 // XXX - possibly not neccessary tests? | |
66 if (ltc_ecc_is_valid_idx(private_key->idx) == 0 || ltc_ecc_is_valid_idx(public_key->idx) == 0) { | |
67 goto done; | |
68 } | |
69 | |
70 if (XSTRCMP(private_key->dp->name, public_key->dp->name) != 0) { | |
71 goto done; | |
72 } | |
73 #endif | |
74 | |
75 /* make new point */ | |
76 result = ltc_ecc_new_point(); | |
77 if (result == NULL) { | |
78 goto done; | |
79 } | |
80 | |
81 prime = m_malloc(sizeof(*prime)); | |
82 m_mp_init(prime); | |
83 | |
84 if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) { | |
85 goto done; | |
86 } | |
87 if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) { | |
88 goto done; | |
89 } | |
90 | |
91 err = DROPBEAR_SUCCESS; | |
92 done: | |
93 if (err == DROPBEAR_SUCCESS) { | |
94 shared_secret = prime; | |
95 prime = NULL; | |
96 } | |
97 | |
98 if (prime) { | |
99 mp_clear(prime); | |
100 m_free(prime); | |
101 } | |
102 ltc_ecc_del_point(result); | |
103 | |
104 if (err == DROPBEAR_FAILURE) { | |
105 dropbear_exit("ECC error"); | |
106 } | |
107 | |
108 return shared_secret; | |
109 return err; | |
110 } | |
111 | |
112 } | |
16 | 113 |
17 #endif | 114 #endif |