Back to home page

LXR

 
 

    


0001 /* Kernel cryptographic api.
0002  * cast6.c - Cast6 cipher algorithm [rfc2612].
0003  *
0004  * CAST-256 (*cast6*) is a DES like Substitution-Permutation Network (SPN)
0005  * cryptosystem built upon the CAST-128 (*cast5*) [rfc2144] encryption
0006  * algorithm.
0007  *
0008  * Copyright (C) 2003 Kartikey Mahendra Bhatt <kartik_me@hotmail.com>.
0009  *
0010  * This program is free software; you can redistribute it and/or modify it
0011  * under the terms of GNU General Public License as published by the Free
0012  * Software Foundation; either version 2 of the License, or (at your option)
0013  * any later version.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
0018  */
0019 
0020 
0021 #include <asm/byteorder.h>
0022 #include <linux/init.h>
0023 #include <linux/crypto.h>
0024 #include <linux/module.h>
0025 #include <linux/errno.h>
0026 #include <linux/string.h>
0027 #include <linux/types.h>
0028 #include <crypto/cast6.h>
0029 
0030 #define s1 cast_s1
0031 #define s2 cast_s2
0032 #define s3 cast_s3
0033 #define s4 cast_s4
0034 
0035 #define F1(D, r, m)  ((I = ((m) + (D))), (I = rol32(I, (r))),   \
0036     (((s1[I >> 24] ^ s2[(I>>16)&0xff]) - s3[(I>>8)&0xff]) + s4[I&0xff]))
0037 #define F2(D, r, m)  ((I = ((m) ^ (D))), (I = rol32(I, (r))),   \
0038     (((s1[I >> 24] - s2[(I>>16)&0xff]) + s3[(I>>8)&0xff]) ^ s4[I&0xff]))
0039 #define F3(D, r, m)  ((I = ((m) - (D))), (I = rol32(I, (r))),   \
0040     (((s1[I >> 24] + s2[(I>>16)&0xff]) ^ s3[(I>>8)&0xff]) - s4[I&0xff]))
0041 
0042 static const u32 Tm[24][8] = {
0043     { 0x5a827999, 0xc95c653a, 0x383650db, 0xa7103c7c, 0x15ea281d,
0044         0x84c413be, 0xf39dff5f, 0x6277eb00 } ,
0045     { 0xd151d6a1, 0x402bc242, 0xaf05ade3, 0x1ddf9984, 0x8cb98525,
0046         0xfb9370c6, 0x6a6d5c67, 0xd9474808 } ,
0047     { 0x482133a9, 0xb6fb1f4a, 0x25d50aeb, 0x94aef68c, 0x0388e22d,
0048         0x7262cdce, 0xe13cb96f, 0x5016a510 } ,
0049     { 0xbef090b1, 0x2dca7c52, 0x9ca467f3, 0x0b7e5394, 0x7a583f35,
0050         0xe9322ad6, 0x580c1677, 0xc6e60218 } ,
0051     { 0x35bfedb9, 0xa499d95a, 0x1373c4fb, 0x824db09c, 0xf1279c3d,
0052         0x600187de, 0xcedb737f, 0x3db55f20 } ,
0053     { 0xac8f4ac1, 0x1b693662, 0x8a432203, 0xf91d0da4, 0x67f6f945,
0054         0xd6d0e4e6, 0x45aad087, 0xb484bc28 } ,
0055     { 0x235ea7c9, 0x9238936a, 0x01127f0b, 0x6fec6aac, 0xdec6564d,
0056         0x4da041ee, 0xbc7a2d8f, 0x2b541930 } ,
0057     { 0x9a2e04d1, 0x0907f072, 0x77e1dc13, 0xe6bbc7b4, 0x5595b355,
0058         0xc46f9ef6, 0x33498a97, 0xa2237638 } ,
0059     { 0x10fd61d9, 0x7fd74d7a, 0xeeb1391b, 0x5d8b24bc, 0xcc65105d,
0060         0x3b3efbfe, 0xaa18e79f, 0x18f2d340 } ,
0061     { 0x87ccbee1, 0xf6a6aa82, 0x65809623, 0xd45a81c4, 0x43346d65,
0062         0xb20e5906, 0x20e844a7, 0x8fc23048 } ,
0063     { 0xfe9c1be9, 0x6d76078a, 0xdc4ff32b, 0x4b29decc, 0xba03ca6d,
0064         0x28ddb60e, 0x97b7a1af, 0x06918d50 } ,
0065     { 0x756b78f1, 0xe4456492, 0x531f5033, 0xc1f93bd4, 0x30d32775,
0066         0x9fad1316, 0x0e86feb7, 0x7d60ea58 } ,
0067     { 0xec3ad5f9, 0x5b14c19a, 0xc9eead3b, 0x38c898dc, 0xa7a2847d,
0068         0x167c701e, 0x85565bbf, 0xf4304760 } ,
0069     { 0x630a3301, 0xd1e41ea2, 0x40be0a43, 0xaf97f5e4, 0x1e71e185,
0070         0x8d4bcd26, 0xfc25b8c7, 0x6affa468 } ,
0071     { 0xd9d99009, 0x48b37baa, 0xb78d674b, 0x266752ec, 0x95413e8d,
0072         0x041b2a2e, 0x72f515cf, 0xe1cf0170 } ,
0073     { 0x50a8ed11, 0xbf82d8b2, 0x2e5cc453, 0x9d36aff4, 0x0c109b95,
0074         0x7aea8736, 0xe9c472d7, 0x589e5e78 } ,
0075     { 0xc7784a19, 0x365235ba, 0xa52c215b, 0x14060cfc, 0x82dff89d,
0076         0xf1b9e43e, 0x6093cfdf, 0xcf6dbb80 } ,
0077     { 0x3e47a721, 0xad2192c2, 0x1bfb7e63, 0x8ad56a04, 0xf9af55a5,
0078         0x68894146, 0xd7632ce7, 0x463d1888 } ,
0079     { 0xb5170429, 0x23f0efca, 0x92cadb6b, 0x01a4c70c, 0x707eb2ad,
0080         0xdf589e4e, 0x4e3289ef, 0xbd0c7590 } ,
0081     { 0x2be66131, 0x9ac04cd2, 0x099a3873, 0x78742414, 0xe74e0fb5,
0082         0x5627fb56, 0xc501e6f7, 0x33dbd298 } ,
0083     { 0xa2b5be39, 0x118fa9da, 0x8069957b, 0xef43811c, 0x5e1d6cbd,
0084         0xccf7585e, 0x3bd143ff, 0xaaab2fa0 } ,
0085     { 0x19851b41, 0x885f06e2, 0xf738f283, 0x6612de24, 0xd4ecc9c5,
0086         0x43c6b566, 0xb2a0a107, 0x217a8ca8 } ,
0087     { 0x90547849, 0xff2e63ea, 0x6e084f8b, 0xdce23b2c, 0x4bbc26cd,
0088         0xba96126e, 0x296ffe0f, 0x9849e9b0 } ,
0089     { 0x0723d551, 0x75fdc0f2, 0xe4d7ac93, 0x53b19834, 0xc28b83d5,
0090         0x31656f76, 0xa03f5b17, 0x0f1946b8 }
0091 };
0092 
0093 static const u8 Tr[4][8] = {
0094     { 0x13, 0x04, 0x15, 0x06, 0x17, 0x08, 0x19, 0x0a } ,
0095     { 0x1b, 0x0c, 0x1d, 0x0e, 0x1f, 0x10, 0x01, 0x12 } ,
0096     { 0x03, 0x14, 0x05, 0x16, 0x07, 0x18, 0x09, 0x1a } ,
0097     { 0x0b, 0x1c, 0x0d, 0x1e, 0x0f, 0x00, 0x11, 0x02 }
0098 };
0099 
0100 /* forward octave */
0101 static inline void W(u32 *key, unsigned int i)
0102 {
0103     u32 I;
0104     key[6] ^= F1(key[7], Tr[i % 4][0], Tm[i][0]);
0105     key[5] ^= F2(key[6], Tr[i % 4][1], Tm[i][1]);
0106     key[4] ^= F3(key[5], Tr[i % 4][2], Tm[i][2]);
0107     key[3] ^= F1(key[4], Tr[i % 4][3], Tm[i][3]);
0108     key[2] ^= F2(key[3], Tr[i % 4][4], Tm[i][4]);
0109     key[1] ^= F3(key[2], Tr[i % 4][5], Tm[i][5]);
0110     key[0] ^= F1(key[1], Tr[i % 4][6], Tm[i][6]);
0111     key[7] ^= F2(key[0], Tr[i % 4][7], Tm[i][7]);
0112 }
0113 
0114 int __cast6_setkey(struct cast6_ctx *c, const u8 *in_key,
0115            unsigned key_len, u32 *flags)
0116 {
0117     int i;
0118     u32 key[8];
0119     __be32 p_key[8]; /* padded key */
0120 
0121     if (key_len % 4 != 0) {
0122         *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
0123         return -EINVAL;
0124     }
0125 
0126     memset(p_key, 0, 32);
0127     memcpy(p_key, in_key, key_len);
0128 
0129     key[0] = be32_to_cpu(p_key[0]);     /* A */
0130     key[1] = be32_to_cpu(p_key[1]);     /* B */
0131     key[2] = be32_to_cpu(p_key[2]);     /* C */
0132     key[3] = be32_to_cpu(p_key[3]);     /* D */
0133     key[4] = be32_to_cpu(p_key[4]);     /* E */
0134     key[5] = be32_to_cpu(p_key[5]);     /* F */
0135     key[6] = be32_to_cpu(p_key[6]);     /* G */
0136     key[7] = be32_to_cpu(p_key[7]);     /* H */
0137 
0138     for (i = 0; i < 12; i++) {
0139         W(key, 2 * i);
0140         W(key, 2 * i + 1);
0141 
0142         c->Kr[i][0] = key[0] & 0x1f;
0143         c->Kr[i][1] = key[2] & 0x1f;
0144         c->Kr[i][2] = key[4] & 0x1f;
0145         c->Kr[i][3] = key[6] & 0x1f;
0146 
0147         c->Km[i][0] = key[7];
0148         c->Km[i][1] = key[5];
0149         c->Km[i][2] = key[3];
0150         c->Km[i][3] = key[1];
0151     }
0152 
0153     return 0;
0154 }
0155 EXPORT_SYMBOL_GPL(__cast6_setkey);
0156 
0157 int cast6_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
0158 {
0159     return __cast6_setkey(crypto_tfm_ctx(tfm), key, keylen,
0160                   &tfm->crt_flags);
0161 }
0162 EXPORT_SYMBOL_GPL(cast6_setkey);
0163 
0164 /*forward quad round*/
0165 static inline void Q(u32 *block, u8 *Kr, u32 *Km)
0166 {
0167     u32 I;
0168     block[2] ^= F1(block[3], Kr[0], Km[0]);
0169     block[1] ^= F2(block[2], Kr[1], Km[1]);
0170     block[0] ^= F3(block[1], Kr[2], Km[2]);
0171     block[3] ^= F1(block[0], Kr[3], Km[3]);
0172 }
0173 
0174 /*reverse quad round*/
0175 static inline void QBAR(u32 *block, u8 *Kr, u32 *Km)
0176 {
0177     u32 I;
0178     block[3] ^= F1(block[0], Kr[3], Km[3]);
0179     block[0] ^= F3(block[1], Kr[2], Km[2]);
0180     block[1] ^= F2(block[2], Kr[1], Km[1]);
0181     block[2] ^= F1(block[3], Kr[0], Km[0]);
0182 }
0183 
0184 void __cast6_encrypt(struct cast6_ctx *c, u8 *outbuf, const u8 *inbuf)
0185 {
0186     const __be32 *src = (const __be32 *)inbuf;
0187     __be32 *dst = (__be32 *)outbuf;
0188     u32 block[4];
0189     u32 *Km;
0190     u8 *Kr;
0191 
0192     block[0] = be32_to_cpu(src[0]);
0193     block[1] = be32_to_cpu(src[1]);
0194     block[2] = be32_to_cpu(src[2]);
0195     block[3] = be32_to_cpu(src[3]);
0196 
0197     Km = c->Km[0]; Kr = c->Kr[0]; Q(block, Kr, Km);
0198     Km = c->Km[1]; Kr = c->Kr[1]; Q(block, Kr, Km);
0199     Km = c->Km[2]; Kr = c->Kr[2]; Q(block, Kr, Km);
0200     Km = c->Km[3]; Kr = c->Kr[3]; Q(block, Kr, Km);
0201     Km = c->Km[4]; Kr = c->Kr[4]; Q(block, Kr, Km);
0202     Km = c->Km[5]; Kr = c->Kr[5]; Q(block, Kr, Km);
0203     Km = c->Km[6]; Kr = c->Kr[6]; QBAR(block, Kr, Km);
0204     Km = c->Km[7]; Kr = c->Kr[7]; QBAR(block, Kr, Km);
0205     Km = c->Km[8]; Kr = c->Kr[8]; QBAR(block, Kr, Km);
0206     Km = c->Km[9]; Kr = c->Kr[9]; QBAR(block, Kr, Km);
0207     Km = c->Km[10]; Kr = c->Kr[10]; QBAR(block, Kr, Km);
0208     Km = c->Km[11]; Kr = c->Kr[11]; QBAR(block, Kr, Km);
0209 
0210     dst[0] = cpu_to_be32(block[0]);
0211     dst[1] = cpu_to_be32(block[1]);
0212     dst[2] = cpu_to_be32(block[2]);
0213     dst[3] = cpu_to_be32(block[3]);
0214 }
0215 EXPORT_SYMBOL_GPL(__cast6_encrypt);
0216 
0217 static void cast6_encrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
0218 {
0219     __cast6_encrypt(crypto_tfm_ctx(tfm), outbuf, inbuf);
0220 }
0221 
0222 void __cast6_decrypt(struct cast6_ctx *c, u8 *outbuf, const u8 *inbuf)
0223 {
0224     const __be32 *src = (const __be32 *)inbuf;
0225     __be32 *dst = (__be32 *)outbuf;
0226     u32 block[4];
0227     u32 *Km;
0228     u8 *Kr;
0229 
0230     block[0] = be32_to_cpu(src[0]);
0231     block[1] = be32_to_cpu(src[1]);
0232     block[2] = be32_to_cpu(src[2]);
0233     block[3] = be32_to_cpu(src[3]);
0234 
0235     Km = c->Km[11]; Kr = c->Kr[11]; Q(block, Kr, Km);
0236     Km = c->Km[10]; Kr = c->Kr[10]; Q(block, Kr, Km);
0237     Km = c->Km[9]; Kr = c->Kr[9]; Q(block, Kr, Km);
0238     Km = c->Km[8]; Kr = c->Kr[8]; Q(block, Kr, Km);
0239     Km = c->Km[7]; Kr = c->Kr[7]; Q(block, Kr, Km);
0240     Km = c->Km[6]; Kr = c->Kr[6]; Q(block, Kr, Km);
0241     Km = c->Km[5]; Kr = c->Kr[5]; QBAR(block, Kr, Km);
0242     Km = c->Km[4]; Kr = c->Kr[4]; QBAR(block, Kr, Km);
0243     Km = c->Km[3]; Kr = c->Kr[3]; QBAR(block, Kr, Km);
0244     Km = c->Km[2]; Kr = c->Kr[2]; QBAR(block, Kr, Km);
0245     Km = c->Km[1]; Kr = c->Kr[1]; QBAR(block, Kr, Km);
0246     Km = c->Km[0]; Kr = c->Kr[0]; QBAR(block, Kr, Km);
0247 
0248     dst[0] = cpu_to_be32(block[0]);
0249     dst[1] = cpu_to_be32(block[1]);
0250     dst[2] = cpu_to_be32(block[2]);
0251     dst[3] = cpu_to_be32(block[3]);
0252 }
0253 EXPORT_SYMBOL_GPL(__cast6_decrypt);
0254 
0255 static void cast6_decrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
0256 {
0257     __cast6_decrypt(crypto_tfm_ctx(tfm), outbuf, inbuf);
0258 }
0259 
0260 static struct crypto_alg alg = {
0261     .cra_name = "cast6",
0262     .cra_driver_name = "cast6-generic",
0263     .cra_priority = 100,
0264     .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
0265     .cra_blocksize = CAST6_BLOCK_SIZE,
0266     .cra_ctxsize = sizeof(struct cast6_ctx),
0267     .cra_alignmask = 3,
0268     .cra_module = THIS_MODULE,
0269     .cra_u = {
0270           .cipher = {
0271                  .cia_min_keysize = CAST6_MIN_KEY_SIZE,
0272                  .cia_max_keysize = CAST6_MAX_KEY_SIZE,
0273                  .cia_setkey = cast6_setkey,
0274                  .cia_encrypt = cast6_encrypt,
0275                  .cia_decrypt = cast6_decrypt}
0276           }
0277 };
0278 
0279 static int __init cast6_mod_init(void)
0280 {
0281     return crypto_register_alg(&alg);
0282 }
0283 
0284 static void __exit cast6_mod_fini(void)
0285 {
0286     crypto_unregister_alg(&alg);
0287 }
0288 
0289 module_init(cast6_mod_init);
0290 module_exit(cast6_mod_fini);
0291 
0292 MODULE_LICENSE("GPL");
0293 MODULE_DESCRIPTION("Cast6 Cipher Algorithm");
0294 MODULE_ALIAS_CRYPTO("cast6");
0295 MODULE_ALIAS_CRYPTO("cast6-generic");