blob: 8a2b8b4803b3dd2cec3089cc45d39dfcfb4bfa6b [file] [log] [blame]
David Zeuthen21e95262016-07-27 17:58:40 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
David Zeuthenc612e2e2016-09-16 16:44:08 -04004 * 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:
David Zeuthen21e95262016-07-27 17:58:40 -040011 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040012 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
David Zeuthen21e95262016-07-27 17:58:40 -040014 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040015 * 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.
David Zeuthen21e95262016-07-27 17:58:40 -040023 */
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 Key {
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} Key;
46
47Key* parse_key_data(const uint8_t* data, size_t length) {
48 AvbRSAPublicKeyHeader h;
49 Key* key = NULL;
50 size_t expected_length;
51 unsigned int i;
52 const uint8_t* n;
53 const uint8_t* rr;
54
55 if (!avb_rsa_public_key_header_validate_and_byteswap(
56 (const AvbRSAPublicKeyHeader*)data, &h)) {
57 avb_error("Invalid key.\n");
58 goto fail;
59 }
60
61 if (!(h.key_num_bits == 2048 || h.key_num_bits == 4096 ||
62 h.key_num_bits == 8192)) {
63 avb_error("Unexpected key length.\n");
64 goto fail;
65 }
66
67 expected_length = sizeof(AvbRSAPublicKeyHeader) + 2 * h.key_num_bits / 8;
68 if (length != expected_length) {
69 avb_error("Key does not match expected length.\n");
70 goto fail;
71 }
72
73 n = data + sizeof(AvbRSAPublicKeyHeader);
74 rr = data + sizeof(AvbRSAPublicKeyHeader) + h.key_num_bits / 8;
75
76 /* Store n and rr following the key header so we only have to do one
77 * allocation.
78 */
79 key = (Key*)(avb_malloc(sizeof(Key) + 2 * h.key_num_bits / 8));
80 if (key == NULL) goto fail;
81
82 key->len = h.key_num_bits / 32;
83 key->n0inv = h.n0inv;
84 key->n = (uint32_t*)(key + 1); /* Skip ahead sizeof(Key) bytes. */
85 key->rr = key->n + key->len;
86
87 /* Crypto-code below (modpowF4() and friends) expects the key in
88 * little-endian format (rather than the format we're storing the
89 * key in), so convert it.
90 */
91 for (i = 0; i < key->len; i++) {
92 key->n[i] = avb_be32toh(((uint32_t*)n)[key->len - i - 1]);
93 key->rr[i] = avb_be32toh(((uint32_t*)rr)[key->len - i - 1]);
94 }
95 return key;
96
97fail:
98 if (key != NULL) avb_free(key);
99 return NULL;
100}
101
102void free_parsed_key(Key* key) { avb_free(key); }
103
104/* a[] -= mod */
105static void subM(const Key* key, uint32_t* a) {
106 int64_t A = 0;
107 uint32_t i;
108 for (i = 0; i < key->len; ++i) {
109 A += (uint64_t)a[i] - key->n[i];
110 a[i] = (uint32_t)A;
111 A >>= 32;
112 }
113}
114
115/* return a[] >= mod */
116static int geM(const Key* key, uint32_t* a) {
117 uint32_t i;
118 for (i = key->len; i;) {
119 --i;
120 if (a[i] < key->n[i]) return 0;
121 if (a[i] > key->n[i]) return 1;
122 }
123 return 1; /* equal */
124}
125
126/* montgomery c[] += a * b[] / R % mod */
127static void montMulAdd(const Key* key, uint32_t* c, const uint32_t a,
128 const uint32_t* b) {
129 uint64_t A = (uint64_t)a * b[0] + c[0];
130 uint32_t d0 = (uint32_t)A * key->n0inv;
131 uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
132 uint32_t i;
133
134 for (i = 1; i < key->len; ++i) {
135 A = (A >> 32) + (uint64_t)a * b[i] + c[i];
136 B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
137 c[i - 1] = (uint32_t)B;
138 }
139
140 A = (A >> 32) + (B >> 32);
141
142 c[i - 1] = (uint32_t)A;
143
144 if (A >> 32) {
145 subM(key, c);
146 }
147}
148
149/* montgomery c[] = a[] * b[] / R % mod */
150static void montMul(const Key* key, uint32_t* c, uint32_t* a, uint32_t* b) {
151 uint32_t i;
152 for (i = 0; i < key->len; ++i) {
153 c[i] = 0;
154 }
155 for (i = 0; i < key->len; ++i) {
156 montMulAdd(key, c, a[i], b);
157 }
158}
159
160/* In-place public exponentiation. (65537}
161 * Input and output big-endian byte array in inout.
162 */
163static void modpowF4(const Key* key, uint8_t* inout) {
164 uint32_t* a = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
165 uint32_t* aR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
166 uint32_t* aaR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
167 if (a == NULL || aR == NULL || aaR == NULL) goto out;
168
169 uint32_t* aaa = aaR; /* Re-use location. */
170 int i;
171
172 /* Convert from big endian byte array to little endian word array. */
173 for (i = 0; i < (int)key->len; ++i) {
174 uint32_t tmp = (inout[((key->len - 1 - i) * 4) + 0] << 24) |
175 (inout[((key->len - 1 - i) * 4) + 1] << 16) |
176 (inout[((key->len - 1 - i) * 4) + 2] << 8) |
177 (inout[((key->len - 1 - i) * 4) + 3] << 0);
178 a[i] = tmp;
179 }
180
181 montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
182 for (i = 0; i < 16; i += 2) {
183 montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
184 montMul(key, aR, aaR, aaR); /* aR = aaR * aaR / R mod M */
185 }
186 montMul(key, aaa, aR, a); /* aaa = aR * a / R mod M */
187
188 /* Make sure aaa < mod; aaa is at most 1x mod too large. */
189 if (geM(key, aaa)) {
190 subM(key, aaa);
191 }
192
193 /* Convert to bigendian byte array */
194 for (i = (int)key->len - 1; i >= 0; --i) {
195 uint32_t tmp = aaa[i];
196 *inout++ = (uint8_t)(tmp >> 24);
197 *inout++ = (uint8_t)(tmp >> 16);
198 *inout++ = (uint8_t)(tmp >> 8);
199 *inout++ = (uint8_t)(tmp >> 0);
200 }
201
202out:
203 if (a != NULL) avb_free(a);
204 if (aR != NULL) avb_free(aR);
205 if (aaR != NULL) avb_free(aaR);
206}
207
208/* Verify a RSA PKCS1.5 signature against an expected hash.
209 * Returns false on failure, true on success.
210 */
211bool avb_rsa_verify(const uint8_t* key, size_t key_num_bytes,
212 const uint8_t* sig, size_t sig_num_bytes,
213 const uint8_t* hash, size_t hash_num_bytes,
214 const uint8_t* padding, size_t padding_num_bytes) {
215 uint8_t* buf = NULL;
216 Key* parsed_key = NULL;
217 bool success = false;
218
219 if (key == NULL || sig == NULL || hash == NULL || padding == NULL) {
220 avb_error("Invalid input.\n");
221 goto out;
222 }
223
224 parsed_key = parse_key_data(key, key_num_bytes);
225 if (parsed_key == NULL) {
226 avb_error("Error parsing key.\n");
227 goto out;
228 }
229
230 if (sig_num_bytes != (parsed_key->len * sizeof(uint32_t))) {
231 avb_error("Signature length does not match key length.\n");
232 goto out;
233 }
234
235 if (padding_num_bytes != sig_num_bytes - hash_num_bytes) {
236 avb_error("Padding length does not match hash and signature lengths.\n");
237 goto out;
238 }
239
240 buf = (uint8_t*)avb_malloc(sig_num_bytes);
241 if (buf == NULL) {
242 avb_error("Error allocating memory.\n");
243 goto out;
244 }
245 avb_memcpy(buf, sig, sig_num_bytes);
246
247 modpowF4(parsed_key, buf);
248
249 /* Check padding bytes.
250 *
251 * Even though there are probably no timing issues here, we use
252 * avb_safe_memcmp() just to be on the safe side.
253 */
254 if (avb_safe_memcmp(buf, padding, padding_num_bytes)) {
255 avb_error("Padding check failed.\n");
256 goto out;
257 }
258
259 /* Check hash. */
260 if (avb_safe_memcmp(buf + padding_num_bytes, hash, hash_num_bytes)) {
261 avb_error("Hash check failed.\n");
262 goto out;
263 }
264
265 success = true;
266
267out:
268 if (parsed_key != NULL) free_parsed_key(parsed_key);
269 if (buf != NULL) avb_free(buf);
270 return success;
271}