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 | # |
| 3 | # Copyright (C) 2018, Arm Limited, All Rights Reserved |
| 4 | # |
| 5 | # This file is part of Mbed TLS (https://tls.mbed.org) |
| 6 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 7 | set -e -u |
| 8 | |
| 9 | program="${0%/*}"/key_ladder_demo |
| 10 | files_to_clean= |
| 11 | |
| 12 | run () { |
| 13 | echo |
| 14 | echo "# $1" |
| 15 | shift |
| 16 | echo "+ $*" |
| 17 | "$@" |
| 18 | } |
| 19 | |
| 20 | if [ -e master.key ]; then |
| 21 | echo "# Reusing the existing master.key file." |
| 22 | else |
| 23 | files_to_clean="$files_to_clean master.key" |
| 24 | run "Generate a master key." \ |
| 25 | "$program" generate master=master.key |
| 26 | fi |
| 27 | |
| 28 | files_to_clean="$files_to_clean input.txt hello_world.wrap" |
| 29 | echo "Here is some input. See it wrapped." >input.txt |
| 30 | run "Derive a key and wrap some data with it." \ |
| 31 | "$program" wrap master=master.key label=hello label=world \ |
| 32 | input=input.txt output=hello_world.wrap |
| 33 | |
| 34 | files_to_clean="$files_to_clean hello_world.txt" |
| 35 | run "Derive the same key again and unwrap the data." \ |
| 36 | "$program" unwrap master=master.key label=hello label=world \ |
| 37 | input=hello_world.wrap output=hello_world.txt |
| 38 | run "Compare the unwrapped data with the original input." \ |
| 39 | cmp input.txt hello_world.txt |
| 40 | |
| 41 | files_to_clean="$files_to_clean hellow_orld.txt" |
| 42 | ! run "Derive a different key and attempt to unwrap the data. This must fail." \ |
| 43 | "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld |
| 44 | |
| 45 | files_to_clean="$files_to_clean hello.key" |
| 46 | run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \ |
| 47 | "$program" save master=master.key label=hello \ |
| 48 | input=hello_world.wrap output=hello.key |
| 49 | run "Check that we get the same key by unwrapping data made by the other key." \ |
| 50 | "$program" unwrap master=hello.key label=world \ |
| 51 | input=hello_world.wrap output=hello_world.txt |
| 52 | |
| 53 | # Cleanup |
| 54 | rm -f $files_to_clean |