Mercurial > dropbear
annotate rsa.c @ 641:2b1bb792cd4d dropbear-tfm
- Update tfm changes to current default tip
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 21 Nov 2011 19:52:28 +0800 |
parents | 76097ec1a29a a98a2138364a |
children | 33fd2f3499d2 |
rev | line source |
---|---|
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 /* |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 * Dropbear - a SSH2 server |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
3 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 * Copyright (c) 2002,2003 Matt Johnston |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
5 * All rights reserved. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
6 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 * Permission is hereby granted, free of charge, to any person obtaining a copy |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 * of this software and associated documentation files (the "Software"), to deal |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
9 * in the Software without restriction, including without limitation the rights |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 * copies of the Software, and to permit persons to whom the Software is |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
12 * furnished to do so, subject to the following conditions: |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
13 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
14 * The above copyright notice and this permission notice shall be included in |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
15 * all copies or substantial portions of the Software. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
16 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
23 * SOFTWARE. */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
24 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
25 /* Perform RSA operations on data, including reading keys, signing and |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
26 * verification. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
27 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
28 * The format is specified in rfc2437, Applied Cryptography or The Handbook of |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
29 * Applied Cryptography detail the general algorithm. */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
30 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
31 #include "includes.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
32 #include "dbutil.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
33 #include "bignum.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
34 #include "rsa.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
35 #include "buffer.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
36 #include "ssh.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
37 #include "random.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
38 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 #ifdef DROPBEAR_RSA |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
40 |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
41 static void rsa_pad_em(rsa_key * key, |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
42 const unsigned char * data, unsigned int len, |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
43 fp_int * rsa_em); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
44 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
45 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
46 /* Load a public rsa key from a buffer, initialising the values. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 * The key will have the same format as buf_put_rsa_key. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 * These should be freed with rsa_key_free. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 int buf_get_rsa_pub_key(buffer* buf, rsa_key *key) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
52 int ret = DROPBEAR_FAILURE; |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
53 TRACE(("enter buf_get_rsa_pub_key")) |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
54 dropbear_assert(key != NULL); |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
55 key->e = m_malloc(sizeof(fp_int)); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
56 key->n = m_malloc(sizeof(fp_int)); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
57 fp_init(key->e); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
58 fp_init(key->n); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
59 key->d = NULL; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
60 key->p = NULL; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
61 key->q = NULL; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
62 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
63 buf_incrpos(buf, 4+SSH_SIGNKEY_RSA_LEN); /* int + "ssh-rsa" */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
64 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
65 if (buf_getfpint(buf, key->e) == DROPBEAR_FAILURE |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
66 || buf_getfpint(buf, key->n) == DROPBEAR_FAILURE) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
67 TRACE(("leave buf_get_rsa_pub_key: failure")) |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
68 goto out; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
69 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
70 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
71 if (fp_count_bits(key->n) < MIN_RSA_KEYLEN) { |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
586
diff
changeset
|
72 dropbear_log(LOG_WARNING, "RSA key too short"); |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
73 goto out; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
74 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
75 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
76 TRACE(("leave buf_get_rsa_pub_key: success")) |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
77 ret = DROPBEAR_SUCCESS; |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
78 out: |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
79 if (ret == DROPBEAR_FAILURE) { |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
80 m_free(key->e); |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
81 m_free(key->n); |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
82 } |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
83 return ret; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
85 |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
86 /* Same as buf_get_rsa_pub_key, but reads private bits at the end. |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
87 * Loads a private rsa key from a buffer |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
88 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
89 int buf_get_rsa_priv_key(buffer* buf, rsa_key *key) { |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
90 int ret = DROPBEAR_FAILURE; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
91 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
92 TRACE(("enter buf_get_rsa_priv_key")) |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
93 dropbear_assert(key != NULL); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
94 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
95 if (buf_get_rsa_pub_key(buf, key) == DROPBEAR_FAILURE) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
96 TRACE(("leave buf_get_rsa_priv_key: pub: ret == DROPBEAR_FAILURE")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
97 return DROPBEAR_FAILURE; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
98 } |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
99 |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
100 key->d = NULL; |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
101 key->p = NULL; |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
102 key->q = NULL; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
103 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
104 key->d = m_malloc(sizeof(fp_int)); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
105 fp_init(key->d); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
106 if (buf_getfpint(buf, key->d) == DROPBEAR_FAILURE) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
107 TRACE(("leave buf_get_rsa_priv_key: d: ret == DROPBEAR_FAILURE")) |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
108 goto out; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
109 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
110 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
111 if (buf->pos == buf->len) { |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
112 /* old Dropbear private keys didn't keep p and q, so we will ignore them*/ |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
113 } else { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
114 key->p = m_malloc(sizeof(fp_int)); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
115 key->q = m_malloc(sizeof(fp_int)); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
116 fp_init(key->p); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
117 fp_init(key->q); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
118 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
119 if (buf_getfpint(buf, key->p) == DROPBEAR_FAILURE) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
120 TRACE(("leave buf_get_rsa_priv_key: p: ret == DROPBEAR_FAILURE")) |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
121 goto out; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
122 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
123 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
124 if (buf_getfpint(buf, key->q) == DROPBEAR_FAILURE) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
125 TRACE(("leave buf_get_rsa_priv_key: q: ret == DROPBEAR_FAILURE")) |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
126 goto out; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
127 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
128 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
129 |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
130 ret = DROPBEAR_SUCCESS; |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
131 out: |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
132 if (ret == DROPBEAR_FAILURE) { |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
133 m_free(key->d); |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
134 m_free(key->p); |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
135 m_free(key->q); |
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
136 } |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
137 TRACE(("leave buf_get_rsa_priv_key")) |
370
9a789fc03f40
Make sure that we clean up key parts if we fail during reading a rsa key
Matt Johnston <matt@ucc.asn.au>
parents:
276
diff
changeset
|
138 return ret; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
139 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
140 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
141 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
142 /* Clear and free the memory used by a public or private key */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
143 void rsa_key_free(rsa_key *key) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
144 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
145 TRACE(("enter rsa_key_free")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
146 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
147 if (key == NULL) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
148 TRACE(("leave rsa_key_free: key == NULL")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
149 return; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
150 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
151 if (key->d) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
152 fp_zero(key->d); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
153 m_free(key->d); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
154 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
155 if (key->e) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
156 fp_zero(key->e); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
157 m_free(key->e); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
158 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
159 if (key->n) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
160 fp_zero(key->n); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
161 m_free(key->n); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
162 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
163 if (key->p) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
164 fp_zero(key->p); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
165 m_free(key->p); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
166 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
167 if (key->q) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
168 fp_zero(key->q); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
169 m_free(key->q); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
170 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
171 m_free(key); |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
172 TRACE(("leave rsa_key_free")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
173 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
174 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
175 /* Put the public rsa key into the buffer in the required format: |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
176 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
177 * string "ssh-rsa" |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
178 * fp_int e |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
179 * fp_int n |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
180 */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
181 void buf_put_rsa_pub_key(buffer* buf, rsa_key *key) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
182 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
183 TRACE(("enter buf_put_rsa_pub_key")) |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
184 dropbear_assert(key != NULL); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
185 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
186 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN); |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
187 buf_putfpint(buf, key->e); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
188 buf_putfpint(buf, key->n); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
189 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
190 TRACE(("leave buf_put_rsa_pub_key")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
191 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
192 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
193 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
194 /* Same as buf_put_rsa_pub_key, but with the private "x" key appended */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
195 void buf_put_rsa_priv_key(buffer* buf, rsa_key *key) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
196 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
197 TRACE(("enter buf_put_rsa_priv_key")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
198 |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
199 dropbear_assert(key != NULL); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
200 buf_put_rsa_pub_key(buf, key); |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
201 buf_putfpint(buf, key->d); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
202 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
203 /* new versions have p and q, old versions don't */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
204 if (key->p) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
205 buf_putfpint(buf, key->p); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
206 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
207 if (key->q) { |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
208 buf_putfpint(buf, key->q); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
209 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
210 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
211 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
212 TRACE(("leave buf_put_rsa_priv_key")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
213 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
214 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
215 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
216 #ifdef DROPBEAR_SIGNKEY_VERIFY |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
217 /* Verify a signature in buf, made on data by the key given. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
218 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
219 int buf_rsa_verify(buffer * buf, rsa_key *key, const unsigned char* data, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
220 unsigned int len) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
221 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
222 unsigned int slen; |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
223 DEF_FP_INT(rsa_s); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
224 DEF_FP_INT(rsa_mdash); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
225 DEF_FP_INT(rsa_em); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
226 int ret = DROPBEAR_FAILURE; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
227 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
228 TRACE(("enter buf_rsa_verify")) |
34
e2a1eaa19f22
Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
229 |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
230 dropbear_assert(key != NULL); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
231 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
232 fp_init(&rsa_mdash); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
233 fp_init(&rsa_s); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
234 fp_init(&rsa_em); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
235 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
236 slen = buf_getint(buf); |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
237 if (slen != (unsigned int)fp_unsigned_bin_size(key->n)) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
238 TRACE(("bad size")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
239 goto out; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
240 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
241 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
242 fp_read_unsigned_bin(&rsa_s, buf_getptr(buf, buf->len - buf->pos), |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
243 buf->len - buf->pos); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
244 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
245 /* check that s <= n-1 */ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
246 if (fp_cmp(&rsa_s, key->n) != FP_LT) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
247 TRACE(("s > n-1")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
248 goto out; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
249 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
250 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
251 /* create the magic PKCS padded value */ |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
252 rsa_pad_em(key, data, len, &rsa_em); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
253 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
254 if (fp_exptmod(&rsa_s, key->e, key->n, &rsa_mdash) != FP_OKAY) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
255 TRACE(("failed exptmod rsa_s")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
256 goto out; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
257 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
258 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
259 if (fp_cmp(&rsa_em, &rsa_mdash) == FP_EQ) { |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
260 /* signature is valid */ |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
261 TRACE(("success!")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
262 ret = DROPBEAR_SUCCESS; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
263 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
264 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
265 out: |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
266 fp_zero(&rsa_mdash); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
267 fp_zero(&rsa_s); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
268 fp_zero(&rsa_em); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
269 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
270 TRACE(("leave buf_rsa_verify: ret %d", ret)) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
271 return ret; |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
272 } |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
273 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
274 #endif /* DROPBEAR_SIGNKEY_VERIFY */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
275 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
276 /* Sign the data presented with key, writing the signature contents |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
277 * to the buffer */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
278 void buf_put_rsa_sign(buffer* buf, rsa_key *key, const unsigned char* data, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
279 unsigned int len) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
280 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
281 unsigned int nsize, ssize; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
282 unsigned int i; |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
283 DEF_FP_INT(rsa_s); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
284 DEF_FP_INT(rsa_tmp1); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
285 DEF_FP_INT(rsa_tmp2); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
286 DEF_FP_INT(rsa_tmp3); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
287 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
288 TRACE(("enter buf_put_rsa_sign")) |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
289 dropbear_assert(key != NULL); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
290 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
291 fp_init(&rsa_s); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
292 fp_init(&rsa_tmp1); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
293 fp_init(&rsa_tmp2); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
294 fp_init(&rsa_tmp3); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
295 |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
296 rsa_pad_em(key, data, len, &rsa_tmp1); |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
297 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
298 /* the actual signing of the padded data */ |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
299 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
300 #ifdef RSA_BLINDING |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
301 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
302 /* With blinding, s = (r^(-1))((em)*r^e)^d mod n */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
303 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
304 /* generate the r blinding value */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
305 /* rsa_tmp2 is r */ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
306 gen_random_fpint(key->n, &rsa_tmp2); |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
307 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
308 /* rsa_tmp1 is em */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
309 /* em' = em * r^e mod n */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
310 |
340 | 311 /* rsa_s used as a temp var*/ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
312 if (fp_exptmod(&rsa_tmp2, key->e, key->n, &rsa_s) != FP_OKAY) { |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
586
diff
changeset
|
313 dropbear_exit("RSA error"); |
340 | 314 } |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
315 if (fp_invmod(&rsa_tmp2, key->n, &rsa_tmp3) != FP_OKAY) { |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
586
diff
changeset
|
316 dropbear_exit("RSA error"); |
340 | 317 } |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
318 if (fp_mulmod(&rsa_tmp1, &rsa_s, key->n, &rsa_tmp2) != FP_OKAY) { |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
586
diff
changeset
|
319 dropbear_exit("RSA error"); |
340 | 320 } |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
321 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
322 /* rsa_tmp2 is em' */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
323 /* s' = (em')^d mod n */ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
324 if (fp_exptmod(&rsa_tmp2, key->d, key->n, &rsa_tmp1) != FP_OKAY) { |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
586
diff
changeset
|
325 dropbear_exit("RSA error"); |
340 | 326 } |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
327 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
328 /* rsa_tmp1 is s' */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
329 /* rsa_tmp3 is r^(-1) mod n */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
330 /* s = (s')r^(-1) mod n */ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
331 if (fp_mulmod(&rsa_tmp1, &rsa_tmp3, key->n, &rsa_s) != FP_OKAY) { |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
586
diff
changeset
|
332 dropbear_exit("RSA error"); |
340 | 333 } |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
334 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
335 #else |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
336 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
337 /* s = em^d mod n */ |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
338 /* rsa_tmp1 is em */ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
339 if (fp_exptmod(&rsa_tmp1, key->d, key->n, &rsa_s) != FP_OKAY) { |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
586
diff
changeset
|
340 dropbear_exit("RSA error"); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
341 } |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
342 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
343 #endif /* RSA_BLINDING */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
344 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
345 fp_zero(&rsa_tmp1); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
346 fp_zero(&rsa_tmp2); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
347 fp_zero(&rsa_tmp3); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
348 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
349 /* create the signature to return */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
350 buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
351 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
352 nsize = fp_unsigned_bin_size(key->n); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
353 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
354 /* string rsa_signature_blob length */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
355 buf_putint(buf, nsize); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
356 /* pad out s to same length as n */ |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
357 ssize = fp_unsigned_bin_size(&rsa_s); |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
358 dropbear_assert(ssize <= nsize); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
359 for (i = 0; i < nsize-ssize; i++) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
360 buf_putbyte(buf, 0x00); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
361 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
362 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
363 fp_to_unsigned_bin(&rsa_s, buf_getwriteptr(buf, ssize)); |
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
364 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
365 buf_incrwritepos(buf, ssize); |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
366 fp_zero(&rsa_s); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
367 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
368 #if defined(DEBUG_RSA) && defined(DEBUG_TRACE) |
198
65585699d980
* add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
369 printhex("RSA sig", buf->data, buf->len); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
370 #endif |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
371 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
372 |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
84
diff
changeset
|
373 TRACE(("leave buf_put_rsa_sign")) |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
374 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
375 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
376 /* Creates the message value as expected by PKCS, see rfc2437 etc */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
377 /* format to be padded to is: |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
378 * EM = 01 | FF* | 00 | prefix | hash |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
379 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
380 * where FF is repeated enough times to make EM one byte |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
381 * shorter than the size of key->n |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
382 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
383 * prefix is the ASN1 designator prefix, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
384 * hex 30 21 30 09 06 05 2B 0E 03 02 1A 05 00 04 14 |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
385 * |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
386 * rsa_em must be a pointer to an initialised fp_int. |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
387 */ |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
388 static void rsa_pad_em(rsa_key * key, |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
389 const unsigned char * data, unsigned int len, |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
390 fp_int * rsa_em) { |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
391 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
392 /* ASN1 designator (including the 0x00 preceding) */ |
198
65585699d980
* add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
393 const unsigned char rsa_asn1_magic[] = |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
394 {0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
395 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14}; |
198
65585699d980
* add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
396 const unsigned int RSA_ASN1_MAGIC_LEN = 16; |
65585699d980
* add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents:
188
diff
changeset
|
397 |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
34
diff
changeset
|
398 buffer * rsa_EM = NULL; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
399 hash_state hs; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
400 unsigned int nsize; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
401 |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
402 dropbear_assert(key != NULL); |
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
403 dropbear_assert(data != NULL); |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
404 nsize = fp_unsigned_bin_size(key->n); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
405 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
406 rsa_EM = buf_new(nsize-1); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
407 /* type byte */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
408 buf_putbyte(rsa_EM, 0x01); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
409 /* Padding with 0xFF bytes */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
410 while(rsa_EM->pos != rsa_EM->size - RSA_ASN1_MAGIC_LEN - SHA1_HASH_SIZE) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
411 buf_putbyte(rsa_EM, 0xff); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
412 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
413 /* Magic ASN1 stuff */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
414 memcpy(buf_getwriteptr(rsa_EM, RSA_ASN1_MAGIC_LEN), |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
415 rsa_asn1_magic, RSA_ASN1_MAGIC_LEN); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
416 buf_incrwritepos(rsa_EM, RSA_ASN1_MAGIC_LEN); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
417 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
418 /* The hash of the data */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
419 sha1_init(&hs); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
420 sha1_process(&hs, data, len); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
421 sha1_done(&hs, buf_getwriteptr(rsa_EM, SHA1_HASH_SIZE)); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
422 buf_incrwritepos(rsa_EM, SHA1_HASH_SIZE); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
423 |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
216
diff
changeset
|
424 dropbear_assert(rsa_EM->pos == rsa_EM->size); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
425 |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
426 /* Create the fp_int from the encoded bytes */ |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
427 buf_setpos(rsa_EM, 0); |
640
76097ec1a29a
- Bring in original tomsfastmath patch against 0.52 from Peter Turczak
Matt Johnston <matt@ucc.asn.au>
parents:
378
diff
changeset
|
428 bytes_to_fp(rsa_em, buf_getptr(rsa_EM, rsa_EM->size), |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
429 rsa_EM->size); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
430 buf_free(rsa_EM); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
431 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
432 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
433 #endif /* DROPBEAR_RSA */ |