Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 1 | /** |
| 2 | * PSA API key derivation demonstration |
| 3 | * |
| 4 | * This program calculates a key ladder: a chain of secret material, each |
| 5 | * derived from the previous one in a deterministic way based on a label. |
| 6 | * Two keys are identical if and only if they are derived from the same key |
| 7 | * using the same label. |
| 8 | * |
| 9 | * The initial key is called the master key. The master key is normally |
| 10 | * randomly generated, but it could itself be derived from another key. |
| 11 | * |
| 12 | * This program derives a series of keys called intermediate keys. |
| 13 | * The first intermediate key is derived from the master key using the |
| 14 | * first label passed on the command line. Each subsequent intermediate |
| 15 | * key is derived from the previous one using the next label passed |
| 16 | * on the command line. |
| 17 | * |
| 18 | * This program has four modes of operation: |
| 19 | * |
| 20 | * - "generate": generate a random master key. |
| 21 | * - "wrap": derive a wrapping key from the last intermediate key, |
| 22 | * and use that key to encrypt-and-authenticate some data. |
| 23 | * - "unwrap": derive a wrapping key from the last intermediate key, |
| 24 | * and use that key to decrypt-and-authenticate some |
| 25 | * ciphertext created by wrap mode. |
| 26 | * - "save": save the last intermediate key so that it can be reused as |
| 27 | * the master key in another run of the program. |
| 28 | * |
| 29 | * See the usage() output for the command line usage. See the file |
| 30 | * `key_ladder_demo.sh` for an example run. |
| 31 | */ |
| 32 | |
| 33 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 34 | * SPDX-License-Identifier: Apache-2.0 |
| 35 | * |
| 36 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 37 | * not use this file except in compliance with the License. |
| 38 | * You may obtain a copy of the License at |
| 39 | * |
| 40 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 41 | * |
| 42 | * Unless required by applicable law or agreed to in writing, software |
| 43 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 44 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 45 | * See the License for the specific language governing permissions and |
| 46 | * limitations under the License. |
| 47 | * |
| 48 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 49 | */ |
| 50 | |
| 51 | /* First include Mbed TLS headers to get the Mbed TLS configuration and |
| 52 | * platform definitions that we'll use in this program. Also include |
| 53 | * standard C headers for functions we'll use here. */ |
| 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 55 | #include "mbedtls/config.h" |
| 56 | #else |
| 57 | #include MBEDTLS_CONFIG_FILE |
| 58 | #endif |
| 59 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 60 | #include <stdlib.h> |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 61 | #include <stdio.h> |
| 62 | #include <string.h> |
| 63 | |
| 64 | #include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize |
| 65 | |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 66 | #include <psa/crypto.h> |
| 67 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 68 | /* If the build options we need are not enabled, compile a placeholder. */ |
| 69 | #if !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \ |
| 70 | !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_CCM_C) || \ |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 71 | !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_FS_IO) ||\ |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 72 | defined(PSA_PRE_1_0_KEY_DERIVATION) |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 73 | int main( void ) |
| 74 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 75 | printf("MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or " |
| 76 | "MBEDTLS_AES_C and/or MBEDTLS_CCM_C and/or " |
Janos Follath | 71a4c91 | 2019-06-11 09:14:47 +0100 | [diff] [blame] | 77 | "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_FS_IO and/or " |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 78 | "not defined and/or PSA_PRE_1_0_KEY_DERIVATION defined.\n"); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 79 | return( 0 ); |
| 80 | } |
| 81 | #else |
| 82 | |
| 83 | /* The real program starts here. */ |
| 84 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 85 | /* Run a system function and bail out if it fails. */ |
| 86 | #define SYS_CHECK( expr ) \ |
| 87 | do \ |
| 88 | { \ |
| 89 | if( ! ( expr ) ) \ |
| 90 | { \ |
| 91 | perror( #expr ); \ |
| 92 | status = DEMO_ERROR; \ |
| 93 | goto exit; \ |
| 94 | } \ |
| 95 | } \ |
| 96 | while( 0 ) |
| 97 | |
| 98 | /* Run a PSA function and bail out if it fails. */ |
| 99 | #define PSA_CHECK( expr ) \ |
| 100 | do \ |
| 101 | { \ |
| 102 | status = ( expr ); \ |
| 103 | if( status != PSA_SUCCESS ) \ |
| 104 | { \ |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 105 | printf( "Error %d at line %u: %s\n", \ |
| 106 | (int) status, \ |
| 107 | __LINE__, \ |
| 108 | #expr ); \ |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 109 | goto exit; \ |
| 110 | } \ |
| 111 | } \ |
| 112 | while( 0 ) |
| 113 | |
| 114 | /* To report operational errors in this program, use an error code that is |
| 115 | * different from every PSA error code. */ |
| 116 | #define DEMO_ERROR 120 |
| 117 | |
| 118 | /* The maximum supported key ladder depth. */ |
| 119 | #define MAX_LADDER_DEPTH 10 |
| 120 | |
| 121 | /* Salt to use when deriving an intermediate key. */ |
| 122 | #define DERIVE_KEY_SALT ( (uint8_t *) "key_ladder_demo.derive" ) |
| 123 | #define DERIVE_KEY_SALT_LENGTH ( strlen( (const char*) DERIVE_KEY_SALT ) ) |
| 124 | |
| 125 | /* Salt to use when deriving a wrapping key. */ |
| 126 | #define WRAPPING_KEY_SALT ( (uint8_t *) "key_ladder_demo.wrap" ) |
| 127 | #define WRAPPING_KEY_SALT_LENGTH ( strlen( (const char*) WRAPPING_KEY_SALT ) ) |
| 128 | |
| 129 | /* Size of the key derivation keys (applies both to the master key and |
| 130 | * to intermediate keys). */ |
| 131 | #define KEY_SIZE_BYTES 40 |
| 132 | |
| 133 | /* Algorithm for key derivation. */ |
| 134 | #define KDF_ALG PSA_ALG_HKDF( PSA_ALG_SHA_256 ) |
| 135 | |
| 136 | /* Type and size of the key used to wrap data. */ |
| 137 | #define WRAPPING_KEY_TYPE PSA_KEY_TYPE_AES |
| 138 | #define WRAPPING_KEY_BITS 128 |
| 139 | |
| 140 | /* Cipher mode used to wrap data. */ |
| 141 | #define WRAPPING_ALG PSA_ALG_CCM |
| 142 | |
| 143 | /* Nonce size used to wrap data. */ |
| 144 | #define WRAPPING_IV_SIZE 13 |
| 145 | |
| 146 | /* Header used in files containing wrapped data. We'll save this header |
| 147 | * directly without worrying about data representation issues such as |
| 148 | * integer sizes and endianness, because the data is meant to be read |
| 149 | * back by the same program on the same machine. */ |
| 150 | #define WRAPPED_DATA_MAGIC "key_ladder_demo" // including trailing null byte |
| 151 | #define WRAPPED_DATA_MAGIC_LENGTH ( sizeof( WRAPPED_DATA_MAGIC ) ) |
| 152 | typedef struct |
| 153 | { |
| 154 | char magic[WRAPPED_DATA_MAGIC_LENGTH]; |
| 155 | size_t ad_size; /* Size of the additional data, which is this header. */ |
| 156 | size_t payload_size; /* Size of the encrypted data. */ |
| 157 | /* Store the IV inside the additional data. It's convenient. */ |
| 158 | uint8_t iv[WRAPPING_IV_SIZE]; |
| 159 | } wrapped_data_header_t; |
| 160 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 161 | /* The modes that this program can operate in (see usage). */ |
| 162 | enum program_mode |
| 163 | { |
| 164 | MODE_GENERATE, |
| 165 | MODE_SAVE, |
| 166 | MODE_UNWRAP, |
| 167 | MODE_WRAP |
| 168 | }; |
| 169 | |
| 170 | /* Save a key to a file. In the real world, you may want to export a derived |
| 171 | * key sometimes, to share it with another party. */ |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 172 | static psa_status_t save_key( psa_key_handle_t key_handle, |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 173 | const char *output_file_name ) |
| 174 | { |
| 175 | psa_status_t status = PSA_SUCCESS; |
| 176 | uint8_t key_data[KEY_SIZE_BYTES]; |
| 177 | size_t key_size; |
| 178 | FILE *key_file = NULL; |
| 179 | |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 180 | PSA_CHECK( psa_export_key( key_handle, |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 181 | key_data, sizeof( key_data ), |
| 182 | &key_size ) ); |
| 183 | SYS_CHECK( ( key_file = fopen( output_file_name, "wb" ) ) != NULL ); |
| 184 | SYS_CHECK( fwrite( key_data, 1, key_size, key_file ) == key_size ); |
| 185 | SYS_CHECK( fclose( key_file ) == 0 ); |
| 186 | key_file = NULL; |
| 187 | |
| 188 | exit: |
| 189 | if( key_file != NULL) |
| 190 | fclose( key_file ); |
| 191 | return( status ); |
| 192 | } |
| 193 | |
| 194 | /* Generate a master key for use in this demo. |
| 195 | * |
| 196 | * Normally a master key would be non-exportable. For the purpose of this |
| 197 | * demo, we want to save it to a file, to avoid relying on the keystore |
| 198 | * capability of the PSA crypto library. */ |
| 199 | static psa_status_t generate( const char *key_file_name ) |
| 200 | { |
| 201 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 202 | psa_key_handle_t key_handle = 0; |
Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 203 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 204 | |
Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 205 | psa_set_key_usage_flags( &attributes, |
| 206 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 207 | psa_set_key_algorithm( &attributes, KDF_ALG ); |
| 208 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 209 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( KEY_SIZE_BYTES ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 210 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 211 | PSA_CHECK( psa_generate_key( &attributes, &key_handle ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 212 | |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 213 | PSA_CHECK( save_key( key_handle, key_file_name ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 214 | |
| 215 | exit: |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 216 | (void) psa_destroy_key( key_handle ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 217 | return( status ); |
| 218 | } |
| 219 | |
| 220 | /* Load the master key from a file. |
| 221 | * |
| 222 | * In the real world, this master key would be stored in an internal memory |
| 223 | * and the storage would be managed by the keystore capability of the PSA |
| 224 | * crypto library. */ |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 225 | static psa_status_t import_key_from_file( psa_key_usage_t usage, |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 226 | psa_algorithm_t alg, |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 227 | const char *key_file_name, |
| 228 | psa_key_handle_t *master_key_handle ) |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 229 | { |
| 230 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 231 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 232 | uint8_t key_data[KEY_SIZE_BYTES]; |
| 233 | size_t key_size; |
| 234 | FILE *key_file = NULL; |
| 235 | unsigned char extra_byte; |
| 236 | |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 237 | *master_key_handle = 0; |
| 238 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 239 | SYS_CHECK( ( key_file = fopen( key_file_name, "rb" ) ) != NULL ); |
| 240 | SYS_CHECK( ( key_size = fread( key_data, 1, sizeof( key_data ), |
| 241 | key_file ) ) != 0 ); |
| 242 | if( fread( &extra_byte, 1, 1, key_file ) != 0 ) |
| 243 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 244 | printf( "Key file too large (max: %u).\n", |
| 245 | (unsigned) sizeof( key_data ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 246 | status = DEMO_ERROR; |
| 247 | goto exit; |
| 248 | } |
| 249 | SYS_CHECK( fclose( key_file ) == 0 ); |
| 250 | key_file = NULL; |
| 251 | |
Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 252 | psa_set_key_usage_flags( &attributes, usage ); |
| 253 | psa_set_key_algorithm( &attributes, alg ); |
| 254 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 255 | PSA_CHECK( psa_import_key( &attributes, key_data, key_size, |
| 256 | master_key_handle ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 257 | exit: |
| 258 | if( key_file != NULL ) |
| 259 | fclose( key_file ); |
| 260 | mbedtls_platform_zeroize( key_data, sizeof( key_data ) ); |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 261 | if( status != PSA_SUCCESS ) |
| 262 | { |
Gilles Peskine | 5163a92 | 2019-05-27 14:52:34 +0200 | [diff] [blame] | 263 | /* If the key creation hasn't happened yet or has failed, |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 264 | * *master_key_handle is 0. psa_destroy_key(0) is guaranteed to do |
| 265 | * nothing and return PSA_ERROR_INVALID_HANDLE. */ |
| 266 | (void) psa_destroy_key( *master_key_handle ); |
| 267 | *master_key_handle = 0; |
| 268 | } |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 269 | return( status ); |
| 270 | } |
| 271 | |
| 272 | /* Derive the intermediate keys, using the list of labels provided on |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 273 | * the command line. On input, *key_handle is a handle to the master key. |
| 274 | * This function closes the master key. On successful output, *key_handle |
| 275 | * is a handle to the final derived key. */ |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 276 | static psa_status_t derive_key_ladder( const char *ladder[], |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 277 | size_t ladder_depth, |
| 278 | psa_key_handle_t *key_handle ) |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 279 | { |
| 280 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 281 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 282 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 283 | size_t i; |
Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 284 | |
| 285 | psa_set_key_usage_flags( &attributes, |
| 286 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); |
| 287 | psa_set_key_algorithm( &attributes, KDF_ALG ); |
| 288 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 289 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( KEY_SIZE_BYTES ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 290 | |
| 291 | /* For each label in turn, ... */ |
| 292 | for( i = 0; i < ladder_depth; i++ ) |
| 293 | { |
| 294 | /* Start deriving material from the master key (if i=0) or from |
| 295 | * the current intermediate key (if i>0). */ |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 296 | PSA_CHECK( psa_key_derivation_setup( &operation, KDF_ALG ) ); |
| 297 | PSA_CHECK( psa_key_derivation_input_bytes( |
| 298 | &operation, PSA_KEY_DERIVATION_INPUT_SALT, |
| 299 | DERIVE_KEY_SALT, DERIVE_KEY_SALT_LENGTH ) ); |
| 300 | PSA_CHECK( psa_key_derivation_input_key( |
| 301 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 302 | *key_handle ) ); |
| 303 | PSA_CHECK( psa_key_derivation_input_bytes( |
| 304 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 305 | (uint8_t*) ladder[i], strlen( ladder[i] ) ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 306 | /* When the parent key is not the master key, destroy it, |
| 307 | * since it is no longer needed. */ |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 308 | PSA_CHECK( psa_close_key( *key_handle ) ); |
| 309 | *key_handle = 0; |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 310 | /* Derive the next intermediate key from the parent key. */ |
| 311 | PSA_CHECK( psa_key_derivation_output_key( &attributes, &operation, |
| 312 | key_handle ) ); |
| 313 | PSA_CHECK( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | exit: |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 317 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 318 | if( status != PSA_SUCCESS ) |
| 319 | { |
| 320 | psa_close_key( *key_handle ); |
| 321 | *key_handle = 0; |
| 322 | } |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 323 | return( status ); |
| 324 | } |
| 325 | |
| 326 | /* Derive a wrapping key from the last intermediate key. */ |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 327 | static psa_status_t derive_wrapping_key( psa_key_usage_t usage, |
| 328 | psa_key_handle_t derived_key_handle, |
| 329 | psa_key_handle_t *wrapping_key_handle ) |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 330 | { |
| 331 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 332 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 333 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 334 | |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 335 | *wrapping_key_handle = 0; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 336 | |
Gilles Peskine | 2a38e24 | 2019-05-29 14:33:00 +0200 | [diff] [blame] | 337 | /* Set up a key derivation operation from the key derived from |
| 338 | * the master key. */ |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 339 | PSA_CHECK( psa_key_derivation_setup( &operation, KDF_ALG ) ); |
| 340 | PSA_CHECK( psa_key_derivation_input_bytes( |
| 341 | &operation, PSA_KEY_DERIVATION_INPUT_SALT, |
| 342 | WRAPPING_KEY_SALT, WRAPPING_KEY_SALT_LENGTH ) ); |
| 343 | PSA_CHECK( psa_key_derivation_input_key( |
| 344 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 345 | derived_key_handle ) ); |
| 346 | PSA_CHECK( psa_key_derivation_input_bytes( |
| 347 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 348 | NULL, 0 ) ); |
Gilles Peskine | 2a38e24 | 2019-05-29 14:33:00 +0200 | [diff] [blame] | 349 | |
| 350 | /* Create the wrapping key. */ |
| 351 | psa_set_key_usage_flags( &attributes, usage ); |
| 352 | psa_set_key_algorithm( &attributes, WRAPPING_ALG ); |
| 353 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
| 354 | psa_set_key_bits( &attributes, WRAPPING_KEY_BITS ); |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 355 | PSA_CHECK( psa_key_derivation_output_key( &attributes, &operation, |
| 356 | wrapping_key_handle ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 357 | |
| 358 | exit: |
Gilles Peskine | 4e2cc53 | 2019-05-29 14:30:27 +0200 | [diff] [blame] | 359 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 360 | return( status ); |
| 361 | } |
| 362 | |
| 363 | static psa_status_t wrap_data( const char *input_file_name, |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 364 | const char *output_file_name, |
| 365 | psa_key_handle_t wrapping_key_handle ) |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 366 | { |
| 367 | psa_status_t status; |
| 368 | FILE *input_file = NULL; |
| 369 | FILE *output_file = NULL; |
| 370 | long input_position; |
| 371 | size_t input_size; |
Gilles Peskine | 5e09bc7 | 2018-12-21 12:06:15 +0100 | [diff] [blame] | 372 | size_t buffer_size = 0; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 373 | unsigned char *buffer = NULL; |
| 374 | size_t ciphertext_size; |
| 375 | wrapped_data_header_t header; |
| 376 | |
| 377 | /* Find the size of the data to wrap. */ |
| 378 | SYS_CHECK( ( input_file = fopen( input_file_name, "rb" ) ) != NULL ); |
| 379 | SYS_CHECK( fseek( input_file, 0, SEEK_END ) == 0 ); |
| 380 | SYS_CHECK( ( input_position = ftell( input_file ) ) != -1 ); |
| 381 | #if LONG_MAX > SIZE_MAX |
| 382 | if( input_position > SIZE_MAX ) |
| 383 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 384 | printf( "Input file too large.\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 385 | status = DEMO_ERROR; |
| 386 | goto exit; |
| 387 | } |
| 388 | #endif |
| 389 | input_size = input_position; |
| 390 | buffer_size = PSA_AEAD_ENCRYPT_OUTPUT_SIZE( WRAPPING_ALG, input_size ); |
| 391 | /* Check for integer overflow. */ |
| 392 | if( buffer_size < input_size ) |
| 393 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 394 | printf( "Input file too large.\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 395 | status = DEMO_ERROR; |
| 396 | goto exit; |
| 397 | } |
| 398 | |
| 399 | /* Load the data to wrap. */ |
| 400 | SYS_CHECK( fseek( input_file, 0, SEEK_SET ) == 0 ); |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 401 | SYS_CHECK( ( buffer = calloc( 1, buffer_size ) ) != NULL ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 402 | SYS_CHECK( fread( buffer, 1, input_size, input_file ) == input_size ); |
| 403 | SYS_CHECK( fclose( input_file ) == 0 ); |
| 404 | input_file = NULL; |
| 405 | |
| 406 | /* Construct a header. */ |
| 407 | memcpy( &header.magic, WRAPPED_DATA_MAGIC, WRAPPED_DATA_MAGIC_LENGTH ); |
| 408 | header.ad_size = sizeof( header ); |
| 409 | header.payload_size = input_size; |
| 410 | |
| 411 | /* Wrap the data. */ |
| 412 | PSA_CHECK( psa_generate_random( header.iv, WRAPPING_IV_SIZE ) ); |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 413 | PSA_CHECK( psa_aead_encrypt( wrapping_key_handle, WRAPPING_ALG, |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 414 | header.iv, WRAPPING_IV_SIZE, |
| 415 | (uint8_t *) &header, sizeof( header ), |
| 416 | buffer, input_size, |
| 417 | buffer, buffer_size, |
| 418 | &ciphertext_size ) ); |
| 419 | |
| 420 | /* Write the output. */ |
| 421 | SYS_CHECK( ( output_file = fopen( output_file_name, "wb" ) ) != NULL ); |
| 422 | SYS_CHECK( fwrite( &header, 1, sizeof( header ), |
| 423 | output_file ) == sizeof( header ) ); |
| 424 | SYS_CHECK( fwrite( buffer, 1, ciphertext_size, |
| 425 | output_file ) == ciphertext_size ); |
| 426 | SYS_CHECK( fclose( output_file ) == 0 ); |
| 427 | output_file = NULL; |
| 428 | |
| 429 | exit: |
| 430 | if( input_file != NULL ) |
| 431 | fclose( input_file ); |
| 432 | if( output_file != NULL ) |
| 433 | fclose( output_file ); |
| 434 | if( buffer != NULL ) |
| 435 | mbedtls_platform_zeroize( buffer, buffer_size ); |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 436 | free( buffer ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 437 | return( status ); |
| 438 | } |
| 439 | |
| 440 | static psa_status_t unwrap_data( const char *input_file_name, |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 441 | const char *output_file_name, |
| 442 | psa_key_handle_t wrapping_key_handle ) |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 443 | { |
| 444 | psa_status_t status; |
| 445 | FILE *input_file = NULL; |
| 446 | FILE *output_file = NULL; |
| 447 | unsigned char *buffer = NULL; |
Gilles Peskine | 5e09bc7 | 2018-12-21 12:06:15 +0100 | [diff] [blame] | 448 | size_t ciphertext_size = 0; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 449 | size_t plaintext_size; |
| 450 | wrapped_data_header_t header; |
| 451 | unsigned char extra_byte; |
| 452 | |
| 453 | /* Load and validate the header. */ |
| 454 | SYS_CHECK( ( input_file = fopen( input_file_name, "rb" ) ) != NULL ); |
| 455 | SYS_CHECK( fread( &header, 1, sizeof( header ), |
| 456 | input_file ) == sizeof( header ) ); |
| 457 | if( memcmp( &header.magic, WRAPPED_DATA_MAGIC, |
| 458 | WRAPPED_DATA_MAGIC_LENGTH ) != 0 ) |
| 459 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 460 | printf( "The input does not start with a valid magic header.\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 461 | status = DEMO_ERROR; |
| 462 | goto exit; |
| 463 | } |
| 464 | if( header.ad_size != sizeof( header ) ) |
| 465 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 466 | printf( "The header size is not correct.\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 467 | status = DEMO_ERROR; |
| 468 | goto exit; |
| 469 | } |
| 470 | ciphertext_size = |
| 471 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( WRAPPING_ALG, header.payload_size ); |
| 472 | /* Check for integer overflow. */ |
| 473 | if( ciphertext_size < header.payload_size ) |
| 474 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 475 | printf( "Input file too large.\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 476 | status = DEMO_ERROR; |
| 477 | goto exit; |
| 478 | } |
| 479 | |
| 480 | /* Load the payload data. */ |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 481 | SYS_CHECK( ( buffer = calloc( 1, ciphertext_size ) ) != NULL ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 482 | SYS_CHECK( fread( buffer, 1, ciphertext_size, |
| 483 | input_file ) == ciphertext_size ); |
| 484 | if( fread( &extra_byte, 1, 1, input_file ) != 0 ) |
| 485 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 486 | printf( "Extra garbage after ciphertext\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 487 | status = DEMO_ERROR; |
| 488 | goto exit; |
| 489 | } |
| 490 | SYS_CHECK( fclose( input_file ) == 0 ); |
| 491 | input_file = NULL; |
| 492 | |
| 493 | /* Unwrap the data. */ |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 494 | PSA_CHECK( psa_aead_decrypt( wrapping_key_handle, WRAPPING_ALG, |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 495 | header.iv, WRAPPING_IV_SIZE, |
| 496 | (uint8_t *) &header, sizeof( header ), |
| 497 | buffer, ciphertext_size, |
| 498 | buffer, ciphertext_size, |
| 499 | &plaintext_size ) ); |
| 500 | if( plaintext_size != header.payload_size ) |
| 501 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 502 | printf( "Incorrect payload size in the header.\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 503 | status = DEMO_ERROR; |
| 504 | goto exit; |
| 505 | } |
| 506 | |
| 507 | /* Write the output. */ |
| 508 | SYS_CHECK( ( output_file = fopen( output_file_name, "wb" ) ) != NULL ); |
| 509 | SYS_CHECK( fwrite( buffer, 1, plaintext_size, |
| 510 | output_file ) == plaintext_size ); |
| 511 | SYS_CHECK( fclose( output_file ) == 0 ); |
| 512 | output_file = NULL; |
| 513 | |
| 514 | exit: |
| 515 | if( input_file != NULL ) |
| 516 | fclose( input_file ); |
| 517 | if( output_file != NULL ) |
| 518 | fclose( output_file ); |
| 519 | if( buffer != NULL ) |
| 520 | mbedtls_platform_zeroize( buffer, ciphertext_size ); |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 521 | free( buffer ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 522 | return( status ); |
| 523 | } |
| 524 | |
| 525 | static psa_status_t run( enum program_mode mode, |
| 526 | const char *key_file_name, |
| 527 | const char *ladder[], size_t ladder_depth, |
| 528 | const char *input_file_name, |
| 529 | const char *output_file_name ) |
| 530 | { |
| 531 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 532 | psa_key_handle_t derivation_key_handle = 0; |
| 533 | psa_key_handle_t wrapping_key_handle = 0; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 534 | |
| 535 | /* Initialize the PSA crypto library. */ |
| 536 | PSA_CHECK( psa_crypto_init( ) ); |
| 537 | |
| 538 | /* Generate mode is unlike the others. Generate the master key and exit. */ |
| 539 | if( mode == MODE_GENERATE ) |
| 540 | return( generate( key_file_name ) ); |
| 541 | |
| 542 | /* Read the master key. */ |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 543 | PSA_CHECK( import_key_from_file( PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT, |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 544 | KDF_ALG, |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 545 | key_file_name, |
| 546 | &derivation_key_handle ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 547 | |
| 548 | /* Calculate the derived key for this session. */ |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 549 | PSA_CHECK( derive_key_ladder( ladder, ladder_depth, |
| 550 | &derivation_key_handle ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 551 | |
| 552 | switch( mode ) |
| 553 | { |
| 554 | case MODE_SAVE: |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 555 | PSA_CHECK( save_key( derivation_key_handle, output_file_name ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 556 | break; |
| 557 | case MODE_UNWRAP: |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 558 | PSA_CHECK( derive_wrapping_key( PSA_KEY_USAGE_DECRYPT, |
| 559 | derivation_key_handle, |
| 560 | &wrapping_key_handle ) ); |
| 561 | PSA_CHECK( unwrap_data( input_file_name, output_file_name, |
| 562 | wrapping_key_handle ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 563 | break; |
| 564 | case MODE_WRAP: |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 565 | PSA_CHECK( derive_wrapping_key( PSA_KEY_USAGE_ENCRYPT, |
| 566 | derivation_key_handle, |
| 567 | &wrapping_key_handle ) ); |
| 568 | PSA_CHECK( wrap_data( input_file_name, output_file_name, |
| 569 | wrapping_key_handle ) ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 570 | break; |
| 571 | default: |
| 572 | /* Unreachable but some compilers don't realize it. */ |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | exit: |
Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 577 | /* Close any remaining key. Deinitializing the crypto library would do |
| 578 | * this anyway, but explicitly closing handles makes the code easier |
| 579 | * to reuse. */ |
| 580 | (void) psa_close_key( derivation_key_handle ); |
| 581 | (void) psa_close_key( wrapping_key_handle ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 582 | /* Deinitialize the PSA crypto library. */ |
| 583 | mbedtls_psa_crypto_free( ); |
| 584 | return( status ); |
| 585 | } |
| 586 | |
| 587 | static void usage( void ) |
| 588 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 589 | printf( "Usage: key_ladder_demo MODE [OPTION=VALUE]...\n" ); |
| 590 | printf( "Demonstrate the usage of a key derivation ladder.\n" ); |
| 591 | printf( "\n" ); |
| 592 | printf( "Modes:\n" ); |
| 593 | printf( " generate Generate the master key\n" ); |
| 594 | printf( " save Save the derived key\n" ); |
| 595 | printf( " unwrap Unwrap (decrypt) input with the derived key\n" ); |
| 596 | printf( " wrap Wrap (encrypt) input with the derived key\n" ); |
| 597 | printf( "\n" ); |
| 598 | printf( "Options:\n" ); |
| 599 | printf( " input=FILENAME Input file (required for wrap/unwrap)\n" ); |
| 600 | printf( " master=FILENAME File containing the master key (default: master.key)\n" ); |
| 601 | printf( " output=FILENAME Output file (required for save/wrap/unwrap)\n" ); |
| 602 | printf( " label=TEXT Label for the key derivation.\n" ); |
| 603 | printf( " This may be repeated multiple times.\n" ); |
| 604 | printf( " To get the same key, you must use the same master key\n" ); |
| 605 | printf( " and the same sequence of labels.\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Jaeden Amero | 44a59ab | 2019-02-11 13:24:47 +0000 | [diff] [blame] | 608 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 609 | #include "mbedtls/platform_util.h" |
| 610 | void mbedtls_param_failed( const char *failure_condition, |
| 611 | const char *file, |
| 612 | int line ) |
| 613 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 614 | printf( "%s:%i: Input param failed - %s\n", |
Jaeden Amero | 44a59ab | 2019-02-11 13:24:47 +0000 | [diff] [blame] | 615 | file, line, failure_condition ); |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 616 | exit( EXIT_FAILURE ); |
Jaeden Amero | 44a59ab | 2019-02-11 13:24:47 +0000 | [diff] [blame] | 617 | } |
| 618 | #endif |
| 619 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 620 | int main( int argc, char *argv[] ) |
| 621 | { |
Gilles Peskine | 738f017 | 2019-01-02 17:25:16 +0100 | [diff] [blame] | 622 | const char *key_file_name = "master.key"; |
| 623 | const char *input_file_name = NULL; |
| 624 | const char *output_file_name = NULL; |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 625 | const char *ladder[MAX_LADDER_DEPTH]; |
| 626 | size_t ladder_depth = 0; |
| 627 | int i; |
| 628 | enum program_mode mode; |
| 629 | psa_status_t status; |
| 630 | |
| 631 | if( argc <= 1 || |
| 632 | strcmp( argv[1], "help" ) == 0 || |
| 633 | strcmp( argv[1], "-help" ) == 0 || |
| 634 | strcmp( argv[1], "--help" ) == 0 ) |
| 635 | { |
| 636 | usage( ); |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 637 | return( EXIT_SUCCESS ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | for( i = 2; i < argc; i++ ) |
| 641 | { |
| 642 | char *q = strchr( argv[i], '=' ); |
| 643 | if( q == NULL ) |
| 644 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 645 | printf( "Missing argument to option %s\n", argv[i] ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 646 | goto usage_failure; |
| 647 | } |
| 648 | *q = 0; |
| 649 | ++q; |
| 650 | if( strcmp( argv[i], "input" ) == 0 ) |
| 651 | input_file_name = q; |
| 652 | else if( strcmp( argv[i], "label" ) == 0 ) |
| 653 | { |
| 654 | if( ladder_depth == MAX_LADDER_DEPTH ) |
| 655 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 656 | printf( "Maximum ladder depth %u exceeded.\n", |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 657 | (unsigned) MAX_LADDER_DEPTH ); |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 658 | return( EXIT_FAILURE ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 659 | } |
| 660 | ladder[ladder_depth] = q; |
| 661 | ++ladder_depth; |
| 662 | } |
| 663 | else if( strcmp( argv[i], "master" ) == 0 ) |
| 664 | key_file_name = q; |
| 665 | else if( strcmp( argv[i], "output" ) == 0 ) |
| 666 | output_file_name = q; |
| 667 | else |
| 668 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 669 | printf( "Unknown option: %s\n", argv[i] ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 670 | goto usage_failure; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if( strcmp( argv[1], "generate" ) == 0 ) |
| 675 | mode = MODE_GENERATE; |
| 676 | else if( strcmp( argv[1], "save" ) == 0 ) |
| 677 | mode = MODE_SAVE; |
| 678 | else if( strcmp( argv[1], "unwrap" ) == 0 ) |
| 679 | mode = MODE_UNWRAP; |
| 680 | else if( strcmp( argv[1], "wrap" ) == 0 ) |
| 681 | mode = MODE_WRAP; |
| 682 | else |
| 683 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 684 | printf( "Unknown action: %s\n", argv[1] ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 685 | goto usage_failure; |
| 686 | } |
| 687 | |
| 688 | if( input_file_name == NULL && |
| 689 | ( mode == MODE_WRAP || mode == MODE_UNWRAP ) ) |
| 690 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 691 | printf( "Required argument missing: input\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 692 | return( DEMO_ERROR ); |
| 693 | } |
| 694 | if( output_file_name == NULL && |
| 695 | ( mode == MODE_SAVE || mode == MODE_WRAP || mode == MODE_UNWRAP ) ) |
| 696 | { |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 697 | printf( "Required argument missing: output\n" ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 698 | return( DEMO_ERROR ); |
| 699 | } |
| 700 | |
| 701 | status = run( mode, key_file_name, |
| 702 | ladder, ladder_depth, |
| 703 | input_file_name, output_file_name ); |
| 704 | return( status == PSA_SUCCESS ? |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 705 | EXIT_SUCCESS : |
| 706 | EXIT_FAILURE ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 707 | |
| 708 | usage_failure: |
| 709 | usage( ); |
Jaeden Amero | fa30c33 | 2018-12-21 18:42:18 +0000 | [diff] [blame] | 710 | return( EXIT_FAILURE ); |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 711 | } |
| 712 | #endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C && MBEDTLS_AES_C && MBEDTLS_CCM_C && MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */ |