blob: e21d1abf0300d7dd3ae3572a3ebadcb229e79107 [file] [log] [blame]
Gilles Peskinef0fa4362018-07-16 17:08:43 +02001#!/bin/sh
Bence Szépkúti700ee442020-05-26 00:33:31 +02002#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# 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úti700ee442020-05-26 00:33:31 +020017
Gilles Peskinef0fa4362018-07-16 17:08:43 +020018set -e -u
19
David Horstmann4dfa3682021-11-26 17:58:05 +000020program_name="key_ladder_demo"
21program="${0%/*}/$program_name"
Gilles Peskinef0fa4362018-07-16 17:08:43 +020022files_to_clean=
23
David Horstmann4dfa3682021-11-26 17:58:05 +000024if [ ! -e "$program" ]; then
25 # Look for programs in the current directory and the directories above it
26 for dir in "." ".." "../.."; do
27 program="$dir/programs/psa/$program_name"
28 if [ -e "$program" ]; then
29 break
30 fi
31 done
32 if [ ! -e "$program" ]; then
33 echo "Could not find $program_name executable"
34
35 echo "If building out-of-tree, this script must be run" \
36 "from the project build directory."
37 exit 1
38 fi
39fi
40
Gilles Peskinef0fa4362018-07-16 17:08:43 +020041run () {
42 echo
43 echo "# $1"
44 shift
45 echo "+ $*"
46 "$@"
47}
48
49if [ -e master.key ]; then
50 echo "# Reusing the existing master.key file."
51else
52 files_to_clean="$files_to_clean master.key"
53 run "Generate a master key." \
54 "$program" generate master=master.key
55fi
56
57files_to_clean="$files_to_clean input.txt hello_world.wrap"
58echo "Here is some input. See it wrapped." >input.txt
59run "Derive a key and wrap some data with it." \
60 "$program" wrap master=master.key label=hello label=world \
61 input=input.txt output=hello_world.wrap
62
63files_to_clean="$files_to_clean hello_world.txt"
64run "Derive the same key again and unwrap the data." \
65 "$program" unwrap master=master.key label=hello label=world \
66 input=hello_world.wrap output=hello_world.txt
67run "Compare the unwrapped data with the original input." \
68 cmp input.txt hello_world.txt
69
70files_to_clean="$files_to_clean hellow_orld.txt"
71! run "Derive a different key and attempt to unwrap the data. This must fail." \
72 "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld
73
74files_to_clean="$files_to_clean hello.key"
75run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \
76 "$program" save master=master.key label=hello \
77 input=hello_world.wrap output=hello.key
78run "Check that we get the same key by unwrapping data made by the other key." \
79 "$program" unwrap master=hello.key label=world \
80 input=hello_world.wrap output=hello_world.txt
81
82# Cleanup
83rm -f $files_to_clean