1 /* 2 * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef CRYPTO_MOD_H 8 #define CRYPTO_MOD_H 9 10 #define CRYPTO_AUTH_VERIFY_ONLY 1 11 #define CRYPTO_HASH_CALC_ONLY 2 12 #define CRYPTO_AUTH_VERIFY_AND_HASH_CALC 3 13 14 #include <stddef.h> 15 16 /* Return values */ 17 enum crypto_ret_value { 18 CRYPTO_SUCCESS = 0, 19 CRYPTO_ERR_INIT, 20 CRYPTO_ERR_HASH, 21 CRYPTO_ERR_SIGNATURE, 22 CRYPTO_ERR_DECRYPTION, 23 CRYPTO_ERR_UNKNOWN 24 }; 25 26 #define CRYPTO_MAX_IV_SIZE 16U 27 #define CRYPTO_MAX_TAG_SIZE 16U 28 29 /* Decryption algorithm */ 30 enum crypto_dec_algo { 31 CRYPTO_GCM_DECRYPT = 0 32 }; 33 34 /* Message digest algorithm */ 35 enum crypto_md_algo { 36 CRYPTO_MD_SHA256, 37 CRYPTO_MD_SHA384, 38 CRYPTO_MD_SHA512, 39 }; 40 41 /* Maximum size as per the known stronger hash algorithm i.e.SHA512 */ 42 #define CRYPTO_MD_MAX_SIZE 64U 43 44 /* 45 * Cryptographic library descriptor 46 */ 47 typedef struct crypto_lib_desc_s { 48 const char *name; 49 50 /* Initialize library. This function is not expected to fail. All errors 51 * must be handled inside the function, asserting or panicking in case of 52 * a non-recoverable error */ 53 void (*init)(void); 54 55 /* Verify a digital signature. Return one of the 56 * 'enum crypto_ret_value' options */ 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); 65 66 /* Calculate a hash. Return hash value */ 67 int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr, 68 unsigned int data_len, 69 unsigned char output[CRYPTO_MD_MAX_SIZE]); 70 71 /* Convert Public key (optional) */ 72 int (*convert_pk)(void *full_pk_ptr, unsigned int full_pk_len, 73 void **hashed_pk_ptr, unsigned int *hashed_pk_len); 74 75 /* 76 * Authenticated decryption. Return one of the 77 * 'enum crypto_ret_value' options. 78 */ 79 int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr, 80 size_t len, const void *key, unsigned int key_len, 81 unsigned int key_flags, const void *iv, 82 unsigned int iv_len, const void *tag, 83 unsigned int tag_len); 84 85 /* 86 * Finish using the crypto library, 87 * anything to be done to wrap up crypto usage done here. 88 */ 89 void (*finish)(void); 90 } crypto_lib_desc_t; 91 92 /* Public functions */ 93 #if CRYPTO_SUPPORT 94 void crypto_mod_init(void); 95 #else 96 static inline void crypto_mod_init(void) 97 { 98 } 99 #endif /* CRYPTO_SUPPORT */ 100 101 #if (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \ 102 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) 103 int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len, 104 void *sig_ptr, unsigned int sig_len, 105 void *sig_alg_ptr, unsigned int sig_alg_len, 106 void *pk_ptr, unsigned int pk_len); 107 int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len, 108 void *digest_info_ptr, unsigned int digest_info_len); 109 #endif /* (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \ 110 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */ 111 112 int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr, 113 size_t len, const void *key, unsigned int key_len, 114 unsigned int key_flags, const void *iv, 115 unsigned int iv_len, const void *tag, 116 unsigned int tag_len); 117 118 #if (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \ 119 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) 120 int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr, 121 unsigned int data_len, 122 unsigned char output[CRYPTO_MD_MAX_SIZE]); 123 #endif /* (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \ 124 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */ 125 126 int crypto_mod_convert_pk(void *full_pk_ptr, unsigned int full_pk_len, 127 void **hashed_pk_ptr, unsigned int *hashed_pk_len); 128 129 #if CRYPTO_SUPPORT 130 void crypto_mod_finish(void); 131 #else 132 static inline void crypto_mod_finish(void) 133 { 134 } 135 #endif /* CRYPTO_SUPPORT */ 136 137 /* Macro to register a cryptographic library */ 138 #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ 139 _calc_hash, _auth_decrypt, _convert_pk, _finish) \ 140 const crypto_lib_desc_t crypto_lib_desc = { \ 141 .name = _name, \ 142 .init = _init, \ 143 .verify_signature = _verify_signature, \ 144 .verify_hash = _verify_hash, \ 145 .calc_hash = _calc_hash, \ 146 .auth_decrypt = _auth_decrypt, \ 147 .convert_pk = _convert_pk, \ 148 .finish = _finish \ 149 } 150 151 extern const crypto_lib_desc_t crypto_lib_desc; 152 153 #endif /* CRYPTO_MOD_H */ 154