blob: 79eb61b9138fbba1fe6bc483fa014272b3642e7b [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Ricardo Salvetia2d5b1a2017-01-18 11:41:39 -020020#include <string.h>
21
Fabio Utzigba1fbe62017-07-21 14:01:20 -030022#ifdef MCUBOOT_MYNEWT
23#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030024#endif
25
Fabio Utzig19356bf2017-05-11 16:19:36 -030026#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -080027#include "bootutil/sign_key.h"
David Brownf4e904d2017-05-31 13:21:39 -060028#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080029
30#include "mbedtls/rsa.h"
31#include "mbedtls/asn1.h"
32
33#include "bootutil_priv.h"
34
David Brownf4e904d2017-05-31 13:21:39 -060035/*
36 * Constants for this particular constrained implementation of
37 * RSA-PSS. In particular, we support RSA 2048, with a SHA256 hash,
38 * and a 32-byte salt. A signature with different parameters will be
39 * rejected as invalid.
40 */
41
42/* The size, in octets, of the message. */
43#define PSS_EMLEN 256
44
45/* The size of the hash function. For SHA256, this is 32 bytes. */
46#define PSS_HLEN 32
47
48/* Size of the salt, should be fixed. */
49#define PSS_SLEN 32
50
51/* The length of the mask: emLen - hLen - 1. */
52#define PSS_MASK_LEN (256 - PSS_HLEN - 1)
53
54#define PSS_HASH_OFFSET PSS_MASK_LEN
55
56/* For the mask itself, how many bytes should be all zeros. */
57#define PSS_MASK_ZERO_COUNT (PSS_MASK_LEN - PSS_SLEN - 1)
58#define PSS_MASK_ONE_POS PSS_MASK_ZERO_COUNT
59
60/* Where the salt starts. */
61#define PSS_MASK_SALT_POS (PSS_MASK_ONE_POS + 1)
62
63static const uint8_t pss_zeros[8] = {0};
Christopher Collins92ea77f2016-12-12 15:59:26 -080064
65/*
66 * Parse the public key used for signing. Simple RSA format.
67 */
68static int
69bootutil_parse_rsakey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end)
70{
71 int rc;
72 size_t len;
73
74 if ((rc = mbedtls_asn1_get_tag(p, end, &len,
75 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
76 return -1;
77 }
78
79 if (*p + len != end) {
80 return -2;
81 }
82
83 if ((rc = mbedtls_asn1_get_mpi(p, end, &ctx->N)) != 0 ||
84 (rc = mbedtls_asn1_get_mpi(p, end, &ctx->E)) != 0) {
85 return -3;
86 }
87
David Brown785dc4c2018-02-13 14:31:24 -070088 ctx->len = mbedtls_mpi_size(&ctx->N);
89
Christopher Collins92ea77f2016-12-12 15:59:26 -080090 if (*p != end) {
91 return -4;
92 }
93
94 if ((rc = mbedtls_rsa_check_pubkey(ctx)) != 0) {
95 return -5;
96 }
97
98 ctx->len = mbedtls_mpi_size(&ctx->N);
99
100 return 0;
101}
102
David Brownf4e904d2017-05-31 13:21:39 -0600103/*
104 * Compute the RSA-PSS mask-generation function, MGF1. Assumptions
105 * are that the mask length will be less than 256 * PSS_HLEN, and
106 * therefore we never need to increment anything other than the low
107 * byte of the counter.
108 *
109 * This is described in PKCS#1, B.2.1.
110 */
111static void
112pss_mgf1(uint8_t *mask, const uint8_t *hash)
113{
114 bootutil_sha256_context ctx;
115 uint8_t counter[4] = { 0, 0, 0, 0 };
116 uint8_t htmp[PSS_HLEN];
117 int count = PSS_MASK_LEN;
118 int bytes;
119
120 while (count > 0) {
121 bootutil_sha256_init(&ctx);
122 bootutil_sha256_update(&ctx, hash, PSS_HLEN);
123 bootutil_sha256_update(&ctx, counter, 4);
124 bootutil_sha256_finish(&ctx, htmp);
125
126 counter[3]++;
127
128 bytes = PSS_HLEN;
129 if (bytes > count)
130 bytes = count;
131
132 memcpy(mask, htmp, bytes);
133 mask += bytes;
134 count -= bytes;
135 }
136}
137
138/*
139 * Validate an RSA signature, using RSA-PSS, as described in PKCS #1
140 * v2.2, section 9.1.2, with many parameters required to have fixed
141 * values.
142 */
143static int
144bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen,
145 uint8_t *sig)
146{
147 bootutil_sha256_context shactx;
148 uint8_t em[MBEDTLS_MPI_MAX_SIZE];
149 uint8_t db_mask[PSS_MASK_LEN];
150 uint8_t h2[PSS_HLEN];
151 int i;
152
David Browncdb968f2017-06-05 12:57:26 -0600153 if (ctx->len != PSS_EMLEN || PSS_EMLEN > MBEDTLS_MPI_MAX_SIZE) {
David Brownf4e904d2017-05-31 13:21:39 -0600154 return -1;
155 }
156
157 if (hlen != PSS_HLEN) {
158 return -1;
159 }
160
161 if (mbedtls_rsa_public(ctx, sig, em)) {
162 return -1;
163 }
164
165 /*
166 * PKCS #1 v2.2, 9.1.2 EMSA-PSS-Verify
167 *
168 * emBits is 2048
169 * emLen = ceil(emBits/8) = 256
170 *
171 * The salt length is not known at the beginning.
172 */
173
174 /* Step 1. The message is constrained by the address space of a
175 * 32-bit processor, which is far less than the 2^61-1 limit of
176 * SHA-256.
177 */
178
179 /* Step 2. mHash is passed in as 'hash', with hLen the hlen
180 * argument. */
181
182 /* Step 3. if emLen < hLen + sLen + 2, inconsistent and stop.
183 * The salt length is not known at this point.
184 */
185
186 /* Step 4. If the rightmost octect of EM does have the value
187 * 0xbc, output inconsistent and stop.
188 */
189 if (em[PSS_EMLEN - 1] != 0xbc) {
190 return -1;
191 }
192
193 /* Step 5. Let maskedDB be the leftmost emLen - hLen - 1 octets
194 * of EM, and H be the next hLen octets.
195 *
196 * maskedDB is then the first 256 - 32 - 1 = 0-222
197 * H is 32 bytes 223-254
198 */
199
200 /* Step 6. If the leftmost 8emLen - emBits bits of the leftmost
201 * octet in maskedDB are not all equal to zero, output
202 * inconsistent and stop.
203 *
204 * 8emLen - emBits is zero, so there is nothing to test here.
205 */
206
207 /* Step 7. let dbMask = MGF(H, emLen - hLen - 1). */
208 pss_mgf1(db_mask, &em[PSS_HASH_OFFSET]);
209
210 /* Step 8. let DB = maskedDB xor dbMask.
211 * To avoid needing an additional buffer, store the 'db' in the
212 * same buffer as db_mask. From now, to the end of this function,
213 * db_mask refers to the unmasked 'db'. */
214 for (i = 0; i < PSS_MASK_LEN; i++) {
215 db_mask[i] ^= em[i];
216 }
217
218 /* Step 9. Set the leftmost 8emLen - emBits bits of the leftmost
219 * octet in DB to zero.
220 * pycrypto seems to always make the emBits 2047, so we need to
221 * clear the top bit. */
222 db_mask[0] &= 0x7F;
223
224 /* Step 10. If the emLen - hLen - sLen - 2 leftmost octets of DB
225 * are not zero or if the octet at position emLen - hLen - sLen -
226 * 1 (the leftmost position is "position 1") does not have
227 * hexadecimal value 0x01, output "inconsistent" and stop. */
228 for (i = 0; i < PSS_MASK_ZERO_COUNT; i++) {
229 if (db_mask[i] != 0) {
230 return -1;
231 }
232 }
233
234 if (db_mask[PSS_MASK_ONE_POS] != 1) {
235 return -1;
236 }
237
238 /* Step 11. Let salt be the last sLen octets of DB */
239
240 /* Step 12. Let M' = 0x00 00 00 00 00 00 00 00 || mHash || salt; */
241
242 /* Step 13. Let H' = Hash(M') */
243 bootutil_sha256_init(&shactx);
244 bootutil_sha256_update(&shactx, pss_zeros, 8);
245 bootutil_sha256_update(&shactx, hash, PSS_HLEN);
246 bootutil_sha256_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN);
247 bootutil_sha256_finish(&shactx, h2);
248
249 /* Step 14. If H = H', output "consistent". Otherwise, output
250 * "inconsistent". */
251 if (memcmp(h2, &em[PSS_HASH_OFFSET], PSS_HLEN) != 0) {
252 return -1;
253 }
254
255 return 0;
256}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257
258int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200259bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260 uint8_t key_id)
261{
262 mbedtls_rsa_context ctx;
263 int rc;
264 uint8_t *cp;
265 uint8_t *end;
266
267 mbedtls_rsa_init(&ctx, 0, 0);
268
269 cp = (uint8_t *)bootutil_keys[key_id].key;
270 end = cp + *bootutil_keys[key_id].len;
271
272 rc = bootutil_parse_rsakey(&ctx, &cp, end);
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200273 if (rc || slen != ctx.len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274 mbedtls_rsa_free(&ctx);
275 return rc;
276 }
277 rc = bootutil_cmp_rsasig(&ctx, hash, hlen, sig);
278 mbedtls_rsa_free(&ctx);
279
280 return rc;
281}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300282#endif /* MCUBOOT_SIGN_RSA */