blob: d96d9839a2a05315591a49ccc062330c7faa429a [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Justin Chadwellfebe86c2019-07-29 17:13:45 +01002 * Copyright (c) 2015-2019, 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
25 KEY_ALG_ECDSA,
26#endif /* OPENSSL_NO_EC */
27 KEY_ALG_MAX_NUM
Juan Castillof9f39c32015-06-01 16:34:23 +010028};
29
Justin Chadwellfebe86c2019-07-29 17:13:45 +010030/* Maximum number of valid key sizes per algorithm */
31#define KEY_SIZE_MAX_NUM 4
32
Qixiang Xu76a5a9b2017-11-09 13:51:58 +080033/* Supported hash algorithms */
34enum{
35 HASH_ALG_SHA256,
36 HASH_ALG_SHA384,
37 HASH_ALG_SHA512,
38};
39
Justin Chadwellfebe86c2019-07-29 17:13:45 +010040/* Supported key sizes */
41/* NOTE: the first item in each array is the default key size */
42static const unsigned int KEY_SIZES[KEY_ALG_MAX_NUM][KEY_SIZE_MAX_NUM] = {
43 { 2048, 1024, 3072, 4096 }, /* KEY_ALG_RSA */
Justin Chadwellfebe86c2019-07-29 17:13:45 +010044#ifndef OPENSSL_NO_EC
45 {} /* KEY_ALG_ECDSA */
46#endif /* OPENSSL_NO_EC */
47};
48
Juan Castillo11abdcd2014-10-21 11:30:42 +010049/*
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 */
57typedef struct key_s {
58 int id; /* Key id */
Juan Castillo1218dd52015-07-03 16:23:16 +010059 const char *opt; /* Command line option to specify a key */
Juan Castillo212f7382015-12-15 16:37:57 +000060 const char *help_msg; /* Help message */
Juan Castillo11abdcd2014-10-21 11:30:42 +010061 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
Juan Castilloe6d30e92015-06-12 11:27:59 +010066/* Exported API */
Juan Castillo1218dd52015-07-03 16:23:16 +010067int key_init(void);
68key_t *key_get_by_opt(const char *opt);
Masahiro Yamadabccb1092017-02-06 21:15:01 +090069int key_new(key_t *key);
Justin Chadwellfebe86c2019-07-29 17:13:45 +010070int key_create(key_t *key, int type, int key_bits);
Juan Castillof9f39c32015-06-01 16:34:23 +010071int key_load(key_t *key, unsigned int *err_code);
Juan Castillo11abdcd2014-10-21 11:30:42 +010072int key_store(key_t *key);
73
Juan Castilloe6d30e92015-06-12 11:27:59 +010074/* Macro to register the keys used in the CoT */
75#define REGISTER_KEYS(_keys) \
76 key_t *keys = &_keys[0]; \
Sandrine Bailleuxdf8de2d2016-01-04 15:49:23 +000077 const unsigned int num_keys = sizeof(_keys)/sizeof(_keys[0])
Juan Castilloe6d30e92015-06-12 11:27:59 +010078
79/* Exported variables */
80extern key_t *keys;
81extern const unsigned int num_keys;
82
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +000083#endif /* KEY_H */