xref: /aosp_15_r20/external/vboot_reference/tests/gen_test_keys.sh (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1#!/bin/bash
2
3# Copyright 2010 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# Generate test keys for use by the tests.
8
9# Load common constants and variables.
10. "$(dirname "$0")/common.sh"
11
12set -e
13
14sha_types=( 1 256 512 )
15
16# Generate RSA test keys of various lengths.
17function generate_keys {
18  key_index=0
19  key_name_base="${TESTKEY_DIR}/key_rsa"
20  for i in "${key_lengths[@]}"
21  do
22    key_base="${key_name_base}${i}"
23    if [ -f "${key_base}.keyb" ]; then
24      key_index=$((key_index + 1))
25      continue
26    fi
27
28    # Extract exponent from key_length name, if necessary
29    exp="F4"
30    bits=$i
31    if [ "${i##*_exp}" != "${i}" ]; then
32        exp="${i##*_exp}"
33        bits="${i%%_exp${exp}}"
34    fi
35
36    openssl genrsa "-${exp}" -out "${key_base}.pem" "${bits}"
37    # Generate self-signed certificate from key.
38    openssl req -batch -new -x509 -key "${key_base}.pem" \
39      -out "${key_base}.crt"
40
41    # Generate pre-processed key for use by RSA signature verification code.
42    "${BIN_DIR}/dumpRSAPublicKey" -cert "${key_base}.crt" > "${key_base}.keyb"
43
44    alg_index=0
45    for sha_type in "${sha_types[@]}"
46    do
47      alg=$((key_index * 3 + alg_index))
48  # wrap the public key
49      "${FUTILITY}" vbutil_key \
50        --pack "${key_base}.sha${sha_type}.vbpubk" \
51        --key "${key_base}.keyb" \
52        --version 1 \
53        --algorithm ${alg}
54
55  # wrap the private key
56      "${FUTILITY}" vbutil_key \
57        --pack "${key_base}.sha${sha_type}.vbprivk" \
58        --key "${key_base}.pem" \
59        --algorithm ${alg}
60      alg_index=$((alg_index} + 1))
61    done
62    key_index=$((key_index + 1))
63  done
64}
65
66mkdir -p ${TESTKEY_DIR}
67generate_keys
68