[CRYPTO] api: Include sched.h for cond_resched in scatterwalk.h
[deliverable/linux.git] / drivers / crypto / padlock-aes.c
CommitLineData
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 *
f8246af0 8 * Key expansion routine taken from crypto/aes_generic.c
1da177e4
LT
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
28ce728a 46#include <crypto/algapi.h>
89e12654 47#include <crypto/aes.h>
1da177e4
LT
48#include <linux/module.h>
49#include <linux/init.h>
50#include <linux/types.h>
51#include <linux/errno.h>
1da177e4 52#include <linux/interrupt.h>
6789b2dc 53#include <linux/kernel.h>
1da177e4
LT
54#include <asm/byteorder.h>
55#include "padlock.h"
56
1da177e4
LT
57#define AES_EXTENDED_KEY_SIZE 64 /* in uint32_t units */
58#define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t))
59
ccc17c34
ML
60/* Control word. */
61struct cword {
62 unsigned int __attribute__ ((__packed__))
63 rounds:4,
64 algo:3,
65 keygen:1,
66 interm:1,
67 encdec:1,
68 ksize:2;
69} __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
70
cc08632f
ML
71/* Whenever making any changes to the following
72 * structure *make sure* you keep E, d_data
73 * and cword aligned on 16 Bytes boundaries!!! */
1da177e4 74struct aes_ctx {
6789b2dc
HX
75 struct {
76 struct cword encrypt;
77 struct cword decrypt;
78 } cword;
82062c72 79 u32 *D;
1da177e4 80 int key_length;
cc08632f
ML
81 u32 E[AES_EXTENDED_KEY_SIZE]
82 __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
83 u32 d_data[AES_EXTENDED_KEY_SIZE]
84 __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
1da177e4
LT
85};
86
87/* ====== Key management routines ====== */
88
89static inline uint32_t
90generic_rotr32 (const uint32_t x, const unsigned bits)
91{
92 const unsigned n = bits % 32;
93 return (x >> n) | (x << (32 - n));
94}
95
96static inline uint32_t
97generic_rotl32 (const uint32_t x, const unsigned bits)
98{
99 const unsigned n = bits % 32;
100 return (x << n) | (x >> (32 - n));
101}
102
103#define rotl generic_rotl32
104#define rotr generic_rotr32
105
106/*
107 * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
108 */
109static inline uint8_t
110byte(const uint32_t x, const unsigned n)
111{
112 return x >> (n << 3);
113}
114
1da177e4
LT
115#define E_KEY ctx->E
116#define D_KEY ctx->D
117
118static uint8_t pow_tab[256];
119static uint8_t log_tab[256];
120static uint8_t sbx_tab[256];
121static uint8_t isb_tab[256];
122static uint32_t rco_tab[10];
123static uint32_t ft_tab[4][256];
124static uint32_t it_tab[4][256];
125
126static uint32_t fl_tab[4][256];
127static uint32_t il_tab[4][256];
128
129static inline uint8_t
130f_mult (uint8_t a, uint8_t b)
131{
132 uint8_t aa = log_tab[a], cc = aa + log_tab[b];
133
134 return pow_tab[cc + (cc < aa ? 1 : 0)];
135}
136
137#define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
138
139#define f_rn(bo, bi, n, k) \
140 bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
141 ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
142 ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
143 ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
144
145#define i_rn(bo, bi, n, k) \
146 bo[n] = it_tab[0][byte(bi[n],0)] ^ \
147 it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
148 it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
149 it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
150
151#define ls_box(x) \
152 ( fl_tab[0][byte(x, 0)] ^ \
153 fl_tab[1][byte(x, 1)] ^ \
154 fl_tab[2][byte(x, 2)] ^ \
155 fl_tab[3][byte(x, 3)] )
156
157#define f_rl(bo, bi, n, k) \
158 bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
159 fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
160 fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
161 fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
162
163#define i_rl(bo, bi, n, k) \
164 bo[n] = il_tab[0][byte(bi[n],0)] ^ \
165 il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
166 il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
167 il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
168
169static void
170gen_tabs (void)
171{
172 uint32_t i, t;
173 uint8_t p, q;
174
175 /* log and power tables for GF(2**8) finite field with
176 0x011b as modular polynomial - the simplest prmitive
177 root is 0x03, used here to generate the tables */
178
179 for (i = 0, p = 1; i < 256; ++i) {
180 pow_tab[i] = (uint8_t) p;
181 log_tab[p] = (uint8_t) i;
182
183 p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
184 }
185
186 log_tab[1] = 0;
187
188 for (i = 0, p = 1; i < 10; ++i) {
189 rco_tab[i] = p;
190
191 p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
192 }
193
194 for (i = 0; i < 256; ++i) {
195 p = (i ? pow_tab[255 - log_tab[i]] : 0);
196 q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
197 p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
198 sbx_tab[i] = p;
199 isb_tab[p] = (uint8_t) i;
200 }
201
202 for (i = 0; i < 256; ++i) {
203 p = sbx_tab[i];
204
205 t = p;
206 fl_tab[0][i] = t;
207 fl_tab[1][i] = rotl (t, 8);
208 fl_tab[2][i] = rotl (t, 16);
209 fl_tab[3][i] = rotl (t, 24);
210
211 t = ((uint32_t) ff_mult (2, p)) |
212 ((uint32_t) p << 8) |
213 ((uint32_t) p << 16) | ((uint32_t) ff_mult (3, p) << 24);
214
215 ft_tab[0][i] = t;
216 ft_tab[1][i] = rotl (t, 8);
217 ft_tab[2][i] = rotl (t, 16);
218 ft_tab[3][i] = rotl (t, 24);
219
220 p = isb_tab[i];
221
222 t = p;
223 il_tab[0][i] = t;
224 il_tab[1][i] = rotl (t, 8);
225 il_tab[2][i] = rotl (t, 16);
226 il_tab[3][i] = rotl (t, 24);
227
228 t = ((uint32_t) ff_mult (14, p)) |
229 ((uint32_t) ff_mult (9, p) << 8) |
230 ((uint32_t) ff_mult (13, p) << 16) |
231 ((uint32_t) ff_mult (11, p) << 24);
232
233 it_tab[0][i] = t;
234 it_tab[1][i] = rotl (t, 8);
235 it_tab[2][i] = rotl (t, 16);
236 it_tab[3][i] = rotl (t, 24);
237 }
238}
239
240#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
241
242#define imix_col(y,x) \
243 u = star_x(x); \
244 v = star_x(u); \
245 w = star_x(v); \
246 t = w ^ (x); \
247 (y) = u ^ v ^ w; \
248 (y) ^= rotr(u ^ t, 8) ^ \
249 rotr(v ^ t, 16) ^ \
250 rotr(t,24)
251
252/* initialise the key schedule from the user supplied key */
253
254#define loop4(i) \
255{ t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
256 t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
257 t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
258 t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
259 t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
260}
261
262#define loop6(i) \
263{ t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
264 t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
265 t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
266 t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
267 t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
268 t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
269 t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
270}
271
272#define loop8(i) \
273{ t = rotr(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
274 t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
275 t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
276 t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
277 t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
278 t = E_KEY[8 * i + 4] ^ ls_box(t); \
279 E_KEY[8 * i + 12] = t; \
280 t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
281 t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
282 t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
283}
284
285/* Tells whether the ACE is capable to generate
286 the extended key for a given key_len. */
287static inline int
288aes_hw_extkey_available(uint8_t key_len)
289{
290 /* TODO: We should check the actual CPU model/stepping
291 as it's possible that the capability will be
292 added in the next CPU revisions. */
293 if (key_len == 16)
294 return 1;
295 return 0;
296}
297
28ce728a 298static inline struct aes_ctx *aes_ctx_common(void *ctx)
6789b2dc 299{
28ce728a 300 unsigned long addr = (unsigned long)ctx;
f10b7897
HX
301 unsigned long align = PADLOCK_ALIGNMENT;
302
303 if (align <= crypto_tfm_ctx_alignment())
304 align = 1;
6c2bb98b 305 return (struct aes_ctx *)ALIGN(addr, align);
6789b2dc
HX
306}
307
28ce728a
HX
308static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
309{
310 return aes_ctx_common(crypto_tfm_ctx(tfm));
311}
312
313static inline struct aes_ctx *blk_aes_ctx(struct crypto_blkcipher *tfm)
314{
315 return aes_ctx_common(crypto_blkcipher_ctx(tfm));
316}
317
6c2bb98b 318static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
560c06ae 319 unsigned int key_len)
1da177e4 320{
6c2bb98b 321 struct aes_ctx *ctx = aes_ctx(tfm);
06ace7a9 322 const __le32 *key = (const __le32 *)in_key;
560c06ae 323 u32 *flags = &tfm->crt_flags;
1da177e4
LT
324 uint32_t i, t, u, v, w;
325 uint32_t P[AES_EXTENDED_KEY_SIZE];
326 uint32_t rounds;
327
560c06ae 328 if (key_len % 8) {
1da177e4
LT
329 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
330 return -EINVAL;
331 }
332
333 ctx->key_length = key_len;
334
6789b2dc
HX
335 /*
336 * If the hardware is capable of generating the extended key
337 * itself we must supply the plain key for both encryption
338 * and decryption.
339 */
82062c72 340 ctx->D = ctx->E;
1da177e4 341
06ace7a9
HX
342 E_KEY[0] = le32_to_cpu(key[0]);
343 E_KEY[1] = le32_to_cpu(key[1]);
344 E_KEY[2] = le32_to_cpu(key[2]);
345 E_KEY[3] = le32_to_cpu(key[3]);
1da177e4 346
6789b2dc
HX
347 /* Prepare control words. */
348 memset(&ctx->cword, 0, sizeof(ctx->cword));
349
350 ctx->cword.decrypt.encdec = 1;
351 ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
352 ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
353 ctx->cword.encrypt.ksize = (key_len - 16) / 8;
354 ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
355
1da177e4
LT
356 /* Don't generate extended keys if the hardware can do it. */
357 if (aes_hw_extkey_available(key_len))
358 return 0;
359
6789b2dc
HX
360 ctx->D = ctx->d_data;
361 ctx->cword.encrypt.keygen = 1;
362 ctx->cword.decrypt.keygen = 1;
363
1da177e4
LT
364 switch (key_len) {
365 case 16:
366 t = E_KEY[3];
367 for (i = 0; i < 10; ++i)
368 loop4 (i);
369 break;
370
371 case 24:
06ace7a9
HX
372 E_KEY[4] = le32_to_cpu(key[4]);
373 t = E_KEY[5] = le32_to_cpu(key[5]);
1da177e4
LT
374 for (i = 0; i < 8; ++i)
375 loop6 (i);
376 break;
377
378 case 32:
102d60a2
HX
379 E_KEY[4] = le32_to_cpu(key[4]);
380 E_KEY[5] = le32_to_cpu(key[5]);
381 E_KEY[6] = le32_to_cpu(key[6]);
382 t = E_KEY[7] = le32_to_cpu(key[7]);
1da177e4
LT
383 for (i = 0; i < 7; ++i)
384 loop8 (i);
385 break;
386 }
387
388 D_KEY[0] = E_KEY[0];
389 D_KEY[1] = E_KEY[1];
390 D_KEY[2] = E_KEY[2];
391 D_KEY[3] = E_KEY[3];
392
393 for (i = 4; i < key_len + 24; ++i) {
394 imix_col (D_KEY[i], E_KEY[i]);
395 }
396
397 /* PadLock needs a different format of the decryption key. */
398 rounds = 10 + (key_len - 16) / 4;
399
400 for (i = 0; i < rounds; i++) {
401 P[((i + 1) * 4) + 0] = D_KEY[((rounds - i - 1) * 4) + 0];
402 P[((i + 1) * 4) + 1] = D_KEY[((rounds - i - 1) * 4) + 1];
403 P[((i + 1) * 4) + 2] = D_KEY[((rounds - i - 1) * 4) + 2];
404 P[((i + 1) * 4) + 3] = D_KEY[((rounds - i - 1) * 4) + 3];
405 }
406
407 P[0] = E_KEY[(rounds * 4) + 0];
408 P[1] = E_KEY[(rounds * 4) + 1];
409 P[2] = E_KEY[(rounds * 4) + 2];
410 P[3] = E_KEY[(rounds * 4) + 3];
411
412 memcpy(D_KEY, P, AES_EXTENDED_KEY_SIZE_B);
413
414 return 0;
415}
416
417/* ====== Encryption/decryption routines ====== */
418
28e8c3ad 419/* These are the real call to PadLock. */
d4a7dd8e
HX
420static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
421 void *control_word)
422{
423 asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
424 : "+S"(input), "+D"(output)
425 : "d"(control_word), "b"(key), "c"(1));
426}
427
428static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword)
429{
490fe3f0
HX
430 u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1];
431 u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
d4a7dd8e
HX
432
433 memcpy(tmp, in, AES_BLOCK_SIZE);
434 padlock_xcrypt(tmp, out, key, cword);
435}
436
437static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
438 struct cword *cword)
439{
440 asm volatile ("pushfl; popfl");
441
442 /* padlock_xcrypt requires at least two blocks of data. */
443 if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) &
444 (PAGE_SIZE - 1)))) {
445 aes_crypt_copy(in, out, key, cword);
446 return;
447 }
448
449 padlock_xcrypt(in, out, key, cword);
450}
451
6789b2dc
HX
452static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
453 void *control_word, u32 count)
1da177e4 454{
d4a7dd8e
HX
455 if (count == 1) {
456 aes_crypt(input, output, key, control_word);
457 return;
458 }
459
1da177e4 460 asm volatile ("pushfl; popfl"); /* enforce key reload. */
d4a7dd8e
HX
461 asm volatile ("test $1, %%cl;"
462 "je 1f;"
463 "lea -1(%%ecx), %%eax;"
464 "mov $1, %%ecx;"
465 ".byte 0xf3,0x0f,0xa7,0xc8;" /* rep xcryptecb */
466 "mov %%eax, %%ecx;"
467 "1:"
468 ".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
1da177e4 469 : "+S"(input), "+D"(output)
d4a7dd8e
HX
470 : "d"(control_word), "b"(key), "c"(count)
471 : "ax");
1da177e4
LT
472}
473
476df259
HX
474static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
475 u8 *iv, void *control_word, u32 count)
28e8c3ad
HX
476{
477 /* Enforce key reload. */
478 asm volatile ("pushfl; popfl");
479 /* rep xcryptcbc */
480 asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
481 : "+S" (input), "+D" (output), "+a" (iv)
482 : "d" (control_word), "b" (key), "c" (count));
476df259 483 return iv;
28e8c3ad
HX
484}
485
6c2bb98b 486static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 487{
6c2bb98b 488 struct aes_ctx *ctx = aes_ctx(tfm);
d4a7dd8e 489 aes_crypt(in, out, ctx->E, &ctx->cword.encrypt);
1da177e4
LT
490}
491
6c2bb98b 492static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 493{
6c2bb98b 494 struct aes_ctx *ctx = aes_ctx(tfm);
d4a7dd8e 495 aes_crypt(in, out, ctx->D, &ctx->cword.decrypt);
1da177e4
LT
496}
497
498static struct crypto_alg aes_alg = {
499 .cra_name = "aes",
c8a19c91 500 .cra_driver_name = "aes-padlock",
ccc17c34 501 .cra_priority = PADLOCK_CRA_PRIORITY,
1da177e4
LT
502 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
503 .cra_blocksize = AES_BLOCK_SIZE,
fbdae9f3 504 .cra_ctxsize = sizeof(struct aes_ctx),
6789b2dc 505 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
1da177e4
LT
506 .cra_module = THIS_MODULE,
507 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
508 .cra_u = {
509 .cipher = {
510 .cia_min_keysize = AES_MIN_KEY_SIZE,
511 .cia_max_keysize = AES_MAX_KEY_SIZE,
512 .cia_setkey = aes_set_key,
513 .cia_encrypt = aes_encrypt,
28e8c3ad 514 .cia_decrypt = aes_decrypt,
1da177e4
LT
515 }
516 }
517};
518
28ce728a
HX
519static int ecb_aes_encrypt(struct blkcipher_desc *desc,
520 struct scatterlist *dst, struct scatterlist *src,
521 unsigned int nbytes)
522{
523 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
524 struct blkcipher_walk walk;
525 int err;
526
527 blkcipher_walk_init(&walk, dst, src, nbytes);
528 err = blkcipher_walk_virt(desc, &walk);
529
530 while ((nbytes = walk.nbytes)) {
531 padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
532 ctx->E, &ctx->cword.encrypt,
533 nbytes / AES_BLOCK_SIZE);
534 nbytes &= AES_BLOCK_SIZE - 1;
535 err = blkcipher_walk_done(desc, &walk, nbytes);
536 }
537
538 return err;
539}
540
541static int ecb_aes_decrypt(struct blkcipher_desc *desc,
542 struct scatterlist *dst, struct scatterlist *src,
543 unsigned int nbytes)
544{
545 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
546 struct blkcipher_walk walk;
547 int err;
548
549 blkcipher_walk_init(&walk, dst, src, nbytes);
550 err = blkcipher_walk_virt(desc, &walk);
551
552 while ((nbytes = walk.nbytes)) {
553 padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
554 ctx->D, &ctx->cword.decrypt,
555 nbytes / AES_BLOCK_SIZE);
556 nbytes &= AES_BLOCK_SIZE - 1;
557 err = blkcipher_walk_done(desc, &walk, nbytes);
558 }
559
560 return err;
561}
562
563static struct crypto_alg ecb_aes_alg = {
564 .cra_name = "ecb(aes)",
565 .cra_driver_name = "ecb-aes-padlock",
566 .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
567 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
568 .cra_blocksize = AES_BLOCK_SIZE,
569 .cra_ctxsize = sizeof(struct aes_ctx),
570 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
571 .cra_type = &crypto_blkcipher_type,
572 .cra_module = THIS_MODULE,
573 .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
574 .cra_u = {
575 .blkcipher = {
576 .min_keysize = AES_MIN_KEY_SIZE,
577 .max_keysize = AES_MAX_KEY_SIZE,
578 .setkey = aes_set_key,
579 .encrypt = ecb_aes_encrypt,
580 .decrypt = ecb_aes_decrypt,
581 }
582 }
583};
584
585static int cbc_aes_encrypt(struct blkcipher_desc *desc,
586 struct scatterlist *dst, struct scatterlist *src,
587 unsigned int nbytes)
588{
589 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
590 struct blkcipher_walk walk;
591 int err;
592
593 blkcipher_walk_init(&walk, dst, src, nbytes);
594 err = blkcipher_walk_virt(desc, &walk);
595
596 while ((nbytes = walk.nbytes)) {
597 u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr,
598 walk.dst.virt.addr, ctx->E,
599 walk.iv, &ctx->cword.encrypt,
600 nbytes / AES_BLOCK_SIZE);
601 memcpy(walk.iv, iv, AES_BLOCK_SIZE);
602 nbytes &= AES_BLOCK_SIZE - 1;
603 err = blkcipher_walk_done(desc, &walk, nbytes);
604 }
605
606 return err;
607}
608
609static int cbc_aes_decrypt(struct blkcipher_desc *desc,
610 struct scatterlist *dst, struct scatterlist *src,
611 unsigned int nbytes)
612{
613 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
614 struct blkcipher_walk walk;
615 int err;
616
617 blkcipher_walk_init(&walk, dst, src, nbytes);
618 err = blkcipher_walk_virt(desc, &walk);
619
620 while ((nbytes = walk.nbytes)) {
621 padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr,
622 ctx->D, walk.iv, &ctx->cword.decrypt,
623 nbytes / AES_BLOCK_SIZE);
624 nbytes &= AES_BLOCK_SIZE - 1;
625 err = blkcipher_walk_done(desc, &walk, nbytes);
626 }
627
628 return err;
629}
630
631static struct crypto_alg cbc_aes_alg = {
632 .cra_name = "cbc(aes)",
633 .cra_driver_name = "cbc-aes-padlock",
634 .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
635 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
636 .cra_blocksize = AES_BLOCK_SIZE,
637 .cra_ctxsize = sizeof(struct aes_ctx),
638 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
639 .cra_type = &crypto_blkcipher_type,
640 .cra_module = THIS_MODULE,
641 .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
642 .cra_u = {
643 .blkcipher = {
644 .min_keysize = AES_MIN_KEY_SIZE,
645 .max_keysize = AES_MAX_KEY_SIZE,
646 .ivsize = AES_BLOCK_SIZE,
647 .setkey = aes_set_key,
648 .encrypt = cbc_aes_encrypt,
649 .decrypt = cbc_aes_decrypt,
650 }
651 }
652};
653
1191f0a4 654static int __init padlock_init(void)
1da177e4 655{
1191f0a4
ML
656 int ret;
657
658 if (!cpu_has_xcrypt) {
659 printk(KERN_ERR PFX "VIA PadLock not detected.\n");
660 return -ENODEV;
661 }
662
663 if (!cpu_has_xcrypt_enabled) {
664 printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
665 return -ENODEV;
666 }
1da177e4
LT
667
668 gen_tabs();
28ce728a
HX
669 if ((ret = crypto_register_alg(&aes_alg)))
670 goto aes_err;
671
672 if ((ret = crypto_register_alg(&ecb_aes_alg)))
673 goto ecb_aes_err;
674
675 if ((ret = crypto_register_alg(&cbc_aes_alg)))
676 goto cbc_aes_err;
1191f0a4
ML
677
678 printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
679
28ce728a 680out:
1191f0a4 681 return ret;
28ce728a
HX
682
683cbc_aes_err:
684 crypto_unregister_alg(&ecb_aes_alg);
685ecb_aes_err:
686 crypto_unregister_alg(&aes_alg);
687aes_err:
688 printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
689 goto out;
1da177e4
LT
690}
691
1191f0a4 692static void __exit padlock_fini(void)
1da177e4 693{
28ce728a
HX
694 crypto_unregister_alg(&cbc_aes_alg);
695 crypto_unregister_alg(&ecb_aes_alg);
1da177e4
LT
696 crypto_unregister_alg(&aes_alg);
697}
1191f0a4
ML
698
699module_init(padlock_init);
700module_exit(padlock_fini);
701
702MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
703MODULE_LICENSE("GPL");
704MODULE_AUTHOR("Michal Ludvig");
705
f8246af0 706MODULE_ALIAS("aes");
This page took 0.316866 seconds and 5 git commands to generate.