comparison rsa.c @ 642:33fd2f3499d2 dropbear-tfm

A few build fixes
author Matt Johnston <matt@ucc.asn.au>
date Tue, 22 Nov 2011 19:28:58 +0700
parents 2b1bb792cd4d
children
comparison
equal deleted inserted replaced
641:2b1bb792cd4d 642:33fd2f3499d2
36 #include "ssh.h" 36 #include "ssh.h"
37 #include "random.h" 37 #include "random.h"
38 38
39 #ifdef DROPBEAR_RSA 39 #ifdef DROPBEAR_RSA
40 40
41 static void rsa_pad_em(rsa_key * key, 41 static void rsa_pad_em(dropbear_rsa_key * key,
42 const unsigned char * data, unsigned int len, 42 const unsigned char * data, unsigned int len,
43 fp_int * rsa_em); 43 fp_int * rsa_em);
44 44
45 45
46 /* Load a public rsa key from a buffer, initialising the values. 46 /* Load a public rsa key from a buffer, initialising the values.
47 * The key will have the same format as buf_put_rsa_key. 47 * The key will have the same format as buf_put_rsa_key.
48 * These should be freed with rsa_key_free. 48 * These should be freed with rsa_key_free.
49 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 49 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
50 int buf_get_rsa_pub_key(buffer* buf, rsa_key *key) { 50 int buf_get_rsa_pub_key(buffer* buf, dropbear_rsa_key *key) {
51 51
52 int ret = DROPBEAR_FAILURE; 52 int ret = DROPBEAR_FAILURE;
53 TRACE(("enter buf_get_rsa_pub_key")) 53 TRACE(("enter buf_get_rsa_pub_key"))
54 dropbear_assert(key != NULL); 54 dropbear_assert(key != NULL);
55 key->e = m_malloc(sizeof(fp_int)); 55 key->e = m_malloc(sizeof(fp_int));
56 key->n = m_malloc(sizeof(fp_int)); 56 key->n = m_malloc(sizeof(fp_int));
57 fp_init(key->e); 57 m_fp_init_multi(key->e, key->n, NULL);
58 fp_init(key->n);
59 key->d = NULL; 58 key->d = NULL;
60 key->p = NULL; 59 key->p = NULL;
61 key->q = NULL; 60 key->q = NULL;
62 61
63 buf_incrpos(buf, 4+SSH_SIGNKEY_RSA_LEN); /* int + "ssh-rsa" */ 62 buf_incrpos(buf, 4+SSH_SIGNKEY_RSA_LEN); /* int + "ssh-rsa" */
84 } 83 }
85 84
86 /* Same as buf_get_rsa_pub_key, but reads private bits at the end. 85 /* Same as buf_get_rsa_pub_key, but reads private bits at the end.
87 * Loads a private rsa key from a buffer 86 * Loads a private rsa key from a buffer
88 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 87 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
89 int buf_get_rsa_priv_key(buffer* buf, rsa_key *key) { 88 int buf_get_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) {
90 int ret = DROPBEAR_FAILURE; 89 int ret = DROPBEAR_FAILURE;
91 90
92 TRACE(("enter buf_get_rsa_priv_key")) 91 TRACE(("enter buf_get_rsa_priv_key"))
93 dropbear_assert(key != NULL); 92 dropbear_assert(key != NULL);
94 93
138 return ret; 137 return ret;
139 } 138 }
140 139
141 140
142 /* Clear and free the memory used by a public or private key */ 141 /* Clear and free the memory used by a public or private key */
143 void rsa_key_free(rsa_key *key) { 142 void rsa_key_free(dropbear_rsa_key *key) {
144 143
145 TRACE(("enter rsa_key_free")) 144 TRACE(("enter rsa_key_free"))
146 145
147 if (key == NULL) { 146 if (key == NULL) {
148 TRACE(("leave rsa_key_free: key == NULL")) 147 TRACE(("leave rsa_key_free: key == NULL"))
176 * 175 *
177 * string "ssh-rsa" 176 * string "ssh-rsa"
178 * fp_int e 177 * fp_int e
179 * fp_int n 178 * fp_int n
180 */ 179 */
181 void buf_put_rsa_pub_key(buffer* buf, rsa_key *key) { 180 void buf_put_rsa_pub_key(buffer* buf, dropbear_rsa_key *key) {
182 181
183 TRACE(("enter buf_put_rsa_pub_key")) 182 TRACE(("enter buf_put_rsa_pub_key"))
184 dropbear_assert(key != NULL); 183 dropbear_assert(key != NULL);
185 184
186 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN); 185 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN);
190 TRACE(("leave buf_put_rsa_pub_key")) 189 TRACE(("leave buf_put_rsa_pub_key"))
191 190
192 } 191 }
193 192
194 /* Same as buf_put_rsa_pub_key, but with the private "x" key appended */ 193 /* Same as buf_put_rsa_pub_key, but with the private "x" key appended */
195 void buf_put_rsa_priv_key(buffer* buf, rsa_key *key) { 194 void buf_put_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) {
196 195
197 TRACE(("enter buf_put_rsa_priv_key")) 196 TRACE(("enter buf_put_rsa_priv_key"))
198 197
199 dropbear_assert(key != NULL); 198 dropbear_assert(key != NULL);
200 buf_put_rsa_pub_key(buf, key); 199 buf_put_rsa_pub_key(buf, key);
214 } 213 }
215 214
216 #ifdef DROPBEAR_SIGNKEY_VERIFY 215 #ifdef DROPBEAR_SIGNKEY_VERIFY
217 /* Verify a signature in buf, made on data by the key given. 216 /* Verify a signature in buf, made on data by the key given.
218 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 217 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
219 int buf_rsa_verify(buffer * buf, rsa_key *key, const unsigned char* data, 218 int buf_rsa_verify(buffer * buf, dropbear_rsa_key *key, const unsigned char* data,
220 unsigned int len) { 219 unsigned int len) {
221 220
222 unsigned int slen; 221 unsigned int slen;
223 DEF_FP_INT(rsa_s); 222 DEF_FP_INT(rsa_s);
224 DEF_FP_INT(rsa_mdash); 223 DEF_FP_INT(rsa_mdash);
273 272
274 #endif /* DROPBEAR_SIGNKEY_VERIFY */ 273 #endif /* DROPBEAR_SIGNKEY_VERIFY */
275 274
276 /* Sign the data presented with key, writing the signature contents 275 /* Sign the data presented with key, writing the signature contents
277 * to the buffer */ 276 * to the buffer */
278 void buf_put_rsa_sign(buffer* buf, rsa_key *key, const unsigned char* data, 277 void buf_put_rsa_sign(buffer* buf, dropbear_rsa_key *key, const unsigned char* data,
279 unsigned int len) { 278 unsigned int len) {
280 279
281 unsigned int nsize, ssize; 280 unsigned int nsize, ssize;
282 unsigned int i; 281 unsigned int i;
283 DEF_FP_INT(rsa_s); 282 DEF_FP_INT(rsa_s);
340 dropbear_exit("RSA error"); 339 dropbear_exit("RSA error");
341 } 340 }
342 341
343 #endif /* RSA_BLINDING */ 342 #endif /* RSA_BLINDING */
344 343
345 fp_zero(&rsa_tmp1); 344 m_fp_zero_multi(&rsa_tmp1, &rsa_tmp2, &rsa_tmp3, NULL);
346 fp_zero(&rsa_tmp2);
347 fp_zero(&rsa_tmp3);
348 345
349 /* create the signature to return */ 346 /* create the signature to return */
350 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN); 347 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN);
351 348
352 nsize = fp_unsigned_bin_size(key->n); 349 nsize = fp_unsigned_bin_size(key->n);
383 * prefix is the ASN1 designator prefix, 380 * prefix is the ASN1 designator prefix,
384 * hex 30 21 30 09 06 05 2B 0E 03 02 1A 05 00 04 14 381 * hex 30 21 30 09 06 05 2B 0E 03 02 1A 05 00 04 14
385 * 382 *
386 * rsa_em must be a pointer to an initialised fp_int. 383 * rsa_em must be a pointer to an initialised fp_int.
387 */ 384 */
388 static void rsa_pad_em(rsa_key * key, 385 static void rsa_pad_em(dropbear_rsa_key * key,
389 const unsigned char * data, unsigned int len, 386 const unsigned char * data, unsigned int len,
390 fp_int * rsa_em) { 387 fp_int * rsa_em) {
391 388
392 /* ASN1 designator (including the 0x00 preceding) */ 389 /* ASN1 designator (including the 0x00 preceding) */
393 const unsigned char rsa_asn1_magic[] = 390 const unsigned char rsa_asn1_magic[] =