blob: 71cf67306da51c5ae574a9501f4284f098f35cc6 [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Alexei Fedorov913cb7e2020-01-23 14:27:38 +00002 * Copyright (c) 2015-2020, 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
10/* Return values */
11enum crypto_ret_value {
12 CRYPTO_SUCCESS = 0,
13 CRYPTO_ERR_INIT,
14 CRYPTO_ERR_HASH,
15 CRYPTO_ERR_SIGNATURE,
Sumit Garg392e4df2019-11-15 10:43:00 +053016 CRYPTO_ERR_DECRYPTION,
Juan Castillo8e55d932015-04-02 09:48:16 +010017 CRYPTO_ERR_UNKNOWN
18};
19
Sumit Garg392e4df2019-11-15 10:43:00 +053020#define CRYPTO_MAX_IV_SIZE 16U
21#define CRYPTO_MAX_TAG_SIZE 16U
22
23/* Decryption algorithm */
24enum crypto_dec_algo {
25 CRYPTO_GCM_DECRYPT = 0
26};
27
Juan Castillo8e55d932015-04-02 09:48:16 +010028/*
29 * Cryptographic library descriptor
30 */
31typedef 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);
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000049
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
Sumit Garg392e4df2019-11-15 10:43:00 +053056 /*
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);
Juan Castillo8e55d932015-04-02 09:48:16 +010065} crypto_lib_desc_t;
66
67/* Public functions */
68void crypto_mod_init(void);
69int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
70 void *sig_ptr, unsigned int sig_len,
Roberto Vargasbe126ed2018-02-12 12:36:17 +000071 void *sig_alg_ptr, unsigned int sig_alg_len,
Juan Castillo8e55d932015-04-02 09:48:16 +010072 void *pk_ptr, unsigned int pk_len);
73int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
74 void *digest_info_ptr, unsigned int digest_info_len);
Sumit Garg392e4df2019-11-15 10:43:00 +053075int 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);
Juan Castillo8e55d932015-04-02 09:48:16 +010080
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000081#if MEASURED_BOOT
82int crypto_mod_calc_hash(unsigned int alg, void *data_ptr,
83 unsigned int data_len, unsigned char *output);
84
Juan Castillo8e55d932015-04-02 09:48:16 +010085/* Macro to register a cryptographic library */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000086#define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
Sumit Garg392e4df2019-11-15 10:43:00 +053087 _calc_hash, _auth_decrypt) \
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000088 const crypto_lib_desc_t crypto_lib_desc = { \
89 .name = _name, \
90 .init = _init, \
91 .verify_signature = _verify_signature, \
92 .verify_hash = _verify_hash, \
Sumit Garg392e4df2019-11-15 10:43:00 +053093 .calc_hash = _calc_hash, \
94 .auth_decrypt = _auth_decrypt \
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000095 }
96#else
Sumit Garg392e4df2019-11-15 10:43:00 +053097#define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
98 _auth_decrypt) \
Juan Castillo8e55d932015-04-02 09:48:16 +010099 const crypto_lib_desc_t crypto_lib_desc = { \
100 .name = _name, \
101 .init = _init, \
102 .verify_signature = _verify_signature, \
Sumit Garg392e4df2019-11-15 10:43:00 +0530103 .verify_hash = _verify_hash, \
104 .auth_decrypt = _auth_decrypt \
Juan Castillo8e55d932015-04-02 09:48:16 +0100105 }
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000106#endif /* MEASURED_BOOT */
Juan Castillo8e55d932015-04-02 09:48:16 +0100107
Roberto Vargas52f707f2018-02-12 12:36:17 +0000108extern const crypto_lib_desc_t crypto_lib_desc;
109
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000110#endif /* CRYPTO_MOD_H */