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