aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorJohannes Goetzfried <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>2012-07-11 13:38:12 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2012-08-01 05:47:30 -0400
commit2b49b906729644dd4696b9291b7e2f6cd1266dc0 (patch)
tree23efa38a81d4c7da484307ace9d37b137ced8efe /crypto
parent4d6d6a2c850f89bc9283d02519cb536baba72032 (diff)
crypto: cast6 - prepare generic module for optimized implementations
Rename cast6 module to cast6_generic to allow autoloading of optimized implementations. Generic functions and s-boxes are exported to be able to use them within optimized implementations. Signed-off-by: Johannes Goetzfried <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/Makefile2
-rw-r--r--crypto/cast6_generic.c (renamed from crypto/cast6.c)66
2 files changed, 44 insertions, 24 deletions
diff --git a/crypto/Makefile b/crypto/Makefile
index a56821e5d573..396966d2d849 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -69,7 +69,7 @@ obj-$(CONFIG_CRYPTO_SERPENT) += serpent_generic.o
69obj-$(CONFIG_CRYPTO_AES) += aes_generic.o 69obj-$(CONFIG_CRYPTO_AES) += aes_generic.o
70obj-$(CONFIG_CRYPTO_CAMELLIA) += camellia_generic.o 70obj-$(CONFIG_CRYPTO_CAMELLIA) += camellia_generic.o
71obj-$(CONFIG_CRYPTO_CAST5) += cast5_generic.o 71obj-$(CONFIG_CRYPTO_CAST5) += cast5_generic.o
72obj-$(CONFIG_CRYPTO_CAST6) += cast6.o 72obj-$(CONFIG_CRYPTO_CAST6) += cast6_generic.o
73obj-$(CONFIG_CRYPTO_ARC4) += arc4.o 73obj-$(CONFIG_CRYPTO_ARC4) += arc4.o
74obj-$(CONFIG_CRYPTO_TEA) += tea.o 74obj-$(CONFIG_CRYPTO_TEA) += tea.o
75obj-$(CONFIG_CRYPTO_KHAZAD) += khazad.o 75obj-$(CONFIG_CRYPTO_KHAZAD) += khazad.o
diff --git a/crypto/cast6.c b/crypto/cast6_generic.c
index 04264f574601..dc9309d70405 100644
--- a/crypto/cast6.c
+++ b/crypto/cast6_generic.c
@@ -25,24 +25,21 @@
25#include <linux/errno.h> 25#include <linux/errno.h>
26#include <linux/string.h> 26#include <linux/string.h>
27#include <linux/types.h> 27#include <linux/types.h>
28#include <crypto/cast6.h>
28 29
29#define CAST6_BLOCK_SIZE 16 30#define s1 cast6_s1
30#define CAST6_MIN_KEY_SIZE 16 31#define s2 cast6_s2
31#define CAST6_MAX_KEY_SIZE 32 32#define s3 cast6_s3
32 33#define s4 cast6_s4
33struct cast6_ctx {
34 u32 Km[12][4];
35 u8 Kr[12][4];
36};
37 34
38#define F1(D, r, m) ((I = ((m) + (D))), (I = rol32(I, (r))), \ 35#define F1(D, r, m) ((I = ((m) + (D))), (I = rol32(I, (r))), \
39 (((s1[I >> 24] ^ s2[(I>>16)&0xff]) - s3[(I>>8)&0xff]) + s4[I&0xff])) 36 (((s1[I >> 24] ^ s2[(I>>16)&0xff]) - s3[(I>>8)&0xff]) + s4[I&0xff]))
40#define F2(D, r, m) ((I = ((m) ^ (D))), (I = rol32(I, (r))), \ 37#define F2(D, r, m) ((I = ((m) ^ (D))), (I = rol32(I, (r))), \
41 (((s1[I >> 24] - s2[(I>>16)&0xff]) + s3[(I>>8)&0xff]) ^ s4[I&0xff])) 38 (((s1[I >> 24] - s2[(I>>16)&0xff]) + s3[(I>>8)&0xff]) ^ s4[I&0xff]))
42#define F3(D, r, m) ((I = ((m) - (D))), (I = rol32(I, (r))), \ 39#define F3(D, r, m) ((I = ((m) - (D))), (I = rol32(I, (r))), \
43 (((s1[I >> 24] + s2[(I>>16)&0xff]) ^ s3[(I>>8)&0xff]) - s4[I&0xff])) 40 (((s1[I >> 24] + s2[(I>>16)&0xff]) ^ s3[(I>>8)&0xff]) - s4[I&0xff]))
44 41
45static const u32 s1[256] = { 42const u32 cast6_s1[256] = {
46 0x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f, 43 0x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f,
47 0x9c004dd3, 0x6003e540, 0xcf9fc949, 44 0x9c004dd3, 0x6003e540, 0xcf9fc949,
48 0xbfd4af27, 0x88bbbdb5, 0xe2034090, 0x98d09675, 0x6e63a0e0, 45 0xbfd4af27, 0x88bbbdb5, 0xe2034090, 0x98d09675, 0x6e63a0e0,
@@ -108,8 +105,9 @@ static const u32 s1[256] = {
108 0x1a69e783, 0x02cc4843, 0xa2f7c579, 0x429ef47d, 0x427b169c, 105 0x1a69e783, 0x02cc4843, 0xa2f7c579, 0x429ef47d, 0x427b169c,
109 0x5ac9f049, 0xdd8f0f00, 0x5c8165bf 106 0x5ac9f049, 0xdd8f0f00, 0x5c8165bf
110}; 107};
108EXPORT_SYMBOL_GPL(cast6_s1);
111 109
112static const u32 s2[256] = { 110const u32 cast6_s2[256] = {
113 0x1f201094, 0xef0ba75b, 0x69e3cf7e, 0x393f4380, 0xfe61cf7a, 111 0x1f201094, 0xef0ba75b, 0x69e3cf7e, 0x393f4380, 0xfe61cf7a,
114 0xeec5207a, 0x55889c94, 0x72fc0651, 112 0xeec5207a, 0x55889c94, 0x72fc0651,
115 0xada7ef79, 0x4e1d7235, 0xd55a63ce, 0xde0436ba, 0x99c430ef, 113 0xada7ef79, 0x4e1d7235, 0xd55a63ce, 0xde0436ba, 0x99c430ef,
@@ -175,8 +173,9 @@ static const u32 s2[256] = {
175 0x43d79572, 0x7e6dd07c, 0x06dfdf1e, 0x6c6cc4ef, 0x7160a539, 173 0x43d79572, 0x7e6dd07c, 0x06dfdf1e, 0x6c6cc4ef, 0x7160a539,
176 0x73bfbe70, 0x83877605, 0x4523ecf1 174 0x73bfbe70, 0x83877605, 0x4523ecf1
177}; 175};
176EXPORT_SYMBOL_GPL(cast6_s2);
178 177
179static const u32 s3[256] = { 178const u32 cast6_s3[256] = {
180 0x8defc240, 0x25fa5d9f, 0xeb903dbf, 0xe810c907, 0x47607fff, 179 0x8defc240, 0x25fa5d9f, 0xeb903dbf, 0xe810c907, 0x47607fff,
181 0x369fe44b, 0x8c1fc644, 0xaececa90, 180 0x369fe44b, 0x8c1fc644, 0xaececa90,
182 0xbeb1f9bf, 0xeefbcaea, 0xe8cf1950, 0x51df07ae, 0x920e8806, 181 0xbeb1f9bf, 0xeefbcaea, 0xe8cf1950, 0x51df07ae, 0x920e8806,
@@ -242,8 +241,9 @@ static const u32 s3[256] = {
242 0xf7baefd5, 0x4142ed9c, 0xa4315c11, 0x83323ec5, 0xdfef4636, 241 0xf7baefd5, 0x4142ed9c, 0xa4315c11, 0x83323ec5, 0xdfef4636,
243 0xa133c501, 0xe9d3531c, 0xee353783 242 0xa133c501, 0xe9d3531c, 0xee353783
244}; 243};
244EXPORT_SYMBOL_GPL(cast6_s3);
245 245
246static const u32 s4[256] = { 246const u32 cast6_s4[256] = {
247 0x9db30420, 0x1fb6e9de, 0xa7be7bef, 0xd273a298, 0x4a4f7bdb, 247 0x9db30420, 0x1fb6e9de, 0xa7be7bef, 0xd273a298, 0x4a4f7bdb,
248 0x64ad8c57, 0x85510443, 0xfa020ed1, 248 0x64ad8c57, 0x85510443, 0xfa020ed1,
249 0x7e287aff, 0xe60fb663, 0x095f35a1, 0x79ebf120, 0xfd059d43, 249 0x7e287aff, 0xe60fb663, 0x095f35a1, 0x79ebf120, 0xfd059d43,
@@ -309,6 +309,7 @@ static const u32 s4[256] = {
309 0x7ae5290c, 0x3cb9536b, 0x851e20fe, 0x9833557e, 0x13ecf0b0, 309 0x7ae5290c, 0x3cb9536b, 0x851e20fe, 0x9833557e, 0x13ecf0b0,
310 0xd3ffb372, 0x3f85c5c1, 0x0aef7ed2 310 0xd3ffb372, 0x3f85c5c1, 0x0aef7ed2
311}; 311};
312EXPORT_SYMBOL_GPL(cast6_s4);
312 313
313static const u32 Tm[24][8] = { 314static const u32 Tm[24][8] = {
314 { 0x5a827999, 0xc95c653a, 0x383650db, 0xa7103c7c, 0x15ea281d, 315 { 0x5a827999, 0xc95c653a, 0x383650db, 0xa7103c7c, 0x15ea281d,
@@ -382,14 +383,12 @@ static void W(u32 *key, unsigned int i)
382 key[7] ^= F2(key[0], Tr[i % 4][7], Tm[i][7]); 383 key[7] ^= F2(key[0], Tr[i % 4][7], Tm[i][7]);
383} 384}
384 385
385static int cast6_setkey(struct crypto_tfm *tfm, const u8 *in_key, 386int __cast6_setkey(struct cast6_ctx *c, const u8 *in_key,
386 unsigned key_len) 387 unsigned key_len, u32 *flags)
387{ 388{
388 int i; 389 int i;
389 u32 key[8]; 390 u32 key[8];
390 __be32 p_key[8]; /* padded key */ 391 __be32 p_key[8]; /* padded key */
391 struct cast6_ctx *c = crypto_tfm_ctx(tfm);
392 u32 *flags = &tfm->crt_flags;
393 392
394 if (key_len % 4 != 0) { 393 if (key_len % 4 != 0) {
395 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; 394 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
@@ -425,6 +424,14 @@ static int cast6_setkey(struct crypto_tfm *tfm, const u8 *in_key,
425 424
426 return 0; 425 return 0;
427} 426}
427EXPORT_SYMBOL_GPL(__cast6_setkey);
428
429int cast6_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
430{
431 return __cast6_setkey(crypto_tfm_ctx(tfm), key, keylen,
432 &tfm->crt_flags);
433}
434EXPORT_SYMBOL_GPL(cast6_setkey);
428 435
429/*forward quad round*/ 436/*forward quad round*/
430static void Q(u32 *block, u8 *Kr, u32 *Km) 437static void Q(u32 *block, u8 *Kr, u32 *Km)
@@ -446,9 +453,8 @@ static void QBAR(u32 *block, u8 *Kr, u32 *Km)
446 block[2] ^= F1(block[3], Kr[0], Km[0]); 453 block[2] ^= F1(block[3], Kr[0], Km[0]);
447} 454}
448 455
449static void cast6_encrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf) 456void __cast6_encrypt(struct cast6_ctx *c, u8 *outbuf, const u8 *inbuf)
450{ 457{
451 struct cast6_ctx *c = crypto_tfm_ctx(tfm);
452 const __be32 *src = (const __be32 *)inbuf; 458 const __be32 *src = (const __be32 *)inbuf;
453 __be32 *dst = (__be32 *)outbuf; 459 __be32 *dst = (__be32 *)outbuf;
454 u32 block[4]; 460 u32 block[4];
@@ -478,10 +484,15 @@ static void cast6_encrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
478 dst[2] = cpu_to_be32(block[2]); 484 dst[2] = cpu_to_be32(block[2]);
479 dst[3] = cpu_to_be32(block[3]); 485 dst[3] = cpu_to_be32(block[3]);
480} 486}
487EXPORT_SYMBOL_GPL(__cast6_encrypt);
481 488
482static void cast6_decrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf) 489static void cast6_encrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
490{
491 __cast6_encrypt(crypto_tfm_ctx(tfm), outbuf, inbuf);
492}
493
494void __cast6_decrypt(struct cast6_ctx *c, u8 *outbuf, const u8 *inbuf)
483{ 495{
484 struct cast6_ctx *c = crypto_tfm_ctx(tfm);
485 const __be32 *src = (const __be32 *)inbuf; 496 const __be32 *src = (const __be32 *)inbuf;
486 __be32 *dst = (__be32 *)outbuf; 497 __be32 *dst = (__be32 *)outbuf;
487 u32 block[4]; 498 u32 block[4];
@@ -511,9 +522,17 @@ static void cast6_decrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
511 dst[2] = cpu_to_be32(block[2]); 522 dst[2] = cpu_to_be32(block[2]);
512 dst[3] = cpu_to_be32(block[3]); 523 dst[3] = cpu_to_be32(block[3]);
513} 524}
525EXPORT_SYMBOL_GPL(__cast6_decrypt);
526
527static void cast6_decrypt(struct crypto_tfm *tfm, u8 *outbuf, const u8 *inbuf)
528{
529 __cast6_decrypt(crypto_tfm_ctx(tfm), outbuf, inbuf);
530}
514 531
515static struct crypto_alg alg = { 532static struct crypto_alg alg = {
516 .cra_name = "cast6", 533 .cra_name = "cast6",
534 .cra_driver_name = "cast6-generic",
535 .cra_priority = 100,
517 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 536 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
518 .cra_blocksize = CAST6_BLOCK_SIZE, 537 .cra_blocksize = CAST6_BLOCK_SIZE,
519 .cra_ctxsize = sizeof(struct cast6_ctx), 538 .cra_ctxsize = sizeof(struct cast6_ctx),
@@ -544,3 +563,4 @@ module_exit(cast6_mod_fini);
544 563
545MODULE_LICENSE("GPL"); 564MODULE_LICENSE("GPL");
546MODULE_DESCRIPTION("Cast6 Cipher Algorithm"); 565MODULE_DESCRIPTION("Cast6 Cipher Algorithm");
566MODULE_ALIAS("cast6");