blob: 3e21a6db67ef50c9db6edded9e8709be72c1527e [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#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030023
Fabio Utzig19356bf2017-05-11 16:19:36 -030024#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "bootutil/sign_key.h"
David Brownf4e904d2017-05-31 13:21:39 -060026#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080027
28#include "mbedtls/rsa.h"
29#include "mbedtls/asn1.h"
Yiping Peng33939922018-09-30 15:06:53 +080030#include "mbedtls/version.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080031
32#include "bootutil_priv.h"
33
David Brownf4e904d2017-05-31 13:21:39 -060034/*
35 * Constants for this particular constrained implementation of
36 * RSA-PSS. In particular, we support RSA 2048, with a SHA256 hash,
37 * and a 32-byte salt. A signature with different parameters will be
38 * rejected as invalid.
39 */
40
41/* The size, in octets, of the message. */
Fabio Utzig3501c012019-05-13 15:07:25 -070042#define PSS_EMLEN (MCUBOOT_SIGN_RSA_LEN / 8)
David Brownf4e904d2017-05-31 13:21:39 -060043
44/* The size of the hash function. For SHA256, this is 32 bytes. */
45#define PSS_HLEN 32
46
47/* Size of the salt, should be fixed. */
48#define PSS_SLEN 32
49
50/* The length of the mask: emLen - hLen - 1. */
Fabio Utzig3501c012019-05-13 15:07:25 -070051#define PSS_MASK_LEN (PSS_EMLEN - PSS_HLEN - 1)
David Brownf4e904d2017-05-31 13:21:39 -060052
53#define PSS_HASH_OFFSET PSS_MASK_LEN
54
55/* For the mask itself, how many bytes should be all zeros. */
56#define PSS_MASK_ZERO_COUNT (PSS_MASK_LEN - PSS_SLEN - 1)
57#define PSS_MASK_ONE_POS PSS_MASK_ZERO_COUNT
58
59/* Where the salt starts. */
60#define PSS_MASK_SALT_POS (PSS_MASK_ONE_POS + 1)
61
62static const uint8_t pss_zeros[8] = {0};
Christopher Collins92ea77f2016-12-12 15:59:26 -080063
64/*
65 * Parse the public key used for signing. Simple RSA format.
66 */
67static int
68bootutil_parse_rsakey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end)
69{
70 int rc;
71 size_t len;
72
73 if ((rc = mbedtls_asn1_get_tag(p, end, &len,
74 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
75 return -1;
76 }
77
78 if (*p + len != end) {
79 return -2;
80 }
81
82 if ((rc = mbedtls_asn1_get_mpi(p, end, &ctx->N)) != 0 ||
83 (rc = mbedtls_asn1_get_mpi(p, end, &ctx->E)) != 0) {
84 return -3;
85 }
86
David Brown785dc4c2018-02-13 14:31:24 -070087 ctx->len = mbedtls_mpi_size(&ctx->N);
88
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 if (*p != end) {
90 return -4;
91 }
92
Yiping Peng33939922018-09-30 15:06:53 +080093 /* The mbedtls version is more than 2.6.1 */
94#if MBEDTLS_VERSION_NUMBER > 0x02060100
95 rc = mbedtls_rsa_import(ctx, &ctx->N, NULL, NULL, NULL, &ctx->E);
96 if (rc != 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -080097 return -5;
98 }
Yiping Peng33939922018-09-30 15:06:53 +080099#endif
100
101 rc = mbedtls_rsa_check_pubkey(ctx);
102 if (rc != 0) {
103 return -6;
104 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105
106 ctx->len = mbedtls_mpi_size(&ctx->N);
107
108 return 0;
109}
110
David Brownf4e904d2017-05-31 13:21:39 -0600111/*
112 * Compute the RSA-PSS mask-generation function, MGF1. Assumptions
113 * are that the mask length will be less than 256 * PSS_HLEN, and
114 * therefore we never need to increment anything other than the low
115 * byte of the counter.
116 *
117 * This is described in PKCS#1, B.2.1.
118 */
119static void
120pss_mgf1(uint8_t *mask, const uint8_t *hash)
121{
122 bootutil_sha256_context ctx;
123 uint8_t counter[4] = { 0, 0, 0, 0 };
124 uint8_t htmp[PSS_HLEN];
125 int count = PSS_MASK_LEN;
126 int bytes;
127
128 while (count > 0) {
129 bootutil_sha256_init(&ctx);
130 bootutil_sha256_update(&ctx, hash, PSS_HLEN);
131 bootutil_sha256_update(&ctx, counter, 4);
132 bootutil_sha256_finish(&ctx, htmp);
133
134 counter[3]++;
135
136 bytes = PSS_HLEN;
137 if (bytes > count)
138 bytes = count;
139
140 memcpy(mask, htmp, bytes);
141 mask += bytes;
142 count -= bytes;
143 }
144}
145
146/*
147 * Validate an RSA signature, using RSA-PSS, as described in PKCS #1
148 * v2.2, section 9.1.2, with many parameters required to have fixed
149 * values.
150 */
151static int
152bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen,
153 uint8_t *sig)
154{
155 bootutil_sha256_context shactx;
156 uint8_t em[MBEDTLS_MPI_MAX_SIZE];
157 uint8_t db_mask[PSS_MASK_LEN];
158 uint8_t h2[PSS_HLEN];
159 int i;
160
David Browncdb968f2017-06-05 12:57:26 -0600161 if (ctx->len != PSS_EMLEN || PSS_EMLEN > MBEDTLS_MPI_MAX_SIZE) {
David Brownf4e904d2017-05-31 13:21:39 -0600162 return -1;
163 }
164
165 if (hlen != PSS_HLEN) {
166 return -1;
167 }
168
169 if (mbedtls_rsa_public(ctx, sig, em)) {
170 return -1;
171 }
172
173 /*
174 * PKCS #1 v2.2, 9.1.2 EMSA-PSS-Verify
175 *
176 * emBits is 2048
177 * emLen = ceil(emBits/8) = 256
178 *
179 * The salt length is not known at the beginning.
180 */
181
182 /* Step 1. The message is constrained by the address space of a
183 * 32-bit processor, which is far less than the 2^61-1 limit of
184 * SHA-256.
185 */
186
187 /* Step 2. mHash is passed in as 'hash', with hLen the hlen
188 * argument. */
189
190 /* Step 3. if emLen < hLen + sLen + 2, inconsistent and stop.
191 * The salt length is not known at this point.
192 */
193
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300194 /* Step 4. If the rightmost octet of EM does have the value
David Brownf4e904d2017-05-31 13:21:39 -0600195 * 0xbc, output inconsistent and stop.
196 */
197 if (em[PSS_EMLEN - 1] != 0xbc) {
198 return -1;
199 }
200
201 /* Step 5. Let maskedDB be the leftmost emLen - hLen - 1 octets
202 * of EM, and H be the next hLen octets.
203 *
204 * maskedDB is then the first 256 - 32 - 1 = 0-222
205 * H is 32 bytes 223-254
206 */
207
208 /* Step 6. If the leftmost 8emLen - emBits bits of the leftmost
209 * octet in maskedDB are not all equal to zero, output
210 * inconsistent and stop.
211 *
212 * 8emLen - emBits is zero, so there is nothing to test here.
213 */
214
215 /* Step 7. let dbMask = MGF(H, emLen - hLen - 1). */
216 pss_mgf1(db_mask, &em[PSS_HASH_OFFSET]);
217
218 /* Step 8. let DB = maskedDB xor dbMask.
219 * To avoid needing an additional buffer, store the 'db' in the
220 * same buffer as db_mask. From now, to the end of this function,
221 * db_mask refers to the unmasked 'db'. */
222 for (i = 0; i < PSS_MASK_LEN; i++) {
223 db_mask[i] ^= em[i];
224 }
225
226 /* Step 9. Set the leftmost 8emLen - emBits bits of the leftmost
227 * octet in DB to zero.
228 * pycrypto seems to always make the emBits 2047, so we need to
229 * clear the top bit. */
230 db_mask[0] &= 0x7F;
231
232 /* Step 10. If the emLen - hLen - sLen - 2 leftmost octets of DB
233 * are not zero or if the octet at position emLen - hLen - sLen -
234 * 1 (the leftmost position is "position 1") does not have
235 * hexadecimal value 0x01, output "inconsistent" and stop. */
236 for (i = 0; i < PSS_MASK_ZERO_COUNT; i++) {
237 if (db_mask[i] != 0) {
238 return -1;
239 }
240 }
241
242 if (db_mask[PSS_MASK_ONE_POS] != 1) {
243 return -1;
244 }
245
246 /* Step 11. Let salt be the last sLen octets of DB */
247
248 /* Step 12. Let M' = 0x00 00 00 00 00 00 00 00 || mHash || salt; */
249
250 /* Step 13. Let H' = Hash(M') */
251 bootutil_sha256_init(&shactx);
252 bootutil_sha256_update(&shactx, pss_zeros, 8);
253 bootutil_sha256_update(&shactx, hash, PSS_HLEN);
254 bootutil_sha256_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN);
255 bootutil_sha256_finish(&shactx, h2);
256
257 /* Step 14. If H = H', output "consistent". Otherwise, output
258 * "inconsistent". */
259 if (memcmp(h2, &em[PSS_HASH_OFFSET], PSS_HLEN) != 0) {
260 return -1;
261 }
262
263 return 0;
264}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800265
266int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200267bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268 uint8_t key_id)
269{
270 mbedtls_rsa_context ctx;
271 int rc;
272 uint8_t *cp;
273 uint8_t *end;
274
275 mbedtls_rsa_init(&ctx, 0, 0);
276
277 cp = (uint8_t *)bootutil_keys[key_id].key;
278 end = cp + *bootutil_keys[key_id].len;
279
280 rc = bootutil_parse_rsakey(&ctx, &cp, end);
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200281 if (rc || slen != ctx.len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800282 mbedtls_rsa_free(&ctx);
283 return rc;
284 }
285 rc = bootutil_cmp_rsasig(&ctx, hash, hlen, sig);
286 mbedtls_rsa_free(&ctx);
287
288 return rc;
289}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300290#endif /* MCUBOOT_SIGN_RSA */