blob: 5ed32a5f6ace1e25bb61b6427417483fefb03b6c [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é-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é-Gonnard3aae30c2022-01-27 11:56:24 +010075
Manuel Pégourié-Gonnardfd1d13c2022-01-28 12:52:35 +010076#define MSG_MAX_SIZE 5
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010077
78const unsigned char key_bytes[32] = { 0x2a };
79
80void print_out( const char *title, unsigned char *out, size_t len )
81{
82 printf( "%s:", title );
83 for( size_t i = 0; i < len; i++ )
84 printf( " %02x", out[i] );
85 printf( "\n" );
86}
87
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010088#define CHK( code ) \
89 do { \
90 status = code; \
91 if( status != PSA_SUCCESS ) { \
Manuel Pégourié-Gonnard763641a2022-01-17 11:58:54 +010092 printf( "%03d: status = %d\n", __LINE__, status ); \
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +010093 goto exit; \
94 } \
95 } while( 0 )
96
97static psa_status_t aead_prepare( const char *info,
98 psa_key_id_t *key,
99 psa_algorithm_t *alg )
100{
101 psa_status_t status;
102
103 size_t key_bits;
104 psa_key_type_t key_type;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100105 if( strcmp( info, "aes128-gcm" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100106 *alg = PSA_ALG_GCM;
107 key_bits = 128;
108 key_type = PSA_KEY_TYPE_AES;
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 *alg = PSA_ALG_GCM;
111 key_bits = 256;
112 key_type = PSA_KEY_TYPE_AES;
Manuel Pégourié-Gonnard428a97e2022-01-27 11:35:12 +0100113 } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) {
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100114 *alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8);
115 key_bits = 128;
116 key_type = PSA_KEY_TYPE_AES;
117 } else if( strcmp( info, "chachapoly" ) == 0 ) {
118 *alg = PSA_ALG_CHACHA20_POLY1305;
119 key_bits = 256;
120 key_type = PSA_KEY_TYPE_CHACHA20;
121 } else {
122 puts( usage );
123 return( PSA_ERROR_INVALID_ARGUMENT );
124 }
125
126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
128 psa_set_key_algorithm( &attributes, *alg );
129 psa_set_key_type( &attributes, key_type );
130 psa_set_key_bits( &attributes, key_bits );
131
132 CHK( psa_import_key( &attributes, key_bytes, key_bits / 8, key ) );
133
134exit:
135 return( status );
136}
137
138static void aead_info( psa_key_id_t key, psa_algorithm_t alg )
139{
140 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
141 (void) psa_get_key_attributes( key, &attr );
142 psa_key_type_t key_type = psa_get_key_type( &attr );
143 size_t key_bits = psa_get_key_bits( &attr );
144 psa_algorithm_t base_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
145 size_t tag_len = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
146
147 const char *type_str = key_type == PSA_KEY_TYPE_AES ? "AES"
148 : key_type == PSA_KEY_TYPE_CHACHA20 ? "Chacha"
149 : "???";
150 const char *base_str = base_alg == PSA_ALG_GCM ? "GCM"
151 : base_alg == PSA_ALG_CHACHA20_POLY1305 ? "ChachaPoly"
152 : "???";
153
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100154 printf( "%s, %u, %s, %u\n",
Manuel Pégourié-Gonnardaab52582022-01-18 09:30:51 +0100155 type_str, (unsigned) key_bits, base_str, (unsigned) tag_len );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100156}
157
158static int aead_encrypt( psa_key_id_t key, psa_algorithm_t alg,
159 const unsigned char *iv, size_t iv_len,
160 const unsigned char *ad, size_t ad_len,
161 const unsigned char *pa, size_t pa_len,
162 const unsigned char *pb, size_t pb_len )
163{
164 psa_status_t status;
165 size_t olen, olen_tag;
Manuel Pégourié-Gonnardfd1d13c2022-01-28 12:52:35 +0100166 unsigned char out[PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(MSG_MAX_SIZE)];
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100167 unsigned char *p = out, *end = out + sizeof( out );
Manuel Pégourié-Gonnard3aae30c2022-01-27 11:56:24 +0100168 unsigned char tag[PSA_AEAD_TAG_MAX_SIZE];
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100169
170 psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;
171 CHK( psa_aead_encrypt_setup( &op, key, alg ) );
172
173 CHK( psa_aead_set_nonce( &op, iv, iv_len ) );
174 CHK( psa_aead_update_ad( &op, ad, ad_len ) );
175 CHK( psa_aead_update( &op, pa, pa_len, p, end - p, &olen ) );
176 p += olen;
177 CHK( psa_aead_update( &op, pb, pb_len, p, end - p, &olen ) );
178 p += olen;
179 CHK( psa_aead_finish( &op, p, end - p, &olen,
180 tag, sizeof( tag ), &olen_tag ) );
181 p += olen;
182 memcpy( p, tag, olen_tag );
183 p += olen_tag;
184
185 olen = p - out;
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100186 print_out( "out", out, olen );
Manuel Pégourié-Gonnard1a45c712022-01-27 12:17:20 +0100187
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100188exit:
Manuel Pégourié-Gonnard1a45c712022-01-27 12:17:20 +0100189 /* required on errors, harmless on success */
190 psa_aead_abort( &op );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100191 return( status );
192}
193
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100194static psa_status_t aead_demo( const char *info )
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100195{
196 psa_status_t status;
197
198 psa_key_id_t key;
199 psa_algorithm_t alg;
200
201 CHK( aead_prepare( info, &key, &alg ) );
202
203 aead_info( key, alg );
204
205 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100206 iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ),
207 msg1_part1, sizeof( msg1_part1 ),
208 msg1_part2, sizeof( msg1_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100209 CHK( aead_encrypt( key, alg,
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100210 iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ),
211 msg2_part1, sizeof( msg2_part1 ),
212 msg2_part2, sizeof( msg2_part2 ) ) );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100213
214exit:
Manuel Pégourié-Gonnard1a45c712022-01-27 12:17:20 +0100215 psa_destroy_key( key );
216
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100217 return( status );
218}
219
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100220/*
221 * Main function
222 */
223int main( int argc, char **argv )
224{
225 if( argc != 2 )
226 {
227 puts( usage );
228 return( 1 );
229 }
230
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100231 psa_status_t status = psa_crypto_init();
232 if( status != PSA_SUCCESS )
233 printf( "psa init: %d\n", status );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100234
Manuel Pégourié-Gonnard7d5ef172022-01-27 13:09:13 +0100235 aead_demo( argv[1] );
Manuel Pégourié-Gonnard398d4592022-01-07 12:26:32 +0100236}
237
238#endif