annotate ecdsa.c @ 767:e465ed10c51d ecc

Be safer with how we handle ltc_ecc_sets[] (particularly with system libtomcrypt) A bit of progress with ecdsa code
author Matt Johnston <matt@ucc.asn.au>
date Tue, 09 Apr 2013 22:44:19 +0800
parents d1575fdc29a6
children 70625eed40c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
766
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #include "includes.h"
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #include "dbutil.h"
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 #include "crypto_desc.h"
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
4 #include "ecc.h"
766
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 #ifdef DROPBEAR_ECDSA
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 ecc_key *gen_ecdsa_priv_key(unsigned int bit_size) {
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 const ltc_ecc_set_type *dp = NULL; // curve domain parameters
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 // TODO: use raw bytes for the dp rather than the hex strings in libtomcrypt's ecc.c
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 switch (bit_size) {
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #ifdef DROPBEAR_ECC_256
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 case 256:
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
14 dp = ecc_curve_nistp256.dp;
766
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 break;
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #endif
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 #ifdef DROPBEAR_ECC_384
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 case 384:
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
19 dp = ecc_curve_nistp384.dp;
766
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 break;
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 #endif
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 #ifdef DROPBEAR_ECC_521
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 case 521:
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
24 dp = ecc_curve_nistp521.dp;
766
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 break;
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #endif
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 }
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 if (!dp) {
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 dropbear_exit("Key size %d isn't valid. Try "
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #ifdef DROPBEAR_ECC_256
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 "256 "
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #endif
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #ifdef DROPBEAR_ECC_384
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 "384 "
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #endif
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #ifdef DROPBEAR_ECC_521
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 "521 "
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #endif
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 , bit_size);
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 }
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 ecc_key *new_key = m_malloc(sizeof(*new_key));
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 if (ecc_make_key_ex(NULL, dropbear_ltc_prng, new_key, dp) != CRYPT_OK) {
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 dropbear_exit("ECC error");
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 }
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 return new_key;
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 }
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
49 ecc_key *buf_get_ecdsa_pub_key(buffer* buf) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
50 unsigned char *key_ident = NULL, *identifier = NULL;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
51 unsigned int key_ident_len, identifier_len;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
52 buffer *q_buf = NULL;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
53 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
54 ecc_key *new_key = NULL;
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 // string "ecdsa-sha2-[identifier]"
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
57 key_ident = buf_getstring(buf, &key_ident_len);
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
58 // string "ecdsa-sha2-[identifier]"
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
59 identifier = buf_getstring(buf, &identifier_len);
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 if (key_ident_len != identifier_len + strlen("ecdsa-sha2-")) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
62 TRACE(("Bad identifier lengths"))
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
63 goto out;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
64 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
65 if (memcmp(&key_ident[strlen("ecdsa-sha2-")], identifier, identifier_len) != 0) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
66 TRACE(("mismatching identifiers"))
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
67 goto out;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
68 }
766
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
767
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
70 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
71 if (memcmp(identifier, (*curve)->name, strlen((*curve)->name)) == 0) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
72 break;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
73 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
74 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
75 if (!*curve) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
76 TRACE(("couldn't match ecc curve"))
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
77 goto out;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
78 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
79
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
80 // string Q
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
81 q_buf = buf_getstringbuf(buf);
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
82 new_key = buf_get_ecc_raw_pubkey(q_buf, *curve);
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
83
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
84 out:
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
85 if (key_ident) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
86 m_free(key_ident);
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
87 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
88 if (identifier) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
89 m_free(identifier);
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
90 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
91 if (q_buf) {
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
92 buf_free(q_buf);
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
93 q_buf = NULL;
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
94 }
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
95 TRACE(("leave buf_get_ecdsa_pub_key"))
e465ed10c51d Be safer with how we handle ltc_ecc_sets[] (particularly with
Matt Johnston <matt@ucc.asn.au>
parents: 766
diff changeset
96 return new_key;
766
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 }
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
d1575fdc29a6 start on ecdsa keys
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 #endif // DROPBEAR_ECDSA