Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "mkimage.h" |
Jonathan Gray | cc3ed40 | 2020-05-15 22:20:34 +1000 | [diff] [blame] | 7 | #include <stdlib.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <string.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 10 | #include <image.h> |
| 11 | #include <time.h> |
Alexandru Gagniuc | 32abcbd | 2021-02-19 12:45:11 -0600 | [diff] [blame] | 12 | #include <u-boot/fdt-libcrypto.h> |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 13 | #include <openssl/bn.h> |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 14 | #include <openssl/ec.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 15 | #include <openssl/rsa.h> |
| 16 | #include <openssl/pem.h> |
| 17 | #include <openssl/err.h> |
| 18 | #include <openssl/ssl.h> |
| 19 | #include <openssl/evp.h> |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 20 | #include <openssl/engine.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 21 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 22 | static int rsa_err(const char *msg) |
| 23 | { |
| 24 | unsigned long sslErr = ERR_get_error(); |
| 25 | |
| 26 | fprintf(stderr, "%s", msg); |
| 27 | fprintf(stderr, ": %s\n", |
| 28 | ERR_error_string(sslErr, 0)); |
| 29 | |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | /** |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 34 | * rsa_pem_get_pub_key() - read a public key from a .crt file |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 35 | * |
| 36 | * @keydir: Directory containins the key |
| 37 | * @name Name of key file (will have a .crt extension) |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 38 | * @evpp Returns EVP_PKEY object, or NULL on failure |
| 39 | * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 40 | */ |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 41 | static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **evpp) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 42 | { |
| 43 | char path[1024]; |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 44 | EVP_PKEY *key = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 45 | X509 *cert; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 46 | FILE *f; |
| 47 | int ret; |
| 48 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 49 | if (!evpp) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | *evpp = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 53 | snprintf(path, sizeof(path), "%s/%s.crt", keydir, name); |
| 54 | f = fopen(path, "r"); |
| 55 | if (!f) { |
| 56 | fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", |
| 57 | path, strerror(errno)); |
| 58 | return -EACCES; |
| 59 | } |
| 60 | |
| 61 | /* Read the certificate */ |
| 62 | cert = NULL; |
| 63 | if (!PEM_read_X509(f, &cert, NULL, NULL)) { |
| 64 | rsa_err("Couldn't read certificate"); |
| 65 | ret = -EINVAL; |
| 66 | goto err_cert; |
| 67 | } |
| 68 | |
| 69 | /* Get the public key from the certificate. */ |
| 70 | key = X509_get_pubkey(cert); |
| 71 | if (!key) { |
| 72 | rsa_err("Couldn't read public key\n"); |
| 73 | ret = -EINVAL; |
| 74 | goto err_pubkey; |
| 75 | } |
| 76 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 77 | fclose(f); |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 78 | *evpp = key; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 79 | X509_free(cert); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 83 | err_pubkey: |
| 84 | X509_free(cert); |
| 85 | err_cert: |
| 86 | fclose(f); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /** |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 91 | * rsa_engine_get_pub_key() - read a public key from given engine |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 92 | * |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 93 | * @keydir: Key prefix |
| 94 | * @name Name of key |
| 95 | * @engine Engine to use |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 96 | * @evpp Returns EVP_PKEY object, or NULL on failure |
| 97 | * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 98 | */ |
| 99 | static int rsa_engine_get_pub_key(const char *keydir, const char *name, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 100 | ENGINE *engine, EVP_PKEY **evpp) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 101 | { |
| 102 | const char *engine_id; |
| 103 | char key_id[1024]; |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 104 | EVP_PKEY *key = NULL; |
| 105 | |
| 106 | if (!evpp) |
| 107 | return -EINVAL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 108 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 109 | *evpp = NULL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 110 | |
| 111 | engine_id = ENGINE_get_id(engine); |
| 112 | |
| 113 | if (engine_id && !strcmp(engine_id, "pkcs11")) { |
| 114 | if (keydir) |
Jan Luebbe | cd4eafb | 2020-05-13 12:26:24 +0200 | [diff] [blame] | 115 | if (strstr(keydir, "object=")) |
| 116 | snprintf(key_id, sizeof(key_id), |
| 117 | "pkcs11:%s;type=public", |
| 118 | keydir); |
| 119 | else |
| 120 | snprintf(key_id, sizeof(key_id), |
| 121 | "pkcs11:%s;object=%s;type=public", |
| 122 | keydir, name); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 123 | else |
| 124 | snprintf(key_id, sizeof(key_id), |
| 125 | "pkcs11:object=%s;type=public", |
| 126 | name); |
Vesa Jääskeläinen | abf8eb2 | 2019-06-16 20:53:38 +0300 | [diff] [blame] | 127 | } else if (engine_id) { |
| 128 | if (keydir) |
| 129 | snprintf(key_id, sizeof(key_id), |
| 130 | "%s%s", |
| 131 | keydir, name); |
| 132 | else |
| 133 | snprintf(key_id, sizeof(key_id), |
| 134 | "%s", |
| 135 | name); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 136 | } else { |
| 137 | fprintf(stderr, "Engine not supported\n"); |
| 138 | return -ENOTSUP; |
| 139 | } |
| 140 | |
| 141 | key = ENGINE_load_public_key(engine, key_id, NULL, NULL); |
| 142 | if (!key) |
| 143 | return rsa_err("Failure loading public key from engine"); |
| 144 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 145 | *evpp = key; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 146 | |
| 147 | return 0; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * rsa_get_pub_key() - read a public key |
| 152 | * |
| 153 | * @keydir: Directory containing the key (PEM file) or key prefix (engine) |
| 154 | * @name Name of key file (will have a .crt extension) |
| 155 | * @engine Engine to use |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 156 | * @evpp Returns EVP_PKEY object, or NULL on failure |
| 157 | * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 158 | */ |
| 159 | static int rsa_get_pub_key(const char *keydir, const char *name, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 160 | ENGINE *engine, EVP_PKEY **evpp) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 161 | { |
| 162 | if (engine) |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 163 | return rsa_engine_get_pub_key(keydir, name, engine, evpp); |
| 164 | return rsa_pem_get_pub_key(keydir, name, evpp); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * rsa_pem_get_priv_key() - read a private key from a .key file |
| 169 | * |
| 170 | * @keydir: Directory containing the key |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 171 | * @name Name of key file (will have a .key extension) |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 172 | * @evpp Returns EVP_PKEY object, or NULL on failure |
| 173 | * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 174 | */ |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 175 | static int rsa_pem_get_priv_key(const char *keydir, const char *name, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 176 | const char *keyfile, EVP_PKEY **evpp) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 177 | { |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 178 | char path[1024] = {0}; |
| 179 | FILE *f = NULL; |
| 180 | |
| 181 | if (!evpp) |
| 182 | return -EINVAL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 183 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 184 | *evpp = NULL; |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 185 | if (keydir && name) |
| 186 | snprintf(path, sizeof(path), "%s/%s.key", keydir, name); |
| 187 | else if (keyfile) |
| 188 | snprintf(path, sizeof(path), "%s", keyfile); |
| 189 | else |
| 190 | return -EINVAL; |
| 191 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 192 | f = fopen(path, "r"); |
| 193 | if (!f) { |
| 194 | fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n", |
| 195 | path, strerror(errno)); |
| 196 | return -ENOENT; |
| 197 | } |
| 198 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 199 | if (!PEM_read_PrivateKey(f, evpp, NULL, path)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 200 | rsa_err("Failure reading private key"); |
| 201 | fclose(f); |
| 202 | return -EPROTO; |
| 203 | } |
| 204 | fclose(f); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 209 | /** |
| 210 | * rsa_engine_get_priv_key() - read a private key from given engine |
| 211 | * |
| 212 | * @keydir: Key prefix |
| 213 | * @name Name of key |
| 214 | * @engine Engine to use |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 215 | * @evpp Returns EVP_PKEY object, or NULL on failure |
| 216 | * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 217 | */ |
| 218 | static int rsa_engine_get_priv_key(const char *keydir, const char *name, |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 219 | const char *keyfile, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 220 | ENGINE *engine, EVP_PKEY **evpp) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 221 | { |
| 222 | const char *engine_id; |
| 223 | char key_id[1024]; |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 224 | EVP_PKEY *key = NULL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 225 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 226 | if (!evpp) |
| 227 | return -EINVAL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 228 | |
| 229 | engine_id = ENGINE_get_id(engine); |
| 230 | |
| 231 | if (engine_id && !strcmp(engine_id, "pkcs11")) { |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 232 | if (!keydir && !name) { |
| 233 | fprintf(stderr, "Please use 'keydir' with PKCS11\n"); |
| 234 | return -EINVAL; |
| 235 | } |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 236 | if (keydir) |
Jan Luebbe | cd4eafb | 2020-05-13 12:26:24 +0200 | [diff] [blame] | 237 | if (strstr(keydir, "object=")) |
| 238 | snprintf(key_id, sizeof(key_id), |
| 239 | "pkcs11:%s;type=private", |
| 240 | keydir); |
| 241 | else |
| 242 | snprintf(key_id, sizeof(key_id), |
| 243 | "pkcs11:%s;object=%s;type=private", |
| 244 | keydir, name); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 245 | else |
| 246 | snprintf(key_id, sizeof(key_id), |
| 247 | "pkcs11:object=%s;type=private", |
| 248 | name); |
Vesa Jääskeläinen | abf8eb2 | 2019-06-16 20:53:38 +0300 | [diff] [blame] | 249 | } else if (engine_id) { |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 250 | if (keydir && name) |
Vesa Jääskeläinen | abf8eb2 | 2019-06-16 20:53:38 +0300 | [diff] [blame] | 251 | snprintf(key_id, sizeof(key_id), |
| 252 | "%s%s", |
| 253 | keydir, name); |
Heinrich Schuchardt | 48806ee | 2021-08-28 12:13:05 +0200 | [diff] [blame] | 254 | else if (name) |
Vesa Jääskeläinen | abf8eb2 | 2019-06-16 20:53:38 +0300 | [diff] [blame] | 255 | snprintf(key_id, sizeof(key_id), |
| 256 | "%s", |
Heinrich Schuchardt | 1e92a8a | 2021-07-30 17:05:07 +0200 | [diff] [blame] | 257 | name ? name : ""); |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 258 | else if (keyfile) |
| 259 | snprintf(key_id, sizeof(key_id), "%s", keyfile); |
| 260 | else |
| 261 | return -EINVAL; |
| 262 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 263 | } else { |
| 264 | fprintf(stderr, "Engine not supported\n"); |
| 265 | return -ENOTSUP; |
| 266 | } |
| 267 | |
| 268 | key = ENGINE_load_private_key(engine, key_id, NULL, NULL); |
| 269 | if (!key) |
| 270 | return rsa_err("Failure loading private key from engine"); |
| 271 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 272 | *evpp = key; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 273 | |
| 274 | return 0; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
| 278 | * rsa_get_priv_key() - read a private key |
| 279 | * |
| 280 | * @keydir: Directory containing the key (PEM file) or key prefix (engine) |
| 281 | * @name Name of key |
| 282 | * @engine Engine to use for signing |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 283 | * @evpp Returns EVP_PKEY object, or NULL on failure |
| 284 | * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 285 | */ |
| 286 | static int rsa_get_priv_key(const char *keydir, const char *name, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 287 | const char *keyfile, ENGINE *engine, EVP_PKEY **evpp) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 288 | { |
| 289 | if (engine) |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 290 | return rsa_engine_get_priv_key(keydir, name, keyfile, engine, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 291 | evpp); |
| 292 | return rsa_pem_get_priv_key(keydir, name, keyfile, evpp); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 293 | } |
| 294 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 295 | static int rsa_init(void) |
| 296 | { |
| 297 | int ret; |
| 298 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 299 | ret = OPENSSL_init_ssl(0, NULL); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 300 | if (!ret) { |
| 301 | fprintf(stderr, "Failure to init SSL library\n"); |
| 302 | return -1; |
| 303 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 308 | static int rsa_engine_init(const char *engine_id, ENGINE **pe) |
| 309 | { |
Marc Kleine-Budde | 88e07a7 | 2021-07-23 22:17:50 +0200 | [diff] [blame] | 310 | const char *key_pass; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 311 | ENGINE *e; |
| 312 | int ret; |
| 313 | |
| 314 | ENGINE_load_builtin_engines(); |
| 315 | |
| 316 | e = ENGINE_by_id(engine_id); |
| 317 | if (!e) { |
| 318 | fprintf(stderr, "Engine isn't available\n"); |
Alexandru Gagniuc | b63dc08 | 2021-07-29 13:31:21 -0500 | [diff] [blame] | 319 | return -1; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | if (!ENGINE_init(e)) { |
| 323 | fprintf(stderr, "Couldn't initialize engine\n"); |
| 324 | ret = -1; |
| 325 | goto err_engine_init; |
| 326 | } |
| 327 | |
| 328 | if (!ENGINE_set_default_RSA(e)) { |
| 329 | fprintf(stderr, "Couldn't set engine as default for RSA\n"); |
| 330 | ret = -1; |
| 331 | goto err_set_rsa; |
| 332 | } |
| 333 | |
Marc Kleine-Budde | 88e07a7 | 2021-07-23 22:17:50 +0200 | [diff] [blame] | 334 | key_pass = getenv("MKIMAGE_SIGN_PIN"); |
| 335 | if (key_pass) { |
| 336 | if (!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) { |
| 337 | fprintf(stderr, "Couldn't set PIN\n"); |
| 338 | ret = -1; |
| 339 | goto err_set_pin; |
| 340 | } |
| 341 | } |
| 342 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 343 | *pe = e; |
| 344 | |
| 345 | return 0; |
| 346 | |
Marc Kleine-Budde | 88e07a7 | 2021-07-23 22:17:50 +0200 | [diff] [blame] | 347 | err_set_pin: |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 348 | err_set_rsa: |
| 349 | ENGINE_finish(e); |
| 350 | err_engine_init: |
| 351 | ENGINE_free(e); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 352 | return ret; |
| 353 | } |
| 354 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 355 | static void rsa_engine_remove(ENGINE *e) |
| 356 | { |
| 357 | if (e) { |
| 358 | ENGINE_finish(e); |
| 359 | ENGINE_free(e); |
| 360 | } |
| 361 | } |
| 362 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 363 | static int rsa_sign_with_key(EVP_PKEY *pkey, struct padding_algo *padding_algo, |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 364 | struct checksum_algo *checksum_algo, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 365 | const struct image_region region[], int region_count, |
| 366 | uint8_t **sigp, uint *sig_size) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 367 | { |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 368 | EVP_PKEY_CTX *ckey; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 369 | EVP_MD_CTX *context; |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 370 | int ret = 0; |
| 371 | size_t size; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 372 | uint8_t *sig; |
| 373 | int i; |
| 374 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 375 | size = EVP_PKEY_size(pkey); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 376 | sig = malloc(size); |
| 377 | if (!sig) { |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 378 | fprintf(stderr, "Out of memory for signature (%zu bytes)\n", |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 379 | size); |
| 380 | ret = -ENOMEM; |
| 381 | goto err_alloc; |
| 382 | } |
| 383 | |
| 384 | context = EVP_MD_CTX_create(); |
| 385 | if (!context) { |
| 386 | ret = rsa_err("EVP context creation failed"); |
| 387 | goto err_create; |
| 388 | } |
| 389 | EVP_MD_CTX_init(context); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 390 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 391 | ckey = EVP_PKEY_CTX_new(pkey, NULL); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 392 | if (!ckey) { |
| 393 | ret = rsa_err("EVP key context creation failed"); |
| 394 | goto err_create; |
| 395 | } |
| 396 | |
| 397 | if (EVP_DigestSignInit(context, &ckey, |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 398 | checksum_algo->calculate_sign(), |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 399 | NULL, pkey) <= 0) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 400 | ret = rsa_err("Signer setup failed"); |
| 401 | goto err_sign; |
| 402 | } |
| 403 | |
Simon Glass | effa452 | 2021-09-25 19:43:23 -0600 | [diff] [blame] | 404 | if (CONFIG_IS_ENABLED(FIT_RSASSA_PSS) && padding_algo && |
| 405 | !strcmp(padding_algo->name, "pss")) { |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 406 | if (EVP_PKEY_CTX_set_rsa_padding(ckey, |
| 407 | RSA_PKCS1_PSS_PADDING) <= 0) { |
| 408 | ret = rsa_err("Signer padding setup failed"); |
| 409 | goto err_sign; |
| 410 | } |
| 411 | } |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 412 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 413 | for (i = 0; i < region_count; i++) { |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 414 | if (!EVP_DigestSignUpdate(context, region[i].data, |
| 415 | region[i].size)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 416 | ret = rsa_err("Signing data failed"); |
| 417 | goto err_sign; |
| 418 | } |
| 419 | } |
| 420 | |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 421 | if (!EVP_DigestSignFinal(context, sig, &size)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 422 | ret = rsa_err("Could not obtain signature"); |
| 423 | goto err_sign; |
| 424 | } |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 425 | |
Alexandru Gagniuc | b63dc08 | 2021-07-29 13:31:21 -0500 | [diff] [blame] | 426 | EVP_MD_CTX_reset(context); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 427 | EVP_MD_CTX_destroy(context); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 428 | |
Chan, Donald | 9ccd605 | 2021-07-19 09:18:54 -0700 | [diff] [blame] | 429 | debug("Got signature: %zu bytes, expected %d\n", size, EVP_PKEY_size(pkey)); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 430 | *sigp = sig; |
| 431 | *sig_size = size; |
| 432 | |
| 433 | return 0; |
| 434 | |
| 435 | err_sign: |
| 436 | EVP_MD_CTX_destroy(context); |
| 437 | err_create: |
| 438 | free(sig); |
| 439 | err_alloc: |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | int rsa_sign(struct image_sign_info *info, |
| 444 | const struct image_region region[], int region_count, |
| 445 | uint8_t **sigp, uint *sig_len) |
| 446 | { |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 447 | EVP_PKEY *pkey = NULL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 448 | ENGINE *e = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 449 | int ret; |
| 450 | |
| 451 | ret = rsa_init(); |
| 452 | if (ret) |
| 453 | return ret; |
| 454 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 455 | if (info->engine_id) { |
| 456 | ret = rsa_engine_init(info->engine_id, &e); |
| 457 | if (ret) |
Alexandru Gagniuc | b63dc08 | 2021-07-29 13:31:21 -0500 | [diff] [blame] | 458 | return ret; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 459 | } |
| 460 | |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 461 | ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 462 | e, &pkey); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 463 | if (ret) |
| 464 | goto err_priv; |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 465 | ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 466 | region_count, sigp, sig_len); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 467 | if (ret) |
| 468 | goto err_sign; |
| 469 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 470 | EVP_PKEY_free(pkey); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 471 | if (info->engine_id) |
| 472 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 473 | |
| 474 | return ret; |
| 475 | |
| 476 | err_sign: |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 477 | EVP_PKEY_free(pkey); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 478 | err_priv: |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 479 | if (info->engine_id) |
| 480 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 481 | return ret; |
| 482 | } |
| 483 | |
| 484 | /* |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 485 | * rsa_get_exponent(): - Get the public exponent from an RSA key |
| 486 | */ |
| 487 | static int rsa_get_exponent(RSA *key, uint64_t *e) |
| 488 | { |
| 489 | int ret; |
| 490 | BIGNUM *bn_te; |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 491 | const BIGNUM *key_e; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 492 | uint64_t te; |
| 493 | |
| 494 | ret = -EINVAL; |
| 495 | bn_te = NULL; |
| 496 | |
| 497 | if (!e) |
| 498 | goto cleanup; |
| 499 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 500 | RSA_get0_key(key, NULL, &key_e, NULL); |
| 501 | if (BN_num_bits(key_e) > 64) |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 502 | goto cleanup; |
| 503 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 504 | *e = BN_get_word(key_e); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 505 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 506 | if (BN_num_bits(key_e) < 33) { |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 507 | ret = 0; |
| 508 | goto cleanup; |
| 509 | } |
| 510 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 511 | bn_te = BN_dup(key_e); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 512 | if (!bn_te) |
| 513 | goto cleanup; |
| 514 | |
| 515 | if (!BN_rshift(bn_te, bn_te, 32)) |
| 516 | goto cleanup; |
| 517 | |
| 518 | if (!BN_mask_bits(bn_te, 32)) |
| 519 | goto cleanup; |
| 520 | |
| 521 | te = BN_get_word(bn_te); |
| 522 | te <<= 32; |
| 523 | *e |= te; |
| 524 | ret = 0; |
| 525 | |
| 526 | cleanup: |
| 527 | if (bn_te) |
| 528 | BN_free(bn_te); |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | /* |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 534 | * rsa_get_params(): - Get the important parameters of an RSA public key |
| 535 | */ |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 536 | int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp, |
| 537 | BIGNUM **modulusp, BIGNUM **r_squaredp) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 538 | { |
| 539 | BIGNUM *big1, *big2, *big32, *big2_32; |
| 540 | BIGNUM *n, *r, *r_squared, *tmp; |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 541 | const BIGNUM *key_n; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 542 | BN_CTX *bn_ctx = BN_CTX_new(); |
| 543 | int ret = 0; |
| 544 | |
| 545 | /* Initialize BIGNUMs */ |
| 546 | big1 = BN_new(); |
| 547 | big2 = BN_new(); |
| 548 | big32 = BN_new(); |
| 549 | r = BN_new(); |
| 550 | r_squared = BN_new(); |
| 551 | tmp = BN_new(); |
| 552 | big2_32 = BN_new(); |
| 553 | n = BN_new(); |
| 554 | if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 || |
| 555 | !n) { |
| 556 | fprintf(stderr, "Out of memory (bignum)\n"); |
| 557 | return -ENOMEM; |
| 558 | } |
| 559 | |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 560 | if (0 != rsa_get_exponent(key, exponent)) |
| 561 | ret = -1; |
| 562 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 563 | RSA_get0_key(key, &key_n, NULL, NULL); |
| 564 | if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) || |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 565 | !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L)) |
| 566 | ret = -1; |
| 567 | |
| 568 | /* big2_32 = 2^32 */ |
| 569 | if (!BN_exp(big2_32, big2, big32, bn_ctx)) |
| 570 | ret = -1; |
| 571 | |
| 572 | /* Calculate n0_inv = -1 / n[0] mod 2^32 */ |
| 573 | if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) || |
| 574 | !BN_sub(tmp, big2_32, tmp)) |
| 575 | ret = -1; |
| 576 | *n0_invp = BN_get_word(tmp); |
| 577 | |
| 578 | /* Calculate R = 2^(# of key bits) */ |
| 579 | if (!BN_set_word(tmp, BN_num_bits(n)) || |
| 580 | !BN_exp(r, big2, tmp, bn_ctx)) |
| 581 | ret = -1; |
| 582 | |
| 583 | /* Calculate r_squared = R^2 mod n */ |
| 584 | if (!BN_copy(r_squared, r) || |
| 585 | !BN_mul(tmp, r_squared, r, bn_ctx) || |
| 586 | !BN_mod(r_squared, tmp, n, bn_ctx)) |
| 587 | ret = -1; |
| 588 | |
| 589 | *modulusp = n; |
| 590 | *r_squaredp = r_squared; |
| 591 | |
| 592 | BN_free(big1); |
| 593 | BN_free(big2); |
| 594 | BN_free(big32); |
| 595 | BN_free(r); |
| 596 | BN_free(tmp); |
| 597 | BN_free(big2_32); |
| 598 | if (ret) { |
| 599 | fprintf(stderr, "Bignum operations failed\n"); |
| 600 | return -ENOMEM; |
| 601 | } |
| 602 | |
| 603 | return ret; |
| 604 | } |
| 605 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 606 | int rsa_add_verify_data(struct image_sign_info *info, void *keydest) |
| 607 | { |
| 608 | BIGNUM *modulus, *r_squared; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 609 | uint64_t exponent; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 610 | uint32_t n0_inv; |
| 611 | int parent, node; |
| 612 | char name[100]; |
| 613 | int ret; |
| 614 | int bits; |
| 615 | RSA *rsa; |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 616 | EVP_PKEY *pkey = NULL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 617 | ENGINE *e = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 618 | |
| 619 | debug("%s: Getting verification data\n", __func__); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 620 | if (info->engine_id) { |
| 621 | ret = rsa_engine_init(info->engine_id, &e); |
| 622 | if (ret) |
| 623 | return ret; |
| 624 | } |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 625 | ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 626 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 627 | goto err_get_pub_key; |
Alexandru Gagniuc | b63dc08 | 2021-07-29 13:31:21 -0500 | [diff] [blame] | 628 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 629 | rsa = EVP_PKEY_get0_RSA(pkey); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 630 | ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 631 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 632 | goto err_get_params; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 633 | bits = BN_num_bits(modulus); |
| 634 | parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME); |
| 635 | if (parent == -FDT_ERR_NOTFOUND) { |
| 636 | parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME); |
| 637 | if (parent < 0) { |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 638 | ret = parent; |
| 639 | if (ret != -FDT_ERR_NOSPACE) { |
| 640 | fprintf(stderr, "Couldn't create signature node: %s\n", |
| 641 | fdt_strerror(parent)); |
| 642 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 645 | if (ret) |
| 646 | goto done; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 647 | |
| 648 | /* Either create or overwrite the named key node */ |
| 649 | snprintf(name, sizeof(name), "key-%s", info->keyname); |
| 650 | node = fdt_subnode_offset(keydest, parent, name); |
| 651 | if (node == -FDT_ERR_NOTFOUND) { |
| 652 | node = fdt_add_subnode(keydest, parent, name); |
| 653 | if (node < 0) { |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 654 | ret = node; |
| 655 | if (ret != -FDT_ERR_NOSPACE) { |
| 656 | fprintf(stderr, "Could not create key subnode: %s\n", |
| 657 | fdt_strerror(node)); |
| 658 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 659 | } |
| 660 | } else if (node < 0) { |
| 661 | fprintf(stderr, "Cannot select keys parent: %s\n", |
| 662 | fdt_strerror(node)); |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 663 | ret = node; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 666 | if (!ret) { |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 667 | ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT, |
| 668 | info->keyname); |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 669 | } |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 670 | if (!ret) |
| 671 | ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits); |
| 672 | if (!ret) |
| 673 | ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv); |
| 674 | if (!ret) { |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 675 | ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent); |
| 676 | } |
| 677 | if (!ret) { |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 678 | ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus, |
| 679 | bits); |
| 680 | } |
| 681 | if (!ret) { |
| 682 | ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared, |
| 683 | bits); |
| 684 | } |
| 685 | if (!ret) { |
| 686 | ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP, |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 687 | info->name); |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 688 | } |
mario.six@gdsys.cc | 99fc6ea | 2016-07-19 11:07:07 +0200 | [diff] [blame] | 689 | if (!ret && info->require_keys) { |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 690 | ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED, |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 691 | info->require_keys); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 692 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 693 | done: |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 694 | BN_free(modulus); |
| 695 | BN_free(r_squared); |
| 696 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 697 | ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
| 698 | err_get_params: |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 699 | EVP_PKEY_free(pkey); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 700 | err_get_pub_key: |
| 701 | if (info->engine_id) |
| 702 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 703 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 704 | return ret; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 705 | } |