blob: dbe925b51cd2416a0766611e3988150578a62113 [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 Peskined0a4dc82020-04-23 17:33:36 +020018. "${0%/*}/../demo_common.sh"
Gilles Peskinef0fa4362018-07-16 17:08:43 +020019
Gilles Peskined0a4dc82020-04-23 17:33:36 +020020msg <<'EOF'
21This script demonstrates the use of the PSA cryptography interface to
22create a master key, derive a key from it and use that key to wrap
23the derived key using an AEAD algorithm.
24EOF
Gilles Peskinef0fa4362018-07-16 17:08:43 +020025
Gilles Peskine019416c2020-04-22 21:45:49 +020026depends_on MBEDTLS_SHA256_C MBEDTLS_MD_C MBEDTLS_AES_C MBEDTLS_CCM_C MBEDTLS_PSA_CRYPTO_C MBEDTLS_FS_IO
27
Gilles Peskined0a4dc82020-04-23 17:33:36 +020028program="${0%/*}"/key_ladder_demo
Gilles Peskinef0fa4362018-07-16 17:08:43 +020029
30if [ -e master.key ]; then
31 echo "# Reusing the existing master.key file."
32else
33 files_to_clean="$files_to_clean master.key"
34 run "Generate a master key." \
35 "$program" generate master=master.key
36fi
37
38files_to_clean="$files_to_clean input.txt hello_world.wrap"
39echo "Here is some input. See it wrapped." >input.txt
40run "Derive a key and wrap some data with it." \
41 "$program" wrap master=master.key label=hello label=world \
42 input=input.txt output=hello_world.wrap
43
44files_to_clean="$files_to_clean hello_world.txt"
45run "Derive the same key again and unwrap the data." \
46 "$program" unwrap master=master.key label=hello label=world \
47 input=hello_world.wrap output=hello_world.txt
48run "Compare the unwrapped data with the original input." \
49 cmp input.txt hello_world.txt
50
51files_to_clean="$files_to_clean hellow_orld.txt"
Gilles Peskined0a4dc82020-04-23 17:33:36 +020052run_bad "Derive a different key and attempt to unwrap the data. This must fail." \
Gilles Peskinef0fa4362018-07-16 17:08:43 +020053 "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld
54
55files_to_clean="$files_to_clean hello.key"
56run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \
57 "$program" save master=master.key label=hello \
58 input=hello_world.wrap output=hello.key
59run "Check that we get the same key by unwrapping data made by the other key." \
60 "$program" unwrap master=hello.key label=world \
61 input=hello_world.wrap output=hello_world.txt
62
Gilles Peskined0a4dc82020-04-23 17:33:36 +020063cleanup