1 /* 2 * Copyright (c) 2015-2020, 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 /* Return values */ 11 enum crypto_ret_value { 12 CRYPTO_SUCCESS = 0, 13 CRYPTO_ERR_INIT, 14 CRYPTO_ERR_HASH, 15 CRYPTO_ERR_SIGNATURE, 16 CRYPTO_ERR_UNKNOWN 17 }; 18 19 /* 20 * Cryptographic library descriptor 21 */ 22 typedef struct crypto_lib_desc_s { 23 const char *name; 24 25 /* Initialize library. This function is not expected to fail. All errors 26 * must be handled inside the function, asserting or panicing in case of 27 * a non-recoverable error */ 28 void (*init)(void); 29 30 /* Verify a digital signature. Return one of the 31 * 'enum crypto_ret_value' options */ 32 int (*verify_signature)(void *data_ptr, unsigned int data_len, 33 void *sig_ptr, unsigned int sig_len, 34 void *sig_alg, unsigned int sig_alg_len, 35 void *pk_ptr, unsigned int pk_len); 36 37 /* Verify a hash. Return one of the 'enum crypto_ret_value' options */ 38 int (*verify_hash)(void *data_ptr, unsigned int data_len, 39 void *digest_info_ptr, unsigned int digest_info_len); 40 41 #if MEASURED_BOOT 42 /* Calculate a hash. Return hash value */ 43 int (*calc_hash)(unsigned int alg, void *data_ptr, 44 unsigned int data_len, unsigned char *output); 45 #endif /* MEASURED_BOOT */ 46 47 } crypto_lib_desc_t; 48 49 /* Public functions */ 50 void crypto_mod_init(void); 51 int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len, 52 void *sig_ptr, unsigned int sig_len, 53 void *sig_alg_ptr, unsigned int sig_alg_len, 54 void *pk_ptr, unsigned int pk_len); 55 int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len, 56 void *digest_info_ptr, unsigned int digest_info_len); 57 58 #if MEASURED_BOOT 59 int crypto_mod_calc_hash(unsigned int alg, void *data_ptr, 60 unsigned int data_len, unsigned char *output); 61 62 /* Macro to register a cryptographic library */ 63 #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ 64 _calc_hash) \ 65 const crypto_lib_desc_t crypto_lib_desc = { \ 66 .name = _name, \ 67 .init = _init, \ 68 .verify_signature = _verify_signature, \ 69 .verify_hash = _verify_hash, \ 70 .calc_hash = _calc_hash \ 71 } 72 #else 73 #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash) \ 74 const crypto_lib_desc_t crypto_lib_desc = { \ 75 .name = _name, \ 76 .init = _init, \ 77 .verify_signature = _verify_signature, \ 78 .verify_hash = _verify_hash \ 79 } 80 #endif /* MEASURED_BOOT */ 81 82 extern const crypto_lib_desc_t crypto_lib_desc; 83 84 #endif /* CRYPTO_MOD_H */ 85