Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Cryptographic API. | |
3 | * | |
4 | * Support for VIA PadLock hardware crypto engine. | |
5 | * | |
6 | * Copyright (c) 2004 Michal Ludvig <michal@logix.cz> | |
7 | * | |
8 | * Key expansion routine taken from crypto/aes.c | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * --------------------------------------------------------------------------- | |
16 | * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK. | |
17 | * All rights reserved. | |
18 | * | |
19 | * LICENSE TERMS | |
20 | * | |
21 | * The free distribution and use of this software in both source and binary | |
22 | * form is allowed (with or without changes) provided that: | |
23 | * | |
24 | * 1. distributions of this source code include the above copyright | |
25 | * notice, this list of conditions and the following disclaimer; | |
26 | * | |
27 | * 2. distributions in binary form include the above copyright | |
28 | * notice, this list of conditions and the following disclaimer | |
29 | * in the documentation and/or other associated materials; | |
30 | * | |
31 | * 3. the copyright holder's name is not used to endorse products | |
32 | * built using this software without specific written permission. | |
33 | * | |
34 | * ALTERNATIVELY, provided that this notice is retained in full, this product | |
35 | * may be distributed under the terms of the GNU General Public License (GPL), | |
36 | * in which case the provisions of the GPL apply INSTEAD OF those given above. | |
37 | * | |
38 | * DISCLAIMER | |
39 | * | |
40 | * This software is provided 'as is' with no explicit or implied warranties | |
41 | * in respect of its properties, including, but not limited to, correctness | |
42 | * and/or fitness for purpose. | |
43 | * --------------------------------------------------------------------------- | |
44 | */ | |
45 | ||
46 | #include <linux/module.h> | |
47 | #include <linux/init.h> | |
48 | #include <linux/types.h> | |
49 | #include <linux/errno.h> | |
50 | #include <linux/crypto.h> | |
51 | #include <linux/interrupt.h> | |
6789b2dc | 52 | #include <linux/kernel.h> |
1da177e4 LT |
53 | #include <asm/byteorder.h> |
54 | #include "padlock.h" | |
55 | ||
56 | #define AES_MIN_KEY_SIZE 16 /* in uint8_t units */ | |
57 | #define AES_MAX_KEY_SIZE 32 /* ditto */ | |
58 | #define AES_BLOCK_SIZE 16 /* ditto */ | |
59 | #define AES_EXTENDED_KEY_SIZE 64 /* in uint32_t units */ | |
60 | #define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t)) | |
61 | ||
ccc17c34 ML |
62 | /* Control word. */ |
63 | struct cword { | |
64 | unsigned int __attribute__ ((__packed__)) | |
65 | rounds:4, | |
66 | algo:3, | |
67 | keygen:1, | |
68 | interm:1, | |
69 | encdec:1, | |
70 | ksize:2; | |
71 | } __attribute__ ((__aligned__(PADLOCK_ALIGNMENT))); | |
72 | ||
cc08632f ML |
73 | /* Whenever making any changes to the following |
74 | * structure *make sure* you keep E, d_data | |
75 | * and cword aligned on 16 Bytes boundaries!!! */ | |
1da177e4 | 76 | struct aes_ctx { |
6789b2dc HX |
77 | struct { |
78 | struct cword encrypt; | |
79 | struct cword decrypt; | |
80 | } cword; | |
82062c72 | 81 | u32 *D; |
1da177e4 | 82 | int key_length; |
cc08632f ML |
83 | u32 E[AES_EXTENDED_KEY_SIZE] |
84 | __attribute__ ((__aligned__(PADLOCK_ALIGNMENT))); | |
85 | u32 d_data[AES_EXTENDED_KEY_SIZE] | |
86 | __attribute__ ((__aligned__(PADLOCK_ALIGNMENT))); | |
1da177e4 LT |
87 | }; |
88 | ||
89 | /* ====== Key management routines ====== */ | |
90 | ||
91 | static inline uint32_t | |
92 | generic_rotr32 (const uint32_t x, const unsigned bits) | |
93 | { | |
94 | const unsigned n = bits % 32; | |
95 | return (x >> n) | (x << (32 - n)); | |
96 | } | |
97 | ||
98 | static inline uint32_t | |
99 | generic_rotl32 (const uint32_t x, const unsigned bits) | |
100 | { | |
101 | const unsigned n = bits % 32; | |
102 | return (x << n) | (x >> (32 - n)); | |
103 | } | |
104 | ||
105 | #define rotl generic_rotl32 | |
106 | #define rotr generic_rotr32 | |
107 | ||
108 | /* | |
109 | * #define byte(x, nr) ((unsigned char)((x) >> (nr*8))) | |
110 | */ | |
111 | static inline uint8_t | |
112 | byte(const uint32_t x, const unsigned n) | |
113 | { | |
114 | return x >> (n << 3); | |
115 | } | |
116 | ||
1da177e4 LT |
117 | #define E_KEY ctx->E |
118 | #define D_KEY ctx->D | |
119 | ||
120 | static uint8_t pow_tab[256]; | |
121 | static uint8_t log_tab[256]; | |
122 | static uint8_t sbx_tab[256]; | |
123 | static uint8_t isb_tab[256]; | |
124 | static uint32_t rco_tab[10]; | |
125 | static uint32_t ft_tab[4][256]; | |
126 | static uint32_t it_tab[4][256]; | |
127 | ||
128 | static uint32_t fl_tab[4][256]; | |
129 | static uint32_t il_tab[4][256]; | |
130 | ||
131 | static inline uint8_t | |
132 | f_mult (uint8_t a, uint8_t b) | |
133 | { | |
134 | uint8_t aa = log_tab[a], cc = aa + log_tab[b]; | |
135 | ||
136 | return pow_tab[cc + (cc < aa ? 1 : 0)]; | |
137 | } | |
138 | ||
139 | #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0) | |
140 | ||
141 | #define f_rn(bo, bi, n, k) \ | |
142 | bo[n] = ft_tab[0][byte(bi[n],0)] ^ \ | |
143 | ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \ | |
144 | ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ | |
145 | ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n) | |
146 | ||
147 | #define i_rn(bo, bi, n, k) \ | |
148 | bo[n] = it_tab[0][byte(bi[n],0)] ^ \ | |
149 | it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \ | |
150 | it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ | |
151 | it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n) | |
152 | ||
153 | #define ls_box(x) \ | |
154 | ( fl_tab[0][byte(x, 0)] ^ \ | |
155 | fl_tab[1][byte(x, 1)] ^ \ | |
156 | fl_tab[2][byte(x, 2)] ^ \ | |
157 | fl_tab[3][byte(x, 3)] ) | |
158 | ||
159 | #define f_rl(bo, bi, n, k) \ | |
160 | bo[n] = fl_tab[0][byte(bi[n],0)] ^ \ | |
161 | fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \ | |
162 | fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ | |
163 | fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n) | |
164 | ||
165 | #define i_rl(bo, bi, n, k) \ | |
166 | bo[n] = il_tab[0][byte(bi[n],0)] ^ \ | |
167 | il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \ | |
168 | il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ | |
169 | il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n) | |
170 | ||
171 | static void | |
172 | gen_tabs (void) | |
173 | { | |
174 | uint32_t i, t; | |
175 | uint8_t p, q; | |
176 | ||
177 | /* log and power tables for GF(2**8) finite field with | |
178 | 0x011b as modular polynomial - the simplest prmitive | |
179 | root is 0x03, used here to generate the tables */ | |
180 | ||
181 | for (i = 0, p = 1; i < 256; ++i) { | |
182 | pow_tab[i] = (uint8_t) p; | |
183 | log_tab[p] = (uint8_t) i; | |
184 | ||
185 | p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0); | |
186 | } | |
187 | ||
188 | log_tab[1] = 0; | |
189 | ||
190 | for (i = 0, p = 1; i < 10; ++i) { | |
191 | rco_tab[i] = p; | |
192 | ||
193 | p = (p << 1) ^ (p & 0x80 ? 0x01b : 0); | |
194 | } | |
195 | ||
196 | for (i = 0; i < 256; ++i) { | |
197 | p = (i ? pow_tab[255 - log_tab[i]] : 0); | |
198 | q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2)); | |
199 | p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2)); | |
200 | sbx_tab[i] = p; | |
201 | isb_tab[p] = (uint8_t) i; | |
202 | } | |
203 | ||
204 | for (i = 0; i < 256; ++i) { | |
205 | p = sbx_tab[i]; | |
206 | ||
207 | t = p; | |
208 | fl_tab[0][i] = t; | |
209 | fl_tab[1][i] = rotl (t, 8); | |
210 | fl_tab[2][i] = rotl (t, 16); | |
211 | fl_tab[3][i] = rotl (t, 24); | |
212 | ||
213 | t = ((uint32_t) ff_mult (2, p)) | | |
214 | ((uint32_t) p << 8) | | |
215 | ((uint32_t) p << 16) | ((uint32_t) ff_mult (3, p) << 24); | |
216 | ||
217 | ft_tab[0][i] = t; | |
218 | ft_tab[1][i] = rotl (t, 8); | |
219 | ft_tab[2][i] = rotl (t, 16); | |
220 | ft_tab[3][i] = rotl (t, 24); | |
221 | ||
222 | p = isb_tab[i]; | |
223 | ||
224 | t = p; | |
225 | il_tab[0][i] = t; | |
226 | il_tab[1][i] = rotl (t, 8); | |
227 | il_tab[2][i] = rotl (t, 16); | |
228 | il_tab[3][i] = rotl (t, 24); | |
229 | ||
230 | t = ((uint32_t) ff_mult (14, p)) | | |
231 | ((uint32_t) ff_mult (9, p) << 8) | | |
232 | ((uint32_t) ff_mult (13, p) << 16) | | |
233 | ((uint32_t) ff_mult (11, p) << 24); | |
234 | ||
235 | it_tab[0][i] = t; | |
236 | it_tab[1][i] = rotl (t, 8); | |
237 | it_tab[2][i] = rotl (t, 16); | |
238 | it_tab[3][i] = rotl (t, 24); | |
239 | } | |
240 | } | |
241 | ||
242 | #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b) | |
243 | ||
244 | #define imix_col(y,x) \ | |
245 | u = star_x(x); \ | |
246 | v = star_x(u); \ | |
247 | w = star_x(v); \ | |
248 | t = w ^ (x); \ | |
249 | (y) = u ^ v ^ w; \ | |
250 | (y) ^= rotr(u ^ t, 8) ^ \ | |
251 | rotr(v ^ t, 16) ^ \ | |
252 | rotr(t,24) | |
253 | ||
254 | /* initialise the key schedule from the user supplied key */ | |
255 | ||
256 | #define loop4(i) \ | |
257 | { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \ | |
258 | t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \ | |
259 | t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \ | |
260 | t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \ | |
261 | t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \ | |
262 | } | |
263 | ||
264 | #define loop6(i) \ | |
265 | { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \ | |
266 | t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \ | |
267 | t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \ | |
268 | t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \ | |
269 | t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \ | |
270 | t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \ | |
271 | t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \ | |
272 | } | |
273 | ||
274 | #define loop8(i) \ | |
275 | { t = rotr(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \ | |
276 | t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \ | |
277 | t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \ | |
278 | t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \ | |
279 | t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \ | |
280 | t = E_KEY[8 * i + 4] ^ ls_box(t); \ | |
281 | E_KEY[8 * i + 12] = t; \ | |
282 | t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \ | |
283 | t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \ | |
284 | t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \ | |
285 | } | |
286 | ||
287 | /* Tells whether the ACE is capable to generate | |
288 | the extended key for a given key_len. */ | |
289 | static inline int | |
290 | aes_hw_extkey_available(uint8_t key_len) | |
291 | { | |
292 | /* TODO: We should check the actual CPU model/stepping | |
293 | as it's possible that the capability will be | |
294 | added in the next CPU revisions. */ | |
295 | if (key_len == 16) | |
296 | return 1; | |
297 | return 0; | |
298 | } | |
299 | ||
6c2bb98b | 300 | static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm) |
6789b2dc | 301 | { |
6c2bb98b | 302 | unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm); |
f10b7897 HX |
303 | unsigned long align = PADLOCK_ALIGNMENT; |
304 | ||
305 | if (align <= crypto_tfm_ctx_alignment()) | |
306 | align = 1; | |
6c2bb98b | 307 | return (struct aes_ctx *)ALIGN(addr, align); |
6789b2dc HX |
308 | } |
309 | ||
6c2bb98b | 310 | static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
560c06ae | 311 | unsigned int key_len) |
1da177e4 | 312 | { |
6c2bb98b | 313 | struct aes_ctx *ctx = aes_ctx(tfm); |
06ace7a9 | 314 | const __le32 *key = (const __le32 *)in_key; |
560c06ae | 315 | u32 *flags = &tfm->crt_flags; |
1da177e4 LT |
316 | uint32_t i, t, u, v, w; |
317 | uint32_t P[AES_EXTENDED_KEY_SIZE]; | |
318 | uint32_t rounds; | |
319 | ||
560c06ae | 320 | if (key_len % 8) { |
1da177e4 LT |
321 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
322 | return -EINVAL; | |
323 | } | |
324 | ||
325 | ctx->key_length = key_len; | |
326 | ||
6789b2dc HX |
327 | /* |
328 | * If the hardware is capable of generating the extended key | |
329 | * itself we must supply the plain key for both encryption | |
330 | * and decryption. | |
331 | */ | |
82062c72 | 332 | ctx->D = ctx->E; |
1da177e4 | 333 | |
06ace7a9 HX |
334 | E_KEY[0] = le32_to_cpu(key[0]); |
335 | E_KEY[1] = le32_to_cpu(key[1]); | |
336 | E_KEY[2] = le32_to_cpu(key[2]); | |
337 | E_KEY[3] = le32_to_cpu(key[3]); | |
1da177e4 | 338 | |
6789b2dc HX |
339 | /* Prepare control words. */ |
340 | memset(&ctx->cword, 0, sizeof(ctx->cword)); | |
341 | ||
342 | ctx->cword.decrypt.encdec = 1; | |
343 | ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4; | |
344 | ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds; | |
345 | ctx->cword.encrypt.ksize = (key_len - 16) / 8; | |
346 | ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize; | |
347 | ||
1da177e4 LT |
348 | /* Don't generate extended keys if the hardware can do it. */ |
349 | if (aes_hw_extkey_available(key_len)) | |
350 | return 0; | |
351 | ||
6789b2dc HX |
352 | ctx->D = ctx->d_data; |
353 | ctx->cword.encrypt.keygen = 1; | |
354 | ctx->cword.decrypt.keygen = 1; | |
355 | ||
1da177e4 LT |
356 | switch (key_len) { |
357 | case 16: | |
358 | t = E_KEY[3]; | |
359 | for (i = 0; i < 10; ++i) | |
360 | loop4 (i); | |
361 | break; | |
362 | ||
363 | case 24: | |
06ace7a9 HX |
364 | E_KEY[4] = le32_to_cpu(key[4]); |
365 | t = E_KEY[5] = le32_to_cpu(key[5]); | |
1da177e4 LT |
366 | for (i = 0; i < 8; ++i) |
367 | loop6 (i); | |
368 | break; | |
369 | ||
370 | case 32: | |
102d60a2 HX |
371 | E_KEY[4] = le32_to_cpu(key[4]); |
372 | E_KEY[5] = le32_to_cpu(key[5]); | |
373 | E_KEY[6] = le32_to_cpu(key[6]); | |
374 | t = E_KEY[7] = le32_to_cpu(key[7]); | |
1da177e4 LT |
375 | for (i = 0; i < 7; ++i) |
376 | loop8 (i); | |
377 | break; | |
378 | } | |
379 | ||
380 | D_KEY[0] = E_KEY[0]; | |
381 | D_KEY[1] = E_KEY[1]; | |
382 | D_KEY[2] = E_KEY[2]; | |
383 | D_KEY[3] = E_KEY[3]; | |
384 | ||
385 | for (i = 4; i < key_len + 24; ++i) { | |
386 | imix_col (D_KEY[i], E_KEY[i]); | |
387 | } | |
388 | ||
389 | /* PadLock needs a different format of the decryption key. */ | |
390 | rounds = 10 + (key_len - 16) / 4; | |
391 | ||
392 | for (i = 0; i < rounds; i++) { | |
393 | P[((i + 1) * 4) + 0] = D_KEY[((rounds - i - 1) * 4) + 0]; | |
394 | P[((i + 1) * 4) + 1] = D_KEY[((rounds - i - 1) * 4) + 1]; | |
395 | P[((i + 1) * 4) + 2] = D_KEY[((rounds - i - 1) * 4) + 2]; | |
396 | P[((i + 1) * 4) + 3] = D_KEY[((rounds - i - 1) * 4) + 3]; | |
397 | } | |
398 | ||
399 | P[0] = E_KEY[(rounds * 4) + 0]; | |
400 | P[1] = E_KEY[(rounds * 4) + 1]; | |
401 | P[2] = E_KEY[(rounds * 4) + 2]; | |
402 | P[3] = E_KEY[(rounds * 4) + 3]; | |
403 | ||
404 | memcpy(D_KEY, P, AES_EXTENDED_KEY_SIZE_B); | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | /* ====== Encryption/decryption routines ====== */ | |
410 | ||
28e8c3ad | 411 | /* These are the real call to PadLock. */ |
6789b2dc HX |
412 | static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key, |
413 | void *control_word, u32 count) | |
1da177e4 LT |
414 | { |
415 | asm volatile ("pushfl; popfl"); /* enforce key reload. */ | |
416 | asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */ | |
417 | : "+S"(input), "+D"(output) | |
418 | : "d"(control_word), "b"(key), "c"(count)); | |
419 | } | |
420 | ||
476df259 HX |
421 | static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key, |
422 | u8 *iv, void *control_word, u32 count) | |
28e8c3ad HX |
423 | { |
424 | /* Enforce key reload. */ | |
425 | asm volatile ("pushfl; popfl"); | |
426 | /* rep xcryptcbc */ | |
427 | asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" | |
428 | : "+S" (input), "+D" (output), "+a" (iv) | |
429 | : "d" (control_word), "b" (key), "c" (count)); | |
476df259 | 430 | return iv; |
28e8c3ad HX |
431 | } |
432 | ||
6c2bb98b | 433 | static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
1da177e4 | 434 | { |
6c2bb98b | 435 | struct aes_ctx *ctx = aes_ctx(tfm); |
6789b2dc | 436 | padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, 1); |
1da177e4 LT |
437 | } |
438 | ||
6c2bb98b | 439 | static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
1da177e4 | 440 | { |
6c2bb98b | 441 | struct aes_ctx *ctx = aes_ctx(tfm); |
6789b2dc | 442 | padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, 1); |
1da177e4 LT |
443 | } |
444 | ||
28e8c3ad HX |
445 | static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out, |
446 | const u8 *in, unsigned int nbytes) | |
447 | { | |
6c2bb98b | 448 | struct aes_ctx *ctx = aes_ctx(desc->tfm); |
28e8c3ad HX |
449 | padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, |
450 | nbytes / AES_BLOCK_SIZE); | |
451 | return nbytes & ~(AES_BLOCK_SIZE - 1); | |
452 | } | |
453 | ||
454 | static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out, | |
455 | const u8 *in, unsigned int nbytes) | |
456 | { | |
6c2bb98b | 457 | struct aes_ctx *ctx = aes_ctx(desc->tfm); |
28e8c3ad HX |
458 | padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, |
459 | nbytes / AES_BLOCK_SIZE); | |
460 | return nbytes & ~(AES_BLOCK_SIZE - 1); | |
461 | } | |
462 | ||
463 | static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out, | |
464 | const u8 *in, unsigned int nbytes) | |
465 | { | |
6c2bb98b | 466 | struct aes_ctx *ctx = aes_ctx(desc->tfm); |
476df259 HX |
467 | u8 *iv; |
468 | ||
469 | iv = padlock_xcrypt_cbc(in, out, ctx->E, desc->info, | |
470 | &ctx->cword.encrypt, nbytes / AES_BLOCK_SIZE); | |
471 | memcpy(desc->info, iv, AES_BLOCK_SIZE); | |
472 | ||
28e8c3ad HX |
473 | return nbytes & ~(AES_BLOCK_SIZE - 1); |
474 | } | |
475 | ||
476 | static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out, | |
477 | const u8 *in, unsigned int nbytes) | |
478 | { | |
6c2bb98b | 479 | struct aes_ctx *ctx = aes_ctx(desc->tfm); |
28e8c3ad HX |
480 | padlock_xcrypt_cbc(in, out, ctx->D, desc->info, &ctx->cword.decrypt, |
481 | nbytes / AES_BLOCK_SIZE); | |
482 | return nbytes & ~(AES_BLOCK_SIZE - 1); | |
483 | } | |
484 | ||
1da177e4 LT |
485 | static struct crypto_alg aes_alg = { |
486 | .cra_name = "aes", | |
c8a19c91 | 487 | .cra_driver_name = "aes-padlock", |
ccc17c34 | 488 | .cra_priority = PADLOCK_CRA_PRIORITY, |
1da177e4 LT |
489 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
490 | .cra_blocksize = AES_BLOCK_SIZE, | |
fbdae9f3 | 491 | .cra_ctxsize = sizeof(struct aes_ctx), |
6789b2dc | 492 | .cra_alignmask = PADLOCK_ALIGNMENT - 1, |
1da177e4 LT |
493 | .cra_module = THIS_MODULE, |
494 | .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), | |
495 | .cra_u = { | |
496 | .cipher = { | |
497 | .cia_min_keysize = AES_MIN_KEY_SIZE, | |
498 | .cia_max_keysize = AES_MAX_KEY_SIZE, | |
499 | .cia_setkey = aes_set_key, | |
500 | .cia_encrypt = aes_encrypt, | |
28e8c3ad HX |
501 | .cia_decrypt = aes_decrypt, |
502 | .cia_encrypt_ecb = aes_encrypt_ecb, | |
503 | .cia_decrypt_ecb = aes_decrypt_ecb, | |
504 | .cia_encrypt_cbc = aes_encrypt_cbc, | |
505 | .cia_decrypt_cbc = aes_decrypt_cbc, | |
1da177e4 LT |
506 | } |
507 | } | |
508 | }; | |
509 | ||
1191f0a4 | 510 | static int __init padlock_init(void) |
1da177e4 | 511 | { |
1191f0a4 ML |
512 | int ret; |
513 | ||
514 | if (!cpu_has_xcrypt) { | |
515 | printk(KERN_ERR PFX "VIA PadLock not detected.\n"); | |
516 | return -ENODEV; | |
517 | } | |
518 | ||
519 | if (!cpu_has_xcrypt_enabled) { | |
520 | printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n"); | |
521 | return -ENODEV; | |
522 | } | |
1da177e4 LT |
523 | |
524 | gen_tabs(); | |
1191f0a4 ML |
525 | if ((ret = crypto_register_alg(&aes_alg))) { |
526 | printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n"); | |
527 | return ret; | |
528 | } | |
529 | ||
530 | printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n"); | |
531 | ||
532 | return ret; | |
1da177e4 LT |
533 | } |
534 | ||
1191f0a4 | 535 | static void __exit padlock_fini(void) |
1da177e4 LT |
536 | { |
537 | crypto_unregister_alg(&aes_alg); | |
538 | } | |
1191f0a4 ML |
539 | |
540 | module_init(padlock_init); | |
541 | module_exit(padlock_fini); | |
542 | ||
543 | MODULE_DESCRIPTION("VIA PadLock AES algorithm support"); | |
544 | MODULE_LICENSE("GPL"); | |
545 | MODULE_AUTHOR("Michal Ludvig"); | |
546 | ||
547 | MODULE_ALIAS("aes-padlock"); |