Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 1 | /* |
| 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é-Gonnard | 9efbf53 | 2022-01-17 11:57:44 +0100 | [diff] [blame] | 44 | !defined(MBEDTLS_CHACHAPOLY_C) || \ |
| 45 | defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 46 | int 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é-Gonnard | 9efbf53 | 2022-01-17 11:57:44 +0100 | [diff] [blame] | 50 | "MBEDTLS_CHACHAPOLY_C not defined, and/or " |
| 51 | "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 52 | 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 | */ |
| 64 | const char usage[] = "Usage: aead_cipher_psa [gcm128|gcm256|gcm128_8|chachapoly]"; |
| 65 | |
| 66 | const unsigned char iv1[12] = { 0x00 }; |
| 67 | const unsigned char ad1[] = { 0x01, 0x02 }; |
| 68 | const unsigned char pa1[] = { 0x03, 0x04 }; |
| 69 | const unsigned char pb1[] = { 0x05, 0x06, 0x07 }; |
| 70 | |
| 71 | const unsigned char iv2[12] = { 0x10 }; |
| 72 | const unsigned char ad2[] = { 0x11, 0x12 }; |
| 73 | const unsigned char pa2[] = { 0x13, 0x14 }; |
| 74 | const unsigned char pb2[] = { 0x15, 0x16, 0x17 }; |
| 75 | |
| 76 | const unsigned char key_bytes[32] = { 0x2a }; |
| 77 | |
| 78 | void 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é-Gonnard | 763641a | 2022-01-17 11:58:54 +0100 | [diff] [blame^] | 93 | printf( "%03d: ret = -0x%04x\n", __LINE__, (unsigned) -ret ); \ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 94 | goto exit; \ |
| 95 | } \ |
| 96 | } while( 0 ) |
| 97 | |
| 98 | |
| 99 | static 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 | |
| 126 | size_t key_len = mbedtls_cipher_get_key_bitlen( ctx ); |
| 127 | CHK( mbedtls_cipher_setkey( ctx, key_bytes, key_len, MBEDTLS_ENCRYPT ) ); |
| 128 | |
| 129 | exit: |
| 130 | return( ret ); |
| 131 | } |
| 132 | |
| 133 | static 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 | |
| 144 | printf( "cipher: %s, %d, %s, %zu\n", ciph, key_bits, mode_str, tag_len ); |
| 145 | } |
| 146 | |
| 147 | static 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 | |
| 173 | exit: |
| 174 | return( ret ); |
| 175 | } |
| 176 | |
| 177 | static 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 | |
| 197 | exit: |
| 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é-Gonnard | 763641a | 2022-01-17 11:58:54 +0100 | [diff] [blame^] | 213 | printf( "%03d: status = %d\n", __LINE__, status ); \ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 214 | goto exit; \ |
| 215 | } \ |
| 216 | } while( 0 ) |
| 217 | |
| 218 | static 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 | |
| 255 | exit: |
| 256 | return( status ); |
| 257 | } |
| 258 | |
| 259 | static 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 | |
| 275 | printf( "aead : %s, %zu, %s, %zu\n", type_str, key_bits, base_str, tag_len ); |
| 276 | } |
| 277 | |
| 278 | static int aead_encrypt( psa_key_id_t key, psa_algorithm_t alg, |
| 279 | const unsigned char *iv, size_t iv_len, |
| 280 | const unsigned char *ad, size_t ad_len, |
| 281 | const unsigned char *pa, size_t pa_len, |
| 282 | const unsigned char *pb, size_t pb_len ) |
| 283 | { |
| 284 | psa_status_t status; |
| 285 | size_t olen, olen_tag; |
| 286 | unsigned char out[32]; |
| 287 | unsigned char *p = out, *end = out + sizeof( out ); |
| 288 | unsigned char tag[16]; |
| 289 | |
| 290 | psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT; |
| 291 | CHK( psa_aead_encrypt_setup( &op, key, alg ) ); |
| 292 | |
| 293 | CHK( psa_aead_set_nonce( &op, iv, iv_len ) ); |
| 294 | CHK( psa_aead_update_ad( &op, ad, ad_len ) ); |
| 295 | CHK( psa_aead_update( &op, pa, pa_len, p, end - p, &olen ) ); |
| 296 | p += olen; |
| 297 | CHK( psa_aead_update( &op, pb, pb_len, p, end - p, &olen ) ); |
| 298 | p += olen; |
| 299 | CHK( psa_aead_finish( &op, p, end - p, &olen, |
| 300 | tag, sizeof( tag ), &olen_tag ) ); |
| 301 | p += olen; |
| 302 | memcpy( p, tag, olen_tag ); |
| 303 | p += olen_tag; |
| 304 | |
| 305 | olen = p - out; |
| 306 | print_out( "aead ", out, olen ); |
| 307 | exit: |
| 308 | return( status ); |
| 309 | } |
| 310 | |
| 311 | static psa_status_t aead( const char *info ) |
| 312 | { |
| 313 | psa_status_t status; |
| 314 | |
| 315 | psa_key_id_t key; |
| 316 | psa_algorithm_t alg; |
| 317 | |
| 318 | CHK( aead_prepare( info, &key, &alg ) ); |
| 319 | |
| 320 | aead_info( key, alg ); |
| 321 | |
| 322 | CHK( aead_encrypt( key, alg, |
| 323 | iv1, sizeof( iv1 ), ad1, sizeof( ad1 ), |
| 324 | pa1, sizeof( pa1 ), pb1, sizeof( pb1 ) ) ); |
| 325 | CHK( aead_encrypt( key, alg, |
| 326 | iv2, sizeof( iv2 ), ad2, sizeof( ad2 ), |
| 327 | pa2, sizeof( pa2 ), pb2, sizeof( pb2 ) ) ); |
| 328 | |
| 329 | exit: |
| 330 | return( status ); |
| 331 | } |
| 332 | |
| 333 | #undef CHK |
| 334 | |
| 335 | /* |
| 336 | * Main function |
| 337 | */ |
| 338 | int main( int argc, char **argv ) |
| 339 | { |
| 340 | if( argc != 2 ) |
| 341 | { |
| 342 | puts( usage ); |
| 343 | return( 1 ); |
| 344 | } |
| 345 | |
| 346 | psa_crypto_init(); |
| 347 | |
| 348 | cipher( argv[1] ); |
| 349 | aead( argv[1] ); |
| 350 | } |
| 351 | |
| 352 | #endif |