blob: aa0a54b07f05e44f378da3df62e643ee690118f0 [file] [log] [blame]
Gilles Peskinef0fa4362018-07-16 17:08:43 +02001/**
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
Bence Szépkúti86974652020-06-15 11:59:37 +020033/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020034 * Copyright The Mbed TLS Contributors
Gilles Peskinef0fa4362018-07-16 17:08:43 +020035 * SPDX-License-Identifier: Apache-2.0
36 *
37 * Licensed under the Apache License, Version 2.0 (the "License"); you may
38 * not use this file except in compliance with the License.
39 * You may obtain a copy of the License at
40 *
41 * http://www.apache.org/licenses/LICENSE-2.0
42 *
43 * Unless required by applicable law or agreed to in writing, software
44 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
45 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46 * See the License for the specific language governing permissions and
47 * limitations under the License.
Gilles Peskinef0fa4362018-07-16 17:08:43 +020048 */
49
50/* First include Mbed TLS headers to get the Mbed TLS configuration and
51 * platform definitions that we'll use in this program. Also include
52 * standard C headers for functions we'll use here. */
53#if !defined(MBEDTLS_CONFIG_FILE)
54#include "mbedtls/config.h"
55#else
56#include MBEDTLS_CONFIG_FILE
57#endif
58
Gilles Peskinef0fa4362018-07-16 17:08:43 +020059#include <stdlib.h>
Gilles Peskinef0fa4362018-07-16 17:08:43 +020060#include <stdio.h>
61#include <string.h>
62
63#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
64
Gilles Peskine4e2cc532019-05-29 14:30:27 +020065#include <psa/crypto.h>
66
Gilles Peskinef0fa4362018-07-16 17:08:43 +020067/* If the build options we need are not enabled, compile a placeholder. */
Ronald Cronadc2ff22020-09-16 16:49:27 +020068#if !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
69 !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_CCM_C) || \
70 !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_FS_IO) || \
71 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072int main(void)
Gilles Peskinef0fa4362018-07-16 17:08:43 +020073{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 printf("MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
75 "MBEDTLS_AES_C and/or MBEDTLS_CCM_C and/or "
76 "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_FS_IO "
77 "not defined and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER "
78 "defined.\n");
79 return 0;
Gilles Peskinef0fa4362018-07-16 17:08:43 +020080}
81#else
82
83/* The real program starts here. */
84
Gilles Peskinef0fa4362018-07-16 17:08:43 +020085/* Run a system function and bail out if it fails. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086#define SYS_CHECK(expr) \
Gilles Peskinef0fa4362018-07-16 17:08:43 +020087 do \
88 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 if (!(expr)) \
Gilles Peskinef0fa4362018-07-16 17:08:43 +020090 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 perror( #expr); \
Gilles Peskinef0fa4362018-07-16 17:08:43 +020092 status = DEMO_ERROR; \
93 goto exit; \
94 } \
95 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 while (0)
Gilles Peskinef0fa4362018-07-16 17:08:43 +020097
98/* Run a PSA function and bail out if it fails. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099#define PSA_CHECK(expr) \
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200100 do \
101 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 status = (expr); \
103 if (status != PSA_SUCCESS) \
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200104 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 printf("Error %d at line %d: %s\n", \
106 (int) status, \
107 __LINE__, \
108 #expr); \
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200109 goto exit; \
110 } \
111 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 while (0)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200113
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. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122#define DERIVE_KEY_SALT ((uint8_t *) "key_ladder_demo.derive")
123#define DERIVE_KEY_SALT_LENGTH (strlen((const char *) DERIVE_KEY_SALT))
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200124
125/* Salt to use when deriving a wrapping key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126#define WRAPPING_KEY_SALT ((uint8_t *) "key_ladder_demo.wrap")
127#define WRAPPING_KEY_SALT_LENGTH (strlen((const char *) WRAPPING_KEY_SALT))
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200128
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. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134#define KDF_ALG PSA_ALG_HKDF(PSA_ALG_SHA_256)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200135
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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151#define WRAPPED_DATA_MAGIC_LENGTH (sizeof(WRAPPED_DATA_MAGIC))
152typedef struct {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200153 char magic[WRAPPED_DATA_MAGIC_LENGTH];
154 size_t ad_size; /* Size of the additional data, which is this header. */
155 size_t payload_size; /* Size of the encrypted data. */
156 /* Store the IV inside the additional data. It's convenient. */
157 uint8_t iv[WRAPPING_IV_SIZE];
158} wrapped_data_header_t;
159
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200160/* The modes that this program can operate in (see usage). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161enum program_mode {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200162 MODE_GENERATE,
163 MODE_SAVE,
164 MODE_UNWRAP,
165 MODE_WRAP
166};
167
168/* Save a key to a file. In the real world, you may want to export a derived
169 * key sometimes, to share it with another party. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170static psa_status_t save_key(psa_key_id_t key,
171 const char *output_file_name)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200172{
173 psa_status_t status = PSA_SUCCESS;
174 uint8_t key_data[KEY_SIZE_BYTES];
175 size_t key_size;
176 FILE *key_file = NULL;
177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 PSA_CHECK(psa_export_key(key,
179 key_data, sizeof(key_data),
180 &key_size));
181 SYS_CHECK((key_file = fopen(output_file_name, "wb")) != NULL);
182 SYS_CHECK(fwrite(key_data, 1, key_size, key_file) == key_size);
183 SYS_CHECK(fclose(key_file) == 0);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200184 key_file = NULL;
185
186exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if (key_file != NULL) {
188 fclose(key_file);
189 }
190 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200191}
192
193/* Generate a master key for use in this demo.
194 *
195 * Normally a master key would be non-exportable. For the purpose of this
196 * demo, we want to save it to a file, to avoid relying on the keystore
197 * capability of the PSA crypto library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198static psa_status_t generate(const char *key_file_name)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200199{
200 psa_status_t status = PSA_SUCCESS;
Ronald Cronadc2ff22020-09-16 16:49:27 +0200201 psa_key_id_t key = 0;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 psa_set_key_usage_flags(&attributes,
205 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
206 psa_set_key_algorithm(&attributes, KDF_ALG);
207 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
208 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(KEY_SIZE_BYTES));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 PSA_CHECK(psa_generate_key(&attributes, &key));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 PSA_CHECK(save_key(key, key_file_name));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200213
214exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 (void) psa_destroy_key(key);
216 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200217}
218
219/* Load the master key from a file.
220 *
221 * In the real world, this master key would be stored in an internal memory
222 * and the storage would be managed by the keystore capability of the PSA
223 * crypto library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224static psa_status_t import_key_from_file(psa_key_usage_t usage,
225 psa_algorithm_t alg,
226 const char *key_file_name,
227 psa_key_id_t *master_key)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200228{
229 psa_status_t status = PSA_SUCCESS;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200231 uint8_t key_data[KEY_SIZE_BYTES];
232 size_t key_size;
233 FILE *key_file = NULL;
234 unsigned char extra_byte;
235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 SYS_CHECK((key_file = fopen(key_file_name, "rb")) != NULL);
237 SYS_CHECK((key_size = fread(key_data, 1, sizeof(key_data),
238 key_file)) != 0);
239 if (fread(&extra_byte, 1, 1, key_file) != 0) {
240 printf("Key file too large (max: %u).\n",
241 (unsigned) sizeof(key_data));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200242 status = DEMO_ERROR;
243 goto exit;
244 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 SYS_CHECK(fclose(key_file) == 0);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200246 key_file = NULL;
247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 psa_set_key_usage_flags(&attributes, usage);
249 psa_set_key_algorithm(&attributes, alg);
250 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
251 PSA_CHECK(psa_import_key(&attributes, key_data, key_size, master_key));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200252exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 if (key_file != NULL) {
254 fclose(key_file);
255 }
256 mbedtls_platform_zeroize(key_data, sizeof(key_data));
257 if (status != PSA_SUCCESS) {
Gilles Peskine5163a922019-05-27 14:52:34 +0200258 /* If the key creation hasn't happened yet or has failed,
Ronald Cronadc2ff22020-09-16 16:49:27 +0200259 * *master_key is null. psa_destroy_key( 0 ) is
260 * guaranteed to do nothing and return PSA_SUCCESS. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 (void) psa_destroy_key(*master_key);
Ronald Cronadc2ff22020-09-16 16:49:27 +0200262 *master_key = 0;
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100263 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200265}
266
267/* Derive the intermediate keys, using the list of labels provided on
Ronald Cronadc2ff22020-09-16 16:49:27 +0200268 * the command line. On input, *key is the master key identifier.
269 * This function destroys the master key. On successful output, *key
270 * is the identifier of the final derived key.
271 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272static psa_status_t derive_key_ladder(const char *ladder[],
273 size_t ladder_depth,
274 psa_key_id_t *key)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200275{
276 psa_status_t status = PSA_SUCCESS;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4e2cc532019-05-29 14:30:27 +0200278 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200279 size_t i;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 psa_set_key_usage_flags(&attributes,
282 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
283 psa_set_key_algorithm(&attributes, KDF_ALG);
284 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
285 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(KEY_SIZE_BYTES));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200286
287 /* For each label in turn, ... */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 for (i = 0; i < ladder_depth; i++) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200289 /* Start deriving material from the master key (if i=0) or from
290 * the current intermediate key (if i>0). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 PSA_CHECK(psa_key_derivation_setup(&operation, KDF_ALG));
292 PSA_CHECK(psa_key_derivation_input_bytes(
293 &operation, PSA_KEY_DERIVATION_INPUT_SALT,
294 DERIVE_KEY_SALT, DERIVE_KEY_SALT_LENGTH));
295 PSA_CHECK(psa_key_derivation_input_key(
296 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
297 *key));
298 PSA_CHECK(psa_key_derivation_input_bytes(
299 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
300 (uint8_t *) ladder[i], strlen(ladder[i])));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200301 /* When the parent key is not the master key, destroy it,
302 * since it is no longer needed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 PSA_CHECK(psa_destroy_key(*key));
Ronald Cronadc2ff22020-09-16 16:49:27 +0200304 *key = 0;
Gilles Peskine4e2cc532019-05-29 14:30:27 +0200305 /* Derive the next intermediate key from the parent key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 PSA_CHECK(psa_key_derivation_output_key(&attributes, &operation,
307 key));
308 PSA_CHECK(psa_key_derivation_abort(&operation));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200309 }
310
311exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 psa_key_derivation_abort(&operation);
313 if (status != PSA_SUCCESS) {
314 psa_destroy_key(*key);
Ronald Cronadc2ff22020-09-16 16:49:27 +0200315 *key = 0;
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100316 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200318}
319
320/* Derive a wrapping key from the last intermediate key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321static psa_status_t derive_wrapping_key(psa_key_usage_t usage,
322 psa_key_id_t derived_key,
323 psa_key_id_t *wrapping_key)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200324{
325 psa_status_t status = PSA_SUCCESS;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4e2cc532019-05-29 14:30:27 +0200327 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200328
Ronald Cronadc2ff22020-09-16 16:49:27 +0200329 *wrapping_key = 0;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200330
Gilles Peskine2a38e242019-05-29 14:33:00 +0200331 /* Set up a key derivation operation from the key derived from
332 * the master key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 PSA_CHECK(psa_key_derivation_setup(&operation, KDF_ALG));
334 PSA_CHECK(psa_key_derivation_input_bytes(
335 &operation, PSA_KEY_DERIVATION_INPUT_SALT,
336 WRAPPING_KEY_SALT, WRAPPING_KEY_SALT_LENGTH));
337 PSA_CHECK(psa_key_derivation_input_key(
338 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
339 derived_key));
340 PSA_CHECK(psa_key_derivation_input_bytes(
341 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
342 NULL, 0));
Gilles Peskine2a38e242019-05-29 14:33:00 +0200343
344 /* Create the wrapping key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 psa_set_key_usage_flags(&attributes, usage);
346 psa_set_key_algorithm(&attributes, WRAPPING_ALG);
347 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
348 psa_set_key_bits(&attributes, WRAPPING_KEY_BITS);
349 PSA_CHECK(psa_key_derivation_output_key(&attributes, &operation,
350 wrapping_key));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200351
352exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 psa_key_derivation_abort(&operation);
354 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200355}
356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357static psa_status_t wrap_data(const char *input_file_name,
358 const char *output_file_name,
359 psa_key_id_t wrapping_key)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200360{
361 psa_status_t status;
362 FILE *input_file = NULL;
363 FILE *output_file = NULL;
Bence Szépkútiec174e22021-03-19 18:46:15 +0100364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
365 psa_key_type_t key_type;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200366 long input_position;
367 size_t input_size;
Gilles Peskine5e09bc72018-12-21 12:06:15 +0100368 size_t buffer_size = 0;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200369 unsigned char *buffer = NULL;
370 size_t ciphertext_size;
371 wrapped_data_header_t header;
372
373 /* Find the size of the data to wrap. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 SYS_CHECK((input_file = fopen(input_file_name, "rb")) != NULL);
375 SYS_CHECK(fseek(input_file, 0, SEEK_END) == 0);
376 SYS_CHECK((input_position = ftell(input_file)) != -1);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200377#if LONG_MAX > SIZE_MAX
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 if (input_position > SIZE_MAX) {
379 printf("Input file too large.\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200380 status = DEMO_ERROR;
381 goto exit;
382 }
383#endif
384 input_size = input_position;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 PSA_CHECK(psa_get_key_attributes(wrapping_key, &attributes));
386 key_type = psa_get_key_type(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +0100387 buffer_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, WRAPPING_ALG, input_size);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200389 /* Check for integer overflow. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 if (buffer_size < input_size) {
391 printf("Input file too large.\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200392 status = DEMO_ERROR;
393 goto exit;
394 }
395
396 /* Load the data to wrap. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 SYS_CHECK(fseek(input_file, 0, SEEK_SET) == 0);
398 SYS_CHECK((buffer = calloc(1, buffer_size)) != NULL);
399 SYS_CHECK(fread(buffer, 1, input_size, input_file) == input_size);
400 SYS_CHECK(fclose(input_file) == 0);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200401 input_file = NULL;
402
403 /* Construct a header. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 memcpy(&header.magic, WRAPPED_DATA_MAGIC, WRAPPED_DATA_MAGIC_LENGTH);
405 header.ad_size = sizeof(header);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200406 header.payload_size = input_size;
407
408 /* Wrap the data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 PSA_CHECK(psa_generate_random(header.iv, WRAPPING_IV_SIZE));
410 PSA_CHECK(psa_aead_encrypt(wrapping_key, WRAPPING_ALG,
411 header.iv, WRAPPING_IV_SIZE,
412 (uint8_t *) &header, sizeof(header),
413 buffer, input_size,
414 buffer, buffer_size,
415 &ciphertext_size));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200416
417 /* Write the output. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 SYS_CHECK((output_file = fopen(output_file_name, "wb")) != NULL);
419 SYS_CHECK(fwrite(&header, 1, sizeof(header),
420 output_file) == sizeof(header));
421 SYS_CHECK(fwrite(buffer, 1, ciphertext_size,
422 output_file) == ciphertext_size);
423 SYS_CHECK(fclose(output_file) == 0);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200424 output_file = NULL;
425
426exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 if (input_file != NULL) {
428 fclose(input_file);
429 }
430 if (output_file != NULL) {
431 fclose(output_file);
432 }
433 if (buffer != NULL) {
434 mbedtls_platform_zeroize(buffer, buffer_size);
435 }
436 free(buffer);
437 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200438}
439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440static psa_status_t unwrap_data(const char *input_file_name,
441 const char *output_file_name,
442 psa_key_id_t wrapping_key)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200443{
444 psa_status_t status;
445 FILE *input_file = NULL;
446 FILE *output_file = NULL;
Bence Szépkútiec174e22021-03-19 18:46:15 +0100447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
448 psa_key_type_t key_type;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200449 unsigned char *buffer = NULL;
Gilles Peskine5e09bc72018-12-21 12:06:15 +0100450 size_t ciphertext_size = 0;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200451 size_t plaintext_size;
452 wrapped_data_header_t header;
453 unsigned char extra_byte;
454
455 /* Load and validate the header. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 SYS_CHECK((input_file = fopen(input_file_name, "rb")) != NULL);
457 SYS_CHECK(fread(&header, 1, sizeof(header),
458 input_file) == sizeof(header));
459 if (memcmp(&header.magic, WRAPPED_DATA_MAGIC,
460 WRAPPED_DATA_MAGIC_LENGTH) != 0) {
461 printf("The input does not start with a valid magic header.\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200462 status = DEMO_ERROR;
463 goto exit;
464 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 if (header.ad_size != sizeof(header)) {
466 printf("The header size is not correct.\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200467 status = DEMO_ERROR;
468 goto exit;
469 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 PSA_CHECK(psa_get_key_attributes(wrapping_key, &attributes));
471 key_type = psa_get_key_type(&attributes);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200472 ciphertext_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, WRAPPING_ALG, header.payload_size);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200474 /* Check for integer overflow. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 if (ciphertext_size < header.payload_size) {
476 printf("Input file too large.\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200477 status = DEMO_ERROR;
478 goto exit;
479 }
480
481 /* Load the payload data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 SYS_CHECK((buffer = calloc(1, ciphertext_size)) != NULL);
483 SYS_CHECK(fread(buffer, 1, ciphertext_size,
484 input_file) == ciphertext_size);
485 if (fread(&extra_byte, 1, 1, input_file) != 0) {
486 printf("Extra garbage after ciphertext\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200487 status = DEMO_ERROR;
488 goto exit;
489 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 SYS_CHECK(fclose(input_file) == 0);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200491 input_file = NULL;
492
493 /* Unwrap the data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 PSA_CHECK(psa_aead_decrypt(wrapping_key, WRAPPING_ALG,
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 printf("Incorrect payload size in the header.\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200502 status = DEMO_ERROR;
503 goto exit;
504 }
505
506 /* Write the output. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 SYS_CHECK((output_file = fopen(output_file_name, "wb")) != NULL);
508 SYS_CHECK(fwrite(buffer, 1, plaintext_size,
509 output_file) == plaintext_size);
510 SYS_CHECK(fclose(output_file) == 0);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200511 output_file = NULL;
512
513exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 if (input_file != NULL) {
515 fclose(input_file);
516 }
517 if (output_file != NULL) {
518 fclose(output_file);
519 }
520 if (buffer != NULL) {
521 mbedtls_platform_zeroize(buffer, ciphertext_size);
522 }
523 free(buffer);
524 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200525}
526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527static psa_status_t run(enum program_mode mode,
528 const char *key_file_name,
529 const char *ladder[], size_t ladder_depth,
530 const char *input_file_name,
531 const char *output_file_name)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200532{
533 psa_status_t status = PSA_SUCCESS;
Ronald Cronadc2ff22020-09-16 16:49:27 +0200534 psa_key_id_t derivation_key = 0;
535 psa_key_id_t wrapping_key = 0;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200536
537 /* Initialize the PSA crypto library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 PSA_CHECK(psa_crypto_init());
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200539
540 /* Generate mode is unlike the others. Generate the master key and exit. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 if (mode == MODE_GENERATE) {
542 return generate(key_file_name);
543 }
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200544
545 /* Read the master key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 PSA_CHECK(import_key_from_file(PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT,
547 KDF_ALG,
548 key_file_name,
549 &derivation_key));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200550
551 /* Calculate the derived key for this session. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 PSA_CHECK(derive_key_ladder(ladder, ladder_depth,
553 &derivation_key));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 switch (mode) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200556 case MODE_SAVE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 PSA_CHECK(save_key(derivation_key, output_file_name));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200558 break;
559 case MODE_UNWRAP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 PSA_CHECK(derive_wrapping_key(PSA_KEY_USAGE_DECRYPT,
561 derivation_key,
562 &wrapping_key));
563 PSA_CHECK(unwrap_data(input_file_name, output_file_name,
564 wrapping_key));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200565 break;
566 case MODE_WRAP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 PSA_CHECK(derive_wrapping_key(PSA_KEY_USAGE_ENCRYPT,
568 derivation_key,
569 &wrapping_key));
570 PSA_CHECK(wrap_data(input_file_name, output_file_name,
571 wrapping_key));
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200572 break;
573 default:
574 /* Unreachable but some compilers don't realize it. */
575 break;
576 }
577
578exit:
Ronald Cronadc2ff22020-09-16 16:49:27 +0200579 /* Destroy any remaining key. Deinitializing the crypto library would do
580 * this anyway since they are volatile keys, but explicitly destroying
Ronald Cron77c89f52020-11-10 17:45:56 +0100581 * keys makes the code easier to reuse. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 (void) psa_destroy_key(derivation_key);
583 (void) psa_destroy_key(wrapping_key);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200584 /* Deinitialize the PSA crypto library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 mbedtls_psa_crypto_free();
586 return status;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200587}
588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589static void usage(void)
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200590{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 printf("Usage: key_ladder_demo MODE [OPTION=VALUE]...\n");
592 printf("Demonstrate the usage of a key derivation ladder.\n");
593 printf("\n");
594 printf("Modes:\n");
595 printf(" generate Generate the master key\n");
596 printf(" save Save the derived key\n");
597 printf(" unwrap Unwrap (decrypt) input with the derived key\n");
598 printf(" wrap Wrap (encrypt) input with the derived key\n");
599 printf("\n");
600 printf("Options:\n");
601 printf(" input=FILENAME Input file (required for wrap/unwrap)\n");
602 printf(" master=FILENAME File containing the master key (default: master.key)\n");
603 printf(" output=FILENAME Output file (required for save/wrap/unwrap)\n");
604 printf(" label=TEXT Label for the key derivation.\n");
605 printf(" This may be repeated multiple times.\n");
606 printf(" To get the same key, you must use the same master key\n");
607 printf(" and the same sequence of labels.\n");
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200608}
609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610int main(int argc, char *argv[])
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200611{
Gilles Peskine738f0172019-01-02 17:25:16 +0100612 const char *key_file_name = "master.key";
613 const char *input_file_name = NULL;
614 const char *output_file_name = NULL;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200615 const char *ladder[MAX_LADDER_DEPTH];
616 size_t ladder_depth = 0;
617 int i;
618 enum program_mode mode;
619 psa_status_t status;
620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if (argc <= 1 ||
622 strcmp(argv[1], "help") == 0 ||
623 strcmp(argv[1], "-help") == 0 ||
624 strcmp(argv[1], "--help") == 0) {
625 usage();
626 return EXIT_SUCCESS;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200627 }
628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 for (i = 2; i < argc; i++) {
630 char *q = strchr(argv[i], '=');
631 if (q == NULL) {
632 printf("Missing argument to option %s\n", argv[i]);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200633 goto usage_failure;
634 }
635 *q = 0;
636 ++q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100637 if (strcmp(argv[i], "input") == 0) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200638 input_file_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 } else if (strcmp(argv[i], "label") == 0) {
640 if (ladder_depth == MAX_LADDER_DEPTH) {
641 printf("Maximum ladder depth %u exceeded.\n",
642 (unsigned) MAX_LADDER_DEPTH);
643 return EXIT_FAILURE;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200644 }
645 ladder[ladder_depth] = q;
646 ++ladder_depth;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 } else if (strcmp(argv[i], "master") == 0) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200648 key_file_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 } else if (strcmp(argv[i], "output") == 0) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200650 output_file_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 } else {
652 printf("Unknown option: %s\n", argv[i]);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200653 goto usage_failure;
654 }
655 }
656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 if (strcmp(argv[1], "generate") == 0) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200658 mode = MODE_GENERATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 } else if (strcmp(argv[1], "save") == 0) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200660 mode = MODE_SAVE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 } else if (strcmp(argv[1], "unwrap") == 0) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200662 mode = MODE_UNWRAP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 } else if (strcmp(argv[1], "wrap") == 0) {
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200664 mode = MODE_WRAP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 } else {
666 printf("Unknown action: %s\n", argv[1]);
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200667 goto usage_failure;
668 }
669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100670 if (input_file_name == NULL &&
671 (mode == MODE_WRAP || mode == MODE_UNWRAP)) {
672 printf("Required argument missing: input\n");
673 return DEMO_ERROR;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200674 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 if (output_file_name == NULL &&
676 (mode == MODE_SAVE || mode == MODE_WRAP || mode == MODE_UNWRAP)) {
677 printf("Required argument missing: output\n");
678 return DEMO_ERROR;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200679 }
680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 status = run(mode, key_file_name,
682 ladder, ladder_depth,
683 input_file_name, output_file_name);
684 return status == PSA_SUCCESS ?
685 EXIT_SUCCESS :
686 EXIT_FAILURE;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200687
688usage_failure:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 usage();
690 return EXIT_FAILURE;
Gilles Peskinef0fa4362018-07-16 17:08:43 +0200691}
David Horstmannbcc535c2022-11-10 18:55:00 +0000692#endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C &&
693 MBEDTLS_AES_C && MBEDTLS_CCM_C &&
694 MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */