blob: 858ad92a6f6f51f88b64c5166178fc439e1f9aad [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;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600107
Chan, Donaldfbc77742021-04-01 22:42:36 +0000108 if (!evpp)
109 return -EINVAL;
110
111 *evpp = NULL;
George McCollisterf1ca1fd2017-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 Luebbe24bf6e82020-05-13 12:26:24 +0200117 if (strstr(keydir, "object="))
118 snprintf(key_id, sizeof(key_id),
Ayoub Zakiece85cc2023-08-26 13:53:29 +0200119 "%s;type=public",
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200120 keydir);
121 else
122 snprintf(key_id, sizeof(key_id),
Ayoub Zakiece85cc2023-08-26 13:53:29 +0200123 "%s;object=%s;type=public",
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200124 keydir, name);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600125 else
126 snprintf(key_id, sizeof(key_id),
Ayoub Zakiece85cc2023-08-26 13:53:29 +0200127 "object=%s;type=public",
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600128 name);
Vesa Jääskeläinen5b123e02019-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 McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000147 *evpp = key;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600148
149 return 0;
George McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000158 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100159 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600160 */
161static int rsa_get_pub_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000162 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600163{
164 if (engine)
Chan, Donaldfbc77742021-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 McCollisterf1ca1fd2017-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 Glass19c402a2013-06-13 15:10:02 -0700173 * @name Name of key file (will have a .key extension)
Chan, Donaldfbc77742021-04-01 22:42:36 +0000174 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100175 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
Simon Glass19c402a2013-06-13 15:10:02 -0700176 */
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600177static int rsa_pem_get_priv_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000178 const char *keyfile, EVP_PKEY **evpp)
Simon Glass19c402a2013-06-13 15:10:02 -0700179{
Chan, Donaldfbc77742021-04-01 22:42:36 +0000180 char path[1024] = {0};
181 FILE *f = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700182
Chan, Donaldfbc77742021-04-01 22:42:36 +0000183 if (!evpp)
184 return -EINVAL;
185
186 *evpp = NULL;
Alexandru Gagniuc824ee742021-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 Glass19c402a2013-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, Donaldfbc77742021-04-01 22:42:36 +0000201 if (!PEM_read_PrivateKey(f, evpp, NULL, path)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700202 rsa_err("Failure reading private key");
203 fclose(f);
204 return -EPROTO;
205 }
206 fclose(f);
Simon Glass19c402a2013-06-13 15:10:02 -0700207
208 return 0;
209}
210
George McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000217 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100218 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600219 */
220static int rsa_engine_get_priv_key(const char *keydir, const char *name,
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600221 const char *keyfile,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000222 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600223{
224 const char *engine_id;
225 char key_id[1024];
Chan, Donaldfbc77742021-04-01 22:42:36 +0000226 EVP_PKEY *key = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600227
Chan, Donaldfbc77742021-04-01 22:42:36 +0000228 if (!evpp)
229 return -EINVAL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600230
231 engine_id = ENGINE_get_id(engine);
232
233 if (engine_id && !strcmp(engine_id, "pkcs11")) {
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600234 if (!keydir && !name) {
235 fprintf(stderr, "Please use 'keydir' with PKCS11\n");
236 return -EINVAL;
237 }
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600238 if (keydir)
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200239 if (strstr(keydir, "object="))
240 snprintf(key_id, sizeof(key_id),
Ayoub Zakiece85cc2023-08-26 13:53:29 +0200241 "%s;type=private",
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200242 keydir);
243 else
244 snprintf(key_id, sizeof(key_id),
Ayoub Zakiece85cc2023-08-26 13:53:29 +0200245 "%s;object=%s;type=private",
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200246 keydir, name);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600247 else
248 snprintf(key_id, sizeof(key_id),
Ayoub Zakiece85cc2023-08-26 13:53:29 +0200249 "object=%s;type=private",
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600250 name);
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300251 } else if (engine_id) {
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600252 if (keydir && name)
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300253 snprintf(key_id, sizeof(key_id),
254 "%s%s",
255 keydir, name);
Heinrich Schuchardtd607dfd2021-08-28 12:13:05 +0200256 else if (name)
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300257 snprintf(key_id, sizeof(key_id),
258 "%s",
Heinrich Schuchardt295ab732021-07-30 17:05:07 +0200259 name ? name : "");
Alexandru Gagniuc824ee742021-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 McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000274 *evpp = key;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600275
276 return 0;
George McCollisterf1ca1fd2017-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, Donaldfbc77742021-04-01 22:42:36 +0000285 * @evpp Returns EVP_PKEY object, or NULL on failure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100286 * Return: 0 if ok, -ve on error (in which case *evpp will be set to NULL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600287 */
288static int rsa_get_priv_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000289 const char *keyfile, ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600290{
291 if (engine)
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600292 return rsa_engine_get_priv_key(keydir, name, keyfile, engine,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000293 evpp);
294 return rsa_pem_get_priv_key(keydir, name, keyfile, evpp);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600295}
296
Simon Glass19c402a2013-06-13 15:10:02 -0700297static int rsa_init(void)
298{
299 int ret;
300
Jelle van der Waac3b43282017-05-08 21:31:19 +0200301 ret = OPENSSL_init_ssl(0, NULL);
Simon Glass19c402a2013-06-13 15:10:02 -0700302 if (!ret) {
303 fprintf(stderr, "Failure to init SSL library\n");
304 return -1;
305 }
Simon Glass19c402a2013-06-13 15:10:02 -0700306
307 return 0;
308}
309
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600310static int rsa_engine_init(const char *engine_id, ENGINE **pe)
311{
Marc Kleine-Budde62b27a52021-07-23 22:17:50 +0200312 const char *key_pass;
George McCollisterf1ca1fd2017-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) {
Csókás Bencefa783012023-12-14 17:54:17 +0100320 fprintf(stderr, "Engine '%s' isn't available\n", engine_id);
321 ERR_print_errors_fp(stderr);
Alexandru Gagniucfe68a672021-07-29 13:31:21 -0500322 return -1;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600323 }
324
325 if (!ENGINE_init(e)) {
326 fprintf(stderr, "Couldn't initialize engine\n");
327 ret = -1;
328 goto err_engine_init;
329 }
330
331 if (!ENGINE_set_default_RSA(e)) {
332 fprintf(stderr, "Couldn't set engine as default for RSA\n");
333 ret = -1;
334 goto err_set_rsa;
335 }
336
Marc Kleine-Budde62b27a52021-07-23 22:17:50 +0200337 key_pass = getenv("MKIMAGE_SIGN_PIN");
338 if (key_pass) {
339 if (!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
340 fprintf(stderr, "Couldn't set PIN\n");
341 ret = -1;
342 goto err_set_pin;
343 }
344 }
345
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600346 *pe = e;
347
348 return 0;
349
Marc Kleine-Budde62b27a52021-07-23 22:17:50 +0200350err_set_pin:
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600351err_set_rsa:
352 ENGINE_finish(e);
353err_engine_init:
354 ENGINE_free(e);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600355 return ret;
356}
357
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600358static void rsa_engine_remove(ENGINE *e)
359{
360 if (e) {
361 ENGINE_finish(e);
362 ENGINE_free(e);
363 }
364}
365
Chan, Donaldfbc77742021-04-01 22:42:36 +0000366static int rsa_sign_with_key(EVP_PKEY *pkey, struct padding_algo *padding_algo,
Philippe Reynes20031562018-11-14 13:51:00 +0100367 struct checksum_algo *checksum_algo,
Heiko Schocher646257d2014-03-03 12:19:26 +0100368 const struct image_region region[], int region_count,
369 uint8_t **sigp, uint *sig_size)
Simon Glass19c402a2013-06-13 15:10:02 -0700370{
Philippe Reynes20031562018-11-14 13:51:00 +0100371 EVP_PKEY_CTX *ckey;
Simon Glass19c402a2013-06-13 15:10:02 -0700372 EVP_MD_CTX *context;
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100373 int ret = 0;
374 size_t size;
Simon Glass19c402a2013-06-13 15:10:02 -0700375 uint8_t *sig;
376 int i;
377
Chan, Donaldfbc77742021-04-01 22:42:36 +0000378 size = EVP_PKEY_size(pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700379 sig = malloc(size);
380 if (!sig) {
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100381 fprintf(stderr, "Out of memory for signature (%zu bytes)\n",
Simon Glass19c402a2013-06-13 15:10:02 -0700382 size);
383 ret = -ENOMEM;
384 goto err_alloc;
385 }
386
Yann Droneaud9b5ad4f2022-03-01 16:12:34 +0100387 context = EVP_MD_CTX_new();
Simon Glass19c402a2013-06-13 15:10:02 -0700388 if (!context) {
389 ret = rsa_err("EVP context creation failed");
390 goto err_create;
391 }
Philippe Reynes20031562018-11-14 13:51:00 +0100392
Chan, Donaldfbc77742021-04-01 22:42:36 +0000393 ckey = EVP_PKEY_CTX_new(pkey, NULL);
Philippe Reynes20031562018-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 Reynes3b5d6972018-11-14 13:50:59 +0100400 checksum_algo->calculate_sign(),
Chan, Donaldfbc77742021-04-01 22:42:36 +0000401 NULL, pkey) <= 0) {
Simon Glass19c402a2013-06-13 15:10:02 -0700402 ret = rsa_err("Signer setup failed");
403 goto err_sign;
404 }
405
Simon Glass2bbed3f2021-09-25 19:43:23 -0600406 if (CONFIG_IS_ENABLED(FIT_RSASSA_PSS) && padding_algo &&
407 !strcmp(padding_algo->name, "pss")) {
Philippe Reynes061daa02018-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 Reynes061daa02018-11-14 13:51:01 +0100414
Simon Glass19c402a2013-06-13 15:10:02 -0700415 for (i = 0; i < region_count; i++) {
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100416 if (!EVP_DigestSignUpdate(context, region[i].data,
417 region[i].size)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700418 ret = rsa_err("Signing data failed");
419 goto err_sign;
420 }
421 }
422
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100423 if (!EVP_DigestSignFinal(context, sig, &size)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700424 ret = rsa_err("Could not obtain signature");
425 goto err_sign;
426 }
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100427
Yann Droneaud9b5ad4f2022-03-01 16:12:34 +0100428 EVP_MD_CTX_free(context);
Simon Glass19c402a2013-06-13 15:10:02 -0700429
Chan, Donald6d59ace2021-07-19 09:18:54 -0700430 debug("Got signature: %zu bytes, expected %d\n", size, EVP_PKEY_size(pkey));
Simon Glass19c402a2013-06-13 15:10:02 -0700431 *sigp = sig;
432 *sig_size = size;
433
434 return 0;
435
436err_sign:
Yann Droneaud9b5ad4f2022-03-01 16:12:34 +0100437 EVP_MD_CTX_free(context);
Simon Glass19c402a2013-06-13 15:10:02 -0700438err_create:
439 free(sig);
440err_alloc:
Simon Glass19c402a2013-06-13 15:10:02 -0700441 return ret;
442}
443
444int rsa_sign(struct image_sign_info *info,
445 const struct image_region region[], int region_count,
446 uint8_t **sigp, uint *sig_len)
447{
Chan, Donaldfbc77742021-04-01 22:42:36 +0000448 EVP_PKEY *pkey = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600449 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700450 int ret;
451
452 ret = rsa_init();
453 if (ret)
454 return ret;
455
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600456 if (info->engine_id) {
457 ret = rsa_engine_init(info->engine_id, &e);
458 if (ret)
Alexandru Gagniucfe68a672021-07-29 13:31:21 -0500459 return ret;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600460 }
461
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600462 ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000463 e, &pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700464 if (ret)
465 goto err_priv;
Chan, Donaldfbc77742021-04-01 22:42:36 +0000466 ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region,
Heiko Schocher646257d2014-03-03 12:19:26 +0100467 region_count, sigp, sig_len);
Simon Glass19c402a2013-06-13 15:10:02 -0700468 if (ret)
469 goto err_sign;
470
Chan, Donaldfbc77742021-04-01 22:42:36 +0000471 EVP_PKEY_free(pkey);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600472 if (info->engine_id)
473 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700474
475 return ret;
476
477err_sign:
Chan, Donaldfbc77742021-04-01 22:42:36 +0000478 EVP_PKEY_free(pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700479err_priv:
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600480 if (info->engine_id)
481 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700482 return ret;
483}
484
485/*
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200486 * rsa_get_exponent(): - Get the public exponent from an RSA key
487 */
488static int rsa_get_exponent(RSA *key, uint64_t *e)
489{
490 int ret;
491 BIGNUM *bn_te;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200492 const BIGNUM *key_e;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200493 uint64_t te;
494
495 ret = -EINVAL;
496 bn_te = NULL;
497
498 if (!e)
499 goto cleanup;
500
Jelle van der Waac3b43282017-05-08 21:31:19 +0200501 RSA_get0_key(key, NULL, &key_e, NULL);
502 if (BN_num_bits(key_e) > 64)
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200503 goto cleanup;
504
Jelle van der Waac3b43282017-05-08 21:31:19 +0200505 *e = BN_get_word(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200506
Jelle van der Waac3b43282017-05-08 21:31:19 +0200507 if (BN_num_bits(key_e) < 33) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200508 ret = 0;
509 goto cleanup;
510 }
511
Jelle van der Waac3b43282017-05-08 21:31:19 +0200512 bn_te = BN_dup(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200513 if (!bn_te)
514 goto cleanup;
515
516 if (!BN_rshift(bn_te, bn_te, 32))
517 goto cleanup;
518
519 if (!BN_mask_bits(bn_te, 32))
520 goto cleanup;
521
522 te = BN_get_word(bn_te);
523 te <<= 32;
524 *e |= te;
525 ret = 0;
526
527cleanup:
528 if (bn_te)
529 BN_free(bn_te);
530
531 return ret;
532}
533
534/*
Simon Glass19c402a2013-06-13 15:10:02 -0700535 * rsa_get_params(): - Get the important parameters of an RSA public key
536 */
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200537int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
538 BIGNUM **modulusp, BIGNUM **r_squaredp)
Simon Glass19c402a2013-06-13 15:10:02 -0700539{
540 BIGNUM *big1, *big2, *big32, *big2_32;
541 BIGNUM *n, *r, *r_squared, *tmp;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200542 const BIGNUM *key_n;
Simon Glass19c402a2013-06-13 15:10:02 -0700543 BN_CTX *bn_ctx = BN_CTX_new();
544 int ret = 0;
545
546 /* Initialize BIGNUMs */
547 big1 = BN_new();
548 big2 = BN_new();
549 big32 = BN_new();
550 r = BN_new();
551 r_squared = BN_new();
552 tmp = BN_new();
553 big2_32 = BN_new();
554 n = BN_new();
555 if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
556 !n) {
557 fprintf(stderr, "Out of memory (bignum)\n");
558 return -ENOMEM;
559 }
560
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200561 if (0 != rsa_get_exponent(key, exponent))
562 ret = -1;
563
Jelle van der Waac3b43282017-05-08 21:31:19 +0200564 RSA_get0_key(key, &key_n, NULL, NULL);
565 if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
Simon Glass19c402a2013-06-13 15:10:02 -0700566 !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
567 ret = -1;
568
569 /* big2_32 = 2^32 */
570 if (!BN_exp(big2_32, big2, big32, bn_ctx))
571 ret = -1;
572
573 /* Calculate n0_inv = -1 / n[0] mod 2^32 */
574 if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
575 !BN_sub(tmp, big2_32, tmp))
576 ret = -1;
577 *n0_invp = BN_get_word(tmp);
578
579 /* Calculate R = 2^(# of key bits) */
580 if (!BN_set_word(tmp, BN_num_bits(n)) ||
581 !BN_exp(r, big2, tmp, bn_ctx))
582 ret = -1;
583
584 /* Calculate r_squared = R^2 mod n */
585 if (!BN_copy(r_squared, r) ||
586 !BN_mul(tmp, r_squared, r, bn_ctx) ||
587 !BN_mod(r_squared, tmp, n, bn_ctx))
588 ret = -1;
589
590 *modulusp = n;
591 *r_squaredp = r_squared;
592
593 BN_free(big1);
594 BN_free(big2);
595 BN_free(big32);
596 BN_free(r);
597 BN_free(tmp);
598 BN_free(big2_32);
599 if (ret) {
600 fprintf(stderr, "Bignum operations failed\n");
601 return -ENOMEM;
602 }
603
604 return ret;
605}
606
Simon Glass19c402a2013-06-13 15:10:02 -0700607int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
608{
609 BIGNUM *modulus, *r_squared;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200610 uint64_t exponent;
Simon Glass19c402a2013-06-13 15:10:02 -0700611 uint32_t n0_inv;
Haijun Qindd02c662022-12-06 15:41:37 +0800612 int parent, node = -FDT_ERR_NOTFOUND;
Simon Glass19c402a2013-06-13 15:10:02 -0700613 char name[100];
614 int ret;
615 int bits;
616 RSA *rsa;
Chan, Donaldfbc77742021-04-01 22:42:36 +0000617 EVP_PKEY *pkey = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600618 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700619
620 debug("%s: Getting verification data\n", __func__);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600621 if (info->engine_id) {
622 ret = rsa_engine_init(info->engine_id, &e);
623 if (ret)
624 return ret;
625 }
Chan, Donaldfbc77742021-04-01 22:42:36 +0000626 ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700627 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600628 goto err_get_pub_key;
Alexandru Gagniucfe68a672021-07-29 13:31:21 -0500629
Heinrich Schuchardt675c3cc2022-01-09 15:39:40 +0100630 rsa = (RSA *)EVP_PKEY_get0_RSA(pkey);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200631 ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
Simon Glass19c402a2013-06-13 15:10:02 -0700632 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600633 goto err_get_params;
Simon Glass19c402a2013-06-13 15:10:02 -0700634 bits = BN_num_bits(modulus);
635 parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
636 if (parent == -FDT_ERR_NOTFOUND) {
637 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
638 if (parent < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600639 ret = parent;
640 if (ret != -FDT_ERR_NOSPACE) {
641 fprintf(stderr, "Couldn't create signature node: %s\n",
642 fdt_strerror(parent));
643 }
Simon Glass19c402a2013-06-13 15:10:02 -0700644 }
645 }
Simon Glass597a8b22014-06-12 07:24:42 -0600646 if (ret)
647 goto done;
Simon Glass19c402a2013-06-13 15:10:02 -0700648
649 /* Either create or overwrite the named key node */
650 snprintf(name, sizeof(name), "key-%s", info->keyname);
651 node = fdt_subnode_offset(keydest, parent, name);
652 if (node == -FDT_ERR_NOTFOUND) {
653 node = fdt_add_subnode(keydest, parent, name);
654 if (node < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600655 ret = node;
656 if (ret != -FDT_ERR_NOSPACE) {
657 fprintf(stderr, "Could not create key subnode: %s\n",
658 fdt_strerror(node));
659 }
Simon Glass19c402a2013-06-13 15:10:02 -0700660 }
661 } else if (node < 0) {
662 fprintf(stderr, "Cannot select keys parent: %s\n",
663 fdt_strerror(node));
Simon Glass597a8b22014-06-12 07:24:42 -0600664 ret = node;
Simon Glass19c402a2013-06-13 15:10:02 -0700665 }
666
Simon Glass597a8b22014-06-12 07:24:42 -0600667 if (!ret) {
Simon Glass72188f52020-03-18 11:44:06 -0600668 ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT,
669 info->keyname);
Simon Glass597a8b22014-06-12 07:24:42 -0600670 }
Simon Glass4f427a42014-06-02 22:04:51 -0600671 if (!ret)
672 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
673 if (!ret)
674 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
675 if (!ret) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200676 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
677 }
678 if (!ret) {
Simon Glass4f427a42014-06-02 22:04:51 -0600679 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
680 bits);
681 }
682 if (!ret) {
683 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
684 bits);
685 }
686 if (!ret) {
687 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
Andrew Duda83dd98e2016-11-08 18:53:41 +0000688 info->name);
Simon Glass4f427a42014-06-02 22:04:51 -0600689 }
mario.six@gdsys.cc2b9ec762016-07-19 11:07:07 +0200690 if (!ret && info->require_keys) {
Simon Glass72188f52020-03-18 11:44:06 -0600691 ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED,
Simon Glass4f427a42014-06-02 22:04:51 -0600692 info->require_keys);
Simon Glass19c402a2013-06-13 15:10:02 -0700693 }
Simon Glass597a8b22014-06-12 07:24:42 -0600694done:
Simon Glass19c402a2013-06-13 15:10:02 -0700695 BN_free(modulus);
696 BN_free(r_squared);
697 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600698 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
699err_get_params:
Chan, Donaldfbc77742021-04-01 22:42:36 +0000700 EVP_PKEY_free(pkey);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600701err_get_pub_key:
702 if (info->engine_id)
703 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700704
Simon Glassc033dc82021-11-12 12:28:11 -0700705 if (ret)
706 return ret;
707
708 return node;
Simon Glass19c402a2013-06-13 15:10:02 -0700709}