105799ae0SJuan Castillo /* 2*14db963fSManish V Badarkhe * Copyright (c) 2015-2021, Arm Limited and Contributors. All rights reserved. 305799ae0SJuan Castillo * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 505799ae0SJuan Castillo */ 605799ae0SJuan Castillo 7c3cf06f1SAntonio Nino Diaz #ifndef CRYPTO_MOD_H 8c3cf06f1SAntonio Nino Diaz #define CRYPTO_MOD_H 905799ae0SJuan Castillo 1005799ae0SJuan Castillo /* Return values */ 1105799ae0SJuan Castillo enum crypto_ret_value { 1205799ae0SJuan Castillo CRYPTO_SUCCESS = 0, 1305799ae0SJuan Castillo CRYPTO_ERR_INIT, 1405799ae0SJuan Castillo CRYPTO_ERR_HASH, 1505799ae0SJuan Castillo CRYPTO_ERR_SIGNATURE, 167cda17bbSSumit Garg CRYPTO_ERR_DECRYPTION, 1705799ae0SJuan Castillo CRYPTO_ERR_UNKNOWN 1805799ae0SJuan Castillo }; 1905799ae0SJuan Castillo 207cda17bbSSumit Garg #define CRYPTO_MAX_IV_SIZE 16U 217cda17bbSSumit Garg #define CRYPTO_MAX_TAG_SIZE 16U 227cda17bbSSumit Garg 237cda17bbSSumit Garg /* Decryption algorithm */ 247cda17bbSSumit Garg enum crypto_dec_algo { 257cda17bbSSumit Garg CRYPTO_GCM_DECRYPT = 0 267cda17bbSSumit Garg }; 277cda17bbSSumit Garg 28*14db963fSManish V Badarkhe /* Message digest algorithm */ 29*14db963fSManish V Badarkhe enum crypto_md_algo { 30*14db963fSManish V Badarkhe CRYPTO_MD_SHA256, 31*14db963fSManish V Badarkhe CRYPTO_MD_SHA384, 32*14db963fSManish V Badarkhe CRYPTO_MD_SHA512, 33*14db963fSManish V Badarkhe }; 34*14db963fSManish V Badarkhe 35*14db963fSManish V Badarkhe /* Maximum size as per the known stronger hash algorithm i.e.SHA512 */ 36*14db963fSManish V Badarkhe #define CRYPTO_MD_MAX_SIZE 64U 37*14db963fSManish V Badarkhe 3805799ae0SJuan Castillo /* 3905799ae0SJuan Castillo * Cryptographic library descriptor 4005799ae0SJuan Castillo */ 4105799ae0SJuan Castillo typedef struct crypto_lib_desc_s { 4205799ae0SJuan Castillo const char *name; 4305799ae0SJuan Castillo 4405799ae0SJuan Castillo /* Initialize library. This function is not expected to fail. All errors 4505799ae0SJuan Castillo * must be handled inside the function, asserting or panicing in case of 4605799ae0SJuan Castillo * a non-recoverable error */ 4705799ae0SJuan Castillo void (*init)(void); 4805799ae0SJuan Castillo 4905799ae0SJuan Castillo /* Verify a digital signature. Return one of the 5005799ae0SJuan Castillo * 'enum crypto_ret_value' options */ 5105799ae0SJuan Castillo int (*verify_signature)(void *data_ptr, unsigned int data_len, 5205799ae0SJuan Castillo void *sig_ptr, unsigned int sig_len, 5305799ae0SJuan Castillo void *sig_alg, unsigned int sig_alg_len, 5405799ae0SJuan Castillo void *pk_ptr, unsigned int pk_len); 5505799ae0SJuan Castillo 5605799ae0SJuan Castillo /* Verify a hash. Return one of the 'enum crypto_ret_value' options */ 5705799ae0SJuan Castillo int (*verify_hash)(void *data_ptr, unsigned int data_len, 5805799ae0SJuan Castillo void *digest_info_ptr, unsigned int digest_info_len); 598c105290SAlexei Fedorov 608c105290SAlexei Fedorov #if MEASURED_BOOT 618c105290SAlexei Fedorov /* Calculate a hash. Return hash value */ 62*14db963fSManish V Badarkhe int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr, 63*14db963fSManish V Badarkhe unsigned int data_len, 64*14db963fSManish V Badarkhe unsigned char output[CRYPTO_MD_MAX_SIZE]); 658c105290SAlexei Fedorov #endif /* MEASURED_BOOT */ 668c105290SAlexei Fedorov 677cda17bbSSumit Garg /* 687cda17bbSSumit Garg * Authenticated decryption. Return one of the 697cda17bbSSumit Garg * 'enum crypto_ret_value' options. 707cda17bbSSumit Garg */ 717cda17bbSSumit Garg int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr, 727cda17bbSSumit Garg size_t len, const void *key, unsigned int key_len, 737cda17bbSSumit Garg unsigned int key_flags, const void *iv, 747cda17bbSSumit Garg unsigned int iv_len, const void *tag, 757cda17bbSSumit Garg unsigned int tag_len); 7605799ae0SJuan Castillo } crypto_lib_desc_t; 7705799ae0SJuan Castillo 7805799ae0SJuan Castillo /* Public functions */ 7905799ae0SJuan Castillo void crypto_mod_init(void); 8005799ae0SJuan Castillo int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len, 8105799ae0SJuan Castillo void *sig_ptr, unsigned int sig_len, 82735181b6SRoberto Vargas void *sig_alg_ptr, unsigned int sig_alg_len, 8305799ae0SJuan Castillo void *pk_ptr, unsigned int pk_len); 8405799ae0SJuan Castillo int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len, 8505799ae0SJuan Castillo void *digest_info_ptr, unsigned int digest_info_len); 867cda17bbSSumit Garg int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr, 877cda17bbSSumit Garg size_t len, const void *key, unsigned int key_len, 887cda17bbSSumit Garg unsigned int key_flags, const void *iv, 897cda17bbSSumit Garg unsigned int iv_len, const void *tag, 907cda17bbSSumit Garg unsigned int tag_len); 9105799ae0SJuan Castillo 928c105290SAlexei Fedorov #if MEASURED_BOOT 93*14db963fSManish V Badarkhe int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr, 94*14db963fSManish V Badarkhe unsigned int data_len, 95*14db963fSManish V Badarkhe unsigned char output[CRYPTO_MD_MAX_SIZE]); 968c105290SAlexei Fedorov 9705799ae0SJuan Castillo /* Macro to register a cryptographic library */ 988c105290SAlexei Fedorov #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ 997cda17bbSSumit Garg _calc_hash, _auth_decrypt) \ 1008c105290SAlexei Fedorov const crypto_lib_desc_t crypto_lib_desc = { \ 1018c105290SAlexei Fedorov .name = _name, \ 1028c105290SAlexei Fedorov .init = _init, \ 1038c105290SAlexei Fedorov .verify_signature = _verify_signature, \ 1048c105290SAlexei Fedorov .verify_hash = _verify_hash, \ 1057cda17bbSSumit Garg .calc_hash = _calc_hash, \ 1067cda17bbSSumit Garg .auth_decrypt = _auth_decrypt \ 1078c105290SAlexei Fedorov } 1088c105290SAlexei Fedorov #else 1097cda17bbSSumit Garg #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ 1107cda17bbSSumit Garg _auth_decrypt) \ 11105799ae0SJuan Castillo const crypto_lib_desc_t crypto_lib_desc = { \ 11205799ae0SJuan Castillo .name = _name, \ 11305799ae0SJuan Castillo .init = _init, \ 11405799ae0SJuan Castillo .verify_signature = _verify_signature, \ 1157cda17bbSSumit Garg .verify_hash = _verify_hash, \ 1167cda17bbSSumit Garg .auth_decrypt = _auth_decrypt \ 11705799ae0SJuan Castillo } 1188c105290SAlexei Fedorov #endif /* MEASURED_BOOT */ 11905799ae0SJuan Castillo 1203b94189aSRoberto Vargas extern const crypto_lib_desc_t crypto_lib_desc; 1213b94189aSRoberto Vargas 122c3cf06f1SAntonio Nino Diaz #endif /* CRYPTO_MOD_H */ 123