1 /*
2  * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef KEY_H
8 #define KEY_H
9 
10 #include <openssl/ossl_typ.h>
11 
12 /* Error codes */
13 enum {
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 */
22 enum {
23 	KEY_ALG_RSA,		/* RSA PSS as defined by PKCS#1 v2.1 (default) */
24 #ifndef OPENSSL_NO_EC
25 	KEY_ALG_ECDSA,
26 #endif /* OPENSSL_NO_EC */
27 	KEY_ALG_MAX_NUM
28 };
29 
30 /* Maximum number of valid key sizes per algorithm */
31 #define KEY_SIZE_MAX_NUM	4
32 
33 /* Supported hash algorithms */
34 enum{
35 	HASH_ALG_SHA256,
36 	HASH_ALG_SHA384,
37 	HASH_ALG_SHA512,
38 };
39 
40 /* Supported key sizes */
41 /* NOTE: the first item in each array is the default key size */
42 static const unsigned int KEY_SIZES[KEY_ALG_MAX_NUM][KEY_SIZE_MAX_NUM] = {
43 	{ 2048, 1024, 3072, 4096 },	/* KEY_ALG_RSA */
44 #ifndef OPENSSL_NO_EC
45 	{}				/* KEY_ALG_ECDSA */
46 #endif /* OPENSSL_NO_EC */
47 };
48 
49 /*
50  * This structure contains the relevant information to create the keys
51  * required to sign the certificates.
52  *
53  * One instance of this structure must be created for each key, usually in an
54  * array fashion. The filename is obtained at run time from the command line
55  * parameters
56  */
57 typedef struct key_s {
58 	int id;			/* Key id */
59 	const char *opt;	/* Command line option to specify a key */
60 	const char *help_msg;	/* Help message */
61 	const char *desc;	/* Key description (debug purposes) */
62 	char *fn;		/* Filename to load/store the key */
63 	EVP_PKEY *key;		/* Key container */
64 } key_t;
65 
66 /* Exported API */
67 int key_init(void);
68 key_t *key_get_by_opt(const char *opt);
69 int key_new(key_t *key);
70 int key_create(key_t *key, int type, int key_bits);
71 int key_load(key_t *key, unsigned int *err_code);
72 int key_store(key_t *key);
73 
74 /* Macro to register the keys used in the CoT */
75 #define REGISTER_KEYS(_keys) \
76 	key_t *def_keys = &_keys[0]; \
77 	const unsigned int num_def_keys = sizeof(_keys)/sizeof(_keys[0])
78 
79 /* Macro to register the platform defined keys used in the CoT */
80 #define PLAT_REGISTER_KEYS(_pdef_keys) \
81 	key_t *pdef_keys = &_pdef_keys[0]; \
82 	const unsigned int num_pdef_keys = sizeof(_pdef_keys)/sizeof(_pdef_keys[0])
83 
84 /* Exported variables */
85 extern key_t *def_keys;
86 extern const unsigned int num_def_keys;
87 extern key_t *pdef_keys;
88 extern const unsigned int num_pdef_keys;
89 
90 extern key_t *keys;
91 extern unsigned int num_keys;
92 #endif /* KEY_H */
93