Debian Codegraph @

util/nvidia/cbootimage/src/crypto.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * See file CREDITS for list of people who contributed to this
17 * project.
18 */
19
20 /*
21 * crypto.c - Cryptography support
22 */
23 #include "crypto.h"
24 #include "parse.h"
25 #include "nvaes_ref.h"
26 #include <stdio.h>
27
28 /* Local function declarations */
29 static void
30 apply_cbc_chain_data(uint8_t *cbc_chain_data,
31 uint8_t *src,
32 uint8_t *dst);
33
34 static void
35 generate_key_schedule(uint8_t *key, uint8_t *key_schedule);
36
37 static void
38 encrypt_object( uint8_t *key_schedule,
39 uint8_t *src,
40 uint8_t *dst,
41 uint32_t num_aes_blocks);
42
43 static int
44 encrypt_and_sign(uint8_t *key,
45 uint8_t *src,
46 uint32_t length,
47 uint8_t *sig_dst);
48
49 uint8_t enable_debug_crypto = 0;
50
51 /* Implementation */
52 static uint8_t zero_key[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0 };
54
55 static void
56 print_vector(char *name, uint32_t num_bytes, uint8_t *data)
57 {
58 uint32_t i;
59
60 printf("%s [%d] @%p", name, num_bytes, data);
61 for (i=0; i<num_bytes; i++) {
62 if ( i % 16 == 0 )
63 printf(" = ");
64 printf("%02x", data[i]);
65 if ( (i+1) % 16 != 0 )
66 printf(" ");
67 }
68 printf("\n");
69 }
70
71
72 static void
73 apply_cbc_chain_data(uint8_t *cbc_chain_data,
74 uint8_t *src,
75 uint8_t *dst)
76 {
77 int i;
78
79 for (i = 0; i < 16; i++) {
80 *dst++ = *src++ ^ *cbc_chain_data++;
81 }
82 }
83
84 static void
85 generate_key_schedule(uint8_t *key, uint8_t *key_schedule)
86 {
87 /*
88 * The only need for a key is for signing/checksum purposes, so
89 * expand a key of 0's.
90 */
91 nv_aes_expand_key(zero_key, key_schedule);
92 }
93
94 static void
95 encrypt_object(uint8_t *key_schedule,
96 uint8_t *src,
97 uint8_t *dst,
98 uint32_t num_aes_blocks)
99 {
100 uint32_t i;
101 uint8_t *cbc_chain_data;
102 uint8_t tmp_data[KEY_LENGTH];
103
104 cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
105
106 for (i = 0; i < num_aes_blocks; i++) {
107 if (enable_debug_crypto) {
108 printf("encrypt_object: block %d of %d\n", i,
109 num_aes_blocks);
110 print_vector("AES Src", KEY_LENGTH, src);
111 }
112
113 /* Apply the chain data */
114 apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
115
116 if (enable_debug_crypto)
117 print_vector("AES Xor", KEY_LENGTH,
118 tmp_data);
119
120 /* encrypt the AES block */
121 nv_aes_encrypt(tmp_data, key_schedule, dst);
122
123 if (enable_debug_crypto)
124 print_vector("AES Dst", KEY_LENGTH, dst);
125 /* Update pointers for next loop. */
126 cbc_chain_data = dst;
127 src += KEY_LENGTH;
128 dst += KEY_LENGTH;
129 }
130 }
131
132 static void
133 left_shift_vector(uint8_t *in,
134 uint8_t *out,
135 uint32_t size)
136 {
137 uint32_t i;
138 uint8_t carry = 0;
139
140 for (i=0; i<size; i++) {
141 uint32_t j = size-1-i;
142
143 out[j] = (in[j] << 1) | carry;
144 carry = in[j] >> 7; /* get most significant bit */
145 }
146 }
147
148 static void
149 sign_objext(
150 uint8_t *key,
151 uint8_t *key_schedule,
152 uint8_t *src,
153 uint8_t *dst,
154 uint32_t num_aes_blocks)
155 {
156 uint32_t i;
157 uint8_t *cbc_chain_data;
158
159 uint8_t l[KEY_LENGTH];
160 uint8_t k1[KEY_LENGTH];
161 uint8_t tmp_data[KEY_LENGTH];
162
163 cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
164
165 /* compute k1 constant needed by AES-CMAC calculation */
166
167 for (i=0; i<KEY_LENGTH; i++)
168 tmp_data[i] = 0;
169
170 encrypt_object(key_schedule, tmp_data, l, 1);
171
172 if (enable_debug_crypto)
173 print_vector("AES(key, nonce)", KEY_LENGTH, l);
174
175 left_shift_vector(l, k1, sizeof(l));
176
177 if (enable_debug_crypto)
178 print_vector("L", KEY_LENGTH, l);
179
180 if ( (l[0] >> 7) != 0 ) /* get MSB of L */
181 k1[KEY_LENGTH-1] ^= AES_CMAC_CONST_RB;
182
183 if (enable_debug_crypto)
184 print_vector("K1", KEY_LENGTH, k1);
185
186 /* compute the AES-CMAC value */
187 for (i = 0; i < num_aes_blocks; i++) {
188 /* Apply the chain data */
189 apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
190
191 /* for the final block, XOR k1 into the IV */
192 if ( i == num_aes_blocks-1 )
193 apply_cbc_chain_data(tmp_data, k1, tmp_data);
194
195 /* encrypt the AES block */
196 nv_aes_encrypt(tmp_data, key_schedule, (uint8_t*)dst);
197
198 if (enable_debug_crypto) {
199 printf("sign_objext: block %d of %d\n", i,
200 num_aes_blocks);
201 print_vector("AES-CMAC Src", KEY_LENGTH, src);
202 print_vector("AES-CMAC Xor", KEY_LENGTH, tmp_data);
203 print_vector("AES-CMAC Dst",
204 KEY_LENGTH,
205 (uint8_t*)dst);
206 }
207
208 /* Update pointers for next loop. */
209 cbc_chain_data = (uint8_t*)dst;
210 src += KEY_LENGTH;
211 }
212
213 if (enable_debug_crypto)
214 print_vector("AES-CMAC Hash", KEY_LENGTH, (uint8_t*)dst);
215 }
216
217 static int
218 encrypt_and_sign(uint8_t *key,
219 uint8_t *src,
220 uint32_t length,
221 uint8_t *sig_dst)
222 {
223 uint32_t num_aes_blocks;
224 uint8_t key_schedule[4*NVAES_STATECOLS*(NVAES_ROUNDS+1)];
225
226 if (enable_debug_crypto) {
227 printf("encrypt_and_sign: length = %d\n", length);
228 print_vector("AES key", KEY_LENGTH, key);
229 }
230
231 generate_key_schedule(key, key_schedule);
232
233 num_aes_blocks = ICEIL(length, KEY_LENGTH);
234
235 if (enable_debug_crypto)
236 printf("encrypt_and_sign: begin signing\n");
237
238 /* encrypt the data, overwriting the result in signature. */
239 sign_objext(key, key_schedule, src, sig_dst, num_aes_blocks);
240
241 if (enable_debug_crypto)
242 printf("encrypt_and_sign: end signing\n");
243
244 return 0;
245 }
246
247 int
248 sign_data_block(uint8_t *source,
249 uint32_t length,
250 uint8_t *signature)
251 {
252 return encrypt_and_sign(zero_key,
253 source,
254 length,
255 signature);
256 }
257
258 int
259 sign_bct(build_image_context *context,
260 uint8_t *bct)
261 {
262 uint32_t Offset;
263 uint32_t length;
264 uint32_t hash_size;
265 uint8_t *hash_buffer = NULL;
266 int e = 0;
267
268 assert(bct != NULL);
269
270 if (g_soc_config->get_value(token_hash_size,
271 &hash_size,
272 bct) != 0)
273 return -ENODATA;
274 if (g_soc_config->get_value(token_crypto_offset,
275 &Offset,
276 bct) != 0)
277 return -ENODATA;
278 if (g_soc_config->get_value(token_crypto_length,
279 &length,
280 bct) != 0)
281 return -ENODATA;
282
283 hash_buffer = calloc(1, hash_size);
284 if (hash_buffer == NULL)
285 return -ENOMEM;
286 e = sign_data_block(bct + Offset, length, hash_buffer);
287 if (e != 0)
288 goto fail;
289 e = g_soc_config->set_data(token_crypto_hash,
290 hash_buffer,
291 hash_size,
292 bct);
293 if (e != 0)
294 goto fail;
295
296 fail:
297 free(hash_buffer);
298 return e;
299 }
300
301 /*
302 * reverse_byte_order
303 *
304 * Reverse the order of bytes pointed by 'in' and place the results
305 * to location pointed by 'out'. If 'out' is the same as 'in', then
306 * order of bytes pointed by 'in' is reversed.
307 */
308 void
309 reverse_byte_order(
310 uint8_t *out,
311 const uint8_t *in,
312 const uint32_t size)
313 {
314 uint32_t i, j;
315 uint8_t b1, b2;
316
317 for (i = 0; i < size / 2; i++) {
318 j = size - 1 - i;
319 b1 = in[i];
320 b2 = in[j];
321 out[i] = b2;
322 out[j] = b1;
323 }
324
325 /* In case odd number of bytes */
326 if (size % 2)
327 out[size / 2] = in[size / 2];
328 }
329
330 int
331 sign_bl(build_image_context *context,
332 uint8_t *bootloader,
333 uint32_t length,
334 uint32_t image_instance)
335 {
336 int e = 0;
337 uint8_t *hash_buffer;
338 uint32_t hash_size;
339
340 g_soc_config->get_value(token_hash_size,
341 &hash_size, context->bct);
342
343 hash_buffer = calloc(1, hash_size);
344 if (hash_buffer == NULL)
345 return -ENOMEM;
346
347 /* Encrypt and compute hash */
348 if ((e = sign_data_block(bootloader,
349 length,
350 hash_buffer)) != 0)
351 goto fail;
352
353 if ((e = g_soc_config->setbl_param(image_instance,
354 token_bl_crypto_hash,
355 (uint32_t*)hash_buffer,
356 context->bct)) != 0)
357 goto fail;
358
359 fail:
360 free(hash_buffer);
361 return e;
362 }