blob: 386d7aed626bbdd08ae32b378efd0a2a3f0bdb6f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
26 * Use of this source code is governed by a BSD-style license that can be
27 * found in the LICENSE file.
28 */
29
30/* Implementation of RSA signature verification which uses a pre-processed
31 * key for computation. The code extends libmincrypt RSA verification code to
32 * support multiple RSA key lengths and hash digest algorithms.
33 */
34
35#include "avb_rsa.h"
36#include "avb_sha.h"
37#include "avb_util.h"
38#include "avb_vbmeta_image.h"
39
40typedef struct IAvbKey {
41 unsigned int len; /* Length of n[] in number of uint32_t */
42 uint32_t n0inv; /* -1 / n[0] mod 2^32 */
43 uint32_t *n; /* modulus as array (host-byte order) */
44 uint32_t *rr; /* R^2 as array (host-byte order) */
45} IAvbKey;
46
47static IAvbKey *iavb_parse_key_data(const uint8_t *data, size_t length)
48{
49 AvbRSAPublicKeyHeader h;
50 IAvbKey *key = NULL;
51 size_t expected_length;
52 unsigned int i;
53 const uint8_t *n;
54 const uint8_t *rr;
55
56 if (!avb_rsa_public_key_header_validate_and_byteswap(
57 (const AvbRSAPublicKeyHeader *)data, &h)) {
58 avb_error("Invalid key.\n");
59 goto fail;
60 }
61
62 if (!(h.key_num_bits == 2048 || h.key_num_bits == 4096 ||
63 h.key_num_bits == 8192)) {
64 avb_error("Unexpected key length.\n");
65 goto fail;
66 }
67
68 expected_length = sizeof(AvbRSAPublicKeyHeader) + 2 * h.key_num_bits / 8;
69 if (length != expected_length) {
70 avb_error("Key does not match expected length.\n");
71 goto fail;
72 }
73
74 n = data + sizeof(AvbRSAPublicKeyHeader);
75 rr = data + sizeof(AvbRSAPublicKeyHeader) + h.key_num_bits / 8;
76
77 /* Store n and rr following the key header so we only have to do one
78 * allocation.
79 */
80 key = (IAvbKey *)(avb_malloc(sizeof(IAvbKey) + 2 * h.key_num_bits / 8));
81 if (key == NULL) {
82 goto fail;
83 }
84
85 key->len = h.key_num_bits / 32;
86 key->n0inv = h.n0inv;
87 key->n = (uint32_t *)(key + 1); /* Skip ahead sizeof(IAvbKey) bytes. */
88 key->rr = key->n + key->len;
89
90 /* Crypto-code below (modpowF4() and friends) expects the key in
91 * little-endian format (rather than the format we're storing the
92 * key in), so convert it.
93 */
94 for (i = 0; i < key->len; i++) {
95 key->n[i] = avb_be32toh(((uint32_t *)n)[key->len - i - 1]);
96 key->rr[i] = avb_be32toh(((uint32_t *)rr)[key->len - i - 1]);
97 }
98 return key;
99
100fail:
101 if (key != NULL) {
102 avb_free(key);
103 }
104 return NULL;
105}
106
107static void iavb_free_parsed_key(IAvbKey *key)
108{
109 avb_free(key);
110}
111
112/* a[] -= mod */
113static void subM(const IAvbKey *key, uint32_t *a)
114{
115 int64_t A = 0;
116 uint32_t i;
117 for (i = 0; i < key->len; ++i) {
118 A += (uint64_t)a[i] - key->n[i];
119 a[i] = (uint32_t)A;
120 A >>= 32;
121 }
122}
123
124/* return a[] >= mod */
125static int geM(const IAvbKey *key, uint32_t *a)
126{
127 uint32_t i;
128 for (i = key->len; i;) {
129 --i;
130 if (a[i] < key->n[i]) {
131 return 0;
132 }
133 if (a[i] > key->n[i]) {
134 return 1;
135 }
136 }
137 return 1; /* equal */
138}
139
140/* montgomery c[] += a * b[] / R % mod */
141static void montMulAdd(const IAvbKey *key,
142 uint32_t *c,
143 const uint32_t a,
144 const uint32_t *b)
145{
146 uint64_t A = (uint64_t)a * b[0] + c[0];
147 uint32_t d0 = (uint32_t)A * key->n0inv;
148 uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
149 uint32_t i;
150
151 for (i = 1; i < key->len; ++i) {
152 A = (A >> 32) + (uint64_t)a * b[i] + c[i];
153 B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
154 c[i - 1] = (uint32_t)B;
155 }
156
157 A = (A >> 32) + (B >> 32);
158
159 c[i - 1] = (uint32_t)A;
160
161 if (A >> 32) {
162 subM(key, c);
163 }
164}
165
166/* montgomery c[] = a[] * b[] / R % mod */
167static void montMul(const IAvbKey *key, uint32_t *c, uint32_t *a, uint32_t *b)
168{
169 uint32_t i;
170 for (i = 0; i < key->len; ++i) {
171 c[i] = 0;
172 }
173 for (i = 0; i < key->len; ++i) {
174 montMulAdd(key, c, a[i], b);
175 }
176}
177
178/* In-place public exponentiation. (65537}
179 * Input and output big-endian byte array in inout.
180 */
181static void modpowF4(const IAvbKey *key, uint8_t *inout)
182{
183 uint32_t *a = (uint32_t *)avb_malloc(key->len * sizeof(uint32_t));
184 uint32_t *aR = (uint32_t *)avb_malloc(key->len * sizeof(uint32_t));
185 uint32_t *aaR = (uint32_t *)avb_malloc(key->len * sizeof(uint32_t));
186 if (a == NULL || aR == NULL || aaR == NULL) {
187 goto out;
188 }
189
190 uint32_t *aaa = aaR; /* Re-use location. */
191 int i;
192
193 /* Convert from big endian byte array to little endian word array. */
194 for (i = 0; i < (int)key->len; ++i) {
195 uint32_t tmp = (inout[((key->len - 1 - i) * 4) + 0] << 24) |
196 (inout[((key->len - 1 - i) * 4) + 1] << 16) |
197 (inout[((key->len - 1 - i) * 4) + 2] << 8) |
198 (inout[((key->len - 1 - i) * 4) + 3] << 0);
199 a[i] = tmp;
200 }
201
202 montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
203 for (i = 0; i < 16; i += 2) {
204 montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
205 montMul(key, aR, aaR, aaR); /* aR = aaR * aaR / R mod M */
206 }
207 montMul(key, aaa, aR, a); /* aaa = aR * a / R mod M */
208
209 /* Make sure aaa < mod; aaa is at most 1x mod too large. */
210 if (geM(key, aaa)) {
211 subM(key, aaa);
212 }
213
214 /* Convert to bigendian byte array */
215 for (i = (int)key->len - 1; i >= 0; --i) {
216 uint32_t tmp = aaa[i];
217 *inout++ = (uint8_t)(tmp >> 24);
218 *inout++ = (uint8_t)(tmp >> 16);
219 *inout++ = (uint8_t)(tmp >> 8);
220 *inout++ = (uint8_t)(tmp >> 0);
221 }
222
223out:
224 if (a != NULL) {
225 avb_free(a);
226 }
227 if (aR != NULL) {
228 avb_free(aR);
229 }
230 if (aaR != NULL) {
231 avb_free(aaR);
232 }
233}
234
235/* Verify a RSA PKCS1.5 signature against an expected hash.
236 * Returns false on failure, true on success.
237 */
238bool avb_rsa_verify(const uint8_t *key,
239 size_t key_num_bytes,
240 const uint8_t *sig,
241 size_t sig_num_bytes,
242 const uint8_t *hash,
243 size_t hash_num_bytes,
244 const uint8_t *padding,
245 size_t padding_num_bytes)
246{
247 uint8_t *buf = NULL;
248 IAvbKey *parsed_key = NULL;
249 bool success = false;
250
251 if (key == NULL || sig == NULL || hash == NULL || padding == NULL) {
252 avb_error("Invalid input.\n");
253 goto out;
254 }
255
256 parsed_key = iavb_parse_key_data(key, key_num_bytes);
257 if (parsed_key == NULL) {
258 avb_error("Error parsing key.\n");
259 goto out;
260 }
261
262 if (sig_num_bytes != (parsed_key->len * sizeof(uint32_t))) {
263 avb_error("Signature length does not match key length.\n");
264 goto out;
265 }
266
267 if (padding_num_bytes != sig_num_bytes - hash_num_bytes) {
268 avb_error("Padding length does not match hash and signature lengths.\n");
269 goto out;
270 }
271
272 buf = (uint8_t *)avb_malloc(sig_num_bytes);
273 if (buf == NULL) {
274 avb_error("Error allocating memory.\n");
275 goto out;
276 }
277 avb_memcpy(buf, sig, sig_num_bytes);
278
279 modpowF4(parsed_key, buf);
280
281 /* Check padding bytes.
282 *
283 * Even though there are probably no timing issues here, we use
284 * avb_safe_memcmp() just to be on the safe side.
285 */
286 if (avb_safe_memcmp(buf, padding, padding_num_bytes)) {
287 avb_error("Padding check failed.\n");
288 goto out;
289 }
290
291 /* Check hash. */
292 if (avb_safe_memcmp(buf + padding_num_bytes, hash, hash_num_bytes)) {
293 avb_error("Hash check failed.\n");
294 goto out;
295 }
296
297 success = true;
298
299out:
300 if (parsed_key != NULL) {
301 iavb_free_parsed_key(parsed_key);
302 }
303 if (buf != NULL) {
304 avb_free(buf);
305 }
306 return success;
307}