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