blob: d437add18f0553cd8b7eefaa4703ee3fa5900662 [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
88 if (*p != end) {
89 return -4;
90 }
91
92 if ((rc = mbedtls_rsa_check_pubkey(ctx)) != 0) {
93 return -5;
94 }
95
96 ctx->len = mbedtls_mpi_size(&ctx->N);
97
98 return 0;
99}
100
David Brownf4e904d2017-05-31 13:21:39 -0600101/*
102 * Compute the RSA-PSS mask-generation function, MGF1. Assumptions
103 * are that the mask length will be less than 256 * PSS_HLEN, and
104 * therefore we never need to increment anything other than the low
105 * byte of the counter.
106 *
107 * This is described in PKCS#1, B.2.1.
108 */
109static void
110pss_mgf1(uint8_t *mask, const uint8_t *hash)
111{
112 bootutil_sha256_context ctx;
113 uint8_t counter[4] = { 0, 0, 0, 0 };
114 uint8_t htmp[PSS_HLEN];
115 int count = PSS_MASK_LEN;
116 int bytes;
117
118 while (count > 0) {
119 bootutil_sha256_init(&ctx);
120 bootutil_sha256_update(&ctx, hash, PSS_HLEN);
121 bootutil_sha256_update(&ctx, counter, 4);
122 bootutil_sha256_finish(&ctx, htmp);
123
124 counter[3]++;
125
126 bytes = PSS_HLEN;
127 if (bytes > count)
128 bytes = count;
129
130 memcpy(mask, htmp, bytes);
131 mask += bytes;
132 count -= bytes;
133 }
134}
135
136/*
137 * Validate an RSA signature, using RSA-PSS, as described in PKCS #1
138 * v2.2, section 9.1.2, with many parameters required to have fixed
139 * values.
140 */
141static int
142bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen,
143 uint8_t *sig)
144{
145 bootutil_sha256_context shactx;
146 uint8_t em[MBEDTLS_MPI_MAX_SIZE];
147 uint8_t db_mask[PSS_MASK_LEN];
148 uint8_t h2[PSS_HLEN];
149 int i;
150
David Browncdb968f2017-06-05 12:57:26 -0600151 if (ctx->len != PSS_EMLEN || PSS_EMLEN > MBEDTLS_MPI_MAX_SIZE) {
David Brownf4e904d2017-05-31 13:21:39 -0600152 return -1;
153 }
154
155 if (hlen != PSS_HLEN) {
156 return -1;
157 }
158
159 if (mbedtls_rsa_public(ctx, sig, em)) {
160 return -1;
161 }
162
163 /*
164 * PKCS #1 v2.2, 9.1.2 EMSA-PSS-Verify
165 *
166 * emBits is 2048
167 * emLen = ceil(emBits/8) = 256
168 *
169 * The salt length is not known at the beginning.
170 */
171
172 /* Step 1. The message is constrained by the address space of a
173 * 32-bit processor, which is far less than the 2^61-1 limit of
174 * SHA-256.
175 */
176
177 /* Step 2. mHash is passed in as 'hash', with hLen the hlen
178 * argument. */
179
180 /* Step 3. if emLen < hLen + sLen + 2, inconsistent and stop.
181 * The salt length is not known at this point.
182 */
183
184 /* Step 4. If the rightmost octect of EM does have the value
185 * 0xbc, output inconsistent and stop.
186 */
187 if (em[PSS_EMLEN - 1] != 0xbc) {
188 return -1;
189 }
190
191 /* Step 5. Let maskedDB be the leftmost emLen - hLen - 1 octets
192 * of EM, and H be the next hLen octets.
193 *
194 * maskedDB is then the first 256 - 32 - 1 = 0-222
195 * H is 32 bytes 223-254
196 */
197
198 /* Step 6. If the leftmost 8emLen - emBits bits of the leftmost
199 * octet in maskedDB are not all equal to zero, output
200 * inconsistent and stop.
201 *
202 * 8emLen - emBits is zero, so there is nothing to test here.
203 */
204
205 /* Step 7. let dbMask = MGF(H, emLen - hLen - 1). */
206 pss_mgf1(db_mask, &em[PSS_HASH_OFFSET]);
207
208 /* Step 8. let DB = maskedDB xor dbMask.
209 * To avoid needing an additional buffer, store the 'db' in the
210 * same buffer as db_mask. From now, to the end of this function,
211 * db_mask refers to the unmasked 'db'. */
212 for (i = 0; i < PSS_MASK_LEN; i++) {
213 db_mask[i] ^= em[i];
214 }
215
216 /* Step 9. Set the leftmost 8emLen - emBits bits of the leftmost
217 * octet in DB to zero.
218 * pycrypto seems to always make the emBits 2047, so we need to
219 * clear the top bit. */
220 db_mask[0] &= 0x7F;
221
222 /* Step 10. If the emLen - hLen - sLen - 2 leftmost octets of DB
223 * are not zero or if the octet at position emLen - hLen - sLen -
224 * 1 (the leftmost position is "position 1") does not have
225 * hexadecimal value 0x01, output "inconsistent" and stop. */
226 for (i = 0; i < PSS_MASK_ZERO_COUNT; i++) {
227 if (db_mask[i] != 0) {
228 return -1;
229 }
230 }
231
232 if (db_mask[PSS_MASK_ONE_POS] != 1) {
233 return -1;
234 }
235
236 /* Step 11. Let salt be the last sLen octets of DB */
237
238 /* Step 12. Let M' = 0x00 00 00 00 00 00 00 00 || mHash || salt; */
239
240 /* Step 13. Let H' = Hash(M') */
241 bootutil_sha256_init(&shactx);
242 bootutil_sha256_update(&shactx, pss_zeros, 8);
243 bootutil_sha256_update(&shactx, hash, PSS_HLEN);
244 bootutil_sha256_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN);
245 bootutil_sha256_finish(&shactx, h2);
246
247 /* Step 14. If H = H', output "consistent". Otherwise, output
248 * "inconsistent". */
249 if (memcmp(h2, &em[PSS_HASH_OFFSET], PSS_HLEN) != 0) {
250 return -1;
251 }
252
253 return 0;
254}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255
256int
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200257bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800258 uint8_t key_id)
259{
260 mbedtls_rsa_context ctx;
261 int rc;
262 uint8_t *cp;
263 uint8_t *end;
264
265 mbedtls_rsa_init(&ctx, 0, 0);
266
267 cp = (uint8_t *)bootutil_keys[key_id].key;
268 end = cp + *bootutil_keys[key_id].len;
269
270 rc = bootutil_parse_rsakey(&ctx, &cp, end);
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200271 if (rc || slen != ctx.len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800272 mbedtls_rsa_free(&ctx);
273 return rc;
274 }
275 rc = bootutil_cmp_rsasig(&ctx, hash, hlen, sig);
276 mbedtls_rsa_free(&ctx);
277
278 return rc;
279}
Fabio Utzig19356bf2017-05-11 16:19:36 -0300280#endif /* MCUBOOT_SIGN_RSA */