xref: /rk3399_ARM-atf/tools/cert_create/include/key.h (revision 5d8c721836606327c6a09678527933b4d180c10c)
16f971622SJuan Castillo /*
2ccbfd01dSManish V Badarkhe  * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
36f971622SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
56f971622SJuan Castillo  */
66f971622SJuan Castillo 
7c3cf06f1SAntonio Nino Diaz #ifndef KEY_H
8c3cf06f1SAntonio Nino Diaz #define KEY_H
96f971622SJuan Castillo 
106f971622SJuan Castillo #include <openssl/ossl_typ.h>
116f971622SJuan Castillo 
12ccbf890eSJuan Castillo /* Error codes */
13ccbf890eSJuan Castillo enum {
14ccbf890eSJuan Castillo 	KEY_ERR_NONE,
15ccbf890eSJuan Castillo 	KEY_ERR_MALLOC,
16ccbf890eSJuan Castillo 	KEY_ERR_FILENAME,
17ccbf890eSJuan Castillo 	KEY_ERR_OPEN,
18ccbf890eSJuan Castillo 	KEY_ERR_LOAD
19ccbf890eSJuan Castillo };
20ccbf890eSJuan Castillo 
21ccbf890eSJuan Castillo /* Supported key algorithms */
22ccbf890eSJuan Castillo enum {
23a8eb286aSSoby Mathew 	KEY_ALG_RSA,		/* RSA PSS as defined by PKCS#1 v2.1 (default) */
24ed2a76eaSJuan Castillo #ifndef OPENSSL_NO_EC
25e78ba69eSLionel Debieve 	KEY_ALG_ECDSA_NIST,
26e78ba69eSLionel Debieve 	KEY_ALG_ECDSA_BRAINPOOL_R,
27e78ba69eSLionel Debieve 	KEY_ALG_ECDSA_BRAINPOOL_T,
28ed2a76eaSJuan Castillo #endif /* OPENSSL_NO_EC */
29ed2a76eaSJuan Castillo 	KEY_ALG_MAX_NUM
30ccbf890eSJuan Castillo };
31ccbf890eSJuan Castillo 
32dfe0f4c2SJustin Chadwell /* Maximum number of valid key sizes per algorithm */
33dfe0f4c2SJustin Chadwell #define KEY_SIZE_MAX_NUM	4
34dfe0f4c2SJustin Chadwell 
352972247cSQixiang Xu /* Supported hash algorithms */
362972247cSQixiang Xu enum{
372972247cSQixiang Xu 	HASH_ALG_SHA256,
382972247cSQixiang Xu 	HASH_ALG_SHA384,
392972247cSQixiang Xu 	HASH_ALG_SHA512,
402972247cSQixiang Xu };
412972247cSQixiang Xu 
42dfe0f4c2SJustin Chadwell /* Supported key sizes */
43dfe0f4c2SJustin Chadwell /* NOTE: the first item in each array is the default key size */
44dfe0f4c2SJustin Chadwell static const unsigned int KEY_SIZES[KEY_ALG_MAX_NUM][KEY_SIZE_MAX_NUM] = {
45dfe0f4c2SJustin Chadwell 	{ 2048, 1024, 3072, 4096 },	/* KEY_ALG_RSA */
46dfe0f4c2SJustin Chadwell #ifndef OPENSSL_NO_EC
47c512c89cSlaurenw-arm 	{ 256, 384 },			/* KEY_ALG_ECDSA_NIST */
48*0da16fe3SMaxime Méré 	{ 256 },			/* KEY_ALG_ECDSA_BRAINPOOL_R */
49*0da16fe3SMaxime Méré 	{ 256 }				/* KEY_ALG_ECDSA_BRAINPOOL_T */
50dfe0f4c2SJustin Chadwell #endif /* OPENSSL_NO_EC */
51dfe0f4c2SJustin Chadwell };
52dfe0f4c2SJustin Chadwell 
536f971622SJuan Castillo /*
546f971622SJuan Castillo  * This structure contains the relevant information to create the keys
556f971622SJuan Castillo  * required to sign the certificates.
566f971622SJuan Castillo  *
576f971622SJuan Castillo  * One instance of this structure must be created for each key, usually in an
586f971622SJuan Castillo  * array fashion. The filename is obtained at run time from the command line
596f971622SJuan Castillo  * parameters
606f971622SJuan Castillo  */
616f971622SJuan Castillo typedef struct key_s {
626f971622SJuan Castillo 	int id;			/* Key id */
63ad2c1a9aSJuan Castillo 	const char *opt;	/* Command line option to specify a key */
64159807e2SJuan Castillo 	const char *help_msg;	/* Help message */
656f971622SJuan Castillo 	const char *desc;	/* Key description (debug purposes) */
666f971622SJuan Castillo 	char *fn;		/* Filename to load/store the key */
676f971622SJuan Castillo 	EVP_PKEY *key;		/* Key container */
68ccbfd01dSManish V Badarkhe } cert_key_t;
696f971622SJuan Castillo 
7055e291a4SJuan Castillo /* Exported API */
71ad2c1a9aSJuan Castillo int key_init(void);
72ccbfd01dSManish V Badarkhe cert_key_t *key_get_by_opt(const char *opt);
73cf2dd17dSJuan Pablo Conde #if !USING_OPENSSL3
74ccbfd01dSManish V Badarkhe int key_new(cert_key_t *key);
75cf2dd17dSJuan Pablo Conde #endif
76ccbfd01dSManish V Badarkhe int key_create(cert_key_t *key, int type, int key_bits);
77ccbfd01dSManish V Badarkhe unsigned int key_load(cert_key_t *key);
78ccbfd01dSManish V Badarkhe int key_store(cert_key_t *key);
79cf2dd17dSJuan Pablo Conde void key_cleanup(void);
806f971622SJuan Castillo 
8155e291a4SJuan Castillo /* Macro to register the keys used in the CoT */
8255e291a4SJuan Castillo #define REGISTER_KEYS(_keys) \
83ccbfd01dSManish V Badarkhe 	cert_key_t *def_keys = &_keys[0]; \
84b94bf967SPankaj Gupta 	const unsigned int num_def_keys = sizeof(_keys)/sizeof(_keys[0])
85b94bf967SPankaj Gupta 
86b94bf967SPankaj Gupta /* Macro to register the platform defined keys used in the CoT */
87b94bf967SPankaj Gupta #define PLAT_REGISTER_KEYS(_pdef_keys) \
88ccbfd01dSManish V Badarkhe 	cert_key_t *pdef_keys = &_pdef_keys[0]; \
89b94bf967SPankaj Gupta 	const unsigned int num_pdef_keys = sizeof(_pdef_keys)/sizeof(_pdef_keys[0])
9055e291a4SJuan Castillo 
9155e291a4SJuan Castillo /* Exported variables */
92ccbfd01dSManish V Badarkhe extern cert_key_t *def_keys;
93b94bf967SPankaj Gupta extern const unsigned int num_def_keys;
94ccbfd01dSManish V Badarkhe extern cert_key_t *pdef_keys;
95b94bf967SPankaj Gupta extern const unsigned int num_pdef_keys;
9655e291a4SJuan Castillo 
97ccbfd01dSManish V Badarkhe extern cert_key_t *keys;
98b94bf967SPankaj Gupta extern unsigned int num_keys;
99c3cf06f1SAntonio Nino Diaz #endif /* KEY_H */
100