blob: fa9e143b4ca8cbc50297acdfbb4ccb39cb160f84 [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;
Csókás Bencebf665812024-01-05 15:08:04 +0100107 const char *const pkcs11_schema = "pkcs11:";
108 const char *pkcs11_uri_prepend = "";
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000109
110 if (!evpp)
111 return -EINVAL;
George McCollister23d14892017-01-06 13:14:17 -0600112
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000113 *evpp = NULL;
George McCollister23d14892017-01-06 13:14:17 -0600114
115 engine_id = ENGINE_get_id(engine);
116
117 if (engine_id && !strcmp(engine_id, "pkcs11")) {
Csókás Bencebf665812024-01-05 15:08:04 +0100118 if (keydir) {
119 // Check for legacy keydir spec and prepend
120 if (strncmp(pkcs11_schema, keydir, strlen(pkcs11_schema))) {
121 pkcs11_uri_prepend = pkcs11_schema;
122 fprintf(stderr, "WARNING: Legacy URI specified. Please add '%s'.\n", pkcs11_schema);
123 }
124
Jan Luebbecd4eafb2020-05-13 12:26:24 +0200125 if (strstr(keydir, "object="))
126 snprintf(key_id, sizeof(key_id),
Csókás Bencebf665812024-01-05 15:08:04 +0100127 "%s%s;type=public",
128 pkcs11_uri_prepend, keydir);
Jan Luebbecd4eafb2020-05-13 12:26:24 +0200129 else
130 snprintf(key_id, sizeof(key_id),
Csókás Bencebf665812024-01-05 15:08:04 +0100131 "%s%s;object=%s;type=public",
132 pkcs11_uri_prepend, keydir, name);
133 } else {
George McCollister23d14892017-01-06 13:14:17 -0600134 snprintf(key_id, sizeof(key_id),
Csókás Bencec625d9a2024-01-05 15:08:03 +0100135 "pkcs11:object=%s;type=public",
George McCollister23d14892017-01-06 13:14:17 -0600136 name);
Csókás Bencebf665812024-01-05 15:08:04 +0100137 }
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300138 } else if (engine_id) {
139 if (keydir)
140 snprintf(key_id, sizeof(key_id),
141 "%s%s",
142 keydir, name);
143 else
144 snprintf(key_id, sizeof(key_id),
145 "%s",
146 name);
George McCollister23d14892017-01-06 13:14:17 -0600147 } else {
148 fprintf(stderr, "Engine not supported\n");
149 return -ENOTSUP;
150 }
151
152 key = ENGINE_load_public_key(engine, key_id, NULL, NULL);
153 if (!key)
154 return rsa_err("Failure loading public key from engine");
155
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000156 *evpp = key;
George McCollister23d14892017-01-06 13:14:17 -0600157
158 return 0;
George McCollister23d14892017-01-06 13:14:17 -0600159}
160
161/**
162 * rsa_get_pub_key() - read a public key
163 *
164 * @keydir: Directory containing the key (PEM file) or key prefix (engine)
165 * @name Name of key file (will have a .crt extension)
166 * @engine Engine to use
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000167 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100168 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollister23d14892017-01-06 13:14:17 -0600169 */
170static int rsa_get_pub_key(const char *keydir, const char *name,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000171 ENGINE *engine, EVP_PKEY **evpp)
George McCollister23d14892017-01-06 13:14:17 -0600172{
173 if (engine)
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000174 return rsa_engine_get_pub_key(keydir, name, engine, evpp);
175 return rsa_pem_get_pub_key(keydir, name, evpp);
George McCollister23d14892017-01-06 13:14:17 -0600176}
177
178/**
179 * rsa_pem_get_priv_key() - read a private key from a .key file
180 *
181 * @keydir: Directory containing the key
Simon Glass35191a32013-06-13 15:10:02 -0700182 * @name Name of key file (will have a .key extension)
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000183 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100184 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
Simon Glass35191a32013-06-13 15:10:02 -0700185 */
George McCollister23d14892017-01-06 13:14:17 -0600186static int rsa_pem_get_priv_key(const char *keydir, const char *name,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000187 const char *keyfile, EVP_PKEY **evpp)
Simon Glass35191a32013-06-13 15:10:02 -0700188{
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000189 char path[1024] = {0};
190 FILE *f = NULL;
191
192 if (!evpp)
193 return -EINVAL;
Simon Glass35191a32013-06-13 15:10:02 -0700194
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000195 *evpp = NULL;
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600196 if (keydir && name)
197 snprintf(path, sizeof(path), "%s/%s.key", keydir, name);
198 else if (keyfile)
199 snprintf(path, sizeof(path), "%s", keyfile);
200 else
201 return -EINVAL;
202
Simon Glass35191a32013-06-13 15:10:02 -0700203 f = fopen(path, "r");
204 if (!f) {
205 fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
206 path, strerror(errno));
207 return -ENOENT;
208 }
209
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000210 if (!PEM_read_PrivateKey(f, evpp, NULL, path)) {
Simon Glass35191a32013-06-13 15:10:02 -0700211 rsa_err("Failure reading private key");
212 fclose(f);
213 return -EPROTO;
214 }
215 fclose(f);
Simon Glass35191a32013-06-13 15:10:02 -0700216
217 return 0;
218}
219
George McCollister23d14892017-01-06 13:14:17 -0600220/**
221 * rsa_engine_get_priv_key() - read a private key from given engine
222 *
223 * @keydir: Key prefix
224 * @name Name of key
225 * @engine Engine to use
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000226 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100227 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollister23d14892017-01-06 13:14:17 -0600228 */
229static int rsa_engine_get_priv_key(const char *keydir, const char *name,
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600230 const char *keyfile,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000231 ENGINE *engine, EVP_PKEY **evpp)
George McCollister23d14892017-01-06 13:14:17 -0600232{
233 const char *engine_id;
234 char key_id[1024];
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000235 EVP_PKEY *key = NULL;
Csókás Bencebf665812024-01-05 15:08:04 +0100236 const char *const pkcs11_schema = "pkcs11:";
237 const char *pkcs11_uri_prepend = "";
George McCollister23d14892017-01-06 13:14:17 -0600238
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000239 if (!evpp)
240 return -EINVAL;
George McCollister23d14892017-01-06 13:14:17 -0600241
242 engine_id = ENGINE_get_id(engine);
243
244 if (engine_id && !strcmp(engine_id, "pkcs11")) {
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600245 if (!keydir && !name) {
246 fprintf(stderr, "Please use 'keydir' with PKCS11\n");
247 return -EINVAL;
248 }
Csókás Bencebf665812024-01-05 15:08:04 +0100249 if (keydir) {
250 // Check for legacy keydir spec and prepend
251 if (strncmp(pkcs11_schema, keydir, strlen(pkcs11_schema))) {
252 pkcs11_uri_prepend = pkcs11_schema;
253 fprintf(stderr, "WARNING: Legacy URI specified. Please add '%s'.\n", pkcs11_schema);
254 }
255
Jan Luebbecd4eafb2020-05-13 12:26:24 +0200256 if (strstr(keydir, "object="))
257 snprintf(key_id, sizeof(key_id),
Csókás Bencebf665812024-01-05 15:08:04 +0100258 "%s%s;type=private",
259 pkcs11_uri_prepend, keydir);
Jan Luebbecd4eafb2020-05-13 12:26:24 +0200260 else
261 snprintf(key_id, sizeof(key_id),
Csókás Bencebf665812024-01-05 15:08:04 +0100262 "%s%s;object=%s;type=private",
263 pkcs11_uri_prepend, keydir, name);
264 } else {
George McCollister23d14892017-01-06 13:14:17 -0600265 snprintf(key_id, sizeof(key_id),
Csókás Bencec625d9a2024-01-05 15:08:03 +0100266 "pkcs11:object=%s;type=private",
George McCollister23d14892017-01-06 13:14:17 -0600267 name);
Csókás Bencebf665812024-01-05 15:08:04 +0100268 }
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300269 } else if (engine_id) {
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600270 if (keydir && name)
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300271 snprintf(key_id, sizeof(key_id),
272 "%s%s",
273 keydir, name);
Heinrich Schuchardt48806ee2021-08-28 12:13:05 +0200274 else if (name)
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300275 snprintf(key_id, sizeof(key_id),
276 "%s",
Heinrich Schuchardt1e92a8a2021-07-30 17:05:07 +0200277 name ? name : "");
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600278 else if (keyfile)
279 snprintf(key_id, sizeof(key_id), "%s", keyfile);
280 else
281 return -EINVAL;
282
George McCollister23d14892017-01-06 13:14:17 -0600283 } else {
284 fprintf(stderr, "Engine not supported\n");
285 return -ENOTSUP;
286 }
287
288 key = ENGINE_load_private_key(engine, key_id, NULL, NULL);
289 if (!key)
290 return rsa_err("Failure loading private key from engine");
291
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000292 *evpp = key;
George McCollister23d14892017-01-06 13:14:17 -0600293
294 return 0;
George McCollister23d14892017-01-06 13:14:17 -0600295}
296
297/**
298 * rsa_get_priv_key() - read a private key
299 *
300 * @keydir: Directory containing the key (PEM file) or key prefix (engine)
301 * @name Name of key
302 * @engine Engine to use for signing
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000303 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100304 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollister23d14892017-01-06 13:14:17 -0600305 */
306static int rsa_get_priv_key(const char *keydir, const char *name,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000307 const char *keyfile, ENGINE *engine, EVP_PKEY **evpp)
George McCollister23d14892017-01-06 13:14:17 -0600308{
309 if (engine)
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600310 return rsa_engine_get_priv_key(keydir, name, keyfile, engine,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000311 evpp);
312 return rsa_pem_get_priv_key(keydir, name, keyfile, evpp);
George McCollister23d14892017-01-06 13:14:17 -0600313}
314
Simon Glass35191a32013-06-13 15:10:02 -0700315static int rsa_init(void)
316{
317 int ret;
318
Jelle van der Waae9805902017-05-08 21:31:19 +0200319 ret = OPENSSL_init_ssl(0, NULL);
Simon Glass35191a32013-06-13 15:10:02 -0700320 if (!ret) {
321 fprintf(stderr, "Failure to init SSL library\n");
322 return -1;
323 }
Simon Glass35191a32013-06-13 15:10:02 -0700324
325 return 0;
326}
327
George McCollister23d14892017-01-06 13:14:17 -0600328static int rsa_engine_init(const char *engine_id, ENGINE **pe)
329{
Marc Kleine-Budde88e07a72021-07-23 22:17:50 +0200330 const char *key_pass;
George McCollister23d14892017-01-06 13:14:17 -0600331 ENGINE *e;
332 int ret;
333
334 ENGINE_load_builtin_engines();
335
336 e = ENGINE_by_id(engine_id);
337 if (!e) {
Csókás Benced6d59ab2023-12-14 17:54:17 +0100338 fprintf(stderr, "Engine '%s' isn't available\n", engine_id);
339 ERR_print_errors_fp(stderr);
Alexandru Gagniucb63dc082021-07-29 13:31:21 -0500340 return -1;
George McCollister23d14892017-01-06 13:14:17 -0600341 }
342
343 if (!ENGINE_init(e)) {
344 fprintf(stderr, "Couldn't initialize engine\n");
345 ret = -1;
346 goto err_engine_init;
347 }
348
349 if (!ENGINE_set_default_RSA(e)) {
350 fprintf(stderr, "Couldn't set engine as default for RSA\n");
351 ret = -1;
352 goto err_set_rsa;
353 }
354
Marc Kleine-Budde88e07a72021-07-23 22:17:50 +0200355 key_pass = getenv("MKIMAGE_SIGN_PIN");
356 if (key_pass) {
357 if (!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
358 fprintf(stderr, "Couldn't set PIN\n");
359 ret = -1;
360 goto err_set_pin;
361 }
362 }
363
George McCollister23d14892017-01-06 13:14:17 -0600364 *pe = e;
365
366 return 0;
367
Marc Kleine-Budde88e07a72021-07-23 22:17:50 +0200368err_set_pin:
George McCollister23d14892017-01-06 13:14:17 -0600369err_set_rsa:
370 ENGINE_finish(e);
371err_engine_init:
372 ENGINE_free(e);
George McCollister23d14892017-01-06 13:14:17 -0600373 return ret;
374}
375
George McCollister23d14892017-01-06 13:14:17 -0600376static void rsa_engine_remove(ENGINE *e)
377{
378 if (e) {
379 ENGINE_finish(e);
380 ENGINE_free(e);
381 }
382}
383
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000384static int rsa_sign_with_key(EVP_PKEY *pkey, struct padding_algo *padding_algo,
Philippe Reynes12468352018-11-14 13:51:00 +0100385 struct checksum_algo *checksum_algo,
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100386 const struct image_region region[], int region_count,
387 uint8_t **sigp, uint *sig_size)
Simon Glass35191a32013-06-13 15:10:02 -0700388{
Philippe Reynes12468352018-11-14 13:51:00 +0100389 EVP_PKEY_CTX *ckey;
Simon Glass35191a32013-06-13 15:10:02 -0700390 EVP_MD_CTX *context;
Philippe Reynes8161ff82018-11-14 13:50:59 +0100391 int ret = 0;
392 size_t size;
Simon Glass35191a32013-06-13 15:10:02 -0700393 uint8_t *sig;
394 int i;
395
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000396 size = EVP_PKEY_size(pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700397 sig = malloc(size);
398 if (!sig) {
Philippe Reynes8161ff82018-11-14 13:50:59 +0100399 fprintf(stderr, "Out of memory for signature (%zu bytes)\n",
Simon Glass35191a32013-06-13 15:10:02 -0700400 size);
401 ret = -ENOMEM;
402 goto err_alloc;
403 }
404
Yann Droneaud68ca2802022-03-01 16:12:34 +0100405 context = EVP_MD_CTX_new();
Simon Glass35191a32013-06-13 15:10:02 -0700406 if (!context) {
407 ret = rsa_err("EVP context creation failed");
408 goto err_create;
409 }
Philippe Reynes12468352018-11-14 13:51:00 +0100410
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000411 ckey = EVP_PKEY_CTX_new(pkey, NULL);
Philippe Reynes12468352018-11-14 13:51:00 +0100412 if (!ckey) {
413 ret = rsa_err("EVP key context creation failed");
414 goto err_create;
415 }
416
417 if (EVP_DigestSignInit(context, &ckey,
Philippe Reynes8161ff82018-11-14 13:50:59 +0100418 checksum_algo->calculate_sign(),
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000419 NULL, pkey) <= 0) {
Simon Glass35191a32013-06-13 15:10:02 -0700420 ret = rsa_err("Signer setup failed");
421 goto err_sign;
422 }
423
Simon Glasseffa4522021-09-25 19:43:23 -0600424 if (CONFIG_IS_ENABLED(FIT_RSASSA_PSS) && padding_algo &&
425 !strcmp(padding_algo->name, "pss")) {
Philippe Reynes47d73f02018-11-14 13:51:01 +0100426 if (EVP_PKEY_CTX_set_rsa_padding(ckey,
427 RSA_PKCS1_PSS_PADDING) <= 0) {
428 ret = rsa_err("Signer padding setup failed");
429 goto err_sign;
430 }
Loic Poulainec596ed2024-10-31 10:15:31 +0100431
432 /* Per RFC 3447 (and convention) the Typical salt length is the
433 * length of the output of the digest algorithm.
434 */
435 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(ckey,
436 checksum_algo->checksum_len) <= 0) {
437 ret = rsa_err("Signer salt length setup failed");
438 goto err_sign;
439 }
Philippe Reynes47d73f02018-11-14 13:51:01 +0100440 }
Philippe Reynes47d73f02018-11-14 13:51:01 +0100441
Simon Glass35191a32013-06-13 15:10:02 -0700442 for (i = 0; i < region_count; i++) {
Philippe Reynes8161ff82018-11-14 13:50:59 +0100443 if (!EVP_DigestSignUpdate(context, region[i].data,
444 region[i].size)) {
Simon Glass35191a32013-06-13 15:10:02 -0700445 ret = rsa_err("Signing data failed");
446 goto err_sign;
447 }
448 }
449
Philippe Reynes8161ff82018-11-14 13:50:59 +0100450 if (!EVP_DigestSignFinal(context, sig, &size)) {
Simon Glass35191a32013-06-13 15:10:02 -0700451 ret = rsa_err("Could not obtain signature");
452 goto err_sign;
453 }
Philippe Reynes8161ff82018-11-14 13:50:59 +0100454
Yann Droneaud68ca2802022-03-01 16:12:34 +0100455 EVP_MD_CTX_free(context);
Simon Glass35191a32013-06-13 15:10:02 -0700456
Chan, Donald9ccd6052021-07-19 09:18:54 -0700457 debug("Got signature: %zu bytes, expected %d\n", size, EVP_PKEY_size(pkey));
Simon Glass35191a32013-06-13 15:10:02 -0700458 *sigp = sig;
459 *sig_size = size;
460
461 return 0;
462
463err_sign:
Yann Droneaud68ca2802022-03-01 16:12:34 +0100464 EVP_MD_CTX_free(context);
Simon Glass35191a32013-06-13 15:10:02 -0700465err_create:
466 free(sig);
467err_alloc:
Simon Glass35191a32013-06-13 15:10:02 -0700468 return ret;
469}
470
471int rsa_sign(struct image_sign_info *info,
472 const struct image_region region[], int region_count,
473 uint8_t **sigp, uint *sig_len)
474{
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000475 EVP_PKEY *pkey = NULL;
George McCollister23d14892017-01-06 13:14:17 -0600476 ENGINE *e = NULL;
Simon Glass35191a32013-06-13 15:10:02 -0700477 int ret;
478
479 ret = rsa_init();
480 if (ret)
481 return ret;
482
George McCollister23d14892017-01-06 13:14:17 -0600483 if (info->engine_id) {
484 ret = rsa_engine_init(info->engine_id, &e);
485 if (ret)
Alexandru Gagniucb63dc082021-07-29 13:31:21 -0500486 return ret;
George McCollister23d14892017-01-06 13:14:17 -0600487 }
488
Alexandru Gagniuca35da202021-02-19 12:45:18 -0600489 ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile,
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000490 e, &pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700491 if (ret)
492 goto err_priv;
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000493 ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region,
Heiko Schocheredaf9b12014-03-03 12:19:26 +0100494 region_count, sigp, sig_len);
Simon Glass35191a32013-06-13 15:10:02 -0700495 if (ret)
496 goto err_sign;
497
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000498 EVP_PKEY_free(pkey);
George McCollister23d14892017-01-06 13:14:17 -0600499 if (info->engine_id)
500 rsa_engine_remove(e);
Simon Glass35191a32013-06-13 15:10:02 -0700501
502 return ret;
503
504err_sign:
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000505 EVP_PKEY_free(pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700506err_priv:
George McCollister23d14892017-01-06 13:14:17 -0600507 if (info->engine_id)
508 rsa_engine_remove(e);
Simon Glass35191a32013-06-13 15:10:02 -0700509 return ret;
510}
511
512/*
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200513 * rsa_get_exponent(): - Get the public exponent from an RSA key
514 */
515static int rsa_get_exponent(RSA *key, uint64_t *e)
516{
517 int ret;
518 BIGNUM *bn_te;
Jelle van der Waae9805902017-05-08 21:31:19 +0200519 const BIGNUM *key_e;
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200520 uint64_t te;
521
522 ret = -EINVAL;
523 bn_te = NULL;
524
525 if (!e)
526 goto cleanup;
527
Jelle van der Waae9805902017-05-08 21:31:19 +0200528 RSA_get0_key(key, NULL, &key_e, NULL);
529 if (BN_num_bits(key_e) > 64)
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200530 goto cleanup;
531
Jelle van der Waae9805902017-05-08 21:31:19 +0200532 *e = BN_get_word(key_e);
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200533
Jelle van der Waae9805902017-05-08 21:31:19 +0200534 if (BN_num_bits(key_e) < 33) {
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200535 ret = 0;
536 goto cleanup;
537 }
538
Jelle van der Waae9805902017-05-08 21:31:19 +0200539 bn_te = BN_dup(key_e);
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200540 if (!bn_te)
541 goto cleanup;
542
543 if (!BN_rshift(bn_te, bn_te, 32))
544 goto cleanup;
545
546 if (!BN_mask_bits(bn_te, 32))
547 goto cleanup;
548
549 te = BN_get_word(bn_te);
550 te <<= 32;
551 *e |= te;
552 ret = 0;
553
554cleanup:
555 if (bn_te)
556 BN_free(bn_te);
557
558 return ret;
559}
560
561/*
Simon Glass35191a32013-06-13 15:10:02 -0700562 * rsa_get_params(): - Get the important parameters of an RSA public key
563 */
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200564int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
565 BIGNUM **modulusp, BIGNUM **r_squaredp)
Simon Glass35191a32013-06-13 15:10:02 -0700566{
567 BIGNUM *big1, *big2, *big32, *big2_32;
568 BIGNUM *n, *r, *r_squared, *tmp;
Jelle van der Waae9805902017-05-08 21:31:19 +0200569 const BIGNUM *key_n;
Simon Glass35191a32013-06-13 15:10:02 -0700570 BN_CTX *bn_ctx = BN_CTX_new();
571 int ret = 0;
572
573 /* Initialize BIGNUMs */
574 big1 = BN_new();
575 big2 = BN_new();
576 big32 = BN_new();
577 r = BN_new();
578 r_squared = BN_new();
579 tmp = BN_new();
580 big2_32 = BN_new();
581 n = BN_new();
582 if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
583 !n) {
584 fprintf(stderr, "Out of memory (bignum)\n");
585 return -ENOMEM;
586 }
587
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200588 if (0 != rsa_get_exponent(key, exponent))
589 ret = -1;
590
Jelle van der Waae9805902017-05-08 21:31:19 +0200591 RSA_get0_key(key, &key_n, NULL, NULL);
592 if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
Simon Glass35191a32013-06-13 15:10:02 -0700593 !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
594 ret = -1;
595
596 /* big2_32 = 2^32 */
597 if (!BN_exp(big2_32, big2, big32, bn_ctx))
598 ret = -1;
599
600 /* Calculate n0_inv = -1 / n[0] mod 2^32 */
601 if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
602 !BN_sub(tmp, big2_32, tmp))
603 ret = -1;
604 *n0_invp = BN_get_word(tmp);
605
606 /* Calculate R = 2^(# of key bits) */
607 if (!BN_set_word(tmp, BN_num_bits(n)) ||
608 !BN_exp(r, big2, tmp, bn_ctx))
609 ret = -1;
610
611 /* Calculate r_squared = R^2 mod n */
612 if (!BN_copy(r_squared, r) ||
613 !BN_mul(tmp, r_squared, r, bn_ctx) ||
614 !BN_mod(r_squared, tmp, n, bn_ctx))
615 ret = -1;
616
617 *modulusp = n;
618 *r_squaredp = r_squared;
619
620 BN_free(big1);
621 BN_free(big2);
622 BN_free(big32);
623 BN_free(r);
624 BN_free(tmp);
625 BN_free(big2_32);
626 if (ret) {
627 fprintf(stderr, "Bignum operations failed\n");
628 return -ENOMEM;
629 }
630
631 return ret;
632}
633
Simon Glass35191a32013-06-13 15:10:02 -0700634int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
635{
636 BIGNUM *modulus, *r_squared;
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200637 uint64_t exponent;
Simon Glass35191a32013-06-13 15:10:02 -0700638 uint32_t n0_inv;
Haijun Qin811863c2022-12-06 15:41:37 +0800639 int parent, node = -FDT_ERR_NOTFOUND;
Simon Glass35191a32013-06-13 15:10:02 -0700640 char name[100];
641 int ret;
642 int bits;
643 RSA *rsa;
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000644 EVP_PKEY *pkey = NULL;
George McCollister23d14892017-01-06 13:14:17 -0600645 ENGINE *e = NULL;
Simon Glass35191a32013-06-13 15:10:02 -0700646
647 debug("%s: Getting verification data\n", __func__);
George McCollister23d14892017-01-06 13:14:17 -0600648 if (info->engine_id) {
649 ret = rsa_engine_init(info->engine_id, &e);
650 if (ret)
651 return ret;
652 }
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000653 ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
Simon Glass35191a32013-06-13 15:10:02 -0700654 if (ret)
George McCollister23d14892017-01-06 13:14:17 -0600655 goto err_get_pub_key;
Alexandru Gagniucb63dc082021-07-29 13:31:21 -0500656
Heinrich Schuchardtdccc9da2022-01-09 15:39:40 +0100657 rsa = (RSA *)EVP_PKEY_get0_RSA(pkey);
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200658 ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
Simon Glass35191a32013-06-13 15:10:02 -0700659 if (ret)
George McCollister23d14892017-01-06 13:14:17 -0600660 goto err_get_params;
Simon Glass35191a32013-06-13 15:10:02 -0700661 bits = BN_num_bits(modulus);
662 parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
663 if (parent == -FDT_ERR_NOTFOUND) {
664 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
665 if (parent < 0) {
Simon Glass9423bad2014-06-12 07:24:42 -0600666 ret = parent;
667 if (ret != -FDT_ERR_NOSPACE) {
668 fprintf(stderr, "Couldn't create signature node: %s\n",
669 fdt_strerror(parent));
670 }
Simon Glass35191a32013-06-13 15:10:02 -0700671 }
672 }
Simon Glass9423bad2014-06-12 07:24:42 -0600673 if (ret)
674 goto done;
Simon Glass35191a32013-06-13 15:10:02 -0700675
676 /* Either create or overwrite the named key node */
677 snprintf(name, sizeof(name), "key-%s", info->keyname);
678 node = fdt_subnode_offset(keydest, parent, name);
679 if (node == -FDT_ERR_NOTFOUND) {
680 node = fdt_add_subnode(keydest, parent, name);
681 if (node < 0) {
Simon Glass9423bad2014-06-12 07:24:42 -0600682 ret = node;
683 if (ret != -FDT_ERR_NOSPACE) {
684 fprintf(stderr, "Could not create key subnode: %s\n",
685 fdt_strerror(node));
686 }
Simon Glass35191a32013-06-13 15:10:02 -0700687 }
688 } else if (node < 0) {
689 fprintf(stderr, "Cannot select keys parent: %s\n",
690 fdt_strerror(node));
Simon Glass9423bad2014-06-12 07:24:42 -0600691 ret = node;
Simon Glass35191a32013-06-13 15:10:02 -0700692 }
693
Simon Glass9423bad2014-06-12 07:24:42 -0600694 if (!ret) {
Simon Glassd7aabcc2020-03-18 11:44:06 -0600695 ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT,
696 info->keyname);
Simon Glass9423bad2014-06-12 07:24:42 -0600697 }
Simon Glassaf2f9d52014-06-02 22:04:51 -0600698 if (!ret)
699 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
700 if (!ret)
701 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
702 if (!ret) {
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +0200703 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
704 }
705 if (!ret) {
Simon Glassaf2f9d52014-06-02 22:04:51 -0600706 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
707 bits);
708 }
709 if (!ret) {
710 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
711 bits);
712 }
713 if (!ret) {
714 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
Andrew Duda6616c822016-11-08 18:53:41 +0000715 info->name);
Simon Glassaf2f9d52014-06-02 22:04:51 -0600716 }
mario.six@gdsys.cc99fc6ea2016-07-19 11:07:07 +0200717 if (!ret && info->require_keys) {
Simon Glassd7aabcc2020-03-18 11:44:06 -0600718 ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED,
Simon Glassaf2f9d52014-06-02 22:04:51 -0600719 info->require_keys);
Simon Glass35191a32013-06-13 15:10:02 -0700720 }
Simon Glass9423bad2014-06-12 07:24:42 -0600721done:
Simon Glass35191a32013-06-13 15:10:02 -0700722 BN_free(modulus);
723 BN_free(r_squared);
724 if (ret)
George McCollister23d14892017-01-06 13:14:17 -0600725 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
726err_get_params:
Chan, Donald0bdd4ee2021-04-01 22:42:36 +0000727 EVP_PKEY_free(pkey);
George McCollister23d14892017-01-06 13:14:17 -0600728err_get_pub_key:
729 if (info->engine_id)
730 rsa_engine_remove(e);
Simon Glass35191a32013-06-13 15:10:02 -0700731
Simon Glass94336dc2021-11-12 12:28:11 -0700732 if (ret)
733 return ret;
734
735 return node;
Simon Glass35191a32013-06-13 15:10:02 -0700736}