Merge branch 'for_paulus' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc
[pandora-kernel.git] / drivers / crypto / padlock-aes.c
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>
52 #include <linux/kernel.h>
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
62 struct aes_ctx {
63         struct {
64                 struct cword encrypt;
65                 struct cword decrypt;
66         } cword;
67         u32 *D;
68         int key_length;
69         u32 E[AES_EXTENDED_KEY_SIZE];
70         u32 d_data[AES_EXTENDED_KEY_SIZE];
71 };
72
73 /* ====== Key management routines ====== */
74
75 static inline uint32_t
76 generic_rotr32 (const uint32_t x, const unsigned bits)
77 {
78         const unsigned n = bits % 32;
79         return (x >> n) | (x << (32 - n));
80 }
81
82 static inline uint32_t
83 generic_rotl32 (const uint32_t x, const unsigned bits)
84 {
85         const unsigned n = bits % 32;
86         return (x << n) | (x >> (32 - n));
87 }
88
89 #define rotl generic_rotl32
90 #define rotr generic_rotr32
91
92 /*
93  * #define byte(x, nr) ((unsigned char)((x) >> (nr*8))) 
94  */
95 static inline uint8_t
96 byte(const uint32_t x, const unsigned n)
97 {
98         return x >> (n << 3);
99 }
100
101 #define E_KEY ctx->E
102 #define D_KEY ctx->D
103
104 static uint8_t pow_tab[256];
105 static uint8_t log_tab[256];
106 static uint8_t sbx_tab[256];
107 static uint8_t isb_tab[256];
108 static uint32_t rco_tab[10];
109 static uint32_t ft_tab[4][256];
110 static uint32_t it_tab[4][256];
111
112 static uint32_t fl_tab[4][256];
113 static uint32_t il_tab[4][256];
114
115 static inline uint8_t
116 f_mult (uint8_t a, uint8_t b)
117 {
118         uint8_t aa = log_tab[a], cc = aa + log_tab[b];
119
120         return pow_tab[cc + (cc < aa ? 1 : 0)];
121 }
122
123 #define ff_mult(a,b)    (a && b ? f_mult(a, b) : 0)
124
125 #define f_rn(bo, bi, n, k)                                      \
126     bo[n] =  ft_tab[0][byte(bi[n],0)] ^                         \
127              ft_tab[1][byte(bi[(n + 1) & 3],1)] ^               \
128              ft_tab[2][byte(bi[(n + 2) & 3],2)] ^               \
129              ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
130
131 #define i_rn(bo, bi, n, k)                                      \
132     bo[n] =  it_tab[0][byte(bi[n],0)] ^                         \
133              it_tab[1][byte(bi[(n + 3) & 3],1)] ^               \
134              it_tab[2][byte(bi[(n + 2) & 3],2)] ^               \
135              it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
136
137 #define ls_box(x)                               \
138     ( fl_tab[0][byte(x, 0)] ^                   \
139       fl_tab[1][byte(x, 1)] ^                   \
140       fl_tab[2][byte(x, 2)] ^                   \
141       fl_tab[3][byte(x, 3)] )
142
143 #define f_rl(bo, bi, n, k)                                      \
144     bo[n] =  fl_tab[0][byte(bi[n],0)] ^                         \
145              fl_tab[1][byte(bi[(n + 1) & 3],1)] ^               \
146              fl_tab[2][byte(bi[(n + 2) & 3],2)] ^               \
147              fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
148
149 #define i_rl(bo, bi, n, k)                                      \
150     bo[n] =  il_tab[0][byte(bi[n],0)] ^                         \
151              il_tab[1][byte(bi[(n + 3) & 3],1)] ^               \
152              il_tab[2][byte(bi[(n + 2) & 3],2)] ^               \
153              il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
154
155 static void
156 gen_tabs (void)
157 {
158         uint32_t i, t;
159         uint8_t p, q;
160
161         /* log and power tables for GF(2**8) finite field with
162            0x011b as modular polynomial - the simplest prmitive
163            root is 0x03, used here to generate the tables */
164
165         for (i = 0, p = 1; i < 256; ++i) {
166                 pow_tab[i] = (uint8_t) p;
167                 log_tab[p] = (uint8_t) i;
168
169                 p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
170         }
171
172         log_tab[1] = 0;
173
174         for (i = 0, p = 1; i < 10; ++i) {
175                 rco_tab[i] = p;
176
177                 p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
178         }
179
180         for (i = 0; i < 256; ++i) {
181                 p = (i ? pow_tab[255 - log_tab[i]] : 0);
182                 q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
183                 p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
184                 sbx_tab[i] = p;
185                 isb_tab[p] = (uint8_t) i;
186         }
187
188         for (i = 0; i < 256; ++i) {
189                 p = sbx_tab[i];
190
191                 t = p;
192                 fl_tab[0][i] = t;
193                 fl_tab[1][i] = rotl (t, 8);
194                 fl_tab[2][i] = rotl (t, 16);
195                 fl_tab[3][i] = rotl (t, 24);
196
197                 t = ((uint32_t) ff_mult (2, p)) |
198                     ((uint32_t) p << 8) |
199                     ((uint32_t) p << 16) | ((uint32_t) ff_mult (3, p) << 24);
200
201                 ft_tab[0][i] = t;
202                 ft_tab[1][i] = rotl (t, 8);
203                 ft_tab[2][i] = rotl (t, 16);
204                 ft_tab[3][i] = rotl (t, 24);
205
206                 p = isb_tab[i];
207
208                 t = p;
209                 il_tab[0][i] = t;
210                 il_tab[1][i] = rotl (t, 8);
211                 il_tab[2][i] = rotl (t, 16);
212                 il_tab[3][i] = rotl (t, 24);
213
214                 t = ((uint32_t) ff_mult (14, p)) |
215                     ((uint32_t) ff_mult (9, p) << 8) |
216                     ((uint32_t) ff_mult (13, p) << 16) |
217                     ((uint32_t) ff_mult (11, p) << 24);
218
219                 it_tab[0][i] = t;
220                 it_tab[1][i] = rotl (t, 8);
221                 it_tab[2][i] = rotl (t, 16);
222                 it_tab[3][i] = rotl (t, 24);
223         }
224 }
225
226 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
227
228 #define imix_col(y,x)       \
229     u   = star_x(x);        \
230     v   = star_x(u);        \
231     w   = star_x(v);        \
232     t   = w ^ (x);          \
233    (y)  = u ^ v ^ w;        \
234    (y) ^= rotr(u ^ t,  8) ^ \
235           rotr(v ^ t, 16) ^ \
236           rotr(t,24)
237
238 /* initialise the key schedule from the user supplied key */
239
240 #define loop4(i)                                    \
241 {   t = rotr(t,  8); t = ls_box(t) ^ rco_tab[i];    \
242     t ^= E_KEY[4 * i];     E_KEY[4 * i + 4] = t;    \
243     t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t;    \
244     t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t;    \
245     t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t;    \
246 }
247
248 #define loop6(i)                                    \
249 {   t = rotr(t,  8); t = ls_box(t) ^ rco_tab[i];    \
250     t ^= E_KEY[6 * i];     E_KEY[6 * i + 6] = t;    \
251     t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t;    \
252     t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t;    \
253     t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t;    \
254     t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t;   \
255     t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t;   \
256 }
257
258 #define loop8(i)                                    \
259 {   t = rotr(t,  8); ; t = ls_box(t) ^ rco_tab[i];  \
260     t ^= E_KEY[8 * i];     E_KEY[8 * i + 8] = t;    \
261     t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t;    \
262     t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t;   \
263     t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t;   \
264     t  = E_KEY[8 * i + 4] ^ ls_box(t);    \
265     E_KEY[8 * i + 12] = t;                \
266     t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t;   \
267     t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t;   \
268     t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t;   \
269 }
270
271 /* Tells whether the ACE is capable to generate
272    the extended key for a given key_len. */
273 static inline int
274 aes_hw_extkey_available(uint8_t key_len)
275 {
276         /* TODO: We should check the actual CPU model/stepping
277                  as it's possible that the capability will be
278                  added in the next CPU revisions. */
279         if (key_len == 16)
280                 return 1;
281         return 0;
282 }
283
284 static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
285 {
286         unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
287         unsigned long align = PADLOCK_ALIGNMENT;
288
289         if (align <= crypto_tfm_ctx_alignment())
290                 align = 1;
291         return (struct aes_ctx *)ALIGN(addr, align);
292 }
293
294 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
295                        unsigned int key_len, u32 *flags)
296 {
297         struct aes_ctx *ctx = aes_ctx(tfm);
298         const __le32 *key = (const __le32 *)in_key;
299         uint32_t i, t, u, v, w;
300         uint32_t P[AES_EXTENDED_KEY_SIZE];
301         uint32_t rounds;
302
303         if (key_len != 16 && key_len != 24 && key_len != 32) {
304                 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
305                 return -EINVAL;
306         }
307
308         ctx->key_length = key_len;
309
310         /*
311          * If the hardware is capable of generating the extended key
312          * itself we must supply the plain key for both encryption
313          * and decryption.
314          */
315         ctx->D = ctx->E;
316
317         E_KEY[0] = le32_to_cpu(key[0]);
318         E_KEY[1] = le32_to_cpu(key[1]);
319         E_KEY[2] = le32_to_cpu(key[2]);
320         E_KEY[3] = le32_to_cpu(key[3]);
321
322         /* Prepare control words. */
323         memset(&ctx->cword, 0, sizeof(ctx->cword));
324
325         ctx->cword.decrypt.encdec = 1;
326         ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
327         ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
328         ctx->cword.encrypt.ksize = (key_len - 16) / 8;
329         ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
330
331         /* Don't generate extended keys if the hardware can do it. */
332         if (aes_hw_extkey_available(key_len))
333                 return 0;
334
335         ctx->D = ctx->d_data;
336         ctx->cword.encrypt.keygen = 1;
337         ctx->cword.decrypt.keygen = 1;
338
339         switch (key_len) {
340         case 16:
341                 t = E_KEY[3];
342                 for (i = 0; i < 10; ++i)
343                         loop4 (i);
344                 break;
345
346         case 24:
347                 E_KEY[4] = le32_to_cpu(key[4]);
348                 t = E_KEY[5] = le32_to_cpu(key[5]);
349                 for (i = 0; i < 8; ++i)
350                         loop6 (i);
351                 break;
352
353         case 32:
354                 E_KEY[4] = le32_to_cpu(key[4]);
355                 E_KEY[5] = le32_to_cpu(key[5]);
356                 E_KEY[6] = le32_to_cpu(key[6]);
357                 t = E_KEY[7] = le32_to_cpu(key[7]);
358                 for (i = 0; i < 7; ++i)
359                         loop8 (i);
360                 break;
361         }
362
363         D_KEY[0] = E_KEY[0];
364         D_KEY[1] = E_KEY[1];
365         D_KEY[2] = E_KEY[2];
366         D_KEY[3] = E_KEY[3];
367
368         for (i = 4; i < key_len + 24; ++i) {
369                 imix_col (D_KEY[i], E_KEY[i]);
370         }
371
372         /* PadLock needs a different format of the decryption key. */
373         rounds = 10 + (key_len - 16) / 4;
374
375         for (i = 0; i < rounds; i++) {
376                 P[((i + 1) * 4) + 0] = D_KEY[((rounds - i - 1) * 4) + 0];
377                 P[((i + 1) * 4) + 1] = D_KEY[((rounds - i - 1) * 4) + 1];
378                 P[((i + 1) * 4) + 2] = D_KEY[((rounds - i - 1) * 4) + 2];
379                 P[((i + 1) * 4) + 3] = D_KEY[((rounds - i - 1) * 4) + 3];
380         }
381
382         P[0] = E_KEY[(rounds * 4) + 0];
383         P[1] = E_KEY[(rounds * 4) + 1];
384         P[2] = E_KEY[(rounds * 4) + 2];
385         P[3] = E_KEY[(rounds * 4) + 3];
386
387         memcpy(D_KEY, P, AES_EXTENDED_KEY_SIZE_B);
388
389         return 0;
390 }
391
392 /* ====== Encryption/decryption routines ====== */
393
394 /* These are the real call to PadLock. */
395 static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
396                                       void *control_word, u32 count)
397 {
398         asm volatile ("pushfl; popfl");         /* enforce key reload. */
399         asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
400                       : "+S"(input), "+D"(output)
401                       : "d"(control_word), "b"(key), "c"(count));
402 }
403
404 static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
405                                      u8 *iv, void *control_word, u32 count)
406 {
407         /* Enforce key reload. */
408         asm volatile ("pushfl; popfl");
409         /* rep xcryptcbc */
410         asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
411                       : "+S" (input), "+D" (output), "+a" (iv)
412                       : "d" (control_word), "b" (key), "c" (count));
413         return iv;
414 }
415
416 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
417 {
418         struct aes_ctx *ctx = aes_ctx(tfm);
419         padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, 1);
420 }
421
422 static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
423 {
424         struct aes_ctx *ctx = aes_ctx(tfm);
425         padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, 1);
426 }
427
428 static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
429                                     const u8 *in, unsigned int nbytes)
430 {
431         struct aes_ctx *ctx = aes_ctx(desc->tfm);
432         padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt,
433                            nbytes / AES_BLOCK_SIZE);
434         return nbytes & ~(AES_BLOCK_SIZE - 1);
435 }
436
437 static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
438                                     const u8 *in, unsigned int nbytes)
439 {
440         struct aes_ctx *ctx = aes_ctx(desc->tfm);
441         padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt,
442                            nbytes / AES_BLOCK_SIZE);
443         return nbytes & ~(AES_BLOCK_SIZE - 1);
444 }
445
446 static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
447                                     const u8 *in, unsigned int nbytes)
448 {
449         struct aes_ctx *ctx = aes_ctx(desc->tfm);
450         u8 *iv;
451
452         iv = padlock_xcrypt_cbc(in, out, ctx->E, desc->info,
453                                 &ctx->cword.encrypt, nbytes / AES_BLOCK_SIZE);
454         memcpy(desc->info, iv, AES_BLOCK_SIZE);
455
456         return nbytes & ~(AES_BLOCK_SIZE - 1);
457 }
458
459 static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
460                                     const u8 *in, unsigned int nbytes)
461 {
462         struct aes_ctx *ctx = aes_ctx(desc->tfm);
463         padlock_xcrypt_cbc(in, out, ctx->D, desc->info, &ctx->cword.decrypt,
464                            nbytes / AES_BLOCK_SIZE);
465         return nbytes & ~(AES_BLOCK_SIZE - 1);
466 }
467
468 static struct crypto_alg aes_alg = {
469         .cra_name               =       "aes",
470         .cra_driver_name        =       "aes-padlock",
471         .cra_priority           =       300,
472         .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
473         .cra_blocksize          =       AES_BLOCK_SIZE,
474         .cra_ctxsize            =       sizeof(struct aes_ctx),
475         .cra_alignmask          =       PADLOCK_ALIGNMENT - 1,
476         .cra_module             =       THIS_MODULE,
477         .cra_list               =       LIST_HEAD_INIT(aes_alg.cra_list),
478         .cra_u                  =       {
479                 .cipher = {
480                         .cia_min_keysize        =       AES_MIN_KEY_SIZE,
481                         .cia_max_keysize        =       AES_MAX_KEY_SIZE,
482                         .cia_setkey             =       aes_set_key,
483                         .cia_encrypt            =       aes_encrypt,
484                         .cia_decrypt            =       aes_decrypt,
485                         .cia_encrypt_ecb        =       aes_encrypt_ecb,
486                         .cia_decrypt_ecb        =       aes_decrypt_ecb,
487                         .cia_encrypt_cbc        =       aes_encrypt_cbc,
488                         .cia_decrypt_cbc        =       aes_decrypt_cbc,
489                 }
490         }
491 };
492
493 int __init padlock_init_aes(void)
494 {
495         printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
496
497         gen_tabs();
498         return crypto_register_alg(&aes_alg);
499 }
500
501 void __exit padlock_fini_aes(void)
502 {
503         crypto_unregister_alg(&aes_alg);
504 }