blob: 61f0530332523db12ade23f97e11ffa1ba03e3fe [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é-Gonnard398d4592022-01-07 12:26:32 +010070
71const unsigned char iv2[12] = { 0x10 };
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +010072const unsigned char add_data2[] = { 0x11, 0x12 };
73const unsigned char msg2_part1[] = { 0x13, 0x14 };
74const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 };
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010075
76const unsigned char key_bytes[32] = { 0x2a };
77
78void print_out( const char *title, unsigned char *out, size_t len )
79{
80 printf( "%s:", title );
81 for( size_t i = 0; i < len; i++ )
82 printf( " %02x", out[i] );
83 printf( "\n" );
84}
85
86/*
87 * Functions using the Cipher API
88 */
89#define CHK( code ) \
90 do { \
91 ret = code; \
92 if( ret != 0 ) { \
Manuel Pégourié-Gonnard763641a2022-01-17 11:58:54 +010093 printf( "%03d: ret = -0x%04x\n", __LINE__, (unsigned) -ret ); \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010094 goto exit; \
95 } \
96 } while( 0 )
97
98
99static int cipher_prepare( const char *info,
100 mbedtls_cipher_context_t *ctx,
101 size_t *tag_len )
102{
103 int ret;
104
105 mbedtls_cipher_type_t type;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100106 if( strcmp( info, "aes128-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100107 type = MBEDTLS_CIPHER_AES_128_GCM;
108 *tag_len = 16;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100109 } else if( strcmp( info, "aes256-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100110 type = MBEDTLS_CIPHER_AES_256_GCM;
111 *tag_len = 16;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100112 } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100113 type = MBEDTLS_CIPHER_AES_128_GCM;
114 *tag_len = 8;
115 } else if( strcmp( info, "chachapoly" ) == 0 ) {
116 type = MBEDTLS_CIPHER_CHACHA20_POLY1305;
117 *tag_len = 16;
118 } else {
119 puts( usage );
120 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
121 }
122
123 CHK( mbedtls_cipher_setup( ctx,
124 mbedtls_cipher_info_from_type( type ) ) );
125
Manuel Pégourié-Gonnard24e82de2022-01-18 09:29:41 +0100126 int key_len = mbedtls_cipher_get_key_bitlen( ctx );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100127 CHK( mbedtls_cipher_setkey( ctx, key_bytes, key_len, MBEDTLS_ENCRYPT ) );
128
129exit:
130 return( ret );
131}
132
133static void cipher_info( const mbedtls_cipher_context_t *ctx, size_t tag_len )
134{
135 // no convenient way to get the cipher type (for example, AES)
136 const char *ciph = "???";
137 int key_bits = mbedtls_cipher_get_key_bitlen( ctx );
138 mbedtls_cipher_mode_t mode = mbedtls_cipher_get_cipher_mode( ctx );
139
140 const char *mode_str = mode == MBEDTLS_MODE_GCM ? "GCM"
141 : mode == MBEDTLS_MODE_CHACHAPOLY ? "ChachaPoly"
142 : "???";
143
Manuel Pégourié-Gonnardaab52582022-01-18 09:30:51 +0100144 printf( "cipher: %s, %d, %s, %u\n", ciph, key_bits, mode_str, (unsigned) tag_len );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100145}
146
147static int cipher_encrypt( mbedtls_cipher_context_t *ctx, size_t tag_len,
148 const unsigned char *iv, size_t iv_len,
149 const unsigned char *ad, size_t ad_len,
150 const unsigned char *pa, size_t pa_len,
151 const unsigned char *pb, size_t pb_len )
152{
153 int ret;
154 size_t olen;
155 unsigned char out[32];
156 unsigned char *p = out;
157
158 CHK( mbedtls_cipher_set_iv( ctx, iv, iv_len ) );
159 CHK( mbedtls_cipher_reset( ctx ) );
160 CHK( mbedtls_cipher_update_ad( ctx, ad, ad_len ) );
161 CHK( mbedtls_cipher_update( ctx, pa, pa_len, p, &olen ) );
162 p += olen;
163 CHK( mbedtls_cipher_update( ctx, pb, pb_len, p, &olen ) );
164 p += olen;
165 CHK( mbedtls_cipher_finish( ctx, p, &olen ) );
166 p += olen;
167 CHK( mbedtls_cipher_write_tag( ctx, p, tag_len ) );
168 p += tag_len;
169
170 olen = p - out;
171 print_out( "cipher", out, olen );
172
173exit:
174 return( ret );
175}
176
177static int cipher( const char *info )
178{
179 int ret = 0;
180
181 mbedtls_cipher_context_t ctx;
182 size_t tag_len;
183
184 mbedtls_cipher_init( &ctx );
185
186 CHK( cipher_prepare( info, &ctx, &tag_len ) );
187
188 cipher_info( &ctx, tag_len );
189
190 CHK( cipher_encrypt( &ctx, tag_len,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100191 iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ),
192 msg1_part1, sizeof( msg1_part1 ),
193 msg1_part2, sizeof( msg1_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100194 CHK( cipher_encrypt( &ctx, tag_len,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100195 iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ),
196 msg2_part1, sizeof( msg2_part1 ),
197 msg2_part2, sizeof( msg2_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100198
199exit:
200 mbedtls_cipher_free( &ctx );
201
202 return( ret );
203}
204
205#undef CHK
206
207/*
208 * Functions using the PSA Crypto API
209 */
210
211#define CHK( code ) \
212 do { \
213 status = code; \
214 if( status != PSA_SUCCESS ) { \
Manuel Pégourié-Gonnard763641a2022-01-17 11:58:54 +0100215 printf( "%03d: status = %d\n", __LINE__, status ); \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100216 goto exit; \
217 } \
218 } while( 0 )
219
220static psa_status_t aead_prepare( const char *info,
221 psa_key_id_t *key,
222 psa_algorithm_t *alg )
223{
224 psa_status_t status;
225
226 size_t key_bits;
227 psa_key_type_t key_type;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100228 if( strcmp( info, "aes128-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100229 *alg = PSA_ALG_GCM;
230 key_bits = 128;
231 key_type = PSA_KEY_TYPE_AES;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100232 } else if( strcmp( info, "aes256-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100233 *alg = PSA_ALG_GCM;
234 key_bits = 256;
235 key_type = PSA_KEY_TYPE_AES;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100236 } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100237 *alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8);
238 key_bits = 128;
239 key_type = PSA_KEY_TYPE_AES;
240 } else if( strcmp( info, "chachapoly" ) == 0 ) {
241 *alg = PSA_ALG_CHACHA20_POLY1305;
242 key_bits = 256;
243 key_type = PSA_KEY_TYPE_CHACHA20;
244 } else {
245 puts( usage );
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 }
248
249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
251 psa_set_key_algorithm( &attributes, *alg );
252 psa_set_key_type( &attributes, key_type );
253 psa_set_key_bits( &attributes, key_bits );
254
255 CHK( psa_import_key( &attributes, key_bytes, key_bits / 8, key ) );
256
257exit:
258 return( status );
259}
260
261static void aead_info( psa_key_id_t key, psa_algorithm_t alg )
262{
263 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
264 (void) psa_get_key_attributes( key, &attr );
265 psa_key_type_t key_type = psa_get_key_type( &attr );
266 size_t key_bits = psa_get_key_bits( &attr );
267 psa_algorithm_t base_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
268 size_t tag_len = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
269
270 const char *type_str = key_type == PSA_KEY_TYPE_AES ? "AES"
271 : key_type == PSA_KEY_TYPE_CHACHA20 ? "Chacha"
272 : "???";
273 const char *base_str = base_alg == PSA_ALG_GCM ? "GCM"
274 : base_alg == PSA_ALG_CHACHA20_POLY1305 ? "ChachaPoly"
275 : "???";
276
Manuel Pégourié-Gonnardaab52582022-01-18 09:30:51 +0100277 printf( "aead : %s, %u, %s, %u\n",
278 type_str, (unsigned) key_bits, base_str, (unsigned) tag_len );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100279}
280
281static int aead_encrypt( psa_key_id_t key, psa_algorithm_t alg,
282 const unsigned char *iv, size_t iv_len,
283 const unsigned char *ad, size_t ad_len,
284 const unsigned char *pa, size_t pa_len,
285 const unsigned char *pb, size_t pb_len )
286{
287 psa_status_t status;
288 size_t olen, olen_tag;
289 unsigned char out[32];
290 unsigned char *p = out, *end = out + sizeof( out );
291 unsigned char tag[16];
292
293 psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;
294 CHK( psa_aead_encrypt_setup( &op, key, alg ) );
295
296 CHK( psa_aead_set_nonce( &op, iv, iv_len ) );
297 CHK( psa_aead_update_ad( &op, ad, ad_len ) );
298 CHK( psa_aead_update( &op, pa, pa_len, p, end - p, &olen ) );
299 p += olen;
300 CHK( psa_aead_update( &op, pb, pb_len, p, end - p, &olen ) );
301 p += olen;
302 CHK( psa_aead_finish( &op, p, end - p, &olen,
303 tag, sizeof( tag ), &olen_tag ) );
304 p += olen;
305 memcpy( p, tag, olen_tag );
306 p += olen_tag;
307
308 olen = p - out;
309 print_out( "aead ", out, olen );
310exit:
311 return( status );
312}
313
314static psa_status_t aead( const char *info )
315{
316 psa_status_t status;
317
318 psa_key_id_t key;
319 psa_algorithm_t alg;
320
321 CHK( aead_prepare( info, &key, &alg ) );
322
323 aead_info( key, alg );
324
325 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100326 iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ),
327 msg1_part1, sizeof( msg1_part1 ),
328 msg1_part2, sizeof( msg1_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100329 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100330 iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ),
331 msg2_part1, sizeof( msg2_part1 ),
332 msg2_part2, sizeof( msg2_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100333
334exit:
335 return( status );
336}
337
338#undef CHK
339
340/*
341 * Main function
342 */
343int main( int argc, char **argv )
344{
345 if( argc != 2 )
346 {
347 puts( usage );
348 return( 1 );
349 }
350
351 psa_crypto_init();
352
353 cipher( argv[1] );
354 aead( argv[1] );
355}
356
357#endif