blob: 1d08fdbe38206b7a76ae526fab20e602d47b01f5 [file] [log] [blame]
Gaurav Shah57468452011-03-02 14:50:46 -08001#!/bin/bash
2# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Common key generation functions.
7
8SCRIPT_DIR="$(dirname "$0")"
9
10# 0 = (RSA1024 SHA1)
11# 1 = (RSA1024 SHA256)
12# 2 = (RSA1024 SHA512)
13# 3 = (RSA2048 SHA1)
14# 4 = (RSA2048 SHA256)
15# 5 = (RSA2048 SHA512)
16# 6 = (RSA4096 SHA1)
17# 7 = (RSA4096 SHA256)
18# 8 = (RSA4096 SHA512)
19# 9 = (RSA8192 SHA1)
20# 10 = (RSA8192 SHA256)
21# 11 = (RSA8192 SHA512)
22function alg_to_keylen {
23 echo $(( 1 << (10 + ($1 / 3)) ))
24}
25
Gaurav Shah41f444a2011-04-12 17:05:37 -070026# Default alrogithms.
27ROOT_KEY_ALGOID=11
28RECOVERY_KEY_ALGOID=11
29
30FIRMWARE_DATAKEY_ALGOID=7
31DEV_FIRMWARE_DATAKEY_ALGOID=7
32
33RECOVERY_KERNEL_ALGOID=11
34INSTALLER_KERNEL_ALGOID=11
35KERNEL_SUBKEY_ALGOID=7
36KERNEL_DATAKEY_ALGOID=4
37
38# Keyblock modes determine which boot modes a signing key is valid for use
39# in verification.
40FIRMWARE_KEYBLOCK_MODE=7
41DEV_FIRMWARE_KEYBLOCK_MODE=6 # Only allow in dev mode.
42RECOVERY_KERNEL_KEYBLOCK_MODE=11
43KERNEL_KEYBLOCK_MODE=7 # Only allow in non-recovery.
44INSTALLER_KERNEL_KEYBLOCK_MODE=10 # Only allow in Dev + Recovery.
45
46
Gaurav Shah57468452011-03-02 14:50:46 -080047# Emit .vbpubk and .vbprivk using given basename and algorithm
48# NOTE: This function also appears in ../../utility/dev_make_keypair. Making
49# the two implementations the same would require some common.sh, which is more
50# likely to cause problems than just keeping an eye out for any differences. If
51# you feel the need to change this file, check the history of that other file
52# to see what may need updating here too.
53function make_pair {
54 local base=$1
55 local alg=$2
Gaurav Shah41f444a2011-04-12 17:05:37 -070056 local key_version=${3:-1}
Gaurav Shah57468452011-03-02 14:50:46 -080057 local len=$(alg_to_keylen $alg)
58
Gaurav Shah41f444a2011-04-12 17:05:37 -070059 echo "creating $base keypair (version = $key_version)..."
Gaurav Shah57468452011-03-02 14:50:46 -080060
61 # make the RSA keypair
62 openssl genrsa -F4 -out "${base}_${len}.pem" $len
63 # create a self-signed certificate
64 openssl req -batch -new -x509 -key "${base}_${len}.pem" \
65 -out "${base}_${len}.crt"
66 # generate pre-processed RSA public key
67 dumpRSAPublicKey -cert "${base}_${len}.crt" > "${base}_${len}.keyb"
68
69 # wrap the public key
70 vbutil_key \
71 --pack "${base}.vbpubk" \
72 --key "${base}_${len}.keyb" \
Gaurav Shah41f444a2011-04-12 17:05:37 -070073 --version "${key_version}" \
Gaurav Shah57468452011-03-02 14:50:46 -080074 --algorithm $alg
75
76 # wrap the private key
77 vbutil_key \
78 --pack "${base}.vbprivk" \
79 --key "${base}_${len}.pem" \
80 --algorithm $alg
81
82 # remove intermediate files
83 rm -f "${base}_${len}.pem" "${base}_${len}.crt" "${base}_${len}.keyb"
84}
85
86
87# Emit a .keyblock containing flags and a public key, signed by a private key
88# flags are the bitwise OR of these (passed in decimal, though)
89# 0x01 Developer switch off
90# 0x02 Developer switch on
91# 0x04 Not recovery mode
92# 0x08 Recovery mode
93function make_keyblock {
94 local base=$1
95 local flags=$2
96 local pubkey=$3
97 local signkey=$4
98
99 echo "creating $base keyblock..."
100
101 # create it
102 vbutil_keyblock \
103 --pack "${base}.keyblock" \
104 --flags $flags \
105 --datapubkey "${pubkey}.vbpubk" \
106 --signprivate "${signkey}.vbprivk"
107
108 # verify it
109 vbutil_keyblock \
110 --unpack "${base}.keyblock" \
111 --signpubkey "${signkey}.vbpubk"
112}
113
114