blob: 87f92c39b5f001fc0fd18416026c05b2e708621b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * Support for VIA PadLock hardware crypto engine.
5 *
6 * Copyright (c) 2004 Michal Ludvig <michal@logix.cz>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Herbert Xu28ce7282006-08-21 21:38:42 +100010#include <crypto/algapi.h>
Sebastian Siewior89e12652007-10-17 23:18:57 +080011#include <crypto/aes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/types.h>
15#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/interrupt.h>
Herbert Xu6789b2d2005-07-06 13:52:27 -070017#include <linux/kernel.h>
Herbert Xu420a4b22008-08-31 15:58:45 +100018#include <linux/percpu.h>
19#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/byteorder.h>
Suresh Siddhae4914012008-08-13 22:02:26 +100021#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "padlock.h"
23
Michal Ludvigccc17c32006-07-15 10:23:49 +100024/* Control word. */
25struct cword {
26 unsigned int __attribute__ ((__packed__))
27 rounds:4,
28 algo:3,
29 keygen:1,
30 interm:1,
31 encdec:1,
32 ksize:2;
33} __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
34
Michal Ludvigcc086322006-07-15 11:08:50 +100035/* Whenever making any changes to the following
36 * structure *make sure* you keep E, d_data
Sebastian Siewior7dc748e2008-04-01 21:24:50 +080037 * and cword aligned on 16 Bytes boundaries and
38 * the Hardware can access 16 * 16 bytes of E and d_data
39 * (only the first 15 * 16 bytes matter but the HW reads
40 * more).
41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042struct aes_ctx {
Sebastian Siewior7dc748e2008-04-01 21:24:50 +080043 u32 E[AES_MAX_KEYLENGTH_U32]
44 __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
45 u32 d_data[AES_MAX_KEYLENGTH_U32]
46 __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
Herbert Xu6789b2d2005-07-06 13:52:27 -070047 struct {
48 struct cword encrypt;
49 struct cword decrypt;
50 } cword;
Herbert Xu82062c72006-05-16 22:20:34 +100051 u32 *D;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
Herbert Xu420a4b22008-08-31 15:58:45 +100054static DEFINE_PER_CPU(struct cword *, last_cword);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* Tells whether the ACE is capable to generate
57 the extended key for a given key_len. */
58static inline int
59aes_hw_extkey_available(uint8_t key_len)
60{
61 /* TODO: We should check the actual CPU model/stepping
62 as it's possible that the capability will be
63 added in the next CPU revisions. */
64 if (key_len == 16)
65 return 1;
66 return 0;
67}
68
Herbert Xu28ce7282006-08-21 21:38:42 +100069static inline struct aes_ctx *aes_ctx_common(void *ctx)
Herbert Xu6789b2d2005-07-06 13:52:27 -070070{
Herbert Xu28ce7282006-08-21 21:38:42 +100071 unsigned long addr = (unsigned long)ctx;
Herbert Xuf10b7892006-01-25 22:34:01 +110072 unsigned long align = PADLOCK_ALIGNMENT;
73
74 if (align <= crypto_tfm_ctx_alignment())
75 align = 1;
Herbert Xu6c2bb982006-05-16 22:09:29 +100076 return (struct aes_ctx *)ALIGN(addr, align);
Herbert Xu6789b2d2005-07-06 13:52:27 -070077}
78
Herbert Xu28ce7282006-08-21 21:38:42 +100079static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
80{
81 return aes_ctx_common(crypto_tfm_ctx(tfm));
82}
83
84static inline struct aes_ctx *blk_aes_ctx(struct crypto_blkcipher *tfm)
85{
86 return aes_ctx_common(crypto_blkcipher_ctx(tfm));
87}
88
Herbert Xu6c2bb982006-05-16 22:09:29 +100089static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100090 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Herbert Xu6c2bb982006-05-16 22:09:29 +100092 struct aes_ctx *ctx = aes_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110093 const __le32 *key = (const __le32 *)in_key;
Herbert Xu560c06a2006-08-13 14:16:39 +100094 u32 *flags = &tfm->crt_flags;
Sebastian Siewior7dc748e2008-04-01 21:24:50 +080095 struct crypto_aes_ctx gen_aes;
Herbert Xu420a4b22008-08-31 15:58:45 +100096 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Herbert Xu560c06a2006-08-13 14:16:39 +100098 if (key_len % 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
100 return -EINVAL;
101 }
102
Herbert Xu6789b2d2005-07-06 13:52:27 -0700103 /*
104 * If the hardware is capable of generating the extended key
105 * itself we must supply the plain key for both encryption
106 * and decryption.
107 */
Herbert Xu82062c72006-05-16 22:20:34 +1000108 ctx->D = ctx->E;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Sebastian Siewior7dc748e2008-04-01 21:24:50 +0800110 ctx->E[0] = le32_to_cpu(key[0]);
111 ctx->E[1] = le32_to_cpu(key[1]);
112 ctx->E[2] = le32_to_cpu(key[2]);
113 ctx->E[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Herbert Xu6789b2d2005-07-06 13:52:27 -0700115 /* Prepare control words. */
116 memset(&ctx->cword, 0, sizeof(ctx->cword));
117
118 ctx->cword.decrypt.encdec = 1;
119 ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
120 ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
121 ctx->cword.encrypt.ksize = (key_len - 16) / 8;
122 ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Don't generate extended keys if the hardware can do it. */
125 if (aes_hw_extkey_available(key_len))
Herbert Xu420a4b22008-08-31 15:58:45 +1000126 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Herbert Xu6789b2d2005-07-06 13:52:27 -0700128 ctx->D = ctx->d_data;
129 ctx->cword.encrypt.keygen = 1;
130 ctx->cword.decrypt.keygen = 1;
131
Sebastian Siewior7dc748e2008-04-01 21:24:50 +0800132 if (crypto_aes_expand_key(&gen_aes, in_key, key_len)) {
133 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
134 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
Sebastian Siewior7dc748e2008-04-01 21:24:50 +0800137 memcpy(ctx->E, gen_aes.key_enc, AES_MAX_KEYLENGTH);
138 memcpy(ctx->D, gen_aes.key_dec, AES_MAX_KEYLENGTH);
Herbert Xu420a4b22008-08-31 15:58:45 +1000139
140ok:
141 for_each_online_cpu(cpu)
142 if (&ctx->cword.encrypt == per_cpu(last_cword, cpu) ||
143 &ctx->cword.decrypt == per_cpu(last_cword, cpu))
144 per_cpu(last_cword, cpu) = NULL;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return 0;
147}
148
149/* ====== Encryption/decryption routines ====== */
150
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700151/* These are the real call to PadLock. */
Herbert Xu420a4b22008-08-31 15:58:45 +1000152static inline void padlock_reset_key(struct cword *cword)
Herbert Xu866cd902007-12-27 00:04:44 +1100153{
Herbert Xu420a4b22008-08-31 15:58:45 +1000154 int cpu = raw_smp_processor_id();
155
156 if (cword != per_cpu(last_cword, cpu))
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800157#ifndef CONFIG_X86_64
Herbert Xu420a4b22008-08-31 15:58:45 +1000158 asm volatile ("pushfl; popfl");
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800159#else
160 asm volatile ("pushfq; popfq");
161#endif
Herbert Xu420a4b22008-08-31 15:58:45 +1000162}
163
164static inline void padlock_store_cword(struct cword *cword)
165{
166 per_cpu(last_cword, raw_smp_processor_id()) = cword;
Herbert Xu866cd902007-12-27 00:04:44 +1100167}
168
Suresh Siddhae4914012008-08-13 22:02:26 +1000169/*
170 * While the padlock instructions don't use FP/SSE registers, they
171 * generate a spurious DNA fault when cr0.ts is '1'. These instructions
172 * should be used only inside the irq_ts_save/restore() context
173 */
174
Herbert Xud4a7dd82007-12-28 11:05:46 +1100175static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
Herbert Xu420a4b22008-08-31 15:58:45 +1000176 struct cword *control_word)
Herbert Xud4a7dd82007-12-28 11:05:46 +1100177{
178 asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
179 : "+S"(input), "+D"(output)
180 : "d"(control_word), "b"(key), "c"(1));
181}
182
183static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword)
184{
Herbert Xu490fe3f2008-01-11 08:09:35 +1100185 u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1];
186 u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
Herbert Xud4a7dd82007-12-28 11:05:46 +1100187
188 memcpy(tmp, in, AES_BLOCK_SIZE);
189 padlock_xcrypt(tmp, out, key, cword);
190}
191
192static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
193 struct cword *cword)
194{
Herbert Xud4a7dd82007-12-28 11:05:46 +1100195 /* padlock_xcrypt requires at least two blocks of data. */
196 if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) &
197 (PAGE_SIZE - 1)))) {
198 aes_crypt_copy(in, out, key, cword);
199 return;
200 }
201
202 padlock_xcrypt(in, out, key, cword);
203}
204
Herbert Xu6789b2d2005-07-06 13:52:27 -0700205static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
206 void *control_word, u32 count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Herbert Xud4a7dd82007-12-28 11:05:46 +1100208 if (count == 1) {
209 aes_crypt(input, output, key, control_word);
210 return;
211 }
212
Herbert Xud4a7dd82007-12-28 11:05:46 +1100213 asm volatile ("test $1, %%cl;"
214 "je 1f;"
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800215#ifndef CONFIG_X86_64
Herbert Xud4a7dd82007-12-28 11:05:46 +1100216 "lea -1(%%ecx), %%eax;"
217 "mov $1, %%ecx;"
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800218#else
219 "lea -1(%%rcx), %%rax;"
220 "mov $1, %%rcx;"
221#endif
Herbert Xud4a7dd82007-12-28 11:05:46 +1100222 ".byte 0xf3,0x0f,0xa7,0xc8;" /* rep xcryptecb */
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800223#ifndef CONFIG_X86_64
Herbert Xud4a7dd82007-12-28 11:05:46 +1100224 "mov %%eax, %%ecx;"
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800225#else
226 "mov %%rax, %%rcx;"
227#endif
Herbert Xud4a7dd82007-12-28 11:05:46 +1100228 "1:"
229 ".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 : "+S"(input), "+D"(output)
Herbert Xud4a7dd82007-12-28 11:05:46 +1100231 : "d"(control_word), "b"(key), "c"(count)
232 : "ax");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Herbert Xu476df252005-07-06 13:54:09 -0700235static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
236 u8 *iv, void *control_word, u32 count)
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700237{
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700238 /* rep xcryptcbc */
239 asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
240 : "+S" (input), "+D" (output), "+a" (iv)
241 : "d" (control_word), "b" (key), "c" (count));
Herbert Xu476df252005-07-06 13:54:09 -0700242 return iv;
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700243}
244
Herbert Xu6c2bb982006-05-16 22:09:29 +1000245static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000247 struct aes_ctx *ctx = aes_ctx(tfm);
Suresh Siddhae4914012008-08-13 22:02:26 +1000248 int ts_state;
Suresh Siddhae4914012008-08-13 22:02:26 +1000249
Herbert Xu420a4b22008-08-31 15:58:45 +1000250 padlock_reset_key(&ctx->cword.encrypt);
Suresh Siddhae4914012008-08-13 22:02:26 +1000251 ts_state = irq_ts_save();
Herbert Xud4a7dd82007-12-28 11:05:46 +1100252 aes_crypt(in, out, ctx->E, &ctx->cword.encrypt);
Suresh Siddhae4914012008-08-13 22:02:26 +1000253 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000254 padlock_store_cword(&ctx->cword.encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Herbert Xu6c2bb982006-05-16 22:09:29 +1000257static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000259 struct aes_ctx *ctx = aes_ctx(tfm);
Suresh Siddhae4914012008-08-13 22:02:26 +1000260 int ts_state;
Suresh Siddhae4914012008-08-13 22:02:26 +1000261
Herbert Xu420a4b22008-08-31 15:58:45 +1000262 padlock_reset_key(&ctx->cword.encrypt);
Suresh Siddhae4914012008-08-13 22:02:26 +1000263 ts_state = irq_ts_save();
Herbert Xud4a7dd82007-12-28 11:05:46 +1100264 aes_crypt(in, out, ctx->D, &ctx->cword.decrypt);
Suresh Siddhae4914012008-08-13 22:02:26 +1000265 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000266 padlock_store_cword(&ctx->cword.encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static struct crypto_alg aes_alg = {
270 .cra_name = "aes",
Herbert Xuc8a19c92005-11-05 18:06:26 +1100271 .cra_driver_name = "aes-padlock",
Michal Ludvigccc17c32006-07-15 10:23:49 +1000272 .cra_priority = PADLOCK_CRA_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
274 .cra_blocksize = AES_BLOCK_SIZE,
Herbert Xufbdae9f2005-07-06 13:53:29 -0700275 .cra_ctxsize = sizeof(struct aes_ctx),
Herbert Xu6789b2d2005-07-06 13:52:27 -0700276 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .cra_module = THIS_MODULE,
278 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
279 .cra_u = {
280 .cipher = {
281 .cia_min_keysize = AES_MIN_KEY_SIZE,
282 .cia_max_keysize = AES_MAX_KEY_SIZE,
283 .cia_setkey = aes_set_key,
284 .cia_encrypt = aes_encrypt,
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700285 .cia_decrypt = aes_decrypt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 }
288};
289
Herbert Xu28ce7282006-08-21 21:38:42 +1000290static int ecb_aes_encrypt(struct blkcipher_desc *desc,
291 struct scatterlist *dst, struct scatterlist *src,
292 unsigned int nbytes)
293{
294 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
295 struct blkcipher_walk walk;
296 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000297 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000298
Herbert Xu420a4b22008-08-31 15:58:45 +1000299 padlock_reset_key(&ctx->cword.encrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100300
Herbert Xu28ce7282006-08-21 21:38:42 +1000301 blkcipher_walk_init(&walk, dst, src, nbytes);
302 err = blkcipher_walk_virt(desc, &walk);
303
Suresh Siddhae4914012008-08-13 22:02:26 +1000304 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000305 while ((nbytes = walk.nbytes)) {
306 padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
307 ctx->E, &ctx->cword.encrypt,
308 nbytes / AES_BLOCK_SIZE);
309 nbytes &= AES_BLOCK_SIZE - 1;
310 err = blkcipher_walk_done(desc, &walk, nbytes);
311 }
Suresh Siddhae4914012008-08-13 22:02:26 +1000312 irq_ts_restore(ts_state);
Herbert Xu28ce7282006-08-21 21:38:42 +1000313
Herbert Xu420a4b22008-08-31 15:58:45 +1000314 padlock_store_cword(&ctx->cword.encrypt);
315
Herbert Xu28ce7282006-08-21 21:38:42 +1000316 return err;
317}
318
319static int ecb_aes_decrypt(struct blkcipher_desc *desc,
320 struct scatterlist *dst, struct scatterlist *src,
321 unsigned int nbytes)
322{
323 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
324 struct blkcipher_walk walk;
325 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000326 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000327
Herbert Xu420a4b22008-08-31 15:58:45 +1000328 padlock_reset_key(&ctx->cword.decrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100329
Herbert Xu28ce7282006-08-21 21:38:42 +1000330 blkcipher_walk_init(&walk, dst, src, nbytes);
331 err = blkcipher_walk_virt(desc, &walk);
332
Suresh Siddhae4914012008-08-13 22:02:26 +1000333 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000334 while ((nbytes = walk.nbytes)) {
335 padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
336 ctx->D, &ctx->cword.decrypt,
337 nbytes / AES_BLOCK_SIZE);
338 nbytes &= AES_BLOCK_SIZE - 1;
339 err = blkcipher_walk_done(desc, &walk, nbytes);
340 }
Suresh Siddhae4914012008-08-13 22:02:26 +1000341 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000342
343 padlock_store_cword(&ctx->cword.encrypt);
344
Herbert Xu28ce7282006-08-21 21:38:42 +1000345 return err;
346}
347
348static struct crypto_alg ecb_aes_alg = {
349 .cra_name = "ecb(aes)",
350 .cra_driver_name = "ecb-aes-padlock",
351 .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
352 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
353 .cra_blocksize = AES_BLOCK_SIZE,
354 .cra_ctxsize = sizeof(struct aes_ctx),
355 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
356 .cra_type = &crypto_blkcipher_type,
357 .cra_module = THIS_MODULE,
358 .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
359 .cra_u = {
360 .blkcipher = {
361 .min_keysize = AES_MIN_KEY_SIZE,
362 .max_keysize = AES_MAX_KEY_SIZE,
363 .setkey = aes_set_key,
364 .encrypt = ecb_aes_encrypt,
365 .decrypt = ecb_aes_decrypt,
366 }
367 }
368};
369
370static int cbc_aes_encrypt(struct blkcipher_desc *desc,
371 struct scatterlist *dst, struct scatterlist *src,
372 unsigned int nbytes)
373{
374 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
375 struct blkcipher_walk walk;
376 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000377 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000378
Herbert Xu420a4b22008-08-31 15:58:45 +1000379 padlock_reset_key(&ctx->cword.encrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100380
Herbert Xu28ce7282006-08-21 21:38:42 +1000381 blkcipher_walk_init(&walk, dst, src, nbytes);
382 err = blkcipher_walk_virt(desc, &walk);
383
Suresh Siddhae4914012008-08-13 22:02:26 +1000384 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000385 while ((nbytes = walk.nbytes)) {
386 u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr,
387 walk.dst.virt.addr, ctx->E,
388 walk.iv, &ctx->cword.encrypt,
389 nbytes / AES_BLOCK_SIZE);
390 memcpy(walk.iv, iv, AES_BLOCK_SIZE);
391 nbytes &= AES_BLOCK_SIZE - 1;
392 err = blkcipher_walk_done(desc, &walk, nbytes);
393 }
Suresh Siddhae4914012008-08-13 22:02:26 +1000394 irq_ts_restore(ts_state);
Herbert Xu28ce7282006-08-21 21:38:42 +1000395
Herbert Xu420a4b22008-08-31 15:58:45 +1000396 padlock_store_cword(&ctx->cword.decrypt);
397
Herbert Xu28ce7282006-08-21 21:38:42 +1000398 return err;
399}
400
401static int cbc_aes_decrypt(struct blkcipher_desc *desc,
402 struct scatterlist *dst, struct scatterlist *src,
403 unsigned int nbytes)
404{
405 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
406 struct blkcipher_walk walk;
407 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000408 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000409
Herbert Xu420a4b22008-08-31 15:58:45 +1000410 padlock_reset_key(&ctx->cword.encrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100411
Herbert Xu28ce7282006-08-21 21:38:42 +1000412 blkcipher_walk_init(&walk, dst, src, nbytes);
413 err = blkcipher_walk_virt(desc, &walk);
414
Suresh Siddhae4914012008-08-13 22:02:26 +1000415 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000416 while ((nbytes = walk.nbytes)) {
417 padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr,
418 ctx->D, walk.iv, &ctx->cword.decrypt,
419 nbytes / AES_BLOCK_SIZE);
420 nbytes &= AES_BLOCK_SIZE - 1;
421 err = blkcipher_walk_done(desc, &walk, nbytes);
422 }
423
Suresh Siddhae4914012008-08-13 22:02:26 +1000424 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000425
426 padlock_store_cword(&ctx->cword.encrypt);
427
Herbert Xu28ce7282006-08-21 21:38:42 +1000428 return err;
429}
430
431static struct crypto_alg cbc_aes_alg = {
432 .cra_name = "cbc(aes)",
433 .cra_driver_name = "cbc-aes-padlock",
434 .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
435 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
436 .cra_blocksize = AES_BLOCK_SIZE,
437 .cra_ctxsize = sizeof(struct aes_ctx),
438 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
439 .cra_type = &crypto_blkcipher_type,
440 .cra_module = THIS_MODULE,
441 .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
442 .cra_u = {
443 .blkcipher = {
444 .min_keysize = AES_MIN_KEY_SIZE,
445 .max_keysize = AES_MAX_KEY_SIZE,
446 .ivsize = AES_BLOCK_SIZE,
447 .setkey = aes_set_key,
448 .encrypt = cbc_aes_encrypt,
449 .decrypt = cbc_aes_decrypt,
450 }
451 }
452};
453
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000454static int __init padlock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000456 int ret;
457
458 if (!cpu_has_xcrypt) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800459 printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000460 return -ENODEV;
461 }
462
463 if (!cpu_has_xcrypt_enabled) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800464 printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000465 return -ENODEV;
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Herbert Xu28ce7282006-08-21 21:38:42 +1000468 if ((ret = crypto_register_alg(&aes_alg)))
469 goto aes_err;
470
471 if ((ret = crypto_register_alg(&ecb_aes_alg)))
472 goto ecb_aes_err;
473
474 if ((ret = crypto_register_alg(&cbc_aes_alg)))
475 goto cbc_aes_err;
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000476
477 printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
478
Herbert Xu28ce7282006-08-21 21:38:42 +1000479out:
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000480 return ret;
Herbert Xu28ce7282006-08-21 21:38:42 +1000481
482cbc_aes_err:
483 crypto_unregister_alg(&ecb_aes_alg);
484ecb_aes_err:
485 crypto_unregister_alg(&aes_alg);
486aes_err:
487 printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000491static void __exit padlock_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Herbert Xu28ce7282006-08-21 21:38:42 +1000493 crypto_unregister_alg(&cbc_aes_alg);
494 crypto_unregister_alg(&ecb_aes_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 crypto_unregister_alg(&aes_alg);
496}
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000497
498module_init(padlock_init);
499module_exit(padlock_fini);
500
501MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
502MODULE_LICENSE("GPL");
503MODULE_AUTHOR("Michal Ludvig");
504
Herbert Xuacd246b2009-04-21 13:55:20 +0800505MODULE_ALIAS("aes");