1a8e1175bSopenharmony_ci#!/bin/sh
2a8e1175bSopenharmony_ci#
3a8e1175bSopenharmony_ci# Copyright The Mbed TLS Contributors
4a8e1175bSopenharmony_ci# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5a8e1175bSopenharmony_ci
6a8e1175bSopenharmony_ci. "${0%/*}/../demo_common.sh"
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_cimsg <<'EOF'
9a8e1175bSopenharmony_ciThis script demonstrates the use of the PSA cryptography interface to
10a8e1175bSopenharmony_cicreate a master key, derive a key from it and use that derived key to
11a8e1175bSopenharmony_ciwrap some data using an AEAD algorithm.
12a8e1175bSopenharmony_ciEOF
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_cidepends_on MBEDTLS_SHA256_C MBEDTLS_MD_C MBEDTLS_AES_C MBEDTLS_CCM_C MBEDTLS_PSA_CRYPTO_C MBEDTLS_FS_IO
15a8e1175bSopenharmony_ci
16a8e1175bSopenharmony_ciprogram="${0%/*}"/key_ladder_demo
17a8e1175bSopenharmony_ci
18a8e1175bSopenharmony_ciif [ -e master.key ]; then
19a8e1175bSopenharmony_ci    echo "# Reusing the existing master.key file."
20a8e1175bSopenharmony_cielse
21a8e1175bSopenharmony_ci    files_to_clean="$files_to_clean master.key"
22a8e1175bSopenharmony_ci    run "Generate a master key." \
23a8e1175bSopenharmony_ci        "$program" generate master=master.key
24a8e1175bSopenharmony_cifi
25a8e1175bSopenharmony_ci
26a8e1175bSopenharmony_cifiles_to_clean="$files_to_clean input.txt hello_world.wrap"
27a8e1175bSopenharmony_ciecho "Here is some input. See it wrapped." >input.txt
28a8e1175bSopenharmony_cirun "Derive a key and wrap some data with it." \
29a8e1175bSopenharmony_ci    "$program" wrap master=master.key label=hello label=world \
30a8e1175bSopenharmony_ci               input=input.txt output=hello_world.wrap
31a8e1175bSopenharmony_ci
32a8e1175bSopenharmony_cifiles_to_clean="$files_to_clean hello_world.txt"
33a8e1175bSopenharmony_cirun "Derive the same key again and unwrap the data." \
34a8e1175bSopenharmony_ci    "$program" unwrap master=master.key label=hello label=world \
35a8e1175bSopenharmony_ci               input=hello_world.wrap output=hello_world.txt
36a8e1175bSopenharmony_cirun "Compare the unwrapped data with the original input." \
37a8e1175bSopenharmony_ci    cmp input.txt hello_world.txt
38a8e1175bSopenharmony_ci
39a8e1175bSopenharmony_cifiles_to_clean="$files_to_clean hellow_orld.txt"
40a8e1175bSopenharmony_cirun_bad "Derive a different key and attempt to unwrap the data." \
41a8e1175bSopenharmony_ci  "$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld
42a8e1175bSopenharmony_ci
43a8e1175bSopenharmony_cifiles_to_clean="$files_to_clean hello.key"
44a8e1175bSopenharmony_cirun "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \
45a8e1175bSopenharmony_ci    "$program" save master=master.key label=hello \
46a8e1175bSopenharmony_ci               input=hello_world.wrap output=hello.key
47a8e1175bSopenharmony_cirun "Check that we get the same key by unwrapping data made by the other key." \
48a8e1175bSopenharmony_ci    "$program" unwrap master=hello.key label=world \
49a8e1175bSopenharmony_ci               input=hello_world.wrap output=hello_world.txt
50a8e1175bSopenharmony_ci
51a8e1175bSopenharmony_cicleanup
52