crypto: tcrypt - add lrw(twofish) tests
[linux-flexiantxendom0-3.2.10.git] / crypto / serpent.c
index 465d091..eb61630 100644 (file)
 #include <asm/byteorder.h>
 #include <linux/crypto.h>
 #include <linux/types.h>
+#include <crypto/serpent.h>
 
 /* Key is padded to the maximum of 256 bits before round key generation.
  * Any key length <= 256 bits (32 bytes) is allowed by the algorithm.
  */
 
-#define SERPENT_MIN_KEY_SIZE             0
-#define SERPENT_MAX_KEY_SIZE            32
-#define SERPENT_EXPKEY_WORDS           132
-#define SERPENT_BLOCK_SIZE              16
-
 #define PHI 0x9e3779b9UL
 
 #define keyiter(a,b,c,d,i,j) \
        x1 ^= x4;       x3 ^= x4;       x4 &= x0;       \
        x4 ^= x2;
 
-struct serpent_ctx {
-       u32 expkey[SERPENT_EXPKEY_WORDS];
-};
-
-
-static int serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
-                         unsigned int keylen)
+int serpent_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
 {
        struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
        u32 *k = ctx->expkey;
@@ -359,15 +349,14 @@ static int serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(serpent_setkey);
 
-static void serpent_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
+void __serpent_encrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src)
 {
-       struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
-       const u32
-               *k = ctx->expkey,
-               *s = (const u32 *)src;
-       u32     *d = (u32 *)dst,
-               r0, r1, r2, r3, r4;
+       const u32 *k = ctx->expkey;
+       const __le32 *s = (const __le32 *)src;
+       __le32  *d = (__le32 *)dst;
+       u32     r0, r1, r2, r3, r4;
 
 /*
  * Note: The conversions between u8* and u32* might cause trouble
@@ -418,15 +407,21 @@ static void serpent_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
        d[2] = cpu_to_le32(r2);
        d[3] = cpu_to_le32(r3);
 }
+EXPORT_SYMBOL_GPL(__serpent_encrypt);
 
-static void serpent_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
+static void serpent_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 {
        struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
-       const u32
-               *k = ((struct serpent_ctx *)ctx)->expkey,
-               *s = (const u32 *)src;
-       u32     *d = (u32 *)dst,
-               r0, r1, r2, r3, r4;
+
+       __serpent_encrypt(ctx, dst, src);
+}
+
+void __serpent_decrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src)
+{
+       const u32 *k = ctx->expkey;
+       const __le32 *s = (const __le32 *)src;
+       __le32  *d = (__le32 *)dst;
+       u32     r0, r1, r2, r3, r4;
 
        r0 = le32_to_cpu(s[0]);
        r1 = le32_to_cpu(s[1]);
@@ -472,9 +467,19 @@ static void serpent_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
        d[2] = cpu_to_le32(r1);
        d[3] = cpu_to_le32(r4);
 }
+EXPORT_SYMBOL_GPL(__serpent_decrypt);
+
+static void serpent_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
+{
+       struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
+
+       __serpent_decrypt(ctx, dst, src);
+}
 
 static struct crypto_alg serpent_alg = {
        .cra_name               =       "serpent",
+       .cra_driver_name        =       "serpent-generic",
+       .cra_priority           =       100,
        .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
        .cra_blocksize          =       SERPENT_BLOCK_SIZE,
        .cra_ctxsize            =       sizeof(struct serpent_ctx),
@@ -557,7 +562,7 @@ static struct crypto_alg tnepres_alg = {
        .cia_decrypt            =       tnepres_decrypt } }
 };
 
-static int __init init(void)
+static int __init serpent_mod_init(void)
 {
        int ret = crypto_register_alg(&serpent_alg);
 
@@ -572,16 +577,17 @@ static int __init init(void)
        return ret;
 }
 
-static void __exit fini(void)
+static void __exit serpent_mod_fini(void)
 {
        crypto_unregister_alg(&tnepres_alg);
        crypto_unregister_alg(&serpent_alg);
 }
 
-module_init(init);
-module_exit(fini);
+module_init(serpent_mod_init);
+module_exit(serpent_mod_fini);
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Serpent and tnepres (kerneli compatible serpent reversed) Cipher Algorithm");
 MODULE_AUTHOR("Dag Arne Osvik <osvik@ii.uib.no>");
 MODULE_ALIAS("tnepres");
+MODULE_ALIAS("serpent");