blob: e1d8776c69729c0ccd917f908ccf4b0d0a319820 [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>
Chuck Ebberta76c1c22009-06-18 19:24:10 +080021#include <asm/processor.h>
Suresh Siddhae4914012008-08-13 22:02:26 +100022#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "padlock.h"
24
Chuck Ebberta76c1c22009-06-18 19:24:10 +080025/* number of data blocks actually fetched for each xcrypt insn */
26static unsigned int ecb_fetch_blocks = 2;
27static unsigned int cbc_fetch_blocks = 1;
28
29#define ecb_fetch_bytes (ecb_fetch_blocks * AES_BLOCK_SIZE)
30#define cbc_fetch_bytes (cbc_fetch_blocks * AES_BLOCK_SIZE)
31
Michal Ludvigccc17c32006-07-15 10:23:49 +100032/* Control word. */
33struct cword {
34 unsigned int __attribute__ ((__packed__))
35 rounds:4,
36 algo:3,
37 keygen:1,
38 interm:1,
39 encdec:1,
40 ksize:2;
41} __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
42
Michal Ludvigcc086322006-07-15 11:08:50 +100043/* Whenever making any changes to the following
44 * structure *make sure* you keep E, d_data
Sebastian Siewior7dc748e2008-04-01 21:24:50 +080045 * and cword aligned on 16 Bytes boundaries and
46 * the Hardware can access 16 * 16 bytes of E and d_data
47 * (only the first 15 * 16 bytes matter but the HW reads
48 * more).
49 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct aes_ctx {
Sebastian Siewior7dc748e2008-04-01 21:24:50 +080051 u32 E[AES_MAX_KEYLENGTH_U32]
52 __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
53 u32 d_data[AES_MAX_KEYLENGTH_U32]
54 __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
Herbert Xu6789b2d2005-07-06 13:52:27 -070055 struct {
56 struct cword encrypt;
57 struct cword decrypt;
58 } cword;
Herbert Xu82062c72006-05-16 22:20:34 +100059 u32 *D;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Herbert Xu420a4b22008-08-31 15:58:45 +100062static DEFINE_PER_CPU(struct cword *, last_cword);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Tells whether the ACE is capable to generate
65 the extended key for a given key_len. */
66static inline int
67aes_hw_extkey_available(uint8_t key_len)
68{
69 /* TODO: We should check the actual CPU model/stepping
70 as it's possible that the capability will be
71 added in the next CPU revisions. */
72 if (key_len == 16)
73 return 1;
74 return 0;
75}
76
Herbert Xu28ce7282006-08-21 21:38:42 +100077static inline struct aes_ctx *aes_ctx_common(void *ctx)
Herbert Xu6789b2d2005-07-06 13:52:27 -070078{
Herbert Xu28ce7282006-08-21 21:38:42 +100079 unsigned long addr = (unsigned long)ctx;
Herbert Xuf10b7892006-01-25 22:34:01 +110080 unsigned long align = PADLOCK_ALIGNMENT;
81
82 if (align <= crypto_tfm_ctx_alignment())
83 align = 1;
Herbert Xu6c2bb982006-05-16 22:09:29 +100084 return (struct aes_ctx *)ALIGN(addr, align);
Herbert Xu6789b2d2005-07-06 13:52:27 -070085}
86
Herbert Xu28ce7282006-08-21 21:38:42 +100087static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
88{
89 return aes_ctx_common(crypto_tfm_ctx(tfm));
90}
91
92static inline struct aes_ctx *blk_aes_ctx(struct crypto_blkcipher *tfm)
93{
94 return aes_ctx_common(crypto_blkcipher_ctx(tfm));
95}
96
Herbert Xu6c2bb982006-05-16 22:09:29 +100097static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100098 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000100 struct aes_ctx *ctx = aes_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100101 const __le32 *key = (const __le32 *)in_key;
Herbert Xu560c06a2006-08-13 14:16:39 +1000102 u32 *flags = &tfm->crt_flags;
Sebastian Siewior7dc748e2008-04-01 21:24:50 +0800103 struct crypto_aes_ctx gen_aes;
Herbert Xu420a4b22008-08-31 15:58:45 +1000104 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Herbert Xu560c06a2006-08-13 14:16:39 +1000106 if (key_len % 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
108 return -EINVAL;
109 }
110
Herbert Xu6789b2d2005-07-06 13:52:27 -0700111 /*
112 * If the hardware is capable of generating the extended key
113 * itself we must supply the plain key for both encryption
114 * and decryption.
115 */
Herbert Xu82062c72006-05-16 22:20:34 +1000116 ctx->D = ctx->E;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Sebastian Siewior7dc748e2008-04-01 21:24:50 +0800118 ctx->E[0] = le32_to_cpu(key[0]);
119 ctx->E[1] = le32_to_cpu(key[1]);
120 ctx->E[2] = le32_to_cpu(key[2]);
121 ctx->E[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Herbert Xu6789b2d2005-07-06 13:52:27 -0700123 /* Prepare control words. */
124 memset(&ctx->cword, 0, sizeof(ctx->cword));
125
126 ctx->cword.decrypt.encdec = 1;
127 ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
128 ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
129 ctx->cword.encrypt.ksize = (key_len - 16) / 8;
130 ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /* Don't generate extended keys if the hardware can do it. */
133 if (aes_hw_extkey_available(key_len))
Herbert Xu420a4b22008-08-31 15:58:45 +1000134 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Herbert Xu6789b2d2005-07-06 13:52:27 -0700136 ctx->D = ctx->d_data;
137 ctx->cword.encrypt.keygen = 1;
138 ctx->cword.decrypt.keygen = 1;
139
Sebastian Siewior7dc748e2008-04-01 21:24:50 +0800140 if (crypto_aes_expand_key(&gen_aes, in_key, key_len)) {
141 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Sebastian Siewior7dc748e2008-04-01 21:24:50 +0800145 memcpy(ctx->E, gen_aes.key_enc, AES_MAX_KEYLENGTH);
146 memcpy(ctx->D, gen_aes.key_dec, AES_MAX_KEYLENGTH);
Herbert Xu420a4b22008-08-31 15:58:45 +1000147
148ok:
149 for_each_online_cpu(cpu)
150 if (&ctx->cword.encrypt == per_cpu(last_cword, cpu) ||
151 &ctx->cword.decrypt == per_cpu(last_cword, cpu))
152 per_cpu(last_cword, cpu) = NULL;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0;
155}
156
157/* ====== Encryption/decryption routines ====== */
158
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700159/* These are the real call to PadLock. */
Herbert Xu420a4b22008-08-31 15:58:45 +1000160static inline void padlock_reset_key(struct cword *cword)
Herbert Xu866cd902007-12-27 00:04:44 +1100161{
Herbert Xu420a4b22008-08-31 15:58:45 +1000162 int cpu = raw_smp_processor_id();
163
164 if (cword != per_cpu(last_cword, cpu))
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800165#ifndef CONFIG_X86_64
Herbert Xu420a4b22008-08-31 15:58:45 +1000166 asm volatile ("pushfl; popfl");
Sebastian Andrzej Siewiord1c8b0a2009-04-21 14:14:37 +0800167#else
168 asm volatile ("pushfq; popfq");
169#endif
Herbert Xu420a4b22008-08-31 15:58:45 +1000170}
171
172static inline void padlock_store_cword(struct cword *cword)
173{
174 per_cpu(last_cword, raw_smp_processor_id()) = cword;
Herbert Xu866cd902007-12-27 00:04:44 +1100175}
176
Suresh Siddhae4914012008-08-13 22:02:26 +1000177/*
178 * While the padlock instructions don't use FP/SSE registers, they
179 * generate a spurious DNA fault when cr0.ts is '1'. These instructions
180 * should be used only inside the irq_ts_save/restore() context
181 */
182
Herbert Xud4a7dd82007-12-28 11:05:46 +1100183static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800184 struct cword *control_word, int count)
Herbert Xud4a7dd82007-12-28 11:05:46 +1100185{
186 asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
187 : "+S"(input), "+D"(output)
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800188 : "d"(control_word), "b"(key), "c"(count));
Herbert Xud4a7dd82007-12-28 11:05:46 +1100189}
190
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800191static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key,
192 struct cword *cword, int count)
Herbert Xud4a7dd82007-12-28 11:05:46 +1100193{
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800194 /*
195 * Padlock prefetches extra data so we must provide mapped input buffers.
196 * Assume there are at least 16 bytes of stack already in use.
197 */
198 u8 buf[AES_BLOCK_SIZE * 7 + PADLOCK_ALIGNMENT - 1];
Herbert Xu490fe3f2008-01-11 08:09:35 +1100199 u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
Herbert Xud4a7dd82007-12-28 11:05:46 +1100200
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800201 memcpy(tmp, in, count * AES_BLOCK_SIZE);
202 padlock_xcrypt(tmp, out, key, cword, count);
Herbert Xud4a7dd82007-12-28 11:05:46 +1100203}
204
205static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800206 struct cword *cword, int count)
Herbert Xud4a7dd82007-12-28 11:05:46 +1100207{
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800208 /* Padlock in ECB mode fetches at least ecb_fetch_bytes of data.
209 * We could avoid some copying here but it's probably not worth it.
210 */
211 if (unlikely(((unsigned long)in & PAGE_SIZE) + ecb_fetch_bytes > PAGE_SIZE)) {
212 aes_crypt_copy(in, out, key, cword, count);
Herbert Xud4a7dd82007-12-28 11:05:46 +1100213 return;
214 }
215
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800216 padlock_xcrypt(in, out, key, cword, count);
Herbert Xud4a7dd82007-12-28 11:05:46 +1100217}
218
Herbert Xu6789b2d2005-07-06 13:52:27 -0700219static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
220 void *control_word, u32 count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800222 u32 initial = count & (ecb_fetch_blocks - 1);
223
224 if (count < ecb_fetch_blocks) {
225 aes_crypt(input, output, key, control_word, count);
Herbert Xud4a7dd82007-12-28 11:05:46 +1100226 return;
227 }
228
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800229 if (initial)
230 asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
231 : "+S"(input), "+D"(output)
232 : "d"(control_word), "b"(key), "c"(initial));
233
234 asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 : "+S"(input), "+D"(output)
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800236 : "d"(control_word), "b"(key), "c"(count - initial));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Herbert Xu476df252005-07-06 13:54:09 -0700239static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
240 u8 *iv, void *control_word, u32 count)
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700241{
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700242 /* rep xcryptcbc */
243 asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
244 : "+S" (input), "+D" (output), "+a" (iv)
245 : "d" (control_word), "b" (key), "c" (count));
Herbert Xu476df252005-07-06 13:54:09 -0700246 return iv;
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700247}
248
Herbert Xu6c2bb982006-05-16 22:09:29 +1000249static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000251 struct aes_ctx *ctx = aes_ctx(tfm);
Suresh Siddhae4914012008-08-13 22:02:26 +1000252 int ts_state;
Suresh Siddhae4914012008-08-13 22:02:26 +1000253
Herbert Xu420a4b22008-08-31 15:58:45 +1000254 padlock_reset_key(&ctx->cword.encrypt);
Suresh Siddhae4914012008-08-13 22:02:26 +1000255 ts_state = irq_ts_save();
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800256 aes_crypt(in, out, ctx->E, &ctx->cword.encrypt, 1);
Suresh Siddhae4914012008-08-13 22:02:26 +1000257 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000258 padlock_store_cword(&ctx->cword.encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Herbert Xu6c2bb982006-05-16 22:09:29 +1000261static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000263 struct aes_ctx *ctx = aes_ctx(tfm);
Suresh Siddhae4914012008-08-13 22:02:26 +1000264 int ts_state;
Suresh Siddhae4914012008-08-13 22:02:26 +1000265
Herbert Xu420a4b22008-08-31 15:58:45 +1000266 padlock_reset_key(&ctx->cword.encrypt);
Suresh Siddhae4914012008-08-13 22:02:26 +1000267 ts_state = irq_ts_save();
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800268 aes_crypt(in, out, ctx->D, &ctx->cword.decrypt, 1);
Suresh Siddhae4914012008-08-13 22:02:26 +1000269 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000270 padlock_store_cword(&ctx->cword.encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273static struct crypto_alg aes_alg = {
274 .cra_name = "aes",
Herbert Xuc8a19c92005-11-05 18:06:26 +1100275 .cra_driver_name = "aes-padlock",
Michal Ludvigccc17c32006-07-15 10:23:49 +1000276 .cra_priority = PADLOCK_CRA_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
278 .cra_blocksize = AES_BLOCK_SIZE,
Herbert Xufbdae9f2005-07-06 13:53:29 -0700279 .cra_ctxsize = sizeof(struct aes_ctx),
Herbert Xu6789b2d2005-07-06 13:52:27 -0700280 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 .cra_module = THIS_MODULE,
282 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
283 .cra_u = {
284 .cipher = {
285 .cia_min_keysize = AES_MIN_KEY_SIZE,
286 .cia_max_keysize = AES_MAX_KEY_SIZE,
287 .cia_setkey = aes_set_key,
288 .cia_encrypt = aes_encrypt,
Herbert Xu28e8c3a2005-07-06 13:52:43 -0700289 .cia_decrypt = aes_decrypt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 }
292};
293
Herbert Xu28ce7282006-08-21 21:38:42 +1000294static int ecb_aes_encrypt(struct blkcipher_desc *desc,
295 struct scatterlist *dst, struct scatterlist *src,
296 unsigned int nbytes)
297{
298 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
299 struct blkcipher_walk walk;
300 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000301 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000302
Herbert Xu420a4b22008-08-31 15:58:45 +1000303 padlock_reset_key(&ctx->cword.encrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100304
Herbert Xu28ce7282006-08-21 21:38:42 +1000305 blkcipher_walk_init(&walk, dst, src, nbytes);
306 err = blkcipher_walk_virt(desc, &walk);
307
Suresh Siddhae4914012008-08-13 22:02:26 +1000308 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000309 while ((nbytes = walk.nbytes)) {
310 padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
311 ctx->E, &ctx->cword.encrypt,
312 nbytes / AES_BLOCK_SIZE);
313 nbytes &= AES_BLOCK_SIZE - 1;
314 err = blkcipher_walk_done(desc, &walk, nbytes);
315 }
Suresh Siddhae4914012008-08-13 22:02:26 +1000316 irq_ts_restore(ts_state);
Herbert Xu28ce7282006-08-21 21:38:42 +1000317
Herbert Xu420a4b22008-08-31 15:58:45 +1000318 padlock_store_cword(&ctx->cword.encrypt);
319
Herbert Xu28ce7282006-08-21 21:38:42 +1000320 return err;
321}
322
323static int ecb_aes_decrypt(struct blkcipher_desc *desc,
324 struct scatterlist *dst, struct scatterlist *src,
325 unsigned int nbytes)
326{
327 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
328 struct blkcipher_walk walk;
329 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000330 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000331
Herbert Xu420a4b22008-08-31 15:58:45 +1000332 padlock_reset_key(&ctx->cword.decrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100333
Herbert Xu28ce7282006-08-21 21:38:42 +1000334 blkcipher_walk_init(&walk, dst, src, nbytes);
335 err = blkcipher_walk_virt(desc, &walk);
336
Suresh Siddhae4914012008-08-13 22:02:26 +1000337 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000338 while ((nbytes = walk.nbytes)) {
339 padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
340 ctx->D, &ctx->cword.decrypt,
341 nbytes / AES_BLOCK_SIZE);
342 nbytes &= AES_BLOCK_SIZE - 1;
343 err = blkcipher_walk_done(desc, &walk, nbytes);
344 }
Suresh Siddhae4914012008-08-13 22:02:26 +1000345 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000346
347 padlock_store_cword(&ctx->cword.encrypt);
348
Herbert Xu28ce7282006-08-21 21:38:42 +1000349 return err;
350}
351
352static struct crypto_alg ecb_aes_alg = {
353 .cra_name = "ecb(aes)",
354 .cra_driver_name = "ecb-aes-padlock",
355 .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
356 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
357 .cra_blocksize = AES_BLOCK_SIZE,
358 .cra_ctxsize = sizeof(struct aes_ctx),
359 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
360 .cra_type = &crypto_blkcipher_type,
361 .cra_module = THIS_MODULE,
362 .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
363 .cra_u = {
364 .blkcipher = {
365 .min_keysize = AES_MIN_KEY_SIZE,
366 .max_keysize = AES_MAX_KEY_SIZE,
367 .setkey = aes_set_key,
368 .encrypt = ecb_aes_encrypt,
369 .decrypt = ecb_aes_decrypt,
370 }
371 }
372};
373
374static int cbc_aes_encrypt(struct blkcipher_desc *desc,
375 struct scatterlist *dst, struct scatterlist *src,
376 unsigned int nbytes)
377{
378 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
379 struct blkcipher_walk walk;
380 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000381 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000382
Herbert Xu420a4b22008-08-31 15:58:45 +1000383 padlock_reset_key(&ctx->cword.encrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100384
Herbert Xu28ce7282006-08-21 21:38:42 +1000385 blkcipher_walk_init(&walk, dst, src, nbytes);
386 err = blkcipher_walk_virt(desc, &walk);
387
Suresh Siddhae4914012008-08-13 22:02:26 +1000388 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000389 while ((nbytes = walk.nbytes)) {
390 u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr,
391 walk.dst.virt.addr, ctx->E,
392 walk.iv, &ctx->cword.encrypt,
393 nbytes / AES_BLOCK_SIZE);
394 memcpy(walk.iv, iv, AES_BLOCK_SIZE);
395 nbytes &= AES_BLOCK_SIZE - 1;
396 err = blkcipher_walk_done(desc, &walk, nbytes);
397 }
Suresh Siddhae4914012008-08-13 22:02:26 +1000398 irq_ts_restore(ts_state);
Herbert Xu28ce7282006-08-21 21:38:42 +1000399
Herbert Xu420a4b22008-08-31 15:58:45 +1000400 padlock_store_cword(&ctx->cword.decrypt);
401
Herbert Xu28ce7282006-08-21 21:38:42 +1000402 return err;
403}
404
405static int cbc_aes_decrypt(struct blkcipher_desc *desc,
406 struct scatterlist *dst, struct scatterlist *src,
407 unsigned int nbytes)
408{
409 struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
410 struct blkcipher_walk walk;
411 int err;
Suresh Siddhae4914012008-08-13 22:02:26 +1000412 int ts_state;
Herbert Xu28ce7282006-08-21 21:38:42 +1000413
Herbert Xu420a4b22008-08-31 15:58:45 +1000414 padlock_reset_key(&ctx->cword.encrypt);
Herbert Xu866cd902007-12-27 00:04:44 +1100415
Herbert Xu28ce7282006-08-21 21:38:42 +1000416 blkcipher_walk_init(&walk, dst, src, nbytes);
417 err = blkcipher_walk_virt(desc, &walk);
418
Suresh Siddhae4914012008-08-13 22:02:26 +1000419 ts_state = irq_ts_save();
Herbert Xu28ce7282006-08-21 21:38:42 +1000420 while ((nbytes = walk.nbytes)) {
421 padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr,
422 ctx->D, walk.iv, &ctx->cword.decrypt,
423 nbytes / AES_BLOCK_SIZE);
424 nbytes &= AES_BLOCK_SIZE - 1;
425 err = blkcipher_walk_done(desc, &walk, nbytes);
426 }
427
Suresh Siddhae4914012008-08-13 22:02:26 +1000428 irq_ts_restore(ts_state);
Herbert Xu420a4b22008-08-31 15:58:45 +1000429
430 padlock_store_cword(&ctx->cword.encrypt);
431
Herbert Xu28ce7282006-08-21 21:38:42 +1000432 return err;
433}
434
435static struct crypto_alg cbc_aes_alg = {
436 .cra_name = "cbc(aes)",
437 .cra_driver_name = "cbc-aes-padlock",
438 .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
439 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
440 .cra_blocksize = AES_BLOCK_SIZE,
441 .cra_ctxsize = sizeof(struct aes_ctx),
442 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
443 .cra_type = &crypto_blkcipher_type,
444 .cra_module = THIS_MODULE,
445 .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
446 .cra_u = {
447 .blkcipher = {
448 .min_keysize = AES_MIN_KEY_SIZE,
449 .max_keysize = AES_MAX_KEY_SIZE,
450 .ivsize = AES_BLOCK_SIZE,
451 .setkey = aes_set_key,
452 .encrypt = cbc_aes_encrypt,
453 .decrypt = cbc_aes_decrypt,
454 }
455 }
456};
457
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000458static int __init padlock_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000460 int ret;
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800461 struct cpuinfo_x86 *c = &cpu_data(0);
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000462
463 if (!cpu_has_xcrypt) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800464 printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000465 return -ENODEV;
466 }
467
468 if (!cpu_has_xcrypt_enabled) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800469 printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000470 return -ENODEV;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Herbert Xu28ce7282006-08-21 21:38:42 +1000473 if ((ret = crypto_register_alg(&aes_alg)))
474 goto aes_err;
475
476 if ((ret = crypto_register_alg(&ecb_aes_alg)))
477 goto ecb_aes_err;
478
479 if ((ret = crypto_register_alg(&cbc_aes_alg)))
480 goto cbc_aes_err;
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000481
482 printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
483
Chuck Ebberta76c1c22009-06-18 19:24:10 +0800484 if (c->x86 == 6 && c->x86_model == 15 && c->x86_mask == 2) {
485 ecb_fetch_blocks = 8;
486 cbc_fetch_blocks = 4; /* NOTE: notused */
487 printk(KERN_NOTICE PFX "VIA Nano stepping 2 detected: enabling workaround.\n");
488 }
489
Herbert Xu28ce7282006-08-21 21:38:42 +1000490out:
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000491 return ret;
Herbert Xu28ce7282006-08-21 21:38:42 +1000492
493cbc_aes_err:
494 crypto_unregister_alg(&ecb_aes_alg);
495ecb_aes_err:
496 crypto_unregister_alg(&aes_alg);
497aes_err:
498 printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000502static void __exit padlock_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Herbert Xu28ce7282006-08-21 21:38:42 +1000504 crypto_unregister_alg(&cbc_aes_alg);
505 crypto_unregister_alg(&ecb_aes_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 crypto_unregister_alg(&aes_alg);
507}
Michal Ludvig1191f0a2006-08-06 22:46:20 +1000508
509module_init(padlock_init);
510module_exit(padlock_fini);
511
512MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
513MODULE_LICENSE("GPL");
514MODULE_AUTHOR("Michal Ludvig");
515
Herbert Xuacd246b2009-04-21 13:55:20 +0800516MODULE_ALIAS("aes");