blob: fd49b2d37d1c139c85eb845a2002bbbbda884e03 [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Lauren Wehrmeistere3f02cb2025-04-28 15:44:01 -05002 * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
Juan Castillo8e55d932015-04-02 09:48:16 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo8e55d932015-04-02 09:48:16 +01005 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef CRYPTO_MOD_H
8#define CRYPTO_MOD_H
Juan Castillo8e55d932015-04-02 09:48:16 +01009
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010010#define CRYPTO_AUTH_VERIFY_ONLY 1
11#define CRYPTO_HASH_CALC_ONLY 2
12#define CRYPTO_AUTH_VERIFY_AND_HASH_CALC 3
13
Juan Castillo8e55d932015-04-02 09:48:16 +010014/* Return values */
15enum crypto_ret_value {
16 CRYPTO_SUCCESS = 0,
17 CRYPTO_ERR_INIT,
18 CRYPTO_ERR_HASH,
19 CRYPTO_ERR_SIGNATURE,
Sumit Garg392e4df2019-11-15 10:43:00 +053020 CRYPTO_ERR_DECRYPTION,
Juan Castillo8e55d932015-04-02 09:48:16 +010021 CRYPTO_ERR_UNKNOWN
22};
23
Sumit Garg392e4df2019-11-15 10:43:00 +053024#define CRYPTO_MAX_IV_SIZE 16U
25#define CRYPTO_MAX_TAG_SIZE 16U
26
27/* Decryption algorithm */
28enum crypto_dec_algo {
29 CRYPTO_GCM_DECRYPT = 0
30};
31
Manish V Badarkhee112a5a2021-10-06 23:41:50 +010032/* Message digest algorithm */
33enum 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 Castillo8e55d932015-04-02 09:48:16 +010042/*
43 * Cryptographic library descriptor
44 */
45typedef struct crypto_lib_desc_s {
46 const char *name;
47
48 /* Initialize library. This function is not expected to fail. All errors
Elyes Haouas2be03c02023-02-13 09:14:48 +010049 * must be handled inside the function, asserting or panicking in case of
Juan Castillo8e55d932015-04-02 09:48:16 +010050 * a non-recoverable error */
51 void (*init)(void);
52
53 /* Verify a digital signature. Return one of the
54 * 'enum crypto_ret_value' options */
55 int (*verify_signature)(void *data_ptr, unsigned int data_len,
56 void *sig_ptr, unsigned int sig_len,
57 void *sig_alg, unsigned int sig_alg_len,
58 void *pk_ptr, unsigned int pk_len);
59
60 /* Verify a hash. Return one of the 'enum crypto_ret_value' options */
61 int (*verify_hash)(void *data_ptr, unsigned int data_len,
62 void *digest_info_ptr, unsigned int digest_info_len);
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000063
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000064 /* Calculate a hash. Return hash value */
Manish V Badarkhee112a5a2021-10-06 23:41:50 +010065 int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr,
66 unsigned int data_len,
67 unsigned char output[CRYPTO_MD_MAX_SIZE]);
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000068
Yann Gautierc68b8af2023-01-24 09:39:47 +010069 /* Convert Public key (optional) */
70 int (*convert_pk)(void *full_pk_ptr, unsigned int full_pk_len,
71 void **hashed_pk_ptr, unsigned int *hashed_pk_len);
72
Sumit Garg392e4df2019-11-15 10:43:00 +053073 /*
74 * Authenticated decryption. Return one of the
75 * 'enum crypto_ret_value' options.
76 */
77 int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
78 size_t len, const void *key, unsigned int key_len,
79 unsigned int key_flags, const void *iv,
80 unsigned int iv_len, const void *tag,
81 unsigned int tag_len);
Lauren Wehrmeistere3f02cb2025-04-28 15:44:01 -050082
83 /*
84 * Finish using the crypto library,
85 * anything to be done to wrap up crypto usage done here.
86 */
87 void (*finish)(void);
Juan Castillo8e55d932015-04-02 09:48:16 +010088} crypto_lib_desc_t;
89
90/* Public functions */
Manish V Badarkhe92de80a2021-12-16 10:41:47 +000091#if CRYPTO_SUPPORT
Juan Castillo8e55d932015-04-02 09:48:16 +010092void crypto_mod_init(void);
Manish V Badarkhe92de80a2021-12-16 10:41:47 +000093#else
94static inline void crypto_mod_init(void)
95{
96}
97#endif /* CRYPTO_SUPPORT */
98
Yann Gautier2b6673d2023-03-15 11:31:25 +010099#if (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \
100 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC)
Juan Castillo8e55d932015-04-02 09:48:16 +0100101int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
102 void *sig_ptr, unsigned int sig_len,
Roberto Vargasbe126ed2018-02-12 12:36:17 +0000103 void *sig_alg_ptr, unsigned int sig_alg_len,
Juan Castillo8e55d932015-04-02 09:48:16 +0100104 void *pk_ptr, unsigned int pk_len);
105int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
106 void *digest_info_ptr, unsigned int digest_info_len);
Yann Gautier2b6673d2023-03-15 11:31:25 +0100107#endif /* (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \
108 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100109
Sumit Garg392e4df2019-11-15 10:43:00 +0530110int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
111 size_t len, const void *key, unsigned int key_len,
112 unsigned int key_flags, const void *iv,
113 unsigned int iv_len, const void *tag,
114 unsigned int tag_len);
Juan Castillo8e55d932015-04-02 09:48:16 +0100115
Yann Gautier2b6673d2023-03-15 11:31:25 +0100116#if (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \
117 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC)
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100118int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
119 unsigned int data_len,
120 unsigned char output[CRYPTO_MD_MAX_SIZE]);
Yann Gautier2b6673d2023-03-15 11:31:25 +0100121#endif /* (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \
122 (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000123
Yann Gautierc68b8af2023-01-24 09:39:47 +0100124int crypto_mod_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
125 void **hashed_pk_ptr, unsigned int *hashed_pk_len);
126
Lauren Wehrmeistere3f02cb2025-04-28 15:44:01 -0500127#if CRYPTO_SUPPORT
128void crypto_mod_finish(void);
129#else
130static inline void crypto_mod_finish(void)
131{
132}
133#endif /* CRYPTO_SUPPORT */
134
Juan Castillo8e55d932015-04-02 09:48:16 +0100135/* Macro to register a cryptographic library */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000136#define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
Lauren Wehrmeistere3f02cb2025-04-28 15:44:01 -0500137 _calc_hash, _auth_decrypt, _convert_pk, _finish) \
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000138 const crypto_lib_desc_t crypto_lib_desc = { \
139 .name = _name, \
140 .init = _init, \
141 .verify_signature = _verify_signature, \
142 .verify_hash = _verify_hash, \
Sumit Garg392e4df2019-11-15 10:43:00 +0530143 .calc_hash = _calc_hash, \
Yann Gautierc68b8af2023-01-24 09:39:47 +0100144 .auth_decrypt = _auth_decrypt, \
Lauren Wehrmeistere3f02cb2025-04-28 15:44:01 -0500145 .convert_pk = _convert_pk, \
146 .finish = _finish \
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000147 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100148
Roberto Vargas52f707f2018-02-12 12:36:17 +0000149extern const crypto_lib_desc_t crypto_lib_desc;
150
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000151#endif /* CRYPTO_MOD_H */