xref: /rk3399_ARM-atf/tools/cert_create/include/key.h (revision 762f1ebe8d1b26e78cd4923f832a611c8a5f0a89)
16f971622SJuan Castillo /*
26f971622SJuan Castillo  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
36f971622SJuan Castillo  *
46f971622SJuan Castillo  * Redistribution and use in source and binary forms, with or without
56f971622SJuan Castillo  * modification, are permitted provided that the following conditions are met:
66f971622SJuan Castillo  *
76f971622SJuan Castillo  * Redistributions of source code must retain the above copyright notice, this
86f971622SJuan Castillo  * list of conditions and the following disclaimer.
96f971622SJuan Castillo  *
106f971622SJuan Castillo  * Redistributions in binary form must reproduce the above copyright notice,
116f971622SJuan Castillo  * this list of conditions and the following disclaimer in the documentation
126f971622SJuan Castillo  * and/or other materials provided with the distribution.
136f971622SJuan Castillo  *
146f971622SJuan Castillo  * Neither the name of ARM nor the names of its contributors may be used
156f971622SJuan Castillo  * to endorse or promote products derived from this software without specific
166f971622SJuan Castillo  * prior written permission.
176f971622SJuan Castillo  *
186f971622SJuan Castillo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
196f971622SJuan Castillo  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
206f971622SJuan Castillo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
216f971622SJuan Castillo  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
226f971622SJuan Castillo  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
236f971622SJuan Castillo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
246f971622SJuan Castillo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
256f971622SJuan Castillo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
266f971622SJuan Castillo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
276f971622SJuan Castillo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
286f971622SJuan Castillo  * POSSIBILITY OF SUCH DAMAGE.
296f971622SJuan Castillo  */
306f971622SJuan Castillo 
316f971622SJuan Castillo #ifndef KEY_H_
326f971622SJuan Castillo #define KEY_H_
336f971622SJuan Castillo 
346f971622SJuan Castillo #include <openssl/ossl_typ.h>
356f971622SJuan Castillo 
366f971622SJuan Castillo #define RSA_KEY_BITS		2048
376f971622SJuan Castillo 
38ccbf890eSJuan Castillo /* Error codes */
39ccbf890eSJuan Castillo enum {
40ccbf890eSJuan Castillo 	KEY_ERR_NONE,
41ccbf890eSJuan Castillo 	KEY_ERR_MALLOC,
42ccbf890eSJuan Castillo 	KEY_ERR_FILENAME,
43ccbf890eSJuan Castillo 	KEY_ERR_OPEN,
44ccbf890eSJuan Castillo 	KEY_ERR_LOAD
45ccbf890eSJuan Castillo };
46ccbf890eSJuan Castillo 
47ccbf890eSJuan Castillo /* Supported key algorithms */
48ccbf890eSJuan Castillo enum {
49ccbf890eSJuan Castillo 	KEY_ALG_RSA,
50ed2a76eaSJuan Castillo #ifndef OPENSSL_NO_EC
51ed2a76eaSJuan Castillo 	KEY_ALG_ECDSA,
52ed2a76eaSJuan Castillo #endif /* OPENSSL_NO_EC */
53ed2a76eaSJuan Castillo 	KEY_ALG_MAX_NUM
54ccbf890eSJuan Castillo };
55ccbf890eSJuan Castillo 
566f971622SJuan Castillo /*
576f971622SJuan Castillo  * This structure contains the relevant information to create the keys
586f971622SJuan Castillo  * required to sign the certificates.
596f971622SJuan Castillo  *
606f971622SJuan Castillo  * One instance of this structure must be created for each key, usually in an
616f971622SJuan Castillo  * array fashion. The filename is obtained at run time from the command line
626f971622SJuan Castillo  * parameters
636f971622SJuan Castillo  */
646f971622SJuan Castillo typedef struct key_s {
656f971622SJuan Castillo 	int id;			/* Key id */
66ad2c1a9aSJuan Castillo 	const char *opt;	/* Command line option to specify a key */
67159807e2SJuan Castillo 	const char *help_msg;	/* Help message */
686f971622SJuan Castillo 	const char *desc;	/* Key description (debug purposes) */
696f971622SJuan Castillo 	char *fn;		/* Filename to load/store the key */
706f971622SJuan Castillo 	EVP_PKEY *key;		/* Key container */
716f971622SJuan Castillo } key_t;
726f971622SJuan Castillo 
7355e291a4SJuan Castillo /* Exported API */
74ad2c1a9aSJuan Castillo int key_init(void);
75ad2c1a9aSJuan Castillo key_t *key_get_by_opt(const char *opt);
76*762f1ebeSMasahiro Yamada int key_new(key_t *key);
77ccbf890eSJuan Castillo int key_create(key_t *key, int type);
78ccbf890eSJuan Castillo int key_load(key_t *key, unsigned int *err_code);
796f971622SJuan Castillo int key_store(key_t *key);
806f971622SJuan Castillo 
8155e291a4SJuan Castillo /* Macro to register the keys used in the CoT */
8255e291a4SJuan Castillo #define REGISTER_KEYS(_keys) \
8355e291a4SJuan Castillo 	key_t *keys = &_keys[0]; \
84aa856917SSandrine Bailleux 	const unsigned int num_keys = sizeof(_keys)/sizeof(_keys[0])
8555e291a4SJuan Castillo 
8655e291a4SJuan Castillo /* Exported variables */
8755e291a4SJuan Castillo extern key_t *keys;
8855e291a4SJuan Castillo extern const unsigned int num_keys;
8955e291a4SJuan Castillo 
906f971622SJuan Castillo #endif /* KEY_H_ */
91