blob: 9559877c645227001fea7645cc3ec9743664093d [file] [log] [blame]
Gilles Peskinef0fa4362018-07-16 17:08:43 +02001#!/bin/sh
Bence Szépkúti700ee442020-05-26 00:33:31 +02002#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Bence Szépkúti700ee442020-05-26 00:33:31 +02005
Valerio Setti5533cc82025-04-10 14:15:41 +02006. "${0%/*}/../../framework/scripts/project_detection.sh"
Harry Ramsey4e1a12e2025-02-17 20:56:22 +00007. "${0%/*}/../../framework/scripts/demo_common.sh"
Gilles Peskinef0fa4362018-07-16 17:08:43 +02008
Gilles Peskined1b5f6f2020-04-23 17:33:36 +02009msg <<'EOF'
10This script demonstrates the use of the PSA cryptography interface to
Gilles Peskine086f85f2020-04-26 22:43:05 +020011create a master key, derive a key from it and use that derived key to
12wrap some data using an AEAD algorithm.
Gilles Peskined1b5f6f2020-04-23 17:33:36 +020013EOF
Gilles Peskinef0fa4362018-07-16 17:08:43 +020014
Gilles Peskine03be2342020-04-22 21:45:49 +020015depends_on MBEDTLS_SHA256_C MBEDTLS_MD_C MBEDTLS_AES_C MBEDTLS_CCM_C MBEDTLS_PSA_CRYPTO_C MBEDTLS_FS_IO
16
Gilles Peskined1b5f6f2020-04-23 17:33:36 +020017program="${0%/*}"/key_ladder_demo
Gilles Peskinef0fa4362018-07-16 17:08:43 +020018
19if [ -e master.key ]; then
20 echo "# Reusing the existing master.key file."
21else
22 files_to_clean="$files_to_clean master.key"
23 run "Generate a master key." \
24 "$program" generate master=master.key
25fi
26
27files_to_clean="$files_to_clean input.txt hello_world.wrap"
28echo "Here is some input. See it wrapped." >input.txt
29run "Derive a key and wrap some data with it." \
30 "$program" wrap master=master.key label=hello label=world \
31 input=input.txt output=hello_world.wrap
32
33files_to_clean="$files_to_clean hello_world.txt"
34run "Derive the same key again and unwrap the data." \
35 "$program" unwrap master=master.key label=hello label=world \
36 input=hello_world.wrap output=hello_world.txt
37run "Compare the unwrapped data with the original input." \
38 cmp input.txt hello_world.txt
39
40files_to_clean="$files_to_clean hellow_orld.txt"
Gilles Peskine086f85f2020-04-26 22:43:05 +020041run_bad "Derive a different key and attempt to unwrap the data." \
Gilles Peskinef0fa4362018-07-16 17:08:43 +020042 "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld
43
44files_to_clean="$files_to_clean hello.key"
45run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \
46 "$program" save master=master.key label=hello \
47 input=hello_world.wrap output=hello.key
48run "Check that we get the same key by unwrapping data made by the other key." \
49 "$program" unwrap master=hello.key label=world \
50 input=hello_world.wrap output=hello_world.txt
51
Gilles Peskined1b5f6f2020-04-23 17:33:36 +020052cleanup