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 |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 17 | |
Gilles Peskine | f0fa436 | 2018-07-16 17:08:43 +0200 | [diff] [blame] | 18 | set -e -u |
| 19 | |
| 20 | program="${0%/*}"/key_ladder_demo |
| 21 | files_to_clean= |
| 22 | |
| 23 | run () { |
| 24 | echo |
| 25 | echo "# $1" |
| 26 | shift |
| 27 | echo "+ $*" |
| 28 | "$@" |
| 29 | } |
| 30 | |
| 31 | if [ -e master.key ]; then |
| 32 | echo "# Reusing the existing master.key file." |
| 33 | else |
| 34 | files_to_clean="$files_to_clean master.key" |
| 35 | run "Generate a master key." \ |
| 36 | "$program" generate master=master.key |
| 37 | fi |
| 38 | |
| 39 | files_to_clean="$files_to_clean input.txt hello_world.wrap" |
| 40 | echo "Here is some input. See it wrapped." >input.txt |
| 41 | run "Derive a key and wrap some data with it." \ |
| 42 | "$program" wrap master=master.key label=hello label=world \ |
| 43 | input=input.txt output=hello_world.wrap |
| 44 | |
| 45 | files_to_clean="$files_to_clean hello_world.txt" |
| 46 | run "Derive the same key again and unwrap the data." \ |
| 47 | "$program" unwrap master=master.key label=hello label=world \ |
| 48 | input=hello_world.wrap output=hello_world.txt |
| 49 | run "Compare the unwrapped data with the original input." \ |
| 50 | cmp input.txt hello_world.txt |
| 51 | |
| 52 | files_to_clean="$files_to_clean hellow_orld.txt" |
| 53 | ! run "Derive a different key and attempt to unwrap the data. This must fail." \ |
| 54 | "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld |
| 55 | |
| 56 | files_to_clean="$files_to_clean hello.key" |
| 57 | run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \ |
| 58 | "$program" save master=master.key label=hello \ |
| 59 | input=hello_world.wrap output=hello.key |
| 60 | run "Check that we get the same key by unwrapping data made by the other key." \ |
| 61 | "$program" unwrap master=hello.key label=world \ |
| 62 | input=hello_world.wrap output=hello_world.txt |
| 63 | |
| 64 | # Cleanup |
| 65 | rm -f $files_to_clean |