blob: 3e7b7982890bc95e20eb130cd6ce722ebb82c381 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass35191a32013-06-13 15:10:02 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass35191a32013-06-13 15:10:02 -07004 */
5
Heinrich Schuchardt43ee8012021-12-18 11:25:12 +01006#define OPENSSL_API_COMPAT 0x10101000L
7
Simon Glass35191a32013-06-13 15:10:02 -07008#include "mkimage.h"
Jonathan Graycc3ed402020-05-15 22:20:34 +10009#include <stdlib.h>
Simon Glass35191a32013-06-13 15:10:02 -070010#include <stdio.h>
11#include <string.h>
Simon Glass35191a32013-06-13 15:10:02 -070012#include <image.h>
13#include <time.h>
Alexandru Gagniuc32abcbd2021-02-19 12:45:11 -060014#include <u-boot/fdt-libcrypto.h>
Jelle van der Waae9805902017-05-08 21:31:19 +020015#include <openssl/bn.h>
Chan, Donald0bdd4ee2021-04-01 22:42:36 +000016#include <openssl/ec.h>
Simon Glass35191a32013-06-13 15:10:02 -070017#include <openssl/rsa.h>
18#include <openssl/pem.h>
19#include <openssl/err.h>
20#include <openssl/ssl.h>
21#include <openssl/evp.h>
George McCollister23d14892017-01-06 13:14:17 -060022#include <openssl/engine.h>
Simon Glass35191a32013-06-13 15:10:02 -070023
Simon Glass35191a32013-06-13 15:10:02 -070024static int rsa_err(const char *msg)
25{
26 unsigned long sslErr = ERR_get_error();
27
28 fprintf(stderr, "%s", msg);
29 fprintf(stderr, ": %s\n",
30 ERR_error_string(sslErr, 0));
31
32 return -1;
33}
34
35/**
George McCollister23d14892017-01-06 13:14:17 -060036 * rsa_pem_get_pub_key() - read a public key from a .crt file
Simon Glass35191a32013-06-13 15:10:02 -070037 *
38 * @keydir: Directory containins the key
39 * @name Name of key file (will have a .crt extension)
Chan, Donald0bdd4ee2021-04-01 22:42:36 +000040 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010041 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
Simon Glass35191a32013-06-13 15:10:02 -070042 */
Chan, Donald0bdd4ee2021-04-01 22:42:36 +000043static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **evpp)
Simon Glass35191a32013-06-13 15:10:02 -070044{
45 char path[1024];
Chan, Donald0bdd4ee2021-04-01 22:42:36 +000046 EVP_PKEY *key = NULL;
Simon Glass35191a32013-06-13 15:10:02 -070047 X509 *cert;
Simon Glass35191a32013-06-13 15:10:02 -070048 FILE *f;
49 int ret;
50
Chan, Donald0bdd4ee2021-04-01 22:42:36 +000051 if (!evpp)
52 return -EINVAL;
53
54 *evpp = NULL;
Simon Glass35191a32013-06-13 15:10:02 -070055 snprintf(path, sizeof(path), "%s/%s.crt", keydir, name);
56 f = fopen(path, "r");
57 if (!f) {
58 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
59 path, strerror(errno));
60 return -EACCES;
61 }
62
63 /* Read the certificate */
64 cert = NULL;
65 if (!PEM_read_X509(f, &cert, NULL, NULL)) {
66 rsa_err("Couldn't read certificate");
67 ret = -EINVAL;
68 goto err_cert;
69 }
70
71 /* Get the public key from the certificate. */
72 key = X509_get_pubkey(cert);
73 if (!key) {
74 rsa_err("Couldn't read public key\n");
75 ret = -EINVAL;
76 goto err_pubkey;
77 }
78
Simon Glass35191a32013-06-13 15:10:02 -070079 fclose(f);
Chan, Donald0bdd4ee2021-04-01 22:42:36 +000080 *evpp = key;
Simon Glass35191a32013-06-13 15:10:02 -070081 X509_free(cert);
Simon Glass35191a32013-06-13 15:10:02 -070082
83 return 0;
84
Simon Glass35191a32013-06-13 15:10:02 -070085err_pubkey:
86 X509_free(cert);
87err_cert:
88 fclose(f);
89 return ret;
90}
91
92/**
George McCollister23d14892017-01-06 13:14:17 -060093 * rsa_engine_get_pub_key() - read a public key from given engine
Simon Glass35191a32013-06-13 15:10:02 -070094 *
George McCollister23d14892017-01-06 13:14:17 -060095 * @keydir: Key prefix
96 * @name Name of key
97 * @engine Engine to use
Chan, Donald0bdd4ee2021-04-01 22:42:36 +000098 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010099 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollister23d14892017-01-06 13:14:17 -0600100 */
101static int rsa_engine_get_pub_key(const char *keydir, const char *name,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000102 ENGINE *engine, EVP_PKEY **evpp)
George McCollister23d14892017-01-06 13:14:17 -0600103{
104 const char *engine_id;
105 char key_id[1024];
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000106 EVP_PKEY *key = NULL;
107
108 if (!evpp)
109 return -EINVAL;
George McCollister23d14892017-01-06 13:14:17 -0600110
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000111 *evpp = NULL;
George McCollister23d14892017-01-06 13:14:17 -0600112
113 engine_id = ENGINE_get_id(engine);
114
115 if (engine_id && !strcmp(engine_id, "pkcs11")) {
116 if (keydir)
Jan Luebbecd4eafb2020-05-13 12:26:24 +0200117 if (strstr(keydir, "object="))
118 snprintf(key_id, sizeof(key_id),
119 "pkcs11:%s;type=public",
120 keydir);
121 else
122 snprintf(key_id, sizeof(key_id),
123 "pkcs11:%s;object=%s;type=public",
124 keydir, name);
George McCollister23d14892017-01-06 13:14:17 -0600125 else
126 snprintf(key_id, sizeof(key_id),
127 "pkcs11:object=%s;type=public",
128 name);
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300129 } else if (engine_id) {
130 if (keydir)
131 snprintf(key_id, sizeof(key_id),
132 "%s%s",
133 keydir, name);
134 else
135 snprintf(key_id, sizeof(key_id),
136 "%s",
137 name);
George McCollister23d14892017-01-06 13:14:17 -0600138 } else {
139 fprintf(stderr, "Engine not supported\n");
140 return -ENOTSUP;
141 }
142
143 key = ENGINE_load_public_key(engine, key_id, NULL, NULL);
144 if (!key)
145 return rsa_err("Failure loading public key from engine");
146
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000147 *evpp = key;
George McCollister23d14892017-01-06 13:14:17 -0600148
149 return 0;
George McCollister23d14892017-01-06 13:14:17 -0600150}
151
152/**
153 * rsa_get_pub_key() - read a public key
154 *
155 * @keydir: Directory containing the key (PEM file) or key prefix (engine)
156 * @name Name of key file (will have a .crt extension)
157 * @engine Engine to use
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000158 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100159 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollister23d14892017-01-06 13:14:17 -0600160 */
161static int rsa_get_pub_key(const char *keydir, const char *name,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000162 ENGINE *engine, EVP_PKEY **evpp)
George McCollister23d14892017-01-06 13:14:17 -0600163{
164 if (engine)
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000165 return rsa_engine_get_pub_key(keydir, name, engine, evpp);
166 return rsa_pem_get_pub_key(keydir, name, evpp);
George McCollister23d14892017-01-06 13:14:17 -0600167}
168
169/**
170 * rsa_pem_get_priv_key() - read a private key from a .key file
171 *
172 * @keydir: Directory containing the key
Simon Glass35191a32013-06-13 15:10:02 -0700173 * @name Name of key file (will have a .key extension)
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000174 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100175 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
Simon Glass35191a32013-06-13 15:10:02 -0700176 */
George McCollister23d14892017-01-06 13:14:17 -0600177static int rsa_pem_get_priv_key(const char *keydir, const char *name,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000178 const char *keyfile, EVP_PKEY **evpp)
Simon Glass35191a32013-06-13 15:10:02 -0700179{
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000180 char path[1024] = {0};
181 FILE *f = NULL;
182
183 if (!evpp)
184 return -EINVAL;
Simon Glass35191a32013-06-13 15:10:02 -0700185
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000186 *evpp = NULL;
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600187 if (keydir && name)
188 snprintf(path, sizeof(path), "%s/%s.key", keydir, name);
189 else if (keyfile)
190 snprintf(path, sizeof(path), "%s", keyfile);
191 else
192 return -EINVAL;
193
Simon Glass35191a32013-06-13 15:10:02 -0700194 f = fopen(path, "r");
195 if (!f) {
196 fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
197 path, strerror(errno));
198 return -ENOENT;
199 }
200
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000201 if (!PEM_read_PrivateKey(f, evpp, NULL, path)) {
Simon Glass35191a32013-06-13 15:10:02 -0700202 rsa_err("Failure reading private key");
203 fclose(f);
204 return -EPROTO;
205 }
206 fclose(f);
Simon Glass35191a32013-06-13 15:10:02 -0700207
208 return 0;
209}
210
George McCollister23d14892017-01-06 13:14:17 -0600211/**
212 * rsa_engine_get_priv_key() - read a private key from given engine
213 *
214 * @keydir: Key prefix
215 * @name Name of key
216 * @engine Engine to use
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000217 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100218 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollister23d14892017-01-06 13:14:17 -0600219 */
220static int rsa_engine_get_priv_key(const char *keydir, const char *name,
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600221 const char *keyfile,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000222 ENGINE *engine, EVP_PKEY **evpp)
George McCollister23d14892017-01-06 13:14:17 -0600223{
224 const char *engine_id;
225 char key_id[1024];
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000226 EVP_PKEY *key = NULL;
George McCollister23d14892017-01-06 13:14:17 -0600227
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000228 if (!evpp)
229 return -EINVAL;
George McCollister23d14892017-01-06 13:14:17 -0600230
231 engine_id = ENGINE_get_id(engine);
232
233 if (engine_id && !strcmp(engine_id, "pkcs11")) {
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600234 if (!keydir && !name) {
235 fprintf(stderr, "Please use 'keydir' with PKCS11\n");
236 return -EINVAL;
237 }
George McCollister23d14892017-01-06 13:14:17 -0600238 if (keydir)
Jan Luebbecd4eafb2020-05-13 12:26:24 +0200239 if (strstr(keydir, "object="))
240 snprintf(key_id, sizeof(key_id),
241 "pkcs11:%s;type=private",
242 keydir);
243 else
244 snprintf(key_id, sizeof(key_id),
245 "pkcs11:%s;object=%s;type=private",
246 keydir, name);
George McCollister23d14892017-01-06 13:14:17 -0600247 else
248 snprintf(key_id, sizeof(key_id),
249 "pkcs11:object=%s;type=private",
250 name);
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300251 } else if (engine_id) {
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600252 if (keydir && name)
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300253 snprintf(key_id, sizeof(key_id),
254 "%s%s",
255 keydir, name);
Heinrich Schuchardt48806ee2021-08-28 12:13:05 +0200256 else if (name)
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300257 snprintf(key_id, sizeof(key_id),
258 "%s",
Heinrich Schuchardt1e92a8a2021-07-30 17:05:07 +0200259 name ? name : "");
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600260 else if (keyfile)
261 snprintf(key_id, sizeof(key_id), "%s", keyfile);
262 else
263 return -EINVAL;
264
George McCollister23d14892017-01-06 13:14:17 -0600265 } else {
266 fprintf(stderr, "Engine not supported\n");
267 return -ENOTSUP;
268 }
269
270 key = ENGINE_load_private_key(engine, key_id, NULL, NULL);
271 if (!key)
272 return rsa_err("Failure loading private key from engine");
273
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000274 *evpp = key;
George McCollister23d14892017-01-06 13:14:17 -0600275
276 return 0;
George McCollister23d14892017-01-06 13:14:17 -0600277}
278
279/**
280 * rsa_get_priv_key() - read a private key
281 *
282 * @keydir: Directory containing the key (PEM file) or key prefix (engine)
283 * @name Name of key
284 * @engine Engine to use for signing
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000285 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100286 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollister23d14892017-01-06 13:14:17 -0600287 */
288static int rsa_get_priv_key(const char *keydir, const char *name,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000289 const char *keyfile, ENGINE *engine, EVP_PKEY **evpp)
George McCollister23d14892017-01-06 13:14:17 -0600290{
291 if (engine)
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600292 return rsa_engine_get_priv_key(keydir, name, keyfile, engine,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000293 evpp);
294 return rsa_pem_get_priv_key(keydir, name, keyfile, evpp);
George McCollister23d14892017-01-06 13:14:17 -0600295}
296
Simon Glass35191a32013-06-13 15:10:02 -0700297static int rsa_init(void)
298{
299 int ret;
300
Jelle van der Waae9805902017-05-08 21:31:19 +0200301 ret = OPENSSL_init_ssl(0, NULL);
Simon Glass35191a32013-06-13 15:10:02 -0700302 if (!ret) {
303 fprintf(stderr, "Failure to init SSL library\n");
304 return -1;
305 }
Simon Glass35191a32013-06-13 15:10:02 -0700306
307 return 0;
308}
309
George McCollister23d14892017-01-06 13:14:17 -0600310static int rsa_engine_init(const char *engine_id, ENGINE **pe)
311{
Marc Kleine-Budde88e07a72021-07-23 22:17:50 +0200312 const char *key_pass;
George McCollister23d14892017-01-06 13:14:17 -0600313 ENGINE *e;
314 int ret;
315
316 ENGINE_load_builtin_engines();
317
318 e = ENGINE_by_id(engine_id);
319 if (!e) {
320 fprintf(stderr, "Engine isn't available\n");
Alexandru Gagniucb63dc082021-07-29 13:31:21 -0500321 return -1;
George McCollister23d14892017-01-06 13:14:17 -0600322 }
323
324 if (!ENGINE_init(e)) {
325 fprintf(stderr, "Couldn't initialize engine\n");
326 ret = -1;
327 goto err_engine_init;
328 }
329
330 if (!ENGINE_set_default_RSA(e)) {
331 fprintf(stderr, "Couldn't set engine as default for RSA\n");
332 ret = -1;
333 goto err_set_rsa;
334 }
335
Marc Kleine-Budde88e07a72021-07-23 22:17:50 +0200336 key_pass = getenv("MKIMAGE_SIGN_PIN");
337 if (key_pass) {
338 if (!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
339 fprintf(stderr, "Couldn't set PIN\n");
340 ret = -1;
341 goto err_set_pin;
342 }
343 }
344
George McCollister23d14892017-01-06 13:14:17 -0600345 *pe = e;
346
347 return 0;
348
Marc Kleine-Budde88e07a72021-07-23 22:17:50 +0200349err_set_pin:
George McCollister23d14892017-01-06 13:14:17 -0600350err_set_rsa:
351 ENGINE_finish(e);
352err_engine_init:
353 ENGINE_free(e);
George McCollister23d14892017-01-06 13:14:17 -0600354 return ret;
355}
356
George McCollister23d14892017-01-06 13:14:17 -0600357static void rsa_engine_remove(ENGINE *e)
358{
359 if (e) {
360 ENGINE_finish(e);
361 ENGINE_free(e);
362 }
363}
364
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000365static int rsa_sign_with_key(EVP_PKEY *pkey, struct padding_algo *padding_algo,
Philippe Reynes12468352018-11-14 13:51:00 +0100366 struct checksum_algo *checksum_algo,
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100367 const struct image_region region[], int region_count,
368 uint8_t **sigp, uint *sig_size)
Simon Glass35191a32013-06-13 15:10:02 -0700369{
Philippe Reynes12468352018-11-14 13:51:00 +0100370 EVP_PKEY_CTX *ckey;
Simon Glass35191a32013-06-13 15:10:02 -0700371 EVP_MD_CTX *context;
Philippe Reynes8161ff82018-11-14 13:50:59 +0100372 int ret = 0;
373 size_t size;
Simon Glass35191a32013-06-13 15:10:02 -0700374 uint8_t *sig;
375 int i;
376
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000377 size = EVP_PKEY_size(pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700378 sig = malloc(size);
379 if (!sig) {
Philippe Reynes8161ff82018-11-14 13:50:59 +0100380 fprintf(stderr, "Out of memory for signature (%zu bytes)\n",
Simon Glass35191a32013-06-13 15:10:02 -0700381 size);
382 ret = -ENOMEM;
383 goto err_alloc;
384 }
385
386 context = EVP_MD_CTX_create();
387 if (!context) {
388 ret = rsa_err("EVP context creation failed");
389 goto err_create;
390 }
391 EVP_MD_CTX_init(context);
Philippe Reynes12468352018-11-14 13:51:00 +0100392
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000393 ckey = EVP_PKEY_CTX_new(pkey, NULL);
Philippe Reynes12468352018-11-14 13:51:00 +0100394 if (!ckey) {
395 ret = rsa_err("EVP key context creation failed");
396 goto err_create;
397 }
398
399 if (EVP_DigestSignInit(context, &ckey,
Philippe Reynes8161ff82018-11-14 13:50:59 +0100400 checksum_algo->calculate_sign(),
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000401 NULL, pkey) <= 0) {
Simon Glass35191a32013-06-13 15:10:02 -0700402 ret = rsa_err("Signer setup failed");
403 goto err_sign;
404 }
405
Simon Glasseffa4522021-09-25 19:43:23 -0600406 if (CONFIG_IS_ENABLED(FIT_RSASSA_PSS) && padding_algo &&
407 !strcmp(padding_algo->name, "pss")) {
Philippe Reynes47d73f02018-11-14 13:51:01 +0100408 if (EVP_PKEY_CTX_set_rsa_padding(ckey,
409 RSA_PKCS1_PSS_PADDING) <= 0) {
410 ret = rsa_err("Signer padding setup failed");
411 goto err_sign;
412 }
413 }
Philippe Reynes47d73f02018-11-14 13:51:01 +0100414
Simon Glass35191a32013-06-13 15:10:02 -0700415 for (i = 0; i < region_count; i++) {
Philippe Reynes8161ff82018-11-14 13:50:59 +0100416 if (!EVP_DigestSignUpdate(context, region[i].data,
417 region[i].size)) {
Simon Glass35191a32013-06-13 15:10:02 -0700418 ret = rsa_err("Signing data failed");
419 goto err_sign;
420 }
421 }
422
Philippe Reynes8161ff82018-11-14 13:50:59 +0100423 if (!EVP_DigestSignFinal(context, sig, &size)) {
Simon Glass35191a32013-06-13 15:10:02 -0700424 ret = rsa_err("Could not obtain signature");
425 goto err_sign;
426 }
Philippe Reynes8161ff82018-11-14 13:50:59 +0100427
Alexandru Gagniucb63dc082021-07-29 13:31:21 -0500428 EVP_MD_CTX_reset(context);
Simon Glass35191a32013-06-13 15:10:02 -0700429 EVP_MD_CTX_destroy(context);
Simon Glass35191a32013-06-13 15:10:02 -0700430
Chan, Donald9ccd6052021-07-19 09:18:54 -0700431 debug("Got signature: %zu bytes, expected %d\n", size, EVP_PKEY_size(pkey));
Simon Glass35191a32013-06-13 15:10:02 -0700432 *sigp = sig;
433 *sig_size = size;
434
435 return 0;
436
437err_sign:
438 EVP_MD_CTX_destroy(context);
439err_create:
440 free(sig);
441err_alloc:
Simon Glass35191a32013-06-13 15:10:02 -0700442 return ret;
443}
444
445int rsa_sign(struct image_sign_info *info,
446 const struct image_region region[], int region_count,
447 uint8_t **sigp, uint *sig_len)
448{
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000449 EVP_PKEY *pkey = NULL;
George McCollister23d14892017-01-06 13:14:17 -0600450 ENGINE *e = NULL;
Simon Glass35191a32013-06-13 15:10:02 -0700451 int ret;
452
453 ret = rsa_init();
454 if (ret)
455 return ret;
456
George McCollister23d14892017-01-06 13:14:17 -0600457 if (info->engine_id) {
458 ret = rsa_engine_init(info->engine_id, &e);
459 if (ret)
Alexandru Gagniucb63dc082021-07-29 13:31:21 -0500460 return ret;
George McCollister23d14892017-01-06 13:14:17 -0600461 }
462
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600463 ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000464 e, &pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700465 if (ret)
466 goto err_priv;
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000467 ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region,
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100468 region_count, sigp, sig_len);
Simon Glass35191a32013-06-13 15:10:02 -0700469 if (ret)
470 goto err_sign;
471
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000472 EVP_PKEY_free(pkey);
George McCollister23d14892017-01-06 13:14:17 -0600473 if (info->engine_id)
474 rsa_engine_remove(e);
Simon Glass35191a32013-06-13 15:10:02 -0700475
476 return ret;
477
478err_sign:
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000479 EVP_PKEY_free(pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700480err_priv:
George McCollister23d14892017-01-06 13:14:17 -0600481 if (info->engine_id)
482 rsa_engine_remove(e);
Simon Glass35191a32013-06-13 15:10:02 -0700483 return ret;
484}
485
486/*
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200487 * rsa_get_exponent(): - Get the public exponent from an RSA key
488 */
489static int rsa_get_exponent(RSA *key, uint64_t *e)
490{
491 int ret;
492 BIGNUM *bn_te;
Jelle van der Waae9805902017-05-08 21:31:19 +0200493 const BIGNUM *key_e;
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200494 uint64_t te;
495
496 ret = -EINVAL;
497 bn_te = NULL;
498
499 if (!e)
500 goto cleanup;
501
Jelle van der Waae9805902017-05-08 21:31:19 +0200502 RSA_get0_key(key, NULL, &key_e, NULL);
503 if (BN_num_bits(key_e) > 64)
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200504 goto cleanup;
505
Jelle van der Waae9805902017-05-08 21:31:19 +0200506 *e = BN_get_word(key_e);
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200507
Jelle van der Waae9805902017-05-08 21:31:19 +0200508 if (BN_num_bits(key_e) < 33) {
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200509 ret = 0;
510 goto cleanup;
511 }
512
Jelle van der Waae9805902017-05-08 21:31:19 +0200513 bn_te = BN_dup(key_e);
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200514 if (!bn_te)
515 goto cleanup;
516
517 if (!BN_rshift(bn_te, bn_te, 32))
518 goto cleanup;
519
520 if (!BN_mask_bits(bn_te, 32))
521 goto cleanup;
522
523 te = BN_get_word(bn_te);
524 te <<= 32;
525 *e |= te;
526 ret = 0;
527
528cleanup:
529 if (bn_te)
530 BN_free(bn_te);
531
532 return ret;
533}
534
535/*
Simon Glass35191a32013-06-13 15:10:02 -0700536 * rsa_get_params(): - Get the important parameters of an RSA public key
537 */
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200538int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
539 BIGNUM **modulusp, BIGNUM **r_squaredp)
Simon Glass35191a32013-06-13 15:10:02 -0700540{
541 BIGNUM *big1, *big2, *big32, *big2_32;
542 BIGNUM *n, *r, *r_squared, *tmp;
Jelle van der Waae9805902017-05-08 21:31:19 +0200543 const BIGNUM *key_n;
Simon Glass35191a32013-06-13 15:10:02 -0700544 BN_CTX *bn_ctx = BN_CTX_new();
545 int ret = 0;
546
547 /* Initialize BIGNUMs */
548 big1 = BN_new();
549 big2 = BN_new();
550 big32 = BN_new();
551 r = BN_new();
552 r_squared = BN_new();
553 tmp = BN_new();
554 big2_32 = BN_new();
555 n = BN_new();
556 if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
557 !n) {
558 fprintf(stderr, "Out of memory (bignum)\n");
559 return -ENOMEM;
560 }
561
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200562 if (0 != rsa_get_exponent(key, exponent))
563 ret = -1;
564
Jelle van der Waae9805902017-05-08 21:31:19 +0200565 RSA_get0_key(key, &key_n, NULL, NULL);
566 if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
Simon Glass35191a32013-06-13 15:10:02 -0700567 !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
568 ret = -1;
569
570 /* big2_32 = 2^32 */
571 if (!BN_exp(big2_32, big2, big32, bn_ctx))
572 ret = -1;
573
574 /* Calculate n0_inv = -1 / n[0] mod 2^32 */
575 if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
576 !BN_sub(tmp, big2_32, tmp))
577 ret = -1;
578 *n0_invp = BN_get_word(tmp);
579
580 /* Calculate R = 2^(# of key bits) */
581 if (!BN_set_word(tmp, BN_num_bits(n)) ||
582 !BN_exp(r, big2, tmp, bn_ctx))
583 ret = -1;
584
585 /* Calculate r_squared = R^2 mod n */
586 if (!BN_copy(r_squared, r) ||
587 !BN_mul(tmp, r_squared, r, bn_ctx) ||
588 !BN_mod(r_squared, tmp, n, bn_ctx))
589 ret = -1;
590
591 *modulusp = n;
592 *r_squaredp = r_squared;
593
594 BN_free(big1);
595 BN_free(big2);
596 BN_free(big32);
597 BN_free(r);
598 BN_free(tmp);
599 BN_free(big2_32);
600 if (ret) {
601 fprintf(stderr, "Bignum operations failed\n");
602 return -ENOMEM;
603 }
604
605 return ret;
606}
607
Simon Glass35191a32013-06-13 15:10:02 -0700608int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
609{
610 BIGNUM *modulus, *r_squared;
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200611 uint64_t exponent;
Simon Glass35191a32013-06-13 15:10:02 -0700612 uint32_t n0_inv;
613 int parent, node;
614 char name[100];
615 int ret;
616 int bits;
617 RSA *rsa;
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000618 EVP_PKEY *pkey = NULL;
George McCollister23d14892017-01-06 13:14:17 -0600619 ENGINE *e = NULL;
Simon Glass35191a32013-06-13 15:10:02 -0700620
621 debug("%s: Getting verification data\n", __func__);
George McCollister23d14892017-01-06 13:14:17 -0600622 if (info->engine_id) {
623 ret = rsa_engine_init(info->engine_id, &e);
624 if (ret)
625 return ret;
626 }
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000627 ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700628 if (ret)
George McCollister23d14892017-01-06 13:14:17 -0600629 goto err_get_pub_key;
Alexandru Gagniucb63dc082021-07-29 13:31:21 -0500630
Heinrich Schuchardtdccc9da2022-01-09 15:39:40 +0100631 rsa = (RSA *)EVP_PKEY_get0_RSA(pkey);
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200632 ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
Simon Glass35191a32013-06-13 15:10:02 -0700633 if (ret)
George McCollister23d14892017-01-06 13:14:17 -0600634 goto err_get_params;
Simon Glass35191a32013-06-13 15:10:02 -0700635 bits = BN_num_bits(modulus);
636 parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
637 if (parent == -FDT_ERR_NOTFOUND) {
638 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
639 if (parent < 0) {
Simon Glass9423bad2014-06-12 07:24:42 -0600640 ret = parent;
641 if (ret != -FDT_ERR_NOSPACE) {
642 fprintf(stderr, "Couldn't create signature node: %s\n",
643 fdt_strerror(parent));
644 }
Simon Glass35191a32013-06-13 15:10:02 -0700645 }
646 }
Simon Glass9423bad2014-06-12 07:24:42 -0600647 if (ret)
648 goto done;
Simon Glass35191a32013-06-13 15:10:02 -0700649
650 /* Either create or overwrite the named key node */
651 snprintf(name, sizeof(name), "key-%s", info->keyname);
652 node = fdt_subnode_offset(keydest, parent, name);
653 if (node == -FDT_ERR_NOTFOUND) {
654 node = fdt_add_subnode(keydest, parent, name);
655 if (node < 0) {
Simon Glass9423bad2014-06-12 07:24:42 -0600656 ret = node;
657 if (ret != -FDT_ERR_NOSPACE) {
658 fprintf(stderr, "Could not create key subnode: %s\n",
659 fdt_strerror(node));
660 }
Simon Glass35191a32013-06-13 15:10:02 -0700661 }
662 } else if (node < 0) {
663 fprintf(stderr, "Cannot select keys parent: %s\n",
664 fdt_strerror(node));
Simon Glass9423bad2014-06-12 07:24:42 -0600665 ret = node;
Simon Glass35191a32013-06-13 15:10:02 -0700666 }
667
Simon Glass9423bad2014-06-12 07:24:42 -0600668 if (!ret) {
Simon Glassd7aabcc2020-03-18 11:44:06 -0600669 ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT,
670 info->keyname);
Simon Glass9423bad2014-06-12 07:24:42 -0600671 }
Simon Glassaf2f9d52014-06-02 22:04:51 -0600672 if (!ret)
673 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
674 if (!ret)
675 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
676 if (!ret) {
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200677 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
678 }
679 if (!ret) {
Simon Glassaf2f9d52014-06-02 22:04:51 -0600680 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
681 bits);
682 }
683 if (!ret) {
684 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
685 bits);
686 }
687 if (!ret) {
688 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
Andrew Duda6616c822016-11-08 18:53:41 +0000689 info->name);
Simon Glassaf2f9d52014-06-02 22:04:51 -0600690 }
mario.six@gdsys.cc99fc6ea2016-07-19 11:07:07 +0200691 if (!ret && info->require_keys) {
Simon Glassd7aabcc2020-03-18 11:44:06 -0600692 ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED,
Simon Glassaf2f9d52014-06-02 22:04:51 -0600693 info->require_keys);
Simon Glass35191a32013-06-13 15:10:02 -0700694 }
Simon Glass9423bad2014-06-12 07:24:42 -0600695done:
Simon Glass35191a32013-06-13 15:10:02 -0700696 BN_free(modulus);
697 BN_free(r_squared);
698 if (ret)
George McCollister23d14892017-01-06 13:14:17 -0600699 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
700err_get_params:
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000701 EVP_PKEY_free(pkey);
George McCollister23d14892017-01-06 13:14:17 -0600702err_get_pub_key:
703 if (info->engine_id)
704 rsa_engine_remove(e);
Simon Glass35191a32013-06-13 15:10:02 -0700705
Simon Glass94336dc2021-11-12 12:28:11 -0700706 if (ret)
707 return ret;
708
709 return node;
Simon Glass35191a32013-06-13 15:10:02 -0700710}