1 // SPDX-License-Identifier: MIT OR BSD-3-Clause
3 * Copyright (C) 2016 The Android Open Source Project
6 /* Implementation of RSA signature verification which uses a pre-processed
7 * key for computation. The code extends libmincrypt RSA verification code to
8 * support multiple RSA key lengths and hash digest algorithms.
14 #include "avb_vbmeta_image.h"
16 typedef struct IAvbKey {
17 unsigned int len; /* Length of n[] in number of uint32_t */
18 uint32_t n0inv; /* -1 / n[0] mod 2^32 */
19 uint32_t* n; /* modulus as array (host-byte order) */
20 uint32_t* rr; /* R^2 as array (host-byte order) */
23 static IAvbKey* iavb_parse_key_data(const uint8_t* data, size_t length) {
24 AvbRSAPublicKeyHeader h;
26 size_t expected_length;
31 if (!avb_rsa_public_key_header_validate_and_byteswap(
32 (const AvbRSAPublicKeyHeader*)data, &h)) {
33 avb_error("Invalid key.\n");
37 if (!(h.key_num_bits == 2048 || h.key_num_bits == 4096 ||
38 h.key_num_bits == 8192)) {
39 avb_error("Unexpected key length.\n");
43 expected_length = sizeof(AvbRSAPublicKeyHeader) + 2 * h.key_num_bits / 8;
44 if (length != expected_length) {
45 avb_error("Key does not match expected length.\n");
49 n = data + sizeof(AvbRSAPublicKeyHeader);
50 rr = data + sizeof(AvbRSAPublicKeyHeader) + h.key_num_bits / 8;
52 /* Store n and rr following the key header so we only have to do one
55 key = (IAvbKey*)(avb_malloc(sizeof(IAvbKey) + 2 * h.key_num_bits / 8));
60 key->len = h.key_num_bits / 32;
62 key->n = (uint32_t*)(key + 1); /* Skip ahead sizeof(IAvbKey) bytes. */
63 key->rr = key->n + key->len;
65 /* Crypto-code below (modpowF4() and friends) expects the key in
66 * little-endian format (rather than the format we're storing the
67 * key in), so convert it.
69 for (i = 0; i < key->len; i++) {
70 key->n[i] = avb_be32toh(((uint32_t*)n)[key->len - i - 1]);
71 key->rr[i] = avb_be32toh(((uint32_t*)rr)[key->len - i - 1]);
82 static void iavb_free_parsed_key(IAvbKey* key) {
87 static void subM(const IAvbKey* key, uint32_t* a) {
90 for (i = 0; i < key->len; ++i) {
91 A += (uint64_t)a[i] - key->n[i];
97 /* return a[] >= mod */
98 static int geM(const IAvbKey* key, uint32_t* a) {
100 for (i = key->len; i;) {
102 if (a[i] < key->n[i]) {
105 if (a[i] > key->n[i]) {
109 return 1; /* equal */
112 /* montgomery c[] += a * b[] / R % mod */
113 static void montMulAdd(const IAvbKey* key,
117 uint64_t A = (uint64_t)a * b[0] + c[0];
118 uint32_t d0 = (uint32_t)A * key->n0inv;
119 uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
122 for (i = 1; i < key->len; ++i) {
123 A = (A >> 32) + (uint64_t)a * b[i] + c[i];
124 B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
125 c[i - 1] = (uint32_t)B;
128 A = (A >> 32) + (B >> 32);
130 c[i - 1] = (uint32_t)A;
137 /* montgomery c[] = a[] * b[] / R % mod */
138 static void montMul(const IAvbKey* key, uint32_t* c, uint32_t* a, uint32_t* b) {
140 for (i = 0; i < key->len; ++i) {
143 for (i = 0; i < key->len; ++i) {
144 montMulAdd(key, c, a[i], b);
148 /* In-place public exponentiation. (65537}
149 * Input and output big-endian byte array in inout.
151 static void modpowF4(const IAvbKey* key, uint8_t* inout) {
152 uint32_t* a = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
153 uint32_t* aR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
154 uint32_t* aaR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
155 if (a == NULL || aR == NULL || aaR == NULL) {
159 uint32_t* aaa = aaR; /* Re-use location. */
162 /* Convert from big endian byte array to little endian word array. */
163 for (i = 0; i < (int)key->len; ++i) {
164 uint32_t tmp = (inout[((key->len - 1 - i) * 4) + 0] << 24) |
165 (inout[((key->len - 1 - i) * 4) + 1] << 16) |
166 (inout[((key->len - 1 - i) * 4) + 2] << 8) |
167 (inout[((key->len - 1 - i) * 4) + 3] << 0);
171 montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
172 for (i = 0; i < 16; i += 2) {
173 montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
174 montMul(key, aR, aaR, aaR); /* aR = aaR * aaR / R mod M */
176 montMul(key, aaa, aR, a); /* aaa = aR * a / R mod M */
178 /* Make sure aaa < mod; aaa is at most 1x mod too large. */
183 /* Convert to bigendian byte array */
184 for (i = (int)key->len - 1; i >= 0; --i) {
185 uint32_t tmp = aaa[i];
186 *inout++ = (uint8_t)(tmp >> 24);
187 *inout++ = (uint8_t)(tmp >> 16);
188 *inout++ = (uint8_t)(tmp >> 8);
189 *inout++ = (uint8_t)(tmp >> 0);
204 /* Verify a RSA PKCS1.5 signature against an expected hash.
205 * Returns false on failure, true on success.
207 bool avb_rsa_verify(const uint8_t* key,
208 size_t key_num_bytes,
210 size_t sig_num_bytes,
212 size_t hash_num_bytes,
213 const uint8_t* padding,
214 size_t padding_num_bytes) {
216 IAvbKey* parsed_key = NULL;
217 bool success = false;
219 if (key == NULL || sig == NULL || hash == NULL || padding == NULL) {
220 avb_error("Invalid input.\n");
224 parsed_key = iavb_parse_key_data(key, key_num_bytes);
225 if (parsed_key == NULL) {
226 avb_error("Error parsing key.\n");
230 if (sig_num_bytes != (parsed_key->len * sizeof(uint32_t))) {
231 avb_error("Signature length does not match key length.\n");
235 if (padding_num_bytes != sig_num_bytes - hash_num_bytes) {
236 avb_error("Padding length does not match hash and signature lengths.\n");
240 buf = (uint8_t*)avb_malloc(sig_num_bytes);
242 avb_error("Error allocating memory.\n");
245 avb_memcpy(buf, sig, sig_num_bytes);
247 modpowF4(parsed_key, buf);
249 /* Check padding bytes.
251 * Even though there are probably no timing issues here, we use
252 * avb_safe_memcmp() just to be on the safe side.
254 if (avb_safe_memcmp(buf, padding, padding_num_bytes)) {
255 avb_error("Padding check failed.\n");
260 if (avb_safe_memcmp(buf + padding_num_bytes, hash, hash_num_bytes)) {
261 avb_error("Hash check failed.\n");
268 if (parsed_key != NULL) {
269 iavb_free_parsed_key(parsed_key);