blob: a2b47d13d4d8d2d6d945cdccfe0676b891f91fa1 [file] [log] [blame]
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +01001/*
Manuel Pégourié-Gonnard0e725c32022-01-27 11:15:33 +01002 * This is a simple example of multi-part AEAD computation using both the old
3 * Cipher API and the new PSA API; its goal is to help migration to PSA Crypto.
4 *
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +01005 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21/*
Manuel Pégourié-Gonnard0e725c32022-01-27 11:15:33 +010022 * When used with multi-part AEAD operations, the `mbedtls_cipher_context`
23 * serves a triple purpose (1) hold the key, (2) store the algorithm when no
24 * operation is active, and (3) save progress information for the current
25 * operation. With PSA those roles are held by disinct objects: (1) a
26 * psa_key_id_t to hold the key, a (2) psa_algorithm_t to represent the
27 * algorithm, and (3) a psa_operation_t for multi-part progress.
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010028 *
29 * On the other hand, with PSA, the algorithms encodes the desired tag length;
30 * with Cipher the desired tag length needs to be tracked separately.
31 *
32 * This program illustrates this by doing the same sequence of multi-part AEAD
33 * computation with both APIs; looking at the two series of functions
34 * cipher_xxx() and aead_xxx() side by side should make the differences and
35 * similarities clear.
36 */
37
38#include <stdio.h>
39
40#include "mbedtls/build_info.h"
41
42#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_CIPHER_C) || \
43 !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \
Manuel Pégourié-Gonnard9efbf532022-01-17 11:57:44 +010044 !defined(MBEDTLS_CHACHAPOLY_C) || \
45 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010046int main( void )
47{
48 printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_MD_C and/or "
49 "MBEDTLS_AES_C and/or MBEDTLS_GCM_C and/or "
Manuel Pégourié-Gonnard9efbf532022-01-17 11:57:44 +010050 "MBEDTLS_CHACHAPOLY_C not defined, and/or "
51 "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010052 return( 0 );
53}
54#else
55
56#include <string.h>
57
58#include "mbedtls/cipher.h"
59#include "psa/crypto.h"
60
61/*
62 * Common data and helper functions
63 */
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +010064const char usage[] = "Usage: aead_cipher_psa [aes128-gcm|aes256-gcm|aes128-gcm_8|chachapoly]";
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010065
66const unsigned char iv1[12] = { 0x00 };
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +010067const unsigned char add_data1[] = { 0x01, 0x02 };
68const unsigned char msg1_part1[] = { 0x03, 0x04 };
69const unsigned char msg1_part2[] = { 0x05, 0x06, 0x07 };
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +010070const size_t msg1_size = sizeof( msg1_part1 ) + sizeof( msg1_part2 );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010071
72const unsigned char iv2[12] = { 0x10 };
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +010073const unsigned char add_data2[] = { 0x11, 0x12 };
74const unsigned char msg2_part1[] = { 0x13, 0x14 };
75const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 };
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +010076const size_t msg2_size = sizeof( msg2_part1 ) + sizeof( msg2_part2 );
77
78const size_t msg_max_size = msg1_size > msg2_size ? msg1_size : msg2_size;
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010079
80const unsigned char key_bytes[32] = { 0x2a };
81
82void print_out( const char *title, unsigned char *out, size_t len )
83{
84 printf( "%s:", title );
85 for( size_t i = 0; i < len; i++ )
86 printf( " %02x", out[i] );
87 printf( "\n" );
88}
89
90/*
91 * Functions using the Cipher API
92 */
93#define CHK( code ) \
94 do { \
95 ret = code; \
96 if( ret != 0 ) { \
Manuel Pégourié-Gonnard763641a2022-01-17 11:58:54 +010097 printf( "%03d: ret = -0x%04x\n", __LINE__, (unsigned) -ret ); \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010098 goto exit; \
99 } \
100 } while( 0 )
101
102
103static int cipher_prepare( const char *info,
104 mbedtls_cipher_context_t *ctx,
105 size_t *tag_len )
106{
107 int ret;
108
109 mbedtls_cipher_type_t type;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100110 if( strcmp( info, "aes128-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100111 type = MBEDTLS_CIPHER_AES_128_GCM;
112 *tag_len = 16;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100113 } else if( strcmp( info, "aes256-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100114 type = MBEDTLS_CIPHER_AES_256_GCM;
115 *tag_len = 16;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100116 } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100117 type = MBEDTLS_CIPHER_AES_128_GCM;
118 *tag_len = 8;
119 } else if( strcmp( info, "chachapoly" ) == 0 ) {
120 type = MBEDTLS_CIPHER_CHACHA20_POLY1305;
121 *tag_len = 16;
122 } else {
123 puts( usage );
124 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
125 }
126
127 CHK( mbedtls_cipher_setup( ctx,
128 mbedtls_cipher_info_from_type( type ) ) );
129
Manuel Pégourié-Gonnard24e82de2022-01-18 09:29:41 +0100130 int key_len = mbedtls_cipher_get_key_bitlen( ctx );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100131 CHK( mbedtls_cipher_setkey( ctx, key_bytes, key_len, MBEDTLS_ENCRYPT ) );
132
133exit:
134 return( ret );
135}
136
137static void cipher_info( const mbedtls_cipher_context_t *ctx, size_t tag_len )
138{
139 // no convenient way to get the cipher type (for example, AES)
140 const char *ciph = "???";
141 int key_bits = mbedtls_cipher_get_key_bitlen( ctx );
142 mbedtls_cipher_mode_t mode = mbedtls_cipher_get_cipher_mode( ctx );
143
144 const char *mode_str = mode == MBEDTLS_MODE_GCM ? "GCM"
145 : mode == MBEDTLS_MODE_CHACHAPOLY ? "ChachaPoly"
146 : "???";
147
Manuel Pégourié-Gonnardaab52582022-01-18 09:30:51 +0100148 printf( "cipher: %s, %d, %s, %u\n", ciph, key_bits, mode_str, (unsigned) tag_len );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100149}
150
151static int cipher_encrypt( mbedtls_cipher_context_t *ctx, size_t tag_len,
152 const unsigned char *iv, size_t iv_len,
153 const unsigned char *ad, size_t ad_len,
154 const unsigned char *pa, size_t pa_len,
155 const unsigned char *pb, size_t pb_len )
156{
157 int ret;
158 size_t olen;
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +0100159 unsigned char out[msg_max_size + 16];
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100160 unsigned char *p = out;
161
162 CHK( mbedtls_cipher_set_iv( ctx, iv, iv_len ) );
163 CHK( mbedtls_cipher_reset( ctx ) );
164 CHK( mbedtls_cipher_update_ad( ctx, ad, ad_len ) );
165 CHK( mbedtls_cipher_update( ctx, pa, pa_len, p, &olen ) );
166 p += olen;
167 CHK( mbedtls_cipher_update( ctx, pb, pb_len, p, &olen ) );
168 p += olen;
169 CHK( mbedtls_cipher_finish( ctx, p, &olen ) );
170 p += olen;
171 CHK( mbedtls_cipher_write_tag( ctx, p, tag_len ) );
172 p += tag_len;
173
174 olen = p - out;
175 print_out( "cipher", out, olen );
176
177exit:
178 return( ret );
179}
180
181static int cipher( const char *info )
182{
183 int ret = 0;
184
185 mbedtls_cipher_context_t ctx;
186 size_t tag_len;
187
188 mbedtls_cipher_init( &ctx );
189
190 CHK( cipher_prepare( info, &ctx, &tag_len ) );
191
192 cipher_info( &ctx, tag_len );
193
194 CHK( cipher_encrypt( &ctx, tag_len,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100195 iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ),
196 msg1_part1, sizeof( msg1_part1 ),
197 msg1_part2, sizeof( msg1_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100198 CHK( cipher_encrypt( &ctx, tag_len,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100199 iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ),
200 msg2_part1, sizeof( msg2_part1 ),
201 msg2_part2, sizeof( msg2_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100202
203exit:
204 mbedtls_cipher_free( &ctx );
205
206 return( ret );
207}
208
209#undef CHK
210
211/*
212 * Functions using the PSA Crypto API
213 */
214
215#define CHK( code ) \
216 do { \
217 status = code; \
218 if( status != PSA_SUCCESS ) { \
Manuel Pégourié-Gonnard763641a2022-01-17 11:58:54 +0100219 printf( "%03d: status = %d\n", __LINE__, status ); \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100220 goto exit; \
221 } \
222 } while( 0 )
223
224static psa_status_t aead_prepare( const char *info,
225 psa_key_id_t *key,
226 psa_algorithm_t *alg )
227{
228 psa_status_t status;
229
230 size_t key_bits;
231 psa_key_type_t key_type;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100232 if( strcmp( info, "aes128-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100233 *alg = PSA_ALG_GCM;
234 key_bits = 128;
235 key_type = PSA_KEY_TYPE_AES;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100236 } else if( strcmp( info, "aes256-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100237 *alg = PSA_ALG_GCM;
238 key_bits = 256;
239 key_type = PSA_KEY_TYPE_AES;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100240 } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100241 *alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8);
242 key_bits = 128;
243 key_type = PSA_KEY_TYPE_AES;
244 } else if( strcmp( info, "chachapoly" ) == 0 ) {
245 *alg = PSA_ALG_CHACHA20_POLY1305;
246 key_bits = 256;
247 key_type = PSA_KEY_TYPE_CHACHA20;
248 } else {
249 puts( usage );
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 }
252
253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
255 psa_set_key_algorithm( &attributes, *alg );
256 psa_set_key_type( &attributes, key_type );
257 psa_set_key_bits( &attributes, key_bits );
258
259 CHK( psa_import_key( &attributes, key_bytes, key_bits / 8, key ) );
260
261exit:
262 return( status );
263}
264
265static void aead_info( psa_key_id_t key, psa_algorithm_t alg )
266{
267 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
268 (void) psa_get_key_attributes( key, &attr );
269 psa_key_type_t key_type = psa_get_key_type( &attr );
270 size_t key_bits = psa_get_key_bits( &attr );
271 psa_algorithm_t base_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
272 size_t tag_len = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
273
274 const char *type_str = key_type == PSA_KEY_TYPE_AES ? "AES"
275 : key_type == PSA_KEY_TYPE_CHACHA20 ? "Chacha"
276 : "???";
277 const char *base_str = base_alg == PSA_ALG_GCM ? "GCM"
278 : base_alg == PSA_ALG_CHACHA20_POLY1305 ? "ChachaPoly"
279 : "???";
280
Manuel Pégourié-Gonnardaab52582022-01-18 09:30:51 +0100281 printf( "aead : %s, %u, %s, %u\n",
282 type_str, (unsigned) key_bits, base_str, (unsigned) tag_len );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100283}
284
285static int aead_encrypt( psa_key_id_t key, psa_algorithm_t alg,
286 const unsigned char *iv, size_t iv_len,
287 const unsigned char *ad, size_t ad_len,
288 const unsigned char *pa, size_t pa_len,
289 const unsigned char *pb, size_t pb_len )
290{
291 psa_status_t status;
292 size_t olen, olen_tag;
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +0100293 unsigned char out[PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(msg_max_size)];
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100294 unsigned char *p = out, *end = out + sizeof( out );
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +0100295 unsigned char tag[PSA_AEAD_TAG_MAX_SIZE];
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100296
297 psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;
298 CHK( psa_aead_encrypt_setup( &op, key, alg ) );
299
300 CHK( psa_aead_set_nonce( &op, iv, iv_len ) );
301 CHK( psa_aead_update_ad( &op, ad, ad_len ) );
302 CHK( psa_aead_update( &op, pa, pa_len, p, end - p, &olen ) );
303 p += olen;
304 CHK( psa_aead_update( &op, pb, pb_len, p, end - p, &olen ) );
305 p += olen;
306 CHK( psa_aead_finish( &op, p, end - p, &olen,
307 tag, sizeof( tag ), &olen_tag ) );
308 p += olen;
309 memcpy( p, tag, olen_tag );
310 p += olen_tag;
311
312 olen = p - out;
313 print_out( "aead ", out, olen );
314exit:
315 return( status );
316}
317
318static psa_status_t aead( const char *info )
319{
320 psa_status_t status;
321
322 psa_key_id_t key;
323 psa_algorithm_t alg;
324
325 CHK( aead_prepare( info, &key, &alg ) );
326
327 aead_info( key, alg );
328
329 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100330 iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ),
331 msg1_part1, sizeof( msg1_part1 ),
332 msg1_part2, sizeof( msg1_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100333 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100334 iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ),
335 msg2_part1, sizeof( msg2_part1 ),
336 msg2_part2, sizeof( msg2_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100337
338exit:
339 return( status );
340}
341
342#undef CHK
343
344/*
345 * Main function
346 */
347int main( int argc, char **argv )
348{
349 if( argc != 2 )
350 {
351 puts( usage );
352 return( 1 );
353 }
354
355 psa_crypto_init();
356
357 cipher( argv[1] );
358 aead( argv[1] );
359}
360
361#endif