blob: c8e5ec69790e2a425995f82c1a6f0cf122d0e342 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel cryptographic api.
2 * cast6.c - Cast6 cipher algorithm [rfc2612].
3 *
4 * CAST-256 (*cast6*) is a DES like Substitution-Permutation Network (SPN)
5 * cryptosystem built upon the CAST-128 (*cast5*) [rfc2144] encryption
6 * algorithm.
7 *
8 * Copyright (C) 2003 Kartikey Mahendra Bhatt <kartik_me@hotmail.com>.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
Richard Hartmann87c3be82010-02-16 20:29:45 +080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * You should have received a copy of the GNU General Public License
Martin Kepplinger1af39da2017-11-14 10:25:15 +010016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19
Herbert Xu06ace7a2005-10-30 21:25:15 +110020#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/crypto.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/string.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110026#include <linux/types.h>
Johannes Goetzfried2b49b902012-07-11 19:38:12 +020027#include <crypto/cast6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Jussi Kivilinna044ab522012-11-13 11:43:14 +020029#define s1 cast_s1
30#define s2 cast_s2
31#define s3 cast_s3
32#define s4 cast_s4
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Richard Hartmann87c3be82010-02-16 20:29:45 +080034#define F1(D, r, m) ((I = ((m) + (D))), (I = rol32(I, (r))), \
Johannes Goetzfried2b49b902012-07-11 19:38:12 +020035 (((s1[I >> 24] ^ s2[(I>>16)&0xff]) - s3[(I>>8)&0xff]) + s4[I&0xff]))
Richard Hartmann87c3be82010-02-16 20:29:45 +080036#define F2(D, r, m) ((I = ((m) ^ (D))), (I = rol32(I, (r))), \
Johannes Goetzfried2b49b902012-07-11 19:38:12 +020037 (((s1[I >> 24] - s2[(I>>16)&0xff]) + s3[(I>>8)&0xff]) ^ s4[I&0xff]))
Richard Hartmann87c3be82010-02-16 20:29:45 +080038#define F3(D, r, m) ((I = ((m) - (D))), (I = rol32(I, (r))), \
Johannes Goetzfried2b49b902012-07-11 19:38:12 +020039 (((s1[I >> 24] + s2[(I>>16)&0xff]) ^ s3[(I>>8)&0xff]) - s4[I&0xff]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static const u32 Tm[24][8] = {
42 { 0x5a827999, 0xc95c653a, 0x383650db, 0xa7103c7c, 0x15ea281d,
Richard Hartmann87c3be82010-02-16 20:29:45 +080043 0x84c413be, 0xf39dff5f, 0x6277eb00 } ,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 { 0xd151d6a1, 0x402bc242, 0xaf05ade3, 0x1ddf9984, 0x8cb98525,
45 0xfb9370c6, 0x6a6d5c67, 0xd9474808 } ,
46 { 0x482133a9, 0xb6fb1f4a, 0x25d50aeb, 0x94aef68c, 0x0388e22d,
47 0x7262cdce, 0xe13cb96f, 0x5016a510 } ,
48 { 0xbef090b1, 0x2dca7c52, 0x9ca467f3, 0x0b7e5394, 0x7a583f35,
49 0xe9322ad6, 0x580c1677, 0xc6e60218 } ,
50 { 0x35bfedb9, 0xa499d95a, 0x1373c4fb, 0x824db09c, 0xf1279c3d,
51 0x600187de, 0xcedb737f, 0x3db55f20 } ,
52 { 0xac8f4ac1, 0x1b693662, 0x8a432203, 0xf91d0da4, 0x67f6f945,
53 0xd6d0e4e6, 0x45aad087, 0xb484bc28 } ,
54 { 0x235ea7c9, 0x9238936a, 0x01127f0b, 0x6fec6aac, 0xdec6564d,
55 0x4da041ee, 0xbc7a2d8f, 0x2b541930 } ,
56 { 0x9a2e04d1, 0x0907f072, 0x77e1dc13, 0xe6bbc7b4, 0x5595b355,
57 0xc46f9ef6, 0x33498a97, 0xa2237638 } ,
58 { 0x10fd61d9, 0x7fd74d7a, 0xeeb1391b, 0x5d8b24bc, 0xcc65105d,
59 0x3b3efbfe, 0xaa18e79f, 0x18f2d340 } ,
60 { 0x87ccbee1, 0xf6a6aa82, 0x65809623, 0xd45a81c4, 0x43346d65,
61 0xb20e5906, 0x20e844a7, 0x8fc23048 } ,
62 { 0xfe9c1be9, 0x6d76078a, 0xdc4ff32b, 0x4b29decc, 0xba03ca6d,
63 0x28ddb60e, 0x97b7a1af, 0x06918d50 } ,
64 { 0x756b78f1, 0xe4456492, 0x531f5033, 0xc1f93bd4, 0x30d32775,
65 0x9fad1316, 0x0e86feb7, 0x7d60ea58 } ,
66 { 0xec3ad5f9, 0x5b14c19a, 0xc9eead3b, 0x38c898dc, 0xa7a2847d,
67 0x167c701e, 0x85565bbf, 0xf4304760 } ,
68 { 0x630a3301, 0xd1e41ea2, 0x40be0a43, 0xaf97f5e4, 0x1e71e185,
69 0x8d4bcd26, 0xfc25b8c7, 0x6affa468 } ,
70 { 0xd9d99009, 0x48b37baa, 0xb78d674b, 0x266752ec, 0x95413e8d,
71 0x041b2a2e, 0x72f515cf, 0xe1cf0170 } ,
72 { 0x50a8ed11, 0xbf82d8b2, 0x2e5cc453, 0x9d36aff4, 0x0c109b95,
73 0x7aea8736, 0xe9c472d7, 0x589e5e78 } ,
74 { 0xc7784a19, 0x365235ba, 0xa52c215b, 0x14060cfc, 0x82dff89d,
75 0xf1b9e43e, 0x6093cfdf, 0xcf6dbb80 } ,
76 { 0x3e47a721, 0xad2192c2, 0x1bfb7e63, 0x8ad56a04, 0xf9af55a5,
77 0x68894146, 0xd7632ce7, 0x463d1888 } ,
78 { 0xb5170429, 0x23f0efca, 0x92cadb6b, 0x01a4c70c, 0x707eb2ad,
79 0xdf589e4e, 0x4e3289ef, 0xbd0c7590 } ,
80 { 0x2be66131, 0x9ac04cd2, 0x099a3873, 0x78742414, 0xe74e0fb5,
81 0x5627fb56, 0xc501e6f7, 0x33dbd298 } ,
82 { 0xa2b5be39, 0x118fa9da, 0x8069957b, 0xef43811c, 0x5e1d6cbd,
83 0xccf7585e, 0x3bd143ff, 0xaaab2fa0 } ,
84 { 0x19851b41, 0x885f06e2, 0xf738f283, 0x6612de24, 0xd4ecc9c5,
85 0x43c6b566, 0xb2a0a107, 0x217a8ca8 } ,
86 { 0x90547849, 0xff2e63ea, 0x6e084f8b, 0xdce23b2c, 0x4bbc26cd,
87 0xba96126e, 0x296ffe0f, 0x9849e9b0 } ,
88 { 0x0723d551, 0x75fdc0f2, 0xe4d7ac93, 0x53b19834, 0xc28b83d5,
89 0x31656f76, 0xa03f5b17, 0x0f1946b8 }
90};
91
92static const u8 Tr[4][8] = {
93 { 0x13, 0x04, 0x15, 0x06, 0x17, 0x08, 0x19, 0x0a } ,
94 { 0x1b, 0x0c, 0x1d, 0x0e, 0x1f, 0x10, 0x01, 0x12 } ,
95 { 0x03, 0x14, 0x05, 0x16, 0x07, 0x18, 0x09, 0x1a } ,
96 { 0x0b, 0x1c, 0x0d, 0x1e, 0x0f, 0x00, 0x11, 0x02 }
97};
98
99/* forward octave */
Jussi Kivilinna312639b2012-08-28 16:49:28 +0300100static inline void W(u32 *key, unsigned int i)
Richard Hartmann87c3be82010-02-16 20:29:45 +0800101{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 u32 I;
103 key[6] ^= F1(key[7], Tr[i % 4][0], Tm[i][0]);
104 key[5] ^= F2(key[6], Tr[i % 4][1], Tm[i][1]);
105 key[4] ^= F3(key[5], Tr[i % 4][2], Tm[i][2]);
106 key[3] ^= F1(key[4], Tr[i % 4][3], Tm[i][3]);
107 key[2] ^= F2(key[3], Tr[i % 4][4], Tm[i][4]);
108 key[1] ^= F3(key[2], Tr[i % 4][5], Tm[i][5]);
Richard Hartmann87c3be82010-02-16 20:29:45 +0800109 key[0] ^= F1(key[1], Tr[i % 4][6], Tm[i][6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 key[7] ^= F2(key[0], Tr[i % 4][7], Tm[i][7]);
111}
112
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200113int __cast6_setkey(struct cast6_ctx *c, const u8 *in_key,
114 unsigned key_len, u32 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 int i;
117 u32 key[8];
Herbert Xu06ace7a2005-10-30 21:25:15 +1100118 __be32 p_key[8]; /* padded key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Herbert Xu560c06a2006-08-13 14:16:39 +1000120 if (key_len % 4 != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
122 return -EINVAL;
Richard Hartmann87c3be82010-02-16 20:29:45 +0800123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Richard Hartmann87c3be82010-02-16 20:29:45 +0800125 memset(p_key, 0, 32);
126 memcpy(p_key, in_key, key_len);
127
Herbert Xu06ace7a2005-10-30 21:25:15 +1100128 key[0] = be32_to_cpu(p_key[0]); /* A */
129 key[1] = be32_to_cpu(p_key[1]); /* B */
130 key[2] = be32_to_cpu(p_key[2]); /* C */
131 key[3] = be32_to_cpu(p_key[3]); /* D */
132 key[4] = be32_to_cpu(p_key[4]); /* E */
133 key[5] = be32_to_cpu(p_key[5]); /* F */
134 key[6] = be32_to_cpu(p_key[6]); /* G */
135 key[7] = be32_to_cpu(p_key[7]); /* H */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 for (i = 0; i < 12; i++) {
Richard Hartmann87c3be82010-02-16 20:29:45 +0800138 W(key, 2 * i);
139 W(key, 2 * i + 1);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 c->Kr[i][0] = key[0] & 0x1f;
142 c->Kr[i][1] = key[2] & 0x1f;
143 c->Kr[i][2] = key[4] & 0x1f;
144 c->Kr[i][3] = key[6] & 0x1f;
Richard Hartmann87c3be82010-02-16 20:29:45 +0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 c->Km[i][0] = key[7];
147 c->Km[i][1] = key[5];
148 c->Km[i][2] = key[3];
149 c->Km[i][3] = key[1];
150 }
151
152 return 0;
153}
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200154EXPORT_SYMBOL_GPL(__cast6_setkey);
155
156int cast6_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
157{
158 return __cast6_setkey(crypto_tfm_ctx(tfm), key, keylen,
159 &tfm->crt_flags);
160}
161EXPORT_SYMBOL_GPL(cast6_setkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163/*forward quad round*/
Jussi Kivilinna312639b2012-08-28 16:49:28 +0300164static inline void Q(u32 *block, u8 *Kr, u32 *Km)
Richard Hartmann87c3be82010-02-16 20:29:45 +0800165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 u32 I;
167 block[2] ^= F1(block[3], Kr[0], Km[0]);
168 block[1] ^= F2(block[2], Kr[1], Km[1]);
169 block[0] ^= F3(block[1], Kr[2], Km[2]);
Richard Hartmann87c3be82010-02-16 20:29:45 +0800170 block[3] ^= F1(block[0], Kr[3], Km[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/*reverse quad round*/
Jussi Kivilinna312639b2012-08-28 16:49:28 +0300174static inline void QBAR(u32 *block, u8 *Kr, u32 *Km)
Richard Hartmann87c3be82010-02-16 20:29:45 +0800175{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 u32 I;
Richard Hartmann87c3be82010-02-16 20:29:45 +0800177 block[3] ^= F1(block[0], Kr[3], Km[3]);
178 block[0] ^= F3(block[1], Kr[2], Km[2]);
179 block[1] ^= F2(block[2], Kr[1], Km[1]);
180 block[2] ^= F1(block[3], Kr[0], Km[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200183void __cast6_encrypt(struct cast6_ctx *c, u8 *outbuf, const u8 *inbuf)
Herbert Xu6c2bb982006-05-16 22:09:29 +1000184{
Herbert Xu06ace7a2005-10-30 21:25:15 +1100185 const __be32 *src = (const __be32 *)inbuf;
186 __be32 *dst = (__be32 *)outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 u32 block[4];
Richard Hartmann87c3be82010-02-16 20:29:45 +0800188 u32 *Km;
189 u8 *Kr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Herbert Xu06ace7a2005-10-30 21:25:15 +1100191 block[0] = be32_to_cpu(src[0]);
192 block[1] = be32_to_cpu(src[1]);
193 block[2] = be32_to_cpu(src[2]);
194 block[3] = be32_to_cpu(src[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Richard Hartmann87c3be82010-02-16 20:29:45 +0800196 Km = c->Km[0]; Kr = c->Kr[0]; Q(block, Kr, Km);
197 Km = c->Km[1]; Kr = c->Kr[1]; Q(block, Kr, Km);
198 Km = c->Km[2]; Kr = c->Kr[2]; Q(block, Kr, Km);
199 Km = c->Km[3]; Kr = c->Kr[3]; Q(block, Kr, Km);
200 Km = c->Km[4]; Kr = c->Kr[4]; Q(block, Kr, Km);
201 Km = c->Km[5]; Kr = c->Kr[5]; Q(block, Kr, Km);
202 Km = c->Km[6]; Kr = c->Kr[6]; QBAR(block, Kr, Km);
203 Km = c->Km[7]; Kr = c->Kr[7]; QBAR(block, Kr, Km);
204 Km = c->Km[8]; Kr = c->Kr[8]; QBAR(block, Kr, Km);
205 Km = c->Km[9]; Kr = c->Kr[9]; QBAR(block, Kr, Km);
206 Km = c->Km[10]; Kr = c->Kr[10]; QBAR(block, Kr, Km);
207 Km = c->Km[11]; Kr = c->Kr[11]; QBAR(block, Kr, Km);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100208
209 dst[0] = cpu_to_be32(block[0]);
210 dst[1] = cpu_to_be32(block[1]);
211 dst[2] = cpu_to_be32(block[2]);
212 dst[3] = cpu_to_be32(block[3]);
Richard Hartmann87c3be82010-02-16 20:29:45 +0800213}
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200214EXPORT_SYMBOL_GPL(__cast6_encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200216static void cast6_encrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
Richard Hartmann87c3be82010-02-16 20:29:45 +0800217{
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200218 __cast6_encrypt(crypto_tfm_ctx(tfm), outbuf, inbuf);
219}
220
221void __cast6_decrypt(struct cast6_ctx *c, u8 *outbuf, const u8 *inbuf)
222{
Herbert Xu06ace7a2005-10-30 21:25:15 +1100223 const __be32 *src = (const __be32 *)inbuf;
224 __be32 *dst = (__be32 *)outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 u32 block[4];
Richard Hartmann87c3be82010-02-16 20:29:45 +0800226 u32 *Km;
227 u8 *Kr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Herbert Xu06ace7a2005-10-30 21:25:15 +1100229 block[0] = be32_to_cpu(src[0]);
230 block[1] = be32_to_cpu(src[1]);
231 block[2] = be32_to_cpu(src[2]);
232 block[3] = be32_to_cpu(src[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Richard Hartmann87c3be82010-02-16 20:29:45 +0800234 Km = c->Km[11]; Kr = c->Kr[11]; Q(block, Kr, Km);
235 Km = c->Km[10]; Kr = c->Kr[10]; Q(block, Kr, Km);
236 Km = c->Km[9]; Kr = c->Kr[9]; Q(block, Kr, Km);
237 Km = c->Km[8]; Kr = c->Kr[8]; Q(block, Kr, Km);
238 Km = c->Km[7]; Kr = c->Kr[7]; Q(block, Kr, Km);
239 Km = c->Km[6]; Kr = c->Kr[6]; Q(block, Kr, Km);
240 Km = c->Km[5]; Kr = c->Kr[5]; QBAR(block, Kr, Km);
241 Km = c->Km[4]; Kr = c->Kr[4]; QBAR(block, Kr, Km);
242 Km = c->Km[3]; Kr = c->Kr[3]; QBAR(block, Kr, Km);
243 Km = c->Km[2]; Kr = c->Kr[2]; QBAR(block, Kr, Km);
244 Km = c->Km[1]; Kr = c->Kr[1]; QBAR(block, Kr, Km);
245 Km = c->Km[0]; Kr = c->Kr[0]; QBAR(block, Kr, Km);
246
Herbert Xu06ace7a2005-10-30 21:25:15 +1100247 dst[0] = cpu_to_be32(block[0]);
248 dst[1] = cpu_to_be32(block[1]);
249 dst[2] = cpu_to_be32(block[2]);
250 dst[3] = cpu_to_be32(block[3]);
Richard Hartmann87c3be82010-02-16 20:29:45 +0800251}
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200252EXPORT_SYMBOL_GPL(__cast6_decrypt);
253
254static void cast6_decrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
255{
256 __cast6_decrypt(crypto_tfm_ctx(tfm), outbuf, inbuf);
257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259static struct crypto_alg alg = {
260 .cra_name = "cast6",
Johannes Goetzfried2b49b902012-07-11 19:38:12 +0200261 .cra_driver_name = "cast6-generic",
262 .cra_priority = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
264 .cra_blocksize = CAST6_BLOCK_SIZE,
265 .cra_ctxsize = sizeof(struct cast6_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100266 .cra_alignmask = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .cra_u = {
269 .cipher = {
270 .cia_min_keysize = CAST6_MIN_KEY_SIZE,
271 .cia_max_keysize = CAST6_MAX_KEY_SIZE,
272 .cia_setkey = cast6_setkey,
273 .cia_encrypt = cast6_encrypt,
274 .cia_decrypt = cast6_decrypt}
275 }
276};
277
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800278static int __init cast6_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 return crypto_register_alg(&alg);
281}
282
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800283static void __exit cast6_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 crypto_unregister_alg(&alg);
286}
287
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800288module_init(cast6_mod_init);
289module_exit(cast6_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291MODULE_LICENSE("GPL");
292MODULE_DESCRIPTION("Cast6 Cipher Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800293MODULE_ALIAS_CRYPTO("cast6");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100294MODULE_ALIAS_CRYPTO("cast6-generic");