1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
10 #include <asm/types.h>
11 #include <asm/byteorder.h>
12 #include <linux/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 static inline uint64_t fdt64_to_cpup(const void *p)
32 memcpy(&w, p, sizeof(w));
33 return fdt64_to_cpu(w);
36 /* Default public exponent for backward compatibility */
37 #define RSA_DEFAULT_PUBEXP 65537
40 * subtract_modulus() - subtract modulus from the given value
42 * @key: Key containing modulus to subtract
43 * @num: Number to subtract modulus from, as little endian word array
45 static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
50 for (i = 0; i < key->len; i++) {
51 acc += (uint64_t)num[i] - key->modulus[i];
52 num[i] = (uint32_t)acc;
58 * greater_equal_modulus() - check if a value is >= modulus
60 * @key: Key containing modulus to check
61 * @num: Number to check against modulus, as little endian word array
62 * Return: 0 if num < modulus, 1 if num >= modulus
64 static int greater_equal_modulus(const struct rsa_public_key *key,
69 for (i = (int)key->len - 1; i >= 0; i--) {
70 if (num[i] < key->modulus[i])
72 if (num[i] > key->modulus[i])
80 * montgomery_mul_add_step() - Perform montgomery multiply-add step
82 * Operation: montgomery result[] += a * b[] / n0inv % modulus
85 * @result: Place to put result, as little endian word array
87 * @b: Multiplicand, as little endian word array
89 static void montgomery_mul_add_step(const struct rsa_public_key *key,
90 uint32_t result[], const uint32_t a, const uint32_t b[])
92 uint64_t acc_a, acc_b;
96 acc_a = (uint64_t)a * b[0] + result[0];
97 d0 = (uint32_t)acc_a * key->n0inv;
98 acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
99 for (i = 1; i < key->len; i++) {
100 acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
101 acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
103 result[i - 1] = (uint32_t)acc_b;
106 acc_a = (acc_a >> 32) + (acc_b >> 32);
108 result[i - 1] = (uint32_t)acc_a;
111 subtract_modulus(key, result);
115 * montgomery_mul() - Perform montgomery mutitply
117 * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
120 * @result: Place to put result, as little endian word array
121 * @a: Multiplier, as little endian word array
122 * @b: Multiplicand, as little endian word array
124 static void montgomery_mul(const struct rsa_public_key *key,
125 uint32_t result[], uint32_t a[], const uint32_t b[])
129 for (i = 0; i < key->len; ++i)
131 for (i = 0; i < key->len; ++i)
132 montgomery_mul_add_step(key, result, a[i], b);
136 * num_pub_exponent_bits() - Number of bits in the public exponent
139 * @num_bits: Storage for the number of public exponent bits
141 static int num_public_exponent_bits(const struct rsa_public_key *key,
146 const uint max_bits = (sizeof(exponent) * 8);
148 exponent = key->exponent;
152 *num_bits = exponent_bits;
156 for (exponent_bits = 1; exponent_bits < max_bits + 1; ++exponent_bits)
157 if (!(exponent >>= 1)) {
158 *num_bits = exponent_bits;
166 * is_public_exponent_bit_set() - Check if a bit in the public exponent is set
169 * @pos: The bit position to check
171 static int is_public_exponent_bit_set(const struct rsa_public_key *key,
174 return key->exponent & (1ULL << pos);
178 * pow_mod() - in-place public exponentiation
181 * @inout: Big-endian word array containing value and result
183 static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
185 uint32_t *result, *ptr;
189 /* Sanity check for stack size - key->len is in 32-bit words */
190 if (key->len > RSA_MAX_KEY_BITS / 32) {
191 debug("RSA key words %u exceeds maximum %d\n", key->len,
192 RSA_MAX_KEY_BITS / 32);
196 uint32_t val[key->len], acc[key->len], tmp[key->len];
197 uint32_t a_scaled[key->len];
198 result = tmp; /* Re-use location. */
200 /* Convert from big endian byte array to little endian word array. */
201 for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
202 val[i] = get_unaligned_be32(ptr);
204 if (0 != num_public_exponent_bits(key, &k))
208 debug("Public exponent is too short (%d bits, minimum 2)\n",
213 if (!is_public_exponent_bit_set(key, 0)) {
214 debug("LSB of RSA public exponent must be set.\n");
218 /* the bit at e[k-1] is 1 by definition, so start with: C := M */
219 montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
220 /* retain scaled version for intermediate use */
221 memcpy(a_scaled, acc, key->len * sizeof(a_scaled[0]));
223 for (j = k - 2; j > 0; --j) {
224 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
226 if (is_public_exponent_bit_set(key, j)) {
227 /* acc = tmp * val / R mod n */
228 montgomery_mul(key, acc, tmp, a_scaled);
230 /* e[j] == 0, copy tmp back to acc for next operation */
231 memcpy(acc, tmp, key->len * sizeof(acc[0]));
235 /* the bit at e[0] is always 1 */
236 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
237 montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
238 memcpy(result, acc, key->len * sizeof(result[0]));
240 /* Make sure result < mod; result is at most 1x mod too large. */
241 if (greater_equal_modulus(key, result))
242 subtract_modulus(key, result);
244 /* Convert to bigendian byte array */
245 for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
246 put_unaligned_be32(result[i], ptr);
250 static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
254 for (i = 0; i < len; i++)
255 dst[i] = fdt32_to_cpu(src[len - 1 - i]);
258 int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
259 struct key_prop *prop, uint8_t *out)
261 struct rsa_public_key key;
265 debug("%s: Skipping invalid prop", __func__);
268 key.n0inv = prop->n0inv;
269 key.len = prop->num_bits;
271 if (!prop->public_exponent)
272 key.exponent = RSA_DEFAULT_PUBEXP;
274 key.exponent = fdt64_to_cpup(prop->public_exponent);
276 if (!key.len || !prop->modulus || !prop->rr) {
277 debug("%s: Missing RSA key info", __func__);
281 /* Sanity check for stack size */
282 if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
283 debug("RSA key bits %u outside allowed range %d..%d\n",
284 key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
287 key.len /= sizeof(uint32_t) * 8;
288 uint32_t key1[key.len], key2[key.len];
292 rsa_convert_big_endian(key.modulus, (uint32_t *)prop->modulus, key.len);
293 rsa_convert_big_endian(key.rr, (uint32_t *)prop->rr, key.len);
294 if (!key.modulus || !key.rr) {
295 debug("%s: Out of memory", __func__);
299 uint32_t buf[sig_len / sizeof(uint32_t)];
301 memcpy(buf, sig, sig_len);
303 ret = pow_mod(&key, buf);
307 memcpy(out, buf, sig_len);
312 #if defined(CONFIG_CMD_ZYNQ_RSA)
314 * zynq_pow_mod - in-place public exponentiation
317 * @inout: Big-endian word array containing value and result
318 * Return: 0 on successful calculation, otherwise failure error code
320 * FIXME: Use pow_mod() instead of zynq_pow_mod()
321 * pow_mod calculation required for zynq is bit different from
322 * pw_mod above here, hence defined zynq specific routine.
324 int zynq_pow_mod(uint32_t *keyptr, uint32_t *inout)
328 struct rsa_public_key *key;
329 u32 val[RSA2048_BYTES], acc[RSA2048_BYTES], tmp[RSA2048_BYTES];
331 key = (struct rsa_public_key *)keyptr;
333 /* Sanity check for stack size - key->len is in 32-bit words */
334 if (key->len > RSA_MAX_KEY_BITS / 32) {
335 debug("RSA key words %u exceeds maximum %d\n", key->len,
336 RSA_MAX_KEY_BITS / 32);
340 result = tmp; /* Re-use location. */
342 for (i = 0, ptr = inout; i < key->len; i++, ptr++)
345 montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
346 for (i = 0; i < 16; i += 2) {
347 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
348 montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
350 montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
352 /* Make sure result < mod; result is at most 1x mod too large. */
353 if (greater_equal_modulus(key, result))
354 subtract_modulus(key, result);
356 for (i = 0, ptr = inout; i < key->len; i++, ptr++)