Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: board/tegra2/common/crypto/crypto.c

Issue 4841001: Tegra2: implement Warmboot code and lp0_vec (Closed) Base URL: http://git.chromium.org/git/u-boot-next.git@chromeos-v2010.09
Patch Set: Add GPL headers & fix some 80-column issues Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « board/tegra2/common/crypto/crypto.h ('k') | board/tegra2/common/crypto/nvaes_ref.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * (C) Copyright 2010
3 * NVIDIA Corporation <www.nvidia.com>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24 /*
25 * crypto.c - Cryptography support
26 */
27
28 #include <common.h>
29 #include <asm/arch/nvcommon.h>
30 #include <asm/arch/nvboot_error.h>
31 #include "crypto.h"
32 #include "nvaes_ref.h"
33
34 /* Local function declarations */
35 static void
36 apply_cbc_chain_data(NvU8 *cbc_chain_data, NvU8 *src, NvU8 *dst);
37
38 static NvBootError
39 determine_crypto_ops(security_mode security, NvBool *encrypt, NvBool *sign);
40
41 static void
42 generate_key_schedule(NvU8 *key, NvU8 *key_schedule, NvBool encrypt_data);
43
44 static void
45 encrypt_object( NvU8 *key_schedule,
46 NvU8 *src,
47 NvU8 *dst,
48 NvU32 num_aes_blocks);
49
50 static NvBootError
51 encrypt_and_sign(NvU8 *key,
52 security_mode security,
53 NvU8 *src,
54 NvU32 length,
55 NvU8 *sig_dst);
56
57 NvBool enable_debug_crypto = NV_FALSE;
58
59 /* Implementation */
60 static NvU8 zero_key[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
61
62 static void
63 print_vector(char *name, NvU32 num_bytes, NvU8 *data)
64 {
65 NvU32 i;
66
67 printf("%s [%d] @0x%08x", name, num_bytes, (NvU32)data);
68 for (i=0; i<num_bytes; i++) {
69 if ( i % 16 == 0 )
70 printf(" = ");
71 printf("%02x", data[i]);
72 if ( (i+1) % 16 != 0 )
73 printf(" ");
74 }
75 printf("\n");
76 }
77
78
79 static void
80 apply_cbc_chain_data(NvU8 *cbc_chain_data, NvU8 *src, NvU8 *dst)
81 {
82 int i;
83
84 for (i = 0; i < 16; i++) {
85 *dst++ = *src++ ^ *cbc_chain_data++;
86 }
87 }
88
89 static NvBootError
90 determine_crypto_ops(security_mode security, NvBool *encrypt, NvBool *sign)
91 {
92 NvBootError Error = NvBootError_Success;
93
94 switch (security) {
95 case security_mode_Plaintext:
96 *encrypt = NV_FALSE;
97 *sign = NV_FALSE;
98 break;
99
100 case security_mode_Checksum:
101 *encrypt = NV_FALSE;
102 *sign = NV_TRUE;
103 break;
104
105 case security_mode_Encrypted:
106 *encrypt = NV_TRUE;
107 *sign = NV_TRUE;
108 break;
109
110 default:
111 printf("determine_crypto_ops: security mode not set.\n");
112 Error = NvBootError_InvalidParameter;
113 }
114
115 return Error;
116 }
117
118 static void
119 generate_key_schedule(NvU8 *key, NvU8 *key_schedule, NvBool encrypt_data)
120 {
121 /* Expand the key to produce a key schedule. */
122 if (encrypt_data == NV_TRUE) {
123 /* Expand the provided key. */
124 nv_aes_expand_key(key, key_schedule);
125 } else {
126 /*
127 * The only need for a key is for signing/checksum purposes, so
128 * expand a key of 0's.
129 */
130 nv_aes_expand_key(zero_key, key_schedule);
131 }
132 }
133
134 static void
135 encrypt_object(NvU8 *key_schedule,
136 NvU8 *src,
137 NvU8 *dst,
138 NvU32 num_aes_blocks)
139 {
140 NvU32 i;
141 NvU8 *cbc_chain_data;
142 NvU8 tmp_data[KEY_LENGTH];
143
144 cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
145
146 for (i = 0; i < num_aes_blocks; i++) {
147 if (enable_debug_crypto) {
148 printf("encrypt_object: block %d of %d\n", i,
149 num_aes_blocks);
150 print_vector("AES Src", KEY_LENGTH, src);
151 }
152
153 /* Apply the chain data */
154 apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
155
156 if (enable_debug_crypto) print_vector("AES Xor", KEY_LENGTH,
157 tmp_data);
158
159 /* encrypt the AES block */
160 nv_aes_encrypt(tmp_data, key_schedule, dst);
161
162 if (enable_debug_crypto)
163 print_vector("AES Dst", KEY_LENGTH, dst);
164 /* Update pointers for next loop. */
165 cbc_chain_data = dst;
166 src += KEY_LENGTH;
167 dst += KEY_LENGTH;
168 }
169 }
170
171 static void
172 left_shift_vector(NvU8 *In,
173 NvU8 *Out,
174 NvU32 Size)
175 {
176 NvU32 i;
177 NvU8 Carry = 0;
178
179 for (i=0; i<Size; i++) {
180 NvU32 j = Size-1-i;
181
182 Out[j] = (In[j] << 1) | Carry;
183 Carry = In[j] >> 7; /* get most significant bit */
184 }
185 }
186
187 static void
188 sign_objext(
189 NvU8 *key,
190 NvU8 *key_schedule,
191 NvU8 *src,
192 NvU8 *dst,
193 NvU32 num_aes_blocks)
194 {
195 NvU32 i;
196 NvU8 *cbc_chain_data;
197
198 NvU8 L[KEY_LENGTH];
199 NvU8 K1[KEY_LENGTH];
200 NvU8 tmp_data[KEY_LENGTH];
201
202 cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
203
204 /* compute K1 constant needed by AES-CMAC calculation */
205
206 for (i=0; i<KEY_LENGTH; i++)
207 tmp_data[i] = 0;
208
209 encrypt_object(key_schedule, tmp_data, L, 1);
210
211 if (enable_debug_crypto)
212 print_vector("AES(key, nonce)", KEY_LENGTH, L);
213
214 left_shift_vector(L, K1, sizeof(L));
215
216 if (enable_debug_crypto)
217 print_vector("L", KEY_LENGTH, L);
218
219 if ( (L[0] >> 7) != 0 ) /* get MSB of L */
220 K1[KEY_LENGTH-1] ^= AES_CMAC_CONST_RB;
221
222 if (enable_debug_crypto)
223 print_vector("K1", KEY_LENGTH, K1);
224
225 /* compute the AES-CMAC value */
226 for (i = 0; i < num_aes_blocks; i++) {
227 /* Apply the chain data */
228 apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
229
230 /* for the final block, XOR K1 into the IV */
231 if ( i == num_aes_blocks-1 )
232 apply_cbc_chain_data(tmp_data, K1, tmp_data);
233
234 /* encrypt the AES block */
235 nv_aes_encrypt(tmp_data, key_schedule, (NvU8*)dst);
236
237 if (enable_debug_crypto) {
238 printf("sign_objext: block %d of %d\n", i,
239 num_aes_blocks);
240 print_vector("AES-CMAC Src", KEY_LENGTH, src);
241 print_vector("AES-CMAC Xor", KEY_LENGTH, tmp_data);
242 print_vector("AES-CMAC Dst", KEY_LENGTH, (NvU8*)dst);
243 }
244
245 /* Update pointers for next loop. */
246 cbc_chain_data = (NvU8*)dst;
247 src += KEY_LENGTH;
248 }
249
250 if (enable_debug_crypto)
251 print_vector("AES-CMAC Hash", KEY_LENGTH, (NvU8*)dst);
252 }
253
254 static NvBootError
255 encrypt_and_sign(NvU8 *key,
256 security_mode security,
257 NvU8 *src,
258 NvU32 length,
259 NvU8 *sig_dst)
260 {
261 NvBool encrypt_data;
262 NvBool sign_data;
263 NvU32 num_aes_blocks;
264 NvU8 key_schedule[4*NVAES_STATECOLS*(NVAES_ROUNDS+1)];
265 NvBootError Error;
266
267 Error = determine_crypto_ops(security, &encrypt_data, &sign_data);
268 if (Error != NvBootError_Success)
269 return Error;
270
271 if (enable_debug_crypto) {
272 printf("encrypt_and_sign: Length = %d\n", length);
273 print_vector("AES key", KEY_LENGTH, key);
274 }
275
276 generate_key_schedule(key, key_schedule, encrypt_data);
277
278 num_aes_blocks = ICEIL(length, KEY_LENGTH);
279
280 if (encrypt_data == NV_TRUE) {
281 if (enable_debug_crypto)
282 printf("encrypt_and_sign: begin encryption\n");
283
284 /* Perform this in place, resulting in src being encrypted. */
285 encrypt_object(key_schedule, src, src, num_aes_blocks);
286
287 if (enable_debug_crypto)
288 printf("encrypt_and_sign: end encryption\n");
289 }
290
291 if (sign_data == NV_TRUE) {
292 if (enable_debug_crypto)
293 printf("encrypt_and_sign: begin signing\n");
294
295 /* encrypt the data, overwriting the result in signature. */
296 sign_objext(key, key_schedule, src, sig_dst, num_aes_blocks);
297
298 if (enable_debug_crypto)
299 printf("encrypt_and_sign: end signing\n");
300 }
301
302 return Error;
303 }
304
305 NvBootError
306 encrypt_and_sign_block(NvU8 *key,
307 security_mode security,
308 NvU8 *block_image,
309 NvU32 block_len,
310 NvU8 *signature)
311 {
312 NvBootError e;
313
314 e = encrypt_and_sign(key, security, block_image, block_len, signature);
315 return e;
316 }
317
318 NvBootError
319 sign_data_block(NvU8 *source,
320 NvU32 length,
321 NvU8 *signature)
322 {
323 return encrypt_and_sign_block(zero_key,
324 security_mode_Checksum,
325 source,
326 length,
327 signature);
328 }
OLDNEW
« no previous file with comments | « board/tegra2/common/crypto/crypto.h ('k') | board/tegra2/common/crypto/nvaes_ref.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698