blob: 9af50eaa5fc4fa3e684ad71409470c53017801d5 [file] [log] [blame]
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +01001/*
2 * Copyright The Mbed TLS Contributors
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * This is a simple example of multi-part AEAD computation using both the old
20 * Cipher API and the new PSA API; its goal is to help migration to PSA Crypto.
21 *
22 * When in comes to multi-part HMAC operations, the `mbedtls_md_context`
23 * serves a triple purpose (1) hold the key, (2) store the algorithm, and (3)
24 * save progress information for the current operation. With PSA those roles
25 * are held by disinct objects: (1) a psa_key_id_t to hold the key, a (2)
26 * psa_algorithm_t to represent the algorithm, and (3) a psa_operation_t for
27 * multi-part progress.
28 *
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 */
64const char usage[] = "Usage: aead_cipher_psa [gcm128|gcm256|gcm128_8|chachapoly]";
65
66const unsigned char iv1[12] = { 0x00 };
67const unsigned char ad1[] = { 0x01, 0x02 };
68const unsigned char pa1[] = { 0x03, 0x04 };
69const unsigned char pb1[] = { 0x05, 0x06, 0x07 };
70
71const unsigned char iv2[12] = { 0x10 };
72const unsigned char ad2[] = { 0x11, 0x12 };
73const unsigned char pa2[] = { 0x13, 0x14 };
74const unsigned char pb2[] = { 0x15, 0x16, 0x17 };
75
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;
106 if( strcmp( info, "gcm128" ) == 0 ) {
107 type = MBEDTLS_CIPHER_AES_128_GCM;
108 *tag_len = 16;
109 } else if( strcmp( info, "gcm256" ) == 0 ) {
110 type = MBEDTLS_CIPHER_AES_256_GCM;
111 *tag_len = 16;
112 } else if( strcmp( info, "gcm128_8" ) == 0 ) {
113 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,
191 iv1, sizeof( iv1 ), ad1, sizeof( ad1 ),
192 pa1, sizeof( pa1 ), pb1, sizeof( pb1 ) ) );
193 CHK( cipher_encrypt( &ctx, tag_len,
194 iv2, sizeof( iv2 ), ad2, sizeof( ad2 ),
195 pa2, sizeof( pa2 ), pb2, sizeof( pb2 ) ) );
196
197exit:
198 mbedtls_cipher_free( &ctx );
199
200 return( ret );
201}
202
203#undef CHK
204
205/*
206 * Functions using the PSA Crypto API
207 */
208
209#define CHK( code ) \
210 do { \
211 status = code; \
212 if( status != PSA_SUCCESS ) { \
Manuel Pégourié-Gonnard763641a2022-01-17 11:58:54 +0100213 printf( "%03d: status = %d\n", __LINE__, status ); \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100214 goto exit; \
215 } \
216 } while( 0 )
217
218static psa_status_t aead_prepare( const char *info,
219 psa_key_id_t *key,
220 psa_algorithm_t *alg )
221{
222 psa_status_t status;
223
224 size_t key_bits;
225 psa_key_type_t key_type;
226 if( strcmp( info, "gcm128" ) == 0 ) {
227 *alg = PSA_ALG_GCM;
228 key_bits = 128;
229 key_type = PSA_KEY_TYPE_AES;
230 } else if( strcmp( info, "gcm256" ) == 0 ) {
231 *alg = PSA_ALG_GCM;
232 key_bits = 256;
233 key_type = PSA_KEY_TYPE_AES;
234 } else if( strcmp( info, "gcm128_8" ) == 0 ) {
235 *alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8);
236 key_bits = 128;
237 key_type = PSA_KEY_TYPE_AES;
238 } else if( strcmp( info, "chachapoly" ) == 0 ) {
239 *alg = PSA_ALG_CHACHA20_POLY1305;
240 key_bits = 256;
241 key_type = PSA_KEY_TYPE_CHACHA20;
242 } else {
243 puts( usage );
244 return( PSA_ERROR_INVALID_ARGUMENT );
245 }
246
247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
249 psa_set_key_algorithm( &attributes, *alg );
250 psa_set_key_type( &attributes, key_type );
251 psa_set_key_bits( &attributes, key_bits );
252
253 CHK( psa_import_key( &attributes, key_bytes, key_bits / 8, key ) );
254
255exit:
256 return( status );
257}
258
259static void aead_info( psa_key_id_t key, psa_algorithm_t alg )
260{
261 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
262 (void) psa_get_key_attributes( key, &attr );
263 psa_key_type_t key_type = psa_get_key_type( &attr );
264 size_t key_bits = psa_get_key_bits( &attr );
265 psa_algorithm_t base_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
266 size_t tag_len = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
267
268 const char *type_str = key_type == PSA_KEY_TYPE_AES ? "AES"
269 : key_type == PSA_KEY_TYPE_CHACHA20 ? "Chacha"
270 : "???";
271 const char *base_str = base_alg == PSA_ALG_GCM ? "GCM"
272 : base_alg == PSA_ALG_CHACHA20_POLY1305 ? "ChachaPoly"
273 : "???";
274
Manuel Pégourié-Gonnardaab52582022-01-18 09:30:51 +0100275 printf( "aead : %s, %u, %s, %u\n",
276 type_str, (unsigned) key_bits, base_str, (unsigned) tag_len );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100277}
278
279static int aead_encrypt( psa_key_id_t key, psa_algorithm_t alg,
280 const unsigned char *iv, size_t iv_len,
281 const unsigned char *ad, size_t ad_len,
282 const unsigned char *pa, size_t pa_len,
283 const unsigned char *pb, size_t pb_len )
284{
285 psa_status_t status;
286 size_t olen, olen_tag;
287 unsigned char out[32];
288 unsigned char *p = out, *end = out + sizeof( out );
289 unsigned char tag[16];
290
291 psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;
292 CHK( psa_aead_encrypt_setup( &op, key, alg ) );
293
294 CHK( psa_aead_set_nonce( &op, iv, iv_len ) );
295 CHK( psa_aead_update_ad( &op, ad, ad_len ) );
296 CHK( psa_aead_update( &op, pa, pa_len, p, end - p, &olen ) );
297 p += olen;
298 CHK( psa_aead_update( &op, pb, pb_len, p, end - p, &olen ) );
299 p += olen;
300 CHK( psa_aead_finish( &op, p, end - p, &olen,
301 tag, sizeof( tag ), &olen_tag ) );
302 p += olen;
303 memcpy( p, tag, olen_tag );
304 p += olen_tag;
305
306 olen = p - out;
307 print_out( "aead ", out, olen );
308exit:
309 return( status );
310}
311
312static psa_status_t aead( const char *info )
313{
314 psa_status_t status;
315
316 psa_key_id_t key;
317 psa_algorithm_t alg;
318
319 CHK( aead_prepare( info, &key, &alg ) );
320
321 aead_info( key, alg );
322
323 CHK( aead_encrypt( key, alg,
324 iv1, sizeof( iv1 ), ad1, sizeof( ad1 ),
325 pa1, sizeof( pa1 ), pb1, sizeof( pb1 ) ) );
326 CHK( aead_encrypt( key, alg,
327 iv2, sizeof( iv2 ), ad2, sizeof( ad2 ),
328 pa2, sizeof( pa2 ), pb2, sizeof( pb2 ) ) );
329
330exit:
331 return( status );
332}
333
334#undef CHK
335
336/*
337 * Main function
338 */
339int main( int argc, char **argv )
340{
341 if( argc != 2 )
342 {
343 puts( usage );
344 return( 1 );
345 }
346
347 psa_crypto_init();
348
349 cipher( argv[1] );
350 aead( argv[1] );
351}
352
353#endif