Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 1 | /* |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 7 | #ifndef CRYPTO_MOD_H |
| 8 | #define CRYPTO_MOD_H |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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, |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 16 | CRYPTO_ERR_DECRYPTION, |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 17 | CRYPTO_ERR_UNKNOWN |
| 18 | }; |
| 19 | |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 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 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 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 | |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 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); |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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, |
Roberto Vargas | be126ed | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 71 | void *sig_alg_ptr, unsigned int sig_alg_len, |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 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); |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 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); |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 80 | |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 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 | |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 85 | /* Macro to register a cryptographic library */ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 86 | #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 87 | _calc_hash, _auth_decrypt) \ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 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, \ |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 93 | .calc_hash = _calc_hash, \ |
| 94 | .auth_decrypt = _auth_decrypt \ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 95 | } |
| 96 | #else |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 97 | #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \ |
| 98 | _auth_decrypt) \ |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 99 | const crypto_lib_desc_t crypto_lib_desc = { \ |
| 100 | .name = _name, \ |
| 101 | .init = _init, \ |
| 102 | .verify_signature = _verify_signature, \ |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 103 | .verify_hash = _verify_hash, \ |
| 104 | .auth_decrypt = _auth_decrypt \ |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 105 | } |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 106 | #endif /* MEASURED_BOOT */ |
Juan Castillo | 8e55d93 | 2015-04-02 09:48:16 +0100 | [diff] [blame] | 107 | |
Roberto Vargas | 52f707f | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 108 | extern const crypto_lib_desc_t crypto_lib_desc; |
| 109 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 110 | #endif /* CRYPTO_MOD_H */ |