blob: fc2ef336fadb9a17a0cb4e879a3c14275f79bea2 [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
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#
18# This file is part of Mbed TLS (https://tls.mbed.org)
19
Gilles Peskinef0fa4362018-07-16 17:08:43 +020020set -e -u
21
22program="${0%/*}"/key_ladder_demo
23files_to_clean=
24
25run () {
26 echo
27 echo "# $1"
28 shift
29 echo "+ $*"
30 "$@"
31}
32
33if [ -e master.key ]; then
34 echo "# Reusing the existing master.key file."
35else
36 files_to_clean="$files_to_clean master.key"
37 run "Generate a master key." \
38 "$program" generate master=master.key
39fi
40
41files_to_clean="$files_to_clean input.txt hello_world.wrap"
42echo "Here is some input. See it wrapped." >input.txt
43run "Derive a key and wrap some data with it." \
44 "$program" wrap master=master.key label=hello label=world \
45 input=input.txt output=hello_world.wrap
46
47files_to_clean="$files_to_clean hello_world.txt"
48run "Derive the same key again and unwrap the data." \
49 "$program" unwrap master=master.key label=hello label=world \
50 input=hello_world.wrap output=hello_world.txt
51run "Compare the unwrapped data with the original input." \
52 cmp input.txt hello_world.txt
53
54files_to_clean="$files_to_clean hellow_orld.txt"
55! run "Derive a different key and attempt to unwrap the data. This must fail." \
56 "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld
57
58files_to_clean="$files_to_clean hello.key"
59run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \
60 "$program" save master=master.key label=hello \
61 input=hello_world.wrap output=hello.key
62run "Check that we get the same key by unwrapping data made by the other key." \
63 "$program" unwrap master=hello.key label=world \
64 input=hello_world.wrap output=hello_world.txt
65
66# Cleanup
67rm -f $files_to_clean