blob: 0e0782b1f7ed43e8e941473e85cc2e494bebbe0d [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
Sherry Zhangb111f982021-07-13 17:11:47 +08006 * 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"
Sherry Zhangb111f982021-07-13 17:11:47 +080035#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
Sherry Zhangb111f982021-07-13 17:11:47 +080092 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
Sherry Zhangb111f982021-07-13 17:11:47 +080097 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
Sherry Zhangb111f982021-07-13 17:11:47 +0800105 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
Sherry Zhangb111f982021-07-13 17:11:47 +0800117 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
151 memcpy(mask, htmp, bytes);
152 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 */
Michael Grand5047f032022-11-24 16:49:56 +0100164static fih_ret
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;
Michael Grand5047f032022-11-24 16:49:56 +0100173 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Brownf4e904d2017-05-31 13:21:39 -0600174
Sherry Zhangb111f982021-07-13 17:11:47 +0800175 if (ctx->MBEDTLS_CONTEXT_MEMBER(len) != PSS_EMLEN ||
176 PSS_EMLEN > MBEDTLS_MPI_MAX_SIZE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100177 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600178 }
179
180 if (hlen != PSS_HLEN) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100181 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600182 }
183
184 if (mbedtls_rsa_public(ctx, sig, em)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100185 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600186 }
187
188 /*
189 * PKCS #1 v2.2, 9.1.2 EMSA-PSS-Verify
190 *
191 * emBits is 2048
192 * emLen = ceil(emBits/8) = 256
193 *
194 * The salt length is not known at the beginning.
195 */
196
197 /* Step 1. The message is constrained by the address space of a
198 * 32-bit processor, which is far less than the 2^61-1 limit of
199 * SHA-256.
200 */
201
202 /* Step 2. mHash is passed in as 'hash', with hLen the hlen
203 * argument. */
204
205 /* Step 3. if emLen < hLen + sLen + 2, inconsistent and stop.
206 * The salt length is not known at this point.
207 */
208
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300209 /* Step 4. If the rightmost octet of EM does have the value
David Brownf4e904d2017-05-31 13:21:39 -0600210 * 0xbc, output inconsistent and stop.
211 */
212 if (em[PSS_EMLEN - 1] != 0xbc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100213 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600214 }
215
216 /* Step 5. Let maskedDB be the leftmost emLen - hLen - 1 octets
217 * of EM, and H be the next hLen octets.
218 *
219 * maskedDB is then the first 256 - 32 - 1 = 0-222
220 * H is 32 bytes 223-254
221 */
222
223 /* Step 6. If the leftmost 8emLen - emBits bits of the leftmost
224 * octet in maskedDB are not all equal to zero, output
225 * inconsistent and stop.
226 *
227 * 8emLen - emBits is zero, so there is nothing to test here.
228 */
229
230 /* Step 7. let dbMask = MGF(H, emLen - hLen - 1). */
231 pss_mgf1(db_mask, &em[PSS_HASH_OFFSET]);
232
233 /* Step 8. let DB = maskedDB xor dbMask.
234 * To avoid needing an additional buffer, store the 'db' in the
235 * same buffer as db_mask. From now, to the end of this function,
236 * db_mask refers to the unmasked 'db'. */
237 for (i = 0; i < PSS_MASK_LEN; i++) {
238 db_mask[i] ^= em[i];
239 }
240
241 /* Step 9. Set the leftmost 8emLen - emBits bits of the leftmost
242 * octet in DB to zero.
243 * pycrypto seems to always make the emBits 2047, so we need to
244 * clear the top bit. */
245 db_mask[0] &= 0x7F;
246
247 /* Step 10. If the emLen - hLen - sLen - 2 leftmost octets of DB
248 * are not zero or if the octet at position emLen - hLen - sLen -
249 * 1 (the leftmost position is "position 1") does not have
250 * hexadecimal value 0x01, output "inconsistent" and stop. */
251 for (i = 0; i < PSS_MASK_ZERO_COUNT; i++) {
252 if (db_mask[i] != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100253 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600254 }
255 }
256
257 if (db_mask[PSS_MASK_ONE_POS] != 1) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100258 goto out;
David Brownf4e904d2017-05-31 13:21:39 -0600259 }
260
261 /* Step 11. Let salt be the last sLen octets of DB */
262
263 /* Step 12. Let M' = 0x00 00 00 00 00 00 00 00 || mHash || salt; */
264
265 /* Step 13. Let H' = Hash(M') */
266 bootutil_sha256_init(&shactx);
267 bootutil_sha256_update(&shactx, pss_zeros, 8);
268 bootutil_sha256_update(&shactx, hash, PSS_HLEN);
269 bootutil_sha256_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN);
270 bootutil_sha256_finish(&shactx, h2);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900271 bootutil_sha256_drop(&shactx);
David Brownf4e904d2017-05-31 13:21:39 -0600272
273 /* Step 14. If H = H', output "consistent". Otherwise, output
274 * "inconsistent". */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100275 FIH_CALL(boot_fih_memequal, fih_rc, h2, &em[PSS_HASH_OFFSET], PSS_HLEN);
276
277out:
Raef Colese8fe6cf2020-05-26 13:07:40 +0100278 FIH_RET(fih_rc);
David Brownf4e904d2017-05-31 13:21:39 -0600279}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800280
Michael Grand5047f032022-11-24 16:49:56 +0100281fih_ret
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200282bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283 uint8_t key_id)
284{
285 mbedtls_rsa_context ctx;
286 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100287 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288 uint8_t *cp;
289 uint8_t *end;
290
Sherry Zhangb111f982021-07-13 17:11:47 +0800291#if MBEDTLS_VERSION_NUMBER >= 0x03000000
292 mbedtls_rsa_init(&ctx);
293#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294 mbedtls_rsa_init(&ctx, 0, 0);
Sherry Zhangb111f982021-07-13 17:11:47 +0800295#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800296
297 cp = (uint8_t *)bootutil_keys[key_id].key;
298 end = cp + *bootutil_keys[key_id].len;
299
300 rc = bootutil_parse_rsakey(&ctx, &cp, end);
Sherry Zhangb111f982021-07-13 17:11:47 +0800301 if (rc || slen != ctx.MBEDTLS_CONTEXT_MEMBER(len)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 mbedtls_rsa_free(&ctx);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100303 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100305 FIH_CALL(bootutil_cmp_rsasig, fih_rc, &ctx, hash, hlen, sig);
306
307out:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308 mbedtls_rsa_free(&ctx);
309
Raef Colese8fe6cf2020-05-26 13:07:40 +0100310 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300312#endif /* MCUBOOT_SIGN_RSA */