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 | 7ff7965 | 2023-11-03 12:04:52 +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 | |
Gilles Peskine | d0a4dc8 | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 6 | . "${0%/*}/../demo_common.sh" |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 7 | |
Gilles Peskine | d0a4dc8 | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 8 | msg <<'EOF' |
| 9 | This script demonstrates the use of the PSA cryptography interface to |
Gilles Peskine | 61ae791 | 2020-04-26 22:43:05 +0200 | [diff] [blame] | 10 | create a master key, derive a key from it and use that derived key to |
| 11 | wrap some data using an AEAD algorithm. |
Gilles Peskine | d0a4dc8 | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 12 | EOF |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 13 | |
Gilles Peskine | 019416c | 2020-04-22 21:45:49 +0200 | [diff] [blame] | 14 | depends_on MBEDTLS_SHA256_C MBEDTLS_MD_C MBEDTLS_AES_C MBEDTLS_CCM_C MBEDTLS_PSA_CRYPTO_C MBEDTLS_FS_IO |
| 15 | |
Gilles Peskine | d0a4dc8 | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 16 | program="${0%/*}"/key_ladder_demo |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 17 | |
| 18 | if [ -e master.key ]; then |
| 19 | echo "# Reusing the existing master.key file." |
| 20 | else |
| 21 | files_to_clean="$files_to_clean master.key" |
| 22 | run "Generate a master key." \ |
| 23 | "$program" generate master=master.key |
| 24 | fi |
| 25 | |
| 26 | files_to_clean="$files_to_clean input.txt hello_world.wrap" |
| 27 | echo "Here is some input. See it wrapped." >input.txt |
| 28 | run "Derive a key and wrap some data with it." \ |
| 29 | "$program" wrap master=master.key label=hello label=world \ |
| 30 | input=input.txt output=hello_world.wrap |
| 31 | |
| 32 | files_to_clean="$files_to_clean hello_world.txt" |
| 33 | run "Derive the same key again and unwrap the data." \ |
| 34 | "$program" unwrap master=master.key label=hello label=world \ |
| 35 | input=hello_world.wrap output=hello_world.txt |
| 36 | run "Compare the unwrapped data with the original input." \ |
| 37 | cmp input.txt hello_world.txt |
| 38 | |
| 39 | files_to_clean="$files_to_clean hellow_orld.txt" |
Gilles Peskine | 61ae791 | 2020-04-26 22:43:05 +0200 | [diff] [blame] | 40 | run_bad "Derive a different key and attempt to unwrap the data." \ |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 41 | "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld |
| 42 | |
| 43 | files_to_clean="$files_to_clean hello.key" |
| 44 | run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \ |
| 45 | "$program" save master=master.key label=hello \ |
| 46 | input=hello_world.wrap output=hello.key |
| 47 | run "Check that we get the same key by unwrapping data made by the other key." \ |
| 48 | "$program" unwrap master=hello.key label=world \ |
| 49 | input=hello_world.wrap output=hello_world.txt |
| 50 | |
Gilles Peskine | d0a4dc8 | 2020-04-23 17:33:36 +0200 | [diff] [blame] | 51 | cleanup |