2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/types.h>
11 #include <asm/byteorder.h>
12 #include <asm/errno.h>
13 #include <asm/types.h>
14 #include <asm/unaligned.h>
18 #include <fdt_support.h>
20 #include <u-boot/rsa.h>
21 #include <u-boot/rsa-mod-exp.h>
23 #define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
25 #define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a)
26 #define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a))
28 /* Default public exponent for backward compatibility */
29 #define RSA_DEFAULT_PUBEXP 65537
32 * subtract_modulus() - subtract modulus from the given value
34 * @key: Key containing modulus to subtract
35 * @num: Number to subtract modulus from, as little endian word array
37 static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
42 for (i = 0; i < key->len; i++) {
43 acc += (uint64_t)num[i] - key->modulus[i];
44 num[i] = (uint32_t)acc;
50 * greater_equal_modulus() - check if a value is >= modulus
52 * @key: Key containing modulus to check
53 * @num: Number to check against modulus, as little endian word array
54 * @return 0 if num < modulus, 1 if num >= modulus
56 static int greater_equal_modulus(const struct rsa_public_key *key,
61 for (i = (int)key->len - 1; i >= 0; i--) {
62 if (num[i] < key->modulus[i])
64 if (num[i] > key->modulus[i])
72 * montgomery_mul_add_step() - Perform montgomery multiply-add step
74 * Operation: montgomery result[] += a * b[] / n0inv % modulus
77 * @result: Place to put result, as little endian word array
79 * @b: Multiplicand, as little endian word array
81 static void montgomery_mul_add_step(const struct rsa_public_key *key,
82 uint32_t result[], const uint32_t a, const uint32_t b[])
84 uint64_t acc_a, acc_b;
88 acc_a = (uint64_t)a * b[0] + result[0];
89 d0 = (uint32_t)acc_a * key->n0inv;
90 acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
91 for (i = 1; i < key->len; i++) {
92 acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
93 acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
95 result[i - 1] = (uint32_t)acc_b;
98 acc_a = (acc_a >> 32) + (acc_b >> 32);
100 result[i - 1] = (uint32_t)acc_a;
103 subtract_modulus(key, result);
107 * montgomery_mul() - Perform montgomery mutitply
109 * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
112 * @result: Place to put result, as little endian word array
113 * @a: Multiplier, as little endian word array
114 * @b: Multiplicand, as little endian word array
116 static void montgomery_mul(const struct rsa_public_key *key,
117 uint32_t result[], uint32_t a[], const uint32_t b[])
121 for (i = 0; i < key->len; ++i)
123 for (i = 0; i < key->len; ++i)
124 montgomery_mul_add_step(key, result, a[i], b);
128 * num_pub_exponent_bits() - Number of bits in the public exponent
131 * @num_bits: Storage for the number of public exponent bits
133 static int num_public_exponent_bits(const struct rsa_public_key *key,
138 const uint max_bits = (sizeof(exponent) * 8);
140 exponent = key->exponent;
144 *num_bits = exponent_bits;
148 for (exponent_bits = 1; exponent_bits < max_bits + 1; ++exponent_bits)
149 if (!(exponent >>= 1)) {
150 *num_bits = exponent_bits;
158 * is_public_exponent_bit_set() - Check if a bit in the public exponent is set
161 * @pos: The bit position to check
163 static int is_public_exponent_bit_set(const struct rsa_public_key *key,
166 return key->exponent & (1ULL << pos);
170 * pow_mod() - in-place public exponentiation
173 * @inout: Big-endian word array containing value and result
175 static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
177 uint32_t *result, *ptr;
181 /* Sanity check for stack size - key->len is in 32-bit words */
182 if (key->len > RSA_MAX_KEY_BITS / 32) {
183 debug("RSA key words %u exceeds maximum %d\n", key->len,
184 RSA_MAX_KEY_BITS / 32);
188 uint32_t val[key->len], acc[key->len], tmp[key->len];
189 uint32_t a_scaled[key->len];
190 result = tmp; /* Re-use location. */
192 /* Convert from big endian byte array to little endian word array. */
193 for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
194 val[i] = get_unaligned_be32(ptr);
196 if (0 != num_public_exponent_bits(key, &k))
200 debug("Public exponent is too short (%d bits, minimum 2)\n",
205 if (!is_public_exponent_bit_set(key, 0)) {
206 debug("LSB of RSA public exponent must be set.\n");
210 /* the bit at e[k-1] is 1 by definition, so start with: C := M */
211 montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
212 /* retain scaled version for intermediate use */
213 memcpy(a_scaled, acc, key->len * sizeof(a_scaled[0]));
215 for (j = k - 2; j > 0; --j) {
216 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
218 if (is_public_exponent_bit_set(key, j)) {
219 /* acc = tmp * val / R mod n */
220 montgomery_mul(key, acc, tmp, a_scaled);
222 /* e[j] == 0, copy tmp back to acc for next operation */
223 memcpy(acc, tmp, key->len * sizeof(acc[0]));
227 /* the bit at e[0] is always 1 */
228 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
229 montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
230 memcpy(result, acc, key->len * sizeof(result[0]));
232 /* Make sure result < mod; result is at most 1x mod too large. */
233 if (greater_equal_modulus(key, result))
234 subtract_modulus(key, result);
236 /* Convert to bigendian byte array */
237 for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
238 put_unaligned_be32(result[i], ptr);
242 static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
246 for (i = 0; i < len; i++)
247 dst[i] = fdt32_to_cpu(src[len - 1 - i]);
250 int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
251 struct key_prop *prop, uint8_t *out)
253 struct rsa_public_key key;
257 debug("%s: Skipping invalid prop", __func__);
260 key.n0inv = prop->n0inv;
261 key.len = prop->num_bits;
263 if (!prop->public_exponent)
264 key.exponent = RSA_DEFAULT_PUBEXP;
267 fdt64_to_cpu(*((uint64_t *)(prop->public_exponent)));
269 if (!key.len || !prop->modulus || !prop->rr) {
270 debug("%s: Missing RSA key info", __func__);
274 /* Sanity check for stack size */
275 if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
276 debug("RSA key bits %u outside allowed range %d..%d\n",
277 key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
280 key.len /= sizeof(uint32_t) * 8;
281 uint32_t key1[key.len], key2[key.len];
285 rsa_convert_big_endian(key.modulus, (uint32_t *)prop->modulus, key.len);
286 rsa_convert_big_endian(key.rr, (uint32_t *)prop->rr, key.len);
287 if (!key.modulus || !key.rr) {
288 debug("%s: Out of memory", __func__);
292 uint32_t buf[sig_len / sizeof(uint32_t)];
294 memcpy(buf, sig, sig_len);
296 ret = pow_mod(&key, buf);
300 memcpy(out, buf, sig_len);