blob: 012a357178e23e4cd2649d9c67549c70ee210357 [file] [log] [blame]
Doug Zongker35d9ad52012-07-25 12:08:33 -07001/* rsa_e_3.c
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of Google Inc. nor the names of its contributors may
13** be used to endorse or promote products derived from this software
14** without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
17** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include "mincrypt/rsa.h"
29#include "mincrypt/sha.h"
Doug Zongker515e1632013-04-10 09:22:02 -070030#include "mincrypt/sha256.h"
Doug Zongker35d9ad52012-07-25 12:08:33 -070031
32/* a[] -= mod */
33static void subM(const RSAPublicKey *key, uint32_t *a) {
34 int64_t A = 0;
35 int i;
36 for (i = 0; i < key->len; ++i) {
37 A += (uint64_t)a[i] - key->n[i];
38 a[i] = (uint32_t)A;
39 A >>= 32;
40 }
41}
42
43/* return a[] >= mod */
44static int geM(const RSAPublicKey *key, const uint32_t *a) {
45 int i;
46 for (i = key->len; i;) {
47 --i;
48 if (a[i] < key->n[i]) return 0;
49 if (a[i] > key->n[i]) return 1;
50 }
51 return 1; /* equal */
52}
53
54/* montgomery c[] += a * b[] / R % mod */
55static void montMulAdd(const RSAPublicKey *key,
56 uint32_t* c,
57 const uint32_t a,
58 const uint32_t* b) {
59 uint64_t A = (uint64_t)a * b[0] + c[0];
60 uint32_t d0 = (uint32_t)A * key->n0inv;
61 uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
62 int i;
63
64 for (i = 1; i < key->len; ++i) {
65 A = (A >> 32) + (uint64_t)a * b[i] + c[i];
66 B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
67 c[i - 1] = (uint32_t)B;
68 }
69
70 A = (A >> 32) + (B >> 32);
71
72 c[i - 1] = (uint32_t)A;
73
74 if (A >> 32) {
75 subM(key, c);
76 }
77}
78
79/* montgomery c[] = a[] * b[] / R % mod */
80static void montMul(const RSAPublicKey *key,
81 uint32_t* c,
82 const uint32_t* a,
83 const uint32_t* b) {
84 int i;
85 for (i = 0; i < key->len; ++i) {
86 c[i] = 0;
87 }
88 for (i = 0; i < key->len; ++i) {
89 montMulAdd(key, c, a[i], b);
90 }
91}
92
93/* In-place public exponentiation.
94** Input and output big-endian byte array in inout.
95*/
96static void modpow3(const RSAPublicKey *key,
97 uint8_t* inout) {
98 uint32_t a[RSANUMWORDS];
99 uint32_t aR[RSANUMWORDS];
100 uint32_t aaR[RSANUMWORDS];
101 uint32_t *aaa = aR; /* Re-use location. */
102 int i;
103
104 /* Convert from big endian byte array to little endian word array. */
105 for (i = 0; i < key->len; ++i) {
106 uint32_t tmp =
107 (inout[((key->len - 1 - i) * 4) + 0] << 24) |
108 (inout[((key->len - 1 - i) * 4) + 1] << 16) |
109 (inout[((key->len - 1 - i) * 4) + 2] << 8) |
110 (inout[((key->len - 1 - i) * 4) + 3] << 0);
111 a[i] = tmp;
112 }
113
114 montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
115 montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
116 montMul(key, aaa, aaR, a); /* aaa = aaR * a / R mod M */
117
118 /* Make sure aaa < mod; aaa is at most 1x mod too large. */
119 if (geM(key, aaa)) {
120 subM(key, aaa);
121 }
122
123 /* Convert to bigendian byte array */
124 for (i = key->len - 1; i >= 0; --i) {
125 uint32_t tmp = aaa[i];
126 *inout++ = tmp >> 24;
127 *inout++ = tmp >> 16;
128 *inout++ = tmp >> 8;
129 *inout++ = tmp >> 0;
130 }
131}
132
133/* Expected PKCS1.5 signature padding bytes, for a keytool RSA signature.
134** Has the 0-length optional parameter encoded in the ASN1 (as opposed to the
135** other flavor which omits the optional parameter entirely). This code does not
136** accept signatures without the optional parameter.
137*/
Doug Zongker515e1632013-04-10 09:22:02 -0700138static const uint8_t sha_padding[RSANUMBYTES - SHA_DIGEST_SIZE] = {
Doug Zongker35d9ad52012-07-25 12:08:33 -0700139 0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
140 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
141 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
142 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
143 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
144 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
145 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
146 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
147 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
148 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
149 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
150 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
151 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
152 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
153 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
154 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
155 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,
156 0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05,0x00,
157 0x04,0x14
158};
159
Doug Zongker515e1632013-04-10 09:22:02 -0700160static const uint8_t sha256_padding[RSANUMBYTES - SHA256_DIGEST_SIZE] = {
161 0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
162 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
163 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
164 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
165 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
166
167 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
168 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
169 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
170 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
171 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
172
173 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
174 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
175 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
176 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
177 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
178
179 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x30,0x31,0x30,
180 0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,
181 0x00,0x04,0x20
182};
183
184
Doug Zongker35d9ad52012-07-25 12:08:33 -0700185/* Verify a 2048 bit RSA e=3 PKCS1.5 signature against an expected SHA-1 hash.
186** Returns 0 on failure, 1 on success.
187*/
188int RSA_e_3_verify(const RSAPublicKey *key,
189 const uint8_t *signature,
190 const int len,
Doug Zongker515e1632013-04-10 09:22:02 -0700191 const uint8_t *hash,
192 const int hash_len) {
Doug Zongker35d9ad52012-07-25 12:08:33 -0700193 uint8_t buf[RSANUMBYTES];
194 int i;
Doug Zongker515e1632013-04-10 09:22:02 -0700195 int padding_size;
196 const uint8_t* padding;
197
198 switch (hash_len) {
199 case SHA_DIGEST_SIZE:
200 padding = sha_padding;
201 padding_size = sizeof(sha_padding);
202 break;
203 case SHA256_DIGEST_SIZE:
204 padding = sha256_padding;
205 padding_size = sizeof(sha256_padding);
206 break;
207 default:
208 return 0; // unsupported hash
209 }
Doug Zongker35d9ad52012-07-25 12:08:33 -0700210
211 if (key->len != RSANUMWORDS) {
212 return 0; /* Wrong key passed in. */
213 }
214
215 if (len != sizeof(buf)) {
216 return 0; /* Wrong input length. */
217 }
218
219 if (key->exponent != 3) {
220 return 0; // Wrong exponent.
221 }
222
223 for (i = 0; i < len; ++i) {
224 buf[i] = signature[i];
225 }
226
227 modpow3(key, buf);
228
229 /* Check pkcs1.5 padding bytes. */
Doug Zongker515e1632013-04-10 09:22:02 -0700230 for (i = 0; i < padding_size; ++i) {
Doug Zongker35d9ad52012-07-25 12:08:33 -0700231 if (buf[i] != padding[i]) {
232 return 0;
233 }
234 }
235
236 /* Check sha digest matches. */
237 for (; i < len; ++i) {
Doug Zongker515e1632013-04-10 09:22:02 -0700238 if (buf[i] != *hash++) {
Doug Zongker35d9ad52012-07-25 12:08:33 -0700239 return 0;
240 }
241 }
242
243 return 1;
244}