aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86_64/Makefile4
-rw-r--r--arch/x86_64/crypto/Makefile9
-rw-r--r--arch/x86_64/crypto/aes-x86_64-asm.S186
-rw-r--r--arch/x86_64/crypto/aes.c324
-rw-r--r--crypto/Kconfig22
-rw-r--r--crypto/api.c60
-rw-r--r--crypto/cipher.c312
-rw-r--r--crypto/des.c2005
-rw-r--r--crypto/hmac.c3
-rw-r--r--crypto/internal.h43
-rw-r--r--crypto/scatterwalk.c4
-rw-r--r--crypto/scatterwalk.h12
-rw-r--r--crypto/serpent.c1
-rw-r--r--drivers/crypto/padlock-aes.c153
-rw-r--r--drivers/crypto/padlock.h22
-rw-r--r--include/linux/crypto.h34
16 files changed, 1845 insertions, 1349 deletions
diff --git a/arch/x86_64/Makefile b/arch/x86_64/Makefile
index 8a73794f9b90..428915697675 100644
--- a/arch/x86_64/Makefile
+++ b/arch/x86_64/Makefile
@@ -65,7 +65,9 @@ CFLAGS += $(call cc-option,-mno-sse -mno-mmx -mno-sse2 -mno-3dnow,)
65head-y := arch/x86_64/kernel/head.o arch/x86_64/kernel/head64.o arch/x86_64/kernel/init_task.o 65head-y := arch/x86_64/kernel/head.o arch/x86_64/kernel/head64.o arch/x86_64/kernel/init_task.o
66 66
67libs-y += arch/x86_64/lib/ 67libs-y += arch/x86_64/lib/
68core-y += arch/x86_64/kernel/ arch/x86_64/mm/ 68core-y += arch/x86_64/kernel/ \
69 arch/x86_64/mm/ \
70 arch/x86_64/crypto/
69core-$(CONFIG_IA32_EMULATION) += arch/x86_64/ia32/ 71core-$(CONFIG_IA32_EMULATION) += arch/x86_64/ia32/
70drivers-$(CONFIG_PCI) += arch/x86_64/pci/ 72drivers-$(CONFIG_PCI) += arch/x86_64/pci/
71drivers-$(CONFIG_OPROFILE) += arch/x86_64/oprofile/ 73drivers-$(CONFIG_OPROFILE) += arch/x86_64/oprofile/
diff --git a/arch/x86_64/crypto/Makefile b/arch/x86_64/crypto/Makefile
new file mode 100644
index 000000000000..426d20f4b72e
--- /dev/null
+++ b/arch/x86_64/crypto/Makefile
@@ -0,0 +1,9 @@
1#
2# x86_64/crypto/Makefile
3#
4# Arch-specific CryptoAPI modules.
5#
6
7obj-$(CONFIG_CRYPTO_AES_X86_64) += aes-x86_64.o
8
9aes-x86_64-y := aes-x86_64-asm.o aes.o
diff --git a/arch/x86_64/crypto/aes-x86_64-asm.S b/arch/x86_64/crypto/aes-x86_64-asm.S
new file mode 100644
index 000000000000..483cbb23ab8d
--- /dev/null
+++ b/arch/x86_64/crypto/aes-x86_64-asm.S
@@ -0,0 +1,186 @@
1/* AES (Rijndael) implementation (FIPS PUB 197) for x86_64
2 *
3 * Copyright (C) 2005 Andreas Steinmetz, <ast@domdv.de>
4 *
5 * License:
6 * This code can be distributed under the terms of the GNU General Public
7 * License (GPL) Version 2 provided that the above header down to and
8 * including this sentence is retained in full.
9 */
10
11.extern aes_ft_tab
12.extern aes_it_tab
13.extern aes_fl_tab
14.extern aes_il_tab
15
16.text
17
18#define R1 %rax
19#define R1E %eax
20#define R1X %ax
21#define R1H %ah
22#define R1L %al
23#define R2 %rbx
24#define R2E %ebx
25#define R2X %bx
26#define R2H %bh
27#define R2L %bl
28#define R3 %rcx
29#define R3E %ecx
30#define R3X %cx
31#define R3H %ch
32#define R3L %cl
33#define R4 %rdx
34#define R4E %edx
35#define R4X %dx
36#define R4H %dh
37#define R4L %dl
38#define R5 %rsi
39#define R5E %esi
40#define R6 %rdi
41#define R6E %edi
42#define R7 %rbp
43#define R7E %ebp
44#define R8 %r8
45#define R9 %r9
46#define R10 %r10
47#define R11 %r11
48
49#define prologue(FUNC,BASE,B128,B192,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11) \
50 .global FUNC; \
51 .type FUNC,@function; \
52 .align 8; \
53FUNC: movq r1,r2; \
54 movq r3,r4; \
55 leaq BASE+52(r8),r9; \
56 movq r10,r11; \
57 movl (r7),r5 ## E; \
58 movl 4(r7),r1 ## E; \
59 movl 8(r7),r6 ## E; \
60 movl 12(r7),r7 ## E; \
61 movl (r8),r10 ## E; \
62 xorl -48(r9),r5 ## E; \
63 xorl -44(r9),r1 ## E; \
64 xorl -40(r9),r6 ## E; \
65 xorl -36(r9),r7 ## E; \
66 cmpl $24,r10 ## E; \
67 jb B128; \
68 leaq 32(r9),r9; \
69 je B192; \
70 leaq 32(r9),r9;
71
72#define epilogue(r1,r2,r3,r4,r5,r6,r7,r8,r9) \
73 movq r1,r2; \
74 movq r3,r4; \
75 movl r5 ## E,(r9); \
76 movl r6 ## E,4(r9); \
77 movl r7 ## E,8(r9); \
78 movl r8 ## E,12(r9); \
79 ret;
80
81#define round(TAB,OFFSET,r1,r2,r3,r4,r5,r6,r7,r8,ra,rb,rc,rd) \
82 movzbl r2 ## H,r5 ## E; \
83 movzbl r2 ## L,r6 ## E; \
84 movl TAB+1024(,r5,4),r5 ## E;\
85 movw r4 ## X,r2 ## X; \
86 movl TAB(,r6,4),r6 ## E; \
87 roll $16,r2 ## E; \
88 shrl $16,r4 ## E; \
89 movzbl r4 ## H,r7 ## E; \
90 movzbl r4 ## L,r4 ## E; \
91 xorl OFFSET(r8),ra ## E; \
92 xorl OFFSET+4(r8),rb ## E; \
93 xorl TAB+3072(,r7,4),r5 ## E;\
94 xorl TAB+2048(,r4,4),r6 ## E;\
95 movzbl r1 ## L,r7 ## E; \
96 movzbl r1 ## H,r4 ## E; \
97 movl TAB+1024(,r4,4),r4 ## E;\
98 movw r3 ## X,r1 ## X; \
99 roll $16,r1 ## E; \
100 shrl $16,r3 ## E; \
101 xorl TAB(,r7,4),r5 ## E; \
102 movzbl r3 ## H,r7 ## E; \
103 movzbl r3 ## L,r3 ## E; \
104 xorl TAB+3072(,r7,4),r4 ## E;\
105 xorl TAB+2048(,r3,4),r5 ## E;\
106 movzbl r1 ## H,r7 ## E; \
107 movzbl r1 ## L,r3 ## E; \
108 shrl $16,r1 ## E; \
109 xorl TAB+3072(,r7,4),r6 ## E;\
110 movl TAB+2048(,r3,4),r3 ## E;\
111 movzbl r1 ## H,r7 ## E; \
112 movzbl r1 ## L,r1 ## E; \
113 xorl TAB+1024(,r7,4),r6 ## E;\
114 xorl TAB(,r1,4),r3 ## E; \
115 movzbl r2 ## H,r1 ## E; \
116 movzbl r2 ## L,r7 ## E; \
117 shrl $16,r2 ## E; \
118 xorl TAB+3072(,r1,4),r3 ## E;\
119 xorl TAB+2048(,r7,4),r4 ## E;\
120 movzbl r2 ## H,r1 ## E; \
121 movzbl r2 ## L,r2 ## E; \
122 xorl OFFSET+8(r8),rc ## E; \
123 xorl OFFSET+12(r8),rd ## E; \
124 xorl TAB+1024(,r1,4),r3 ## E;\
125 xorl TAB(,r2,4),r4 ## E;
126
127#define move_regs(r1,r2,r3,r4) \
128 movl r3 ## E,r1 ## E; \
129 movl r4 ## E,r2 ## E;
130
131#define entry(FUNC,BASE,B128,B192) \
132 prologue(FUNC,BASE,B128,B192,R2,R8,R7,R9,R1,R3,R4,R6,R10,R5,R11)
133
134#define return epilogue(R8,R2,R9,R7,R5,R6,R3,R4,R11)
135
136#define encrypt_round(TAB,OFFSET) \
137 round(TAB,OFFSET,R1,R2,R3,R4,R5,R6,R7,R10,R5,R6,R3,R4) \
138 move_regs(R1,R2,R5,R6)
139
140#define encrypt_final(TAB,OFFSET) \
141 round(TAB,OFFSET,R1,R2,R3,R4,R5,R6,R7,R10,R5,R6,R3,R4)
142
143#define decrypt_round(TAB,OFFSET) \
144 round(TAB,OFFSET,R2,R1,R4,R3,R6,R5,R7,R10,R5,R6,R3,R4) \
145 move_regs(R1,R2,R5,R6)
146
147#define decrypt_final(TAB,OFFSET) \
148 round(TAB,OFFSET,R2,R1,R4,R3,R6,R5,R7,R10,R5,R6,R3,R4)
149
150/* void aes_encrypt(void *ctx, u8 *out, const u8 *in) */
151
152 entry(aes_encrypt,0,enc128,enc192)
153 encrypt_round(aes_ft_tab,-96)
154 encrypt_round(aes_ft_tab,-80)
155enc192: encrypt_round(aes_ft_tab,-64)
156 encrypt_round(aes_ft_tab,-48)
157enc128: encrypt_round(aes_ft_tab,-32)
158 encrypt_round(aes_ft_tab,-16)
159 encrypt_round(aes_ft_tab, 0)
160 encrypt_round(aes_ft_tab, 16)
161 encrypt_round(aes_ft_tab, 32)
162 encrypt_round(aes_ft_tab, 48)
163 encrypt_round(aes_ft_tab, 64)
164 encrypt_round(aes_ft_tab, 80)
165 encrypt_round(aes_ft_tab, 96)
166 encrypt_final(aes_fl_tab,112)
167 return
168
169/* void aes_decrypt(void *ctx, u8 *out, const u8 *in) */
170
171 entry(aes_decrypt,240,dec128,dec192)
172 decrypt_round(aes_it_tab,-96)
173 decrypt_round(aes_it_tab,-80)
174dec192: decrypt_round(aes_it_tab,-64)
175 decrypt_round(aes_it_tab,-48)
176dec128: decrypt_round(aes_it_tab,-32)
177 decrypt_round(aes_it_tab,-16)
178 decrypt_round(aes_it_tab, 0)
179 decrypt_round(aes_it_tab, 16)
180 decrypt_round(aes_it_tab, 32)
181 decrypt_round(aes_it_tab, 48)
182 decrypt_round(aes_it_tab, 64)
183 decrypt_round(aes_it_tab, 80)
184 decrypt_round(aes_it_tab, 96)
185 decrypt_final(aes_il_tab,112)
186 return
diff --git a/arch/x86_64/crypto/aes.c b/arch/x86_64/crypto/aes.c
new file mode 100644
index 000000000000..2b5c4010ce38
--- /dev/null
+++ b/arch/x86_64/crypto/aes.c
@@ -0,0 +1,324 @@
1/*
2 * Cryptographic API.
3 *
4 * AES Cipher Algorithm.
5 *
6 * Based on Brian Gladman's code.
7 *
8 * Linux developers:
9 * Alexander Kjeldaas <astor@fast.no>
10 * Herbert Valerio Riedel <hvr@hvrlab.org>
11 * Kyle McMartin <kyle@debian.org>
12 * Adam J. Richter <adam@yggdrasil.com> (conversion to 2.5 API).
13 * Andreas Steinmetz <ast@domdv.de> (adapted to x86_64 assembler)
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * ---------------------------------------------------------------------------
21 * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
22 * All rights reserved.
23 *
24 * LICENSE TERMS
25 *
26 * The free distribution and use of this software in both source and binary
27 * form is allowed (with or without changes) provided that:
28 *
29 * 1. distributions of this source code include the above copyright
30 * notice, this list of conditions and the following disclaimer;
31 *
32 * 2. distributions in binary form include the above copyright
33 * notice, this list of conditions and the following disclaimer
34 * in the documentation and/or other associated materials;
35 *
36 * 3. the copyright holder's name is not used to endorse products
37 * built using this software without specific written permission.
38 *
39 * ALTERNATIVELY, provided that this notice is retained in full, this product
40 * may be distributed under the terms of the GNU General Public License (GPL),
41 * in which case the provisions of the GPL apply INSTEAD OF those given above.
42 *
43 * DISCLAIMER
44 *
45 * This software is provided 'as is' with no explicit or implied warranties
46 * in respect of its properties, including, but not limited to, correctness
47 * and/or fitness for purpose.
48 * ---------------------------------------------------------------------------
49 */
50
51/* Some changes from the Gladman version:
52 s/RIJNDAEL(e_key)/E_KEY/g
53 s/RIJNDAEL(d_key)/D_KEY/g
54*/
55
56#include <asm/byteorder.h>
57#include <linux/bitops.h>
58#include <linux/crypto.h>
59#include <linux/errno.h>
60#include <linux/init.h>
61#include <linux/module.h>
62#include <linux/types.h>
63
64#define AES_MIN_KEY_SIZE 16
65#define AES_MAX_KEY_SIZE 32
66
67#define AES_BLOCK_SIZE 16
68
69/*
70 * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
71 */
72static inline u8 byte(const u32 x, const unsigned n)
73{
74 return x >> (n << 3);
75}
76
77#define u32_in(x) le32_to_cpu(*(const __le32 *)(x))
78
79struct aes_ctx
80{
81 u32 key_length;
82 u32 E[60];
83 u32 D[60];
84};
85
86#define E_KEY ctx->E
87#define D_KEY ctx->D
88
89static u8 pow_tab[256] __initdata;
90static u8 log_tab[256] __initdata;
91static u8 sbx_tab[256] __initdata;
92static u8 isb_tab[256] __initdata;
93static u32 rco_tab[10];
94u32 aes_ft_tab[4][256];
95u32 aes_it_tab[4][256];
96
97u32 aes_fl_tab[4][256];
98u32 aes_il_tab[4][256];
99
100static inline u8 f_mult(u8 a, u8 b)
101{
102 u8 aa = log_tab[a], cc = aa + log_tab[b];
103
104 return pow_tab[cc + (cc < aa ? 1 : 0)];
105}
106
107#define ff_mult(a, b) (a && b ? f_mult(a, b) : 0)
108
109#define ls_box(x) \
110 (aes_fl_tab[0][byte(x, 0)] ^ \
111 aes_fl_tab[1][byte(x, 1)] ^ \
112 aes_fl_tab[2][byte(x, 2)] ^ \
113 aes_fl_tab[3][byte(x, 3)])
114
115static void __init gen_tabs(void)
116{
117 u32 i, t;
118 u8 p, q;
119
120 /* log and power tables for GF(2**8) finite field with
121 0x011b as modular polynomial - the simplest primitive
122 root is 0x03, used here to generate the tables */
123
124 for (i = 0, p = 1; i < 256; ++i) {
125 pow_tab[i] = (u8)p;
126 log_tab[p] = (u8)i;
127
128 p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
129 }
130
131 log_tab[1] = 0;
132
133 for (i = 0, p = 1; i < 10; ++i) {
134 rco_tab[i] = p;
135
136 p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
137 }
138
139 for (i = 0; i < 256; ++i) {
140 p = (i ? pow_tab[255 - log_tab[i]] : 0);
141 q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
142 p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
143 sbx_tab[i] = p;
144 isb_tab[p] = (u8)i;
145 }
146
147 for (i = 0; i < 256; ++i) {
148 p = sbx_tab[i];
149
150 t = p;
151 aes_fl_tab[0][i] = t;
152 aes_fl_tab[1][i] = rol32(t, 8);
153 aes_fl_tab[2][i] = rol32(t, 16);
154 aes_fl_tab[3][i] = rol32(t, 24);
155
156 t = ((u32)ff_mult(2, p)) |
157 ((u32)p << 8) |
158 ((u32)p << 16) | ((u32)ff_mult(3, p) << 24);
159
160 aes_ft_tab[0][i] = t;
161 aes_ft_tab[1][i] = rol32(t, 8);
162 aes_ft_tab[2][i] = rol32(t, 16);
163 aes_ft_tab[3][i] = rol32(t, 24);
164
165 p = isb_tab[i];
166
167 t = p;
168 aes_il_tab[0][i] = t;
169 aes_il_tab[1][i] = rol32(t, 8);
170 aes_il_tab[2][i] = rol32(t, 16);
171 aes_il_tab[3][i] = rol32(t, 24);
172
173 t = ((u32)ff_mult(14, p)) |
174 ((u32)ff_mult(9, p) << 8) |
175 ((u32)ff_mult(13, p) << 16) |
176 ((u32)ff_mult(11, p) << 24);
177
178 aes_it_tab[0][i] = t;
179 aes_it_tab[1][i] = rol32(t, 8);
180 aes_it_tab[2][i] = rol32(t, 16);
181 aes_it_tab[3][i] = rol32(t, 24);
182 }
183}
184
185#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
186
187#define imix_col(y, x) \
188 u = star_x(x); \
189 v = star_x(u); \
190 w = star_x(v); \
191 t = w ^ (x); \
192 (y) = u ^ v ^ w; \
193 (y) ^= ror32(u ^ t, 8) ^ \
194 ror32(v ^ t, 16) ^ \
195 ror32(t, 24)
196
197/* initialise the key schedule from the user supplied key */
198
199#define loop4(i) \
200{ \
201 t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
202 t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
203 t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
204 t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
205 t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
206}
207
208#define loop6(i) \
209{ \
210 t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
211 t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
212 t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
213 t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
214 t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
215 t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
216 t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
217}
218
219#define loop8(i) \
220{ \
221 t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
222 t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
223 t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
224 t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
225 t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
226 t = E_KEY[8 * i + 4] ^ ls_box(t); \
227 E_KEY[8 * i + 12] = t; \
228 t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
229 t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
230 t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
231}
232
233static int aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len,
234 u32 *flags)
235{
236 struct aes_ctx *ctx = ctx_arg;
237 u32 i, j, t, u, v, w;
238
239 if (key_len != 16 && key_len != 24 && key_len != 32) {
240 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
241 return -EINVAL;
242 }
243
244 ctx->key_length = key_len;
245
246 D_KEY[key_len + 24] = E_KEY[0] = u32_in(in_key);
247 D_KEY[key_len + 25] = E_KEY[1] = u32_in(in_key + 4);
248 D_KEY[key_len + 26] = E_KEY[2] = u32_in(in_key + 8);
249 D_KEY[key_len + 27] = E_KEY[3] = u32_in(in_key + 12);
250
251 switch (key_len) {
252 case 16:
253 t = E_KEY[3];
254 for (i = 0; i < 10; ++i)
255 loop4(i);
256 break;
257
258 case 24:
259 E_KEY[4] = u32_in(in_key + 16);
260 t = E_KEY[5] = u32_in(in_key + 20);
261 for (i = 0; i < 8; ++i)
262 loop6 (i);
263 break;
264
265 case 32:
266 E_KEY[4] = u32_in(in_key + 16);
267 E_KEY[5] = u32_in(in_key + 20);
268 E_KEY[6] = u32_in(in_key + 24);
269 t = E_KEY[7] = u32_in(in_key + 28);
270 for (i = 0; i < 7; ++i)
271 loop8(i);
272 break;
273 }
274
275 D_KEY[0] = E_KEY[key_len + 24];
276 D_KEY[1] = E_KEY[key_len + 25];
277 D_KEY[2] = E_KEY[key_len + 26];
278 D_KEY[3] = E_KEY[key_len + 27];
279
280 for (i = 4; i < key_len + 24; ++i) {
281 j = key_len + 24 - (i & ~3) + (i & 3);
282 imix_col(D_KEY[j], E_KEY[i]);
283 }
284
285 return 0;
286}
287
288extern void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in);
289extern void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in);
290
291static struct crypto_alg aes_alg = {
292 .cra_name = "aes",
293 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
294 .cra_blocksize = AES_BLOCK_SIZE,
295 .cra_ctxsize = sizeof(struct aes_ctx),
296 .cra_module = THIS_MODULE,
297 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
298 .cra_u = {
299 .cipher = {
300 .cia_min_keysize = AES_MIN_KEY_SIZE,
301 .cia_max_keysize = AES_MAX_KEY_SIZE,
302 .cia_setkey = aes_set_key,
303 .cia_encrypt = aes_encrypt,
304 .cia_decrypt = aes_decrypt
305 }
306 }
307};
308
309static int __init aes_init(void)
310{
311 gen_tabs();
312 return crypto_register_alg(&aes_alg);
313}
314
315static void __exit aes_fini(void)
316{
317 crypto_unregister_alg(&aes_alg);
318}
319
320module_init(aes_init);
321module_exit(aes_fini);
322
323MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
324MODULE_LICENSE("GPL");
diff --git a/crypto/Kconfig b/crypto/Kconfig
index 90d6089d60ed..256c0b1fed10 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -146,7 +146,7 @@ config CRYPTO_SERPENT
146 146
147config CRYPTO_AES 147config CRYPTO_AES
148 tristate "AES cipher algorithms" 148 tristate "AES cipher algorithms"
149 depends on CRYPTO && !((X86 || UML_X86) && !64BIT) 149 depends on CRYPTO && !(X86 || UML_X86)
150 help 150 help
151 AES cipher algorithms (FIPS-197). AES uses the Rijndael 151 AES cipher algorithms (FIPS-197). AES uses the Rijndael
152 algorithm. 152 algorithm.
@@ -184,6 +184,26 @@ config CRYPTO_AES_586
184 184
185 See <http://csrc.nist.gov/encryption/aes/> for more information. 185 See <http://csrc.nist.gov/encryption/aes/> for more information.
186 186
187config CRYPTO_AES_X86_64
188 tristate "AES cipher algorithms (x86_64)"
189 depends on CRYPTO && ((X86 || UML_X86) && 64BIT)
190 help
191 AES cipher algorithms (FIPS-197). AES uses the Rijndael
192 algorithm.
193
194 Rijndael appears to be consistently a very good performer in
195 both hardware and software across a wide range of computing
196 environments regardless of its use in feedback or non-feedback
197 modes. Its key setup time is excellent, and its key agility is
198 good. Rijndael's very low memory requirements make it very well
199 suited for restricted-space environments, in which it also
200 demonstrates excellent performance. Rijndael's operations are
201 among the easiest to defend against power and timing attacks.
202
203 The AES specifies three key sizes: 128, 192 and 256 bits
204
205 See <http://csrc.nist.gov/encryption/aes/> for more information.
206
187config CRYPTO_CAST5 207config CRYPTO_CAST5
188 tristate "CAST5 (CAST-128) cipher algorithm" 208 tristate "CAST5 (CAST-128) cipher algorithm"
189 depends on CRYPTO 209 depends on CRYPTO
diff --git a/crypto/api.c b/crypto/api.c
index 394169a8577d..b4728811ce3b 100644
--- a/crypto/api.c
+++ b/crypto/api.c
@@ -13,9 +13,12 @@
13 * any later version. 13 * any later version.
14 * 14 *
15 */ 15 */
16
17#include <linux/compiler.h>
16#include <linux/init.h> 18#include <linux/init.h>
17#include <linux/crypto.h> 19#include <linux/crypto.h>
18#include <linux/errno.h> 20#include <linux/errno.h>
21#include <linux/kmod.h>
19#include <linux/rwsem.h> 22#include <linux/rwsem.h>
20#include <linux/slab.h> 23#include <linux/slab.h>
21#include "internal.h" 24#include "internal.h"
@@ -33,7 +36,7 @@ static inline void crypto_alg_put(struct crypto_alg *alg)
33 module_put(alg->cra_module); 36 module_put(alg->cra_module);
34} 37}
35 38
36struct crypto_alg *crypto_alg_lookup(const char *name) 39static struct crypto_alg *crypto_alg_lookup(const char *name)
37{ 40{
38 struct crypto_alg *q, *alg = NULL; 41 struct crypto_alg *q, *alg = NULL;
39 42
@@ -54,6 +57,13 @@ struct crypto_alg *crypto_alg_lookup(const char *name)
54 return alg; 57 return alg;
55} 58}
56 59
60/* A far more intelligent version of this is planned. For now, just
61 * try an exact match on the name of the algorithm. */
62static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
63{
64 return try_then_request_module(crypto_alg_lookup(name), name);
65}
66
57static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags) 67static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
58{ 68{
59 tfm->crt_flags = 0; 69 tfm->crt_flags = 0;
@@ -117,20 +127,46 @@ static void crypto_exit_ops(struct crypto_tfm *tfm)
117 } 127 }
118} 128}
119 129
130static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
131{
132 unsigned int len;
133
134 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
135 default:
136 BUG();
137
138 case CRYPTO_ALG_TYPE_CIPHER:
139 len = crypto_cipher_ctxsize(alg, flags);
140 break;
141
142 case CRYPTO_ALG_TYPE_DIGEST:
143 len = crypto_digest_ctxsize(alg, flags);
144 break;
145
146 case CRYPTO_ALG_TYPE_COMPRESS:
147 len = crypto_compress_ctxsize(alg, flags);
148 break;
149 }
150
151 return len + alg->cra_alignmask;
152}
153
120struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags) 154struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
121{ 155{
122 struct crypto_tfm *tfm = NULL; 156 struct crypto_tfm *tfm = NULL;
123 struct crypto_alg *alg; 157 struct crypto_alg *alg;
158 unsigned int tfm_size;
124 159
125 alg = crypto_alg_mod_lookup(name); 160 alg = crypto_alg_mod_lookup(name);
126 if (alg == NULL) 161 if (alg == NULL)
127 goto out; 162 goto out;
128 163
129 tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL); 164 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
165 tfm = kmalloc(tfm_size, GFP_KERNEL);
130 if (tfm == NULL) 166 if (tfm == NULL)
131 goto out_put; 167 goto out_put;
132 168
133 memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize); 169 memset(tfm, 0, tfm_size);
134 170
135 tfm->__crt_alg = alg; 171 tfm->__crt_alg = alg;
136 172
@@ -155,8 +191,14 @@ out:
155 191
156void crypto_free_tfm(struct crypto_tfm *tfm) 192void crypto_free_tfm(struct crypto_tfm *tfm)
157{ 193{
158 struct crypto_alg *alg = tfm->__crt_alg; 194 struct crypto_alg *alg;
159 int size = sizeof(*tfm) + alg->cra_ctxsize; 195 int size;
196
197 if (unlikely(!tfm))
198 return;
199
200 alg = tfm->__crt_alg;
201 size = sizeof(*tfm) + alg->cra_ctxsize;
160 202
161 crypto_exit_ops(tfm); 203 crypto_exit_ops(tfm);
162 crypto_alg_put(alg); 204 crypto_alg_put(alg);
@@ -168,6 +210,12 @@ int crypto_register_alg(struct crypto_alg *alg)
168{ 210{
169 int ret = 0; 211 int ret = 0;
170 struct crypto_alg *q; 212 struct crypto_alg *q;
213
214 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
215 return -EINVAL;
216
217 if (alg->cra_alignmask > PAGE_SIZE)
218 return -EINVAL;
171 219
172 down_write(&crypto_alg_sem); 220 down_write(&crypto_alg_sem);
173 221
diff --git a/crypto/cipher.c b/crypto/cipher.c
index f434ce7c2d0b..1c92c6bb138b 100644
--- a/crypto/cipher.c
+++ b/crypto/cipher.c
@@ -4,6 +4,7 @@
4 * Cipher operations. 4 * Cipher operations.
5 * 5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify it 9 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free 10 * under the terms of the GNU General Public License as published by the Free
@@ -22,10 +23,6 @@
22#include "internal.h" 23#include "internal.h"
23#include "scatterwalk.h" 24#include "scatterwalk.h"
24 25
25typedef void (cryptfn_t)(void *, u8 *, const u8 *);
26typedef void (procfn_t)(struct crypto_tfm *, u8 *,
27 u8*, cryptfn_t, void *);
28
29static inline void xor_64(u8 *a, const u8 *b) 26static inline void xor_64(u8 *a, const u8 *b)
30{ 27{
31 ((u32 *)a)[0] ^= ((u32 *)b)[0]; 28 ((u32 *)a)[0] ^= ((u32 *)b)[0];
@@ -39,63 +36,70 @@ static inline void xor_128(u8 *a, const u8 *b)
39 ((u32 *)a)[2] ^= ((u32 *)b)[2]; 36 ((u32 *)a)[2] ^= ((u32 *)b)[2];
40 ((u32 *)a)[3] ^= ((u32 *)b)[3]; 37 ((u32 *)a)[3] ^= ((u32 *)b)[3];
41} 38}
42 39
43static inline void *prepare_src(struct scatter_walk *walk, int bsize, 40static unsigned int crypt_slow(const struct cipher_desc *desc,
44 void *tmp, int in_place) 41 struct scatter_walk *in,
42 struct scatter_walk *out, unsigned int bsize)
45{ 43{
46 void *src = walk->data; 44 unsigned int alignmask = crypto_tfm_alg_alignmask(desc->tfm);
47 int n = bsize; 45 u8 buffer[bsize * 2 + alignmask];
46 u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
47 u8 *dst = src + bsize;
48 unsigned int n;
48 49
49 if (unlikely(scatterwalk_across_pages(walk, bsize))) { 50 n = scatterwalk_copychunks(src, in, bsize, 0);
50 src = tmp; 51 scatterwalk_advance(in, n);
51 n = scatterwalk_copychunks(src, walk, bsize, 0); 52
52 } 53 desc->prfn(desc, dst, src, bsize);
53 scatterwalk_advance(walk, n); 54
54 return src; 55 n = scatterwalk_copychunks(dst, out, bsize, 1);
56 scatterwalk_advance(out, n);
57
58 return bsize;
55} 59}
56 60
57static inline void *prepare_dst(struct scatter_walk *walk, int bsize, 61static inline unsigned int crypt_fast(const struct cipher_desc *desc,
58 void *tmp, int in_place) 62 struct scatter_walk *in,
63 struct scatter_walk *out,
64 unsigned int nbytes, u8 *tmp)
59{ 65{
60 void *dst = walk->data; 66 u8 *src, *dst;
61 67
62 if (unlikely(scatterwalk_across_pages(walk, bsize)) || in_place) 68 src = in->data;
69 dst = scatterwalk_samebuf(in, out) ? src : out->data;
70
71 if (tmp) {
72 memcpy(tmp, in->data, nbytes);
73 src = tmp;
63 dst = tmp; 74 dst = tmp;
64 return dst; 75 }
65}
66 76
67static inline void complete_src(struct scatter_walk *walk, int bsize, 77 nbytes = desc->prfn(desc, dst, src, nbytes);
68 void *src, int in_place)
69{
70}
71 78
72static inline void complete_dst(struct scatter_walk *walk, int bsize, 79 if (tmp)
73 void *dst, int in_place) 80 memcpy(out->data, tmp, nbytes);
74{ 81
75 int n = bsize; 82 scatterwalk_advance(in, nbytes);
83 scatterwalk_advance(out, nbytes);
76 84
77 if (unlikely(scatterwalk_across_pages(walk, bsize))) 85 return nbytes;
78 n = scatterwalk_copychunks(dst, walk, bsize, 1);
79 else if (in_place)
80 memcpy(walk->data, dst, bsize);
81 scatterwalk_advance(walk, n);
82} 86}
83 87
84/* 88/*
85 * Generic encrypt/decrypt wrapper for ciphers, handles operations across 89 * Generic encrypt/decrypt wrapper for ciphers, handles operations across
86 * multiple page boundaries by using temporary blocks. In user context, 90 * multiple page boundaries by using temporary blocks. In user context,
87 * the kernel is given a chance to schedule us once per block. 91 * the kernel is given a chance to schedule us once per page.
88 */ 92 */
89static int crypt(struct crypto_tfm *tfm, 93static int crypt(const struct cipher_desc *desc,
90 struct scatterlist *dst, 94 struct scatterlist *dst,
91 struct scatterlist *src, 95 struct scatterlist *src,
92 unsigned int nbytes, cryptfn_t crfn, 96 unsigned int nbytes)
93 procfn_t prfn, void *info)
94{ 97{
95 struct scatter_walk walk_in, walk_out; 98 struct scatter_walk walk_in, walk_out;
99 struct crypto_tfm *tfm = desc->tfm;
96 const unsigned int bsize = crypto_tfm_alg_blocksize(tfm); 100 const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
97 u8 tmp_src[bsize]; 101 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
98 u8 tmp_dst[bsize]; 102 unsigned long buffer = 0;
99 103
100 if (!nbytes) 104 if (!nbytes)
101 return 0; 105 return 0;
@@ -109,64 +113,144 @@ static int crypt(struct crypto_tfm *tfm,
109 scatterwalk_start(&walk_out, dst); 113 scatterwalk_start(&walk_out, dst);
110 114
111 for(;;) { 115 for(;;) {
112 u8 *src_p, *dst_p; 116 unsigned int n = nbytes;
113 int in_place; 117 u8 *tmp = NULL;
118
119 if (!scatterwalk_aligned(&walk_in, alignmask) ||
120 !scatterwalk_aligned(&walk_out, alignmask)) {
121 if (!buffer) {
122 buffer = __get_free_page(GFP_ATOMIC);
123 if (!buffer)
124 n = 0;
125 }
126 tmp = (u8 *)buffer;
127 }
114 128
115 scatterwalk_map(&walk_in, 0); 129 scatterwalk_map(&walk_in, 0);
116 scatterwalk_map(&walk_out, 1); 130 scatterwalk_map(&walk_out, 1);
117 131
118 in_place = scatterwalk_samebuf(&walk_in, &walk_out); 132 n = scatterwalk_clamp(&walk_in, n);
119 133 n = scatterwalk_clamp(&walk_out, n);
120 do {
121 src_p = prepare_src(&walk_in, bsize, tmp_src,
122 in_place);
123 dst_p = prepare_dst(&walk_out, bsize, tmp_dst,
124 in_place);
125
126 prfn(tfm, dst_p, src_p, crfn, info);
127 134
128 complete_src(&walk_in, bsize, src_p, in_place); 135 if (likely(n >= bsize))
129 complete_dst(&walk_out, bsize, dst_p, in_place); 136 n = crypt_fast(desc, &walk_in, &walk_out, n, tmp);
137 else
138 n = crypt_slow(desc, &walk_in, &walk_out, bsize);
130 139
131 nbytes -= bsize; 140 nbytes -= n;
132 } while (nbytes &&
133 !scatterwalk_across_pages(&walk_in, bsize) &&
134 !scatterwalk_across_pages(&walk_out, bsize));
135 141
136 scatterwalk_done(&walk_in, 0, nbytes); 142 scatterwalk_done(&walk_in, 0, nbytes);
137 scatterwalk_done(&walk_out, 1, nbytes); 143 scatterwalk_done(&walk_out, 1, nbytes);
138 144
139 if (!nbytes) 145 if (!nbytes)
140 return 0; 146 break;
141 147
142 crypto_yield(tfm); 148 crypto_yield(tfm);
143 } 149 }
150
151 if (buffer)
152 free_page(buffer);
153
154 return 0;
144} 155}
145 156
146static void cbc_process_encrypt(struct crypto_tfm *tfm, u8 *dst, u8 *src, 157static int crypt_iv_unaligned(struct cipher_desc *desc,
147 cryptfn_t fn, void *info) 158 struct scatterlist *dst,
159 struct scatterlist *src,
160 unsigned int nbytes)
148{ 161{
149 u8 *iv = info; 162 struct crypto_tfm *tfm = desc->tfm;
163 unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
164 u8 *iv = desc->info;
150 165
151 tfm->crt_u.cipher.cit_xor_block(iv, src); 166 if (unlikely(((unsigned long)iv & alignmask))) {
152 fn(crypto_tfm_ctx(tfm), dst, iv); 167 unsigned int ivsize = tfm->crt_cipher.cit_ivsize;
153 memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm)); 168 u8 buffer[ivsize + alignmask];
169 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
170 int err;
171
172 desc->info = memcpy(tmp, iv, ivsize);
173 err = crypt(desc, dst, src, nbytes);
174 memcpy(iv, tmp, ivsize);
175
176 return err;
177 }
178
179 return crypt(desc, dst, src, nbytes);
154} 180}
155 181
156static void cbc_process_decrypt(struct crypto_tfm *tfm, u8 *dst, u8 *src, 182static unsigned int cbc_process_encrypt(const struct cipher_desc *desc,
157 cryptfn_t fn, void *info) 183 u8 *dst, const u8 *src,
184 unsigned int nbytes)
158{ 185{
159 u8 *iv = info; 186 struct crypto_tfm *tfm = desc->tfm;
187 void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
188 int bsize = crypto_tfm_alg_blocksize(tfm);
189
190 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
191 u8 *iv = desc->info;
192 unsigned int done = 0;
193
194 do {
195 xor(iv, src);
196 fn(crypto_tfm_ctx(tfm), dst, iv);
197 memcpy(iv, dst, bsize);
160 198
161 fn(crypto_tfm_ctx(tfm), dst, src); 199 src += bsize;
162 tfm->crt_u.cipher.cit_xor_block(dst, iv); 200 dst += bsize;
163 memcpy(iv, src, crypto_tfm_alg_blocksize(tfm)); 201 } while ((done += bsize) < nbytes);
202
203 return done;
164} 204}
165 205
166static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src, 206static unsigned int cbc_process_decrypt(const struct cipher_desc *desc,
167 cryptfn_t fn, void *info) 207 u8 *dst, const u8 *src,
208 unsigned int nbytes)
168{ 209{
169 fn(crypto_tfm_ctx(tfm), dst, src); 210 struct crypto_tfm *tfm = desc->tfm;
211 void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
212 int bsize = crypto_tfm_alg_blocksize(tfm);
213
214 u8 stack[src == dst ? bsize : 0];
215 u8 *buf = stack;
216 u8 **dst_p = src == dst ? &buf : &dst;
217
218 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
219 u8 *iv = desc->info;
220 unsigned int done = 0;
221
222 do {
223 u8 *tmp_dst = *dst_p;
224
225 fn(crypto_tfm_ctx(tfm), tmp_dst, src);
226 xor(tmp_dst, iv);
227 memcpy(iv, src, bsize);
228 if (tmp_dst != dst)
229 memcpy(dst, tmp_dst, bsize);
230
231 src += bsize;
232 dst += bsize;
233 } while ((done += bsize) < nbytes);
234
235 return done;
236}
237
238static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst,
239 const u8 *src, unsigned int nbytes)
240{
241 struct crypto_tfm *tfm = desc->tfm;
242 int bsize = crypto_tfm_alg_blocksize(tfm);
243 void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
244 unsigned int done = 0;
245
246 do {
247 fn(crypto_tfm_ctx(tfm), dst, src);
248
249 src += bsize;
250 dst += bsize;
251 } while ((done += bsize) < nbytes);
252
253 return done;
170} 254}
171 255
172static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) 256static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
@@ -185,9 +269,14 @@ static int ecb_encrypt(struct crypto_tfm *tfm,
185 struct scatterlist *dst, 269 struct scatterlist *dst,
186 struct scatterlist *src, unsigned int nbytes) 270 struct scatterlist *src, unsigned int nbytes)
187{ 271{
188 return crypt(tfm, dst, src, nbytes, 272 struct cipher_desc desc;
189 tfm->__crt_alg->cra_cipher.cia_encrypt, 273 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
190 ecb_process, NULL); 274
275 desc.tfm = tfm;
276 desc.crfn = cipher->cia_encrypt;
277 desc.prfn = cipher->cia_encrypt_ecb ?: ecb_process;
278
279 return crypt(&desc, dst, src, nbytes);
191} 280}
192 281
193static int ecb_decrypt(struct crypto_tfm *tfm, 282static int ecb_decrypt(struct crypto_tfm *tfm,
@@ -195,9 +284,14 @@ static int ecb_decrypt(struct crypto_tfm *tfm,
195 struct scatterlist *src, 284 struct scatterlist *src,
196 unsigned int nbytes) 285 unsigned int nbytes)
197{ 286{
198 return crypt(tfm, dst, src, nbytes, 287 struct cipher_desc desc;
199 tfm->__crt_alg->cra_cipher.cia_decrypt, 288 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
200 ecb_process, NULL); 289
290 desc.tfm = tfm;
291 desc.crfn = cipher->cia_decrypt;
292 desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process;
293
294 return crypt(&desc, dst, src, nbytes);
201} 295}
202 296
203static int cbc_encrypt(struct crypto_tfm *tfm, 297static int cbc_encrypt(struct crypto_tfm *tfm,
@@ -205,9 +299,15 @@ static int cbc_encrypt(struct crypto_tfm *tfm,
205 struct scatterlist *src, 299 struct scatterlist *src,
206 unsigned int nbytes) 300 unsigned int nbytes)
207{ 301{
208 return crypt(tfm, dst, src, nbytes, 302 struct cipher_desc desc;
209 tfm->__crt_alg->cra_cipher.cia_encrypt, 303 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
210 cbc_process_encrypt, tfm->crt_cipher.cit_iv); 304
305 desc.tfm = tfm;
306 desc.crfn = cipher->cia_encrypt;
307 desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
308 desc.info = tfm->crt_cipher.cit_iv;
309
310 return crypt(&desc, dst, src, nbytes);
211} 311}
212 312
213static int cbc_encrypt_iv(struct crypto_tfm *tfm, 313static int cbc_encrypt_iv(struct crypto_tfm *tfm,
@@ -215,9 +315,15 @@ static int cbc_encrypt_iv(struct crypto_tfm *tfm,
215 struct scatterlist *src, 315 struct scatterlist *src,
216 unsigned int nbytes, u8 *iv) 316 unsigned int nbytes, u8 *iv)
217{ 317{
218 return crypt(tfm, dst, src, nbytes, 318 struct cipher_desc desc;
219 tfm->__crt_alg->cra_cipher.cia_encrypt, 319 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
220 cbc_process_encrypt, iv); 320
321 desc.tfm = tfm;
322 desc.crfn = cipher->cia_encrypt;
323 desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
324 desc.info = iv;
325
326 return crypt_iv_unaligned(&desc, dst, src, nbytes);
221} 327}
222 328
223static int cbc_decrypt(struct crypto_tfm *tfm, 329static int cbc_decrypt(struct crypto_tfm *tfm,
@@ -225,9 +331,15 @@ static int cbc_decrypt(struct crypto_tfm *tfm,
225 struct scatterlist *src, 331 struct scatterlist *src,
226 unsigned int nbytes) 332 unsigned int nbytes)
227{ 333{
228 return crypt(tfm, dst, src, nbytes, 334 struct cipher_desc desc;
229 tfm->__crt_alg->cra_cipher.cia_decrypt, 335 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
230 cbc_process_decrypt, tfm->crt_cipher.cit_iv); 336
337 desc.tfm = tfm;
338 desc.crfn = cipher->cia_decrypt;
339 desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
340 desc.info = tfm->crt_cipher.cit_iv;
341
342 return crypt(&desc, dst, src, nbytes);
231} 343}
232 344
233static int cbc_decrypt_iv(struct crypto_tfm *tfm, 345static int cbc_decrypt_iv(struct crypto_tfm *tfm,
@@ -235,9 +347,15 @@ static int cbc_decrypt_iv(struct crypto_tfm *tfm,
235 struct scatterlist *src, 347 struct scatterlist *src,
236 unsigned int nbytes, u8 *iv) 348 unsigned int nbytes, u8 *iv)
237{ 349{
238 return crypt(tfm, dst, src, nbytes, 350 struct cipher_desc desc;
239 tfm->__crt_alg->cra_cipher.cia_decrypt, 351 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
240 cbc_process_decrypt, iv); 352
353 desc.tfm = tfm;
354 desc.crfn = cipher->cia_decrypt;
355 desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
356 desc.info = iv;
357
358 return crypt_iv_unaligned(&desc, dst, src, nbytes);
241} 359}
242 360
243static int nocrypt(struct crypto_tfm *tfm, 361static int nocrypt(struct crypto_tfm *tfm,
@@ -306,6 +424,8 @@ int crypto_init_cipher_ops(struct crypto_tfm *tfm)
306 } 424 }
307 425
308 if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) { 426 if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
427 unsigned int align;
428 unsigned long addr;
309 429
310 switch (crypto_tfm_alg_blocksize(tfm)) { 430 switch (crypto_tfm_alg_blocksize(tfm)) {
311 case 8: 431 case 8:
@@ -325,9 +445,11 @@ int crypto_init_cipher_ops(struct crypto_tfm *tfm)
325 } 445 }
326 446
327 ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm); 447 ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
328 ops->cit_iv = kmalloc(ops->cit_ivsize, GFP_KERNEL); 448 align = crypto_tfm_alg_alignmask(tfm) + 1;
329 if (ops->cit_iv == NULL) 449 addr = (unsigned long)crypto_tfm_ctx(tfm);
330 ret = -ENOMEM; 450 addr = ALIGN(addr, align);
451 addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
452 ops->cit_iv = (void *)addr;
331 } 453 }
332 454
333out: 455out:
@@ -336,6 +458,4 @@ out:
336 458
337void crypto_exit_cipher_ops(struct crypto_tfm *tfm) 459void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
338{ 460{
339 if (tfm->crt_cipher.cit_iv)
340 kfree(tfm->crt_cipher.cit_iv);
341} 461}
diff --git a/crypto/des.c b/crypto/des.c
index 1c7e6de9356c..a3c863dddded 100644
--- a/crypto/des.c
+++ b/crypto/des.c
@@ -1,18 +1,9 @@
1/* 1/*
2 * Cryptographic API. 2 * Cryptographic API.
3 * 3 *
4 * DES & Triple DES EDE Cipher Algorithms. 4 * DES & Triple DES EDE Cipher Algorithms.
5 * 5 *
6 * Originally released as descore by Dana L. How <how@isl.stanford.edu>. 6 * Copyright (c) 2005 Dag Arne Osvik <da@osvik.no>
7 * Modified by Raimar Falke <rf13@inf.tu-dresden.de> for the Linux-Kernel.
8 * Derived from Cryptoapi and Nettle implementations, adapted for in-place
9 * scatterlist interface. Changed LGPL to GPL per section 3 of the LGPL.
10 *
11 * Copyright (c) 1992 Dana L. How.
12 * Copyright (c) Raimar Falke <rf13@inf.tu-dresden.de>
13 * Copyright (c) Gisle Sælensminde <gisle@ii.uib.no>
14 * Copyright (C) 2001 Niels Möller.
15 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
16 * 7 *
17 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
@@ -20,11 +11,11 @@
20 * (at your option) any later version. 11 * (at your option) any later version.
21 * 12 *
22 */ 13 */
14
15#include <linux/bitops.h>
23#include <linux/init.h> 16#include <linux/init.h>
24#include <linux/module.h> 17#include <linux/module.h>
25#include <linux/mm.h>
26#include <linux/errno.h> 18#include <linux/errno.h>
27#include <asm/scatterlist.h>
28#include <linux/crypto.h> 19#include <linux/crypto.h>
29 20
30#define DES_KEY_SIZE 8 21#define DES_KEY_SIZE 8
@@ -35,1157 +26,826 @@
35#define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS) 26#define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS)
36#define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE 27#define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE
37 28
38#define ROR(d,c,o) ((d) = (d) >> (c) | (d) << (o)) 29#define ROL(x, r) ((x) = rol32((x), (r)))
30#define ROR(x, r) ((x) = ror32((x), (r)))
39 31
40struct des_ctx { 32struct des_ctx {
41 u8 iv[DES_BLOCK_SIZE];
42 u32 expkey[DES_EXPKEY_WORDS]; 33 u32 expkey[DES_EXPKEY_WORDS];
43}; 34};
44 35
45struct des3_ede_ctx { 36struct des3_ede_ctx {
46 u8 iv[DES_BLOCK_SIZE];
47 u32 expkey[DES3_EDE_EXPKEY_WORDS]; 37 u32 expkey[DES3_EDE_EXPKEY_WORDS];
48}; 38};
49 39
50static const u32 des_keymap[] = { 40/* Lookup tables for key expansion */
51 0x02080008, 0x02082000, 0x00002008, 0x00000000, 41
52 0x02002000, 0x00080008, 0x02080000, 0x02082008, 42static const u8 pc1[256] = {
53 0x00000008, 0x02000000, 0x00082000, 0x00002008, 43 0x00, 0x00, 0x40, 0x04, 0x10, 0x10, 0x50, 0x14,
54 0x00082008, 0x02002008, 0x02000008, 0x02080000, 44 0x04, 0x40, 0x44, 0x44, 0x14, 0x50, 0x54, 0x54,
55 0x00002000, 0x00082008, 0x00080008, 0x02002000, 45 0x02, 0x02, 0x42, 0x06, 0x12, 0x12, 0x52, 0x16,
56 0x02082008, 0x02000008, 0x00000000, 0x00082000, 46 0x06, 0x42, 0x46, 0x46, 0x16, 0x52, 0x56, 0x56,
57 0x02000000, 0x00080000, 0x02002008, 0x02080008, 47 0x80, 0x08, 0xc0, 0x0c, 0x90, 0x18, 0xd0, 0x1c,
58 0x00080000, 0x00002000, 0x02082000, 0x00000008, 48 0x84, 0x48, 0xc4, 0x4c, 0x94, 0x58, 0xd4, 0x5c,
59 0x00080000, 0x00002000, 0x02000008, 0x02082008, 49 0x82, 0x0a, 0xc2, 0x0e, 0x92, 0x1a, 0xd2, 0x1e,
60 0x00002008, 0x02000000, 0x00000000, 0x00082000, 50 0x86, 0x4a, 0xc6, 0x4e, 0x96, 0x5a, 0xd6, 0x5e,
61 0x02080008, 0x02002008, 0x02002000, 0x00080008, 51 0x20, 0x20, 0x60, 0x24, 0x30, 0x30, 0x70, 0x34,
62 0x02082000, 0x00000008, 0x00080008, 0x02002000, 52 0x24, 0x60, 0x64, 0x64, 0x34, 0x70, 0x74, 0x74,
63 0x02082008, 0x00080000, 0x02080000, 0x02000008, 53 0x22, 0x22, 0x62, 0x26, 0x32, 0x32, 0x72, 0x36,
64 0x00082000, 0x00002008, 0x02002008, 0x02080000, 54 0x26, 0x62, 0x66, 0x66, 0x36, 0x72, 0x76, 0x76,
65 0x00000008, 0x02082000, 0x00082008, 0x00000000, 55 0xa0, 0x28, 0xe0, 0x2c, 0xb0, 0x38, 0xf0, 0x3c,
66 0x02000000, 0x02080008, 0x00002000, 0x00082008, 56 0xa4, 0x68, 0xe4, 0x6c, 0xb4, 0x78, 0xf4, 0x7c,
67 57 0xa2, 0x2a, 0xe2, 0x2e, 0xb2, 0x3a, 0xf2, 0x3e,
68 0x08000004, 0x00020004, 0x00000000, 0x08020200, 58 0xa6, 0x6a, 0xe6, 0x6e, 0xb6, 0x7a, 0xf6, 0x7e,
69 0x00020004, 0x00000200, 0x08000204, 0x00020000, 59 0x08, 0x80, 0x48, 0x84, 0x18, 0x90, 0x58, 0x94,
70 0x00000204, 0x08020204, 0x00020200, 0x08000000, 60 0x0c, 0xc0, 0x4c, 0xc4, 0x1c, 0xd0, 0x5c, 0xd4,
71 0x08000200, 0x08000004, 0x08020000, 0x00020204, 61 0x0a, 0x82, 0x4a, 0x86, 0x1a, 0x92, 0x5a, 0x96,
72 0x00020000, 0x08000204, 0x08020004, 0x00000000, 62 0x0e, 0xc2, 0x4e, 0xc6, 0x1e, 0xd2, 0x5e, 0xd6,
73 0x00000200, 0x00000004, 0x08020200, 0x08020004, 63 0x88, 0x88, 0xc8, 0x8c, 0x98, 0x98, 0xd8, 0x9c,
74 0x08020204, 0x08020000, 0x08000000, 0x00000204, 64 0x8c, 0xc8, 0xcc, 0xcc, 0x9c, 0xd8, 0xdc, 0xdc,
75 0x00000004, 0x00020200, 0x00020204, 0x08000200, 65 0x8a, 0x8a, 0xca, 0x8e, 0x9a, 0x9a, 0xda, 0x9e,
76 0x00000204, 0x08000000, 0x08000200, 0x00020204, 66 0x8e, 0xca, 0xce, 0xce, 0x9e, 0xda, 0xde, 0xde,
77 0x08020200, 0x00020004, 0x00000000, 0x08000200, 67 0x28, 0xa0, 0x68, 0xa4, 0x38, 0xb0, 0x78, 0xb4,
78 0x08000000, 0x00000200, 0x08020004, 0x00020000, 68 0x2c, 0xe0, 0x6c, 0xe4, 0x3c, 0xf0, 0x7c, 0xf4,
79 0x00020004, 0x08020204, 0x00020200, 0x00000004, 69 0x2a, 0xa2, 0x6a, 0xa6, 0x3a, 0xb2, 0x7a, 0xb6,
80 0x08020204, 0x00020200, 0x00020000, 0x08000204, 70 0x2e, 0xe2, 0x6e, 0xe6, 0x3e, 0xf2, 0x7e, 0xf6,
81 0x08000004, 0x08020000, 0x00020204, 0x00000000, 71 0xa8, 0xa8, 0xe8, 0xac, 0xb8, 0xb8, 0xf8, 0xbc,
82 0x00000200, 0x08000004, 0x08000204, 0x08020200, 72 0xac, 0xe8, 0xec, 0xec, 0xbc, 0xf8, 0xfc, 0xfc,
83 0x08020000, 0x00000204, 0x00000004, 0x08020004, 73 0xaa, 0xaa, 0xea, 0xae, 0xba, 0xba, 0xfa, 0xbe,
84 74 0xae, 0xea, 0xee, 0xee, 0xbe, 0xfa, 0xfe, 0xfe
85 0x80040100, 0x01000100, 0x80000000, 0x81040100,
86 0x00000000, 0x01040000, 0x81000100, 0x80040000,
87 0x01040100, 0x81000000, 0x01000000, 0x80000100,
88 0x81000000, 0x80040100, 0x00040000, 0x01000000,
89 0x81040000, 0x00040100, 0x00000100, 0x80000000,
90 0x00040100, 0x81000100, 0x01040000, 0x00000100,
91 0x80000100, 0x00000000, 0x80040000, 0x01040100,
92 0x01000100, 0x81040000, 0x81040100, 0x00040000,
93 0x81040000, 0x80000100, 0x00040000, 0x81000000,
94 0x00040100, 0x01000100, 0x80000000, 0x01040000,
95 0x81000100, 0x00000000, 0x00000100, 0x80040000,
96 0x00000000, 0x81040000, 0x01040100, 0x00000100,
97 0x01000000, 0x81040100, 0x80040100, 0x00040000,
98 0x81040100, 0x80000000, 0x01000100, 0x80040100,
99 0x80040000, 0x00040100, 0x01040000, 0x81000100,
100 0x80000100, 0x01000000, 0x81000000, 0x01040100,
101
102 0x04010801, 0x00000000, 0x00010800, 0x04010000,
103 0x04000001, 0x00000801, 0x04000800, 0x00010800,
104 0x00000800, 0x04010001, 0x00000001, 0x04000800,
105 0x00010001, 0x04010800, 0x04010000, 0x00000001,
106 0x00010000, 0x04000801, 0x04010001, 0x00000800,
107 0x00010801, 0x04000000, 0x00000000, 0x00010001,
108 0x04000801, 0x00010801, 0x04010800, 0x04000001,
109 0x04000000, 0x00010000, 0x00000801, 0x04010801,
110 0x00010001, 0x04010800, 0x04000800, 0x00010801,
111 0x04010801, 0x00010001, 0x04000001, 0x00000000,
112 0x04000000, 0x00000801, 0x00010000, 0x04010001,
113 0x00000800, 0x04000000, 0x00010801, 0x04000801,
114 0x04010800, 0x00000800, 0x00000000, 0x04000001,
115 0x00000001, 0x04010801, 0x00010800, 0x04010000,
116 0x04010001, 0x00010000, 0x00000801, 0x04000800,
117 0x04000801, 0x00000001, 0x04010000, 0x00010800,
118
119 0x00000400, 0x00000020, 0x00100020, 0x40100000,
120 0x40100420, 0x40000400, 0x00000420, 0x00000000,
121 0x00100000, 0x40100020, 0x40000020, 0x00100400,
122 0x40000000, 0x00100420, 0x00100400, 0x40000020,
123 0x40100020, 0x00000400, 0x40000400, 0x40100420,
124 0x00000000, 0x00100020, 0x40100000, 0x00000420,
125 0x40100400, 0x40000420, 0x00100420, 0x40000000,
126 0x40000420, 0x40100400, 0x00000020, 0x00100000,
127 0x40000420, 0x00100400, 0x40100400, 0x40000020,
128 0x00000400, 0x00000020, 0x00100000, 0x40100400,
129 0x40100020, 0x40000420, 0x00000420, 0x00000000,
130 0x00000020, 0x40100000, 0x40000000, 0x00100020,
131 0x00000000, 0x40100020, 0x00100020, 0x00000420,
132 0x40000020, 0x00000400, 0x40100420, 0x00100000,
133 0x00100420, 0x40000000, 0x40000400, 0x40100420,
134 0x40100000, 0x00100420, 0x00100400, 0x40000400,
135
136 0x00800000, 0x00001000, 0x00000040, 0x00801042,
137 0x00801002, 0x00800040, 0x00001042, 0x00801000,
138 0x00001000, 0x00000002, 0x00800002, 0x00001040,
139 0x00800042, 0x00801002, 0x00801040, 0x00000000,
140 0x00001040, 0x00800000, 0x00001002, 0x00000042,
141 0x00800040, 0x00001042, 0x00000000, 0x00800002,
142 0x00000002, 0x00800042, 0x00801042, 0x00001002,
143 0x00801000, 0x00000040, 0x00000042, 0x00801040,
144 0x00801040, 0x00800042, 0x00001002, 0x00801000,
145 0x00001000, 0x00000002, 0x00800002, 0x00800040,
146 0x00800000, 0x00001040, 0x00801042, 0x00000000,
147 0x00001042, 0x00800000, 0x00000040, 0x00001002,
148 0x00800042, 0x00000040, 0x00000000, 0x00801042,
149 0x00801002, 0x00801040, 0x00000042, 0x00001000,
150 0x00001040, 0x00801002, 0x00800040, 0x00000042,
151 0x00000002, 0x00001042, 0x00801000, 0x00800002,
152
153 0x10400000, 0x00404010, 0x00000010, 0x10400010,
154 0x10004000, 0x00400000, 0x10400010, 0x00004010,
155 0x00400010, 0x00004000, 0x00404000, 0x10000000,
156 0x10404010, 0x10000010, 0x10000000, 0x10404000,
157 0x00000000, 0x10004000, 0x00404010, 0x00000010,
158 0x10000010, 0x10404010, 0x00004000, 0x10400000,
159 0x10404000, 0x00400010, 0x10004010, 0x00404000,
160 0x00004010, 0x00000000, 0x00400000, 0x10004010,
161 0x00404010, 0x00000010, 0x10000000, 0x00004000,
162 0x10000010, 0x10004000, 0x00404000, 0x10400010,
163 0x00000000, 0x00404010, 0x00004010, 0x10404000,
164 0x10004000, 0x00400000, 0x10404010, 0x10000000,
165 0x10004010, 0x10400000, 0x00400000, 0x10404010,
166 0x00004000, 0x00400010, 0x10400010, 0x00004010,
167 0x00400010, 0x00000000, 0x10404000, 0x10000010,
168 0x10400000, 0x10004010, 0x00000010, 0x00404000,
169
170 0x00208080, 0x00008000, 0x20200000, 0x20208080,
171 0x00200000, 0x20008080, 0x20008000, 0x20200000,
172 0x20008080, 0x00208080, 0x00208000, 0x20000080,
173 0x20200080, 0x00200000, 0x00000000, 0x20008000,
174 0x00008000, 0x20000000, 0x00200080, 0x00008080,
175 0x20208080, 0x00208000, 0x20000080, 0x00200080,
176 0x20000000, 0x00000080, 0x00008080, 0x20208000,
177 0x00000080, 0x20200080, 0x20208000, 0x00000000,
178 0x00000000, 0x20208080, 0x00200080, 0x20008000,
179 0x00208080, 0x00008000, 0x20000080, 0x00200080,
180 0x20208000, 0x00000080, 0x00008080, 0x20200000,
181 0x20008080, 0x20000000, 0x20200000, 0x00208000,
182 0x20208080, 0x00008080, 0x00208000, 0x20200080,
183 0x00200000, 0x20000080, 0x20008000, 0x00000000,
184 0x00008000, 0x00200000, 0x20200080, 0x00208080,
185 0x20000000, 0x20208000, 0x00000080, 0x20008080,
186}; 75};
187 76
188static const u8 rotors[] = { 77static const u8 rs[256] = {
189 34, 13, 5, 46, 47, 18, 32, 41, 11, 53, 33, 20, 78 0x00, 0x00, 0x80, 0x80, 0x02, 0x02, 0x82, 0x82,
190 14, 36, 30, 24, 49, 2, 15, 37, 42, 50, 0, 21, 79 0x04, 0x04, 0x84, 0x84, 0x06, 0x06, 0x86, 0x86,
191 38, 48, 6, 26, 39, 4, 52, 25, 12, 27, 31, 40, 80 0x08, 0x08, 0x88, 0x88, 0x0a, 0x0a, 0x8a, 0x8a,
192 1, 17, 28, 29, 23, 51, 35, 7, 3, 22, 9, 43, 81 0x0c, 0x0c, 0x8c, 0x8c, 0x0e, 0x0e, 0x8e, 0x8e,
193 82 0x10, 0x10, 0x90, 0x90, 0x12, 0x12, 0x92, 0x92,
194 41, 20, 12, 53, 54, 25, 39, 48, 18, 31, 40, 27, 83 0x14, 0x14, 0x94, 0x94, 0x16, 0x16, 0x96, 0x96,
195 21, 43, 37, 0, 1, 9, 22, 44, 49, 2, 7, 28, 84 0x18, 0x18, 0x98, 0x98, 0x1a, 0x1a, 0x9a, 0x9a,
196 45, 55, 13, 33, 46, 11, 6, 32, 19, 34, 38, 47, 85 0x1c, 0x1c, 0x9c, 0x9c, 0x1e, 0x1e, 0x9e, 0x9e,
197 8, 24, 35, 36, 30, 3, 42, 14, 10, 29, 16, 50, 86 0x20, 0x20, 0xa0, 0xa0, 0x22, 0x22, 0xa2, 0xa2,
198 87 0x24, 0x24, 0xa4, 0xa4, 0x26, 0x26, 0xa6, 0xa6,
199 55, 34, 26, 38, 11, 39, 53, 5, 32, 45, 54, 41, 88 0x28, 0x28, 0xa8, 0xa8, 0x2a, 0x2a, 0xaa, 0xaa,
200 35, 2, 51, 14, 15, 23, 36, 3, 8, 16, 21, 42, 89 0x2c, 0x2c, 0xac, 0xac, 0x2e, 0x2e, 0xae, 0xae,
201 6, 12, 27, 47, 31, 25, 20, 46, 33, 48, 52, 4, 90 0x30, 0x30, 0xb0, 0xb0, 0x32, 0x32, 0xb2, 0xb2,
202 22, 7, 49, 50, 44, 17, 1, 28, 24, 43, 30, 9, 91 0x34, 0x34, 0xb4, 0xb4, 0x36, 0x36, 0xb6, 0xb6,
203 92 0x38, 0x38, 0xb8, 0xb8, 0x3a, 0x3a, 0xba, 0xba,
204 12, 48, 40, 52, 25, 53, 38, 19, 46, 6, 11, 55, 93 0x3c, 0x3c, 0xbc, 0xbc, 0x3e, 0x3e, 0xbe, 0xbe,
205 49, 16, 10, 28, 29, 37, 50, 17, 22, 30, 35, 1, 94 0x40, 0x40, 0xc0, 0xc0, 0x42, 0x42, 0xc2, 0xc2,
206 20, 26, 41, 4, 45, 39, 34, 31, 47, 5, 13, 18, 95 0x44, 0x44, 0xc4, 0xc4, 0x46, 0x46, 0xc6, 0xc6,
207 36, 21, 8, 9, 3, 0, 15, 42, 7, 2, 44, 23, 96 0x48, 0x48, 0xc8, 0xc8, 0x4a, 0x4a, 0xca, 0xca,
208 97 0x4c, 0x4c, 0xcc, 0xcc, 0x4e, 0x4e, 0xce, 0xce,
209 26, 5, 54, 13, 39, 38, 52, 33, 31, 20, 25, 12, 98 0x50, 0x50, 0xd0, 0xd0, 0x52, 0x52, 0xd2, 0xd2,
210 8, 30, 24, 42, 43, 51, 9, 0, 36, 44, 49, 15, 99 0x54, 0x54, 0xd4, 0xd4, 0x56, 0x56, 0xd6, 0xd6,
211 34, 40, 55, 18, 6, 53, 48, 45, 4, 19, 27, 32, 100 0x58, 0x58, 0xd8, 0xd8, 0x5a, 0x5a, 0xda, 0xda,
212 50, 35, 22, 23, 17, 14, 29, 1, 21, 16, 3, 37, 101 0x5c, 0x5c, 0xdc, 0xdc, 0x5e, 0x5e, 0xde, 0xde,
213 102 0x60, 0x60, 0xe0, 0xe0, 0x62, 0x62, 0xe2, 0xe2,
214 40, 19, 11, 27, 53, 52, 13, 47, 45, 34, 39, 26, 103 0x64, 0x64, 0xe4, 0xe4, 0x66, 0x66, 0xe6, 0xe6,
215 22, 44, 7, 1, 2, 10, 23, 14, 50, 3, 8, 29, 104 0x68, 0x68, 0xe8, 0xe8, 0x6a, 0x6a, 0xea, 0xea,
216 48, 54, 12, 32, 20, 38, 5, 6, 18, 33, 41, 46, 105 0x6c, 0x6c, 0xec, 0xec, 0x6e, 0x6e, 0xee, 0xee,
217 9, 49, 36, 37, 0, 28, 43, 15, 35, 30, 17, 51, 106 0x70, 0x70, 0xf0, 0xf0, 0x72, 0x72, 0xf2, 0xf2,
218 107 0x74, 0x74, 0xf4, 0xf4, 0x76, 0x76, 0xf6, 0xf6,
219 54, 33, 25, 41, 38, 13, 27, 4, 6, 48, 53, 40, 108 0x78, 0x78, 0xf8, 0xf8, 0x7a, 0x7a, 0xfa, 0xfa,
220 36, 3, 21, 15, 16, 24, 37, 28, 9, 17, 22, 43, 109 0x7c, 0x7c, 0xfc, 0xfc, 0x7e, 0x7e, 0xfe, 0xfe
221 5, 11, 26, 46, 34, 52, 19, 20, 32, 47, 55, 31,
222 23, 8, 50, 51, 14, 42, 2, 29, 49, 44, 0, 10,
223
224 11, 47, 39, 55, 52, 27, 41, 18, 20, 5, 38, 54,
225 50, 17, 35, 29, 30, 7, 51, 42, 23, 0, 36, 2,
226 19, 25, 40, 31, 48, 13, 33, 34, 46, 4, 12, 45,
227 37, 22, 9, 10, 28, 1, 16, 43, 8, 3, 14, 24,
228
229 18, 54, 46, 5, 6, 34, 48, 25, 27, 12, 45, 4,
230 2, 24, 42, 36, 37, 14, 3, 49, 30, 7, 43, 9,
231 26, 32, 47, 38, 55, 20, 40, 41, 53, 11, 19, 52,
232 44, 29, 16, 17, 35, 8, 23, 50, 15, 10, 21, 0,
233
234 32, 11, 31, 19, 20, 48, 5, 39, 41, 26, 6, 18,
235 16, 7, 1, 50, 51, 28, 17, 8, 44, 21, 2, 23,
236 40, 46, 4, 52, 12, 34, 54, 55, 38, 25, 33, 13,
237 3, 43, 30, 0, 49, 22, 37, 9, 29, 24, 35, 14,
238
239 46, 25, 45, 33, 34, 5, 19, 53, 55, 40, 20, 32,
240 30, 21, 15, 9, 10, 42, 0, 22, 3, 35, 16, 37,
241 54, 31, 18, 13, 26, 48, 11, 12, 52, 39, 47, 27,
242 17, 2, 44, 14, 8, 36, 51, 23, 43, 7, 49, 28,
243
244 31, 39, 6, 47, 48, 19, 33, 38, 12, 54, 34, 46,
245 44, 35, 29, 23, 24, 1, 14, 36, 17, 49, 30, 51,
246 11, 45, 32, 27, 40, 5, 25, 26, 13, 53, 4, 41,
247 0, 16, 3, 28, 22, 50, 10, 37, 2, 21, 8, 42,
248
249 45, 53, 20, 4, 5, 33, 47, 52, 26, 11, 48, 31,
250 3, 49, 43, 37, 7, 15, 28, 50, 0, 8, 44, 10,
251 25, 6, 46, 41, 54, 19, 39, 40, 27, 38, 18, 55,
252 14, 30, 17, 42, 36, 9, 24, 51, 16, 35, 22, 1,
253
254 6, 38, 34, 18, 19, 47, 4, 13, 40, 25, 5, 45,
255 17, 8, 2, 51, 21, 29, 42, 9, 14, 22, 3, 24,
256 39, 20, 31, 55, 11, 33, 53, 54, 41, 52, 32, 12,
257 28, 44, 0, 1, 50, 23, 7, 10, 30, 49, 36, 15,
258
259 20, 52, 48, 32, 33, 4, 18, 27, 54, 39, 19, 6,
260 0, 22, 16, 10, 35, 43, 1, 23, 28, 36, 17, 7,
261 53, 34, 45, 12, 25, 47, 38, 11, 55, 13, 46, 26,
262 42, 3, 14, 15, 9, 37, 21, 24, 44, 8, 50, 29,
263
264 27, 6, 55, 39, 40, 11, 25, 34, 4, 46, 26, 13,
265 7, 29, 23, 17, 42, 50, 8, 30, 35, 43, 24, 14,
266 31, 41, 52, 19, 32, 54, 45, 18, 5, 20, 53, 33,
267 49, 10, 21, 22, 16, 44, 28, 0, 51, 15, 2, 36,
268}; 110};
269 111
270static const u8 parity[] = { 112static const u32 pc2[1024] = {
271 8,1,0,8,0,8,8,0,0,8,8,0,8,0,2,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,3, 113 0x00000000, 0x00000000, 0x00000000, 0x00000000,
272 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8, 114 0x00040000, 0x00000000, 0x04000000, 0x00100000,
273 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8, 115 0x00400000, 0x00000008, 0x00000800, 0x40000000,
274 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0, 116 0x00440000, 0x00000008, 0x04000800, 0x40100000,
275 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8, 117 0x00000400, 0x00000020, 0x08000000, 0x00000100,
276 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0, 118 0x00040400, 0x00000020, 0x0c000000, 0x00100100,
277 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0, 119 0x00400400, 0x00000028, 0x08000800, 0x40000100,
278 4,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,5,0,8,0,8,8,0,0,8,8,0,8,0,6,8, 120 0x00440400, 0x00000028, 0x0c000800, 0x40100100,
121 0x80000000, 0x00000010, 0x00000000, 0x00800000,
122 0x80040000, 0x00000010, 0x04000000, 0x00900000,
123 0x80400000, 0x00000018, 0x00000800, 0x40800000,
124 0x80440000, 0x00000018, 0x04000800, 0x40900000,
125 0x80000400, 0x00000030, 0x08000000, 0x00800100,
126 0x80040400, 0x00000030, 0x0c000000, 0x00900100,
127 0x80400400, 0x00000038, 0x08000800, 0x40800100,
128 0x80440400, 0x00000038, 0x0c000800, 0x40900100,
129 0x10000000, 0x00000000, 0x00200000, 0x00001000,
130 0x10040000, 0x00000000, 0x04200000, 0x00101000,
131 0x10400000, 0x00000008, 0x00200800, 0x40001000,
132 0x10440000, 0x00000008, 0x04200800, 0x40101000,
133 0x10000400, 0x00000020, 0x08200000, 0x00001100,
134 0x10040400, 0x00000020, 0x0c200000, 0x00101100,
135 0x10400400, 0x00000028, 0x08200800, 0x40001100,
136 0x10440400, 0x00000028, 0x0c200800, 0x40101100,
137 0x90000000, 0x00000010, 0x00200000, 0x00801000,
138 0x90040000, 0x00000010, 0x04200000, 0x00901000,
139 0x90400000, 0x00000018, 0x00200800, 0x40801000,
140 0x90440000, 0x00000018, 0x04200800, 0x40901000,
141 0x90000400, 0x00000030, 0x08200000, 0x00801100,
142 0x90040400, 0x00000030, 0x0c200000, 0x00901100,
143 0x90400400, 0x00000038, 0x08200800, 0x40801100,
144 0x90440400, 0x00000038, 0x0c200800, 0x40901100,
145 0x00000200, 0x00080000, 0x00000000, 0x00000004,
146 0x00040200, 0x00080000, 0x04000000, 0x00100004,
147 0x00400200, 0x00080008, 0x00000800, 0x40000004,
148 0x00440200, 0x00080008, 0x04000800, 0x40100004,
149 0x00000600, 0x00080020, 0x08000000, 0x00000104,
150 0x00040600, 0x00080020, 0x0c000000, 0x00100104,
151 0x00400600, 0x00080028, 0x08000800, 0x40000104,
152 0x00440600, 0x00080028, 0x0c000800, 0x40100104,
153 0x80000200, 0x00080010, 0x00000000, 0x00800004,
154 0x80040200, 0x00080010, 0x04000000, 0x00900004,
155 0x80400200, 0x00080018, 0x00000800, 0x40800004,
156 0x80440200, 0x00080018, 0x04000800, 0x40900004,
157 0x80000600, 0x00080030, 0x08000000, 0x00800104,
158 0x80040600, 0x00080030, 0x0c000000, 0x00900104,
159 0x80400600, 0x00080038, 0x08000800, 0x40800104,
160 0x80440600, 0x00080038, 0x0c000800, 0x40900104,
161 0x10000200, 0x00080000, 0x00200000, 0x00001004,
162 0x10040200, 0x00080000, 0x04200000, 0x00101004,
163 0x10400200, 0x00080008, 0x00200800, 0x40001004,
164 0x10440200, 0x00080008, 0x04200800, 0x40101004,
165 0x10000600, 0x00080020, 0x08200000, 0x00001104,
166 0x10040600, 0x00080020, 0x0c200000, 0x00101104,
167 0x10400600, 0x00080028, 0x08200800, 0x40001104,
168 0x10440600, 0x00080028, 0x0c200800, 0x40101104,
169 0x90000200, 0x00080010, 0x00200000, 0x00801004,
170 0x90040200, 0x00080010, 0x04200000, 0x00901004,
171 0x90400200, 0x00080018, 0x00200800, 0x40801004,
172 0x90440200, 0x00080018, 0x04200800, 0x40901004,
173 0x90000600, 0x00080030, 0x08200000, 0x00801104,
174 0x90040600, 0x00080030, 0x0c200000, 0x00901104,
175 0x90400600, 0x00080038, 0x08200800, 0x40801104,
176 0x90440600, 0x00080038, 0x0c200800, 0x40901104,
177 0x00000002, 0x00002000, 0x20000000, 0x00000001,
178 0x00040002, 0x00002000, 0x24000000, 0x00100001,
179 0x00400002, 0x00002008, 0x20000800, 0x40000001,
180 0x00440002, 0x00002008, 0x24000800, 0x40100001,
181 0x00000402, 0x00002020, 0x28000000, 0x00000101,
182 0x00040402, 0x00002020, 0x2c000000, 0x00100101,
183 0x00400402, 0x00002028, 0x28000800, 0x40000101,
184 0x00440402, 0x00002028, 0x2c000800, 0x40100101,
185 0x80000002, 0x00002010, 0x20000000, 0x00800001,
186 0x80040002, 0x00002010, 0x24000000, 0x00900001,
187 0x80400002, 0x00002018, 0x20000800, 0x40800001,
188 0x80440002, 0x00002018, 0x24000800, 0x40900001,
189 0x80000402, 0x00002030, 0x28000000, 0x00800101,
190 0x80040402, 0x00002030, 0x2c000000, 0x00900101,
191 0x80400402, 0x00002038, 0x28000800, 0x40800101,
192 0x80440402, 0x00002038, 0x2c000800, 0x40900101,
193 0x10000002, 0x00002000, 0x20200000, 0x00001001,
194 0x10040002, 0x00002000, 0x24200000, 0x00101001,
195 0x10400002, 0x00002008, 0x20200800, 0x40001001,
196 0x10440002, 0x00002008, 0x24200800, 0x40101001,
197 0x10000402, 0x00002020, 0x28200000, 0x00001101,
198 0x10040402, 0x00002020, 0x2c200000, 0x00101101,
199 0x10400402, 0x00002028, 0x28200800, 0x40001101,
200 0x10440402, 0x00002028, 0x2c200800, 0x40101101,
201 0x90000002, 0x00002010, 0x20200000, 0x00801001,
202 0x90040002, 0x00002010, 0x24200000, 0x00901001,
203 0x90400002, 0x00002018, 0x20200800, 0x40801001,
204 0x90440002, 0x00002018, 0x24200800, 0x40901001,
205 0x90000402, 0x00002030, 0x28200000, 0x00801101,
206 0x90040402, 0x00002030, 0x2c200000, 0x00901101,
207 0x90400402, 0x00002038, 0x28200800, 0x40801101,
208 0x90440402, 0x00002038, 0x2c200800, 0x40901101,
209 0x00000202, 0x00082000, 0x20000000, 0x00000005,
210 0x00040202, 0x00082000, 0x24000000, 0x00100005,
211 0x00400202, 0x00082008, 0x20000800, 0x40000005,
212 0x00440202, 0x00082008, 0x24000800, 0x40100005,
213 0x00000602, 0x00082020, 0x28000000, 0x00000105,
214 0x00040602, 0x00082020, 0x2c000000, 0x00100105,
215 0x00400602, 0x00082028, 0x28000800, 0x40000105,
216 0x00440602, 0x00082028, 0x2c000800, 0x40100105,
217 0x80000202, 0x00082010, 0x20000000, 0x00800005,
218 0x80040202, 0x00082010, 0x24000000, 0x00900005,
219 0x80400202, 0x00082018, 0x20000800, 0x40800005,
220 0x80440202, 0x00082018, 0x24000800, 0x40900005,
221 0x80000602, 0x00082030, 0x28000000, 0x00800105,
222 0x80040602, 0x00082030, 0x2c000000, 0x00900105,
223 0x80400602, 0x00082038, 0x28000800, 0x40800105,
224 0x80440602, 0x00082038, 0x2c000800, 0x40900105,
225 0x10000202, 0x00082000, 0x20200000, 0x00001005,
226 0x10040202, 0x00082000, 0x24200000, 0x00101005,
227 0x10400202, 0x00082008, 0x20200800, 0x40001005,
228 0x10440202, 0x00082008, 0x24200800, 0x40101005,
229 0x10000602, 0x00082020, 0x28200000, 0x00001105,
230 0x10040602, 0x00082020, 0x2c200000, 0x00101105,
231 0x10400602, 0x00082028, 0x28200800, 0x40001105,
232 0x10440602, 0x00082028, 0x2c200800, 0x40101105,
233 0x90000202, 0x00082010, 0x20200000, 0x00801005,
234 0x90040202, 0x00082010, 0x24200000, 0x00901005,
235 0x90400202, 0x00082018, 0x20200800, 0x40801005,
236 0x90440202, 0x00082018, 0x24200800, 0x40901005,
237 0x90000602, 0x00082030, 0x28200000, 0x00801105,
238 0x90040602, 0x00082030, 0x2c200000, 0x00901105,
239 0x90400602, 0x00082038, 0x28200800, 0x40801105,
240 0x90440602, 0x00082038, 0x2c200800, 0x40901105,
241
242 0x00000000, 0x00000000, 0x00000000, 0x00000000,
243 0x00000000, 0x00000008, 0x00080000, 0x10000000,
244 0x02000000, 0x00000000, 0x00000080, 0x00001000,
245 0x02000000, 0x00000008, 0x00080080, 0x10001000,
246 0x00004000, 0x00000000, 0x00000040, 0x00040000,
247 0x00004000, 0x00000008, 0x00080040, 0x10040000,
248 0x02004000, 0x00000000, 0x000000c0, 0x00041000,
249 0x02004000, 0x00000008, 0x000800c0, 0x10041000,
250 0x00020000, 0x00008000, 0x08000000, 0x00200000,
251 0x00020000, 0x00008008, 0x08080000, 0x10200000,
252 0x02020000, 0x00008000, 0x08000080, 0x00201000,
253 0x02020000, 0x00008008, 0x08080080, 0x10201000,
254 0x00024000, 0x00008000, 0x08000040, 0x00240000,
255 0x00024000, 0x00008008, 0x08080040, 0x10240000,
256 0x02024000, 0x00008000, 0x080000c0, 0x00241000,
257 0x02024000, 0x00008008, 0x080800c0, 0x10241000,
258 0x00000000, 0x01000000, 0x00002000, 0x00000020,
259 0x00000000, 0x01000008, 0x00082000, 0x10000020,
260 0x02000000, 0x01000000, 0x00002080, 0x00001020,
261 0x02000000, 0x01000008, 0x00082080, 0x10001020,
262 0x00004000, 0x01000000, 0x00002040, 0x00040020,
263 0x00004000, 0x01000008, 0x00082040, 0x10040020,
264 0x02004000, 0x01000000, 0x000020c0, 0x00041020,
265 0x02004000, 0x01000008, 0x000820c0, 0x10041020,
266 0x00020000, 0x01008000, 0x08002000, 0x00200020,
267 0x00020000, 0x01008008, 0x08082000, 0x10200020,
268 0x02020000, 0x01008000, 0x08002080, 0x00201020,
269 0x02020000, 0x01008008, 0x08082080, 0x10201020,
270 0x00024000, 0x01008000, 0x08002040, 0x00240020,
271 0x00024000, 0x01008008, 0x08082040, 0x10240020,
272 0x02024000, 0x01008000, 0x080020c0, 0x00241020,
273 0x02024000, 0x01008008, 0x080820c0, 0x10241020,
274 0x00000400, 0x04000000, 0x00100000, 0x00000004,
275 0x00000400, 0x04000008, 0x00180000, 0x10000004,
276 0x02000400, 0x04000000, 0x00100080, 0x00001004,
277 0x02000400, 0x04000008, 0x00180080, 0x10001004,
278 0x00004400, 0x04000000, 0x00100040, 0x00040004,
279 0x00004400, 0x04000008, 0x00180040, 0x10040004,
280 0x02004400, 0x04000000, 0x001000c0, 0x00041004,
281 0x02004400, 0x04000008, 0x001800c0, 0x10041004,
282 0x00020400, 0x04008000, 0x08100000, 0x00200004,
283 0x00020400, 0x04008008, 0x08180000, 0x10200004,
284 0x02020400, 0x04008000, 0x08100080, 0x00201004,
285 0x02020400, 0x04008008, 0x08180080, 0x10201004,
286 0x00024400, 0x04008000, 0x08100040, 0x00240004,
287 0x00024400, 0x04008008, 0x08180040, 0x10240004,
288 0x02024400, 0x04008000, 0x081000c0, 0x00241004,
289 0x02024400, 0x04008008, 0x081800c0, 0x10241004,
290 0x00000400, 0x05000000, 0x00102000, 0x00000024,
291 0x00000400, 0x05000008, 0x00182000, 0x10000024,
292 0x02000400, 0x05000000, 0x00102080, 0x00001024,
293 0x02000400, 0x05000008, 0x00182080, 0x10001024,
294 0x00004400, 0x05000000, 0x00102040, 0x00040024,
295 0x00004400, 0x05000008, 0x00182040, 0x10040024,
296 0x02004400, 0x05000000, 0x001020c0, 0x00041024,
297 0x02004400, 0x05000008, 0x001820c0, 0x10041024,
298 0x00020400, 0x05008000, 0x08102000, 0x00200024,
299 0x00020400, 0x05008008, 0x08182000, 0x10200024,
300 0x02020400, 0x05008000, 0x08102080, 0x00201024,
301 0x02020400, 0x05008008, 0x08182080, 0x10201024,
302 0x00024400, 0x05008000, 0x08102040, 0x00240024,
303 0x00024400, 0x05008008, 0x08182040, 0x10240024,
304 0x02024400, 0x05008000, 0x081020c0, 0x00241024,
305 0x02024400, 0x05008008, 0x081820c0, 0x10241024,
306 0x00000800, 0x00010000, 0x20000000, 0x00000010,
307 0x00000800, 0x00010008, 0x20080000, 0x10000010,
308 0x02000800, 0x00010000, 0x20000080, 0x00001010,
309 0x02000800, 0x00010008, 0x20080080, 0x10001010,
310 0x00004800, 0x00010000, 0x20000040, 0x00040010,
311 0x00004800, 0x00010008, 0x20080040, 0x10040010,
312 0x02004800, 0x00010000, 0x200000c0, 0x00041010,
313 0x02004800, 0x00010008, 0x200800c0, 0x10041010,
314 0x00020800, 0x00018000, 0x28000000, 0x00200010,
315 0x00020800, 0x00018008, 0x28080000, 0x10200010,
316 0x02020800, 0x00018000, 0x28000080, 0x00201010,
317 0x02020800, 0x00018008, 0x28080080, 0x10201010,
318 0x00024800, 0x00018000, 0x28000040, 0x00240010,
319 0x00024800, 0x00018008, 0x28080040, 0x10240010,
320 0x02024800, 0x00018000, 0x280000c0, 0x00241010,
321 0x02024800, 0x00018008, 0x280800c0, 0x10241010,
322 0x00000800, 0x01010000, 0x20002000, 0x00000030,
323 0x00000800, 0x01010008, 0x20082000, 0x10000030,
324 0x02000800, 0x01010000, 0x20002080, 0x00001030,
325 0x02000800, 0x01010008, 0x20082080, 0x10001030,
326 0x00004800, 0x01010000, 0x20002040, 0x00040030,
327 0x00004800, 0x01010008, 0x20082040, 0x10040030,
328 0x02004800, 0x01010000, 0x200020c0, 0x00041030,
329 0x02004800, 0x01010008, 0x200820c0, 0x10041030,
330 0x00020800, 0x01018000, 0x28002000, 0x00200030,
331 0x00020800, 0x01018008, 0x28082000, 0x10200030,
332 0x02020800, 0x01018000, 0x28002080, 0x00201030,
333 0x02020800, 0x01018008, 0x28082080, 0x10201030,
334 0x00024800, 0x01018000, 0x28002040, 0x00240030,
335 0x00024800, 0x01018008, 0x28082040, 0x10240030,
336 0x02024800, 0x01018000, 0x280020c0, 0x00241030,
337 0x02024800, 0x01018008, 0x280820c0, 0x10241030,
338 0x00000c00, 0x04010000, 0x20100000, 0x00000014,
339 0x00000c00, 0x04010008, 0x20180000, 0x10000014,
340 0x02000c00, 0x04010000, 0x20100080, 0x00001014,
341 0x02000c00, 0x04010008, 0x20180080, 0x10001014,
342 0x00004c00, 0x04010000, 0x20100040, 0x00040014,
343 0x00004c00, 0x04010008, 0x20180040, 0x10040014,
344 0x02004c00, 0x04010000, 0x201000c0, 0x00041014,
345 0x02004c00, 0x04010008, 0x201800c0, 0x10041014,
346 0x00020c00, 0x04018000, 0x28100000, 0x00200014,
347 0x00020c00, 0x04018008, 0x28180000, 0x10200014,
348 0x02020c00, 0x04018000, 0x28100080, 0x00201014,
349 0x02020c00, 0x04018008, 0x28180080, 0x10201014,
350 0x00024c00, 0x04018000, 0x28100040, 0x00240014,
351 0x00024c00, 0x04018008, 0x28180040, 0x10240014,
352 0x02024c00, 0x04018000, 0x281000c0, 0x00241014,
353 0x02024c00, 0x04018008, 0x281800c0, 0x10241014,
354 0x00000c00, 0x05010000, 0x20102000, 0x00000034,
355 0x00000c00, 0x05010008, 0x20182000, 0x10000034,
356 0x02000c00, 0x05010000, 0x20102080, 0x00001034,
357 0x02000c00, 0x05010008, 0x20182080, 0x10001034,
358 0x00004c00, 0x05010000, 0x20102040, 0x00040034,
359 0x00004c00, 0x05010008, 0x20182040, 0x10040034,
360 0x02004c00, 0x05010000, 0x201020c0, 0x00041034,
361 0x02004c00, 0x05010008, 0x201820c0, 0x10041034,
362 0x00020c00, 0x05018000, 0x28102000, 0x00200034,
363 0x00020c00, 0x05018008, 0x28182000, 0x10200034,
364 0x02020c00, 0x05018000, 0x28102080, 0x00201034,
365 0x02020c00, 0x05018008, 0x28182080, 0x10201034,
366 0x00024c00, 0x05018000, 0x28102040, 0x00240034,
367 0x00024c00, 0x05018008, 0x28182040, 0x10240034,
368 0x02024c00, 0x05018000, 0x281020c0, 0x00241034,
369 0x02024c00, 0x05018008, 0x281820c0, 0x10241034
279}; 370};
280 371
372/* S-box lookup tables */
373
374static const u32 S1[64] = {
375 0x01010400, 0x00000000, 0x00010000, 0x01010404,
376 0x01010004, 0x00010404, 0x00000004, 0x00010000,
377 0x00000400, 0x01010400, 0x01010404, 0x00000400,
378 0x01000404, 0x01010004, 0x01000000, 0x00000004,
379 0x00000404, 0x01000400, 0x01000400, 0x00010400,
380 0x00010400, 0x01010000, 0x01010000, 0x01000404,
381 0x00010004, 0x01000004, 0x01000004, 0x00010004,
382 0x00000000, 0x00000404, 0x00010404, 0x01000000,
383 0x00010000, 0x01010404, 0x00000004, 0x01010000,
384 0x01010400, 0x01000000, 0x01000000, 0x00000400,
385 0x01010004, 0x00010000, 0x00010400, 0x01000004,
386 0x00000400, 0x00000004, 0x01000404, 0x00010404,
387 0x01010404, 0x00010004, 0x01010000, 0x01000404,
388 0x01000004, 0x00000404, 0x00010404, 0x01010400,
389 0x00000404, 0x01000400, 0x01000400, 0x00000000,
390 0x00010004, 0x00010400, 0x00000000, 0x01010004
391};
281 392
282static void des_small_fips_encrypt(u32 *expkey, u8 *dst, const u8 *src) 393static const u32 S2[64] = {
283{ 394 0x80108020, 0x80008000, 0x00008000, 0x00108020,
284 u32 x, y, z; 395 0x00100000, 0x00000020, 0x80100020, 0x80008020,
285 396 0x80000020, 0x80108020, 0x80108000, 0x80000000,
286 x = src[7]; 397 0x80008000, 0x00100000, 0x00000020, 0x80100020,
287 x <<= 8; 398 0x00108000, 0x00100020, 0x80008020, 0x00000000,
288 x |= src[6]; 399 0x80000000, 0x00008000, 0x00108020, 0x80100000,
289 x <<= 8; 400 0x00100020, 0x80000020, 0x00000000, 0x00108000,
290 x |= src[5]; 401 0x00008020, 0x80108000, 0x80100000, 0x00008020,
291 x <<= 8; 402 0x00000000, 0x00108020, 0x80100020, 0x00100000,
292 x |= src[4]; 403 0x80008020, 0x80100000, 0x80108000, 0x00008000,
293 y = src[3]; 404 0x80100000, 0x80008000, 0x00000020, 0x80108020,
294 y <<= 8; 405 0x00108020, 0x00000020, 0x00008000, 0x80000000,
295 y |= src[2]; 406 0x00008020, 0x80108000, 0x00100000, 0x80000020,
296 y <<= 8; 407 0x00100020, 0x80008020, 0x80000020, 0x00100020,
297 y |= src[1]; 408 0x00108000, 0x00000000, 0x80008000, 0x00008020,
298 y <<= 8; 409 0x80000000, 0x80100020, 0x80108020, 0x00108000
299 y |= src[0]; 410};
300 z = ((x >> 004) ^ y) & 0x0F0F0F0FL;
301 x ^= z << 004;
302 y ^= z;
303 z = ((y >> 020) ^ x) & 0x0000FFFFL;
304 y ^= z << 020;
305 x ^= z;
306 z = ((x >> 002) ^ y) & 0x33333333L;
307 x ^= z << 002;
308 y ^= z;
309 z = ((y >> 010) ^ x) & 0x00FF00FFL;
310 y ^= z << 010;
311 x ^= z;
312 x = x >> 1 | x << 31;
313 z = (x ^ y) & 0x55555555L;
314 y ^= z;
315 x ^= z;
316 y = y >> 1 | y << 31;
317 z = expkey[0];
318 z ^= y;
319 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
320 z >>= 8;
321 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
322 z >>= 8;
323 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
324 z >>= 8;
325 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
326 z = expkey[1];
327 z ^= y;
328 z = z << 4 | z >> 28;
329 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
330 z >>= 8;
331 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
332 z >>= 8;
333 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
334 z >>= 8;
335 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
336 z = expkey[2];
337 z ^= x;
338 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
339 z >>= 8;
340 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
341 z >>= 8;
342 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
343 z >>= 8;
344 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
345 z = expkey[3];
346 z ^= x;
347 z = z << 4 | z >> 28;
348 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
349 z >>= 8;
350 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
351 z >>= 8;
352 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
353 z >>= 8;
354 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
355 z = expkey[4];
356 z ^= y;
357 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
358 z >>= 8;
359 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
360 z >>= 8;
361 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
362 z >>= 8;
363 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
364 z = expkey[5];
365 z ^= y;
366 z = z << 4 | z >> 28;
367 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
368 z >>= 8;
369 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
370 z >>= 8;
371 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
372 z >>= 8;
373 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
374 z = expkey[6];
375 z ^= x;
376 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
377 z >>= 8;
378 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
379 z >>= 8;
380 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
381 z >>= 8;
382 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
383 z = expkey[7];
384 z ^= x;
385 z = z << 4 | z >> 28;
386 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
387 z >>= 8;
388 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
389 z >>= 8;
390 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
391 z >>= 8;
392 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
393 z = expkey[8];
394 z ^= y;
395 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
396 z >>= 8;
397 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
398 z >>= 8;
399 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
400 z >>= 8;
401 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
402 z = expkey[9];
403 z ^= y;
404 z = z << 4 | z >> 28;
405 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
406 z >>= 8;
407 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
408 z >>= 8;
409 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
410 z >>= 8;
411 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
412 z = expkey[10];
413 z ^= x;
414 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
415 z >>= 8;
416 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
417 z >>= 8;
418 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
419 z >>= 8;
420 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
421 z = expkey[11];
422 z ^= x;
423 z = z << 4 | z >> 28;
424 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
425 z >>= 8;
426 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
427 z >>= 8;
428 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
429 z >>= 8;
430 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
431 z = expkey[12];
432 z ^= y;
433 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
434 z >>= 8;
435 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
436 z >>= 8;
437 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
438 z >>= 8;
439 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
440 z = expkey[13];
441 z ^= y;
442 z = z << 4 | z >> 28;
443 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
444 z >>= 8;
445 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
446 z >>= 8;
447 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
448 z >>= 8;
449 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
450 z = expkey[14];
451 z ^= x;
452 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
453 z >>= 8;
454 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
455 z >>= 8;
456 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
457 z >>= 8;
458 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
459 z = expkey[15];
460 z ^= x;
461 z = z << 4 | z >> 28;
462 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
463 z >>= 8;
464 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
465 z >>= 8;
466 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
467 z >>= 8;
468 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
469 z = expkey[16];
470 z ^= y;
471 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
472 z >>= 8;
473 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
474 z >>= 8;
475 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
476 z >>= 8;
477 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
478 z = expkey[17];
479 z ^= y;
480 z = z << 4 | z >> 28;
481 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
482 z >>= 8;
483 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
484 z >>= 8;
485 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
486 z >>= 8;
487 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
488 z = expkey[18];
489 z ^= x;
490 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
491 z >>= 8;
492 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
493 z >>= 8;
494 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
495 z >>= 8;
496 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
497 z = expkey[19];
498 z ^= x;
499 z = z << 4 | z >> 28;
500 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
501 z >>= 8;
502 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
503 z >>= 8;
504 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
505 z >>= 8;
506 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
507 z = expkey[20];
508 z ^= y;
509 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
510 z >>= 8;
511 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
512 z >>= 8;
513 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
514 z >>= 8;
515 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
516 z = expkey[21];
517 z ^= y;
518 z = z << 4 | z >> 28;
519 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
520 z >>= 8;
521 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
522 z >>= 8;
523 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
524 z >>= 8;
525 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
526 z = expkey[22];
527 z ^= x;
528 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
529 z >>= 8;
530 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
531 z >>= 8;
532 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
533 z >>= 8;
534 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
535 z = expkey[23];
536 z ^= x;
537 z = z << 4 | z >> 28;
538 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
539 z >>= 8;
540 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
541 z >>= 8;
542 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
543 z >>= 8;
544 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
545 z = expkey[24];
546 z ^= y;
547 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
548 z >>= 8;
549 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
550 z >>= 8;
551 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
552 z >>= 8;
553 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
554 z = expkey[25];
555 z ^= y;
556 z = z << 4 | z >> 28;
557 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
558 z >>= 8;
559 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
560 z >>= 8;
561 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
562 z >>= 8;
563 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
564 z = expkey[26];
565 z ^= x;
566 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
567 z >>= 8;
568 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
569 z >>= 8;
570 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
571 z >>= 8;
572 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
573 z = expkey[27];
574 z ^= x;
575 z = z << 4 | z >> 28;
576 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
577 z >>= 8;
578 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
579 z >>= 8;
580 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
581 z >>= 8;
582 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
583 z = expkey[28];
584 z ^= y;
585 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
586 z >>= 8;
587 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
588 z >>= 8;
589 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
590 z >>= 8;
591 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
592 z = expkey[29];
593 z ^= y;
594 z = z << 4 | z >> 28;
595 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
596 z >>= 8;
597 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
598 z >>= 8;
599 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
600 z >>= 8;
601 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
602 z = expkey[30];
603 z ^= x;
604 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
605 z >>= 8;
606 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
607 z >>= 8;
608 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
609 z >>= 8;
610 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
611 z = expkey[31];
612 z ^= x;
613 z = z << 4 | z >> 28;
614 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
615 z >>= 8;
616 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
617 z >>= 8;
618 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
619 z >>= 8;
620 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
621 x = x << 1 | x >> 31;
622 z = (x ^ y) & 0x55555555L;
623 y ^= z;
624 x ^= z;
625 y = y << 1 | y >> 31;
626 z = ((x >> 010) ^ y) & 0x00FF00FFL;
627 x ^= z << 010;
628 y ^= z;
629 z = ((y >> 002) ^ x) & 0x33333333L;
630 y ^= z << 002;
631 x ^= z;
632 z = ((x >> 020) ^ y) & 0x0000FFFFL;
633 x ^= z << 020;
634 y ^= z;
635 z = ((y >> 004) ^ x) & 0x0F0F0F0FL;
636 y ^= z << 004;
637 x ^= z;
638 dst[0] = x;
639 x >>= 8;
640 dst[1] = x;
641 x >>= 8;
642 dst[2] = x;
643 x >>= 8;
644 dst[3] = x;
645 dst[4] = y;
646 y >>= 8;
647 dst[5] = y;
648 y >>= 8;
649 dst[6] = y;
650 y >>= 8;
651 dst[7] = y;
652}
653 411
654static void des_small_fips_decrypt(u32 *expkey, u8 *dst, const u8 *src) 412static const u32 S3[64] = {
655{ 413 0x00000208, 0x08020200, 0x00000000, 0x08020008,
656 u32 x, y, z; 414 0x08000200, 0x00000000, 0x00020208, 0x08000200,
657 415 0x00020008, 0x08000008, 0x08000008, 0x00020000,
658 x = src[7]; 416 0x08020208, 0x00020008, 0x08020000, 0x00000208,
659 x <<= 8; 417 0x08000000, 0x00000008, 0x08020200, 0x00000200,
660 x |= src[6]; 418 0x00020200, 0x08020000, 0x08020008, 0x00020208,
661 x <<= 8; 419 0x08000208, 0x00020200, 0x00020000, 0x08000208,
662 x |= src[5]; 420 0x00000008, 0x08020208, 0x00000200, 0x08000000,
663 x <<= 8; 421 0x08020200, 0x08000000, 0x00020008, 0x00000208,
664 x |= src[4]; 422 0x00020000, 0x08020200, 0x08000200, 0x00000000,
665 y = src[3]; 423 0x00000200, 0x00020008, 0x08020208, 0x08000200,
666 y <<= 8; 424 0x08000008, 0x00000200, 0x00000000, 0x08020008,
667 y |= src[2]; 425 0x08000208, 0x00020000, 0x08000000, 0x08020208,
668 y <<= 8; 426 0x00000008, 0x00020208, 0x00020200, 0x08000008,
669 y |= src[1]; 427 0x08020000, 0x08000208, 0x00000208, 0x08020000,
670 y <<= 8; 428 0x00020208, 0x00000008, 0x08020008, 0x00020200
671 y |= src[0]; 429};
672 z = ((x >> 004) ^ y) & 0x0F0F0F0FL; 430
673 x ^= z << 004; 431static const u32 S4[64] = {
674 y ^= z; 432 0x00802001, 0x00002081, 0x00002081, 0x00000080,
675 z = ((y >> 020) ^ x) & 0x0000FFFFL; 433 0x00802080, 0x00800081, 0x00800001, 0x00002001,
676 y ^= z << 020; 434 0x00000000, 0x00802000, 0x00802000, 0x00802081,
677 x ^= z; 435 0x00000081, 0x00000000, 0x00800080, 0x00800001,
678 z = ((x >> 002) ^ y) & 0x33333333L; 436 0x00000001, 0x00002000, 0x00800000, 0x00802001,
679 x ^= z << 002; 437 0x00000080, 0x00800000, 0x00002001, 0x00002080,
680 y ^= z; 438 0x00800081, 0x00000001, 0x00002080, 0x00800080,
681 z = ((y >> 010) ^ x) & 0x00FF00FFL; 439 0x00002000, 0x00802080, 0x00802081, 0x00000081,
682 y ^= z << 010; 440 0x00800080, 0x00800001, 0x00802000, 0x00802081,
683 x ^= z; 441 0x00000081, 0x00000000, 0x00000000, 0x00802000,
684 x = x >> 1 | x << 31; 442 0x00002080, 0x00800080, 0x00800081, 0x00000001,
685 z = (x ^ y) & 0x55555555L; 443 0x00802001, 0x00002081, 0x00002081, 0x00000080,
686 y ^= z; 444 0x00802081, 0x00000081, 0x00000001, 0x00002000,
687 x ^= z; 445 0x00800001, 0x00002001, 0x00802080, 0x00800081,
688 y = y >> 1 | y << 31; 446 0x00002001, 0x00002080, 0x00800000, 0x00802001,
689 z = expkey[31]; 447 0x00000080, 0x00800000, 0x00002000, 0x00802080
690 z ^= y; 448};
691 z = z << 4 | z >> 28; 449
692 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 450static const u32 S5[64] = {
693 z >>= 8; 451 0x00000100, 0x02080100, 0x02080000, 0x42000100,
694 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 452 0x00080000, 0x00000100, 0x40000000, 0x02080000,
695 z >>= 8; 453 0x40080100, 0x00080000, 0x02000100, 0x40080100,
696 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 454 0x42000100, 0x42080000, 0x00080100, 0x40000000,
697 z >>= 8; 455 0x02000000, 0x40080000, 0x40080000, 0x00000000,
698 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 456 0x40000100, 0x42080100, 0x42080100, 0x02000100,
699 z = expkey[30]; 457 0x42080000, 0x40000100, 0x00000000, 0x42000000,
700 z ^= y; 458 0x02080100, 0x02000000, 0x42000000, 0x00080100,
701 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 459 0x00080000, 0x42000100, 0x00000100, 0x02000000,
702 z >>= 8; 460 0x40000000, 0x02080000, 0x42000100, 0x40080100,
703 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 461 0x02000100, 0x40000000, 0x42080000, 0x02080100,
704 z >>= 8; 462 0x40080100, 0x00000100, 0x02000000, 0x42080000,
705 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 463 0x42080100, 0x00080100, 0x42000000, 0x42080100,
706 z >>= 8; 464 0x02080000, 0x00000000, 0x40080000, 0x42000000,
707 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 465 0x00080100, 0x02000100, 0x40000100, 0x00080000,
708 z = expkey[29]; 466 0x00000000, 0x40080000, 0x02080100, 0x40000100
709 z ^= x; 467};
710 z = z << 4 | z >> 28; 468
711 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 469static const u32 S6[64] = {
712 z >>= 8; 470 0x20000010, 0x20400000, 0x00004000, 0x20404010,
713 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 471 0x20400000, 0x00000010, 0x20404010, 0x00400000,
714 z >>= 8; 472 0x20004000, 0x00404010, 0x00400000, 0x20000010,
715 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 473 0x00400010, 0x20004000, 0x20000000, 0x00004010,
716 z >>= 8; 474 0x00000000, 0x00400010, 0x20004010, 0x00004000,
717 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 475 0x00404000, 0x20004010, 0x00000010, 0x20400010,
718 z = expkey[28]; 476 0x20400010, 0x00000000, 0x00404010, 0x20404000,
719 z ^= x; 477 0x00004010, 0x00404000, 0x20404000, 0x20000000,
720 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 478 0x20004000, 0x00000010, 0x20400010, 0x00404000,
721 z >>= 8; 479 0x20404010, 0x00400000, 0x00004010, 0x20000010,
722 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 480 0x00400000, 0x20004000, 0x20000000, 0x00004010,
723 z >>= 8; 481 0x20000010, 0x20404010, 0x00404000, 0x20400000,
724 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 482 0x00404010, 0x20404000, 0x00000000, 0x20400010,
725 z >>= 8; 483 0x00000010, 0x00004000, 0x20400000, 0x00404010,
726 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 484 0x00004000, 0x00400010, 0x20004010, 0x00000000,
727 z = expkey[27]; 485 0x20404000, 0x20000000, 0x00400010, 0x20004010
728 z ^= y; 486};
729 z = z << 4 | z >> 28; 487
730 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 488static const u32 S7[64] = {
731 z >>= 8; 489 0x00200000, 0x04200002, 0x04000802, 0x00000000,
732 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 490 0x00000800, 0x04000802, 0x00200802, 0x04200800,
733 z >>= 8; 491 0x04200802, 0x00200000, 0x00000000, 0x04000002,
734 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 492 0x00000002, 0x04000000, 0x04200002, 0x00000802,
735 z >>= 8; 493 0x04000800, 0x00200802, 0x00200002, 0x04000800,
736 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 494 0x04000002, 0x04200000, 0x04200800, 0x00200002,
737 z = expkey[26]; 495 0x04200000, 0x00000800, 0x00000802, 0x04200802,
738 z ^= y; 496 0x00200800, 0x00000002, 0x04000000, 0x00200800,
739 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 497 0x04000000, 0x00200800, 0x00200000, 0x04000802,
740 z >>= 8; 498 0x04000802, 0x04200002, 0x04200002, 0x00000002,
741 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 499 0x00200002, 0x04000000, 0x04000800, 0x00200000,
742 z >>= 8; 500 0x04200800, 0x00000802, 0x00200802, 0x04200800,
743 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 501 0x00000802, 0x04000002, 0x04200802, 0x04200000,
744 z >>= 8; 502 0x00200800, 0x00000000, 0x00000002, 0x04200802,
745 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 503 0x00000000, 0x00200802, 0x04200000, 0x00000800,
746 z = expkey[25]; 504 0x04000002, 0x04000800, 0x00000800, 0x00200002
747 z ^= x; 505};
748 z = z << 4 | z >> 28; 506
749 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 507static const u32 S8[64] = {
750 z >>= 8; 508 0x10001040, 0x00001000, 0x00040000, 0x10041040,
751 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 509 0x10000000, 0x10001040, 0x00000040, 0x10000000,
752 z >>= 8; 510 0x00040040, 0x10040000, 0x10041040, 0x00041000,
753 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 511 0x10041000, 0x00041040, 0x00001000, 0x00000040,
754 z >>= 8; 512 0x10040000, 0x10000040, 0x10001000, 0x00001040,
755 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 513 0x00041000, 0x00040040, 0x10040040, 0x10041000,
756 z = expkey[24]; 514 0x00001040, 0x00000000, 0x00000000, 0x10040040,
757 z ^= x; 515 0x10000040, 0x10001000, 0x00041040, 0x00040000,
758 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 516 0x00041040, 0x00040000, 0x10041000, 0x00001000,
759 z >>= 8; 517 0x00000040, 0x10040040, 0x00001000, 0x00041040,
760 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 518 0x10001000, 0x00000040, 0x10000040, 0x10040000,
761 z >>= 8; 519 0x10040040, 0x10000000, 0x00040000, 0x10001040,
762 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 520 0x00000000, 0x10041040, 0x00040040, 0x10000040,
763 z >>= 8; 521 0x10040000, 0x10001000, 0x10001040, 0x00000000,
764 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 522 0x10041040, 0x00041000, 0x00041000, 0x00001040,
765 z = expkey[23]; 523 0x00001040, 0x00040040, 0x10000000, 0x10041000
766 z ^= y; 524};
767 z = z << 4 | z >> 28; 525
768 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 526/* Encryption components: IP, FP, and round function */
769 z >>= 8; 527
770 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 528#define IP(L, R, T) \
771 z >>= 8; 529 ROL(R, 4); \
772 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 530 T = L; \
773 z >>= 8; 531 L ^= R; \
774 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 532 L &= 0xf0f0f0f0; \
775 z = expkey[22]; 533 R ^= L; \
776 z ^= y; 534 L ^= T; \
777 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 535 ROL(R, 12); \
778 z >>= 8; 536 T = L; \
779 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 537 L ^= R; \
780 z >>= 8; 538 L &= 0xffff0000; \
781 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 539 R ^= L; \
782 z >>= 8; 540 L ^= T; \
783 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 541 ROR(R, 14); \
784 z = expkey[21]; 542 T = L; \
785 z ^= x; 543 L ^= R; \
786 z = z << 4 | z >> 28; 544 L &= 0xcccccccc; \
787 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 545 R ^= L; \
788 z >>= 8; 546 L ^= T; \
789 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 547 ROL(R, 6); \
790 z >>= 8; 548 T = L; \
791 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 549 L ^= R; \
792 z >>= 8; 550 L &= 0xff00ff00; \
793 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 551 R ^= L; \
794 z = expkey[20]; 552 L ^= T; \
795 z ^= x; 553 ROR(R, 7); \
796 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 554 T = L; \
797 z >>= 8; 555 L ^= R; \
798 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 556 L &= 0xaaaaaaaa; \
799 z >>= 8; 557 R ^= L; \
800 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 558 L ^= T; \
801 z >>= 8; 559 ROL(L, 1);
802 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 560
803 z = expkey[19]; 561#define FP(L, R, T) \
804 z ^= y; 562 ROR(L, 1); \
805 z = z << 4 | z >> 28; 563 T = L; \
806 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 564 L ^= R; \
807 z >>= 8; 565 L &= 0xaaaaaaaa; \
808 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 566 R ^= L; \
809 z >>= 8; 567 L ^= T; \
810 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 568 ROL(R, 7); \
811 z >>= 8; 569 T = L; \
812 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 570 L ^= R; \
813 z = expkey[18]; 571 L &= 0xff00ff00; \
814 z ^= y; 572 R ^= L; \
815 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 573 L ^= T; \
816 z >>= 8; 574 ROR(R, 6); \
817 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 575 T = L; \
818 z >>= 8; 576 L ^= R; \
819 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 577 L &= 0xcccccccc; \
820 z >>= 8; 578 R ^= L; \
821 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 579 L ^= T; \
822 z = expkey[17]; 580 ROL(R, 14); \
823 z ^= x; 581 T = L; \
824 z = z << 4 | z >> 28; 582 L ^= R; \
825 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 583 L &= 0xffff0000; \
826 z >>= 8; 584 R ^= L; \
827 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 585 L ^= T; \
828 z >>= 8; 586 ROR(R, 12); \
829 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 587 T = L; \
830 z >>= 8; 588 L ^= R; \
831 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 589 L &= 0xf0f0f0f0; \
832 z = expkey[16]; 590 R ^= L; \
833 z ^= x; 591 L ^= T; \
834 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 592 ROR(R, 4);
835 z >>= 8; 593
836 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 594#define ROUND(L, R, A, B, K, d) \
837 z >>= 8; 595 B = K[0]; A = K[1]; K += d; \
838 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 596 B ^= R; A ^= R; \
839 z >>= 8; 597 B &= 0x3f3f3f3f; ROR(A, 4); \
840 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 598 L ^= S8[0xff & B]; A &= 0x3f3f3f3f; \
841 z = expkey[15]; 599 L ^= S6[0xff & (B >> 8)]; B >>= 16; \
842 z ^= y; 600 L ^= S7[0xff & A]; \
843 z = z << 4 | z >> 28; 601 L ^= S5[0xff & (A >> 8)]; A >>= 16; \
844 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 602 L ^= S4[0xff & B]; \
845 z >>= 8; 603 L ^= S2[0xff & (B >> 8)]; \
846 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z)); 604 L ^= S3[0xff & A]; \
847 z >>= 8; 605 L ^= S1[0xff & (A >> 8)];
848 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z)); 606
849 z >>= 8; 607/*
850 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z)); 608 * PC2 lookup tables are organized as 2 consecutive sets of 4 interleaved
851 z = expkey[14]; 609 * tables of 128 elements. One set is for C_i and the other for D_i, while
852 z ^= y; 610 * the 4 interleaved tables correspond to four 7-bit subsets of C_i or D_i.
853 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z)); 611 *
854 z >>= 8; 612 * After PC1 each of the variables a,b,c,d contains a 7 bit subset of C_i
855 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z)); 613 * or D_i in bits 7-1 (bit 0 being the least significant).
856 z >>= 8; 614 */
857 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z)); 615
858 z >>= 8; 616#define T1(x) pt[2 * (x) + 0]
859 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z)); 617#define T2(x) pt[2 * (x) + 1]
860 z = expkey[13]; 618#define T3(x) pt[2 * (x) + 2]
861 z ^= x; 619#define T4(x) pt[2 * (x) + 3]
862 z = z << 4 | z >> 28; 620
863 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z)); 621#define PC2(a, b, c, d) (T4(d) | T3(c) | T2(b) | T1(a))
864 z >>= 8;
865 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
866 z >>= 8;
867 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
868 z >>= 8;
869 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
870 z = expkey[12];
871 z ^= x;
872 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
873 z >>= 8;
874 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
875 z >>= 8;
876 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
877 z >>= 8;
878 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
879 z = expkey[11];
880 z ^= y;
881 z = z << 4 | z >> 28;
882 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
883 z >>= 8;
884 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
885 z >>= 8;
886 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
887 z >>= 8;
888 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
889 z = expkey[10];
890 z ^= y;
891 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
892 z >>= 8;
893 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
894 z >>= 8;
895 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
896 z >>= 8;
897 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
898 z = expkey[9];
899 z ^= x;
900 z = z << 4 | z >> 28;
901 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
902 z >>= 8;
903 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
904 z >>= 8;
905 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
906 z >>= 8;
907 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
908 z = expkey[8];
909 z ^= x;
910 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
911 z >>= 8;
912 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
913 z >>= 8;
914 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
915 z >>= 8;
916 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
917 z = expkey[7];
918 z ^= y;
919 z = z << 4 | z >> 28;
920 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
921 z >>= 8;
922 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
923 z >>= 8;
924 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
925 z >>= 8;
926 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
927 z = expkey[6];
928 z ^= y;
929 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
930 z >>= 8;
931 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
932 z >>= 8;
933 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
934 z >>= 8;
935 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
936 z = expkey[5];
937 z ^= x;
938 z = z << 4 | z >> 28;
939 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
940 z >>= 8;
941 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
942 z >>= 8;
943 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
944 z >>= 8;
945 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
946 z = expkey[4];
947 z ^= x;
948 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
949 z >>= 8;
950 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
951 z >>= 8;
952 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
953 z >>= 8;
954 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
955 z = expkey[3];
956 z ^= y;
957 z = z << 4 | z >> 28;
958 x ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
959 z >>= 8;
960 x ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
961 z >>= 8;
962 x ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
963 z >>= 8;
964 x ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
965 z = expkey[2];
966 z ^= y;
967 x ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
968 z >>= 8;
969 x ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
970 z >>= 8;
971 x ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
972 z >>= 8;
973 x ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
974 z = expkey[1];
975 z ^= x;
976 z = z << 4 | z >> 28;
977 y ^= * (u32 *) ((u8 *) (des_keymap + 448) + (0xFC & z));
978 z >>= 8;
979 y ^= * (u32 *) ((u8 *) (des_keymap + 384) + (0xFC & z));
980 z >>= 8;
981 y ^= * (u32 *) ((u8 *) (des_keymap + 320) + (0xFC & z));
982 z >>= 8;
983 y ^= * (u32 *) ((u8 *) (des_keymap + 256) + (0xFC & z));
984 z = expkey[0];
985 z ^= x;
986 y ^= * (u32 *) ((u8 *) (des_keymap + 192) + (0xFC & z));
987 z >>= 8;
988 y ^= * (u32 *) ((u8 *) (des_keymap + 128) + (0xFC & z));
989 z >>= 8;
990 y ^= * (u32 *) ((u8 *) (des_keymap + 64) + (0xFC & z));
991 z >>= 8;
992 y ^= * (u32 *) ((u8 *) des_keymap + (0xFC & z));
993 x = x << 1 | x >> 31;
994 z = (x ^ y) & 0x55555555L;
995 y ^= z;
996 x ^= z;
997 y = y << 1 | y >> 31;
998 z = ((x >> 010) ^ y) & 0x00FF00FFL;
999 x ^= z << 010;
1000 y ^= z;
1001 z = ((y >> 002) ^ x) & 0x33333333L;
1002 y ^= z << 002;
1003 x ^= z;
1004 z = ((x >> 020) ^ y) & 0x0000FFFFL;
1005 x ^= z << 020;
1006 y ^= z;
1007 z = ((y >> 004) ^ x) & 0x0F0F0F0FL;
1008 y ^= z << 004;
1009 x ^= z;
1010 dst[0] = x;
1011 x >>= 8;
1012 dst[1] = x;
1013 x >>= 8;
1014 dst[2] = x;
1015 x >>= 8;
1016 dst[3] = x;
1017 dst[4] = y;
1018 y >>= 8;
1019 dst[5] = y;
1020 y >>= 8;
1021 dst[6] = y;
1022 y >>= 8;
1023 dst[7] = y;
1024}
1025 622
1026/* 623/*
624 * Encryption key expansion
625 *
1027 * RFC2451: Weak key checks SHOULD be performed. 626 * RFC2451: Weak key checks SHOULD be performed.
627 *
628 * FIPS 74:
629 *
630 * Keys having duals are keys which produce all zeros, all ones, or
631 * alternating zero-one patterns in the C and D registers after Permuted
632 * Choice 1 has operated on the key.
633 *
1028 */ 634 */
1029static int setkey(u32 *expkey, const u8 *key, unsigned int keylen, u32 *flags) 635static unsigned long ekey(u32 *pe, const u8 *k)
1030{ 636{
1031 const u8 *k; 637 /* K&R: long is at least 32 bits */
1032 u8 *b0, *b1; 638 unsigned long a, b, c, d, w;
1033 u32 n, w; 639 const u32 *pt = pc2;
1034 u8 bits0[56], bits1[56]; 640
1035 641 d = k[4]; d &= 0x0e; d <<= 4; d |= k[0] & 0x1e; d = pc1[d];
1036 n = parity[key[0]]; n <<= 4; 642 c = k[5]; c &= 0x0e; c <<= 4; c |= k[1] & 0x1e; c = pc1[c];
1037 n |= parity[key[1]]; n <<= 4; 643 b = k[6]; b &= 0x0e; b <<= 4; b |= k[2] & 0x1e; b = pc1[b];
1038 n |= parity[key[2]]; n <<= 4; 644 a = k[7]; a &= 0x0e; a <<= 4; a |= k[3] & 0x1e; a = pc1[a];
1039 n |= parity[key[3]]; n <<= 4; 645
1040 n |= parity[key[4]]; n <<= 4; 646 pe[15 * 2 + 0] = PC2(a, b, c, d); d = rs[d];
1041 n |= parity[key[5]]; n <<= 4; 647 pe[14 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
1042 n |= parity[key[6]]; n <<= 4; 648 pe[13 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
1043 n |= parity[key[7]]; 649 pe[12 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
1044 w = 0x88888888L; 650 pe[11 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
1045 651 pe[10 * 2 + 0] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
1046 if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY) 652 pe[ 9 * 2 + 0] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
1047 && !((n - (w >> 3)) & w)) { /* 1 in 10^10 keys passes this test */ 653 pe[ 8 * 2 + 0] = PC2(d, a, b, c); c = rs[c];
1048 if (n < 0x41415151) { 654 pe[ 7 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
1049 if (n < 0x31312121) { 655 pe[ 6 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
1050 if (n < 0x14141515) { 656 pe[ 5 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
1051 /* 01 01 01 01 01 01 01 01 */ 657 pe[ 4 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
1052 if (n == 0x11111111) goto weak; 658 pe[ 3 * 2 + 0] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
1053 /* 01 1F 01 1F 01 0E 01 0E */ 659 pe[ 2 * 2 + 0] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
1054 if (n == 0x13131212) goto weak; 660 pe[ 1 * 2 + 0] = PC2(c, d, a, b); b = rs[b];
1055 } else { 661 pe[ 0 * 2 + 0] = PC2(b, c, d, a);
1056 /* 01 E0 01 E0 01 F1 01 F1 */ 662
1057 if (n == 0x14141515) goto weak; 663 /* Check if first half is weak */
1058 /* 01 FE 01 FE 01 FE 01 FE */ 664 w = (a ^ c) | (b ^ d) | (rs[a] ^ c) | (b ^ rs[d]);
1059 if (n == 0x16161616) goto weak; 665
1060 } 666 /* Skip to next table set */
1061 } else { 667 pt += 512;
1062 if (n < 0x34342525) { 668
1063 /* 1F 01 1F 01 0E 01 0E 01 */ 669 d = k[0]; d &= 0xe0; d >>= 4; d |= k[4] & 0xf0; d = pc1[d + 1];
1064 if (n == 0x31312121) goto weak; 670 c = k[1]; c &= 0xe0; c >>= 4; c |= k[5] & 0xf0; c = pc1[c + 1];
1065 /* 1F 1F 1F 1F 0E 0E 0E 0E (?) */ 671 b = k[2]; b &= 0xe0; b >>= 4; b |= k[6] & 0xf0; b = pc1[b + 1];
1066 if (n == 0x33332222) goto weak; 672 a = k[3]; a &= 0xe0; a >>= 4; a |= k[7] & 0xf0; a = pc1[a + 1];
1067 } else { 673
1068 /* 1F E0 1F E0 0E F1 0E F1 */ 674 /* Check if second half is weak */
1069 if (n == 0x34342525) goto weak; 675 w |= (a ^ c) | (b ^ d) | (rs[a] ^ c) | (b ^ rs[d]);
1070 /* 1F FE 1F FE 0E FE 0E FE */ 676
1071 if (n == 0x36362626) goto weak; 677 pe[15 * 2 + 1] = PC2(a, b, c, d); d = rs[d];
1072 } 678 pe[14 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
1073 } 679 pe[13 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
1074 } else { 680 pe[12 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
1075 if (n < 0x61616161) { 681 pe[11 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
1076 if (n < 0x44445555) { 682 pe[10 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
1077 /* E0 01 E0 01 F1 01 F1 01 */ 683 pe[ 9 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
1078 if (n == 0x41415151) goto weak; 684 pe[ 8 * 2 + 1] = PC2(d, a, b, c); c = rs[c];
1079 /* E0 1F E0 1F F1 0E F1 0E */ 685 pe[ 7 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
1080 if (n == 0x43435252) goto weak; 686 pe[ 6 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
1081 } else { 687 pe[ 5 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
1082 /* E0 E0 E0 E0 F1 F1 F1 F1 (?) */ 688 pe[ 4 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
1083 if (n == 0x44445555) goto weak; 689 pe[ 3 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
1084 /* E0 FE E0 FE F1 FE F1 FE */ 690 pe[ 2 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
1085 if (n == 0x46465656) goto weak; 691 pe[ 1 * 2 + 1] = PC2(c, d, a, b); b = rs[b];
1086 } 692 pe[ 0 * 2 + 1] = PC2(b, c, d, a);
1087 } else { 693
1088 if (n < 0x64646565) { 694 /* Fixup: 2413 5768 -> 1357 2468 */
1089 /* FE 01 FE 01 FE 01 FE 01 */ 695 for (d = 0; d < 16; ++d) {
1090 if (n == 0x61616161) goto weak; 696 a = pe[2 * d];
1091 /* FE 1F FE 1F FE 0E FE 0E */ 697 b = pe[2 * d + 1];
1092 if (n == 0x63636262) goto weak; 698 c = a ^ b;
1093 } else { 699 c &= 0xffff0000;
1094 /* FE E0 FE E0 FE F1 FE F1 */ 700 a ^= c;
1095 if (n == 0x64646565) goto weak; 701 b ^= c;
1096 /* FE FE FE FE FE FE FE FE */ 702 ROL(b, 18);
1097 if (n == 0x66666666) goto weak; 703 pe[2 * d] = a;
1098 } 704 pe[2 * d + 1] = b;
1099 }
1100 }
1101
1102 goto not_weak;
1103weak:
1104 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
1105 return -EINVAL;
1106 } 705 }
1107 706
1108not_weak: 707 /* Zero if weak key */
1109 708 return w;
1110 /* explode the bits */ 709}
1111 n = 56;
1112 b0 = bits0;
1113 b1 = bits1;
1114
1115 do {
1116 w = (256 | *key++) << 2;
1117 do {
1118 --n;
1119 b1[n] = 8 & w;
1120 w >>= 1;
1121 b0[n] = 4 & w;
1122 } while ( w >= 16 );
1123 } while ( n );
1124
1125 /* put the bits in the correct places */
1126 n = 16;
1127 k = rotors;
1128
1129 do {
1130 w = (b1[k[ 0 ]] | b0[k[ 1 ]]) << 4;
1131 w |= (b1[k[ 2 ]] | b0[k[ 3 ]]) << 2;
1132 w |= b1[k[ 4 ]] | b0[k[ 5 ]];
1133 w <<= 8;
1134 w |= (b1[k[ 6 ]] | b0[k[ 7 ]]) << 4;
1135 w |= (b1[k[ 8 ]] | b0[k[ 9 ]]) << 2;
1136 w |= b1[k[10 ]] | b0[k[11 ]];
1137 w <<= 8;
1138 w |= (b1[k[12 ]] | b0[k[13 ]]) << 4;
1139 w |= (b1[k[14 ]] | b0[k[15 ]]) << 2;
1140 w |= b1[k[16 ]] | b0[k[17 ]];
1141 w <<= 8;
1142 w |= (b1[k[18 ]] | b0[k[19 ]]) << 4;
1143 w |= (b1[k[20 ]] | b0[k[21 ]]) << 2;
1144 w |= b1[k[22 ]] | b0[k[23 ]];
1145 expkey[0] = w;
1146
1147 w = (b1[k[ 0+24]] | b0[k[ 1+24]]) << 4;
1148 w |= (b1[k[ 2+24]] | b0[k[ 3+24]]) << 2;
1149 w |= b1[k[ 4+24]] | b0[k[ 5+24]];
1150 w <<= 8;
1151 w |= (b1[k[ 6+24]] | b0[k[ 7+24]]) << 4;
1152 w |= (b1[k[ 8+24]] | b0[k[ 9+24]]) << 2;
1153 w |= b1[k[10+24]] | b0[k[11+24]];
1154 w <<= 8;
1155 w |= (b1[k[12+24]] | b0[k[13+24]]) << 4;
1156 w |= (b1[k[14+24]] | b0[k[15+24]]) << 2;
1157 w |= b1[k[16+24]] | b0[k[17+24]];
1158 w <<= 8;
1159 w |= (b1[k[18+24]] | b0[k[19+24]]) << 4;
1160 w |= (b1[k[20+24]] | b0[k[21+24]]) << 2;
1161 w |= b1[k[22+24]] | b0[k[23+24]];
1162
1163 ROR(w, 4, 28); /* could be eliminated */
1164 expkey[1] = w;
1165
1166 k += 48;
1167 expkey += 2;
1168 } while (--n);
1169 710
1170 return 0; 711/*
712 * Decryption key expansion
713 *
714 * No weak key checking is performed, as this is only used by triple DES
715 *
716 */
717static void dkey(u32 *pe, const u8 *k)
718{
719 /* K&R: long is at least 32 bits */
720 unsigned long a, b, c, d;
721 const u32 *pt = pc2;
722
723 d = k[4]; d &= 0x0e; d <<= 4; d |= k[0] & 0x1e; d = pc1[d];
724 c = k[5]; c &= 0x0e; c <<= 4; c |= k[1] & 0x1e; c = pc1[c];
725 b = k[6]; b &= 0x0e; b <<= 4; b |= k[2] & 0x1e; b = pc1[b];
726 a = k[7]; a &= 0x0e; a <<= 4; a |= k[3] & 0x1e; a = pc1[a];
727
728 pe[ 0 * 2] = PC2(a, b, c, d); d = rs[d];
729 pe[ 1 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
730 pe[ 2 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
731 pe[ 3 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
732 pe[ 4 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
733 pe[ 5 * 2] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
734 pe[ 6 * 2] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
735 pe[ 7 * 2] = PC2(d, a, b, c); c = rs[c];
736 pe[ 8 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
737 pe[ 9 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
738 pe[10 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
739 pe[11 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
740 pe[12 * 2] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
741 pe[13 * 2] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
742 pe[14 * 2] = PC2(c, d, a, b); b = rs[b];
743 pe[15 * 2] = PC2(b, c, d, a);
744
745 /* Skip to next table set */
746 pt += 512;
747
748 d = k[0]; d &= 0xe0; d >>= 4; d |= k[4] & 0xf0; d = pc1[d + 1];
749 c = k[1]; c &= 0xe0; c >>= 4; c |= k[5] & 0xf0; c = pc1[c + 1];
750 b = k[2]; b &= 0xe0; b >>= 4; b |= k[6] & 0xf0; b = pc1[b + 1];
751 a = k[3]; a &= 0xe0; a >>= 4; a |= k[7] & 0xf0; a = pc1[a + 1];
752
753 pe[ 0 * 2 + 1] = PC2(a, b, c, d); d = rs[d];
754 pe[ 1 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
755 pe[ 2 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
756 pe[ 3 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
757 pe[ 4 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
758 pe[ 5 * 2 + 1] = PC2(d, a, b, c); c = rs[c]; b = rs[b];
759 pe[ 6 * 2 + 1] = PC2(b, c, d, a); a = rs[a]; d = rs[d];
760 pe[ 7 * 2 + 1] = PC2(d, a, b, c); c = rs[c];
761 pe[ 8 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
762 pe[ 9 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
763 pe[10 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
764 pe[11 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
765 pe[12 * 2 + 1] = PC2(c, d, a, b); b = rs[b]; a = rs[a];
766 pe[13 * 2 + 1] = PC2(a, b, c, d); d = rs[d]; c = rs[c];
767 pe[14 * 2 + 1] = PC2(c, d, a, b); b = rs[b];
768 pe[15 * 2 + 1] = PC2(b, c, d, a);
769
770 /* Fixup: 2413 5768 -> 1357 2468 */
771 for (d = 0; d < 16; ++d) {
772 a = pe[2 * d];
773 b = pe[2 * d + 1];
774 c = a ^ b;
775 c &= 0xffff0000;
776 a ^= c;
777 b ^= c;
778 ROL(b, 18);
779 pe[2 * d] = a;
780 pe[2 * d + 1] = b;
781 }
1171} 782}
1172 783
1173static int des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) 784static int des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
1174{ 785{
1175 return setkey(((struct des_ctx *)ctx)->expkey, key, keylen, flags); 786 struct des_ctx *dctx = ctx;
787 u32 tmp[DES_EXPKEY_WORDS];
788 int ret;
789
790 /* Expand to tmp */
791 ret = ekey(tmp, key);
792
793 if (unlikely(ret == 0) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
794 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
795 return -EINVAL;
796 }
797
798 /* Copy to output */
799 memcpy(dctx->expkey, tmp, sizeof(dctx->expkey));
800
801 return 0;
1176} 802}
1177 803
1178static void des_encrypt(void *ctx, u8 *dst, const u8 *src) 804static void des_encrypt(void *ctx, u8 *dst, const u8 *src)
1179{ 805{
1180 des_small_fips_encrypt(((struct des_ctx *)ctx)->expkey, dst, src); 806 const u32 *K = ((struct des_ctx *)ctx)->expkey;
807 const __le32 *s = (const __le32 *)src;
808 __le32 *d = (__le32 *)dst;
809 u32 L, R, A, B;
810 int i;
811
812 L = le32_to_cpu(s[0]);
813 R = le32_to_cpu(s[1]);
814
815 IP(L, R, A);
816 for (i = 0; i < 8; i++) {
817 ROUND(L, R, A, B, K, 2);
818 ROUND(R, L, A, B, K, 2);
819 }
820 FP(R, L, A);
821
822 d[0] = cpu_to_le32(R);
823 d[1] = cpu_to_le32(L);
1181} 824}
1182 825
1183static void des_decrypt(void *ctx, u8 *dst, const u8 *src) 826static void des_decrypt(void *ctx, u8 *dst, const u8 *src)
1184{ 827{
1185 des_small_fips_decrypt(((struct des_ctx *)ctx)->expkey, dst, src); 828 const u32 *K = ((struct des_ctx *)ctx)->expkey + DES_EXPKEY_WORDS - 2;
829 const __le32 *s = (const __le32 *)src;
830 __le32 *d = (__le32 *)dst;
831 u32 L, R, A, B;
832 int i;
833
834 L = le32_to_cpu(s[0]);
835 R = le32_to_cpu(s[1]);
836
837 IP(L, R, A);
838 for (i = 0; i < 8; i++) {
839 ROUND(L, R, A, B, K, -2);
840 ROUND(R, L, A, B, K, -2);
841 }
842 FP(R, L, A);
843
844 d[0] = cpu_to_le32(R);
845 d[1] = cpu_to_le32(L);
1186} 846}
1187 847
1188/* 848/*
1189 * RFC2451: 849 * RFC2451:
1190 * 850 *
1191 * For DES-EDE3, there is no known need to reject weak or 851 * For DES-EDE3, there is no known need to reject weak or
@@ -1199,44 +859,86 @@ static void des_decrypt(void *ctx, u8 *dst, const u8 *src)
1199 * 859 *
1200 */ 860 */
1201static int des3_ede_setkey(void *ctx, const u8 *key, 861static int des3_ede_setkey(void *ctx, const u8 *key,
1202 unsigned int keylen, u32 *flags) 862 unsigned int keylen, u32 *flags)
1203{ 863{
1204 unsigned int i, off; 864 const u32 *K = (const u32 *)key;
1205 struct des3_ede_ctx *dctx = ctx; 865 struct des3_ede_ctx *dctx = ctx;
866 u32 *expkey = dctx->expkey;
1206 867
1207 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && 868 if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
1208 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], 869 !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
1209 DES_KEY_SIZE))) { 870 {
1210
1211 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; 871 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
1212 return -EINVAL; 872 return -EINVAL;
1213 } 873 }
1214 874
1215 for (i = 0, off = 0; i < 3; i++, off += DES_EXPKEY_WORDS, 875 ekey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE;
1216 key += DES_KEY_SIZE) { 876 dkey(expkey, key); expkey += DES_EXPKEY_WORDS; key += DES_KEY_SIZE;
1217 int ret = setkey(&dctx->expkey[off], key, DES_KEY_SIZE, flags); 877 ekey(expkey, key);
1218 if (ret < 0) 878
1219 return ret;
1220 }
1221 return 0; 879 return 0;
1222} 880}
1223 881
1224static void des3_ede_encrypt(void *ctx, u8 *dst, const u8 *src) 882static void des3_ede_encrypt(void *ctx, u8 *dst, const u8 *src)
1225{ 883{
1226 struct des3_ede_ctx *dctx = ctx; 884 struct des3_ede_ctx *dctx = ctx;
1227 885 const u32 *K = dctx->expkey;
1228 des_small_fips_encrypt(dctx->expkey, dst, src); 886 const __le32 *s = (const __le32 *)src;
1229 des_small_fips_decrypt(&dctx->expkey[DES_EXPKEY_WORDS], dst, dst); 887 __le32 *d = (__le32 *)dst;
1230 des_small_fips_encrypt(&dctx->expkey[DES_EXPKEY_WORDS * 2], dst, dst); 888 u32 L, R, A, B;
889 int i;
890
891 L = le32_to_cpu(s[0]);
892 R = le32_to_cpu(s[1]);
893
894 IP(L, R, A);
895 for (i = 0; i < 8; i++) {
896 ROUND(L, R, A, B, K, 2);
897 ROUND(R, L, A, B, K, 2);
898 }
899 for (i = 0; i < 8; i++) {
900 ROUND(R, L, A, B, K, 2);
901 ROUND(L, R, A, B, K, 2);
902 }
903 for (i = 0; i < 8; i++) {
904 ROUND(L, R, A, B, K, 2);
905 ROUND(R, L, A, B, K, 2);
906 }
907 FP(R, L, A);
908
909 d[0] = cpu_to_le32(R);
910 d[1] = cpu_to_le32(L);
1231} 911}
1232 912
1233static void des3_ede_decrypt(void *ctx, u8 *dst, const u8 *src) 913static void des3_ede_decrypt(void *ctx, u8 *dst, const u8 *src)
1234{ 914{
1235 struct des3_ede_ctx *dctx = ctx; 915 struct des3_ede_ctx *dctx = ctx;
916 const u32 *K = dctx->expkey + DES3_EDE_EXPKEY_WORDS - 2;
917 const __le32 *s = (const __le32 *)src;
918 __le32 *d = (__le32 *)dst;
919 u32 L, R, A, B;
920 int i;
921
922 L = le32_to_cpu(s[0]);
923 R = le32_to_cpu(s[1]);
924
925 IP(L, R, A);
926 for (i = 0; i < 8; i++) {
927 ROUND(L, R, A, B, K, -2);
928 ROUND(R, L, A, B, K, -2);
929 }
930 for (i = 0; i < 8; i++) {
931 ROUND(R, L, A, B, K, -2);
932 ROUND(L, R, A, B, K, -2);
933 }
934 for (i = 0; i < 8; i++) {
935 ROUND(L, R, A, B, K, -2);
936 ROUND(R, L, A, B, K, -2);
937 }
938 FP(R, L, A);
1236 939
1237 des_small_fips_decrypt(&dctx->expkey[DES_EXPKEY_WORDS * 2], dst, src); 940 d[0] = cpu_to_le32(R);
1238 des_small_fips_encrypt(&dctx->expkey[DES_EXPKEY_WORDS], dst, dst); 941 d[1] = cpu_to_le32(L);
1239 des_small_fips_decrypt(dctx->expkey, dst, dst);
1240} 942}
1241 943
1242static struct crypto_alg des_alg = { 944static struct crypto_alg des_alg = {
@@ -1249,7 +951,7 @@ static struct crypto_alg des_alg = {
1249 .cra_u = { .cipher = { 951 .cra_u = { .cipher = {
1250 .cia_min_keysize = DES_KEY_SIZE, 952 .cia_min_keysize = DES_KEY_SIZE,
1251 .cia_max_keysize = DES_KEY_SIZE, 953 .cia_max_keysize = DES_KEY_SIZE,
1252 .cia_setkey = des_setkey, 954 .cia_setkey = des_setkey,
1253 .cia_encrypt = des_encrypt, 955 .cia_encrypt = des_encrypt,
1254 .cia_decrypt = des_decrypt } } 956 .cia_decrypt = des_decrypt } }
1255}; 957};
@@ -1264,9 +966,9 @@ static struct crypto_alg des3_ede_alg = {
1264 .cra_u = { .cipher = { 966 .cra_u = { .cipher = {
1265 .cia_min_keysize = DES3_EDE_KEY_SIZE, 967 .cia_min_keysize = DES3_EDE_KEY_SIZE,
1266 .cia_max_keysize = DES3_EDE_KEY_SIZE, 968 .cia_max_keysize = DES3_EDE_KEY_SIZE,
1267 .cia_setkey = des3_ede_setkey, 969 .cia_setkey = des3_ede_setkey,
1268 .cia_encrypt = des3_ede_encrypt, 970 .cia_encrypt = des3_ede_encrypt,
1269 .cia_decrypt = des3_ede_decrypt } } 971 .cia_decrypt = des3_ede_decrypt } }
1270}; 972};
1271 973
1272MODULE_ALIAS("des3_ede"); 974MODULE_ALIAS("des3_ede");
@@ -1274,7 +976,7 @@ MODULE_ALIAS("des3_ede");
1274static int __init init(void) 976static int __init init(void)
1275{ 977{
1276 int ret = 0; 978 int ret = 0;
1277 979
1278 ret = crypto_register_alg(&des_alg); 980 ret = crypto_register_alg(&des_alg);
1279 if (ret < 0) 981 if (ret < 0)
1280 goto out; 982 goto out;
@@ -1282,7 +984,7 @@ static int __init init(void)
1282 ret = crypto_register_alg(&des3_ede_alg); 984 ret = crypto_register_alg(&des3_ede_alg);
1283 if (ret < 0) 985 if (ret < 0)
1284 crypto_unregister_alg(&des_alg); 986 crypto_unregister_alg(&des_alg);
1285out: 987out:
1286 return ret; 988 return ret;
1287} 989}
1288 990
@@ -1297,3 +999,4 @@ module_exit(fini);
1297 999
1298MODULE_LICENSE("GPL"); 1000MODULE_LICENSE("GPL");
1299MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms"); 1001MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
1002MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>");
diff --git a/crypto/hmac.c b/crypto/hmac.c
index 847df9263e16..da0456b37109 100644
--- a/crypto/hmac.c
+++ b/crypto/hmac.c
@@ -49,8 +49,7 @@ int crypto_alloc_hmac_block(struct crypto_tfm *tfm)
49 49
50void crypto_free_hmac_block(struct crypto_tfm *tfm) 50void crypto_free_hmac_block(struct crypto_tfm *tfm)
51{ 51{
52 if (tfm->crt_digest.dit_hmac_block) 52 kfree(tfm->crt_digest.dit_hmac_block);
53 kfree(tfm->crt_digest.dit_hmac_block);
54} 53}
55 54
56void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen) 55void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen)
diff --git a/crypto/internal.h b/crypto/internal.h
index 964b9a60ca24..68612874b5fd 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -16,7 +16,7 @@
16#include <linux/highmem.h> 16#include <linux/highmem.h>
17#include <linux/interrupt.h> 17#include <linux/interrupt.h>
18#include <linux/init.h> 18#include <linux/init.h>
19#include <linux/kmod.h> 19#include <linux/kernel.h>
20#include <asm/kmap_types.h> 20#include <asm/kmap_types.h>
21 21
22extern enum km_type crypto_km_types[]; 22extern enum km_type crypto_km_types[];
@@ -42,20 +42,6 @@ static inline void crypto_yield(struct crypto_tfm *tfm)
42 cond_resched(); 42 cond_resched();
43} 43}
44 44
45static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
46{
47 return (void *)&tfm[1];
48}
49
50struct crypto_alg *crypto_alg_lookup(const char *name);
51
52/* A far more intelligent version of this is planned. For now, just
53 * try an exact match on the name of the algorithm. */
54static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
55{
56 return try_then_request_module(crypto_alg_lookup(name), name);
57}
58
59#ifdef CONFIG_CRYPTO_HMAC 45#ifdef CONFIG_CRYPTO_HMAC
60int crypto_alloc_hmac_block(struct crypto_tfm *tfm); 46int crypto_alloc_hmac_block(struct crypto_tfm *tfm);
61void crypto_free_hmac_block(struct crypto_tfm *tfm); 47void crypto_free_hmac_block(struct crypto_tfm *tfm);
@@ -76,6 +62,33 @@ static inline void crypto_init_proc(void)
76{ } 62{ }
77#endif 63#endif
78 64
65static inline unsigned int crypto_digest_ctxsize(struct crypto_alg *alg,
66 int flags)
67{
68 return alg->cra_ctxsize;
69}
70
71static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg,
72 int flags)
73{
74 unsigned int len = alg->cra_ctxsize;
75
76 switch (flags & CRYPTO_TFM_MODE_MASK) {
77 case CRYPTO_TFM_MODE_CBC:
78 len = ALIGN(len, alg->cra_alignmask + 1);
79 len += alg->cra_blocksize;
80 break;
81 }
82
83 return len;
84}
85
86static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg,
87 int flags)
88{
89 return alg->cra_ctxsize;
90}
91
79int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags); 92int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags);
80int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags); 93int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags);
81int crypto_init_compress_flags(struct crypto_tfm *tfm, u32 flags); 94int crypto_init_compress_flags(struct crypto_tfm *tfm, u32 flags);
diff --git a/crypto/scatterwalk.c b/crypto/scatterwalk.c
index 50c9461e8cc6..47ac90e615f4 100644
--- a/crypto/scatterwalk.c
+++ b/crypto/scatterwalk.c
@@ -100,7 +100,7 @@ void scatterwalk_done(struct scatter_walk *walk, int out, int more)
100int scatterwalk_copychunks(void *buf, struct scatter_walk *walk, 100int scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
101 size_t nbytes, int out) 101 size_t nbytes, int out)
102{ 102{
103 do { 103 while (nbytes > walk->len_this_page) {
104 memcpy_dir(buf, walk->data, walk->len_this_page, out); 104 memcpy_dir(buf, walk->data, walk->len_this_page, out);
105 buf += walk->len_this_page; 105 buf += walk->len_this_page;
106 nbytes -= walk->len_this_page; 106 nbytes -= walk->len_this_page;
@@ -108,7 +108,7 @@ int scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
108 scatterwalk_unmap(walk, out); 108 scatterwalk_unmap(walk, out);
109 scatterwalk_pagedone(walk, out, 1); 109 scatterwalk_pagedone(walk, out, 1);
110 scatterwalk_map(walk, out); 110 scatterwalk_map(walk, out);
111 } while (nbytes > walk->len_this_page); 111 }
112 112
113 memcpy_dir(buf, walk->data, nbytes, out); 113 memcpy_dir(buf, walk->data, nbytes, out);
114 return nbytes; 114 return nbytes;
diff --git a/crypto/scatterwalk.h b/crypto/scatterwalk.h
index 02aa56c649b4..e79925c474a3 100644
--- a/crypto/scatterwalk.h
+++ b/crypto/scatterwalk.h
@@ -40,10 +40,10 @@ static inline int scatterwalk_samebuf(struct scatter_walk *walk_in,
40 walk_in->offset == walk_out->offset; 40 walk_in->offset == walk_out->offset;
41} 41}
42 42
43static inline int scatterwalk_across_pages(struct scatter_walk *walk, 43static inline unsigned int scatterwalk_clamp(struct scatter_walk *walk,
44 unsigned int nbytes) 44 unsigned int nbytes)
45{ 45{
46 return nbytes > walk->len_this_page; 46 return nbytes > walk->len_this_page ? walk->len_this_page : nbytes;
47} 47}
48 48
49static inline void scatterwalk_advance(struct scatter_walk *walk, 49static inline void scatterwalk_advance(struct scatter_walk *walk,
@@ -55,6 +55,12 @@ static inline void scatterwalk_advance(struct scatter_walk *walk,
55 walk->len_this_segment -= nbytes; 55 walk->len_this_segment -= nbytes;
56} 56}
57 57
58static inline unsigned int scatterwalk_aligned(struct scatter_walk *walk,
59 unsigned int alignmask)
60{
61 return !(walk->offset & alignmask);
62}
63
58void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg); 64void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg);
59int scatterwalk_copychunks(void *buf, struct scatter_walk *walk, size_t nbytes, int out); 65int scatterwalk_copychunks(void *buf, struct scatter_walk *walk, size_t nbytes, int out);
60void scatterwalk_map(struct scatter_walk *walk, int out); 66void scatterwalk_map(struct scatter_walk *walk, int out);
diff --git a/crypto/serpent.c b/crypto/serpent.c
index 7d152e89016f..3cf2c5067eea 100644
--- a/crypto/serpent.c
+++ b/crypto/serpent.c
@@ -210,7 +210,6 @@
210 x4 ^= x2; 210 x4 ^= x2;
211 211
212struct serpent_ctx { 212struct serpent_ctx {
213 u8 iv[SERPENT_BLOCK_SIZE];
214 u32 expkey[SERPENT_EXPKEY_WORDS]; 213 u32 expkey[SERPENT_EXPKEY_WORDS];
215}; 214};
216 215
diff --git a/drivers/crypto/padlock-aes.c b/drivers/crypto/padlock-aes.c
index ed708b4427b0..71407c578afe 100644
--- a/drivers/crypto/padlock-aes.c
+++ b/drivers/crypto/padlock-aes.c
@@ -49,6 +49,7 @@
49#include <linux/errno.h> 49#include <linux/errno.h>
50#include <linux/crypto.h> 50#include <linux/crypto.h>
51#include <linux/interrupt.h> 51#include <linux/interrupt.h>
52#include <linux/kernel.h>
52#include <asm/byteorder.h> 53#include <asm/byteorder.h>
53#include "padlock.h" 54#include "padlock.h"
54 55
@@ -59,8 +60,12 @@
59#define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t)) 60#define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t))
60 61
61struct aes_ctx { 62struct aes_ctx {
62 uint32_t e_data[AES_EXTENDED_KEY_SIZE+4]; 63 uint32_t e_data[AES_EXTENDED_KEY_SIZE];
63 uint32_t d_data[AES_EXTENDED_KEY_SIZE+4]; 64 uint32_t d_data[AES_EXTENDED_KEY_SIZE];
65 struct {
66 struct cword encrypt;
67 struct cword decrypt;
68 } cword;
64 uint32_t *E; 69 uint32_t *E;
65 uint32_t *D; 70 uint32_t *D;
66 int key_length; 71 int key_length;
@@ -280,10 +285,15 @@ aes_hw_extkey_available(uint8_t key_len)
280 return 0; 285 return 0;
281} 286}
282 287
288static inline struct aes_ctx *aes_ctx(void *ctx)
289{
290 return (struct aes_ctx *)ALIGN((unsigned long)ctx, PADLOCK_ALIGNMENT);
291}
292
283static int 293static int
284aes_set_key(void *ctx_arg, const uint8_t *in_key, unsigned int key_len, uint32_t *flags) 294aes_set_key(void *ctx_arg, const uint8_t *in_key, unsigned int key_len, uint32_t *flags)
285{ 295{
286 struct aes_ctx *ctx = ctx_arg; 296 struct aes_ctx *ctx = aes_ctx(ctx_arg);
287 uint32_t i, t, u, v, w; 297 uint32_t i, t, u, v, w;
288 uint32_t P[AES_EXTENDED_KEY_SIZE]; 298 uint32_t P[AES_EXTENDED_KEY_SIZE];
289 uint32_t rounds; 299 uint32_t rounds;
@@ -295,25 +305,36 @@ aes_set_key(void *ctx_arg, const uint8_t *in_key, unsigned int key_len, uint32_t
295 305
296 ctx->key_length = key_len; 306 ctx->key_length = key_len;
297 307
308 /*
309 * If the hardware is capable of generating the extended key
310 * itself we must supply the plain key for both encryption
311 * and decryption.
312 */
298 ctx->E = ctx->e_data; 313 ctx->E = ctx->e_data;
299 ctx->D = ctx->d_data; 314 ctx->D = ctx->e_data;
300
301 /* Ensure 16-Bytes alignmentation of keys for VIA PadLock. */
302 if ((int)(ctx->e_data) & 0x0F)
303 ctx->E += 4 - (((int)(ctx->e_data) & 0x0F) / sizeof (ctx->e_data[0]));
304
305 if ((int)(ctx->d_data) & 0x0F)
306 ctx->D += 4 - (((int)(ctx->d_data) & 0x0F) / sizeof (ctx->d_data[0]));
307 315
308 E_KEY[0] = uint32_t_in (in_key); 316 E_KEY[0] = uint32_t_in (in_key);
309 E_KEY[1] = uint32_t_in (in_key + 4); 317 E_KEY[1] = uint32_t_in (in_key + 4);
310 E_KEY[2] = uint32_t_in (in_key + 8); 318 E_KEY[2] = uint32_t_in (in_key + 8);
311 E_KEY[3] = uint32_t_in (in_key + 12); 319 E_KEY[3] = uint32_t_in (in_key + 12);
312 320
321 /* Prepare control words. */
322 memset(&ctx->cword, 0, sizeof(ctx->cword));
323
324 ctx->cword.decrypt.encdec = 1;
325 ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
326 ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
327 ctx->cword.encrypt.ksize = (key_len - 16) / 8;
328 ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
329
313 /* Don't generate extended keys if the hardware can do it. */ 330 /* Don't generate extended keys if the hardware can do it. */
314 if (aes_hw_extkey_available(key_len)) 331 if (aes_hw_extkey_available(key_len))
315 return 0; 332 return 0;
316 333
334 ctx->D = ctx->d_data;
335 ctx->cword.encrypt.keygen = 1;
336 ctx->cword.decrypt.keygen = 1;
337
317 switch (key_len) { 338 switch (key_len) {
318 case 16: 339 case 16:
319 t = E_KEY[3]; 340 t = E_KEY[3];
@@ -369,10 +390,9 @@ aes_set_key(void *ctx_arg, const uint8_t *in_key, unsigned int key_len, uint32_t
369 390
370/* ====== Encryption/decryption routines ====== */ 391/* ====== Encryption/decryption routines ====== */
371 392
372/* This is the real call to PadLock. */ 393/* These are the real call to PadLock. */
373static inline void 394static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
374padlock_xcrypt_ecb(uint8_t *input, uint8_t *output, uint8_t *key, 395 void *control_word, u32 count)
375 void *control_word, uint32_t count)
376{ 396{
377 asm volatile ("pushfl; popfl"); /* enforce key reload. */ 397 asm volatile ("pushfl; popfl"); /* enforce key reload. */
378 asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */ 398 asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
@@ -380,60 +400,70 @@ padlock_xcrypt_ecb(uint8_t *input, uint8_t *output, uint8_t *key,
380 : "d"(control_word), "b"(key), "c"(count)); 400 : "d"(control_word), "b"(key), "c"(count));
381} 401}
382 402
383static void 403static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
384aes_padlock(void *ctx_arg, uint8_t *out_arg, const uint8_t *in_arg, int encdec) 404 u8 *iv, void *control_word, u32 count)
385{ 405{
386 /* Don't blindly modify this structure - the items must 406 /* Enforce key reload. */
387 fit on 16-Bytes boundaries! */ 407 asm volatile ("pushfl; popfl");
388 struct padlock_xcrypt_data { 408 /* rep xcryptcbc */
389 uint8_t buf[AES_BLOCK_SIZE]; 409 asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
390 union cword cword; 410 : "+S" (input), "+D" (output), "+a" (iv)
391 }; 411 : "d" (control_word), "b" (key), "c" (count));
392 412 return iv;
393 struct aes_ctx *ctx = ctx_arg;
394 char bigbuf[sizeof(struct padlock_xcrypt_data) + 16];
395 struct padlock_xcrypt_data *data;
396 void *key;
397
398 /* Place 'data' at the first 16-Bytes aligned address in 'bigbuf'. */
399 if (((long)bigbuf) & 0x0F)
400 data = (void*)(bigbuf + 16 - ((long)bigbuf & 0x0F));
401 else
402 data = (void*)bigbuf;
403
404 /* Prepare Control word. */
405 memset (data, 0, sizeof(struct padlock_xcrypt_data));
406 data->cword.b.encdec = !encdec; /* in the rest of cryptoapi ENC=1/DEC=0 */
407 data->cword.b.rounds = 10 + (ctx->key_length - 16) / 4;
408 data->cword.b.ksize = (ctx->key_length - 16) / 8;
409
410 /* Is the hardware capable to generate the extended key? */
411 if (!aes_hw_extkey_available(ctx->key_length))
412 data->cword.b.keygen = 1;
413
414 /* ctx->E starts with a plain key - if the hardware is capable
415 to generate the extended key itself we must supply
416 the plain key for both Encryption and Decryption. */
417 if (encdec == CRYPTO_DIR_ENCRYPT || data->cword.b.keygen == 0)
418 key = ctx->E;
419 else
420 key = ctx->D;
421
422 memcpy(data->buf, in_arg, AES_BLOCK_SIZE);
423 padlock_xcrypt_ecb(data->buf, data->buf, key, &data->cword, 1);
424 memcpy(out_arg, data->buf, AES_BLOCK_SIZE);
425} 413}
426 414
427static void 415static void
428aes_encrypt(void *ctx_arg, uint8_t *out, const uint8_t *in) 416aes_encrypt(void *ctx_arg, uint8_t *out, const uint8_t *in)
429{ 417{
430 aes_padlock(ctx_arg, out, in, CRYPTO_DIR_ENCRYPT); 418 struct aes_ctx *ctx = aes_ctx(ctx_arg);
419 padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, 1);
431} 420}
432 421
433static void 422static void
434aes_decrypt(void *ctx_arg, uint8_t *out, const uint8_t *in) 423aes_decrypt(void *ctx_arg, uint8_t *out, const uint8_t *in)
435{ 424{
436 aes_padlock(ctx_arg, out, in, CRYPTO_DIR_DECRYPT); 425 struct aes_ctx *ctx = aes_ctx(ctx_arg);
426 padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, 1);
427}
428
429static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
430 const u8 *in, unsigned int nbytes)
431{
432 struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm));
433 padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt,
434 nbytes / AES_BLOCK_SIZE);
435 return nbytes & ~(AES_BLOCK_SIZE - 1);
436}
437
438static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
439 const u8 *in, unsigned int nbytes)
440{
441 struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm));
442 padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt,
443 nbytes / AES_BLOCK_SIZE);
444 return nbytes & ~(AES_BLOCK_SIZE - 1);
445}
446
447static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
448 const u8 *in, unsigned int nbytes)
449{
450 struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm));
451 u8 *iv;
452
453 iv = padlock_xcrypt_cbc(in, out, ctx->E, desc->info,
454 &ctx->cword.encrypt, nbytes / AES_BLOCK_SIZE);
455 memcpy(desc->info, iv, AES_BLOCK_SIZE);
456
457 return nbytes & ~(AES_BLOCK_SIZE - 1);
458}
459
460static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
461 const u8 *in, unsigned int nbytes)
462{
463 struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm));
464 padlock_xcrypt_cbc(in, out, ctx->D, desc->info, &ctx->cword.decrypt,
465 nbytes / AES_BLOCK_SIZE);
466 return nbytes & ~(AES_BLOCK_SIZE - 1);
437} 467}
438 468
439static struct crypto_alg aes_alg = { 469static struct crypto_alg aes_alg = {
@@ -441,6 +471,7 @@ static struct crypto_alg aes_alg = {
441 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 471 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
442 .cra_blocksize = AES_BLOCK_SIZE, 472 .cra_blocksize = AES_BLOCK_SIZE,
443 .cra_ctxsize = sizeof(struct aes_ctx), 473 .cra_ctxsize = sizeof(struct aes_ctx),
474 .cra_alignmask = PADLOCK_ALIGNMENT - 1,
444 .cra_module = THIS_MODULE, 475 .cra_module = THIS_MODULE,
445 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), 476 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
446 .cra_u = { 477 .cra_u = {
@@ -449,7 +480,11 @@ static struct crypto_alg aes_alg = {
449 .cia_max_keysize = AES_MAX_KEY_SIZE, 480 .cia_max_keysize = AES_MAX_KEY_SIZE,
450 .cia_setkey = aes_set_key, 481 .cia_setkey = aes_set_key,
451 .cia_encrypt = aes_encrypt, 482 .cia_encrypt = aes_encrypt,
452 .cia_decrypt = aes_decrypt 483 .cia_decrypt = aes_decrypt,
484 .cia_encrypt_ecb = aes_encrypt_ecb,
485 .cia_decrypt_ecb = aes_decrypt_ecb,
486 .cia_encrypt_cbc = aes_encrypt_cbc,
487 .cia_decrypt_cbc = aes_decrypt_cbc,
453 } 488 }
454 } 489 }
455}; 490};
diff --git a/drivers/crypto/padlock.h b/drivers/crypto/padlock.h
index 7a500605e449..3cf2b7a12348 100644
--- a/drivers/crypto/padlock.h
+++ b/drivers/crypto/padlock.h
@@ -13,18 +13,18 @@
13#ifndef _CRYPTO_PADLOCK_H 13#ifndef _CRYPTO_PADLOCK_H
14#define _CRYPTO_PADLOCK_H 14#define _CRYPTO_PADLOCK_H
15 15
16#define PADLOCK_ALIGNMENT 16
17
16/* Control word. */ 18/* Control word. */
17union cword { 19struct cword {
18 uint32_t cword[4]; 20 int __attribute__ ((__packed__))
19 struct { 21 rounds:4,
20 int rounds:4; 22 algo:3,
21 int algo:3; 23 keygen:1,
22 int keygen:1; 24 interm:1,
23 int interm:1; 25 encdec:1,
24 int encdec:1; 26 ksize:2;
25 int ksize:2; 27} __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
26 } b;
27};
28 28
29#define PFX "padlock: " 29#define PFX "padlock: "
30 30
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index 387da6a3e58c..5e2bcc636a02 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -61,6 +61,15 @@
61#define CRYPTO_DIR_DECRYPT 0 61#define CRYPTO_DIR_DECRYPT 0
62 62
63struct scatterlist; 63struct scatterlist;
64struct crypto_tfm;
65
66struct cipher_desc {
67 struct crypto_tfm *tfm;
68 void (*crfn)(void *ctx, u8 *dst, const u8 *src);
69 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
70 const u8 *src, unsigned int nbytes);
71 void *info;
72};
64 73
65/* 74/*
66 * Algorithms: modular crypto algorithm implementations, managed 75 * Algorithms: modular crypto algorithm implementations, managed
@@ -73,6 +82,19 @@ struct cipher_alg {
73 unsigned int keylen, u32 *flags); 82 unsigned int keylen, u32 *flags);
74 void (*cia_encrypt)(void *ctx, u8 *dst, const u8 *src); 83 void (*cia_encrypt)(void *ctx, u8 *dst, const u8 *src);
75 void (*cia_decrypt)(void *ctx, u8 *dst, const u8 *src); 84 void (*cia_decrypt)(void *ctx, u8 *dst, const u8 *src);
85
86 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
87 u8 *dst, const u8 *src,
88 unsigned int nbytes);
89 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
90 u8 *dst, const u8 *src,
91 unsigned int nbytes);
92 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
93 u8 *dst, const u8 *src,
94 unsigned int nbytes);
95 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
96 u8 *dst, const u8 *src,
97 unsigned int nbytes);
76}; 98};
77 99
78struct digest_alg { 100struct digest_alg {
@@ -102,6 +124,7 @@ struct crypto_alg {
102 u32 cra_flags; 124 u32 cra_flags;
103 unsigned int cra_blocksize; 125 unsigned int cra_blocksize;
104 unsigned int cra_ctxsize; 126 unsigned int cra_ctxsize;
127 unsigned int cra_alignmask;
105 const char cra_name[CRYPTO_MAX_ALG_NAME]; 128 const char cra_name[CRYPTO_MAX_ALG_NAME];
106 129
107 union { 130 union {
@@ -136,7 +159,6 @@ static inline int crypto_alg_available(const char *name, u32 flags)
136 * and core processing logic. Managed via crypto_alloc_tfm() and 159 * and core processing logic. Managed via crypto_alloc_tfm() and
137 * crypto_free_tfm(), as well as the various helpers below. 160 * crypto_free_tfm(), as well as the various helpers below.
138 */ 161 */
139struct crypto_tfm;
140 162
141struct cipher_tfm { 163struct cipher_tfm {
142 void *cit_iv; 164 void *cit_iv;
@@ -266,6 +288,16 @@ static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
266 return tfm->__crt_alg->cra_digest.dia_digestsize; 288 return tfm->__crt_alg->cra_digest.dia_digestsize;
267} 289}
268 290
291static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
292{
293 return tfm->__crt_alg->cra_alignmask;
294}
295
296static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
297{
298 return (void *)&tfm[1];
299}
300
269/* 301/*
270 * API wrappers. 302 * API wrappers.
271 */ 303 */