blob: 2304030e32f491288b6357f768a216344052cb86 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass19c402a2013-06-13 15:10:02 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass19c402a2013-06-13 15:10:02 -07004 */
5
Heinrich Schuchardt3a8b9192021-12-18 11:25:12 +01006#define OPENSSL_API_COMPAT 0x10101000L
7
Simon Glass19c402a2013-06-13 15:10:02 -07008#include "mkimage.h"
Jonathan Gray0ff042d2020-05-15 22:20:34 +10009#include <stdlib.h>
Simon Glass19c402a2013-06-13 15:10:02 -070010#include <stdio.h>
11#include <string.h>
Simon Glass19c402a2013-06-13 15:10:02 -070012#include <image.h>
13#include <time.h>
Alexandru Gagniuc4c17e5f2021-02-19 12:45:11 -060014#include <u-boot/fdt-libcrypto.h>
Jelle van der Waac3b43282017-05-08 21:31:19 +020015#include <openssl/bn.h>
Chan, Donaldfbc77742021-04-01 22:42:36 +000016#include <openssl/ec.h>
Simon Glass19c402a2013-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 McCollisterf1ca1fd2017-01-06 13:14:17 -060022#include <openssl/engine.h>
Simon Glass19c402a2013-06-13 15:10:02 -070023
Simon Glass19c402a2013-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 McCollisterf1ca1fd2017-01-06 13:14:17 -060036 * rsa_pem_get_pub_key() - read a public key from a .crt file
Simon Glass19c402a2013-06-13 15:10:02 -070037 *
38 * @keydir: Directory containins the key
39 * @name Name of key file (will have a .crt extension)
Chan, Donaldfbc77742021-04-01 22:42:36 +000040 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010041 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
Simon Glass19c402a2013-06-13 15:10:02 -070042 */
Chan, Donaldfbc77742021-04-01 22:42:36 +000043static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **evpp)
Simon Glass19c402a2013-06-13 15:10:02 -070044{
45 char path[1024];
Chan, Donaldfbc77742021-04-01 22:42:36 +000046 EVP_PKEY *key = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -070047 X509 *cert;
Simon Glass19c402a2013-06-13 15:10:02 -070048 FILE *f;
49 int ret;
50
Chan, Donaldfbc77742021-04-01 22:42:36 +000051 if (!evpp)
52 return -EINVAL;
53
54 *evpp = NULL;
Simon Glass19c402a2013-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 Glass19c402a2013-06-13 15:10:02 -070079 fclose(f);
Chan, Donaldfbc77742021-04-01 22:42:36 +000080 *evpp = key;
Simon Glass19c402a2013-06-13 15:10:02 -070081 X509_free(cert);
Simon Glass19c402a2013-06-13 15:10:02 -070082
83 return 0;
84
Simon Glass19c402a2013-06-13 15:10:02 -070085err_pubkey:
86 X509_free(cert);
87err_cert:
88 fclose(f);
89 return ret;
90}
91
92/**
George McCollisterf1ca1fd2017-01-06 13:14:17 -060093 * rsa_engine_get_pub_key() - read a public key from given engine
Simon Glass19c402a2013-06-13 15:10:02 -070094 *
George McCollisterf1ca1fd2017-01-06 13:14:17 -060095 * @keydir: Key prefix
96 * @name Name of key
97 * @engine Engine to use
Chan, Donaldfbc77742021-04-01 22:42:36 +000098 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010099 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600100 */
101static int rsa_engine_get_pub_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000102 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600103{
104 const char *engine_id;
105 char key_id[1024];
Chan, Donaldfbc77742021-04-01 22:42:36 +0000106 EVP_PKEY *key = NULL;
Csókás Bencef055d6e2024-01-05 15:08:04 +0100107 const char *const pkcs11_schema = "pkcs11:";
108 const char *pkcs11_uri_prepend = "";
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600109
Chan, Donaldfbc77742021-04-01 22:42:36 +0000110 if (!evpp)
111 return -EINVAL;
112
113 *evpp = NULL;
George McCollisterf1ca1fd2017-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 Bencef055d6e2024-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 Luebbe24bf6e82020-05-13 12:26:24 +0200125 if (strstr(keydir, "object="))
126 snprintf(key_id, sizeof(key_id),
Csókás Bencef055d6e2024-01-05 15:08:04 +0100127 "%s%s;type=public",
128 pkcs11_uri_prepend, keydir);
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200129 else
130 snprintf(key_id, sizeof(key_id),
Csókás Bencef055d6e2024-01-05 15:08:04 +0100131 "%s%s;object=%s;type=public",
132 pkcs11_uri_prepend, keydir, name);
133 } else {
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600134 snprintf(key_id, sizeof(key_id),
Csókás Bence11ad2bb2024-01-05 15:08:03 +0100135 "pkcs11:object=%s;type=public",
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600136 name);
Csókás Bencef055d6e2024-01-05 15:08:04 +0100137 }
Vesa Jääskeläinen5b123e02019-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 McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000156 *evpp = key;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600157
158 return 0;
George McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000167 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100168 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600169 */
170static int rsa_get_pub_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000171 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600172{
173 if (engine)
Chan, Donaldfbc77742021-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 McCollisterf1ca1fd2017-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 Glass19c402a2013-06-13 15:10:02 -0700182 * @name Name of key file (will have a .key extension)
Chan, Donaldfbc77742021-04-01 22:42:36 +0000183 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100184 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
Simon Glass19c402a2013-06-13 15:10:02 -0700185 */
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600186static int rsa_pem_get_priv_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000187 const char *keyfile, EVP_PKEY **evpp)
Simon Glass19c402a2013-06-13 15:10:02 -0700188{
Chan, Donaldfbc77742021-04-01 22:42:36 +0000189 char path[1024] = {0};
190 FILE *f = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700191
Chan, Donaldfbc77742021-04-01 22:42:36 +0000192 if (!evpp)
193 return -EINVAL;
194
195 *evpp = NULL;
Alexandru Gagniuc824ee742021-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 Glass19c402a2013-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, Donaldfbc77742021-04-01 22:42:36 +0000210 if (!PEM_read_PrivateKey(f, evpp, NULL, path)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700211 rsa_err("Failure reading private key");
212 fclose(f);
213 return -EPROTO;
214 }
215 fclose(f);
Simon Glass19c402a2013-06-13 15:10:02 -0700216
217 return 0;
218}
219
George McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000226 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100227 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600228 */
229static int rsa_engine_get_priv_key(const char *keydir, const char *name,
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600230 const char *keyfile,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000231 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600232{
233 const char *engine_id;
234 char key_id[1024];
Chan, Donaldfbc77742021-04-01 22:42:36 +0000235 EVP_PKEY *key = NULL;
Csókás Bencef055d6e2024-01-05 15:08:04 +0100236 const char *const pkcs11_schema = "pkcs11:";
237 const char *pkcs11_uri_prepend = "";
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600238
Chan, Donaldfbc77742021-04-01 22:42:36 +0000239 if (!evpp)
240 return -EINVAL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600241
242 engine_id = ENGINE_get_id(engine);
243
244 if (engine_id && !strcmp(engine_id, "pkcs11")) {
Alexandru Gagniuc824ee742021-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 Bencef055d6e2024-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 Luebbe24bf6e82020-05-13 12:26:24 +0200256 if (strstr(keydir, "object="))
257 snprintf(key_id, sizeof(key_id),
Csókás Bencef055d6e2024-01-05 15:08:04 +0100258 "%s%s;type=private",
259 pkcs11_uri_prepend, keydir);
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200260 else
261 snprintf(key_id, sizeof(key_id),
Csókás Bencef055d6e2024-01-05 15:08:04 +0100262 "%s%s;object=%s;type=private",
263 pkcs11_uri_prepend, keydir, name);
264 } else {
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600265 snprintf(key_id, sizeof(key_id),
Csókás Bence11ad2bb2024-01-05 15:08:03 +0100266 "pkcs11:object=%s;type=private",
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600267 name);
Csókás Bencef055d6e2024-01-05 15:08:04 +0100268 }
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300269 } else if (engine_id) {
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600270 if (keydir && name)
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300271 snprintf(key_id, sizeof(key_id),
272 "%s%s",
273 keydir, name);
Heinrich Schuchardtd607dfd2021-08-28 12:13:05 +0200274 else if (name)
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300275 snprintf(key_id, sizeof(key_id),
276 "%s",
Heinrich Schuchardt295ab732021-07-30 17:05:07 +0200277 name ? name : "");
Alexandru Gagniuc824ee742021-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 McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000292 *evpp = key;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600293
294 return 0;
George McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000303 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100304 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600305 */
306static int rsa_get_priv_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000307 const char *keyfile, ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600308{
309 if (engine)
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600310 return rsa_engine_get_priv_key(keydir, name, keyfile, engine,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000311 evpp);
312 return rsa_pem_get_priv_key(keydir, name, keyfile, evpp);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600313}
314
Simon Glass19c402a2013-06-13 15:10:02 -0700315static int rsa_init(void)
316{
317 int ret;
318
Jelle van der Waac3b43282017-05-08 21:31:19 +0200319 ret = OPENSSL_init_ssl(0, NULL);
Simon Glass19c402a2013-06-13 15:10:02 -0700320 if (!ret) {
321 fprintf(stderr, "Failure to init SSL library\n");
322 return -1;
323 }
Simon Glass19c402a2013-06-13 15:10:02 -0700324
325 return 0;
326}
327
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600328static int rsa_engine_init(const char *engine_id, ENGINE **pe)
329{
Marc Kleine-Budde62b27a52021-07-23 22:17:50 +0200330 const char *key_pass;
George McCollisterf1ca1fd2017-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 Bencefa783012023-12-14 17:54:17 +0100338 fprintf(stderr, "Engine '%s' isn't available\n", engine_id);
339 ERR_print_errors_fp(stderr);
Alexandru Gagniucfe68a672021-07-29 13:31:21 -0500340 return -1;
George McCollisterf1ca1fd2017-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-Budde62b27a52021-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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600364 *pe = e;
365
366 return 0;
367
Marc Kleine-Budde62b27a52021-07-23 22:17:50 +0200368err_set_pin:
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600369err_set_rsa:
370 ENGINE_finish(e);
371err_engine_init:
372 ENGINE_free(e);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600373 return ret;
374}
375
George McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000384static int rsa_sign_with_key(EVP_PKEY *pkey, struct padding_algo *padding_algo,
Philippe Reynes20031562018-11-14 13:51:00 +0100385 struct checksum_algo *checksum_algo,
Heiko Schocher646257d2014-03-03 12:19:26 +0100386 const struct image_region region[], int region_count,
387 uint8_t **sigp, uint *sig_size)
Simon Glass19c402a2013-06-13 15:10:02 -0700388{
Philippe Reynes20031562018-11-14 13:51:00 +0100389 EVP_PKEY_CTX *ckey;
Simon Glass19c402a2013-06-13 15:10:02 -0700390 EVP_MD_CTX *context;
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100391 int ret = 0;
392 size_t size;
Simon Glass19c402a2013-06-13 15:10:02 -0700393 uint8_t *sig;
394 int i;
395
Chan, Donaldfbc77742021-04-01 22:42:36 +0000396 size = EVP_PKEY_size(pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700397 sig = malloc(size);
398 if (!sig) {
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100399 fprintf(stderr, "Out of memory for signature (%zu bytes)\n",
Simon Glass19c402a2013-06-13 15:10:02 -0700400 size);
401 ret = -ENOMEM;
402 goto err_alloc;
403 }
404
Yann Droneaud9b5ad4f2022-03-01 16:12:34 +0100405 context = EVP_MD_CTX_new();
Simon Glass19c402a2013-06-13 15:10:02 -0700406 if (!context) {
407 ret = rsa_err("EVP context creation failed");
408 goto err_create;
409 }
Philippe Reynes20031562018-11-14 13:51:00 +0100410
Chan, Donaldfbc77742021-04-01 22:42:36 +0000411 ckey = EVP_PKEY_CTX_new(pkey, NULL);
Philippe Reynes20031562018-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 Reynes3b5d6972018-11-14 13:50:59 +0100418 checksum_algo->calculate_sign(),
Chan, Donaldfbc77742021-04-01 22:42:36 +0000419 NULL, pkey) <= 0) {
Simon Glass19c402a2013-06-13 15:10:02 -0700420 ret = rsa_err("Signer setup failed");
421 goto err_sign;
422 }
423
Simon Glass2bbed3f2021-09-25 19:43:23 -0600424 if (CONFIG_IS_ENABLED(FIT_RSASSA_PSS) && padding_algo &&
425 !strcmp(padding_algo->name, "pss")) {
Philippe Reynes061daa02018-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 }
431 }
Philippe Reynes061daa02018-11-14 13:51:01 +0100432
Simon Glass19c402a2013-06-13 15:10:02 -0700433 for (i = 0; i < region_count; i++) {
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100434 if (!EVP_DigestSignUpdate(context, region[i].data,
435 region[i].size)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700436 ret = rsa_err("Signing data failed");
437 goto err_sign;
438 }
439 }
440
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100441 if (!EVP_DigestSignFinal(context, sig, &size)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700442 ret = rsa_err("Could not obtain signature");
443 goto err_sign;
444 }
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100445
Yann Droneaud9b5ad4f2022-03-01 16:12:34 +0100446 EVP_MD_CTX_free(context);
Simon Glass19c402a2013-06-13 15:10:02 -0700447
Chan, Donald6d59ace2021-07-19 09:18:54 -0700448 debug("Got signature: %zu bytes, expected %d\n", size, EVP_PKEY_size(pkey));
Simon Glass19c402a2013-06-13 15:10:02 -0700449 *sigp = sig;
450 *sig_size = size;
451
452 return 0;
453
454err_sign:
Yann Droneaud9b5ad4f2022-03-01 16:12:34 +0100455 EVP_MD_CTX_free(context);
Simon Glass19c402a2013-06-13 15:10:02 -0700456err_create:
457 free(sig);
458err_alloc:
Simon Glass19c402a2013-06-13 15:10:02 -0700459 return ret;
460}
461
462int rsa_sign(struct image_sign_info *info,
463 const struct image_region region[], int region_count,
464 uint8_t **sigp, uint *sig_len)
465{
Chan, Donaldfbc77742021-04-01 22:42:36 +0000466 EVP_PKEY *pkey = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600467 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700468 int ret;
469
470 ret = rsa_init();
471 if (ret)
472 return ret;
473
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600474 if (info->engine_id) {
475 ret = rsa_engine_init(info->engine_id, &e);
476 if (ret)
Alexandru Gagniucfe68a672021-07-29 13:31:21 -0500477 return ret;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600478 }
479
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600480 ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000481 e, &pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700482 if (ret)
483 goto err_priv;
Chan, Donaldfbc77742021-04-01 22:42:36 +0000484 ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region,
Heiko Schocher646257d2014-03-03 12:19:26 +0100485 region_count, sigp, sig_len);
Simon Glass19c402a2013-06-13 15:10:02 -0700486 if (ret)
487 goto err_sign;
488
Chan, Donaldfbc77742021-04-01 22:42:36 +0000489 EVP_PKEY_free(pkey);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600490 if (info->engine_id)
491 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700492
493 return ret;
494
495err_sign:
Chan, Donaldfbc77742021-04-01 22:42:36 +0000496 EVP_PKEY_free(pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700497err_priv:
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600498 if (info->engine_id)
499 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700500 return ret;
501}
502
503/*
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200504 * rsa_get_exponent(): - Get the public exponent from an RSA key
505 */
506static int rsa_get_exponent(RSA *key, uint64_t *e)
507{
508 int ret;
509 BIGNUM *bn_te;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200510 const BIGNUM *key_e;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200511 uint64_t te;
512
513 ret = -EINVAL;
514 bn_te = NULL;
515
516 if (!e)
517 goto cleanup;
518
Jelle van der Waac3b43282017-05-08 21:31:19 +0200519 RSA_get0_key(key, NULL, &key_e, NULL);
520 if (BN_num_bits(key_e) > 64)
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200521 goto cleanup;
522
Jelle van der Waac3b43282017-05-08 21:31:19 +0200523 *e = BN_get_word(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200524
Jelle van der Waac3b43282017-05-08 21:31:19 +0200525 if (BN_num_bits(key_e) < 33) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200526 ret = 0;
527 goto cleanup;
528 }
529
Jelle van der Waac3b43282017-05-08 21:31:19 +0200530 bn_te = BN_dup(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200531 if (!bn_te)
532 goto cleanup;
533
534 if (!BN_rshift(bn_te, bn_te, 32))
535 goto cleanup;
536
537 if (!BN_mask_bits(bn_te, 32))
538 goto cleanup;
539
540 te = BN_get_word(bn_te);
541 te <<= 32;
542 *e |= te;
543 ret = 0;
544
545cleanup:
546 if (bn_te)
547 BN_free(bn_te);
548
549 return ret;
550}
551
552/*
Simon Glass19c402a2013-06-13 15:10:02 -0700553 * rsa_get_params(): - Get the important parameters of an RSA public key
554 */
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200555int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
556 BIGNUM **modulusp, BIGNUM **r_squaredp)
Simon Glass19c402a2013-06-13 15:10:02 -0700557{
558 BIGNUM *big1, *big2, *big32, *big2_32;
559 BIGNUM *n, *r, *r_squared, *tmp;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200560 const BIGNUM *key_n;
Simon Glass19c402a2013-06-13 15:10:02 -0700561 BN_CTX *bn_ctx = BN_CTX_new();
562 int ret = 0;
563
564 /* Initialize BIGNUMs */
565 big1 = BN_new();
566 big2 = BN_new();
567 big32 = BN_new();
568 r = BN_new();
569 r_squared = BN_new();
570 tmp = BN_new();
571 big2_32 = BN_new();
572 n = BN_new();
573 if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
574 !n) {
575 fprintf(stderr, "Out of memory (bignum)\n");
576 return -ENOMEM;
577 }
578
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200579 if (0 != rsa_get_exponent(key, exponent))
580 ret = -1;
581
Jelle van der Waac3b43282017-05-08 21:31:19 +0200582 RSA_get0_key(key, &key_n, NULL, NULL);
583 if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
Simon Glass19c402a2013-06-13 15:10:02 -0700584 !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
585 ret = -1;
586
587 /* big2_32 = 2^32 */
588 if (!BN_exp(big2_32, big2, big32, bn_ctx))
589 ret = -1;
590
591 /* Calculate n0_inv = -1 / n[0] mod 2^32 */
592 if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
593 !BN_sub(tmp, big2_32, tmp))
594 ret = -1;
595 *n0_invp = BN_get_word(tmp);
596
597 /* Calculate R = 2^(# of key bits) */
598 if (!BN_set_word(tmp, BN_num_bits(n)) ||
599 !BN_exp(r, big2, tmp, bn_ctx))
600 ret = -1;
601
602 /* Calculate r_squared = R^2 mod n */
603 if (!BN_copy(r_squared, r) ||
604 !BN_mul(tmp, r_squared, r, bn_ctx) ||
605 !BN_mod(r_squared, tmp, n, bn_ctx))
606 ret = -1;
607
608 *modulusp = n;
609 *r_squaredp = r_squared;
610
611 BN_free(big1);
612 BN_free(big2);
613 BN_free(big32);
614 BN_free(r);
615 BN_free(tmp);
616 BN_free(big2_32);
617 if (ret) {
618 fprintf(stderr, "Bignum operations failed\n");
619 return -ENOMEM;
620 }
621
622 return ret;
623}
624
Simon Glass19c402a2013-06-13 15:10:02 -0700625int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
626{
627 BIGNUM *modulus, *r_squared;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200628 uint64_t exponent;
Simon Glass19c402a2013-06-13 15:10:02 -0700629 uint32_t n0_inv;
Haijun Qindd02c662022-12-06 15:41:37 +0800630 int parent, node = -FDT_ERR_NOTFOUND;
Simon Glass19c402a2013-06-13 15:10:02 -0700631 char name[100];
632 int ret;
633 int bits;
634 RSA *rsa;
Chan, Donaldfbc77742021-04-01 22:42:36 +0000635 EVP_PKEY *pkey = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600636 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700637
638 debug("%s: Getting verification data\n", __func__);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600639 if (info->engine_id) {
640 ret = rsa_engine_init(info->engine_id, &e);
641 if (ret)
642 return ret;
643 }
Chan, Donaldfbc77742021-04-01 22:42:36 +0000644 ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700645 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600646 goto err_get_pub_key;
Alexandru Gagniucfe68a672021-07-29 13:31:21 -0500647
Heinrich Schuchardt675c3cc2022-01-09 15:39:40 +0100648 rsa = (RSA *)EVP_PKEY_get0_RSA(pkey);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200649 ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
Simon Glass19c402a2013-06-13 15:10:02 -0700650 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600651 goto err_get_params;
Simon Glass19c402a2013-06-13 15:10:02 -0700652 bits = BN_num_bits(modulus);
653 parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
654 if (parent == -FDT_ERR_NOTFOUND) {
655 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
656 if (parent < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600657 ret = parent;
658 if (ret != -FDT_ERR_NOSPACE) {
659 fprintf(stderr, "Couldn't create signature node: %s\n",
660 fdt_strerror(parent));
661 }
Simon Glass19c402a2013-06-13 15:10:02 -0700662 }
663 }
Simon Glass597a8b22014-06-12 07:24:42 -0600664 if (ret)
665 goto done;
Simon Glass19c402a2013-06-13 15:10:02 -0700666
667 /* Either create or overwrite the named key node */
668 snprintf(name, sizeof(name), "key-%s", info->keyname);
669 node = fdt_subnode_offset(keydest, parent, name);
670 if (node == -FDT_ERR_NOTFOUND) {
671 node = fdt_add_subnode(keydest, parent, name);
672 if (node < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600673 ret = node;
674 if (ret != -FDT_ERR_NOSPACE) {
675 fprintf(stderr, "Could not create key subnode: %s\n",
676 fdt_strerror(node));
677 }
Simon Glass19c402a2013-06-13 15:10:02 -0700678 }
679 } else if (node < 0) {
680 fprintf(stderr, "Cannot select keys parent: %s\n",
681 fdt_strerror(node));
Simon Glass597a8b22014-06-12 07:24:42 -0600682 ret = node;
Simon Glass19c402a2013-06-13 15:10:02 -0700683 }
684
Simon Glass597a8b22014-06-12 07:24:42 -0600685 if (!ret) {
Simon Glass72188f52020-03-18 11:44:06 -0600686 ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT,
687 info->keyname);
Simon Glass597a8b22014-06-12 07:24:42 -0600688 }
Simon Glass4f427a42014-06-02 22:04:51 -0600689 if (!ret)
690 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
691 if (!ret)
692 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
693 if (!ret) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200694 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
695 }
696 if (!ret) {
Simon Glass4f427a42014-06-02 22:04:51 -0600697 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
698 bits);
699 }
700 if (!ret) {
701 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
702 bits);
703 }
704 if (!ret) {
705 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
Andrew Duda83dd98e2016-11-08 18:53:41 +0000706 info->name);
Simon Glass4f427a42014-06-02 22:04:51 -0600707 }
mario.six@gdsys.cc2b9ec762016-07-19 11:07:07 +0200708 if (!ret && info->require_keys) {
Simon Glass72188f52020-03-18 11:44:06 -0600709 ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED,
Simon Glass4f427a42014-06-02 22:04:51 -0600710 info->require_keys);
Simon Glass19c402a2013-06-13 15:10:02 -0700711 }
Simon Glass597a8b22014-06-12 07:24:42 -0600712done:
Simon Glass19c402a2013-06-13 15:10:02 -0700713 BN_free(modulus);
714 BN_free(r_squared);
715 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600716 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
717err_get_params:
Chan, Donaldfbc77742021-04-01 22:42:36 +0000718 EVP_PKEY_free(pkey);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600719err_get_pub_key:
720 if (info->engine_id)
721 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700722
Simon Glassc033dc82021-11-12 12:28:11 -0700723 if (ret)
724 return ret;
725
726 return node;
Simon Glass19c402a2013-06-13 15:10:02 -0700727}