blob: 4da1bfc5b49ec62c212da7458be7b1efcb47c2ae [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
28 * SPDX-License-Identifier: Apache-2.0
29 *
30 * Licensed under the Apache License, Version 2.0 (the "License"); you may
31 * not use this file except in compliance with the License.
32 * You may obtain a copy of the License at
33 *
34 * http://www.apache.org/licenses/LICENSE-2.0
35 *
36 * Unless required by applicable law or agreed to in writing, software
37 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
38 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 * See the License for the specific language governing permissions and
40 * limitations under the License.
41 */
42
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010043/* First include Mbed TLS headers to get the Mbed TLS configuration and
44 * platform definitions that we'll use in this program. Also include
45 * standard C headers for functions we'll use here. */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010046#include "mbedtls/build_info.h"
47
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010048#include "mbedtls/cipher.h"
49
50#include <stdlib.h>
51#include <stdio.h>
52#include <string.h>
53
54/* If the build options we need are not enabled, compile a placeholder. */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010055#if !defined(MBEDTLS_CIPHER_C) || \
56 !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \
57 !defined(MBEDTLS_CHACHAPOLY_C)
58int main( void )
59{
60 printf( "MBEDTLS_MD_C and/or "
61 "MBEDTLS_AES_C and/or MBEDTLS_GCM_C and/or "
62 "MBEDTLS_CHACHAPOLY_C not defined\r\n" );
63 return( 0 );
64}
65#else
66
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010067/* The real program starts here. */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010068
Manuel Pégourié-Gonnard29088a42022-02-01 09:38:26 +010069const char usage[] = "Usage: cipher_aead_demo [aes128-gcm|aes256-gcm|aes128-gcm_8|chachapoly]";
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010070
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010071/* Dummy data for encryption: IV/nonce, additional data, 2-part message */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010072const unsigned char iv1[12] = { 0x00 };
73const unsigned char add_data1[] = { 0x01, 0x02 };
74const unsigned char msg1_part1[] = { 0x03, 0x04 };
75const unsigned char msg1_part2[] = { 0x05, 0x06, 0x07 };
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010076
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010077/* Dummy data (2nd message) */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010078const unsigned char iv2[12] = { 0x10 };
79const unsigned char add_data2[] = { 0x11, 0x12 };
80const unsigned char msg2_part1[] = { 0x13, 0x14 };
81const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 };
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010082
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010083/* This must at least the sum of the length of the 2 parts for each message.
84 * This is a macro for the sake of compilers with insufficient C99 support. */
Manuel Pégourié-Gonnardfd1d13c2022-01-28 12:52:35 +010085#define MSG_MAX_SIZE 5
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010086
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010087/* Dummy key material - never do this in production!
88 * 32-byte is enough to all the key size supported by this program. */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010089const unsigned char key_bytes[32] = { 0x2a };
90
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010091/* Print the contents of a buffer in hex */
92void print_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010093{
94 printf( "%s:", title );
95 for( size_t i = 0; i < len; i++ )
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +010096 printf( " %02x", buf[i] );
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010097 printf( "\n" );
98}
99
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100100/* Run an Mbed TLS function and bail out if it fails. */
101#define CHK( expr ) \
102 do \
103 { \
104 ret = ( expr ); \
105 if( ret != 0 ) \
106 { \
107 printf( "Error %d at line %d: %s\n", \
108 ret, \
109 __LINE__, \
110 #expr ); \
111 goto exit; \
112 } \
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100113 } while( 0 )
114
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100115/*
116 * Prepare encryption material:
117 * - interpret command-line argument
118 * - set up key
119 * - outputs: context and tag length, which together hold all the information
120 */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100121static int aead_prepare( const char *info,
122 mbedtls_cipher_context_t *ctx,
123 size_t *tag_len )
124{
125 int ret;
126
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100127 /* Convert arg to type + tag_len */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100128 mbedtls_cipher_type_t type;
129 if( strcmp( info, "aes128-gcm" ) == 0 ) {
130 type = MBEDTLS_CIPHER_AES_128_GCM;
131 *tag_len = 16;
132 } else if( strcmp( info, "aes256-gcm" ) == 0 ) {
133 type = MBEDTLS_CIPHER_AES_256_GCM;
134 *tag_len = 16;
135 } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) {
136 type = MBEDTLS_CIPHER_AES_128_GCM;
137 *tag_len = 8;
138 } else if( strcmp( info, "chachapoly" ) == 0 ) {
139 type = MBEDTLS_CIPHER_CHACHA20_POLY1305;
140 *tag_len = 16;
141 } else {
142 puts( usage );
143 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
144 }
145
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100146 /* Prepare context for the given type */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100147 CHK( mbedtls_cipher_setup( ctx,
148 mbedtls_cipher_info_from_type( type ) ) );
149
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100150 /* Import key */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100151 int key_len = mbedtls_cipher_get_key_bitlen( ctx );
152 CHK( mbedtls_cipher_setkey( ctx, key_bytes, key_len, MBEDTLS_ENCRYPT ) );
153
154exit:
155 return( ret );
156}
157
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100158/*
159 * Print out some information.
160 *
161 * All of this information was present in the command line argument, but his
162 * function demonstrates how each piece can be recovered from (ctx, tag_len).
163 */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100164static void aead_info( const mbedtls_cipher_context_t *ctx, size_t tag_len )
165{
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100166 // no convenient way to get the just cipher type (for example, AES)
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100167 const char *ciph = "???";
168 int key_bits = mbedtls_cipher_get_key_bitlen( ctx );
169 mbedtls_cipher_mode_t mode = mbedtls_cipher_get_cipher_mode( ctx );
170
171 const char *mode_str = mode == MBEDTLS_MODE_GCM ? "GCM"
172 : mode == MBEDTLS_MODE_CHACHAPOLY ? "ChachaPoly"
173 : "???";
174
175 printf( "cipher: %s, %d, %s, %u\n", ciph, key_bits, mode_str, (unsigned) tag_len );
176}
177
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100178/*
179 * Encrypt a 2-part message.
180 */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100181static int aead_encrypt( mbedtls_cipher_context_t *ctx, size_t tag_len,
182 const unsigned char *iv, size_t iv_len,
183 const unsigned char *ad, size_t ad_len,
184 const unsigned char *pa, size_t pa_len,
185 const unsigned char *pb, size_t pb_len )
186{
187 int ret;
188 size_t olen;
Manuel Pégourié-Gonnardfd1d13c2022-01-28 12:52:35 +0100189 unsigned char out[MSG_MAX_SIZE + 16];
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100190 unsigned char *p = out;
191
192 CHK( mbedtls_cipher_set_iv( ctx, iv, iv_len ) );
193 CHK( mbedtls_cipher_reset( ctx ) );
194 CHK( mbedtls_cipher_update_ad( ctx, ad, ad_len ) );
195 CHK( mbedtls_cipher_update( ctx, pa, pa_len, p, &olen ) );
196 p += olen;
197 CHK( mbedtls_cipher_update( ctx, pb, pb_len, p, &olen ) );
198 p += olen;
199 CHK( mbedtls_cipher_finish( ctx, p, &olen ) );
200 p += olen;
201 CHK( mbedtls_cipher_write_tag( ctx, p, tag_len ) );
202 p += tag_len;
203
204 olen = p - out;
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100205 print_buf( "cipher", out, olen );
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100206
207exit:
208 return( ret );
209}
210
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100211/*
212 * AEAD demo: set up key/alg, print out info, encrypt messages.
213 */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100214static int aead_demo( const char *info )
215{
216 int ret = 0;
217
218 mbedtls_cipher_context_t ctx;
219 size_t tag_len;
220
221 mbedtls_cipher_init( &ctx );
222
223 CHK( aead_prepare( info, &ctx, &tag_len ) );
224
225 aead_info( &ctx, tag_len );
226
227 CHK( aead_encrypt( &ctx, tag_len,
228 iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ),
229 msg1_part1, sizeof( msg1_part1 ),
230 msg1_part2, sizeof( msg1_part2 ) ) );
231 CHK( aead_encrypt( &ctx, tag_len,
232 iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ),
233 msg2_part1, sizeof( msg2_part1 ),
234 msg2_part2, sizeof( msg2_part2 ) ) );
235
236exit:
237 mbedtls_cipher_free( &ctx );
238
239 return( ret );
240}
241
242
243/*
244 * Main function
245 */
246int main( int argc, char **argv )
247{
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100248 /* Check usage */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100249 if( argc != 2 )
250 {
251 puts( usage );
252 return( 1 );
253 }
254
Manuel Pégourié-Gonnard248b3852022-01-31 12:56:39 +0100255 int ret;
256
257 /* Run the demo */
258 CHK( aead_demo( argv[1] ) );
259
260exit:
261 return( ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE );
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100262}
263
264#endif