| Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 2 | # |
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 5 | |
| Valerio Setti | 5533cc8 | 2025-04-10 14:15:41 +0200 | [diff] [blame] | 6 | . "${0%/*}/../../framework/scripts/project_detection.sh" |
| Harry Ramsey | 4e1a12e | 2025-02-17 20:56:22 +0000 | [diff] [blame] | 7 | . "${0%/*}/../../framework/scripts/demo_common.sh" |
| Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 8 | |
| Gilles Peskine | d1b5f6f | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 9 | msg <<'EOF' |
| 10 | This script demonstrates the use of the PSA cryptography interface to |
| Gilles Peskine | 086f85f | 2020-04-26 22:43:05 +0200 | [diff] [blame] | 11 | create a master key, derive a key from it and use that derived key to |
| 12 | wrap some data using an AEAD algorithm. |
| Gilles Peskine | d1b5f6f | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 13 | EOF |
| Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 14 | |
| Gilles Peskine | 03be234 | 2020-04-22 21:45:49 +0200 | [diff] [blame] | 15 | depends_on MBEDTLS_SHA256_C MBEDTLS_MD_C MBEDTLS_AES_C MBEDTLS_CCM_C MBEDTLS_PSA_CRYPTO_C MBEDTLS_FS_IO |
| 16 | |
| Gilles Peskine | d1b5f6f | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 17 | program="${0%/*}"/key_ladder_demo |
| Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 18 | |
| 19 | if [ -e master.key ]; then |
| 20 | echo "# Reusing the existing master.key file." |
| 21 | else |
| 22 | files_to_clean="$files_to_clean master.key" |
| 23 | run "Generate a master key." \ |
| 24 | "$program" generate master=master.key |
| 25 | fi |
| 26 | |
| 27 | files_to_clean="$files_to_clean input.txt hello_world.wrap" |
| 28 | echo "Here is some input. See it wrapped." >input.txt |
| 29 | run "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 | |
| 33 | files_to_clean="$files_to_clean hello_world.txt" |
| 34 | run "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 |
| 37 | run "Compare the unwrapped data with the original input." \ |
| 38 | cmp input.txt hello_world.txt |
| 39 | |
| 40 | files_to_clean="$files_to_clean hellow_orld.txt" |
| Gilles Peskine | 086f85f | 2020-04-26 22:43:05 +0200 | [diff] [blame] | 41 | run_bad "Derive a different key and attempt to unwrap the data." \ |
| Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 42 | "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld |
| 43 | |
| 44 | files_to_clean="$files_to_clean hello.key" |
| 45 | run "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 |
| 48 | run "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 Peskine | d1b5f6f | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 52 | cleanup |