blob: c08beb8b25b99e36ae71c3162b23190d874b2b2f [file] [log] [blame]
Juan Castillo6f971622014-10-21 11:30:42 +01001/*
Justin Chadwelldfe0f4c2019-07-29 17:13:45 +01002 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
Juan Castillo6f971622014-10-21 11:30:42 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo6f971622014-10-21 11:30:42 +01005 */
6
Antonio Nino Diazc3cf06f2018-11-08 10:20:19 +00007#ifndef KEY_H
8#define KEY_H
Juan Castillo6f971622014-10-21 11:30:42 +01009
10#include <openssl/ossl_typ.h>
11
Juan Castilloccbf8902015-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 Mathewa8eb2862017-08-31 11:50:29 +010023 KEY_ALG_RSA, /* RSA PSS as defined by PKCS#1 v2.1 (default) */
24 KEY_ALG_RSA_1_5, /* RSA as defined by PKCS#1 v1.5 */
Juan Castilloed2a76e2015-06-30 13:36:57 +010025#ifndef OPENSSL_NO_EC
26 KEY_ALG_ECDSA,
27#endif /* OPENSSL_NO_EC */
28 KEY_ALG_MAX_NUM
Juan Castilloccbf8902015-06-01 16:34:23 +010029};
30
Justin Chadwelldfe0f4c2019-07-29 17:13:45 +010031/* Maximum number of valid key sizes per algorithm */
32#define KEY_SIZE_MAX_NUM 4
33
Qixiang Xu29722472017-11-09 13:51:58 +080034/* Supported hash algorithms */
35enum{
36 HASH_ALG_SHA256,
37 HASH_ALG_SHA384,
38 HASH_ALG_SHA512,
39};
40
Justin Chadwelldfe0f4c2019-07-29 17:13:45 +010041/* Supported key sizes */
42/* NOTE: the first item in each array is the default key size */
43static const unsigned int KEY_SIZES[KEY_ALG_MAX_NUM][KEY_SIZE_MAX_NUM] = {
44 { 2048, 1024, 3072, 4096 }, /* KEY_ALG_RSA */
45 { 2048, 1024, 3072, 4096 }, /* KEY_ALG_RSA_1_5 */
46#ifndef OPENSSL_NO_EC
47 {} /* KEY_ALG_ECDSA */
48#endif /* OPENSSL_NO_EC */
49};
50
Juan Castillo6f971622014-10-21 11:30:42 +010051/*
52 * This structure contains the relevant information to create the keys
53 * required to sign the certificates.
54 *
55 * One instance of this structure must be created for each key, usually in an
56 * array fashion. The filename is obtained at run time from the command line
57 * parameters
58 */
59typedef struct key_s {
60 int id; /* Key id */
Juan Castilload2c1a92015-07-03 16:23:16 +010061 const char *opt; /* Command line option to specify a key */
Juan Castillo159807e2015-12-15 16:37:57 +000062 const char *help_msg; /* Help message */
Juan Castillo6f971622014-10-21 11:30:42 +010063 const char *desc; /* Key description (debug purposes) */
64 char *fn; /* Filename to load/store the key */
65 EVP_PKEY *key; /* Key container */
66} key_t;
67
Juan Castillo55e291a2015-06-12 11:27:59 +010068/* Exported API */
Juan Castilload2c1a92015-07-03 16:23:16 +010069int key_init(void);
70key_t *key_get_by_opt(const char *opt);
Masahiro Yamada762f1eb2017-02-06 21:15:01 +090071int key_new(key_t *key);
Justin Chadwelldfe0f4c2019-07-29 17:13:45 +010072int key_create(key_t *key, int type, int key_bits);
Juan Castilloccbf8902015-06-01 16:34:23 +010073int key_load(key_t *key, unsigned int *err_code);
Juan Castillo6f971622014-10-21 11:30:42 +010074int key_store(key_t *key);
75
Juan Castillo55e291a2015-06-12 11:27:59 +010076/* Macro to register the keys used in the CoT */
77#define REGISTER_KEYS(_keys) \
78 key_t *keys = &_keys[0]; \
Sandrine Bailleuxaa856912016-01-04 15:49:23 +000079 const unsigned int num_keys = sizeof(_keys)/sizeof(_keys[0])
Juan Castillo55e291a2015-06-12 11:27:59 +010080
81/* Exported variables */
82extern key_t *keys;
83extern const unsigned int num_keys;
84
Antonio Nino Diazc3cf06f2018-11-08 10:20:19 +000085#endif /* KEY_H */