1#!/bin/sh 2# 3# Copyright The Mbed TLS Contributors 4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 5 6set -e -u 7 8program_name="key_ladder_demo" 9program="${0%/*}/$program_name" 10files_to_clean= 11 12if [ ! -e "$program" ]; then 13 # Look for programs in the current directory and the directories above it 14 for dir in "." ".." "../.."; do 15 program="$dir/programs/psa/$program_name" 16 if [ -e "$program" ]; then 17 break 18 fi 19 done 20 if [ ! -e "$program" ]; then 21 echo "Could not find $program_name executable" 22 23 echo "If building out-of-tree, this script must be run" \ 24 "from the project build directory." 25 exit 1 26 fi 27fi 28 29run () { 30 echo 31 echo "# $1" 32 shift 33 echo "+ $*" 34 "$@" 35} 36 37if [ -e master.key ]; then 38 echo "# Reusing the existing master.key file." 39else 40 files_to_clean="$files_to_clean master.key" 41 run "Generate a master key." \ 42 "$program" generate master=master.key 43fi 44 45files_to_clean="$files_to_clean input.txt hello_world.wrap" 46echo "Here is some input. See it wrapped." >input.txt 47run "Derive a key and wrap some data with it." \ 48 "$program" wrap master=master.key label=hello label=world \ 49 input=input.txt output=hello_world.wrap 50 51files_to_clean="$files_to_clean hello_world.txt" 52run "Derive the same key again and unwrap the data." \ 53 "$program" unwrap master=master.key label=hello label=world \ 54 input=hello_world.wrap output=hello_world.txt 55run "Compare the unwrapped data with the original input." \ 56 cmp input.txt hello_world.txt 57 58files_to_clean="$files_to_clean hellow_orld.txt" 59! run "Derive a different key and attempt to unwrap the data. This must fail." \ 60 "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld 61 62files_to_clean="$files_to_clean hello.key" 63run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \ 64 "$program" save master=master.key label=hello \ 65 input=hello_world.wrap output=hello.key 66run "Check that we get the same key by unwrapping data made by the other key." \ 67 "$program" unwrap master=hello.key label=world \ 68 input=hello_world.wrap output=hello_world.txt 69 70# Cleanup 71rm -f $files_to_clean 72