blob: fa1adb4f7769c29910812743beb0234f4091106e [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Manish V Badarkhe0bf24072022-02-25 08:29:35 +00002 * Copyright (c) 2015-2022, 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
7#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <common/debug.h>
10#include <drivers/auth/crypto_mod.h>
Juan Castillo8e55d932015-04-02 09:48:16 +010011
12/* Variable exported by the crypto library through REGISTER_CRYPTO_LIB() */
Juan Castillo8e55d932015-04-02 09:48:16 +010013
14/*
15 * The crypto module is responsible for verifying digital signatures and hashes.
16 * It relies on a crypto library to perform the cryptographic operations.
17 *
18 * The crypto module itself does not impose any specific format on signatures,
19 * signature algorithm, keys or hashes, but most cryptographic libraries will
20 * take the parameters as the following DER encoded ASN.1 structures:
21 *
22 * AlgorithmIdentifier ::= SEQUENCE {
23 * algorithm OBJECT IDENTIFIER,
24 * parameters ANY DEFINED BY algorithm OPTIONAL
25 * }
26 *
27 * DigestInfo ::= SEQUENCE {
28 * digestAlgorithm AlgorithmIdentifier,
29 * digest OCTET STRING
30 * }
31 *
32 * SubjectPublicKeyInfo ::= SEQUENCE {
33 * algorithm AlgorithmIdentifier,
34 * subjectPublicKey BIT STRING
35 * }
36 *
37 * SignatureAlgorithm ::= AlgorithmIdentifier
38 *
39 * SignatureValue ::= BIT STRING
40 */
41
42/*
43 * Perform some static checking and call the library initialization function
44 */
45void crypto_mod_init(void)
46{
47 assert(crypto_lib_desc.name != NULL);
48 assert(crypto_lib_desc.init != NULL);
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010049#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
50CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Juan Castillo8e55d932015-04-02 09:48:16 +010051 assert(crypto_lib_desc.verify_signature != NULL);
52 assert(crypto_lib_desc.verify_hash != NULL);
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010053#endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
54 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
55
56#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
57CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Manish V Badarkhe92de80a2021-12-16 10:41:47 +000058 assert(crypto_lib_desc.calc_hash != NULL);
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010059#endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
60 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Juan Castillo8e55d932015-04-02 09:48:16 +010061
62 /* Initialize the cryptographic library */
63 crypto_lib_desc.init();
64 INFO("Using crypto library '%s'\n", crypto_lib_desc.name);
65}
66
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010067#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
68CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Juan Castillo8e55d932015-04-02 09:48:16 +010069/*
70 * Function to verify a digital signature
71 *
72 * Parameters:
73 *
74 * data_ptr, data_len: signed data
75 * sig_ptr, sig_len: the digital signature
76 * sig_alg_ptr, sig_alg_len: the digital signature algorithm
77 * pk_ptr, pk_len: the public key
78 */
79int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
80 void *sig_ptr, unsigned int sig_len,
81 void *sig_alg_ptr, unsigned int sig_alg_len,
82 void *pk_ptr, unsigned int pk_len)
83{
84 assert(data_ptr != NULL);
85 assert(data_len != 0);
86 assert(sig_ptr != NULL);
87 assert(sig_len != 0);
88 assert(sig_alg_ptr != NULL);
89 assert(sig_alg_len != 0);
90 assert(pk_ptr != NULL);
91 assert(pk_len != 0);
92
93 return crypto_lib_desc.verify_signature(data_ptr, data_len,
94 sig_ptr, sig_len,
95 sig_alg_ptr, sig_alg_len,
96 pk_ptr, pk_len);
97}
98
99/*
100 * Verify a hash by comparison
101 *
102 * Parameters:
103 *
104 * data_ptr, data_len: data to be hashed
105 * digest_info_ptr, digest_info_len: hash to be compared
106 */
107int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
108 void *digest_info_ptr, unsigned int digest_info_len)
109{
110 assert(data_ptr != NULL);
111 assert(data_len != 0);
112 assert(digest_info_ptr != NULL);
113 assert(digest_info_len != 0);
114
115 return crypto_lib_desc.verify_hash(data_ptr, data_len,
116 digest_info_ptr, digest_info_len);
117}
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100118#endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
119 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000120
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100121#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
122CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000123/*
124 * Calculate a hash
125 *
126 * Parameters:
127 *
128 * alg: message digest algorithm
129 * data_ptr, data_len: data to be hashed
130 * output: resulting hash
131 */
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100132int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
133 unsigned int data_len,
134 unsigned char output[CRYPTO_MD_MAX_SIZE])
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000135{
136 assert(data_ptr != NULL);
137 assert(data_len != 0);
138 assert(output != NULL);
139
140 return crypto_lib_desc.calc_hash(alg, data_ptr, data_len, output);
141}
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100142#endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
143 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Sumit Garg392e4df2019-11-15 10:43:00 +0530144
145/*
146 * Authenticated decryption of data
147 *
148 * Parameters:
149 *
150 * dec_algo: authenticated decryption algorithm
151 * data_ptr, len: data to be decrypted (inout param)
152 * key, key_len, key_flags: symmetric decryption key
153 * iv, iv_len: initialization vector
154 * tag, tag_len: authentication tag
155 */
156int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
157 size_t len, const void *key, unsigned int key_len,
158 unsigned int key_flags, const void *iv,
159 unsigned int iv_len, const void *tag,
160 unsigned int tag_len)
161{
162 assert(crypto_lib_desc.auth_decrypt != NULL);
163 assert(data_ptr != NULL);
164 assert(len != 0U);
165 assert(key != NULL);
166 assert(key_len != 0U);
167 assert(iv != NULL);
168 assert((iv_len != 0U) && (iv_len <= CRYPTO_MAX_IV_SIZE));
169 assert(tag != NULL);
170 assert((tag_len != 0U) && (tag_len <= CRYPTO_MAX_TAG_SIZE));
171
172 return crypto_lib_desc.auth_decrypt(dec_algo, data_ptr, len, key,
173 key_len, key_flags, iv, iv_len, tag,
174 tag_len);
175}