blob: 3ffbd8b011b98d232491b01d2e157d7fd2e587e1 [file] [log] [blame]
Gilles Peskinef0fa4362018-07-16 17:08:43 +02001#!/bin/sh
Bence Szépkúti700ee442020-05-26 00:33:31 +02002#
3# Copyright (C) 2018, Arm Limited, All Rights Reserved
4#
5# This file is part of Mbed TLS (https://tls.mbed.org)
6
Gilles Peskinef0fa4362018-07-16 17:08:43 +02007set -e -u
8
9program="${0%/*}"/key_ladder_demo
10files_to_clean=
11
12run () {
13 echo
14 echo "# $1"
15 shift
16 echo "+ $*"
17 "$@"
18}
19
20if [ -e master.key ]; then
21 echo "# Reusing the existing master.key file."
22else
23 files_to_clean="$files_to_clean master.key"
24 run "Generate a master key." \
25 "$program" generate master=master.key
26fi
27
28files_to_clean="$files_to_clean input.txt hello_world.wrap"
29echo "Here is some input. See it wrapped." >input.txt
30run "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
34files_to_clean="$files_to_clean hello_world.txt"
35run "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
38run "Compare the unwrapped data with the original input." \
39 cmp input.txt hello_world.txt
40
41files_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
45files_to_clean="$files_to_clean hello.key"
46run "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
49run "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
54rm -f $files_to_clean