2020-05-26 00:33:31 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2020-06-15 11:59:37 +02:00
|
|
|
* 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.
|
2020-05-26 00:33:31 +02:00
|
|
|
*/
|
|
|
|
|
2018-07-11 12:44:41 +02:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2019-02-12 17:40:27 +01:00
|
|
|
#include <stdlib.h>
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ASSERT(predicate) \
|
2018-07-11 12:44:41 +02:00
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!(predicate)) \
|
2018-07-11 12:44:41 +02:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("\tassertion failed at %s:%d - '%s'\r\n", \
|
|
|
|
__FILE__, __LINE__, #predicate); \
|
2018-07-11 12:44:41 +02:00
|
|
|
goto exit; \
|
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (0)
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ASSERT_STATUS(actual, expected) \
|
2018-07-11 12:44:41 +02:00
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((actual) != (expected)) \
|
2018-07-11 12:44:41 +02:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("\tassertion failed at %s:%d - " \
|
|
|
|
"actual:%d expected:%d\r\n", __FILE__, __LINE__, \
|
|
|
|
(psa_status_t) actual, (psa_status_t) expected); \
|
2018-07-11 12:44:41 +02:00
|
|
|
goto exit; \
|
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (0)
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2018-07-17 08:28:11 +02:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_AES_C) || \
|
|
|
|
!defined(MBEDTLS_CIPHER_MODE_CBC) || !defined(MBEDTLS_CIPHER_MODE_CTR) || \
|
2020-09-16 16:49:27 +02:00
|
|
|
!defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) || \
|
|
|
|
defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2018-07-17 08:28:11 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_AES_C and/or "
|
|
|
|
"MBEDTLS_CIPHER_MODE_CBC and/or MBEDTLS_CIPHER_MODE_CTR "
|
|
|
|
"and/or MBEDTLS_CIPHER_MODE_WITH_PADDING "
|
|
|
|
"not defined and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER"
|
|
|
|
" defined.\r\n");
|
|
|
|
return 0;
|
2018-07-17 08:28:11 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t cipher_operation(psa_cipher_operation_t *operation,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_size,
|
|
|
|
size_t part_size,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_len)
|
2018-07-11 12:44:41 +02:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
size_t bytes_to_write = 0, bytes_written = 0, len = 0;
|
|
|
|
|
|
|
|
*output_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (bytes_written != input_size) {
|
|
|
|
bytes_to_write = (input_size - bytes_written > part_size ?
|
|
|
|
part_size :
|
|
|
|
input_size - bytes_written);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_update(operation, input + bytes_written,
|
|
|
|
bytes_to_write, output + *output_len,
|
|
|
|
output_size - *output_len, &len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
|
|
|
bytes_written += bytes_to_write;
|
|
|
|
*output_len += len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_finish(operation, output + *output_len,
|
|
|
|
output_size - *output_len, &len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
*output_len += len;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2018-07-11 12:44:41 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t cipher_encrypt(psa_key_id_t key,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
uint8_t *iv,
|
|
|
|
size_t iv_size,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_size,
|
|
|
|
size_t part_size,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_len)
|
2018-07-11 12:44:41 +02:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-02-20 11:40:20 +01:00
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2018-07-11 12:44:41 +02:00
|
|
|
size_t iv_len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&operation, 0, sizeof(operation));
|
|
|
|
status = psa_cipher_encrypt_setup(&operation, key, alg);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_generate_iv(&operation, iv, iv_size, &iv_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_operation(&operation, input, input_size, part_size,
|
|
|
|
output, output_size, output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_cipher_abort(&operation);
|
|
|
|
return status;
|
2018-07-11 12:44:41 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t cipher_decrypt(psa_key_id_t key,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *iv,
|
|
|
|
size_t iv_size,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_size,
|
|
|
|
size_t part_size,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_len)
|
2018-07-11 12:44:41 +02:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2019-02-20 11:40:20 +01:00
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&operation, 0, sizeof(operation));
|
|
|
|
status = psa_cipher_decrypt_setup(&operation, key, alg);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_cipher_set_iv(&operation, iv, iv_size);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_operation(&operation, input, input_size, part_size,
|
|
|
|
output, output_size, output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_cipher_abort(&operation);
|
|
|
|
return status;
|
2018-07-11 12:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static psa_status_t
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block(void)
|
2018-07-11 12:44:41 +02:00
|
|
|
{
|
|
|
|
enum {
|
2023-01-11 14:50:10 +01:00
|
|
|
block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES),
|
2018-07-11 12:44:41 +02:00
|
|
|
key_bits = 256,
|
|
|
|
part_size = block_size,
|
|
|
|
};
|
2018-08-21 14:02:45 +02:00
|
|
|
const psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
|
2018-07-11 12:44:41 +02:00
|
|
|
|
|
|
|
psa_status_t status;
|
2019-04-18 13:39:40 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2020-09-16 16:49:27 +02:00
|
|
|
psa_key_id_t key = 0;
|
2018-07-11 12:44:41 +02:00
|
|
|
size_t output_len = 0;
|
|
|
|
uint8_t iv[block_size];
|
|
|
|
uint8_t input[block_size];
|
|
|
|
uint8_t encrypt[block_size];
|
|
|
|
uint8_t decrypt[block_size];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_random(input, sizeof(input));
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
|
|
|
psa_set_key_bits(&attributes, key_bits);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_key(&attributes, &key);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_encrypt(key, alg, iv, sizeof(iv),
|
|
|
|
input, sizeof(input), part_size,
|
|
|
|
encrypt, sizeof(encrypt), &output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_decrypt(key, alg, iv, sizeof(iv),
|
|
|
|
encrypt, output_len, part_size,
|
|
|
|
decrypt, sizeof(decrypt), &output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = memcmp(input, decrypt, sizeof(input));
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-11 12:44:41 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
return status;
|
2018-07-11 12:44:41 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void)
|
2018-07-12 09:27:58 +02:00
|
|
|
{
|
|
|
|
enum {
|
2023-01-11 14:50:10 +01:00
|
|
|
block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES),
|
2018-07-12 09:27:58 +02:00
|
|
|
key_bits = 256,
|
|
|
|
input_size = 100,
|
|
|
|
part_size = 10,
|
|
|
|
};
|
|
|
|
|
2018-08-21 14:02:45 +02:00
|
|
|
const psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
|
2018-07-12 09:27:58 +02:00
|
|
|
|
|
|
|
psa_status_t status;
|
2019-04-18 13:39:40 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2020-09-16 16:49:27 +02:00
|
|
|
psa_key_id_t key = 0;
|
2018-07-12 09:27:58 +02:00
|
|
|
size_t output_len = 0;
|
|
|
|
uint8_t iv[block_size], input[input_size],
|
|
|
|
encrypt[input_size + block_size], decrypt[input_size + block_size];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_random(input, sizeof(input));
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 09:27:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
|
|
|
psa_set_key_bits(&attributes, key_bits);
|
2018-07-12 09:27:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_key(&attributes, &key);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 09:27:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_encrypt(key, alg, iv, sizeof(iv),
|
|
|
|
input, sizeof(input), part_size,
|
|
|
|
encrypt, sizeof(encrypt), &output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 09:27:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_decrypt(key, alg, iv, sizeof(iv),
|
|
|
|
encrypt, output_len, part_size,
|
|
|
|
decrypt, sizeof(decrypt), &output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 09:27:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = memcmp(input, decrypt, sizeof(input));
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 09:27:58 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
return status;
|
2018-07-12 09:27:58 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void)
|
2018-07-12 12:06:41 +02:00
|
|
|
{
|
|
|
|
enum {
|
2023-01-11 14:50:10 +01:00
|
|
|
block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES),
|
2018-07-12 12:06:41 +02:00
|
|
|
key_bits = 256,
|
|
|
|
input_size = 100,
|
|
|
|
part_size = 10,
|
|
|
|
};
|
|
|
|
const psa_algorithm_t alg = PSA_ALG_CTR;
|
|
|
|
|
|
|
|
psa_status_t status;
|
2019-04-18 13:39:40 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2020-09-16 16:49:27 +02:00
|
|
|
psa_key_id_t key = 0;
|
2018-07-12 12:06:41 +02:00
|
|
|
size_t output_len = 0;
|
|
|
|
uint8_t iv[block_size], input[input_size], encrypt[input_size],
|
|
|
|
decrypt[input_size];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_random(input, sizeof(input));
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 12:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
|
|
|
psa_set_key_bits(&attributes, key_bits);
|
2018-07-12 12:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_key(&attributes, &key);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 12:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_encrypt(key, alg, iv, sizeof(iv),
|
|
|
|
input, sizeof(input), part_size,
|
|
|
|
encrypt, sizeof(encrypt), &output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 12:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cipher_decrypt(key, alg, iv, sizeof(iv),
|
|
|
|
encrypt, output_len, part_size,
|
|
|
|
decrypt, sizeof(decrypt), &output_len);
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 12:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = memcmp(input, decrypt, sizeof(input));
|
|
|
|
ASSERT_STATUS(status, PSA_SUCCESS);
|
2018-07-12 12:06:41 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
|
|
|
return status;
|
2018-07-12 12:06:41 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void cipher_examples(void)
|
2018-07-11 12:44:41 +02:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("cipher encrypt/decrypt AES CBC no padding:\r\n");
|
|
|
|
status = cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block();
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
printf("\tsuccess!\r\n");
|
|
|
|
}
|
2018-07-12 09:27:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n");
|
|
|
|
status = cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi();
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
printf("\tsuccess!\r\n");
|
|
|
|
}
|
2018-07-12 12:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("cipher encrypt/decrypt AES CTR multipart:\r\n");
|
|
|
|
status = cipher_example_encrypt_decrypt_aes_ctr_multi();
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
printf("\tsuccess!\r\n");
|
|
|
|
}
|
2018-07-11 12:44:41 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2018-07-10 09:10:21 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT(psa_crypto_init() == PSA_SUCCESS);
|
|
|
|
cipher_examples();
|
2018-07-11 12:44:41 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_crypto_free();
|
|
|
|
return 0;
|
2018-07-10 09:10:21 +02:00
|
|
|
}
|
2018-07-17 08:28:11 +02:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC &&
|
|
|
|
MBEDTLS_CIPHER_MODE_CTR && MBEDTLS_CIPHER_MODE_WITH_PADDING */
|