Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 10 | |
| 11 | /* FixMe: Use PSA_CONNECTION_REFUSED when performing parameter |
| 12 | * integrity checks but this will have to be revised |
| 13 | * when the full set of error codes mandated by PSA FF |
| 14 | * is available. |
| 15 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 16 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 17 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 18 | #include "tfm_crypto_api.h" |
| 19 | #include "tfm_crypto_defs.h" |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 20 | |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 21 | /*! |
| 22 | * \defgroup public_psa Public functions, PSA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | /*!@{*/ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 27 | psa_status_t tfm_crypto_aead_encrypt(psa_invec in_vec[], |
| 28 | size_t in_len, |
| 29 | psa_outvec out_vec[], |
| 30 | size_t out_len) |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 31 | { |
| 32 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 33 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 34 | if ( !((in_len == 2) || (in_len == 3)) || (out_len != 1)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 35 | return PSA_CONNECTION_REFUSED; |
| 36 | } |
| 37 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 38 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 39 | return PSA_CONNECTION_REFUSED; |
| 40 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 41 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 42 | const struct tfm_crypto_aead_pack_input *aead_pack_input = &iov->aead_in; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 43 | psa_key_handle_t key_handle = iov->key_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 44 | psa_algorithm_t alg = iov->alg; |
| 45 | const uint8_t *nonce = aead_pack_input->nonce; |
| 46 | size_t nonce_length = aead_pack_input->nonce_length; |
| 47 | const uint8_t *plaintext = in_vec[1].base; |
| 48 | size_t plaintext_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 49 | uint8_t *ciphertext = out_vec[0].base; |
| 50 | size_t ciphertext_size = out_vec[0].len; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 51 | const uint8_t *additional_data = NULL; |
| 52 | size_t additional_data_length = 0; |
| 53 | |
| 54 | /* Check if additional data has been passed and initialise it */ |
| 55 | if (in_len == 3) { |
| 56 | additional_data = in_vec[2].base; |
| 57 | additional_data_length = in_vec[2].len; |
| 58 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 59 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 60 | /* Initialise ciphertext_length to zero */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 61 | out_vec[0].len = 0; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 62 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 63 | status = tfm_crypto_check_handle_owner(key_handle, NULL); |
| 64 | if (status == PSA_SUCCESS) { |
| 65 | |
| 66 | status = psa_aead_encrypt(key_handle, alg, nonce, nonce_length, |
| 67 | additional_data, additional_data_length, |
| 68 | plaintext, plaintext_length, |
| 69 | ciphertext, ciphertext_size, &out_vec[0].len); |
| 70 | } |
| 71 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 72 | return status; |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 75 | psa_status_t tfm_crypto_aead_decrypt(psa_invec in_vec[], |
| 76 | size_t in_len, |
| 77 | psa_outvec out_vec[], |
| 78 | size_t out_len) |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 79 | { |
| 80 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 81 | |
Jamie Fox | 9a234e2 | 2019-04-30 11:12:05 +0100 | [diff] [blame] | 82 | if ( !((in_len == 2) || (in_len == 3)) || (out_len > 1)) { |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 83 | return PSA_CONNECTION_REFUSED; |
| 84 | } |
| 85 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 86 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
| 87 | return PSA_CONNECTION_REFUSED; |
| 88 | } |
| 89 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 90 | const struct tfm_crypto_aead_pack_input *aead_pack_input = &iov->aead_in; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 91 | psa_key_handle_t key_handle = iov->key_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 92 | psa_algorithm_t alg = iov->alg; |
| 93 | const uint8_t *nonce = aead_pack_input->nonce; |
| 94 | size_t nonce_length = aead_pack_input->nonce_length; |
| 95 | const uint8_t *ciphertext = in_vec[1].base; |
| 96 | size_t ciphertext_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 97 | uint8_t *plaintext = out_vec[0].base; |
| 98 | size_t plaintext_size = out_vec[0].len; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 99 | const uint8_t *additional_data = NULL; |
| 100 | size_t additional_data_length = 0; |
| 101 | |
| 102 | /* Check if additional data has been passed and initialise it */ |
| 103 | if (in_len == 3) { |
| 104 | additional_data = in_vec[2].base; |
| 105 | additional_data_length = in_vec[2].len; |
| 106 | } |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 107 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 108 | /* Initialise plaintext_length to zero */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 109 | out_vec[0].len = 0; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 110 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 111 | status = tfm_crypto_check_handle_owner(key_handle, NULL); |
| 112 | if (status == PSA_SUCCESS) { |
| 113 | |
| 114 | status = psa_aead_decrypt(key_handle, alg, nonce, nonce_length, |
| 115 | additional_data, additional_data_length, |
| 116 | ciphertext, ciphertext_length, |
| 117 | plaintext, plaintext_size, &out_vec[0].len); |
| 118 | } |
| 119 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 120 | return status; |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 121 | } |
| 122 | /*!@}*/ |