blob: ebc7cd9d4836d31458dcb9788ddd6efac0a533e7 [file] [log] [blame]
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +01001/*
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +01002 * This is a simple example of multi-part AEAD computation using the PSA
3 * Crypto API. It comes with a companion program aead_non_psa.c, which does
4 * the same operations with the legacy Cipher API. The goal is that comparing the
5 * two programs will help people migrating to the PSA Crypto API.
Manuel Pégourié-Gonnard0e725c32022-01-27 11:15:33 +01006 *
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +01007 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23/*
Manuel Pégourié-Gonnard0e725c32022-01-27 11:15:33 +010024 * When used with multi-part AEAD operations, the `mbedtls_cipher_context`
25 * serves a triple purpose (1) hold the key, (2) store the algorithm when no
26 * operation is active, and (3) save progress information for the current
27 * operation. With PSA those roles are held by disinct objects: (1) a
28 * psa_key_id_t to hold the key, a (2) psa_algorithm_t to represent the
29 * algorithm, and (3) a psa_operation_t for multi-part progress.
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010030 *
31 * On the other hand, with PSA, the algorithms encodes the desired tag length;
32 * with Cipher the desired tag length needs to be tracked separately.
33 *
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010034 * This program and its compation aead_non_psa.c illustrate this by doing the
35 * same sequence of multi-part AEAD computation with both APIs; looking at the
36 * two side by side should make the differences and similarities clear.
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010037 */
38
39#include <stdio.h>
40
41#include "mbedtls/build_info.h"
42
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010043#if !defined(MBEDTLS_PSA_CRYPTO_C) || \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010044 !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \
Manuel Pégourié-Gonnard9efbf532022-01-17 11:57:44 +010045 !defined(MBEDTLS_CHACHAPOLY_C) || \
46 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010047int main( void )
48{
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010049 printf( "MBEDTLS_PSA_CRYPTO_C and/or "
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010050 "MBEDTLS_AES_C and/or MBEDTLS_GCM_C and/or "
Manuel Pégourié-Gonnard9efbf532022-01-17 11:57:44 +010051 "MBEDTLS_CHACHAPOLY_C not defined, and/or "
52 "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010053 return( 0 );
54}
55#else
56
57#include <string.h>
58
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010059#include "psa/crypto.h"
60
61/*
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010062 * Dummy data and helper functions
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010063 */
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +010064const char usage[] = "Usage: aead_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
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010090#define CHK( code ) \
91 do { \
92 status = code; \
93 if( status != PSA_SUCCESS ) { \
Manuel Pégourié-Gonnard763641a2022-01-17 11:58:54 +010094 printf( "%03d: status = %d\n", __LINE__, status ); \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010095 goto exit; \
96 } \
97 } while( 0 )
98
99static psa_status_t aead_prepare( const char *info,
100 psa_key_id_t *key,
101 psa_algorithm_t *alg )
102{
103 psa_status_t status;
104
105 size_t key_bits;
106 psa_key_type_t key_type;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100107 if( strcmp( info, "aes128-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100108 *alg = PSA_ALG_GCM;
109 key_bits = 128;
110 key_type = PSA_KEY_TYPE_AES;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100111 } else if( strcmp( info, "aes256-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100112 *alg = PSA_ALG_GCM;
113 key_bits = 256;
114 key_type = PSA_KEY_TYPE_AES;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100115 } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100116 *alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8);
117 key_bits = 128;
118 key_type = PSA_KEY_TYPE_AES;
119 } else if( strcmp( info, "chachapoly" ) == 0 ) {
120 *alg = PSA_ALG_CHACHA20_POLY1305;
121 key_bits = 256;
122 key_type = PSA_KEY_TYPE_CHACHA20;
123 } else {
124 puts( usage );
125 return( PSA_ERROR_INVALID_ARGUMENT );
126 }
127
128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
130 psa_set_key_algorithm( &attributes, *alg );
131 psa_set_key_type( &attributes, key_type );
132 psa_set_key_bits( &attributes, key_bits );
133
134 CHK( psa_import_key( &attributes, key_bytes, key_bits / 8, key ) );
135
136exit:
137 return( status );
138}
139
140static void aead_info( psa_key_id_t key, psa_algorithm_t alg )
141{
142 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
143 (void) psa_get_key_attributes( key, &attr );
144 psa_key_type_t key_type = psa_get_key_type( &attr );
145 size_t key_bits = psa_get_key_bits( &attr );
146 psa_algorithm_t base_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
147 size_t tag_len = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
148
149 const char *type_str = key_type == PSA_KEY_TYPE_AES ? "AES"
150 : key_type == PSA_KEY_TYPE_CHACHA20 ? "Chacha"
151 : "???";
152 const char *base_str = base_alg == PSA_ALG_GCM ? "GCM"
153 : base_alg == PSA_ALG_CHACHA20_POLY1305 ? "ChachaPoly"
154 : "???";
155
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100156 printf( "%s, %u, %s, %u\n",
Manuel Pégourié-Gonnardaab52582022-01-18 09:30:51 +0100157 type_str, (unsigned) key_bits, base_str, (unsigned) tag_len );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100158}
159
160static int aead_encrypt( psa_key_id_t key, psa_algorithm_t alg,
161 const unsigned char *iv, size_t iv_len,
162 const unsigned char *ad, size_t ad_len,
163 const unsigned char *pa, size_t pa_len,
164 const unsigned char *pb, size_t pb_len )
165{
166 psa_status_t status;
167 size_t olen, olen_tag;
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +0100168 unsigned char out[PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(msg_max_size)];
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100169 unsigned char *p = out, *end = out + sizeof( out );
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +0100170 unsigned char tag[PSA_AEAD_TAG_MAX_SIZE];
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100171
172 psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;
173 CHK( psa_aead_encrypt_setup( &op, key, alg ) );
174
175 CHK( psa_aead_set_nonce( &op, iv, iv_len ) );
176 CHK( psa_aead_update_ad( &op, ad, ad_len ) );
177 CHK( psa_aead_update( &op, pa, pa_len, p, end - p, &olen ) );
178 p += olen;
179 CHK( psa_aead_update( &op, pb, pb_len, p, end - p, &olen ) );
180 p += olen;
181 CHK( psa_aead_finish( &op, p, end - p, &olen,
182 tag, sizeof( tag ), &olen_tag ) );
183 p += olen;
184 memcpy( p, tag, olen_tag );
185 p += olen_tag;
186
187 olen = p - out;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100188 print_out( "out", out, olen );
Manuel Pégourié-Gonnard1a45c712022-01-27 12:17:20 +0100189
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100190exit:
Manuel Pégourié-Gonnard1a45c712022-01-27 12:17:20 +0100191 /* required on errors, harmless on success */
192 psa_aead_abort( &op );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100193 return( status );
194}
195
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100196static psa_status_t aead_demo( const char *info )
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100197{
198 psa_status_t status;
199
200 psa_key_id_t key;
201 psa_algorithm_t alg;
202
203 CHK( aead_prepare( info, &key, &alg ) );
204
205 aead_info( key, alg );
206
207 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100208 iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ),
209 msg1_part1, sizeof( msg1_part1 ),
210 msg1_part2, sizeof( msg1_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100211 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100212 iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ),
213 msg2_part1, sizeof( msg2_part1 ),
214 msg2_part2, sizeof( msg2_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100215
216exit:
Manuel Pégourié-Gonnard1a45c712022-01-27 12:17:20 +0100217 psa_destroy_key( key );
218
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100219 return( status );
220}
221
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100222/*
223 * Main function
224 */
225int main( int argc, char **argv )
226{
227 if( argc != 2 )
228 {
229 puts( usage );
230 return( 1 );
231 }
232
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100233 psa_status_t status = psa_crypto_init();
234 if( status != PSA_SUCCESS )
235 printf( "psa init: %d\n", status );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100236
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100237 aead_demo( argv[1] );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100238}
239
240#endif