blob: 74f9eb16cc2642dfde22ece10758e8154f5434fc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Guptafc2f4242015-01-23 16:01:50 +05302/*
3 * Copyright (c) 2013, Google Inc.
Ruchika Guptafc2f4242015-01-23 16:01:50 +05304 */
5
6#ifndef USE_HOSTCC
7#include <common.h>
8#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Ruchika Guptafc2f4242015-01-23 16:01:50 +053010#include <asm/types.h>
11#include <asm/byteorder.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090012#include <linux/errno.h>
Ruchika Guptafc2f4242015-01-23 16:01:50 +053013#include <asm/types.h>
14#include <asm/unaligned.h>
15#else
16#include "fdt_host.h"
17#include "mkimage.h"
18#include <fdt_support.h>
19#endif
20#include <u-boot/rsa.h>
21#include <u-boot/rsa-mod-exp.h>
22
23#define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
24
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))
27
Rasmus Villemoes3f8808e2020-10-06 12:09:45 +020028static inline uint64_t fdt64_to_cpup(const void *p)
29{
30 fdt64_t w;
31
32 memcpy(&w, p, sizeof(w));
33 return fdt64_to_cpu(w);
34}
35
Ruchika Guptafc2f4242015-01-23 16:01:50 +053036/* Default public exponent for backward compatibility */
37#define RSA_DEFAULT_PUBEXP 65537
38
39/**
40 * subtract_modulus() - subtract modulus from the given value
41 *
42 * @key: Key containing modulus to subtract
43 * @num: Number to subtract modulus from, as little endian word array
44 */
45static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
46{
47 int64_t acc = 0;
48 uint i;
49
50 for (i = 0; i < key->len; i++) {
51 acc += (uint64_t)num[i] - key->modulus[i];
52 num[i] = (uint32_t)acc;
53 acc >>= 32;
54 }
55}
56
57/**
58 * greater_equal_modulus() - check if a value is >= modulus
59 *
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
63 */
64static int greater_equal_modulus(const struct rsa_public_key *key,
65 uint32_t num[])
66{
67 int i;
68
69 for (i = (int)key->len - 1; i >= 0; i--) {
70 if (num[i] < key->modulus[i])
71 return 0;
72 if (num[i] > key->modulus[i])
73 return 1;
74 }
75
76 return 1; /* equal */
77}
78
79/**
80 * montgomery_mul_add_step() - Perform montgomery multiply-add step
81 *
82 * Operation: montgomery result[] += a * b[] / n0inv % modulus
83 *
84 * @key: RSA key
85 * @result: Place to put result, as little endian word array
86 * @a: Multiplier
87 * @b: Multiplicand, as little endian word array
88 */
89static void montgomery_mul_add_step(const struct rsa_public_key *key,
90 uint32_t result[], const uint32_t a, const uint32_t b[])
91{
92 uint64_t acc_a, acc_b;
93 uint32_t d0;
94 uint i;
95
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] +
102 (uint32_t)acc_a;
103 result[i - 1] = (uint32_t)acc_b;
104 }
105
106 acc_a = (acc_a >> 32) + (acc_b >> 32);
107
108 result[i - 1] = (uint32_t)acc_a;
109
110 if (acc_a >> 32)
111 subtract_modulus(key, result);
112}
113
114/**
115 * montgomery_mul() - Perform montgomery mutitply
116 *
117 * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
118 *
119 * @key: RSA key
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
123 */
124static void montgomery_mul(const struct rsa_public_key *key,
125 uint32_t result[], uint32_t a[], const uint32_t b[])
126{
127 uint i;
128
129 for (i = 0; i < key->len; ++i)
130 result[i] = 0;
131 for (i = 0; i < key->len; ++i)
132 montgomery_mul_add_step(key, result, a[i], b);
133}
134
135/**
136 * num_pub_exponent_bits() - Number of bits in the public exponent
137 *
138 * @key: RSA key
139 * @num_bits: Storage for the number of public exponent bits
140 */
141static int num_public_exponent_bits(const struct rsa_public_key *key,
142 int *num_bits)
143{
144 uint64_t exponent;
145 int exponent_bits;
146 const uint max_bits = (sizeof(exponent) * 8);
147
148 exponent = key->exponent;
149 exponent_bits = 0;
150
151 if (!exponent) {
152 *num_bits = exponent_bits;
153 return 0;
154 }
155
156 for (exponent_bits = 1; exponent_bits < max_bits + 1; ++exponent_bits)
157 if (!(exponent >>= 1)) {
158 *num_bits = exponent_bits;
159 return 0;
160 }
161
162 return -EINVAL;
163}
164
165/**
166 * is_public_exponent_bit_set() - Check if a bit in the public exponent is set
167 *
168 * @key: RSA key
169 * @pos: The bit position to check
170 */
171static int is_public_exponent_bit_set(const struct rsa_public_key *key,
172 int pos)
173{
174 return key->exponent & (1ULL << pos);
175}
176
177/**
178 * pow_mod() - in-place public exponentiation
179 *
180 * @key: RSA key
181 * @inout: Big-endian word array containing value and result
182 */
183static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
184{
185 uint32_t *result, *ptr;
186 uint i;
187 int j, k;
188
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);
193 return -EINVAL;
194 }
195
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. */
199
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);
203
204 if (0 != num_public_exponent_bits(key, &k))
205 return -EINVAL;
206
207 if (k < 2) {
208 debug("Public exponent is too short (%d bits, minimum 2)\n",
209 k);
210 return -EINVAL;
211 }
212
213 if (!is_public_exponent_bit_set(key, 0)) {
214 debug("LSB of RSA public exponent must be set.\n");
215 return -EINVAL;
216 }
217
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]));
222
223 for (j = k - 2; j > 0; --j) {
224 montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
225
226 if (is_public_exponent_bit_set(key, j)) {
227 /* acc = tmp * val / R mod n */
228 montgomery_mul(key, acc, tmp, a_scaled);
229 } else {
230 /* e[j] == 0, copy tmp back to acc for next operation */
231 memcpy(acc, tmp, key->len * sizeof(acc[0]));
232 }
233 }
234
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]));
239
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);
243
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);
247 return 0;
248}
249
250static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
251{
252 int i;
253
254 for (i = 0; i < len; i++)
255 dst[i] = fdt32_to_cpu(src[len - 1 - i]);
256}
257
258int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
259 struct key_prop *prop, uint8_t *out)
260{
261 struct rsa_public_key key;
262 int ret;
263
264 if (!prop) {
265 debug("%s: Skipping invalid prop", __func__);
266 return -EBADF;
267 }
268 key.n0inv = prop->n0inv;
269 key.len = prop->num_bits;
270
271 if (!prop->public_exponent)
272 key.exponent = RSA_DEFAULT_PUBEXP;
273 else
Rasmus Villemoes3f8808e2020-10-06 12:09:45 +0200274 key.exponent = fdt64_to_cpup(prop->public_exponent);
Ruchika Guptafc2f4242015-01-23 16:01:50 +0530275
276 if (!key.len || !prop->modulus || !prop->rr) {
277 debug("%s: Missing RSA key info", __func__);
278 return -EFAULT;
279 }
280
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);
285 return -EFAULT;
286 }
287 key.len /= sizeof(uint32_t) * 8;
288 uint32_t key1[key.len], key2[key.len];
289
290 key.modulus = key1;
291 key.rr = key2;
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__);
296 return -ENOMEM;
297 }
298
299 uint32_t buf[sig_len / sizeof(uint32_t)];
300
301 memcpy(buf, sig, sig_len);
302
303 ret = pow_mod(&key, buf);
304 if (ret)
305 return ret;
306
307 memcpy(out, buf, sig_len);
308
309 return 0;
310}
Siva Durga Prasad Paladugu37e3a362018-06-26 15:02:19 +0530311
312#if defined(CONFIG_CMD_ZYNQ_RSA)
313/**
314 * zynq_pow_mod - in-place public exponentiation
315 *
316 * @keyptr: RSA key
317 * @inout: Big-endian word array containing value and result
318 * @return 0 on successful calculation, otherwise failure error code
319 *
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.
323 */
Michal Simekc2a2c832020-10-22 10:59:08 +0200324int zynq_pow_mod(uint32_t *keyptr, uint32_t *inout)
Siva Durga Prasad Paladugu37e3a362018-06-26 15:02:19 +0530325{
326 u32 *result, *ptr;
327 uint i;
328 struct rsa_public_key *key;
329 u32 val[RSA2048_BYTES], acc[RSA2048_BYTES], tmp[RSA2048_BYTES];
330
331 key = (struct rsa_public_key *)keyptr;
332
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);
337 return -EINVAL;
338 }
339
340 result = tmp; /* Re-use location. */
341
342 for (i = 0, ptr = inout; i < key->len; i++, ptr++)
343 val[i] = *(ptr);
344
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 */
349 }
350 montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
351
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);
355
356 for (i = 0, ptr = inout; i < key->len; i++, ptr++)
357 *ptr = result[i];
358
359 return 0;
360}
361#endif