105799ae0SJuan Castillo /*
20331bd22SLauren Wehrmeister * Copyright (c) 2015-2025, 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
102bf4f27fSManish V Badarkhe #define CRYPTO_AUTH_VERIFY_ONLY 1
112bf4f27fSManish V Badarkhe #define CRYPTO_HASH_CALC_ONLY 2
122bf4f27fSManish V Badarkhe #define CRYPTO_AUTH_VERIFY_AND_HASH_CALC 3
132bf4f27fSManish V Badarkhe
14*b67e9846SHarrison Mutai #include <stddef.h>
15*b67e9846SHarrison Mutai
1605799ae0SJuan Castillo /* Return values */
1705799ae0SJuan Castillo enum crypto_ret_value {
1805799ae0SJuan Castillo CRYPTO_SUCCESS = 0,
1905799ae0SJuan Castillo CRYPTO_ERR_INIT,
2005799ae0SJuan Castillo CRYPTO_ERR_HASH,
2105799ae0SJuan Castillo CRYPTO_ERR_SIGNATURE,
227cda17bbSSumit Garg CRYPTO_ERR_DECRYPTION,
2305799ae0SJuan Castillo CRYPTO_ERR_UNKNOWN
2405799ae0SJuan Castillo };
2505799ae0SJuan Castillo
267cda17bbSSumit Garg #define CRYPTO_MAX_IV_SIZE 16U
277cda17bbSSumit Garg #define CRYPTO_MAX_TAG_SIZE 16U
287cda17bbSSumit Garg
297cda17bbSSumit Garg /* Decryption algorithm */
307cda17bbSSumit Garg enum crypto_dec_algo {
317cda17bbSSumit Garg CRYPTO_GCM_DECRYPT = 0
327cda17bbSSumit Garg };
337cda17bbSSumit Garg
3414db963fSManish V Badarkhe /* Message digest algorithm */
3514db963fSManish V Badarkhe enum crypto_md_algo {
3614db963fSManish V Badarkhe CRYPTO_MD_SHA256,
3714db963fSManish V Badarkhe CRYPTO_MD_SHA384,
3814db963fSManish V Badarkhe CRYPTO_MD_SHA512,
3914db963fSManish V Badarkhe };
4014db963fSManish V Badarkhe
4114db963fSManish V Badarkhe /* Maximum size as per the known stronger hash algorithm i.e.SHA512 */
4214db963fSManish V Badarkhe #define CRYPTO_MD_MAX_SIZE 64U
4314db963fSManish V Badarkhe
4405799ae0SJuan Castillo /*
4505799ae0SJuan Castillo * Cryptographic library descriptor
4605799ae0SJuan Castillo */
4705799ae0SJuan Castillo typedef struct crypto_lib_desc_s {
4805799ae0SJuan Castillo const char *name;
4905799ae0SJuan Castillo
5005799ae0SJuan Castillo /* Initialize library. This function is not expected to fail. All errors
511b491eeaSElyes Haouas * must be handled inside the function, asserting or panicking in case of
5205799ae0SJuan Castillo * a non-recoverable error */
5305799ae0SJuan Castillo void (*init)(void);
5405799ae0SJuan Castillo
5505799ae0SJuan Castillo /* Verify a digital signature. Return one of the
5605799ae0SJuan Castillo * 'enum crypto_ret_value' options */
5705799ae0SJuan Castillo int (*verify_signature)(void *data_ptr, unsigned int data_len,
5805799ae0SJuan Castillo void *sig_ptr, unsigned int sig_len,
5905799ae0SJuan Castillo void *sig_alg, unsigned int sig_alg_len,
6005799ae0SJuan Castillo void *pk_ptr, unsigned int pk_len);
6105799ae0SJuan Castillo
6205799ae0SJuan Castillo /* Verify a hash. Return one of the 'enum crypto_ret_value' options */
6305799ae0SJuan Castillo int (*verify_hash)(void *data_ptr, unsigned int data_len,
6405799ae0SJuan Castillo void *digest_info_ptr, unsigned int digest_info_len);
658c105290SAlexei Fedorov
668c105290SAlexei Fedorov /* Calculate a hash. Return hash value */
6714db963fSManish V Badarkhe int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr,
6814db963fSManish V Badarkhe unsigned int data_len,
6914db963fSManish V Badarkhe unsigned char output[CRYPTO_MD_MAX_SIZE]);
708c105290SAlexei Fedorov
714ac5b394SYann Gautier /* Convert Public key (optional) */
724ac5b394SYann Gautier int (*convert_pk)(void *full_pk_ptr, unsigned int full_pk_len,
734ac5b394SYann Gautier void **hashed_pk_ptr, unsigned int *hashed_pk_len);
744ac5b394SYann Gautier
757cda17bbSSumit Garg /*
767cda17bbSSumit Garg * Authenticated decryption. Return one of the
777cda17bbSSumit Garg * 'enum crypto_ret_value' options.
787cda17bbSSumit Garg */
797cda17bbSSumit Garg int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
807cda17bbSSumit Garg size_t len, const void *key, unsigned int key_len,
817cda17bbSSumit Garg unsigned int key_flags, const void *iv,
827cda17bbSSumit Garg unsigned int iv_len, const void *tag,
837cda17bbSSumit Garg unsigned int tag_len);
840331bd22SLauren Wehrmeister
850331bd22SLauren Wehrmeister /*
860331bd22SLauren Wehrmeister * Finish using the crypto library,
870331bd22SLauren Wehrmeister * anything to be done to wrap up crypto usage done here.
880331bd22SLauren Wehrmeister */
890331bd22SLauren Wehrmeister void (*finish)(void);
9005799ae0SJuan Castillo } crypto_lib_desc_t;
9105799ae0SJuan Castillo
9205799ae0SJuan Castillo /* Public functions */
930aa0b3afSManish V Badarkhe #if CRYPTO_SUPPORT
9405799ae0SJuan Castillo void crypto_mod_init(void);
950aa0b3afSManish V Badarkhe #else
crypto_mod_init(void)960aa0b3afSManish V Badarkhe static inline void crypto_mod_init(void)
970aa0b3afSManish V Badarkhe {
980aa0b3afSManish V Badarkhe }
990aa0b3afSManish V Badarkhe #endif /* CRYPTO_SUPPORT */
1000aa0b3afSManish V Badarkhe
101dee99f10SYann Gautier #if (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \
102dee99f10SYann Gautier (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC)
10305799ae0SJuan Castillo int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
10405799ae0SJuan Castillo void *sig_ptr, unsigned int sig_len,
105735181b6SRoberto Vargas void *sig_alg_ptr, unsigned int sig_alg_len,
10605799ae0SJuan Castillo void *pk_ptr, unsigned int pk_len);
10705799ae0SJuan Castillo int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
10805799ae0SJuan Castillo void *digest_info_ptr, unsigned int digest_info_len);
109dee99f10SYann Gautier #endif /* (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \
110dee99f10SYann Gautier (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */
1112bf4f27fSManish V Badarkhe
1127cda17bbSSumit Garg int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
1137cda17bbSSumit Garg size_t len, const void *key, unsigned int key_len,
1147cda17bbSSumit Garg unsigned int key_flags, const void *iv,
1157cda17bbSSumit Garg unsigned int iv_len, const void *tag,
1167cda17bbSSumit Garg unsigned int tag_len);
11705799ae0SJuan Castillo
118dee99f10SYann Gautier #if (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \
119dee99f10SYann Gautier (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC)
12014db963fSManish V Badarkhe int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
12114db963fSManish V Badarkhe unsigned int data_len,
12214db963fSManish V Badarkhe unsigned char output[CRYPTO_MD_MAX_SIZE]);
123dee99f10SYann Gautier #endif /* (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \
124dee99f10SYann Gautier (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */
1258c105290SAlexei Fedorov
1264ac5b394SYann Gautier int crypto_mod_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
1274ac5b394SYann Gautier void **hashed_pk_ptr, unsigned int *hashed_pk_len);
1284ac5b394SYann Gautier
1290331bd22SLauren Wehrmeister #if CRYPTO_SUPPORT
1300331bd22SLauren Wehrmeister void crypto_mod_finish(void);
1310331bd22SLauren Wehrmeister #else
crypto_mod_finish(void)1320331bd22SLauren Wehrmeister static inline void crypto_mod_finish(void)
1330331bd22SLauren Wehrmeister {
1340331bd22SLauren Wehrmeister }
1350331bd22SLauren Wehrmeister #endif /* CRYPTO_SUPPORT */
1360331bd22SLauren Wehrmeister
13705799ae0SJuan Castillo /* Macro to register a cryptographic library */
1388c105290SAlexei Fedorov #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
1390331bd22SLauren Wehrmeister _calc_hash, _auth_decrypt, _convert_pk, _finish) \
1408c105290SAlexei Fedorov const crypto_lib_desc_t crypto_lib_desc = { \
1418c105290SAlexei Fedorov .name = _name, \
1428c105290SAlexei Fedorov .init = _init, \
1438c105290SAlexei Fedorov .verify_signature = _verify_signature, \
1448c105290SAlexei Fedorov .verify_hash = _verify_hash, \
1457cda17bbSSumit Garg .calc_hash = _calc_hash, \
1464ac5b394SYann Gautier .auth_decrypt = _auth_decrypt, \
1470331bd22SLauren Wehrmeister .convert_pk = _convert_pk, \
1480331bd22SLauren Wehrmeister .finish = _finish \
1498c105290SAlexei Fedorov }
15005799ae0SJuan Castillo
1513b94189aSRoberto Vargas extern const crypto_lib_desc_t crypto_lib_desc;
1523b94189aSRoberto Vargas
153c3cf06f1SAntonio Nino Diaz #endif /* CRYPTO_MOD_H */
154