Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / dropbear / rsa.c
blob7eb64131a499a7a0306cf9ad6c219b1592d9c494
1 /*
2 * Dropbear - a SSH2 server
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 /* Perform RSA operations on data, including reading keys, signing and
26 * verification.
28 * The format is specified in rfc2437, Applied Cryptography or The Handbook of
29 * Applied Cryptography detail the general algorithm. */
31 #include "includes.h"
32 #include "dbutil.h"
33 #include "bignum.h"
34 #include "rsa.h"
35 #include "buffer.h"
36 #include "ssh.h"
37 #include "dbrandom.h"
39 #ifdef DROPBEAR_RSA
41 static void rsa_pad_em(dropbear_rsa_key * key,
42 buffer *data_buf, mp_int * rsa_em);
44 /* Load a public rsa key from a buffer, initialising the values.
45 * The key will have the same format as buf_put_rsa_key.
46 * These should be freed with rsa_key_free.
47 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
48 int buf_get_rsa_pub_key(buffer* buf, dropbear_rsa_key *key) {
50 int ret = DROPBEAR_FAILURE;
51 TRACE(("enter buf_get_rsa_pub_key"))
52 dropbear_assert(key != NULL);
53 m_mp_alloc_init_multi(&key->e, &key->n, NULL);
54 key->d = NULL;
55 key->p = NULL;
56 key->q = NULL;
58 buf_incrpos(buf, 4+SSH_SIGNKEY_RSA_LEN); /* int + "ssh-rsa" */
60 if (buf_getmpint(buf, key->e) == DROPBEAR_FAILURE
61 || buf_getmpint(buf, key->n) == DROPBEAR_FAILURE) {
62 TRACE(("leave buf_get_rsa_pub_key: failure"))
63 goto out;
66 if (mp_count_bits(key->n) < MIN_RSA_KEYLEN) {
67 dropbear_log(LOG_WARNING, "RSA key too short");
68 goto out;
71 TRACE(("leave buf_get_rsa_pub_key: success"))
72 ret = DROPBEAR_SUCCESS;
73 out:
74 if (ret == DROPBEAR_FAILURE) {
75 m_free(key->e);
76 m_free(key->n);
78 return ret;
81 /* Same as buf_get_rsa_pub_key, but reads private bits at the end.
82 * Loads a private rsa key from a buffer
83 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
84 int buf_get_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) {
85 int ret = DROPBEAR_FAILURE;
87 TRACE(("enter buf_get_rsa_priv_key"))
88 dropbear_assert(key != NULL);
90 if (buf_get_rsa_pub_key(buf, key) == DROPBEAR_FAILURE) {
91 TRACE(("leave buf_get_rsa_priv_key: pub: ret == DROPBEAR_FAILURE"))
92 return DROPBEAR_FAILURE;
95 key->d = NULL;
96 key->p = NULL;
97 key->q = NULL;
99 m_mp_alloc_init_multi(&key->d, NULL);
100 if (buf_getmpint(buf, key->d) == DROPBEAR_FAILURE) {
101 TRACE(("leave buf_get_rsa_priv_key: d: ret == DROPBEAR_FAILURE"))
102 goto out;
105 if (buf->pos == buf->len) {
106 /* old Dropbear private keys didn't keep p and q, so we will ignore them*/
107 } else {
108 m_mp_alloc_init_multi(&key->p, &key->q, NULL);
110 if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE) {
111 TRACE(("leave buf_get_rsa_priv_key: p: ret == DROPBEAR_FAILURE"))
112 goto out;
115 if (buf_getmpint(buf, key->q) == DROPBEAR_FAILURE) {
116 TRACE(("leave buf_get_rsa_priv_key: q: ret == DROPBEAR_FAILURE"))
117 goto out;
121 ret = DROPBEAR_SUCCESS;
122 out:
123 if (ret == DROPBEAR_FAILURE) {
124 m_free(key->d);
125 m_free(key->p);
126 m_free(key->q);
128 TRACE(("leave buf_get_rsa_priv_key"))
129 return ret;
133 /* Clear and free the memory used by a public or private key */
134 void rsa_key_free(dropbear_rsa_key *key) {
136 TRACE2(("enter rsa_key_free"))
138 if (key == NULL) {
139 TRACE2(("leave rsa_key_free: key == NULL"))
140 return;
142 if (key->d) {
143 mp_clear(key->d);
144 m_free(key->d);
146 if (key->e) {
147 mp_clear(key->e);
148 m_free(key->e);
150 if (key->n) {
151 mp_clear(key->n);
152 m_free(key->n);
154 if (key->p) {
155 mp_clear(key->p);
156 m_free(key->p);
158 if (key->q) {
159 mp_clear(key->q);
160 m_free(key->q);
162 m_free(key);
163 TRACE2(("leave rsa_key_free"))
166 /* Put the public rsa key into the buffer in the required format:
168 * string "ssh-rsa"
169 * mp_int e
170 * mp_int n
172 void buf_put_rsa_pub_key(buffer* buf, dropbear_rsa_key *key) {
174 TRACE(("enter buf_put_rsa_pub_key"))
175 dropbear_assert(key != NULL);
177 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN);
178 buf_putmpint(buf, key->e);
179 buf_putmpint(buf, key->n);
181 TRACE(("leave buf_put_rsa_pub_key"))
185 /* Same as buf_put_rsa_pub_key, but with the private "x" key appended */
186 void buf_put_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) {
188 TRACE(("enter buf_put_rsa_priv_key"))
190 dropbear_assert(key != NULL);
191 buf_put_rsa_pub_key(buf, key);
192 buf_putmpint(buf, key->d);
194 /* new versions have p and q, old versions don't */
195 if (key->p) {
196 buf_putmpint(buf, key->p);
198 if (key->q) {
199 buf_putmpint(buf, key->q);
203 TRACE(("leave buf_put_rsa_priv_key"))
207 #ifdef DROPBEAR_SIGNKEY_VERIFY
208 /* Verify a signature in buf, made on data by the key given.
209 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
210 int buf_rsa_verify(buffer * buf, dropbear_rsa_key *key, buffer *data_buf) {
211 unsigned int slen;
212 DEF_MP_INT(rsa_s);
213 DEF_MP_INT(rsa_mdash);
214 DEF_MP_INT(rsa_em);
215 int ret = DROPBEAR_FAILURE;
217 TRACE(("enter buf_rsa_verify"))
219 dropbear_assert(key != NULL);
221 m_mp_init_multi(&rsa_mdash, &rsa_s, &rsa_em, NULL);
223 slen = buf_getint(buf);
224 if (slen != (unsigned int)mp_unsigned_bin_size(key->n)) {
225 TRACE(("bad size"))
226 goto out;
229 if (mp_read_unsigned_bin(&rsa_s, buf_getptr(buf, buf->len - buf->pos),
230 buf->len - buf->pos) != MP_OKAY) {
231 TRACE(("failed reading rsa_s"))
232 goto out;
235 /* check that s <= n-1 */
236 if (mp_cmp(&rsa_s, key->n) != MP_LT) {
237 TRACE(("s > n-1"))
238 goto out;
241 /* create the magic PKCS padded value */
242 rsa_pad_em(key, data_buf, &rsa_em);
244 if (mp_exptmod(&rsa_s, key->e, key->n, &rsa_mdash) != MP_OKAY) {
245 TRACE(("failed exptmod rsa_s"))
246 goto out;
249 if (mp_cmp(&rsa_em, &rsa_mdash) == MP_EQ) {
250 /* signature is valid */
251 TRACE(("success!"))
252 ret = DROPBEAR_SUCCESS;
255 out:
256 mp_clear_multi(&rsa_mdash, &rsa_s, &rsa_em, NULL);
257 TRACE(("leave buf_rsa_verify: ret %d", ret))
258 return ret;
261 #endif /* DROPBEAR_SIGNKEY_VERIFY */
263 /* Sign the data presented with key, writing the signature contents
264 * to the buffer */
265 void buf_put_rsa_sign(buffer* buf, dropbear_rsa_key *key, buffer *data_buf) {
266 unsigned int nsize, ssize;
267 unsigned int i;
268 DEF_MP_INT(rsa_s);
269 DEF_MP_INT(rsa_tmp1);
270 DEF_MP_INT(rsa_tmp2);
271 DEF_MP_INT(rsa_tmp3);
273 TRACE(("enter buf_put_rsa_sign"))
274 dropbear_assert(key != NULL);
276 m_mp_init_multi(&rsa_s, &rsa_tmp1, &rsa_tmp2, &rsa_tmp3, NULL);
278 rsa_pad_em(key, data_buf, &rsa_tmp1);
280 /* the actual signing of the padded data */
282 #ifdef RSA_BLINDING
284 /* With blinding, s = (r^(-1))((em)*r^e)^d mod n */
286 /* generate the r blinding value */
287 /* rsa_tmp2 is r */
288 gen_random_mpint(key->n, &rsa_tmp2);
290 /* rsa_tmp1 is em */
291 /* em' = em * r^e mod n */
293 /* rsa_s used as a temp var*/
294 if (mp_exptmod(&rsa_tmp2, key->e, key->n, &rsa_s) != MP_OKAY) {
295 dropbear_exit("RSA error");
297 if (mp_invmod(&rsa_tmp2, key->n, &rsa_tmp3) != MP_OKAY) {
298 dropbear_exit("RSA error");
300 if (mp_mulmod(&rsa_tmp1, &rsa_s, key->n, &rsa_tmp2) != MP_OKAY) {
301 dropbear_exit("RSA error");
304 /* rsa_tmp2 is em' */
305 /* s' = (em')^d mod n */
306 if (mp_exptmod(&rsa_tmp2, key->d, key->n, &rsa_tmp1) != MP_OKAY) {
307 dropbear_exit("RSA error");
310 /* rsa_tmp1 is s' */
311 /* rsa_tmp3 is r^(-1) mod n */
312 /* s = (s')r^(-1) mod n */
313 if (mp_mulmod(&rsa_tmp1, &rsa_tmp3, key->n, &rsa_s) != MP_OKAY) {
314 dropbear_exit("RSA error");
317 #else
319 /* s = em^d mod n */
320 /* rsa_tmp1 is em */
321 if (mp_exptmod(&rsa_tmp1, key->d, key->n, &rsa_s) != MP_OKAY) {
322 dropbear_exit("RSA error");
325 #endif /* RSA_BLINDING */
327 mp_clear_multi(&rsa_tmp1, &rsa_tmp2, &rsa_tmp3, NULL);
329 /* create the signature to return */
330 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN);
332 nsize = mp_unsigned_bin_size(key->n);
334 /* string rsa_signature_blob length */
335 buf_putint(buf, nsize);
336 /* pad out s to same length as n */
337 ssize = mp_unsigned_bin_size(&rsa_s);
338 dropbear_assert(ssize <= nsize);
339 for (i = 0; i < nsize-ssize; i++) {
340 buf_putbyte(buf, 0x00);
343 if (mp_to_unsigned_bin(&rsa_s, buf_getwriteptr(buf, ssize)) != MP_OKAY) {
344 dropbear_exit("RSA error");
346 buf_incrwritepos(buf, ssize);
347 mp_clear(&rsa_s);
349 #if defined(DEBUG_RSA) && defined(DEBUG_TRACE)
350 printhex("RSA sig", buf->data, buf->len);
351 #endif
354 TRACE(("leave buf_put_rsa_sign"))
357 /* Creates the message value as expected by PKCS, see rfc2437 etc */
358 /* format to be padded to is:
359 * EM = 01 | FF* | 00 | prefix | hash
361 * where FF is repeated enough times to make EM one byte
362 * shorter than the size of key->n
364 * prefix is the ASN1 designator prefix,
365 * hex 30 21 30 09 06 05 2B 0E 03 02 1A 05 00 04 14
367 * rsa_em must be a pointer to an initialised mp_int.
369 static void rsa_pad_em(dropbear_rsa_key * key,
370 buffer *data_buf, mp_int * rsa_em) {
372 /* ASN1 designator (including the 0x00 preceding) */
373 const unsigned char rsa_asn1_magic[] =
374 {0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b,
375 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14};
376 const unsigned int RSA_ASN1_MAGIC_LEN = 16;
378 buffer * rsa_EM = NULL;
379 hash_state hs;
380 unsigned int nsize;
382 dropbear_assert(key != NULL);
383 nsize = mp_unsigned_bin_size(key->n);
385 rsa_EM = buf_new(nsize-1);
386 /* type byte */
387 buf_putbyte(rsa_EM, 0x01);
388 /* Padding with 0xFF bytes */
389 while(rsa_EM->pos != rsa_EM->size - RSA_ASN1_MAGIC_LEN - SHA1_HASH_SIZE) {
390 buf_putbyte(rsa_EM, 0xff);
392 /* Magic ASN1 stuff */
393 memcpy(buf_getwriteptr(rsa_EM, RSA_ASN1_MAGIC_LEN),
394 rsa_asn1_magic, RSA_ASN1_MAGIC_LEN);
395 buf_incrwritepos(rsa_EM, RSA_ASN1_MAGIC_LEN);
397 /* The hash of the data */
398 sha1_init(&hs);
399 sha1_process(&hs, data_buf->data, data_buf->len);
400 sha1_done(&hs, buf_getwriteptr(rsa_EM, SHA1_HASH_SIZE));
401 buf_incrwritepos(rsa_EM, SHA1_HASH_SIZE);
403 dropbear_assert(rsa_EM->pos == rsa_EM->size);
405 /* Create the mp_int from the encoded bytes */
406 buf_setpos(rsa_EM, 0);
407 bytes_to_mp(rsa_em, buf_getptr(rsa_EM, rsa_EM->size),
408 rsa_EM->size);
409 buf_free(rsa_EM);
412 #endif /* DROPBEAR_RSA */