Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 1 | /** |
| 2 | * PSA API multi-part AEAD demonstration. |
Manuel Pégourié-Gonnard | 0e725c3 | 2022-01-27 11:15:33 +0100 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 4 | * This program AEAD-encrypts a message, using the algorithm and key size |
| 5 | * specified on the command line, using the multi-part API. |
| 6 | * |
Manuel Pégourié-Gonnard | 29088a4 | 2022-02-01 09:38:26 +0100 | [diff] [blame] | 7 | * It comes with a companion program cipher/cipher_aead_demo.c, which does the same |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 8 | * operations with the legacy Cipher API. The goal is that comparing the two |
| 9 | * programs will help people migrating to the PSA Crypto API. |
| 10 | * |
| 11 | * When used with multi-part AEAD operations, the `mbedtls_cipher_context` |
| 12 | * serves a triple purpose (1) hold the key, (2) store the algorithm when no |
| 13 | * operation is active, and (3) save progress information for the current |
| 14 | * operation. With PSA those roles are held by disinct objects: (1) a |
| 15 | * psa_key_id_t to hold the key, a (2) psa_algorithm_t to represent the |
| 16 | * algorithm, and (3) a psa_operation_t for multi-part progress. |
| 17 | * |
| 18 | * On the other hand, with PSA, the algorithms encodes the desired tag length; |
| 19 | * with Cipher the desired tag length needs to be tracked separately. |
| 20 | * |
Manuel Pégourié-Gonnard | 29088a4 | 2022-02-01 09:38:26 +0100 | [diff] [blame] | 21 | * This program and its companion cipher/cipher_aead_demo.c illustrate this by doing the |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 22 | * same sequence of multi-part AEAD computation with both APIs; looking at the |
| 23 | * two side by side should make the differences and similarities clear. |
| 24 | */ |
| 25 | |
| 26 | /* |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 27 | * Copyright The Mbed TLS Contributors |
| 28 | * SPDX-License-Identifier: Apache-2.0 |
| 29 | * |
| 30 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 31 | * not use this file except in compliance with the License. |
| 32 | * You may obtain a copy of the License at |
| 33 | * |
| 34 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 35 | * |
| 36 | * Unless required by applicable law or agreed to in writing, software |
| 37 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 38 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 | * See the License for the specific language governing permissions and |
| 40 | * limitations under the License. |
| 41 | */ |
| 42 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 43 | /* First include Mbed TLS headers to get the Mbed TLS configuration and |
| 44 | * platform definitions that we'll use in this program. Also include |
| 45 | * standard C headers for functions we'll use here. */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 46 | #include "mbedtls/build_info.h" |
| 47 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 48 | #include "psa/crypto.h" |
| 49 | |
| 50 | #include <stdlib.h> |
| 51 | #include <stdio.h> |
| 52 | #include <string.h> |
| 53 | |
| 54 | /* If the build options we need are not enabled, compile a placeholder. */ |
Manuel Pégourié-Gonnard | 7d5ef17 | 2022-01-27 13:09:13 +0100 | [diff] [blame] | 55 | #if !defined(MBEDTLS_PSA_CRYPTO_C) || \ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 56 | !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_GCM_C) || \ |
Manuel Pégourié-Gonnard | 9efbf53 | 2022-01-17 11:57:44 +0100 | [diff] [blame] | 57 | !defined(MBEDTLS_CHACHAPOLY_C) || \ |
| 58 | defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 59 | int main( void ) |
| 60 | { |
Manuel Pégourié-Gonnard | 7d5ef17 | 2022-01-27 13:09:13 +0100 | [diff] [blame] | 61 | printf( "MBEDTLS_PSA_CRYPTO_C and/or " |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 62 | "MBEDTLS_AES_C and/or MBEDTLS_GCM_C and/or " |
Manuel Pégourié-Gonnard | 9efbf53 | 2022-01-17 11:57:44 +0100 | [diff] [blame] | 63 | "MBEDTLS_CHACHAPOLY_C not defined, and/or " |
| 64 | "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 65 | return( 0 ); |
| 66 | } |
| 67 | #else |
| 68 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 69 | /* The real program starts here. */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 70 | |
Manuel Pégourié-Gonnard | 6fdc9e8 | 2022-01-31 13:27:39 +0100 | [diff] [blame] | 71 | const char usage[] = "Usage: aead_demo [aes128-gcm|aes256-gcm|aes128-gcm_8|chachapoly]"; |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 73 | /* Dummy data for encryption: IV/nonce, additional data, 2-part message */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 74 | const unsigned char iv1[12] = { 0x00 }; |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 75 | const unsigned char add_data1[] = { 0x01, 0x02 }; |
| 76 | const unsigned char msg1_part1[] = { 0x03, 0x04 }; |
| 77 | const unsigned char msg1_part2[] = { 0x05, 0x06, 0x07 }; |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 78 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 79 | /* Dummy data (2nd message) */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 80 | const unsigned char iv2[12] = { 0x10 }; |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 81 | const unsigned char add_data2[] = { 0x11, 0x12 }; |
| 82 | const unsigned char msg2_part1[] = { 0x13, 0x14 }; |
| 83 | const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 }; |
Manuel Pégourié-Gonnard | 3aae30c | 2022-01-27 11:56:24 +0100 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 85 | /* This must at least the sum of the length of the 2 parts for each message. |
| 86 | * This is a macro for the sake of compilers with insufficient C99 support. */ |
Manuel Pégourié-Gonnard | fd1d13c | 2022-01-28 12:52:35 +0100 | [diff] [blame] | 87 | #define MSG_MAX_SIZE 5 |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 88 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 89 | /* Dummy key material - never do this in production! |
| 90 | * 32-byte is enough to all the key size supported by this program. */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 91 | const unsigned char key_bytes[32] = { 0x2a }; |
| 92 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 93 | /* Print the contents of a buffer in hex */ |
| 94 | void print_buf( const char *title, uint8_t *buf, size_t len ) |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 95 | { |
| 96 | printf( "%s:", title ); |
| 97 | for( size_t i = 0; i < len; i++ ) |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 98 | printf( " %02x", buf[i] ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 99 | printf( "\n" ); |
| 100 | } |
| 101 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 102 | /* Run a PSA function and bail out if it fails. */ |
| 103 | #define PSA_CHECK( expr ) \ |
| 104 | do \ |
| 105 | { \ |
| 106 | status = ( expr ); \ |
| 107 | if( status != PSA_SUCCESS ) \ |
| 108 | { \ |
| 109 | printf( "Error %d at line %d: %s\n", \ |
| 110 | (int) status, \ |
| 111 | __LINE__, \ |
| 112 | #expr ); \ |
| 113 | goto exit; \ |
| 114 | } \ |
| 115 | } \ |
| 116 | while( 0 ) |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 117 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 118 | /* |
| 119 | * Prepare encryption material: |
| 120 | * - interpret command-line argument |
| 121 | * - set up key |
| 122 | * - outputs: key and algorithm, which together hold all the information |
| 123 | */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 124 | static psa_status_t aead_prepare( const char *info, |
| 125 | psa_key_id_t *key, |
| 126 | psa_algorithm_t *alg ) |
| 127 | { |
| 128 | psa_status_t status; |
| 129 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 130 | /* Convert arg to alg + key_bits + key_type */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 131 | size_t key_bits; |
| 132 | psa_key_type_t key_type; |
Manuel Pégourié-Gonnard | 428a97e | 2022-01-27 11:35:12 +0100 | [diff] [blame] | 133 | if( strcmp( info, "aes128-gcm" ) == 0 ) { |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 134 | *alg = PSA_ALG_GCM; |
| 135 | key_bits = 128; |
| 136 | key_type = PSA_KEY_TYPE_AES; |
Manuel Pégourié-Gonnard | 428a97e | 2022-01-27 11:35:12 +0100 | [diff] [blame] | 137 | } else if( strcmp( info, "aes256-gcm" ) == 0 ) { |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 138 | *alg = PSA_ALG_GCM; |
| 139 | key_bits = 256; |
| 140 | key_type = PSA_KEY_TYPE_AES; |
Manuel Pégourié-Gonnard | 428a97e | 2022-01-27 11:35:12 +0100 | [diff] [blame] | 141 | } else if( strcmp( info, "aes128-gcm_8" ) == 0 ) { |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 142 | *alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8); |
| 143 | key_bits = 128; |
| 144 | key_type = PSA_KEY_TYPE_AES; |
| 145 | } else if( strcmp( info, "chachapoly" ) == 0 ) { |
| 146 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 147 | key_bits = 256; |
| 148 | key_type = PSA_KEY_TYPE_CHACHA20; |
| 149 | } else { |
| 150 | puts( usage ); |
| 151 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 152 | } |
| 153 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 154 | /* Prepare key attibutes */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 155 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 156 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 157 | psa_set_key_algorithm( &attributes, *alg ); |
| 158 | psa_set_key_type( &attributes, key_type ); |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 159 | psa_set_key_bits( &attributes, key_bits ); // optional |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 161 | /* Import key */ |
| 162 | PSA_CHECK( psa_import_key( &attributes, key_bytes, key_bits / 8, key ) ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 163 | |
| 164 | exit: |
| 165 | return( status ); |
| 166 | } |
| 167 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 168 | /* |
| 169 | * Print out some information. |
| 170 | * |
| 171 | * All of this information was present in the command line argument, but his |
| 172 | * function demonstrates how each piece can be recovered from (key, alg). |
| 173 | */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 174 | static void aead_info( psa_key_id_t key, psa_algorithm_t alg ) |
| 175 | { |
| 176 | psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT; |
| 177 | (void) psa_get_key_attributes( key, &attr ); |
| 178 | psa_key_type_t key_type = psa_get_key_type( &attr ); |
| 179 | size_t key_bits = psa_get_key_bits( &attr ); |
| 180 | psa_algorithm_t base_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg ); |
| 181 | size_t tag_len = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 182 | |
| 183 | const char *type_str = key_type == PSA_KEY_TYPE_AES ? "AES" |
| 184 | : key_type == PSA_KEY_TYPE_CHACHA20 ? "Chacha" |
| 185 | : "???"; |
| 186 | const char *base_str = base_alg == PSA_ALG_GCM ? "GCM" |
| 187 | : base_alg == PSA_ALG_CHACHA20_POLY1305 ? "ChachaPoly" |
| 188 | : "???"; |
| 189 | |
Manuel Pégourié-Gonnard | 7d5ef17 | 2022-01-27 13:09:13 +0100 | [diff] [blame] | 190 | printf( "%s, %u, %s, %u\n", |
Manuel Pégourié-Gonnard | aab5258 | 2022-01-18 09:30:51 +0100 | [diff] [blame] | 191 | type_str, (unsigned) key_bits, base_str, (unsigned) tag_len ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Encrypt a 2-part message. |
| 196 | */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 197 | static int aead_encrypt( psa_key_id_t key, psa_algorithm_t alg, |
| 198 | const unsigned char *iv, size_t iv_len, |
| 199 | const unsigned char *ad, size_t ad_len, |
| 200 | const unsigned char *pa, size_t pa_len, |
| 201 | const unsigned char *pb, size_t pb_len ) |
| 202 | { |
| 203 | psa_status_t status; |
| 204 | size_t olen, olen_tag; |
Manuel Pégourié-Gonnard | fd1d13c | 2022-01-28 12:52:35 +0100 | [diff] [blame] | 205 | unsigned char out[PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(MSG_MAX_SIZE)]; |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 206 | unsigned char *p = out, *end = out + sizeof( out ); |
Manuel Pégourié-Gonnard | 3aae30c | 2022-01-27 11:56:24 +0100 | [diff] [blame] | 207 | unsigned char tag[PSA_AEAD_TAG_MAX_SIZE]; |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 208 | |
| 209 | psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT; |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 210 | PSA_CHECK( psa_aead_encrypt_setup( &op, key, alg ) ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 211 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 212 | PSA_CHECK( psa_aead_set_nonce( &op, iv, iv_len ) ); |
| 213 | PSA_CHECK( psa_aead_update_ad( &op, ad, ad_len ) ); |
| 214 | PSA_CHECK( psa_aead_update( &op, pa, pa_len, p, end - p, &olen ) ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 215 | p += olen; |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 216 | PSA_CHECK( psa_aead_update( &op, pb, pb_len, p, end - p, &olen ) ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 217 | p += olen; |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 218 | PSA_CHECK( psa_aead_finish( &op, p, end - p, &olen, |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 219 | tag, sizeof( tag ), &olen_tag ) ); |
| 220 | p += olen; |
| 221 | memcpy( p, tag, olen_tag ); |
| 222 | p += olen_tag; |
| 223 | |
| 224 | olen = p - out; |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 225 | print_buf( "out", out, olen ); |
Manuel Pégourié-Gonnard | 1a45c71 | 2022-01-27 12:17:20 +0100 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 227 | exit: |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 228 | psa_aead_abort( &op ); // required on errors, harmless on success |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 229 | return( status ); |
| 230 | } |
| 231 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 232 | /* |
| 233 | * AEAD demo: set up key/alg, print out info, encrypt messages. |
| 234 | */ |
Manuel Pégourié-Gonnard | 7d5ef17 | 2022-01-27 13:09:13 +0100 | [diff] [blame] | 235 | static psa_status_t aead_demo( const char *info ) |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 236 | { |
| 237 | psa_status_t status; |
| 238 | |
| 239 | psa_key_id_t key; |
| 240 | psa_algorithm_t alg; |
| 241 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 242 | PSA_CHECK( aead_prepare( info, &key, &alg ) ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 243 | |
| 244 | aead_info( key, alg ); |
| 245 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 246 | PSA_CHECK( aead_encrypt( key, alg, |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 247 | iv1, sizeof( iv1 ), add_data1, sizeof( add_data1 ), |
| 248 | msg1_part1, sizeof( msg1_part1 ), |
| 249 | msg1_part2, sizeof( msg1_part2 ) ) ); |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 250 | PSA_CHECK( aead_encrypt( key, alg, |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 251 | iv2, sizeof( iv2 ), add_data2, sizeof( add_data2 ), |
| 252 | msg2_part1, sizeof( msg2_part1 ), |
| 253 | msg2_part2, sizeof( msg2_part2 ) ) ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 254 | |
| 255 | exit: |
Manuel Pégourié-Gonnard | 1a45c71 | 2022-01-27 12:17:20 +0100 | [diff] [blame] | 256 | psa_destroy_key( key ); |
| 257 | |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 258 | return( status ); |
| 259 | } |
| 260 | |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 261 | /* |
| 262 | * Main function |
| 263 | */ |
| 264 | int main( int argc, char **argv ) |
| 265 | { |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 266 | psa_status_t status = PSA_SUCCESS; |
| 267 | |
| 268 | /* Check usage */ |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 269 | if( argc != 2 ) |
| 270 | { |
| 271 | puts( usage ); |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 272 | return( EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 275 | /* Initialize the PSA crypto library. */ |
| 276 | PSA_CHECK( psa_crypto_init( ) ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 277 | |
Manuel Pégourié-Gonnard | 248b385 | 2022-01-31 12:56:39 +0100 | [diff] [blame] | 278 | /* Run the demo */ |
| 279 | PSA_CHECK( aead_demo( argv[1] ) ); |
| 280 | |
| 281 | /* Deinitialize the PSA crypto library. */ |
| 282 | mbedtls_psa_crypto_free( ); |
| 283 | |
| 284 | exit: |
| 285 | return( status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | 398d459 | 2022-01-07 12:26:32 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | #endif |