Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * \brief Include the Crypto engine header. This source module implements the |
| 10 | * interface which is being used by the other components of the service |
| 11 | * to interact with the cryptography primitives available, in SW or HW. |
| 12 | * The current implementation provides only a SW implementation based on |
| 13 | * Mbed TLS functions. |
| 14 | */ |
| 15 | #include "crypto_engine.h" |
| 16 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 17 | /** |
| 18 | * \brief Default value for the size of the static buffer used by the Engine |
| 19 | * module as a scratch buffer for its own internal allocations |
| 20 | */ |
| 21 | #ifndef TFM_CRYPTO_ENGINE_BUF_SIZE |
| 22 | #define TFM_CRYPTO_ENGINE_BUF_SIZE (1024) |
| 23 | #endif |
| 24 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 25 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 26 | /** |
| 27 | * \brief Buffer size used by Mbed TLS for its allocations |
| 28 | */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 29 | #define TFM_CRYPTO_MBEDTLS_MEM_BUF_LEN (TFM_CRYPTO_ENGINE_BUF_SIZE) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * \brief Static buffer to be used by Mbed TLS for memory allocations |
| 33 | * |
| 34 | */ |
| 35 | static uint8_t mbedtls_mem_buf[TFM_CRYPTO_MBEDTLS_MEM_BUF_LEN] = {0}; |
| 36 | |
| 37 | /** |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 38 | * \brief Converts a PSA key type and key size to an Mbed TLS cipher ID. |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 39 | * |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 40 | * \param[in] key_type PSA key type |
| 41 | * \param[in] key_size Key size in bits |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 42 | * |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 43 | * \return An mbedtls_cipher_id_t value corresponding to key_type and key_size, |
| 44 | * or MBEDTLS_CIPHER_ID_NONE if no such cipher ID is supported by |
| 45 | * Mbed TLS. |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 46 | */ |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 47 | static mbedtls_cipher_id_t psa_to_mbedtls_cipher_id(psa_key_type_t key_type, |
| 48 | size_t key_size) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 49 | { |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 50 | switch (key_type) { |
| 51 | case PSA_KEY_TYPE_AES: |
| 52 | return MBEDTLS_CIPHER_ID_AES; |
| 53 | case PSA_KEY_TYPE_DES: |
| 54 | return (key_size == 192) ? MBEDTLS_CIPHER_ID_3DES |
| 55 | : MBEDTLS_CIPHER_ID_DES; |
| 56 | case PSA_KEY_TYPE_CAMELLIA: |
| 57 | return MBEDTLS_CIPHER_ID_CAMELLIA; |
| 58 | case PSA_KEY_TYPE_ARC4: |
| 59 | return MBEDTLS_CIPHER_ID_ARC4; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 60 | default: |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 61 | return MBEDTLS_CIPHER_ID_NONE; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 64 | |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 65 | /** |
| 66 | * \brief Converts a PSA algorithm to an Mbed TLS cipher mode. |
| 67 | * |
| 68 | * \param[in] alg PSA algorithm |
| 69 | * |
| 70 | * \return An mbedtls_cipher_mode_t value corresponding to alg, or |
| 71 | * MBEDTLS_MODE_NONE if no such cipher mode is supported by Mbed TLS. |
| 72 | */ |
| 73 | static mbedtls_cipher_mode_t psa_to_mbedtls_cipher_mode(psa_algorithm_t alg) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 74 | { |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 75 | if (PSA_ALG_IS_BLOCK_CIPHER(alg)) { |
| 76 | /* Clear the padding mask */ |
| 77 | alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK; |
| 78 | } |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 79 | |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 80 | switch (alg) { |
| 81 | case PSA_ALG_CBC_BASE: |
| 82 | return MBEDTLS_MODE_CBC; |
| 83 | case PSA_ALG_CFB_BASE: |
| 84 | return MBEDTLS_MODE_CFB; |
| 85 | case PSA_ALG_OFB_BASE: |
| 86 | return MBEDTLS_MODE_OFB; |
| 87 | case PSA_ALG_XTS_BASE: |
| 88 | return MBEDTLS_MODE_NONE; /* FIXME: requires Mbed TLS 2.11 */ |
| 89 | case PSA_ALG_CTR: |
| 90 | return MBEDTLS_MODE_CTR; |
| 91 | case PSA_ALG_ARC4: |
| 92 | return MBEDTLS_MODE_STREAM; /* ARC4 is a stream cipher */ |
| 93 | case PSA_ALG_CCM: |
| 94 | return MBEDTLS_MODE_CCM; |
| 95 | case PSA_ALG_GCM: |
| 96 | return MBEDTLS_MODE_GCM; |
| 97 | default: |
| 98 | return MBEDTLS_MODE_NONE; |
| 99 | } |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 102 | /** |
| 103 | * \brief Given a PSA key type, algorithm and key size, finds the corresponding |
| 104 | * Mbed TLS cipher info struct. |
| 105 | * |
| 106 | * \param[in] key_type PSA key type |
| 107 | * \param[in] alg PSA algorithm |
| 108 | * \param[in] key_size Key size in bits |
| 109 | * |
| 110 | * \return A pointer to the mbedtls_cipher_info_t struct corresponding to |
| 111 | * key_type, alg and key_size, or NULL if Mbed TLS does not support this |
| 112 | * combination. |
| 113 | */ |
| 114 | static const mbedtls_cipher_info_t *get_mbedtls_cipher_info( |
| 115 | psa_key_type_t key_type, |
| 116 | psa_algorithm_t alg, |
| 117 | size_t key_size) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 118 | { |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 119 | mbedtls_cipher_id_t cipher_id; |
| 120 | mbedtls_cipher_mode_t cipher_mode; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 121 | |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 122 | /* Get the Mbed TLS cipher ID */ |
| 123 | cipher_id = psa_to_mbedtls_cipher_id(key_type, key_size); |
| 124 | if (cipher_id == MBEDTLS_CIPHER_ID_NONE) { |
| 125 | /* The requested key type is not supported by Mbed TLS */ |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | /* Get the Mbed TLS cipher mode */ |
| 130 | cipher_mode = psa_to_mbedtls_cipher_mode(alg); |
| 131 | if (cipher_mode == MBEDTLS_MODE_NONE) { |
| 132 | /* The requested algorithm is not supported by Mbed TLS */ |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | /* Get the Mbed TLS cipher info pointer */ |
| 137 | return mbedtls_cipher_info_from_values(cipher_id, key_size, cipher_mode); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * \brief Converts a PSA algorithm to an Mbed TLS message digest type. |
| 142 | * |
| 143 | * \param[in] alg PSA algorithm |
| 144 | * |
| 145 | * \return An mbedtls_md_type_t value corresponding to alg, or |
| 146 | * MBEDTLS_MD_NONE if no such message digest is available. |
| 147 | */ |
| 148 | static mbedtls_md_type_t psa_to_mbedtls_md_type(psa_algorithm_t alg) |
| 149 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 150 | mbedtls_md_type_t type = MBEDTLS_MD_NONE; |
| 151 | |
| 152 | /* Mbed TLS message digest setup */ |
| 153 | switch(alg) { |
| 154 | case PSA_ALG_MD2: |
| 155 | #if defined(MBEDTLS_MD2_C) |
| 156 | type = MBEDTLS_MD_MD2; |
| 157 | #endif |
| 158 | break; |
| 159 | case PSA_ALG_MD4: |
| 160 | #if defined(MBEDTLS_MD4_C) |
| 161 | type = MBEDTLS_MD_MD4; |
| 162 | #endif |
| 163 | break; |
| 164 | case PSA_ALG_MD5: |
| 165 | #if defined(MBEDTLS_MD5_C) |
| 166 | type = MBEDTLS_MD_MD5; |
| 167 | #endif |
| 168 | break; |
| 169 | case PSA_ALG_RIPEMD160: |
| 170 | #if defined(MBEDTLS_RIPEMD160_C) |
| 171 | type = MBEDTLS_MD_RIPEMD160; |
| 172 | #endif |
| 173 | break; |
| 174 | case PSA_ALG_SHA_1: |
| 175 | #if defined(MBEDTLS_SHA1_C) |
| 176 | type = MBEDTLS_MD_SHA1; |
| 177 | #endif |
| 178 | break; |
| 179 | case PSA_ALG_SHA_224: |
| 180 | #if defined(MBEDTLS_SHA256_C) |
| 181 | type = MBEDTLS_MD_SHA224; |
| 182 | #endif |
| 183 | break; |
| 184 | case PSA_ALG_SHA_256: |
| 185 | #if defined(MBEDTLS_SHA256_C) |
| 186 | type = MBEDTLS_MD_SHA256; |
| 187 | #endif |
| 188 | break; |
| 189 | case PSA_ALG_SHA_384: |
| 190 | #if defined(MBEDTLS_SHA512_C) |
| 191 | type = MBEDTLS_MD_SHA384; |
| 192 | #endif |
| 193 | break; |
| 194 | case PSA_ALG_SHA_512: |
| 195 | #if defined(MBEDTLS_SHA512_C) |
| 196 | type = MBEDTLS_MD_SHA512; |
| 197 | #endif |
| 198 | break; |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 199 | case PSA_ALG_SHA3_224: |
| 200 | case PSA_ALG_SHA3_256: |
| 201 | case PSA_ALG_SHA3_384: |
| 202 | case PSA_ALG_SHA3_512: |
| 203 | /* SHA3 not yet supported */ |
| 204 | type = MBEDTLS_MD_NONE; |
| 205 | break; |
| 206 | case PSA_ALG_SHA_512_224: |
| 207 | case PSA_ALG_SHA_512_256: |
| 208 | /* SHA-512 Truncated modes not yet supported */ |
| 209 | type = MBEDTLS_MD_NONE; |
| 210 | break; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 215 | return type; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * \brief This function maps Mbed TLS return codes to PSA return codes. |
| 220 | * |
| 221 | * \param[in] ret Mbed TLS return value |
| 222 | * |
| 223 | * \return Return values as specified by \ref psa_status_t |
| 224 | */ |
| 225 | static psa_status_t mbedtls_to_psa_return(int ret) |
| 226 | { |
| 227 | /* zero return value means success */ |
| 228 | if (ret == 0) { |
| 229 | return PSA_SUCCESS; |
| 230 | } |
| 231 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 232 | /* FIXME: Investigate all possible Mbed TLS errors and map them |
| 233 | * to the the correct corresponding PSA status |
| 234 | */ |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 235 | switch (ret) { |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 236 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 237 | return PSA_ERROR_INVALID_ARGUMENT; |
| 238 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 239 | return PSA_ERROR_INVALID_SIGNATURE; |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 240 | default: |
| 241 | return PSA_ERROR_UNKNOWN_ERROR; |
| 242 | } |
| 243 | } |
| 244 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 245 | |
| 246 | psa_status_t tfm_crypto_engine_init(void) |
| 247 | { |
| 248 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 249 | |
| 250 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 251 | /* Initialise the Mbed TLS static memory allocator so that Mbed TLS |
| 252 | * allocates memory from the provided static buffer instead of from |
| 253 | * the heap. |
| 254 | */ |
| 255 | mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, |
| 256 | TFM_CRYPTO_MBEDTLS_MEM_BUF_LEN); |
| 257 | /* The previous function doesn't return any error code */ |
| 258 | return_value = PSA_SUCCESS; |
| 259 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 260 | |
| 261 | return return_value; |
| 262 | } |
| 263 | |
| 264 | psa_status_t tfm_crypto_engine_hash_setup(const psa_algorithm_t alg, |
| 265 | struct hash_engine_info *engine_info) |
| 266 | { |
| 267 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 268 | |
| 269 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 270 | mbedtls_md_type_t type = MBEDTLS_MD_NONE; |
| 271 | |
| 272 | type = psa_to_mbedtls_md_type(alg); |
| 273 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 274 | engine_info->type = (uint32_t) type; |
| 275 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 276 | if (type == MBEDTLS_MD_NONE) { |
| 277 | return_value = PSA_ERROR_NOT_SUPPORTED; |
| 278 | } else { |
| 279 | return_value = PSA_SUCCESS; |
| 280 | } |
| 281 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 282 | |
| 283 | return return_value; |
| 284 | } |
| 285 | |
| 286 | psa_status_t tfm_crypto_engine_hash_start( |
| 287 | union engine_hash_context *hp, |
| 288 | const struct hash_engine_info *engine_info) |
| 289 | { |
| 290 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 291 | |
| 292 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 293 | const mbedtls_md_info_t *info = NULL; |
| 294 | int ret; |
| 295 | |
| 296 | /* Mbed TLS message digest init */ |
| 297 | mbedtls_md_init(&(hp->ctx)); |
| 298 | info = mbedtls_md_info_from_type((mbedtls_md_type_t)engine_info->type); |
| 299 | ret = mbedtls_md_setup(&(hp->ctx), info, 0); /* 0: not HMAC */ |
| 300 | if (ret != 0) { |
| 301 | return_value = mbedtls_to_psa_return(ret); |
| 302 | } else { |
| 303 | /* Start the message digest context */ |
| 304 | ret = mbedtls_md_starts(&(hp->ctx)); |
| 305 | return_value = mbedtls_to_psa_return(ret); |
| 306 | } |
| 307 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 308 | |
| 309 | return return_value; |
| 310 | } |
| 311 | |
| 312 | psa_status_t tfm_crypto_engine_hash_update(union engine_hash_context *hp, |
| 313 | const uint8_t *input, |
| 314 | const uint32_t input_length) |
| 315 | { |
| 316 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 317 | |
| 318 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 319 | int ret; |
| 320 | /* Update the message digest with a new chunk of information */ |
| 321 | ret = mbedtls_md_update(&(hp->ctx), input, input_length); |
| 322 | return_value = mbedtls_to_psa_return(ret); |
| 323 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 324 | |
| 325 | return return_value; |
| 326 | } |
| 327 | |
| 328 | psa_status_t tfm_crypto_engine_hash_finish(union engine_hash_context *hp, |
| 329 | uint8_t *hash) |
| 330 | { |
| 331 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 332 | |
| 333 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 334 | int ret; |
| 335 | /* Finalise the hash value, producing the output */ |
| 336 | ret = mbedtls_md_finish(&(hp->ctx), hash); |
| 337 | return_value = mbedtls_to_psa_return(ret); |
| 338 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 339 | |
| 340 | return return_value; |
| 341 | } |
| 342 | |
| 343 | psa_status_t tfm_crypto_engine_hash_release(union engine_hash_context *hp) |
| 344 | { |
| 345 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 346 | |
| 347 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 348 | /* Clear the Mbed TLS message digest context */ |
| 349 | mbedtls_md_free(&(hp->ctx)); |
| 350 | /* The previous function doesn't return any error code */ |
| 351 | return_value = PSA_SUCCESS; |
| 352 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 353 | |
| 354 | return return_value; |
| 355 | } |
| 356 | |
| 357 | psa_status_t tfm_crypto_engine_cipher_setup(const psa_algorithm_t alg, |
| 358 | const psa_key_type_t key_type, |
| 359 | const uint32_t key_size, |
| 360 | const enum engine_cipher_mode_t engine_cipher_mode, |
| 361 | struct cipher_engine_info *engine_info) |
| 362 | { |
| 363 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 364 | |
| 365 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 366 | const mbedtls_cipher_info_t *info; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 367 | mbedtls_cipher_padding_t padding_mode = MBEDTLS_PADDING_NONE; |
| 368 | psa_algorithm_t padding_mode_field = PSA_ALG_BLOCK_CIPHER_PAD_NONE; |
| 369 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 370 | if ((engine_cipher_mode != ENGINE_CIPHER_MODE_ENCRYPT) && |
| 371 | (engine_cipher_mode != ENGINE_CIPHER_MODE_DECRYPT)) { |
| 372 | return_value = PSA_ERROR_NOT_SUPPORTED; |
| 373 | } else { |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 374 | /* Get the Mbed TLS cipher info from PSA key_type/alg/key_size. */ |
| 375 | info = get_mbedtls_cipher_info(key_type, alg, key_size); |
| 376 | if (info == NULL) { |
| 377 | /* The combination of key_type, alg and key_size is not a valid |
| 378 | * Mbed TLS cipher configuration. |
| 379 | */ |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 380 | return_value = PSA_ERROR_NOT_SUPPORTED; |
| 381 | } else { |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 382 | /* If CBC, need to check the padding mode */ |
| 383 | if ((alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK) == PSA_ALG_CBC_BASE){ |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 384 | |
| 385 | /* Check the value of padding field */ |
| 386 | padding_mode_field = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; |
| 387 | |
| 388 | switch (padding_mode_field) { |
| 389 | case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7: |
| 390 | padding_mode = MBEDTLS_PADDING_PKCS7; |
| 391 | return_value = PSA_SUCCESS; |
| 392 | break; |
| 393 | case PSA_ALG_BLOCK_CIPHER_PAD_NONE: |
| 394 | padding_mode = MBEDTLS_PADDING_NONE; |
| 395 | return_value = PSA_SUCCESS; |
| 396 | break; |
| 397 | default: |
| 398 | return_value = PSA_ERROR_NOT_SUPPORTED; |
| 399 | } |
| 400 | } else { |
| 401 | /* The engine is correctly configured */ |
| 402 | return_value = PSA_SUCCESS; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | if (return_value == PSA_SUCCESS) { |
Jamie Fox | 4394fcf | 2018-10-08 16:14:52 +0100 | [diff] [blame] | 408 | engine_info->type = (uint32_t) (info->type); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 409 | engine_info->cipher_mode = engine_cipher_mode; |
| 410 | engine_info->padding_mode = (uint32_t) padding_mode; |
| 411 | } |
| 412 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 413 | |
| 414 | return return_value; |
| 415 | } |
| 416 | |
| 417 | psa_status_t tfm_crypto_engine_cipher_set_padding_mode( |
| 418 | union engine_cipher_context *cp, |
| 419 | const struct cipher_engine_info *engine_info) |
| 420 | { |
| 421 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 422 | |
| 423 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 424 | int ret; |
| 425 | ret = mbedtls_cipher_set_padding_mode(&(cp->ctx), |
| 426 | (mbedtls_cipher_padding_t)engine_info->padding_mode); |
| 427 | return_value = mbedtls_to_psa_return(ret); |
| 428 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 429 | |
| 430 | return return_value; |
| 431 | } |
| 432 | |
| 433 | psa_status_t tfm_crypto_engine_cipher_start( |
| 434 | union engine_cipher_context *cp, |
| 435 | const struct cipher_engine_info *engine_info) |
| 436 | { |
| 437 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 438 | |
| 439 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 440 | int ret; |
| 441 | const mbedtls_cipher_info_t *info = NULL; |
| 442 | |
| 443 | /* Mbed TLS cipher init */ |
| 444 | mbedtls_cipher_init(&(cp->ctx)); |
| 445 | info = mbedtls_cipher_info_from_type(engine_info->type); |
| 446 | |
| 447 | ret = mbedtls_cipher_setup(&(cp->ctx), info); |
| 448 | return_value = mbedtls_to_psa_return(ret); |
| 449 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 450 | |
| 451 | return return_value; |
| 452 | } |
| 453 | |
| 454 | psa_status_t tfm_crypto_engine_cipher_set_key( |
| 455 | union engine_cipher_context *cp, |
| 456 | const uint8_t *key_data, |
| 457 | const uint32_t key_size, |
| 458 | const struct cipher_engine_info *engine_info) |
| 459 | { |
| 460 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 461 | |
| 462 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 463 | int ret; |
| 464 | /* Mbed TLS cipher set key */ |
| 465 | if ((engine_info->cipher_mode != ENGINE_CIPHER_MODE_ENCRYPT) && |
| 466 | (engine_info->cipher_mode != ENGINE_CIPHER_MODE_DECRYPT)) { |
| 467 | return_value = PSA_ERROR_NOT_SUPPORTED; |
| 468 | } else { |
| 469 | /* Set the key on the context */ |
| 470 | ret = mbedtls_cipher_setkey( |
| 471 | &(cp->ctx), |
| 472 | key_data, |
| 473 | PSA_BYTES_TO_BITS(key_size), |
| 474 | (engine_info->cipher_mode == ENGINE_CIPHER_MODE_ENCRYPT) ? |
| 475 | MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT); |
| 476 | return_value = mbedtls_to_psa_return(ret); |
| 477 | } |
| 478 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 479 | |
| 480 | return return_value; |
| 481 | } |
| 482 | |
| 483 | psa_status_t tfm_crypto_engine_cipher_set_iv(union engine_cipher_context *cp, |
| 484 | const uint8_t *iv, |
| 485 | const uint32_t iv_length) |
| 486 | { |
| 487 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 488 | |
| 489 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 490 | int ret; |
| 491 | |
| 492 | /* Bind the IV to the cipher operation */ |
| 493 | ret = mbedtls_cipher_set_iv(&(cp->ctx), iv, iv_length); |
| 494 | if (ret != 0) { |
| 495 | return_value = mbedtls_to_psa_return(ret); |
| 496 | } else { |
| 497 | /* Reset the context after IV is set */ |
| 498 | ret = mbedtls_cipher_reset(&(cp->ctx)); |
| 499 | return_value = mbedtls_to_psa_return(ret); |
| 500 | } |
| 501 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 502 | |
| 503 | return return_value; |
| 504 | } |
| 505 | |
| 506 | psa_status_t tfm_crypto_engine_cipher_update(union engine_cipher_context *cp, |
| 507 | const uint8_t *input, |
| 508 | const uint32_t input_length, |
| 509 | uint8_t *output, |
| 510 | uint32_t *output_length) |
| 511 | { |
| 512 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 513 | |
| 514 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 515 | int ret; |
| 516 | /* Update with the chunk of input data, eventually producing output */ |
| 517 | ret = mbedtls_cipher_update(&(cp->ctx), input, input_length, |
| 518 | output, (size_t *)output_length); |
| 519 | return_value = mbedtls_to_psa_return(ret); |
| 520 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 521 | |
| 522 | return return_value; |
| 523 | } |
| 524 | |
| 525 | psa_status_t tfm_crypto_engine_cipher_finish(union engine_cipher_context *cp, |
| 526 | uint8_t *output, |
| 527 | uint32_t *output_length) |
| 528 | { |
| 529 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 530 | |
| 531 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 532 | int ret; |
| 533 | /* Finalise the cipher operation */ |
| 534 | ret = mbedtls_cipher_finish(&(cp->ctx), output, (size_t *)output_length); |
| 535 | return_value = mbedtls_to_psa_return(ret); |
| 536 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 537 | |
| 538 | return return_value; |
| 539 | } |
| 540 | |
| 541 | psa_status_t tfm_crypto_engine_cipher_release(union engine_cipher_context *cp) |
| 542 | { |
| 543 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 544 | |
| 545 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 546 | /* Clear the Mbed TLS context */ |
| 547 | mbedtls_cipher_free(&(cp->ctx)); |
| 548 | /* The previous function doesn't return any error code */ |
| 549 | return_value = PSA_SUCCESS; |
| 550 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 551 | |
| 552 | return return_value; |
| 553 | } |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 554 | |
| 555 | psa_status_t tfm_crypto_engine_aead_encrypt(psa_key_type_t key_type, |
| 556 | psa_algorithm_t alg, |
| 557 | const uint8_t *key_data, |
| 558 | uint32_t key_size, |
| 559 | const uint8_t *nonce, |
| 560 | uint32_t nonce_length, |
| 561 | const uint8_t *additional_data, |
| 562 | uint32_t additional_data_length, |
| 563 | const uint8_t *plaintext, |
| 564 | uint32_t plaintext_length, |
| 565 | uint8_t *ciphertext, |
| 566 | uint32_t ciphertext_size, |
| 567 | uint32_t *ciphertext_length) |
| 568 | { |
| 569 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 570 | |
| 571 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 572 | int ret; |
| 573 | mbedtls_cipher_context_t aead_ctx; |
| 574 | const uint32_t key_size_bits = PSA_BYTES_TO_BITS(key_size); |
| 575 | |
| 576 | const mbedtls_cipher_info_t *info = get_mbedtls_cipher_info(key_type, |
| 577 | alg, |
| 578 | key_size_bits); |
| 579 | const uint32_t encrypted_message_length = |
| 580 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) - |
| 581 | PSA_AEAD_TAG_SIZE(alg); |
| 582 | if (info == NULL) { |
| 583 | /* The combination of key_type, alg and key_size is not a valid Mbed TLS |
| 584 | * cipher configuration. |
| 585 | */ |
| 586 | return_value = PSA_ERROR_NOT_SUPPORTED; |
| 587 | } else { |
| 588 | /* Init the mbedTLS context */ |
| 589 | mbedtls_cipher_init(&aead_ctx); |
| 590 | |
| 591 | /* Setup the mbedTLS context */ |
| 592 | ret = mbedtls_cipher_setup(&aead_ctx, info); |
| 593 | if (ret != 0) { |
| 594 | return_value = mbedtls_to_psa_return(ret); |
| 595 | } else { |
| 596 | /* Set the key on the Mbed TLS context */ |
| 597 | ret = mbedtls_cipher_setkey(&aead_ctx, |
| 598 | key_data, |
| 599 | key_size_bits, |
| 600 | MBEDTLS_ENCRYPT); |
| 601 | if (ret != 0) { |
| 602 | return_value = mbedtls_to_psa_return(ret); |
| 603 | } else { |
| 604 | /* Perform the AEAD operation on the Mbed TLS context */ |
| 605 | ret = mbedtls_cipher_auth_encrypt(&aead_ctx, |
| 606 | nonce, nonce_length, |
| 607 | additional_data, |
| 608 | additional_data_length, |
| 609 | plaintext, |
| 610 | plaintext_length, |
| 611 | ciphertext, |
| 612 | (size_t *)ciphertext_length, |
| 613 | ciphertext + |
| 614 | encrypted_message_length, |
| 615 | PSA_AEAD_TAG_SIZE(alg)); |
| 616 | |
| 617 | /* Clear the Mbed TLS context */ |
| 618 | mbedtls_cipher_free(&aead_ctx); |
| 619 | return_value = mbedtls_to_psa_return(ret); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 624 | |
| 625 | return return_value; |
| 626 | } |
| 627 | |
| 628 | psa_status_t tfm_crypto_engine_aead_decrypt(psa_key_type_t key_type, |
| 629 | psa_algorithm_t alg, |
| 630 | const uint8_t *key_data, |
| 631 | uint32_t key_size, |
| 632 | const uint8_t *nonce, |
| 633 | uint32_t nonce_length, |
| 634 | const uint8_t *additional_data, |
| 635 | uint32_t additional_data_length, |
| 636 | const uint8_t *ciphertext, |
| 637 | uint32_t ciphertext_length, |
| 638 | uint8_t *plaintext, |
| 639 | uint32_t plaintext_size, |
| 640 | uint32_t *plaintext_length) |
| 641 | { |
| 642 | psa_status_t return_value = PSA_ERROR_NOT_SUPPORTED; |
| 643 | |
| 644 | #if defined(TFM_CRYPTO_ENGINE_MBEDTLS) |
| 645 | int ret; |
| 646 | mbedtls_cipher_context_t aead_ctx; |
| 647 | const uint32_t key_size_bits = PSA_BYTES_TO_BITS(key_size); |
| 648 | |
| 649 | const mbedtls_cipher_info_t *info = get_mbedtls_cipher_info(key_type, |
| 650 | alg, |
| 651 | key_size_bits); |
| 652 | |
| 653 | const uint32_t encrypted_message_length = |
| 654 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length); |
| 655 | if (info == NULL) { |
| 656 | /* The combination of key_type, alg and key_size is not a valid Mbed TLS |
| 657 | * cipher configuration. |
| 658 | */ |
| 659 | return_value = PSA_ERROR_NOT_SUPPORTED; |
| 660 | } else { |
| 661 | /* Init the mbedTLS context */ |
| 662 | mbedtls_cipher_init(&aead_ctx); |
| 663 | |
| 664 | /* Setup the mbedTLS context */ |
| 665 | ret = mbedtls_cipher_setup(&aead_ctx, info); |
| 666 | if (ret != 0) { |
| 667 | return_value = mbedtls_to_psa_return(ret); |
| 668 | } else { |
| 669 | /* Set the key on the Mbed TLS context */ |
| 670 | ret = mbedtls_cipher_setkey(&aead_ctx, |
| 671 | key_data, |
| 672 | key_size_bits, |
| 673 | MBEDTLS_DECRYPT); |
| 674 | if (ret != 0) { |
| 675 | return_value = mbedtls_to_psa_return(ret); |
| 676 | } else { |
| 677 | /* Perform the AEAD operation on the Mbed TLS context */ |
| 678 | ret = mbedtls_cipher_auth_decrypt(&aead_ctx, |
| 679 | nonce, nonce_length, |
| 680 | additional_data, |
| 681 | additional_data_length, |
| 682 | ciphertext, |
| 683 | encrypted_message_length, |
| 684 | plaintext, |
| 685 | (size_t *)plaintext_length, |
| 686 | ciphertext + |
| 687 | encrypted_message_length, |
| 688 | PSA_AEAD_TAG_SIZE(alg)); |
| 689 | |
| 690 | /* Clear the Mbed TLS context */ |
| 691 | mbedtls_cipher_free(&aead_ctx); |
| 692 | return_value = mbedtls_to_psa_return(ret); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | #endif /* TFM_CRYPTO_ENGINE_MBEDTLS */ |
| 697 | |
| 698 | return return_value; |
| 699 | } |