pc1crypt: make decrypt/encrypt functions take void * as argument
[project/firmware-utils.git] / src / cros-vbutil.c
1 /*
2 * cros-vbutil - Tool for signing kernels using the Chromium OS verified boot
3 * format, using widely-shared "developer" keys. The output of this tool is
4 * intended to be written to a GPT partition of type "Chrome OS Kernel", such
5 * that a Chromium OS bootloader can find it.
6 *
7 * Much of this is adapted from Google's vboot_reference project found here:
8 * https://chromium.googlesource.com/chromiumos/platform/vboot_reference
9 *
10 * Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are
14 * met:
15 *
16 * * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * * Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following disclaimer
20 * in the documentation and/or other materials provided with the
21 * distribution.
22 * * Neither the name of Google Inc. nor the names of its
23 * contributors may be used to endorse or promote products derived from
24 * this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * Rewritten:
39 * Copyright (c) 2021 Brian Norris <computersforpeace@gmail.com>
40 *
41 * This program is free software; you can redistribute it and/or modify it
42 * under the terms of the GNU General Public License version 2 as published
43 * by the Free Software Foundation.
44 */
45
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <sys/stat.h>
50 #include <unistd.h>
51
52 #include <openssl/rsa.h>
53 #include <openssl/x509.h>
54 #include <openssl/sha.h>
55
56 #define BLOCK_PAD 65536
57 /* Sections are aligned to 4K blocks */
58 #define ALIGN 4096
59 #define ROUNDUP(x) (((x) + (ALIGN-1)) & ~(ALIGN-1))
60
61 /*
62 * Signature size, in bytes. This is an RSA-2048 encryption of a SHA256 hash.
63 */
64 #define SIG_SIZE (2048 / 8)
65
66 /*
67 * From vboot_reference: tests/devkeys/kernel.keyblock
68 */
69 static const unsigned char keyblock[] = {
70 0x43, 0x48, 0x52, 0x4f, 0x4d, 0x45, 0x4f, 0x53, 0x02, 0x00, 0x00, 0x00,
71 0x01, 0x00, 0x00, 0x00, 0xb8, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0xa0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x78, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x48, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x78, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x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1e, 0x0b, 0x08, 0x54, 0xc6, 0x01, 0xc6, 0x20,
94 0xe6, 0xc6, 0x09, 0x5f, 0x0a, 0xc5, 0x02, 0x05, 0x6b, 0x40, 0x96, 0x11,
95 0x20, 0xe5, 0xa2, 0x88, 0x37, 0x6a, 0xd3, 0xda, 0xda, 0xda, 0x76, 0x1c,
96 0xe4, 0x3d, 0x2c, 0xfe, 0x65, 0x86, 0x98, 0xa2, 0xa8, 0x49, 0x7d, 0x26,
97 0x45, 0x14, 0x04, 0x10, 0xe3, 0x38, 0x4b, 0x66, 0x9c, 0x5c, 0x2c, 0xe8,
98 0xd9, 0x06, 0xd4, 0xc3, 0x05, 0x94, 0x1c, 0x7b, 0x23, 0x89, 0x86, 0x1d,
99 0x32, 0xbb, 0x23, 0x90, 0x2b, 0x72, 0xd7, 0x90, 0x53, 0x52, 0x10, 0x12,
100 0xd0, 0xc2, 0xe2, 0x72, 0x81, 0x70, 0x3c, 0xb1, 0x27, 0x42, 0xec, 0xbf,
101 0x2b, 0x54, 0xd6, 0xcb, 0x61, 0xea, 0xb4, 0xd4, 0xa8, 0xde, 0xf3, 0xdf,
102 0x3d, 0xd1, 0x17, 0xde, 0x86, 0x89, 0xf4, 0xa1, 0x28, 0x5e, 0xda, 0xb4,
103 0x47, 0x19, 0x9b, 0xc2, 0xce, 0x49, 0xea, 0x63, 0xda, 0x42, 0x1c, 0x85,
104 0x1c, 0xab, 0x6c, 0xd7, 0xc2, 0xfd, 0x34, 0x1b, 0xd5, 0x11, 0xa2, 0x44,
105 0xb7, 0x7a, 0x85, 0x61, 0x03, 0xba, 0xd4, 0x64, 0xe1, 0x49, 0x87, 0x92,
106 0xe6, 0xdc, 0x39, 0x94, 0x9c, 0xba, 0xef, 0x58, 0x9e, 0x0e, 0x36, 0x98,
107 0xd7, 0xd2, 0x4c, 0x14, 0x71, 0x94, 0x78, 0xaf, 0x86, 0xe7, 0x2f, 0xe9,
108 0xf9, 0x20, 0x4d, 0xfe, 0xda, 0xd7, 0x3f, 0x89, 0xc1, 0x59, 0xae, 0xbd,
109 0x60, 0x9b, 0x97, 0xa4, 0x66, 0x00, 0xbb, 0x7b, 0xd0, 0x49, 0x85, 0x6f,
110 0x7b, 0x01, 0x24, 0xc3, 0x48, 0xe2, 0xc7, 0x61, 0x98, 0x9c, 0xe5, 0x93,
111 0x87, 0x04, 0x09, 0x3a, 0x92, 0x54, 0x1f, 0xb5, 0x80, 0x29, 0x15, 0xe5,
112 0x7f, 0xfe, 0xdc, 0xaf, 0xd3, 0xe1, 0x62, 0xc4, 0xb6, 0xdb, 0x2f, 0x1a,
113 0x08, 0xf3, 0x54, 0x06, 0x3a, 0xce, 0x5e, 0x89, 0x71, 0xdf, 0x3b, 0x5c,
114 0x8c, 0x6b, 0x83, 0xa9, 0x0c, 0x05, 0x50, 0x7b, 0x84, 0x09, 0x18, 0x66,
115 0x84, 0xe1, 0x7e, 0x60, 0xd6, 0xdc, 0xbf, 0x1c, 0xac, 0x5c, 0xdc, 0xe6,
116 0x4c, 0x9e, 0xa0, 0x4a, 0x59, 0x30, 0xff, 0x91, 0x90, 0x1e, 0x21, 0x29,
117 0x50, 0x99, 0x76, 0xfd, 0x2f, 0x5a, 0x67, 0x6a, 0x00, 0x6b, 0x40, 0x27,
118 0xe7, 0xbd, 0x17, 0xe6, 0x5f, 0x9e, 0xe4, 0x6b, 0xe0, 0x05, 0x9e, 0x7d,
119 0xcb, 0x90, 0xe9, 0x3c, 0x9c, 0x79, 0xc9, 0x47, 0xf8, 0x58, 0xbc, 0x3f,
120 0xb1, 0xa5, 0xef, 0x67, 0xf0, 0x22, 0xa1, 0x70, 0x33, 0x36, 0xb0, 0x5c,
121 0xed, 0xea, 0x4e, 0xde, 0x24, 0x0c, 0x5b, 0xd8, 0x4d, 0xbe, 0xac, 0x6a,
122 0x12, 0x31, 0xa6, 0x5e, 0x5f, 0x04, 0xb6, 0x58, 0xa1, 0xd1, 0x29, 0xa3,
123 0x33, 0x9c, 0x64, 0xb1, 0x8e, 0x3b, 0xfa, 0xaf, 0x35, 0x94, 0x7a, 0xa4,
124 0xf5, 0xcb, 0x9e, 0x67, 0x1b, 0x8d, 0x35, 0xca, 0x10, 0x69, 0x8d, 0xea,
125 0x78, 0xc2, 0xf6, 0x63, 0xc0, 0x11, 0xf1, 0xf6, 0x66, 0x70, 0x2e, 0x96,
126 0xf0, 0xbc, 0x7b, 0x1e, 0x16, 0x26, 0xde, 0xe3, 0x7f, 0xc0, 0xf4, 0x6c,
127 0x3c, 0x0b, 0x52, 0xe2, 0x07, 0xde, 0x70, 0x8b, 0xaf, 0x84, 0x95, 0x1b,
128 0x46, 0x95, 0x69, 0x05, 0x92, 0xea, 0x36, 0xa5, 0xca, 0x50, 0xdb, 0x49,
129 0xc8, 0x44, 0xee, 0x7c, 0x49, 0x40, 0x34, 0x05, 0xf3, 0x06, 0x1f, 0x1c,
130 0xcc, 0x78, 0x62, 0x1b, 0x9d, 0xe2, 0x4f, 0x3e, 0x04, 0x96, 0xba, 0xd0,
131 0xc7, 0xa2, 0x3a, 0xaa, 0xf7, 0xc3, 0xae, 0xc0, 0x6b, 0xca, 0xc8, 0x59,
132 0x90, 0xe0, 0x45, 0x7b, 0x4b, 0x60, 0xbf, 0xae, 0x15, 0x25, 0xd0, 0xa2,
133 0x2f, 0x95, 0x6a, 0x2e, 0x37, 0x24, 0x15, 0x69, 0x0e, 0x3a, 0x6d, 0x40,
134 0x21, 0xf6, 0x74, 0x5c, 0xa6, 0x9f, 0xe0, 0x03, 0x60, 0x04, 0x59, 0x70,
135 0x70, 0xa0, 0x46, 0xf4, 0xdf, 0x0a, 0x4b, 0x14, 0xab, 0x87, 0x0c, 0xaa,
136 0x64, 0x9b, 0x82, 0xca, 0x21, 0xb0, 0x72, 0xfa, 0x8b, 0x37, 0xf5, 0x60,
137 0xd7, 0xb7, 0x4e, 0x72, 0x5e, 0x13, 0x23, 0x73, 0x84, 0x6c, 0xd1, 0x8d,
138 0x72, 0xa2, 0x15, 0x8b, 0x51, 0x38, 0x90, 0x68, 0x90, 0xd9, 0x8b, 0x65,
139 0x30, 0x61, 0xdb, 0x51, 0x80, 0x58, 0xdd, 0xc3, 0xf0, 0x29, 0x33, 0xdc,
140 0x39, 0x92, 0xbf, 0x27, 0xe6, 0xc7, 0x82, 0xf6, 0xea, 0xcf, 0x21, 0xd2,
141 0xa0, 0x38, 0x02, 0x57, 0x52, 0xf5, 0xdd, 0x07, 0x99, 0xe3, 0x09, 0xd7,
142 0xad, 0x9e, 0x1e, 0xe7, 0xf3, 0x5e, 0x72, 0xa0, 0xc6, 0x9d, 0x41, 0x86,
143 0x96, 0x82, 0x74, 0x46, 0x1f, 0x05, 0xd2, 0x05, 0x25, 0x29, 0x27, 0x66,
144 0xe1, 0xab, 0x5c, 0x06, 0xf4, 0xac, 0xe7, 0xd0, 0xf8, 0xe6, 0x5e, 0x8b,
145 0x19, 0x55, 0xc2, 0x99, 0x8e, 0xa7, 0x2e, 0x66, 0x0c, 0x2a, 0x61, 0xb0,
146 0x98, 0x41, 0x93, 0x05, 0x54, 0x90, 0xaa, 0xbe, 0x15, 0x26, 0x5b, 0x77,
147 0x7c, 0xb0, 0xf8, 0x08, 0x68, 0xb6, 0xb8, 0xd4, 0xdd, 0xd8, 0xa8, 0x4f,
148 0xdd, 0xf6, 0x93, 0xe5, 0xa1, 0x88, 0x1b, 0x7a, 0x05, 0x81, 0xc5, 0x98,
149 0x34, 0x1d, 0x88, 0x63, 0x55, 0xaf, 0x9a, 0xa1, 0xcb, 0x4b, 0xaf, 0x86,
150 0xb3, 0x45, 0xda, 0xa4, 0xb4, 0xa5, 0x10, 0x7a, 0x50, 0x6a, 0xe5, 0xda,
151 0x6f, 0x8f, 0xc0, 0x1d, 0x95, 0xb1, 0x91, 0x48, 0x53, 0xc3, 0xe5, 0x1f,
152 0x0e, 0x05, 0x75, 0xaa, 0xc7, 0xd8, 0x36, 0xd0, 0x6d, 0x50, 0x48, 0xd4,
153 0xe6, 0xbd, 0xd0, 0x2d,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 0xe2, 0xd7, 0x99, 0x83, 0x6c, 0x72, 0x39, 0x6f, 0x35, 0x20, 0x9d,
169 0xe0, 0xd8, 0xf8, 0xdb, 0x72, 0x24, 0xe5, 0x8d, 0x29, 0xfd, 0x24, 0x7b,
170 0xbc, 0x48, 0xe9, 0xbb, 0xc2, 0x84, 0xc7, 0xb9
171 };
172 static unsigned int keyblock_len = sizeof(keyblock);
173
174 /*
175 * RSA 2048-bit key from vboot_reference:
176 * tests/devkeys/kernel_data_key.vbprivk, minus the 8-byte custom header.
177 */
178 static const unsigned char privk[] = {
179 0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
180 0xcb, 0xd6, 0x54, 0x2b, 0xbf, 0xec, 0x42, 0x27, 0xb1, 0x3c, 0x70, 0x81,
181 0x72, 0xe2, 0xc2, 0xd0, 0x12, 0x10, 0x52, 0x53, 0x90, 0xd7, 0x72, 0x2b,
182 0x90, 0x23, 0xbb, 0x32, 0x1d, 0x86, 0x89, 0x23, 0x7b, 0x1c, 0x94, 0x05,
183 0xc3, 0xd4, 0x06, 0xd9, 0xe8, 0x2c, 0x5c, 0x9c, 0x66, 0x4b, 0x38, 0xe3,
184 0x10, 0x04, 0x14, 0x45, 0x26, 0x7d, 0x49, 0xa8, 0xa2, 0x98, 0x86, 0x65,
185 0xfe, 0x2c, 0x3d, 0xe4, 0x1c, 0x76, 0xda, 0xda, 0xda, 0xd3, 0x6a, 0x37,
186 0x88, 0xa2, 0xe5, 0x20, 0x11, 0x96, 0x40, 0x6b, 0x05, 0x02, 0xc5, 0x0a,
187 0x5f, 0x09, 0xc6, 0xe6, 0x20, 0xc6, 0x01, 0xc6, 0x54, 0x08, 0x0b, 0x1e,
188 0xb4, 0xa9, 0x3f, 0xc9, 0x67, 0x81, 0xd9, 0x2b, 0xd0, 0xdc, 0x7d, 0xa7,
189 0x97, 0x6f, 0x8b, 0x78, 0x33, 0xb7, 0xd7, 0x52, 0x2a, 0x41, 0x69, 0x48,
190 0x69, 0x96, 0x8f, 0xc5, 0x0a, 0x1b, 0x06, 0x88, 0x1f, 0xfb, 0xe8, 0xb0,
191 0x97, 0xbd, 0x6d, 0x3d, 0x28, 0x82, 0xfe, 0xd5, 0x6e, 0xa1, 0x57, 0x1e,
192 0xcb, 0x43, 0x7e, 0xba, 0x11, 0x8c, 0xc5, 0x6a, 0xf0, 0x92, 0x53, 0x7b,
193 0xb2, 0x21, 0x26, 0xdb, 0xf8, 0x8c, 0x5a, 0x1b, 0x9f, 0x1d, 0x81, 0xa9,
194 0xea, 0xa4, 0xa6, 0x83, 0xd6, 0xd1, 0xcc, 0x8a, 0xdb, 0xaf, 0x99, 0x64,
195 0x6c, 0x9d, 0x8b, 0x83, 0xd1, 0x82, 0x44, 0xe7, 0x3a, 0xb4, 0x9f, 0x71,
196 0xd0, 0xaf, 0x15, 0x72, 0x6d, 0x1c, 0x2e, 0x43, 0x8c, 0x7d, 0x4e, 0x01,
197 0xe9, 0x70, 0xcc, 0x59, 0xc7, 0xbd, 0x65, 0x2a, 0xf0, 0x2c, 0x88, 0x46,
198 0x5a, 0x67, 0xaf, 0xaf, 0x8a, 0x16, 0x98, 0x5e, 0x5b, 0x3d, 0xba, 0x94,
199 0x0c, 0x65, 0x03, 0x9c, 0x1d, 0x78, 0xfe, 0x21, 0x12, 0xa5, 0x72, 0x3a,
200 0xc2, 0xc4, 0x22, 0xdc, 0xca, 0x1b, 0x9b, 0xa7, 0xcc, 0xcc, 0x53, 0x6d,
201 0x0f, 0x86, 0x2d, 0x1d, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
202 0x00, 0x4d, 0xb8, 0xea, 0x9d, 0x47, 0xef, 0xf1, 0x05, 0xab, 0x79, 0xed,
203 0x38, 0x1e, 0xb3, 0x42, 0x2f, 0x0e, 0x44, 0x34, 0xef, 0xf2, 0x31, 0x84,
204 0x32, 0x02, 0x22, 0xed, 0xb4, 0xdd, 0x37, 0x38, 0x41, 0x54, 0x27, 0x00,
205 0x75, 0xf6, 0x46, 0x79, 0x5a, 0x4b, 0x41, 0xe7, 0x0a, 0x27, 0xa3, 0x15,
206 0x0c, 0xd7, 0x45, 0x86, 0xe9, 0x1b, 0x14, 0xf8, 0xaa, 0x19, 0x5d, 0x69,
207 0x19, 0xa8, 0xc1, 0xfb, 0xd3, 0x67, 0xdb, 0x15, 0xf6, 0x0f, 0x2c, 0x3f,
208 0xba, 0xef, 0x19, 0x95, 0x9e, 0x15, 0xb6, 0xa1, 0xd7, 0x6c, 0xb0, 0xb2,
209 0xab, 0x12, 0xb3, 0xea, 0xa8, 0x42, 0xe4, 0xdd, 0x36, 0x5f, 0x42, 0x35,
210 0xe3, 0x3e, 0x65, 0xae, 0x91, 0xc0, 0x2a, 0xed, 0xc0, 0xdc, 0x41, 0xc6,
211 0x93, 0xc9, 0x95, 0x59, 0xab, 0xba, 0xa4, 0xa5, 0xb0, 0x89, 0xb6, 0x29,
212 0x4b, 0xd8, 0x1e, 0xf5, 0xb0, 0xde, 0xfa, 0x91, 0x11, 0x55, 0xd6, 0x9d,
213 0xe1, 0xa7, 0x9c, 0xf2, 0xf4, 0x9d, 0x7f, 0x96, 0x49, 0x7a, 0xab, 0xe9,
214 0x9a, 0x0c, 0xcb, 0x10, 0x5c, 0x67, 0xf0, 0x1d, 0xf3, 0x33, 0x84, 0xae,
215 0x6e, 0xe4, 0xbb, 0xa7, 0x57, 0x98, 0x99, 0x2b, 0xf3, 0x1d, 0xab, 0xec,
216 0x01, 0xa2, 0x2e, 0x3c, 0x10, 0x5c, 0xff, 0xe8, 0xcc, 0x50, 0x45, 0x21,
217 0x2f, 0x04, 0x69, 0xdd, 0x02, 0x5b, 0x10, 0xd6, 0x54, 0x36, 0x4c, 0x73,
218 0x0c, 0x1e, 0xc1, 0xff, 0xac, 0x60, 0x5d, 0xbc, 0x13, 0x84, 0xe8, 0xa4,
219 0xbd, 0x66, 0xca, 0xca, 0xaf, 0x55, 0x8a, 0xf8, 0x87, 0x17, 0x93, 0xbf,
220 0x66, 0xa4, 0x97, 0xe9, 0x47, 0x81, 0x03, 0xfa, 0x4c, 0x32, 0xf6, 0xec,
221 0x7a, 0x03, 0xbb, 0x2d, 0x3b, 0x9b, 0xea, 0xdc, 0x6c, 0x61, 0x7f, 0x05,
222 0xc8, 0x2d, 0x50, 0x89, 0xe9, 0x10, 0x3d, 0x3c, 0x02, 0xb0, 0x5d, 0x3e,
223 0x25, 0x71, 0xd2, 0xa1, 0xe1, 0x02, 0x81, 0x81, 0x00, 0xf6, 0x3c, 0x7f,
224 0x15, 0x63, 0x04, 0x0a, 0xd4, 0x68, 0xe8, 0xd9, 0x4b, 0x7f, 0xbc, 0xca,
225 0xb5, 0x84, 0x01, 0x71, 0x11, 0x33, 0x4d, 0xe1, 0x0a, 0xe8, 0xa7, 0x95,
226 0xcc, 0x23, 0x59, 0x19, 0xb5, 0x82, 0xaa, 0xae, 0xe4, 0x90, 0x33, 0x95,
227 0x20, 0x1a, 0xfd, 0xef, 0x8e, 0x36, 0xc1, 0x57, 0xde, 0x94, 0xdd, 0x99,
228 0x6f, 0x14, 0x0c, 0x8e, 0x30, 0xe0, 0xe7, 0x64, 0x33, 0xa7, 0x1d, 0xfd,
229 0x99, 0xdc, 0x58, 0x49, 0x4e, 0x56, 0x82, 0x43, 0xea, 0xd9, 0xf7, 0xcc,
230 0x1a, 0xf5, 0x09, 0x38, 0x4c, 0xf6, 0x87, 0x8f, 0xa6, 0x40, 0x96, 0xd1,
231 0x81, 0x62, 0xbb, 0x49, 0x53, 0x69, 0x56, 0xdf, 0x4a, 0x85, 0xfd, 0x72,
232 0x34, 0x21, 0x56, 0xa3, 0x7b, 0xda, 0x50, 0x24, 0x88, 0xc6, 0x0b, 0x03,
233 0x94, 0x0d, 0xe2, 0x03, 0x2a, 0x3c, 0x5a, 0xdd, 0x73, 0xe7, 0x07, 0x01,
234 0x7d, 0xdd, 0x38, 0x81, 0x19, 0x02, 0x81, 0x81, 0x00, 0xd3, 0xeb, 0x72,
235 0x3f, 0xf6, 0x9b, 0x03, 0x8c, 0xab, 0xd9, 0x41, 0x47, 0xbe, 0x6c, 0x07,
236 0x7f, 0xd7, 0xcb, 0xee, 0xbf, 0x45, 0x13, 0x1e, 0x53, 0x35, 0xd2, 0xe4,
237 0x6a, 0x13, 0xe9, 0xd8, 0xe3, 0xf4, 0x28, 0x5e, 0xeb, 0xd8, 0xd2, 0x87,
238 0x60, 0xdc, 0xa7, 0x21, 0xd8, 0x33, 0xce, 0xca, 0x60, 0x50, 0x1b, 0x87,
239 0xbf, 0x71, 0x88, 0x90, 0x71, 0x6f, 0xde, 0xdb, 0x33, 0x1d, 0x3c, 0xb1,
240 0x2f, 0x1b, 0x41, 0xb8, 0x55, 0xa1, 0x5c, 0xa8, 0xb2, 0x73, 0x1b, 0x39,
241 0x69, 0x5c, 0xa1, 0x57, 0x9b, 0x2e, 0xcd, 0x0d, 0x71, 0x42, 0xdf, 0x2a,
242 0xd3, 0x4f, 0x51, 0xdc, 0x35, 0x5c, 0x14, 0x86, 0x16, 0xb8, 0x22, 0x8c,
243 0x73, 0x37, 0x7a, 0x6c, 0xb7, 0xfe, 0x01, 0x1b, 0xa8, 0xc5, 0xf1, 0xff,
244 0xda, 0xd4, 0x63, 0x58, 0x81, 0xe1, 0x6a, 0xaf, 0xc2, 0x3d, 0xbb, 0x17,
245 0x3f, 0x2c, 0xf9, 0xb8, 0xa5, 0x02, 0x81, 0x81, 0x00, 0xd1, 0x1e, 0x6b,
246 0xa7, 0x54, 0x22, 0x18, 0xb8, 0x70, 0x28, 0x60, 0x72, 0x98, 0x17, 0x53,
247 0xe8, 0x78, 0x25, 0x69, 0x4a, 0xfc, 0x51, 0x45, 0xb0, 0x28, 0x0a, 0x3b,
248 0x53, 0xd5, 0x29, 0x54, 0x86, 0x2c, 0x64, 0x9c, 0x87, 0x8c, 0x57, 0xe2,
249 0x6c, 0x13, 0xc0, 0x59, 0x74, 0x63, 0xd8, 0x2b, 0xb1, 0xe2, 0xa7, 0x98,
250 0x29, 0x17, 0xd3, 0xd9, 0xe6, 0x74, 0xfa, 0xce, 0xc4, 0x77, 0x47, 0x27,
251 0x1b, 0x46, 0x60, 0xcc, 0x50, 0xf5, 0x73, 0x48, 0x0e, 0xe9, 0xf2, 0x62,
252 0xaa, 0xfa, 0x33, 0x4c, 0x57, 0x40, 0x7f, 0xbc, 0x65, 0xab, 0x30, 0xea,
253 0xd8, 0x81, 0x6b, 0x21, 0x48, 0xef, 0x8b, 0x51, 0xb3, 0x4e, 0xbc, 0x02,
254 0x03, 0x77, 0xd4, 0xc6, 0x9e, 0xe6, 0xb7, 0x6d, 0x6f, 0xa3, 0x11, 0xb3,
255 0x86, 0x47, 0x8c, 0x0c, 0xb8, 0x0f, 0xed, 0xf3, 0x68, 0xfd, 0x69, 0x9b,
256 0xc0, 0xb2, 0xe1, 0xcb, 0xf1, 0x02, 0x81, 0x80, 0x46, 0xc6, 0xdb, 0xe1,
257 0x80, 0xfb, 0x44, 0xb4, 0x36, 0xb0, 0xd8, 0x9c, 0x9d, 0x7c, 0x77, 0x04,
258 0xbb, 0x8d, 0xe0, 0xaf, 0x00, 0xe2, 0xe1, 0xbd, 0xe8, 0xbc, 0x06, 0x8d,
259 0x67, 0x19, 0x32, 0xd6, 0xef, 0x34, 0x8c, 0x7f, 0x38, 0x6b, 0x17, 0xcb,
260 0xc1, 0xe6, 0x0f, 0xe7, 0xa5, 0x65, 0xeb, 0x31, 0x3f, 0x8f, 0xf2, 0x46,
261 0x7e, 0x24, 0x9e, 0x19, 0x44, 0xc3, 0xa9, 0x71, 0xe8, 0xd1, 0xdc, 0x5b,
262 0x5f, 0x3f, 0x13, 0xd1, 0x85, 0x92, 0xec, 0xc4, 0xac, 0xac, 0xea, 0xff,
263 0xc5, 0x70, 0x1d, 0x52, 0x36, 0xcf, 0x45, 0x2e, 0x0e, 0xa7, 0x7a, 0x26,
264 0x18, 0xd7, 0xbe, 0x34, 0x14, 0x0c, 0xc6, 0xbb, 0xcf, 0xc3, 0xba, 0x24,
265 0xc0, 0xe2, 0xa2, 0x6c, 0xd0, 0xe8, 0x8d, 0xd4, 0x50, 0x48, 0x02, 0xab,
266 0x93, 0x43, 0x9a, 0xcb, 0xc5, 0xfd, 0x5b, 0xb6, 0x03, 0xe7, 0xf8, 0x5b,
267 0xa1, 0x29, 0xc6, 0x01, 0x02, 0x81, 0x80, 0x0e, 0x18, 0xbc, 0x00, 0xa3,
268 0x81, 0xda, 0xc2, 0xc8, 0xfb, 0x0a, 0xbd, 0x7f, 0xb7, 0x7f, 0xa1, 0x18,
269 0x7f, 0x49, 0xb8, 0x32, 0x83, 0x4b, 0x71, 0x35, 0x59, 0x51, 0x7b, 0xee,
270 0x8e, 0x4e, 0xd2, 0x42, 0xa8, 0x6c, 0x82, 0x57, 0xf7, 0x34, 0xf6, 0x82,
271 0x59, 0xe9, 0x75, 0x7a, 0x8f, 0x0a, 0x8d, 0x46, 0x1e, 0xa1, 0x77, 0xa1,
272 0x4c, 0xb3, 0x9e, 0x86, 0x93, 0x18, 0xdf, 0x3e, 0xfb, 0x81, 0xd0, 0xbc,
273 0x65, 0xa2, 0xb3, 0x80, 0xf0, 0x8f, 0x73, 0x2f, 0xb5, 0xc4, 0xc0, 0x03,
274 0xac, 0x24, 0xfa, 0x4f, 0x05, 0xe3, 0x16, 0x29, 0x7c, 0xff, 0x63, 0xd5,
275 0x0a, 0x0a, 0xbd, 0xd7, 0x65, 0x3a, 0xb5, 0x5e, 0xe7, 0x23, 0xfc, 0x3d,
276 0x08, 0xea, 0x62, 0xc2, 0xe3, 0x4a, 0xbb, 0x86, 0x5a, 0xc5, 0x93, 0xd7,
277 0xbc, 0xb4, 0x4c, 0xcf, 0xcf, 0x78, 0x9f, 0x52, 0x6e, 0xa1, 0x49, 0x21,
278 0xf7, 0x20, 0x42
279 };
280 static const unsigned int privk_len = sizeof(privk);
281
282 /* Signature data (a secure hash, possibly signed) */
283 struct vb2_signature {
284 /* Offset of signature data from start of this struct */
285 uint32_t sig_offset;
286 uint32_t reserved0;
287
288 /* Size of signature data in bytes */
289 uint32_t sig_size;
290 uint32_t reserved1;
291
292 /* Size of the data block which was signed in bytes */
293 uint32_t data_size;
294 uint32_t reserved2;
295 } __attribute__((packed));
296
297 /*
298 * Preamble block for kernel, version 2.2
299 *
300 * This should be followed by:
301 * 1) The signature data for the kernel body, pointed to by
302 * body_signature.sig_offset.
303 * 2) The signature data for (vb2_kernel_preamble + body signature data),
304 * pointed to by preamble_signature.sig_offset.
305 * 3) The 16-bit vmlinuz header, which is used for reconstruction of
306 * vmlinuz image.
307 */
308 struct vb2_kernel_preamble {
309 /*
310 * Size of this preamble, including keys, signatures, vmlinuz header,
311 * and padding, in bytes
312 */
313 uint32_t preamble_size;
314 uint32_t reserved0;
315
316 /* Signature for this preamble (header + body signature) */
317 struct vb2_signature preamble_signature;
318
319 /* Version of this header format */
320 uint32_t header_version_major;
321 uint32_t header_version_minor;
322
323 /* Kernel version */
324 uint32_t kernel_version;
325 uint32_t reserved1;
326
327 /* Load address for kernel body */
328 uint64_t body_load_address;
329
330 /* Address of bootloader, after body is loaded at body_load_address */
331 uint64_t bootloader_address;
332
333 /* Size of bootloader in bytes */
334 uint32_t bootloader_size;
335 uint32_t reserved2;
336
337 /* Signature for the kernel body */
338 struct vb2_signature body_signature;
339
340 /* Address of 16-bit header for vmlinuz reassembly. */
341 uint64_t vmlinuz_header_address;
342
343 /* Size of 16-bit header for vmlinuz in bytes. */
344 uint32_t vmlinuz_header_size;
345 uint32_t reserved3;
346
347 uint32_t flags;
348 } __attribute__((packed));
349
350 /*
351 * Order matters. |data| may overlap with |siginfo|, so we need to fill out
352 * |siginfo| before computing the signature.
353 */
354 static int sign(const void *data, size_t len, struct vb2_signature *siginfo,
355 void *sigout)
356 {
357 unsigned char hash[SHA256_DIGEST_LENGTH];
358 SHA256_CTX sha256;
359 int ret;
360
361 memset(siginfo, 0, sizeof(*siginfo));
362 siginfo->sig_offset = (uintptr_t)(void *)sigout - (uintptr_t)(void *)siginfo;
363 siginfo->sig_size = SIG_SIZE;
364 siginfo->data_size = len;
365
366 const unsigned char *p = privk;
367 char digest_info[] = {
368 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
369 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20,
370 };
371
372 uint8_t digest[sizeof(digest_info) + sizeof(hash)];
373
374 SHA256_Init(&sha256);
375 SHA256_Update(&sha256, data, len);
376 SHA256_Final(hash, &sha256);
377
378 memcpy(digest, digest_info, sizeof(digest_info));
379 memcpy(digest + sizeof(digest_info), hash, sizeof(hash));
380
381 RSA *key = d2i_RSAPrivateKey(0, &p, privk_len);
382 if (!key) {
383 fprintf(stderr, "Failed d2i_RSAPrivateKey()\n");
384 return -1;
385 }
386
387 ret = RSA_private_encrypt(sizeof(digest), digest, sigout, key,
388 RSA_PKCS1_PADDING);
389 if (ret == -1) {
390 fprintf(stderr, "%s: RSA_private_encrypt() failed\n", __func__);
391 return -1;
392 }
393
394 return 0;
395 }
396
397 static struct vb2_kernel_preamble *
398 generate_preamble(const void *kblob, size_t kblob_len,
399 size_t kernel_len, size_t config_len)
400 {
401 struct vb2_kernel_preamble *h;
402 uint32_t signed_size = sizeof(struct vb2_kernel_preamble) + SIG_SIZE;
403 uint32_t block_size = signed_size + SIG_SIZE;
404 int ret;
405
406 if (block_size + keyblock_len < BLOCK_PAD)
407 block_size = BLOCK_PAD - keyblock_len;
408 h = (struct vb2_kernel_preamble *)calloc(block_size, 1);
409 if (!h) {
410 perror("calloc");
411 return NULL;
412 }
413
414 /* Sign the body, place the signature in the preamble */
415 ret = sign(kblob, kblob_len, &h->body_signature, h + 1);
416 if (ret) {
417 fprintf(stderr, "sign() failed: %d\n", ret);
418 return NULL;
419 }
420
421 h->header_version_major = 2;
422 h->header_version_minor = 2;
423 h->preamble_size = block_size;
424 h->kernel_version = 1;
425 h->body_load_address = 0x100000;
426 /*
427 * "Bootloder" area (currently empty) is after kernel + config +
428 * parameter page.
429 */
430 h->bootloader_address = h->body_load_address +
431 ROUNDUP(kernel_len) +
432 ROUNDUP(config_len) +
433 ALIGN;
434 h->bootloader_size = 0x1000;
435 h->vmlinuz_header_address = 0;
436 h->vmlinuz_header_size = 0;
437 h->flags = 0;
438
439 ret = sign(h, signed_size, &h->preamble_signature,
440 (void *)(h + 1) + SIG_SIZE);
441 if (ret) {
442 fprintf(stderr, "failed to sign preamble: %d\n", ret);
443 return NULL;
444 }
445 return h;
446 }
447
448
449 /*
450 * Distilled from vboot_reference futility/cmd_vbutil_kernel.c pack command.
451 *
452 * NB: "config" is the kernel cmdline
453 */
454 static int vbutil_pack(const char *kernel, size_t kernel_len,
455 const char *config, size_t config_len,
456 void **out, size_t *out_len)
457 {
458 size_t kblob_len = ROUNDUP(kernel_len) + ROUNDUP(config_len) + ALIGN + ALIGN;
459 void *kblob = malloc(kblob_len);
460 if (!kblob) {
461 perror("malloc");
462 return -1;
463 }
464 void *kp = kblob;
465 memset(kblob, 0, kblob_len);
466
467 /* Kernel */
468 memcpy(kp, kernel, kernel_len);
469 kp += ROUNDUP(kernel_len);
470
471 /* Kernel command line */
472 memcpy(kp, config, config_len);
473 kp += ROUNDUP(config_len);
474
475 /* "Parameters" -- empty, 4K-aligned */
476 /* "Bootloader" region -- empty, 4K-aligned */
477 /* Vmlinuz header -- only for x86 (not currently supported), empty */
478
479 struct vb2_kernel_preamble *h;
480 h = generate_preamble(kblob, kblob_len, kernel_len, config_len);
481 if (!h) {
482 fprintf(stderr, "Failed to generate preamble\n");
483 return -1;
484 }
485 size_t outlen = sizeof(keyblock) + h->preamble_size + kblob_len;
486 void *outbuf = malloc(outlen);
487 if (!outbuf)
488 return -1;
489
490 void *p = outbuf;
491 memcpy(p, keyblock, sizeof(keyblock));
492 p += sizeof(keyblock);
493
494 memcpy(p, h, h->preamble_size);
495 p += h->preamble_size;
496
497 memcpy(p, kblob, kblob_len);
498 p += kblob_len;
499
500 if (outbuf) {
501 *out = outbuf;
502 *out_len = outlen;
503 }
504
505 return 0;
506 }
507
508 static void * read_file(const char *path, size_t *out_len)
509 {
510 struct stat st;
511 int fd = open(path, O_RDONLY);
512 if (fd == -1) {
513 perror("failed to open");
514 return NULL;
515 }
516
517 if (fstat(fd, &st) < 0) {
518 perror("fstat");
519 return NULL;
520 }
521
522 size_t len = st.st_size;
523 void *p = malloc(len);
524 if (!p)
525 return NULL;
526
527 size_t count = 0;
528 while (count < len) {
529 ssize_t ret = read(fd, p + count, len - count);
530 if (ret < 0) {
531 perror("read");
532 return NULL;
533 }
534 count += ret;
535 }
536
537 close(fd);
538
539 *out_len = len;
540 return p;
541 }
542
543 int main(int argc, char * const argv[])
544 {
545 int ret;
546 const char *kernel_file = NULL;
547 const char *out_file = NULL;
548 const char *cmdline = NULL;
549 size_t cmdline_len;
550 void *kernel;
551 size_t kernel_len;
552 void *out;
553 size_t out_len;
554
555 int opt;
556 while ((opt = getopt(argc, argv, "k:c:o:")) != -1) {
557 switch (opt) {
558 case 'k':
559 kernel_file = optarg;
560 break;
561 case 'c':
562 cmdline = optarg;
563 break;
564 case 'o':
565 out_file = optarg;
566 break;
567 default:
568 fprintf(stderr, "Usage [-k <kernel>] [-c <command line>] -o <outfile>\n");
569 return -1;
570 }
571 }
572 if (optind < argc) {
573 fprintf(stderr, "Unexpected args?\n");
574 return -1;
575 }
576 if (!out_file || !cmdline || !kernel_file) {
577 fprintf(stderr, "Missing required argument\n");
578 return -1;
579 }
580
581 /* Include the \0 terminator */
582 cmdline_len = strlen(cmdline) + 1;
583
584 kernel = read_file(kernel_file, &kernel_len);
585 if (!kernel) {
586 fprintf(stderr, "failed to read kernel file: %s\n", kernel_file);
587 return -1;
588 }
589
590 ret = vbutil_pack(kernel, kernel_len, cmdline, cmdline_len, &out, &out_len);
591 if (ret)
592 return ret;
593
594 int fd = open(out_file, O_RDWR|O_CREAT, 0644);
595 if (fd == -1) {
596 perror("open");
597 return -1;
598 }
599
600 ret = write(fd, out, out_len);
601 if (ret == -1) {
602 perror("write");
603 return -1;
604 }
605 close(fd);
606
607 return 0;
608 }