blob: 327d94f0179fd8350754c80af53a3425c7f7ac11 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2017-2018 Linaro LTD
5 * Copyright (c) 2017-2019 JUUL Labs
Roman Okhrimenko977b3752022-03-31 14:40:48 +03006 * Copyright (c) 2020-2021 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
Ricardo Salvetia2d5b1a2017-01-18 11:41:39 -020028#include <string.h>
29
Fabio Utzigba1fbe62017-07-21 14:01:20 -030030#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030031
Fabio Utzig19356bf2017-05-11 16:19:36 -030032#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include "bootutil/sign_key.h"
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090034#include "bootutil/crypto/sha256.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030035#include "bootutil/crypto/common.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080036
37#include "mbedtls/rsa.h"
38#include "mbedtls/asn1.h"
Yiping Peng33939922018-09-30 15:06:53 +080039#include "mbedtls/version.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080040
41#include "bootutil_priv.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010042#include "bootutil/fault_injection_hardening.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080043
David Brownf4e904d2017-05-31 13:21:39 -060044/*
45 * Constants for this particular constrained implementation of
46 * RSA-PSS. In particular, we support RSA 2048, with a SHA256 hash,
47 * and a 32-byte salt. A signature with different parameters will be
48 * rejected as invalid.
49 */
50
51/* The size, in octets, of the message. */
Fabio Utzig3501c012019-05-13 15:07:25 -070052#define PSS_EMLEN (MCUBOOT_SIGN_RSA_LEN / 8)
David Brownf4e904d2017-05-31 13:21:39 -060053
54/* The size of the hash function. For SHA256, this is 32 bytes. */
55#define PSS_HLEN 32
56
57/* Size of the salt, should be fixed. */
58#define PSS_SLEN 32
59
60/* The length of the mask: emLen - hLen - 1. */
Fabio Utzig3501c012019-05-13 15:07:25 -070061#define PSS_MASK_LEN (PSS_EMLEN - PSS_HLEN - 1)
David Brownf4e904d2017-05-31 13:21:39 -060062
63#define PSS_HASH_OFFSET PSS_MASK_LEN
64
65/* For the mask itself, how many bytes should be all zeros. */
66#define PSS_MASK_ZERO_COUNT (PSS_MASK_LEN - PSS_SLEN - 1)
67#define PSS_MASK_ONE_POS PSS_MASK_ZERO_COUNT
68
69/* Where the salt starts. */
70#define PSS_MASK_SALT_POS (PSS_MASK_ONE_POS + 1)
71
72static const uint8_t pss_zeros[8] = {0};
Christopher Collins92ea77f2016-12-12 15:59:26 -080073
74/*
75 * Parse the public key used for signing. Simple RSA format.
76 */
77static int
78bootutil_parse_rsakey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end)
79{
80 int rc;
81 size_t len;
82
83 if ((rc = mbedtls_asn1_get_tag(p, end, &len,
84 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
85 return -1;
86 }
87
88 if (*p + len != end) {
89 return -2;
90 }
91
Roman Okhrimenko977b3752022-03-31 14:40:48 +030092 if ((rc = mbedtls_asn1_get_mpi(p, end, &ctx->MBEDTLS_CONTEXT_MEMBER(N))) != 0 ||
93 (rc = mbedtls_asn1_get_mpi(p, end, &ctx->MBEDTLS_CONTEXT_MEMBER(E))) != 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 return -3;
95 }
96
Roman Okhrimenko977b3752022-03-31 14:40:48 +030097 ctx->MBEDTLS_CONTEXT_MEMBER(len) = mbedtls_mpi_size(&ctx->MBEDTLS_CONTEXT_MEMBER(N));
David Brown785dc4c2018-02-13 14:31:24 -070098
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 if (*p != end) {
100 return -4;
101 }
102
Yiping Peng33939922018-09-30 15:06:53 +0800103 /* The mbedtls version is more than 2.6.1 */
104#if MBEDTLS_VERSION_NUMBER > 0x02060100
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300105 rc = mbedtls_rsa_import(ctx, &ctx->MBEDTLS_CONTEXT_MEMBER(N), NULL,
106 NULL, NULL, &ctx->MBEDTLS_CONTEXT_MEMBER(E));
Yiping Peng33939922018-09-30 15:06:53 +0800107 if (rc != 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 return -5;
109 }
Yiping Peng33939922018-09-30 15:06:53 +0800110#endif
111
112 rc = mbedtls_rsa_check_pubkey(ctx);
113 if (rc != 0) {
114 return -6;
115 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300117 ctx->MBEDTLS_CONTEXT_MEMBER(len) = mbedtls_mpi_size(&ctx->MBEDTLS_CONTEXT_MEMBER(N));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118
119 return 0;
120}
121
David Brownf4e904d2017-05-31 13:21:39 -0600122/*
123 * Compute the RSA-PSS mask-generation function, MGF1. Assumptions
124 * are that the mask length will be less than 256 * PSS_HLEN, and
125 * therefore we never need to increment anything other than the low
126 * byte of the counter.
127 *
128 * This is described in PKCS#1, B.2.1.
129 */
130static void
131pss_mgf1(uint8_t *mask, const uint8_t *hash)
132{
133 bootutil_sha256_context ctx;
134 uint8_t counter[4] = { 0, 0, 0, 0 };
135 uint8_t htmp[PSS_HLEN];
136 int count = PSS_MASK_LEN;
137 int bytes;
138
139 while (count > 0) {
140 bootutil_sha256_init(&ctx);
141 bootutil_sha256_update(&ctx, hash, PSS_HLEN);
142 bootutil_sha256_update(&ctx, counter, 4);
143 bootutil_sha256_finish(&ctx, htmp);
144
145 counter[3]++;
146
147 bytes = PSS_HLEN;
148 if (bytes > count)
149 bytes = count;
150
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300151 (void)memcpy(mask, htmp, bytes);
David Brownf4e904d2017-05-31 13:21:39 -0600152 mask += bytes;
153 count -= bytes;
154 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900155
156 bootutil_sha256_drop(&ctx);
David Brownf4e904d2017-05-31 13:21:39 -0600157}
158
159/*
160 * Validate an RSA signature, using RSA-PSS, as described in PKCS #1
161 * v2.2, section 9.1.2, with many parameters required to have fixed
162 * values.
163 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100164static fih_int
David Brownf4e904d2017-05-31 13:21:39 -0600165bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen,
166 uint8_t *sig)
167{
168 bootutil_sha256_context shactx;
169 uint8_t em[MBEDTLS_MPI_MAX_SIZE];
170 uint8_t db_mask[PSS_MASK_LEN];
171 uint8_t h2[PSS_HLEN];
172 int i;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100173 int rc = 0;
174 fih_int fih_rc = FIH_FAILURE;
David Brownf4e904d2017-05-31 13:21:39 -0600175
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300176 if (ctx->MBEDTLS_CONTEXT_MEMBER(len) != PSS_EMLEN ||
177 PSS_EMLEN > MBEDTLS_MPI_MAX_SIZE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100178 rc = -1;
179 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600180 }
181
182 if (hlen != PSS_HLEN) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100183 rc = -1;
184 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600185 }
186
187 if (mbedtls_rsa_public(ctx, sig, em)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100188 rc = -1;
189 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600190 }
191
192 /*
193 * PKCS #1 v2.2, 9.1.2 EMSA-PSS-Verify
194 *
195 * emBits is 2048
196 * emLen = ceil(emBits/8) = 256
197 *
198 * The salt length is not known at the beginning.
199 */
200
201 /* Step 1. The message is constrained by the address space of a
202 * 32-bit processor, which is far less than the 2^61-1 limit of
203 * SHA-256.
204 */
205
206 /* Step 2. mHash is passed in as 'hash', with hLen the hlen
207 * argument. */
208
209 /* Step 3. if emLen < hLen + sLen + 2, inconsistent and stop.
210 * The salt length is not known at this point.
211 */
212
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300213 /* Step 4. If the rightmost octet of EM does have the value
David Brownf4e904d2017-05-31 13:21:39 -0600214 * 0xbc, output inconsistent and stop.
215 */
216 if (em[PSS_EMLEN - 1] != 0xbc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100217 rc = -1;
218 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600219 }
220
221 /* Step 5. Let maskedDB be the leftmost emLen - hLen - 1 octets
222 * of EM, and H be the next hLen octets.
223 *
224 * maskedDB is then the first 256 - 32 - 1 = 0-222
225 * H is 32 bytes 223-254
226 */
227
228 /* Step 6. If the leftmost 8emLen - emBits bits of the leftmost
229 * octet in maskedDB are not all equal to zero, output
230 * inconsistent and stop.
231 *
232 * 8emLen - emBits is zero, so there is nothing to test here.
233 */
234
235 /* Step 7. let dbMask = MGF(H, emLen - hLen - 1). */
236 pss_mgf1(db_mask, &em[PSS_HASH_OFFSET]);
237
238 /* Step 8. let DB = maskedDB xor dbMask.
239 * To avoid needing an additional buffer, store the 'db' in the
240 * same buffer as db_mask. From now, to the end of this function,
241 * db_mask refers to the unmasked 'db'. */
242 for (i = 0; i < PSS_MASK_LEN; i++) {
243 db_mask[i] ^= em[i];
244 }
245
246 /* Step 9. Set the leftmost 8emLen - emBits bits of the leftmost
247 * octet in DB to zero.
248 * pycrypto seems to always make the emBits 2047, so we need to
249 * clear the top bit. */
250 db_mask[0] &= 0x7F;
251
252 /* Step 10. If the emLen - hLen - sLen - 2 leftmost octets of DB
253 * are not zero or if the octet at position emLen - hLen - sLen -
254 * 1 (the leftmost position is "position 1") does not have
255 * hexadecimal value 0x01, output "inconsistent" and stop. */
256 for (i = 0; i < PSS_MASK_ZERO_COUNT; i++) {
257 if (db_mask[i] != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100258 rc = -1;
259 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600260 }
261 }
262
263 if (db_mask[PSS_MASK_ONE_POS] != 1) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100264 rc = -1;
265 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600266 }
267
268 /* Step 11. Let salt be the last sLen octets of DB */
269
270 /* Step 12. Let M' = 0x00 00 00 00 00 00 00 00 || mHash || salt; */
271
272 /* Step 13. Let H' = Hash(M') */
273 bootutil_sha256_init(&shactx);
274 bootutil_sha256_update(&shactx, pss_zeros, 8);
275 bootutil_sha256_update(&shactx, hash, PSS_HLEN);
276 bootutil_sha256_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN);
277 bootutil_sha256_finish(&shactx, h2);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900278 bootutil_sha256_drop(&shactx);
David Brownf4e904d2017-05-31 13:21:39 -0600279
280 /* Step 14. If H = H', output "consistent". Otherwise, output
281 * "inconsistent". */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100282 FIH_CALL(boot_fih_memequal, fih_rc, h2, &em[PSS_HASH_OFFSET], PSS_HLEN);
283
284out:
285 if (rc) {
286 fih_rc = fih_int_encode(rc);
David Brownf4e904d2017-05-31 13:21:39 -0600287 }
288
Raef Colese8fe6cf2020-05-26 13:07:40 +0100289 FIH_RET(fih_rc);
David Brownf4e904d2017-05-31 13:21:39 -0600290}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291
Raef Colese8fe6cf2020-05-26 13:07:40 +0100292fih_int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200293bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294 uint8_t key_id)
295{
296 mbedtls_rsa_context ctx;
297 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100298 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299 uint8_t *cp;
300 uint8_t *end;
301
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300302#if MBEDTLS_VERSION_NUMBER >= 0x03000000
303 mbedtls_rsa_init(&ctx);
304#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 mbedtls_rsa_init(&ctx, 0, 0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300306#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307
308 cp = (uint8_t *)bootutil_keys[key_id].key;
309 end = cp + *bootutil_keys[key_id].len;
310
311 rc = bootutil_parse_rsakey(&ctx, &cp, end);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300312 if (rc || slen != ctx.MBEDTLS_CONTEXT_MEMBER(len)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 mbedtls_rsa_free(&ctx);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100314 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800315 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100316 FIH_CALL(bootutil_cmp_rsasig, fih_rc, &ctx, hash, hlen, sig);
317
318out:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319 mbedtls_rsa_free(&ctx);
320
Raef Colese8fe6cf2020-05-26 13:07:40 +0100321 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300323#endif /* MCUBOOT_SIGN_RSA */