2022-01-31 12:56:39 +01:00
|
|
|
/**
|
|
|
|
* PSA API multi-part AEAD demonstration.
|
2022-01-07 12:26:32 +01:00
|
|
|
*
|
2022-01-31 12:56:39 +01:00
|
|
|
* This program AEAD-encrypts a message, using the algorithm and key size
|
|
|
|
* specified on the command line, using the multi-part API.
|
2022-01-07 12:26:32 +01:00
|
|
|
*
|
2022-02-08 11:21:14 +01:00
|
|
|
* It comes with a companion program cipher/cipher_aead_demo.c, which does the
|
|
|
|
* same operations with the legacy Cipher API. The goal is that comparing the
|
|
|
|
* two programs will help people migrating to the PSA Crypto API.
|
2022-01-07 12:26:32 +01:00
|
|
|
*
|
2022-01-27 11:15:33 +01:00
|
|
|
* When used with multi-part AEAD operations, the `mbedtls_cipher_context`
|
|
|
|
* serves a triple purpose (1) hold the key, (2) store the algorithm when no
|
|
|
|
* operation is active, and (3) save progress information for the current
|
|
|
|
* operation. With PSA those roles are held by disinct objects: (1) a
|
|
|
|
* psa_key_id_t to hold the key, a (2) psa_algorithm_t to represent the
|
|
|
|
* algorithm, and (3) a psa_operation_t for multi-part progress.
|
2022-01-07 12:26:32 +01:00
|
|
|
*
|
|
|
|
* On the other hand, with PSA, the algorithms encodes the desired tag length;
|
|
|
|
* with Cipher the desired tag length needs to be tracked separately.
|
|
|
|
*
|
2022-02-08 11:21:14 +01:00
|
|
|
* This program and its companion cipher/cipher_aead_demo.c illustrate this by
|
|
|
|
* doing the same sequence of multi-part AEAD computation with both APIs;
|
|
|
|
* looking at the two side by side should make the differences and
|
|
|
|
* similarities clear.
|
2022-01-07 12:26:32 +01:00
|
|
|
*/
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* First include Mbed TLS headers to get the Mbed TLS configuration and
|
|
|
|
* platform definitions that we'll use in this program. Also include
|
|
|
|
* standard C headers for functions we'll use here. */
|
2022-01-07 12:26:32 +01:00
|
|
|
#include "mbedtls/build_info.h"
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* If the build options we need are not enabled, compile a placeholder. */
|
2022-01-27 13:09:13 +01:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_C) || \
|
2022-01-07 12:26:32 +01:00
|
|
|
!defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \
|
2022-01-17 11:57:44 +01:00
|
|
|
!defined(MBEDTLS_CHACHAPOLY_C) || \
|
|
|
|
defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2022-01-07 12:26:32 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("MBEDTLS_PSA_CRYPTO_C and/or "
|
|
|
|
"MBEDTLS_AES_C and/or MBEDTLS_GCM_C and/or "
|
|
|
|
"MBEDTLS_CHACHAPOLY_C not defined, and/or "
|
|
|
|
"MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n");
|
|
|
|
return 0;
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* The real program starts here. */
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-02-08 11:21:14 +01:00
|
|
|
const char usage[] =
|
2023-01-11 14:50:10 +01:00
|
|
|
"Usage: aead_demo [aes128-gcm|aes256-gcm|aes128-gcm_8|chachapoly]";
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Dummy data for encryption: IV/nonce, additional data, 2-part message */
|
2022-01-07 12:26:32 +01:00
|
|
|
const unsigned char iv1[12] = { 0x00 };
|
2022-01-27 11:42:47 +01:00
|
|
|
const unsigned char add_data1[] = { 0x01, 0x02 };
|
|
|
|
const unsigned char msg1_part1[] = { 0x03, 0x04 };
|
|
|
|
const unsigned char msg1_part2[] = { 0x05, 0x06, 0x07 };
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Dummy data (2nd message) */
|
2022-01-07 12:26:32 +01:00
|
|
|
const unsigned char iv2[12] = { 0x10 };
|
2022-01-27 11:42:47 +01:00
|
|
|
const unsigned char add_data2[] = { 0x11, 0x12 };
|
|
|
|
const unsigned char msg2_part1[] = { 0x13, 0x14 };
|
|
|
|
const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 };
|
2022-01-27 11:56:24 +01:00
|
|
|
|
2022-02-08 11:14:58 +01:00
|
|
|
/* Maximum total size of the messages */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MSG1_SIZE (sizeof(msg1_part1) + sizeof(msg1_part2))
|
|
|
|
#define MSG2_SIZE (sizeof(msg2_part1) + sizeof(msg2_part2))
|
|
|
|
#define MSG_MAX_SIZE (MSG1_SIZE > MSG2_SIZE ? MSG1_SIZE : MSG2_SIZE)
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Dummy key material - never do this in production!
|
|
|
|
* 32-byte is enough to all the key size supported by this program. */
|
2022-01-07 12:26:32 +01:00
|
|
|
const unsigned char key_bytes[32] = { 0x2a };
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Print the contents of a buffer in hex */
|
2023-01-11 14:50:10 +01:00
|
|
|
void print_buf(const char *title, uint8_t *buf, size_t len)
|
2022-01-07 12:26:32 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("%s:", title);
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
printf(" %02x", buf[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 11:15:26 +01:00
|
|
|
/* Run a PSA function and bail out if it fails.
|
|
|
|
* The symbolic name of the error code can be recovered using:
|
2022-12-04 18:19:59 +01:00
|
|
|
* programs/psa/psa_constant_name status <value> */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define PSA_CHECK(expr) \
|
2022-01-31 12:56:39 +01:00
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
status = (expr); \
|
|
|
|
if (status != PSA_SUCCESS) \
|
2022-01-31 12:56:39 +01:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("Error %d at line %d: %s\n", \
|
|
|
|
(int) status, \
|
|
|
|
__LINE__, \
|
|
|
|
#expr); \
|
2022-01-31 12:56:39 +01:00
|
|
|
goto exit; \
|
|
|
|
} \
|
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
while (0)
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/*
|
|
|
|
* Prepare encryption material:
|
|
|
|
* - interpret command-line argument
|
|
|
|
* - set up key
|
|
|
|
* - outputs: key and algorithm, which together hold all the information
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t aead_prepare(const char *info,
|
|
|
|
psa_key_id_t *key,
|
|
|
|
psa_algorithm_t *alg)
|
2022-01-07 12:26:32 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Convert arg to alg + key_bits + key_type */
|
2022-01-07 12:26:32 +01:00
|
|
|
size_t key_bits;
|
|
|
|
psa_key_type_t key_type;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(info, "aes128-gcm") == 0) {
|
2022-01-07 12:26:32 +01:00
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
key_bits = 128;
|
|
|
|
key_type = PSA_KEY_TYPE_AES;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(info, "aes256-gcm") == 0) {
|
2022-01-07 12:26:32 +01:00
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
key_bits = 256;
|
|
|
|
key_type = PSA_KEY_TYPE_AES;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(info, "aes128-gcm_8") == 0) {
|
2022-01-07 12:26:32 +01:00
|
|
|
*alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8);
|
|
|
|
key_bits = 128;
|
|
|
|
key_type = PSA_KEY_TYPE_AES;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(info, "chachapoly") == 0) {
|
2022-01-07 12:26:32 +01:00
|
|
|
*alg = PSA_ALG_CHACHA20_POLY1305;
|
|
|
|
key_bits = 256;
|
|
|
|
key_type = PSA_KEY_TYPE_CHACHA20;
|
|
|
|
} else {
|
2023-01-11 14:50:10 +01:00
|
|
|
puts(usage);
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 20:28:52 +02:00
|
|
|
/* Prepare key attributes */
|
2022-01-07 12:26:32 +01:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, *alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
psa_set_key_bits(&attributes, key_bits); // optional
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Import key */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_import_key(&attributes, key_bytes, key_bits / 8, key));
|
2022-01-07 12:26:32 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/*
|
|
|
|
* Print out some information.
|
|
|
|
*
|
|
|
|
* All of this information was present in the command line argument, but his
|
|
|
|
* function demonstrates how each piece can be recovered from (key, alg).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void aead_info(psa_key_id_t key, psa_algorithm_t alg)
|
2022-01-07 12:26:32 +01:00
|
|
|
{
|
|
|
|
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) psa_get_key_attributes(key, &attr);
|
|
|
|
psa_key_type_t key_type = psa_get_key_type(&attr);
|
|
|
|
size_t key_bits = psa_get_key_bits(&attr);
|
|
|
|
psa_algorithm_t base_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
|
|
|
|
size_t tag_len = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
|
2022-01-07 12:26:32 +01:00
|
|
|
|
|
|
|
const char *type_str = key_type == PSA_KEY_TYPE_AES ? "AES"
|
|
|
|
: key_type == PSA_KEY_TYPE_CHACHA20 ? "Chacha"
|
|
|
|
: "???";
|
|
|
|
const char *base_str = base_alg == PSA_ALG_GCM ? "GCM"
|
|
|
|
: base_alg == PSA_ALG_CHACHA20_POLY1305 ? "ChachaPoly"
|
|
|
|
: "???";
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("%s, %u, %s, %u\n",
|
|
|
|
type_str, (unsigned) key_bits, base_str, (unsigned) tag_len);
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/*
|
|
|
|
* Encrypt a 2-part message.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int aead_encrypt(psa_key_id_t key, psa_algorithm_t alg,
|
|
|
|
const unsigned char *iv, size_t iv_len,
|
|
|
|
const unsigned char *ad, size_t ad_len,
|
|
|
|
const unsigned char *part1, size_t part1_len,
|
|
|
|
const unsigned char *part2, size_t part2_len)
|
2022-01-07 12:26:32 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
size_t olen, olen_tag;
|
2022-01-28 12:52:35 +01:00
|
|
|
unsigned char out[PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(MSG_MAX_SIZE)];
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *p = out, *end = out + sizeof(out);
|
2022-01-27 11:56:24 +01:00
|
|
|
unsigned char tag[PSA_AEAD_TAG_MAX_SIZE];
|
2022-01-07 12:26:32 +01:00
|
|
|
|
|
|
|
psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_aead_encrypt_setup(&op, key, alg));
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_aead_set_nonce(&op, iv, iv_len));
|
|
|
|
PSA_CHECK(psa_aead_update_ad(&op, ad, ad_len));
|
|
|
|
PSA_CHECK(psa_aead_update(&op, part1, part1_len, p, end - p, &olen));
|
2022-01-07 12:26:32 +01:00
|
|
|
p += olen;
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_aead_update(&op, part2, part2_len, p, end - p, &olen));
|
2022-01-07 12:26:32 +01:00
|
|
|
p += olen;
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_aead_finish(&op, p, end - p, &olen,
|
|
|
|
tag, sizeof(tag), &olen_tag));
|
2022-01-07 12:26:32 +01:00
|
|
|
p += olen;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, tag, olen_tag);
|
2022-01-07 12:26:32 +01:00
|
|
|
p += olen_tag;
|
|
|
|
|
|
|
|
olen = p - out;
|
2023-01-11 14:50:10 +01:00
|
|
|
print_buf("out", out, olen);
|
2022-01-27 12:17:20 +01:00
|
|
|
|
2022-01-07 12:26:32 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_aead_abort(&op); // required on errors, harmless on success
|
|
|
|
return status;
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/*
|
|
|
|
* AEAD demo: set up key/alg, print out info, encrypt messages.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t aead_demo(const char *info)
|
2022-01-07 12:26:32 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
psa_key_id_t key;
|
|
|
|
psa_algorithm_t alg;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(aead_prepare(info, &key, &alg));
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
aead_info(key, alg);
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(aead_encrypt(key, alg,
|
|
|
|
iv1, sizeof(iv1), add_data1, sizeof(add_data1),
|
|
|
|
msg1_part1, sizeof(msg1_part1),
|
|
|
|
msg1_part2, sizeof(msg1_part2)));
|
|
|
|
PSA_CHECK(aead_encrypt(key, alg,
|
|
|
|
iv2, sizeof(iv2), add_data2, sizeof(add_data2),
|
|
|
|
msg2_part1, sizeof(msg2_part1),
|
|
|
|
msg2_part2, sizeof(msg2_part2)));
|
2022-01-07 12:26:32 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_destroy_key(key);
|
2022-01-27 12:17:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main function
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(int argc, char **argv)
|
2022-01-07 12:26:32 +01:00
|
|
|
{
|
2022-01-31 12:56:39 +01:00
|
|
|
psa_status_t status = PSA_SUCCESS;
|
|
|
|
|
|
|
|
/* Check usage */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (argc != 2) {
|
|
|
|
puts(usage);
|
|
|
|
return EXIT_FAILURE;
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Initialize the PSA crypto library. */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_crypto_init());
|
2022-01-07 12:26:32 +01:00
|
|
|
|
2022-01-31 12:56:39 +01:00
|
|
|
/* Run the demo */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(aead_demo(argv[1]));
|
2022-01-31 12:56:39 +01:00
|
|
|
|
|
|
|
/* Deinitialize the PSA crypto library. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_crypto_free();
|
2022-01-31 12:56:39 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
|
2022-01-07 12:26:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|