blob: f211035d707a5c9fa8c4bf78c82ac675fefbce7b [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,
16 CRYPTO_ERR_UNKNOWN
17};
18
19/*
20 * Cryptographic library descriptor
21 */
22typedef struct crypto_lib_desc_s {
23 const char *name;
24
25 /* Initialize library. This function is not expected to fail. All errors
26 * must be handled inside the function, asserting or panicing in case of
27 * a non-recoverable error */
28 void (*init)(void);
29
30 /* Verify a digital signature. Return one of the
31 * 'enum crypto_ret_value' options */
32 int (*verify_signature)(void *data_ptr, unsigned int data_len,
33 void *sig_ptr, unsigned int sig_len,
34 void *sig_alg, unsigned int sig_alg_len,
35 void *pk_ptr, unsigned int pk_len);
36
37 /* Verify a hash. Return one of the 'enum crypto_ret_value' options */
38 int (*verify_hash)(void *data_ptr, unsigned int data_len,
39 void *digest_info_ptr, unsigned int digest_info_len);
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000040
41#if MEASURED_BOOT
42 /* Calculate a hash. Return hash value */
43 int (*calc_hash)(unsigned int alg, void *data_ptr,
44 unsigned int data_len, unsigned char *output);
45#endif /* MEASURED_BOOT */
46
Juan Castillo8e55d932015-04-02 09:48:16 +010047} crypto_lib_desc_t;
48
49/* Public functions */
50void crypto_mod_init(void);
51int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
52 void *sig_ptr, unsigned int sig_len,
Roberto Vargasbe126ed2018-02-12 12:36:17 +000053 void *sig_alg_ptr, unsigned int sig_alg_len,
Juan Castillo8e55d932015-04-02 09:48:16 +010054 void *pk_ptr, unsigned int pk_len);
55int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
56 void *digest_info_ptr, unsigned int digest_info_len);
57
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000058#if MEASURED_BOOT
59int crypto_mod_calc_hash(unsigned int alg, void *data_ptr,
60 unsigned int data_len, unsigned char *output);
61
Juan Castillo8e55d932015-04-02 09:48:16 +010062/* Macro to register a cryptographic library */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000063#define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
64 _calc_hash) \
65 const crypto_lib_desc_t crypto_lib_desc = { \
66 .name = _name, \
67 .init = _init, \
68 .verify_signature = _verify_signature, \
69 .verify_hash = _verify_hash, \
70 .calc_hash = _calc_hash \
71 }
72#else
Juan Castillo8e55d932015-04-02 09:48:16 +010073#define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash) \
74 const crypto_lib_desc_t crypto_lib_desc = { \
75 .name = _name, \
76 .init = _init, \
77 .verify_signature = _verify_signature, \
78 .verify_hash = _verify_hash \
79 }
Alexei Fedorov913cb7e2020-01-23 14:27:38 +000080#endif /* MEASURED_BOOT */
Juan Castillo8e55d932015-04-02 09:48:16 +010081
Roberto Vargas52f707f2018-02-12 12:36:17 +000082extern const crypto_lib_desc_t crypto_lib_desc;
83
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +000084#endif /* CRYPTO_MOD_H */