Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 1 | /* |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved. |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 7 | #ifndef CRYPTO_MOD_H |
| 8 | #define CRYPTO_MOD_H |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 9 | |
| 10 | /* Return values */ |
| 11 | enum crypto_ret_value { |
| 12 | CRYPTO_SUCCESS = 0, |
| 13 | CRYPTO_ERR_INIT, |
| 14 | CRYPTO_ERR_HASH, |
| 15 | CRYPTO_ERR_SIGNATURE, |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 16 | CRYPTO_ERR_DECRYPTION, |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 17 | CRYPTO_ERR_UNKNOWN |
| 18 | }; |
| 19 | |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 20 | #define CRYPTO_MAX_IV_SIZE 16U |
| 21 | #define CRYPTO_MAX_TAG_SIZE 16U |
| 22 | |
| 23 | /* Decryption algorithm */ |
| 24 | enum crypto_dec_algo { |
| 25 | CRYPTO_GCM_DECRYPT = 0 |
| 26 | }; |
| 27 | |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 28 | /* Message digest algorithm */ |
| 29 | enum crypto_md_algo { |
| 30 | CRYPTO_MD_SHA256, |
| 31 | CRYPTO_MD_SHA384, |
| 32 | CRYPTO_MD_SHA512, |
| 33 | }; |
| 34 | |
| 35 | /* Maximum size as per the known stronger hash algorithm i.e.SHA512 */ |
| 36 | #define CRYPTO_MD_MAX_SIZE 64U |
| 37 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 38 | /* |
| 39 | * Cryptographic library descriptor |
| 40 | */ |
| 41 | typedef struct crypto_lib_desc_s { |
| 42 | const char *name; |
| 43 | |
| 44 | /* Initialize library. This function is not expected to fail. All errors |
| 45 | * must be handled inside the function, asserting or panicing in case of |
| 46 | * a non-recoverable error */ |
| 47 | void (*init)(void); |
| 48 | |
| 49 | /* Verify a digital signature. Return one of the |
| 50 | * 'enum crypto_ret_value' options */ |
| 51 | int (*verify_signature)(void *data_ptr, unsigned int data_len, |
| 52 | void *sig_ptr, unsigned int sig_len, |
| 53 | void *sig_alg, unsigned int sig_alg_len, |
| 54 | void *pk_ptr, unsigned int pk_len); |
| 55 | |
| 56 | /* Verify a hash. Return one of the 'enum crypto_ret_value' options */ |
| 57 | int (*verify_hash)(void *data_ptr, unsigned int data_len, |
| 58 | void *digest_info_ptr, unsigned int digest_info_len); |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 59 | |
| 60 | #if MEASURED_BOOT |
| 61 | /* Calculate a hash. Return hash value */ |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 62 | int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr, |
| 63 | unsigned int data_len, |
| 64 | unsigned char output[CRYPTO_MD_MAX_SIZE]); |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 65 | #endif /* MEASURED_BOOT */ |
| 66 | |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 67 | /* |
| 68 | * Authenticated decryption. Return one of the |
| 69 | * 'enum crypto_ret_value' options. |
| 70 | */ |
| 71 | int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr, |
| 72 | size_t len, const void *key, unsigned int key_len, |
| 73 | unsigned int key_flags, const void *iv, |
| 74 | unsigned int iv_len, const void *tag, |
| 75 | unsigned int tag_len); |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 76 | } crypto_lib_desc_t; |
| 77 | |
| 78 | /* Public functions */ |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 79 | #if CRYPTO_SUPPORT |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 80 | void crypto_mod_init(void); |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 81 | #else |
| 82 | static inline void crypto_mod_init(void) |
| 83 | { |
| 84 | } |
| 85 | #endif /* CRYPTO_SUPPORT */ |
| 86 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 87 | int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len, |
| 88 | void *sig_ptr, unsigned int sig_len, |
Roberto Vargas | be126ed | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 89 | void *sig_alg_ptr, unsigned int sig_alg_len, |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 90 | void *pk_ptr, unsigned int pk_len); |
| 91 | int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len, |
| 92 | void *digest_info_ptr, unsigned int digest_info_len); |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 93 | int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr, |
| 94 | size_t len, const void *key, unsigned int key_len, |
| 95 | unsigned int key_flags, const void *iv, |
| 96 | unsigned int iv_len, const void *tag, |
| 97 | unsigned int tag_len); |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 98 | |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 99 | #if MEASURED_BOOT |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 100 | int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr, |
| 101 | unsigned int data_len, |
| 102 | unsigned char output[CRYPTO_MD_MAX_SIZE]); |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 103 | #endif /* MEASURED_BOOT */ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 104 | |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 105 | #if MEASURED_BOOT && TRUSTED_BOARD_BOOT |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 106 | /* Macro to register a cryptographic library */ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 107 | #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 108 | _calc_hash, _auth_decrypt) \ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 109 | const crypto_lib_desc_t crypto_lib_desc = { \ |
| 110 | .name = _name, \ |
| 111 | .init = _init, \ |
| 112 | .verify_signature = _verify_signature, \ |
| 113 | .verify_hash = _verify_hash, \ |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 114 | .calc_hash = _calc_hash, \ |
| 115 | .auth_decrypt = _auth_decrypt \ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 116 | } |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 117 | #elif TRUSTED_BOARD_BOOT |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 118 | #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ |
| 119 | _auth_decrypt) \ |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 120 | const crypto_lib_desc_t crypto_lib_desc = { \ |
| 121 | .name = _name, \ |
| 122 | .init = _init, \ |
| 123 | .verify_signature = _verify_signature, \ |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 124 | .verify_hash = _verify_hash, \ |
| 125 | .auth_decrypt = _auth_decrypt \ |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 126 | } |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 127 | #elif MEASURED_BOOT |
| 128 | #define REGISTER_CRYPTO_LIB(_name, _init, _calc_hash) \ |
| 129 | const crypto_lib_desc_t crypto_lib_desc = { \ |
| 130 | .name = _name, \ |
| 131 | .init = _init, \ |
| 132 | .calc_hash = _calc_hash, \ |
| 133 | } |
| 134 | #endif /* MEASURED_BOOT && TRUSTED_BOARD_BOOT */ |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 135 | |
Roberto Vargas | 52f707f | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 136 | extern const crypto_lib_desc_t crypto_lib_desc; |
| 137 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 138 | #endif /* CRYPTO_MOD_H */ |