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