2021-03-08 16:46:35 +01:00
|
|
|
/*
|
|
|
|
* PSA cipher driver entry points
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
2022-08-02 12:44:35 +02:00
|
|
|
#include "psa_crypto_cipher.h"
|
2020-12-14 14:56:02 +01:00
|
|
|
#include "psa_crypto_core.h"
|
2020-10-01 14:10:20 +02:00
|
|
|
#include "psa_crypto_random_impl.h"
|
|
|
|
|
2020-12-14 14:56:02 +01:00
|
|
|
#include "mbedtls/cipher.h"
|
2020-10-01 14:10:20 +02:00
|
|
|
#include "mbedtls/error.h"
|
2021-03-08 16:46:35 +01:00
|
|
|
|
2020-12-14 14:56:02 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-03-17 14:46:05 +01:00
|
|
|
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
psa_key_type_t key_type,
|
|
|
|
size_t key_bits,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_id_t *cipher_id)
|
2021-03-17 14:46:05 +01:00
|
|
|
{
|
|
|
|
mbedtls_cipher_mode_t mode;
|
|
|
|
mbedtls_cipher_id_t cipher_id_tmp;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_AEAD(alg)) {
|
|
|
|
alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0);
|
|
|
|
}
|
2021-03-17 14:46:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg)) {
|
|
|
|
switch (alg) {
|
2022-03-16 12:25:17 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_STREAM_CIPHER)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_ALG_STREAM_CIPHER:
|
|
|
|
mode = MBEDTLS_MODE_STREAM;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CTR)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_ALG_CTR:
|
|
|
|
mode = MBEDTLS_MODE_CTR;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CFB)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_ALG_CFB:
|
|
|
|
mode = MBEDTLS_MODE_CFB;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_OFB)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_ALG_OFB:
|
|
|
|
mode = MBEDTLS_MODE_OFB;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_ALG_ECB_NO_PADDING:
|
|
|
|
mode = MBEDTLS_MODE_ECB;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_ALG_CBC_NO_PADDING:
|
|
|
|
mode = MBEDTLS_MODE_CBC;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_ALG_CBC_PKCS7:
|
|
|
|
mode = MBEDTLS_MODE_CBC;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM_STAR_NO_TAG)
|
2021-10-14 12:23:06 +02:00
|
|
|
case PSA_ALG_CCM_STAR_NO_TAG:
|
2021-10-27 10:42:31 +02:00
|
|
|
mode = MBEDTLS_MODE_CCM_STAR_NO_TAG;
|
2021-10-14 12:23:06 +02:00
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
|
2023-01-11 14:50:10 +01:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
|
2021-03-17 14:46:05 +01:00
|
|
|
mode = MBEDTLS_MODE_CCM;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
|
2023-01-11 14:50:10 +01:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
|
2021-03-17 14:46:05 +01:00
|
|
|
mode = MBEDTLS_MODE_GCM;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
|
2023-01-11 14:50:10 +01:00
|
|
|
case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
|
2021-03-17 14:46:05 +01:00
|
|
|
mode = MBEDTLS_MODE_CHACHAPOLY;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
2021-03-17 14:46:05 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return NULL;
|
2021-03-17 14:46:05 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (alg == PSA_ALG_CMAC) {
|
2021-03-17 14:46:05 +01:00
|
|
|
mode = MBEDTLS_MODE_ECB;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-03-17 14:46:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (key_type) {
|
2022-03-16 12:25:17 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_KEY_TYPE_AES:
|
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARIA)
|
2021-09-21 11:59:39 +02:00
|
|
|
case PSA_KEY_TYPE_ARIA:
|
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_KEY_TYPE_DES:
|
|
|
|
/* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
|
|
|
|
* and 192 for three-key Triple-DES. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_bits == 64) {
|
2021-03-17 14:46:05 +01:00
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-03-17 14:46:05 +01:00
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-17 14:46:05 +01:00
|
|
|
/* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
|
|
|
|
* but two-key Triple-DES is functionally three-key Triple-DES
|
|
|
|
* with K1=K3, so that's how we present it to mbedtls. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_bits == 128) {
|
2021-03-17 14:46:05 +01:00
|
|
|
key_bits = 192;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-17 14:46:05 +01:00
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_KEY_TYPE_CAMELLIA:
|
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
|
2021-03-17 14:46:05 +01:00
|
|
|
case PSA_KEY_TYPE_CHACHA20:
|
|
|
|
cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
|
|
|
|
break;
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif
|
2021-03-17 14:46:05 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return NULL;
|
2021-03-17 14:46:05 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cipher_id != NULL) {
|
2021-03-17 14:46:05 +01:00
|
|
|
*cipher_id = cipher_id_tmp;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-17 14:46:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_cipher_info_from_values(cipher_id_tmp,
|
|
|
|
(int) key_bits, mode);
|
2021-03-17 14:46:05 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
|
2021-03-10 14:43:20 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
static psa_status_t psa_cipher_setup(
|
2021-03-10 09:58:47 +01:00
|
|
|
mbedtls_psa_cipher_operation_t *operation,
|
2020-12-14 14:56:02 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_operation_t cipher_operation)
|
2020-12-14 14:56:02 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
size_t key_bits;
|
|
|
|
const mbedtls_cipher_info_t *cipher_info = NULL;
|
|
|
|
psa_key_type_t key_type = attributes->core.type;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) key_buffer_size;
|
2020-12-14 14:56:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_init(&operation->ctx.cipher);
|
2020-12-14 14:56:02 +01:00
|
|
|
|
2021-03-10 09:58:47 +01:00
|
|
|
operation->alg = alg;
|
2020-12-14 14:56:02 +01:00
|
|
|
key_bits = attributes->core.bits;
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_psa(alg, key_type,
|
|
|
|
key_bits, NULL);
|
|
|
|
if (cipher_info == NULL) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2020-12-14 14:56:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_cipher_setup(&operation->ctx.cipher, cipher_info);
|
|
|
|
if (ret != 0) {
|
2020-12-14 14:56:02 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-12-14 14:56:02 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_type == PSA_KEY_TYPE_DES && key_bits == 128) {
|
2020-12-14 14:56:02 +01:00
|
|
|
/* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
|
|
|
|
uint8_t keys[24];
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(keys, key_buffer, 16);
|
|
|
|
memcpy(keys + 16, key_buffer, 8);
|
|
|
|
ret = mbedtls_cipher_setkey(&operation->ctx.cipher,
|
|
|
|
keys,
|
|
|
|
192, cipher_operation);
|
|
|
|
} else
|
2020-12-14 14:56:02 +01:00
|
|
|
#endif
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_cipher_setkey(&operation->ctx.cipher, key_buffer,
|
|
|
|
(int) key_bits, cipher_operation);
|
2020-12-14 14:56:02 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2020-12-14 14:56:02 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-12-14 14:56:02 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (alg) {
|
2020-12-14 14:56:02 +01:00
|
|
|
case PSA_ALG_CBC_NO_PADDING:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_cipher_set_padding_mode(&operation->ctx.cipher,
|
|
|
|
MBEDTLS_PADDING_NONE);
|
2020-12-14 14:56:02 +01:00
|
|
|
break;
|
|
|
|
case PSA_ALG_CBC_PKCS7:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_cipher_set_padding_mode(&operation->ctx.cipher,
|
|
|
|
MBEDTLS_PADDING_PKCS7);
|
2020-12-14 14:56:02 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* The algorithm doesn't involve padding. */
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2020-12-14 14:56:02 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING ||
|
|
|
|
MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
|
2020-12-14 14:56:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
operation->block_length = (PSA_ALG_IS_STREAM_CIPHER(alg) ? 1 :
|
|
|
|
PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type));
|
|
|
|
operation->iv_length = PSA_CIPHER_IV_LENGTH(key_type, alg);
|
2020-12-14 14:56:02 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_to_psa_error(ret);
|
2020-12-14 14:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_encrypt_setup(
|
2021-03-10 09:58:47 +01:00
|
|
|
mbedtls_psa_cipher_operation_t *operation,
|
2020-12-14 14:56:02 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg)
|
2020-12-14 14:56:02 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return psa_cipher_setup(operation, attributes,
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
alg, MBEDTLS_ENCRYPT);
|
2020-12-14 14:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_decrypt_setup(
|
2021-03-10 09:58:47 +01:00
|
|
|
mbedtls_psa_cipher_operation_t *operation,
|
2020-12-14 14:56:02 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg)
|
2020-12-14 14:56:02 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return psa_cipher_setup(operation, attributes,
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
alg, MBEDTLS_DECRYPT);
|
2020-12-14 14:56:02 +01:00
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_set_iv(
|
|
|
|
mbedtls_psa_cipher_operation_t *operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint8_t *iv, size_t iv_length)
|
2021-03-12 10:35:18 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (iv_length != operation->iv_length) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
2021-03-12 10:35:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_to_psa_error(
|
|
|
|
mbedtls_cipher_set_iv(&operation->ctx.cipher,
|
|
|
|
iv, iv_length));
|
2021-03-12 10:35:18 +01:00
|
|
|
}
|
|
|
|
|
2022-03-16 12:25:17 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING)
|
2021-09-13 09:33:28 +02:00
|
|
|
/** Process input for which the algorithm is set to ECB mode.
|
|
|
|
*
|
|
|
|
* This requires manual processing, since the PSA API is defined as being
|
|
|
|
* able to process arbitrary-length calls to psa_cipher_update() with ECB mode,
|
|
|
|
* but the underlying mbedtls_cipher_update only takes full blocks.
|
|
|
|
*
|
|
|
|
* \param ctx The mbedtls cipher context to use. It must have been
|
|
|
|
* set up for ECB.
|
|
|
|
* \param[in] input The input plaintext or ciphertext to process.
|
|
|
|
* \param input_length The number of bytes to process from \p input.
|
|
|
|
* This does not need to be aligned to a block boundary.
|
|
|
|
* If there is a partial block at the end of the input,
|
|
|
|
* it is stored in \p ctx for future processing.
|
2021-09-13 12:20:51 +02:00
|
|
|
* \param output The buffer where the output is written. It must be
|
|
|
|
* at least `BS * floor((p + input_length) / BS)` bytes
|
|
|
|
* long, where `p` is the number of bytes in the
|
|
|
|
* unprocessed partial block in \p ctx (with
|
|
|
|
* `0 <= p <= BS - 1`) and `BS` is the block size.
|
2021-09-13 09:33:28 +02:00
|
|
|
* \param output_length On success, the number of bytes written to \p output.
|
|
|
|
* \c 0 on error.
|
|
|
|
*
|
|
|
|
* \return #PSA_SUCCESS or an error from a hardware accelerator
|
|
|
|
*/
|
2020-10-01 14:10:20 +02:00
|
|
|
static psa_status_t psa_cipher_update_ecb(
|
|
|
|
mbedtls_cipher_context_t *ctx,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
uint8_t *output,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *output_length)
|
2020-10-01 14:10:20 +02:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t block_size = ctx->cipher_info->block_size;
|
|
|
|
size_t internal_output_length = 0;
|
|
|
|
*output_length = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_length == 0) {
|
2020-10-01 14:10:20 +02:00
|
|
|
status = PSA_SUCCESS;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->unprocessed_len > 0) {
|
2020-10-01 14:10:20 +02:00
|
|
|
/* Fill up to block size, and run the block if there's a full one. */
|
|
|
|
size_t bytes_to_copy = block_size - ctx->unprocessed_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_length < bytes_to_copy) {
|
2020-10-01 14:10:20 +02:00
|
|
|
bytes_to_copy = input_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]),
|
|
|
|
input, bytes_to_copy);
|
2020-10-01 14:10:20 +02:00
|
|
|
input_length -= bytes_to_copy;
|
|
|
|
input += bytes_to_copy;
|
|
|
|
ctx->unprocessed_len += bytes_to_copy;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->unprocessed_len == block_size) {
|
2020-10-01 14:10:20 +02:00
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_update(ctx,
|
|
|
|
ctx->unprocessed_data,
|
|
|
|
block_size,
|
|
|
|
output, &internal_output_length));
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2020-10-01 14:10:20 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
|
|
output += internal_output_length;
|
|
|
|
*output_length += internal_output_length;
|
|
|
|
ctx->unprocessed_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (input_length >= block_size) {
|
2020-10-01 14:10:20 +02:00
|
|
|
/* Run all full blocks we have, one by one */
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_update(ctx, input,
|
|
|
|
block_size,
|
|
|
|
output, &internal_output_length));
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2020-10-01 14:10:20 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
|
|
input_length -= block_size;
|
|
|
|
input += block_size;
|
|
|
|
|
|
|
|
output += internal_output_length;
|
|
|
|
*output_length += internal_output_length;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_length > 0) {
|
2020-10-01 14:10:20 +02:00
|
|
|
/* Save unprocessed bytes for later processing */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]),
|
|
|
|
input, input_length);
|
2020-10-01 14:10:20 +02:00
|
|
|
ctx->unprocessed_len += input_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = PSA_SUCCESS;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-10-01 14:10:20 +02:00
|
|
|
}
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING */
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_update(
|
|
|
|
mbedtls_psa_cipher_operation_t *operation,
|
|
|
|
const uint8_t *input, size_t input_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *output, size_t output_size, size_t *output_length)
|
2020-10-01 14:10:20 +02:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t expected_output_size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_STREAM_CIPHER(operation->alg)) {
|
2020-10-01 14:10:20 +02:00
|
|
|
/* Take the unprocessed partial block left over from previous
|
|
|
|
* update calls, if any, plus the input to this call. Remove
|
|
|
|
* the last partial block, if any. You get the data that will be
|
|
|
|
* output in this call. */
|
|
|
|
expected_output_size =
|
2023-01-11 14:50:10 +01:00
|
|
|
(operation->ctx.cipher.unprocessed_len + input_length)
|
2021-03-26 09:29:09 +01:00
|
|
|
/ operation->block_length * operation->block_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2020-10-01 14:10:20 +02:00
|
|
|
expected_output_size = input_length;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (output_size < expected_output_size) {
|
|
|
|
return PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2022-03-16 12:25:17 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->alg == PSA_ALG_ECB_NO_PADDING) {
|
2020-10-01 14:10:20 +02:00
|
|
|
/* mbedtls_cipher_update has an API inconsistency: it will only
|
2023-01-11 14:50:10 +01:00
|
|
|
* process a single block at a time in ECB mode. Abstract away that
|
|
|
|
* inconsistency here to match the PSA API behaviour. */
|
|
|
|
status = psa_cipher_update_ecb(&operation->ctx.cipher,
|
|
|
|
input,
|
|
|
|
input_length,
|
|
|
|
output,
|
|
|
|
output_length);
|
|
|
|
} else
|
2022-03-16 12:25:17 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING */
|
2020-10-01 14:10:20 +02:00
|
|
|
{
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_update(&operation->ctx.cipher, input,
|
|
|
|
input_length, output, output_length));
|
2021-06-29 16:41:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*output_length > output_size) {
|
|
|
|
return PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-10-01 14:10:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_finish(
|
|
|
|
mbedtls_psa_cipher_operation_t *operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *output, size_t output_size, size_t *output_length)
|
2020-10-01 14:10:20 +02:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->ctx.cipher.unprocessed_len != 0) {
|
|
|
|
if (operation->alg == PSA_ALG_ECB_NO_PADDING ||
|
|
|
|
operation->alg == PSA_ALG_CBC_NO_PADDING) {
|
2020-10-01 14:10:20 +02:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_finish(&operation->ctx.cipher,
|
|
|
|
temp_output_buffer,
|
|
|
|
output_length));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-10-01 14:10:20 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*output_length == 0) {
|
2020-10-01 14:10:20 +02:00
|
|
|
; /* Nothing to copy. Note that output may be NULL in this case. */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (output_size >= *output_length) {
|
|
|
|
memcpy(output, temp_output_buffer, *output_length);
|
|
|
|
} else {
|
2020-10-01 14:10:20 +02:00
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-10-01 14:10:20 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(temp_output_buffer,
|
|
|
|
sizeof(temp_output_buffer));
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-10-01 14:10:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_abort(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_cipher_operation_t *operation)
|
2020-10-01 14:10:20 +02:00
|
|
|
{
|
2021-03-10 09:17:32 +01:00
|
|
|
/* Sanity check (shouldn't happen: operation->alg should
|
|
|
|
* always have been initialized to a valid value). */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!PSA_ALG_IS_CIPHER(operation->alg)) {
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
}
|
2021-03-10 09:17:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_free(&operation->ctx.cipher);
|
2020-10-01 14:10:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2020-10-01 14:10:20 +02:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_encrypt(
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg,
|
2021-12-14 10:58:18 +01:00
|
|
|
const uint8_t *iv,
|
|
|
|
size_t iv_length,
|
2021-03-13 18:50:11 +01:00
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *output_length)
|
2021-03-25 11:17:10 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
|
2021-07-09 09:19:35 +02:00
|
|
|
size_t update_output_length, finish_output_length;
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_cipher_encrypt_setup(&operation, attributes,
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (iv_length > 0) {
|
|
|
|
status = mbedtls_psa_cipher_set_iv(&operation, iv, iv_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_cipher_update(&operation, input, input_length,
|
|
|
|
output, output_size,
|
|
|
|
&update_output_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2022-11-23 14:15:57 +01:00
|
|
|
status = mbedtls_psa_cipher_finish(
|
|
|
|
&operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_buffer_offset(output, update_output_length),
|
|
|
|
output_size - update_output_length, &finish_output_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2021-07-09 09:19:35 +02:00
|
|
|
*output_length = update_output_length + finish_output_length;
|
2021-06-25 15:23:05 +02:00
|
|
|
|
2021-03-25 11:17:10 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
status = mbedtls_psa_cipher_abort(&operation);
|
|
|
|
} else {
|
|
|
|
mbedtls_psa_cipher_abort(&operation);
|
|
|
|
}
|
2021-03-13 18:50:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-03-25 11:17:10 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_cipher_decrypt(
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *output_length)
|
2021-03-25 11:17:10 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
|
2021-06-25 15:23:05 +02:00
|
|
|
size_t olength, accumulated_length;
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_cipher_decrypt_setup(&operation, attributes,
|
|
|
|
key_buffer, key_buffer_size,
|
|
|
|
alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation.iv_length > 0) {
|
|
|
|
status = mbedtls_psa_cipher_set_iv(&operation,
|
|
|
|
input, operation.iv_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:15:57 +01:00
|
|
|
status = mbedtls_psa_cipher_update(
|
|
|
|
&operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_buffer_offset_const(input, operation.iv_length),
|
2022-11-23 14:15:57 +01:00
|
|
|
input_length - operation.iv_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
output, output_size, &olength);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2021-06-29 16:42:13 +02:00
|
|
|
accumulated_length = olength;
|
2021-06-25 15:25:38 +02:00
|
|
|
|
2022-11-23 14:15:57 +01:00
|
|
|
status = mbedtls_psa_cipher_finish(
|
|
|
|
&operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_buffer_offset(output, accumulated_length),
|
|
|
|
output_size - accumulated_length, &olength);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-25 11:17:10 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2021-06-29 19:06:30 +02:00
|
|
|
*output_length = accumulated_length + olength;
|
2021-06-25 15:23:05 +02:00
|
|
|
|
2021-03-25 11:17:10 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
status = mbedtls_psa_cipher_abort(&operation);
|
|
|
|
} else {
|
|
|
|
mbedtls_psa_cipher_abort(&operation);
|
|
|
|
}
|
2021-03-25 11:17:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-03-25 11:17:10 +01:00
|
|
|
}
|
2021-03-10 14:43:20 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
|
2021-03-12 10:35:18 +01:00
|
|
|
|
2021-03-08 16:46:35 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|