blob: 312575b44aef480c77d7a4b73a595065857f442e [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Juan Pablo Conde3539c742022-10-25 19:41:02 -04002 * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
Juan Castillo11abdcd2014-10-21 11:30:42 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo11abdcd2014-10-21 11:30:42 +01005 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef KEY_H
8#define KEY_H
Juan Castillo11abdcd2014-10-21 11:30:42 +01009
10#include <openssl/ossl_typ.h>
11
Juan Castillof9f39c32015-06-01 16:34:23 +010012/* Error codes */
13enum {
14 KEY_ERR_NONE,
15 KEY_ERR_MALLOC,
16 KEY_ERR_FILENAME,
17 KEY_ERR_OPEN,
18 KEY_ERR_LOAD
19};
20
21/* Supported key algorithms */
22enum {
Soby Mathew2fd70f62017-08-31 11:50:29 +010023 KEY_ALG_RSA, /* RSA PSS as defined by PKCS#1 v2.1 (default) */
Juan Castilloa2224ab2015-06-30 13:36:57 +010024#ifndef OPENSSL_NO_EC
Lionel Debievefefeffb2022-11-14 11:03:42 +010025 KEY_ALG_ECDSA_NIST,
26 KEY_ALG_ECDSA_BRAINPOOL_R,
27 KEY_ALG_ECDSA_BRAINPOOL_T,
Juan Castilloa2224ab2015-06-30 13:36:57 +010028#endif /* OPENSSL_NO_EC */
29 KEY_ALG_MAX_NUM
Juan Castillof9f39c32015-06-01 16:34:23 +010030};
31
Justin Chadwellfebe86c2019-07-29 17:13:45 +010032/* Maximum number of valid key sizes per algorithm */
33#define KEY_SIZE_MAX_NUM 4
34
Qixiang Xu76a5a9b2017-11-09 13:51:58 +080035/* Supported hash algorithms */
36enum{
37 HASH_ALG_SHA256,
38 HASH_ALG_SHA384,
39 HASH_ALG_SHA512,
40};
41
Justin Chadwellfebe86c2019-07-29 17:13:45 +010042/* Supported key sizes */
43/* NOTE: the first item in each array is the default key size */
44static const unsigned int KEY_SIZES[KEY_ALG_MAX_NUM][KEY_SIZE_MAX_NUM] = {
45 { 2048, 1024, 3072, 4096 }, /* KEY_ALG_RSA */
Justin Chadwellfebe86c2019-07-29 17:13:45 +010046#ifndef OPENSSL_NO_EC
Lionel Debievefefeffb2022-11-14 11:03:42 +010047 {}, /* KEY_ALG_ECDSA_NIST */
48 {}, /* KEY_ALG_ECDSA_BRAINPOOL_R */
49 {} /* KEY_ALG_ECDSA_BRAINPOOL_T */
Justin Chadwellfebe86c2019-07-29 17:13:45 +010050#endif /* OPENSSL_NO_EC */
51};
52
Juan Castillo11abdcd2014-10-21 11:30:42 +010053/*
54 * This structure contains the relevant information to create the keys
55 * required to sign the certificates.
56 *
57 * One instance of this structure must be created for each key, usually in an
58 * array fashion. The filename is obtained at run time from the command line
59 * parameters
60 */
61typedef struct key_s {
62 int id; /* Key id */
Juan Castillo1218dd52015-07-03 16:23:16 +010063 const char *opt; /* Command line option to specify a key */
Juan Castillo212f7382015-12-15 16:37:57 +000064 const char *help_msg; /* Help message */
Juan Castillo11abdcd2014-10-21 11:30:42 +010065 const char *desc; /* Key description (debug purposes) */
66 char *fn; /* Filename to load/store the key */
67 EVP_PKEY *key; /* Key container */
68} key_t;
69
Juan Castilloe6d30e92015-06-12 11:27:59 +010070/* Exported API */
Juan Castillo1218dd52015-07-03 16:23:16 +010071int key_init(void);
72key_t *key_get_by_opt(const char *opt);
Juan Pablo Conde3539c742022-10-25 19:41:02 -040073#if !USING_OPENSSL3
Masahiro Yamadabccb1092017-02-06 21:15:01 +090074int key_new(key_t *key);
Juan Pablo Conde3539c742022-10-25 19:41:02 -040075#endif
Justin Chadwellfebe86c2019-07-29 17:13:45 +010076int key_create(key_t *key, int type, int key_bits);
Juan Castillof9f39c32015-06-01 16:34:23 +010077int key_load(key_t *key, unsigned int *err_code);
Juan Castillo11abdcd2014-10-21 11:30:42 +010078int key_store(key_t *key);
Juan Pablo Conde3539c742022-10-25 19:41:02 -040079void key_cleanup(void);
Juan Castillo11abdcd2014-10-21 11:30:42 +010080
Juan Castilloe6d30e92015-06-12 11:27:59 +010081/* Macro to register the keys used in the CoT */
82#define REGISTER_KEYS(_keys) \
Pankaj Guptadd906e62020-12-09 14:02:38 +053083 key_t *def_keys = &_keys[0]; \
84 const unsigned int num_def_keys = sizeof(_keys)/sizeof(_keys[0])
85
86/* Macro to register the platform defined keys used in the CoT */
87#define PLAT_REGISTER_KEYS(_pdef_keys) \
88 key_t *pdef_keys = &_pdef_keys[0]; \
89 const unsigned int num_pdef_keys = sizeof(_pdef_keys)/sizeof(_pdef_keys[0])
Juan Castilloe6d30e92015-06-12 11:27:59 +010090
91/* Exported variables */
Pankaj Guptadd906e62020-12-09 14:02:38 +053092extern key_t *def_keys;
93extern const unsigned int num_def_keys;
94extern key_t *pdef_keys;
95extern const unsigned int num_pdef_keys;
Juan Castilloe6d30e92015-06-12 11:27:59 +010096
Pankaj Guptadd906e62020-12-09 14:02:38 +053097extern key_t *keys;
98extern unsigned int num_keys;
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +000099#endif /* KEY_H */