blob: 533af34fc5de16087625675d1fa462e47fe3f3e0 [file] [log] [blame]
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +01001/**
2 * Cipher API multi-part AEAD demonstration.
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +01003 *
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +01004 * This program AEAD-encrypts a message, using the algorithm and key size
5 * specified on the command line, using the multi-part API.
6 *
Manuel Pégourié-Gonnard6fdc9e82022-01-31 13:27:39 +01007 * It comes with a companion program psa/aead_demo.c, which does the same
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +01008 * operations with the PSA Crypto API. The goal is that comparing the two
9 * programs will help people migrating to the PSA Crypto API.
10 *
11 * When used with multi-part AEAD operations, the `mbedtls_cipher_context`
12 * serves a triple purpose (1) hold the key, (2) store the algorithm when no
13 * operation is active, and (3) save progress information for the current
14 * operation. With PSA those roles are held by disinct objects: (1) a
15 * psa_key_id_t to hold the key, a (2) psa_algorithm_t to represent the
16 * algorithm, and (3) a psa_operation_t for multi-part progress.
17 *
18 * On the other hand, with PSA, the algorithms encodes the desired tag length;
19 * with Cipher the desired tag length needs to be tracked separately.
20 *
Manuel Pégourié-Gonnard6fdc9e82022-01-31 13:27:39 +010021 * This program and its companion psa/aead_demo.c illustrate this by doing the
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010022 * same sequence of multi-part AEAD computation with both APIs; looking at the
23 * two side by side should make the differences and similarities clear.
24 */
25
26/*
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010027 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000028 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010029 */
30
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010031/* First include Mbed TLS headers to get the Mbed TLS configuration and
32 * platform definitions that we'll use in this program. Also include
33 * standard C headers for functions we'll use here. */
Felix Conway998760a2025-03-24 11:37:33 +000034#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
35
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010036#include "mbedtls/build_info.h"
37
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010038#include "mbedtls/cipher.h"
39
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43
44/* If the build options we need are not enabled, compile a placeholder. */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010045#if !defined(MBEDTLS_CIPHER_C) || \
46 !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \
47 !defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010048int main(void)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010049{
Gilles Peskine449bd832023-01-11 14:50:10 +010050 printf("MBEDTLS_MD_C and/or "
51 "MBEDTLS_AES_C and/or MBEDTLS_GCM_C and/or "
52 "MBEDTLS_CHACHAPOLY_C not defined\r\n");
53 return 0;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010054}
55#else
56
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010057/* The real program starts here. */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010058
Manuel Pégourié-Gonnard64754e12022-02-08 11:21:14 +010059const char usage[] =
Gilles Peskine449bd832023-01-11 14:50:10 +010060 "Usage: cipher_aead_demo [aes128-gcm|aes256-gcm|aes128-gcm_8|chachapoly]";
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010061
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010062/* Dummy data for encryption: IV/nonce, additional data, 2-part message */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010063const unsigned char iv1[12] = { 0x00 };
64const unsigned char add_data1[] = { 0x01, 0x02 };
65const unsigned char msg1_part1[] = { 0x03, 0x04 };
66const unsigned char msg1_part2[] = { 0x05, 0x06, 0x07 };
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010067
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010068/* Dummy data (2nd message) */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010069const unsigned char iv2[12] = { 0x10 };
70const unsigned char add_data2[] = { 0x11, 0x12 };
71const unsigned char msg2_part1[] = { 0x13, 0x14 };
72const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 };
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010073
Manuel Pégourié-Gonnard48bae022022-02-08 11:14:58 +010074/* Maximum total size of the messages */
Gilles Peskine449bd832023-01-11 14:50:10 +010075#define MSG1_SIZE (sizeof(msg1_part1) + sizeof(msg1_part2))
76#define MSG2_SIZE (sizeof(msg2_part1) + sizeof(msg2_part2))
77#define MSG_MAX_SIZE (MSG1_SIZE > MSG2_SIZE ? MSG1_SIZE : MSG2_SIZE)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010078
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010079/* Dummy key material - never do this in production!
80 * 32-byte is enough to all the key size supported by this program. */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010081const unsigned char key_bytes[32] = { 0x2a };
82
Michael Schusterc1cd26b2024-07-21 00:44:33 +020083/* Print the contents of a buffer in hex */
Michael Schuster9e52d152024-07-21 00:46:10 +020084static void print_buf(const char *title, unsigned char *buf, size_t len)
Michael Schusterc1cd26b2024-07-21 00:44:33 +020085{
86 printf("%s:", title);
87 for (size_t i = 0; i < len; i++) {
88 printf(" %02x", buf[i]);
89 }
90 printf("\n");
91}
92
Manuel Pégourié-Gonnard340808c2022-02-08 11:15:26 +010093/* Run an Mbed TLS function and bail out if it fails.
94 * A string description of the error code can be recovered with:
95 * programs/util/strerror <value> */
Gilles Peskine449bd832023-01-11 14:50:10 +010096#define CHK(expr) \
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010097 do \
98 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010099 ret = (expr); \
100 if (ret != 0) \
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100101 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 printf("Error %d at line %d: %s\n", \
103 ret, \
104 __LINE__, \
105 #expr); \
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100106 goto exit; \
107 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 } while (0)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100109
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100110/*
111 * Prepare encryption material:
112 * - interpret command-line argument
113 * - set up key
114 * - outputs: context and tag length, which together hold all the information
115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116static int aead_prepare(const char *info,
117 mbedtls_cipher_context_t *ctx,
118 size_t *tag_len)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100119{
120 int ret;
121
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100122 /* Convert arg to type + tag_len */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100123 mbedtls_cipher_type_t type;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (strcmp(info, "aes128-gcm") == 0) {
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100125 type = MBEDTLS_CIPHER_AES_128_GCM;
126 *tag_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 } else if (strcmp(info, "aes256-gcm") == 0) {
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100128 type = MBEDTLS_CIPHER_AES_256_GCM;
129 *tag_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 } else if (strcmp(info, "aes128-gcm_8") == 0) {
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100131 type = MBEDTLS_CIPHER_AES_128_GCM;
132 *tag_len = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 } else if (strcmp(info, "chachapoly") == 0) {
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100134 type = MBEDTLS_CIPHER_CHACHA20_POLY1305;
135 *tag_len = 16;
136 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 puts(usage);
138 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100139 }
140
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100141 /* Prepare context for the given type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 CHK(mbedtls_cipher_setup(ctx,
143 mbedtls_cipher_info_from_type(type)));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100144
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100145 /* Import key */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 int key_len = mbedtls_cipher_get_key_bitlen(ctx);
147 CHK(mbedtls_cipher_setkey(ctx, key_bytes, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100148
149exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return ret;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100151}
152
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100153/*
154 * Print out some information.
155 *
156 * All of this information was present in the command line argument, but his
157 * function demonstrates how each piece can be recovered from (ctx, tag_len).
158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159static void aead_info(const mbedtls_cipher_context_t *ctx, size_t tag_len)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100160{
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_cipher_type_t type = mbedtls_cipher_get_type(ctx);
162 const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type(type);
163 const char *ciph = mbedtls_cipher_info_get_name(info);
164 int key_bits = mbedtls_cipher_get_key_bitlen(ctx);
165 mbedtls_cipher_mode_t mode = mbedtls_cipher_get_cipher_mode(ctx);
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100166
167 const char *mode_str = mode == MBEDTLS_MODE_GCM ? "GCM"
168 : mode == MBEDTLS_MODE_CHACHAPOLY ? "ChachaPoly"
169 : "???";
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 printf("%s, %d, %s, %u\n",
172 ciph, key_bits, mode_str, (unsigned) tag_len);
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100173}
174
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100175/*
176 * Encrypt a 2-part message.
177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178static int aead_encrypt(mbedtls_cipher_context_t *ctx, size_t tag_len,
179 const unsigned char *iv, size_t iv_len,
180 const unsigned char *ad, size_t ad_len,
181 const unsigned char *part1, size_t part1_len,
182 const unsigned char *part2, size_t part2_len)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100183{
184 int ret;
185 size_t olen;
Manuel Pégourié-Gonnardae1bae82022-02-08 11:36:28 +0100186#define MAX_TAG_LENGTH 16
187 unsigned char out[MSG_MAX_SIZE + MAX_TAG_LENGTH];
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100188 unsigned char *p = out;
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 CHK(mbedtls_cipher_set_iv(ctx, iv, iv_len));
191 CHK(mbedtls_cipher_reset(ctx));
192 CHK(mbedtls_cipher_update_ad(ctx, ad, ad_len));
193 CHK(mbedtls_cipher_update(ctx, part1, part1_len, p, &olen));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100194 p += olen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 CHK(mbedtls_cipher_update(ctx, part2, part2_len, p, &olen));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100196 p += olen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 CHK(mbedtls_cipher_finish(ctx, p, &olen));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100198 p += olen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 CHK(mbedtls_cipher_write_tag(ctx, p, tag_len));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100200 p += tag_len;
201
202 olen = p - out;
Michael Schusterc1cd26b2024-07-21 00:44:33 +0200203 print_buf("out", out, olen);
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100204
205exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return ret;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100207}
208
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100209/*
210 * AEAD demo: set up key/alg, print out info, encrypt messages.
211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212static int aead_demo(const char *info)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100213{
214 int ret = 0;
215
216 mbedtls_cipher_context_t ctx;
217 size_t tag_len;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 CHK(aead_prepare(info, &ctx, &tag_len));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 aead_info(&ctx, tag_len);
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 CHK(aead_encrypt(&ctx, tag_len,
226 iv1, sizeof(iv1), add_data1, sizeof(add_data1),
227 msg1_part1, sizeof(msg1_part1),
228 msg1_part2, sizeof(msg1_part2)));
229 CHK(aead_encrypt(&ctx, tag_len,
230 iv2, sizeof(iv2), add_data2, sizeof(add_data2),
231 msg2_part1, sizeof(msg2_part1),
232 msg2_part2, sizeof(msg2_part2)));
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100233
234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return ret;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100238}
239
240
241/*
242 * Main function
243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int main(int argc, char **argv)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100245{
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100246 /* Check usage */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (argc != 2) {
248 puts(usage);
249 return 1;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100250 }
251
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100252 int ret;
253
254 /* Run the demo */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 CHK(aead_demo(argv[1]));
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100256
257exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100259}
260
261#endif