Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +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 | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 7 | #include <assert.h> |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <string.h> |
| 10 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 11 | /* mbed TLS headers */ |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 12 | #include <mbedtls/gcm.h> |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 13 | #include <mbedtls/md.h> |
| 14 | #include <mbedtls/memory_buffer_alloc.h> |
| 15 | #include <mbedtls/oid.h> |
| 16 | #include <mbedtls/platform.h> |
Madhukar Pappireddy | 57eaae8 | 2020-03-05 18:18:40 -0600 | [diff] [blame] | 17 | #include <mbedtls/x509.h> |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 18 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 19 | #include <common/debug.h> |
| 20 | #include <drivers/auth/crypto_mod.h> |
| 21 | #include <drivers/auth/mbedtls/mbedtls_common.h> |
| 22 | #include <drivers/auth/mbedtls/mbedtls_config.h> |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 23 | #include <plat/common/platform.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 24 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 25 | #define LIB_NAME "mbed TLS" |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 26 | |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 27 | #if MEASURED_BOOT |
| 28 | /* |
| 29 | * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available |
| 30 | * so make sure that mbed TLS MD maximum size must be lesser than this. |
| 31 | */ |
| 32 | CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE, |
| 33 | assert_mbedtls_md_size_overflow); |
| 34 | |
| 35 | #endif /* MEASURED_BOOT */ |
| 36 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 37 | /* |
| 38 | * AlgorithmIdentifier ::= SEQUENCE { |
| 39 | * algorithm OBJECT IDENTIFIER, |
| 40 | * parameters ANY DEFINED BY algorithm OPTIONAL |
| 41 | * } |
| 42 | * |
| 43 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 44 | * algorithm AlgorithmIdentifier, |
| 45 | * subjectPublicKey BIT STRING |
| 46 | * } |
| 47 | * |
| 48 | * DigestInfo ::= SEQUENCE { |
| 49 | * digestAlgorithm AlgorithmIdentifier, |
| 50 | * digest OCTET STRING |
| 51 | * } |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | * Initialize the library and export the descriptor |
| 56 | */ |
| 57 | static void init(void) |
| 58 | { |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 59 | /* Initialize mbed TLS */ |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 60 | mbedtls_init(); |
| 61 | } |
| 62 | |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 63 | #if TRUSTED_BOARD_BOOT |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 64 | /* |
| 65 | * Verify a signature. |
| 66 | * |
| 67 | * Parameters are passed using the DER encoding format following the ASN.1 |
| 68 | * structures detailed above. |
| 69 | */ |
| 70 | static int verify_signature(void *data_ptr, unsigned int data_len, |
| 71 | void *sig_ptr, unsigned int sig_len, |
| 72 | void *sig_alg, unsigned int sig_alg_len, |
| 73 | void *pk_ptr, unsigned int pk_len) |
| 74 | { |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 75 | mbedtls_asn1_buf sig_oid, sig_params; |
| 76 | mbedtls_asn1_buf signature; |
| 77 | mbedtls_md_type_t md_alg; |
| 78 | mbedtls_pk_type_t pk_alg; |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 79 | mbedtls_pk_context pk = {0}; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 80 | int rc; |
| 81 | void *sig_opts = NULL; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 82 | const mbedtls_md_info_t *md_info; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 83 | unsigned char *p, *end; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 84 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 85 | |
| 86 | /* Get pointers to signature OID and parameters */ |
| 87 | p = (unsigned char *)sig_alg; |
| 88 | end = (unsigned char *)(p + sig_alg_len); |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 89 | rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 90 | if (rc != 0) { |
| 91 | return CRYPTO_ERR_SIGNATURE; |
| 92 | } |
| 93 | |
| 94 | /* Get the actual signature algorithm (MD + PK) */ |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 95 | rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 96 | if (rc != 0) { |
| 97 | return CRYPTO_ERR_SIGNATURE; |
| 98 | } |
| 99 | |
| 100 | /* Parse the public key */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 101 | mbedtls_pk_init(&pk); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 102 | p = (unsigned char *)pk_ptr; |
| 103 | end = (unsigned char *)(p + pk_len); |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 104 | rc = mbedtls_pk_parse_subpubkey(&p, end, &pk); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 105 | if (rc != 0) { |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 106 | rc = CRYPTO_ERR_SIGNATURE; |
| 107 | goto end2; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Get the signature (bitstring) */ |
| 111 | p = (unsigned char *)sig_ptr; |
| 112 | end = (unsigned char *)(p + sig_len); |
| 113 | signature.tag = *p; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 114 | rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 115 | if (rc != 0) { |
| 116 | rc = CRYPTO_ERR_SIGNATURE; |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 117 | goto end1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 118 | } |
| 119 | signature.p = p; |
| 120 | |
| 121 | /* Calculate the hash of the data */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 122 | md_info = mbedtls_md_info_from_type(md_alg); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 123 | if (md_info == NULL) { |
| 124 | rc = CRYPTO_ERR_SIGNATURE; |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 125 | goto end1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 126 | } |
| 127 | p = (unsigned char *)data_ptr; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 128 | rc = mbedtls_md(md_info, p, data_len, hash); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 129 | if (rc != 0) { |
| 130 | rc = CRYPTO_ERR_SIGNATURE; |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 131 | goto end1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Verify the signature */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 135 | rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash, |
| 136 | mbedtls_md_get_size(md_info), |
| 137 | signature.p, signature.len); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 138 | if (rc != 0) { |
| 139 | rc = CRYPTO_ERR_SIGNATURE; |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 140 | goto end1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* Signature verification success */ |
| 144 | rc = CRYPTO_SUCCESS; |
| 145 | |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 146 | end1: |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 147 | mbedtls_pk_free(&pk); |
Soby Mathew | 0a68d13 | 2017-05-31 10:35:27 +0100 | [diff] [blame] | 148 | end2: |
| 149 | mbedtls_free(sig_opts); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 150 | return rc; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Match a hash |
| 155 | * |
| 156 | * Digest info is passed in DER format following the ASN.1 structure detailed |
| 157 | * above. |
| 158 | */ |
| 159 | static int verify_hash(void *data_ptr, unsigned int data_len, |
| 160 | void *digest_info_ptr, unsigned int digest_info_len) |
| 161 | { |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 162 | mbedtls_asn1_buf hash_oid, params; |
| 163 | mbedtls_md_type_t md_alg; |
| 164 | const mbedtls_md_info_t *md_info; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 165 | unsigned char *p, *end, *hash; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 166 | unsigned char data_hash[MBEDTLS_MD_MAX_SIZE]; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 167 | size_t len; |
| 168 | int rc; |
| 169 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 170 | /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */ |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 171 | p = (unsigned char *)digest_info_ptr; |
Sandrine Bailleux | df8de2d | 2016-01-04 15:49:23 +0000 | [diff] [blame] | 172 | end = p + digest_info_len; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 173 | rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 174 | MBEDTLS_ASN1_SEQUENCE); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 175 | if (rc != 0) { |
| 176 | return CRYPTO_ERR_HASH; |
| 177 | } |
| 178 | |
| 179 | /* Get the hash algorithm */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 180 | rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, ¶ms); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 181 | if (rc != 0) { |
| 182 | return CRYPTO_ERR_HASH; |
| 183 | } |
| 184 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 185 | rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 186 | if (rc != 0) { |
| 187 | return CRYPTO_ERR_HASH; |
| 188 | } |
| 189 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 190 | md_info = mbedtls_md_info_from_type(md_alg); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 191 | if (md_info == NULL) { |
| 192 | return CRYPTO_ERR_HASH; |
| 193 | } |
| 194 | |
| 195 | /* Hash should be octet string type */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 196 | rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 197 | if (rc != 0) { |
| 198 | return CRYPTO_ERR_HASH; |
| 199 | } |
| 200 | |
| 201 | /* Length of hash must match the algorithm's size */ |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 202 | if (len != mbedtls_md_get_size(md_info)) { |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 203 | return CRYPTO_ERR_HASH; |
| 204 | } |
| 205 | hash = p; |
| 206 | |
| 207 | /* Calculate the hash of the data */ |
| 208 | p = (unsigned char *)data_ptr; |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 209 | rc = mbedtls_md(md_info, p, data_len, data_hash); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 210 | if (rc != 0) { |
| 211 | return CRYPTO_ERR_HASH; |
| 212 | } |
| 213 | |
| 214 | /* Compare values */ |
Antonio Nino Diaz | 0ca1afa | 2017-02-09 10:26:54 +0000 | [diff] [blame] | 215 | rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info)); |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 216 | if (rc != 0) { |
| 217 | return CRYPTO_ERR_HASH; |
| 218 | } |
| 219 | |
| 220 | return CRYPTO_SUCCESS; |
| 221 | } |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 222 | #endif /* TRUSTED_BOARD_BOOT */ |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 223 | |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 224 | #if MEASURED_BOOT |
| 225 | /* |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 226 | * Map a generic crypto message digest algorithm to the corresponding macro used |
| 227 | * by Mbed TLS. |
| 228 | */ |
| 229 | static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo) |
| 230 | { |
| 231 | switch (algo) { |
| 232 | case CRYPTO_MD_SHA512: |
| 233 | return MBEDTLS_MD_SHA512; |
| 234 | case CRYPTO_MD_SHA384: |
| 235 | return MBEDTLS_MD_SHA384; |
| 236 | case CRYPTO_MD_SHA256: |
| 237 | return MBEDTLS_MD_SHA256; |
| 238 | default: |
| 239 | /* Invalid hash algorithm. */ |
| 240 | return MBEDTLS_MD_NONE; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 245 | * Calculate a hash |
| 246 | * |
| 247 | * output points to the computed hash |
| 248 | */ |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 249 | static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr, |
| 250 | unsigned int data_len, |
| 251 | unsigned char output[CRYPTO_MD_MAX_SIZE]) |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 252 | { |
| 253 | const mbedtls_md_info_t *md_info; |
| 254 | |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 255 | md_info = mbedtls_md_info_from_type(md_type(md_algo)); |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 256 | if (md_info == NULL) { |
| 257 | return CRYPTO_ERR_HASH; |
| 258 | } |
| 259 | |
Manish V Badarkhe | e112a5a | 2021-10-06 23:41:50 +0100 | [diff] [blame] | 260 | /* |
| 261 | * Calculate the hash of the data, it is safe to pass the |
| 262 | * 'output' hash buffer pointer considering its size is always |
| 263 | * bigger than or equal to MBEDTLS_MD_MAX_SIZE. |
| 264 | */ |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 265 | return mbedtls_md(md_info, data_ptr, data_len, output); |
| 266 | } |
| 267 | #endif /* MEASURED_BOOT */ |
| 268 | |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 269 | #if TF_MBEDTLS_USE_AES_GCM |
| 270 | /* |
| 271 | * Stack based buffer allocation for decryption operation. It could |
| 272 | * be configured to balance stack usage vs execution speed. |
| 273 | */ |
| 274 | #define DEC_OP_BUF_SIZE 128 |
| 275 | |
| 276 | static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key, |
| 277 | unsigned int key_len, const void *iv, |
| 278 | unsigned int iv_len, const void *tag, |
| 279 | unsigned int tag_len) |
| 280 | { |
| 281 | mbedtls_gcm_context ctx; |
| 282 | mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES; |
| 283 | unsigned char buf[DEC_OP_BUF_SIZE]; |
| 284 | unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE]; |
| 285 | unsigned char *pt = data_ptr; |
| 286 | size_t dec_len; |
| 287 | int diff, i, rc; |
| 288 | |
| 289 | mbedtls_gcm_init(&ctx); |
| 290 | |
| 291 | rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8); |
| 292 | if (rc != 0) { |
| 293 | rc = CRYPTO_ERR_DECRYPTION; |
| 294 | goto exit_gcm; |
| 295 | } |
| 296 | |
| 297 | rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0); |
| 298 | if (rc != 0) { |
| 299 | rc = CRYPTO_ERR_DECRYPTION; |
| 300 | goto exit_gcm; |
| 301 | } |
| 302 | |
| 303 | while (len > 0) { |
| 304 | dec_len = MIN(sizeof(buf), len); |
| 305 | |
| 306 | rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf); |
| 307 | if (rc != 0) { |
| 308 | rc = CRYPTO_ERR_DECRYPTION; |
| 309 | goto exit_gcm; |
| 310 | } |
| 311 | |
| 312 | memcpy(pt, buf, dec_len); |
| 313 | pt += dec_len; |
| 314 | len -= dec_len; |
| 315 | } |
| 316 | |
| 317 | rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf)); |
| 318 | if (rc != 0) { |
| 319 | rc = CRYPTO_ERR_DECRYPTION; |
| 320 | goto exit_gcm; |
| 321 | } |
| 322 | |
| 323 | /* Check tag in "constant-time" */ |
| 324 | for (diff = 0, i = 0; i < tag_len; i++) |
| 325 | diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i]; |
| 326 | |
| 327 | if (diff != 0) { |
| 328 | rc = CRYPTO_ERR_DECRYPTION; |
| 329 | goto exit_gcm; |
| 330 | } |
| 331 | |
| 332 | /* GCM decryption success */ |
| 333 | rc = CRYPTO_SUCCESS; |
| 334 | |
| 335 | exit_gcm: |
| 336 | mbedtls_gcm_free(&ctx); |
| 337 | return rc; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Authenticated decryption of an image |
| 342 | */ |
| 343 | static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr, |
| 344 | size_t len, const void *key, unsigned int key_len, |
| 345 | unsigned int key_flags, const void *iv, |
| 346 | unsigned int iv_len, const void *tag, |
| 347 | unsigned int tag_len) |
| 348 | { |
| 349 | int rc; |
| 350 | |
| 351 | assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0); |
| 352 | |
| 353 | switch (dec_algo) { |
| 354 | case CRYPTO_GCM_DECRYPT: |
| 355 | rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len, |
| 356 | tag, tag_len); |
| 357 | if (rc != 0) |
| 358 | return rc; |
| 359 | break; |
| 360 | default: |
| 361 | return CRYPTO_ERR_DECRYPTION; |
| 362 | } |
| 363 | |
| 364 | return CRYPTO_SUCCESS; |
| 365 | } |
| 366 | #endif /* TF_MBEDTLS_USE_AES_GCM */ |
| 367 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 368 | /* |
| 369 | * Register crypto library descriptor |
| 370 | */ |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 371 | #if MEASURED_BOOT && TRUSTED_BOARD_BOOT |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 372 | #if TF_MBEDTLS_USE_AES_GCM |
| 373 | REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash, |
| 374 | auth_decrypt); |
| 375 | #else |
| 376 | REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash, |
| 377 | NULL); |
| 378 | #endif |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 379 | #elif TRUSTED_BOARD_BOOT |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 380 | #if TF_MBEDTLS_USE_AES_GCM |
| 381 | REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, |
| 382 | auth_decrypt); |
Alexei Fedorov | 913cb7e | 2020-01-23 14:27:38 +0000 | [diff] [blame] | 383 | #else |
Sumit Garg | 392e4df | 2019-11-15 10:43:00 +0530 | [diff] [blame] | 384 | REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL); |
| 385 | #endif |
Manish V Badarkhe | 92de80a | 2021-12-16 10:41:47 +0000 | [diff] [blame] | 386 | #elif MEASURED_BOOT |
| 387 | REGISTER_CRYPTO_LIB(LIB_NAME, init, calc_hash); |
| 388 | #endif /* MEASURED_BOOT && TRUSTED_BOARD_BOOT */ |