quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

key_ladder_demo.sh (2016B)


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