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_DECRYPTION,
17 	CRYPTO_ERR_UNKNOWN
18 };
19 
20 #define CRYPTO_MAX_IV_SIZE		16U
21 #define CRYPTO_MAX_TAG_SIZE		16U
22 
23 /* Decryption algorithm */
24 enum crypto_dec_algo {
25 	CRYPTO_GCM_DECRYPT = 0
26 };
27 
28 /*
29  * Cryptographic library descriptor
30  */
31 typedef struct crypto_lib_desc_s {
32 	const char *name;
33 
34 	/* Initialize library. This function is not expected to fail. All errors
35 	 * must be handled inside the function, asserting or panicing in case of
36 	 * a non-recoverable error */
37 	void (*init)(void);
38 
39 	/* Verify a digital signature. Return one of the
40 	 * 'enum crypto_ret_value' options */
41 	int (*verify_signature)(void *data_ptr, unsigned int data_len,
42 				void *sig_ptr, unsigned int sig_len,
43 				void *sig_alg, unsigned int sig_alg_len,
44 				void *pk_ptr, unsigned int pk_len);
45 
46 	/* Verify a hash. Return one of the 'enum crypto_ret_value' options */
47 	int (*verify_hash)(void *data_ptr, unsigned int data_len,
48 			   void *digest_info_ptr, unsigned int digest_info_len);
49 
50 #if MEASURED_BOOT
51 	/* Calculate a hash. Return hash value */
52 	int (*calc_hash)(unsigned int alg, void *data_ptr,
53 			 unsigned int data_len, unsigned char *output);
54 #endif /* MEASURED_BOOT */
55 
56 	/*
57 	 * Authenticated decryption. Return one of the
58 	 * 'enum crypto_ret_value' options.
59 	 */
60 	int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
61 			    size_t len, const void *key, unsigned int key_len,
62 			    unsigned int key_flags, const void *iv,
63 			    unsigned int iv_len, const void *tag,
64 			    unsigned int tag_len);
65 } crypto_lib_desc_t;
66 
67 /* Public functions */
68 void crypto_mod_init(void);
69 int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
70 				void *sig_ptr, unsigned int sig_len,
71 				void *sig_alg_ptr, unsigned int sig_alg_len,
72 				void *pk_ptr, unsigned int pk_len);
73 int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
74 			   void *digest_info_ptr, unsigned int digest_info_len);
75 int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
76 			    size_t len, const void *key, unsigned int key_len,
77 			    unsigned int key_flags, const void *iv,
78 			    unsigned int iv_len, const void *tag,
79 			    unsigned int tag_len);
80 
81 #if MEASURED_BOOT
82 int crypto_mod_calc_hash(unsigned int alg, void *data_ptr,
83 			 unsigned int data_len, unsigned char *output);
84 
85 /* Macro to register a cryptographic library */
86 #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
87 			    _calc_hash, _auth_decrypt) \
88 	const crypto_lib_desc_t crypto_lib_desc = { \
89 		.name = _name, \
90 		.init = _init, \
91 		.verify_signature = _verify_signature, \
92 		.verify_hash = _verify_hash, \
93 		.calc_hash = _calc_hash, \
94 		.auth_decrypt = _auth_decrypt \
95 	}
96 #else
97 #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
98 			    _auth_decrypt) \
99 	const crypto_lib_desc_t crypto_lib_desc = { \
100 		.name = _name, \
101 		.init = _init, \
102 		.verify_signature = _verify_signature, \
103 		.verify_hash = _verify_hash, \
104 		.auth_decrypt = _auth_decrypt \
105 	}
106 #endif	/* MEASURED_BOOT */
107 
108 extern const crypto_lib_desc_t crypto_lib_desc;
109 
110 #endif /* CRYPTO_MOD_H */
111