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