ALSA: usb-audio: add support for Akai MPD16
[safe/jmp/linux-2.6] / crypto / anubis.c
index 3925eb0..77530d5 100644 (file)
@@ -32,8 +32,9 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/mm.h>
-#include <asm/scatterlist.h>
+#include <asm/byteorder.h>
 #include <linux/crypto.h>
+#include <linux/types.h>
 
 #define ANUBIS_MIN_KEY_SIZE    16
 #define ANUBIS_MAX_KEY_SIZE    40
@@ -458,24 +459,23 @@ static const u32 rc[] = {
        0xf726ffedU, 0xe89d6f8eU, 0x19a0f089U,
 };
 
-static int anubis_setkey(void *ctx_arg, const u8 *in_key,
-                        unsigned int key_len, u32 *flags)
+static int anubis_setkey(struct crypto_tfm *tfm, const u8 *in_key,
+                        unsigned int key_len)
 {
-
-       int N, R, i, pos, r;
+       struct anubis_ctx *ctx = crypto_tfm_ctx(tfm);
+       const __be32 *key = (const __be32 *)in_key;
+       u32 *flags = &tfm->crt_flags;
+       int N, R, i, r;
        u32 kappa[ANUBIS_MAX_N];
        u32 inter[ANUBIS_MAX_N];
 
-       struct anubis_ctx *ctx = ctx_arg;
-
-       switch (key_len)
-       {
+       switch (key_len) {
                case 16: case 20: case 24: case 28:
                case 32: case 36: case 40:
                        break;
                default:
                        *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
-                       return - EINVAL;
+                       return -EINVAL;
        }
 
        ctx->key_len = key_len * 8;
@@ -483,13 +483,8 @@ static int anubis_setkey(void *ctx_arg, const u8 *in_key,
        ctx->R = R = 8 + N;
 
        /* * map cipher key to initial key state (mu): */
-               for (i = 0, pos = 0; i < N; i++, pos += 4) {
-               kappa[i] =
-                       (in_key[pos    ] << 24) ^
-                       (in_key[pos + 1] << 16) ^
-                       (in_key[pos + 2] <<  8) ^
-                       (in_key[pos + 3]      );
-       }
+       for (i = 0; i < N; i++)
+               kappa[i] = be32_to_cpu(key[i]);
 
        /*
         * generate R + 1 round keys:
@@ -534,23 +529,24 @@ static int anubis_setkey(void *ctx_arg, const u8 *in_key,
                /*
                 * compute kappa^{r+1} from kappa^r:
                 */
-               if (r == R) {
+               if (r == R)
                        break;
-               }
                for (i = 0; i < N; i++) {
                        int j = i;
                        inter[i]  = T0[(kappa[j--] >> 24)       ];
-                       if (j < 0) j = N - 1;
+                       if (j < 0)
+                               j = N - 1;
                        inter[i] ^= T1[(kappa[j--] >> 16) & 0xff];
-                       if (j < 0) j = N - 1;
+                       if (j < 0)
+                               j = N - 1;
                        inter[i] ^= T2[(kappa[j--] >>  8) & 0xff];
-                       if (j < 0) j = N - 1;
+                       if (j < 0)
+                               j = N - 1;
                        inter[i] ^= T3[(kappa[j  ]      ) & 0xff];
                }
                kappa[0] = inter[0] ^ rc[r];
-               for (i = 1; i < N; i++) {
+               for (i = 1; i < N; i++)
                        kappa[i] = inter[i];
-               }
        }
 
        /*
@@ -578,7 +574,9 @@ static int anubis_setkey(void *ctx_arg, const u8 *in_key,
 static void anubis_crypt(u32 roundKey[ANUBIS_MAX_ROUNDS + 1][4],
                u8 *ciphertext, const u8 *plaintext, const int R)
 {
-       int i, pos, r;
+       const __be32 *src = (const __be32 *)plaintext;
+       __be32 *dst = (__be32 *)ciphertext;
+       int i, r;
        u32 state[4];
        u32 inter[4];
 
@@ -586,14 +584,8 @@ static void anubis_crypt(u32 roundKey[ANUBIS_MAX_ROUNDS + 1][4],
         * map plaintext block to cipher state (mu)
         * and add initial round key (sigma[K^0]):
         */
-       for (i = 0, pos = 0; i < 4; i++, pos += 4) {
-               state[i] =
-                       (plaintext[pos    ] << 24) ^
-                       (plaintext[pos + 1] << 16) ^
-                       (plaintext[pos + 2] <<  8) ^
-                       (plaintext[pos + 3]      ) ^
-                       roundKey[0][i];
-       }
+       for (i = 0; i < 4; i++)
+               state[i] = be32_to_cpu(src[i]) ^ roundKey[0][i];
 
        /*
         * R - 1 full rounds:
@@ -663,24 +655,19 @@ static void anubis_crypt(u32 roundKey[ANUBIS_MAX_ROUNDS + 1][4],
         * map cipher state to ciphertext block (mu^{-1}):
         */
 
-       for (i = 0, pos = 0; i < 4; i++, pos += 4) {
-               u32 w = inter[i];
-               ciphertext[pos    ] = (u8)(w >> 24);
-               ciphertext[pos + 1] = (u8)(w >> 16);
-               ciphertext[pos + 2] = (u8)(w >>  8);
-               ciphertext[pos + 3] = (u8)(w      );
-       }
+       for (i = 0; i < 4; i++)
+               dst[i] = cpu_to_be32(inter[i]);
 }
 
-static void anubis_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
+static void anubis_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 {
-       struct anubis_ctx *ctx = ctx_arg;
+       struct anubis_ctx *ctx = crypto_tfm_ctx(tfm);
        anubis_crypt(ctx->E, dst, src, ctx->R);
 }
 
-static void anubis_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
+static void anubis_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 {
-       struct anubis_ctx *ctx = ctx_arg;
+       struct anubis_ctx *ctx = crypto_tfm_ctx(tfm);
        anubis_crypt(ctx->D, dst, src, ctx->R);
 }
 
@@ -689,6 +676,7 @@ static struct crypto_alg anubis_alg = {
        .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
        .cra_blocksize          =       ANUBIS_BLOCK_SIZE,
        .cra_ctxsize            =       sizeof (struct anubis_ctx),
+       .cra_alignmask          =       3,
        .cra_module             =       THIS_MODULE,
        .cra_list               =       LIST_HEAD_INIT(anubis_alg.cra_list),
        .cra_u                  =       { .cipher = {
@@ -699,21 +687,21 @@ static struct crypto_alg anubis_alg = {
        .cia_decrypt            =       anubis_decrypt } }
 };
 
-static int __init init(void)
+static int __init anubis_mod_init(void)
 {
        int ret = 0;
-       
+
        ret = crypto_register_alg(&anubis_alg);
        return ret;
 }
 
-static void __exit fini(void)
+static void __exit anubis_mod_fini(void)
 {
        crypto_unregister_alg(&anubis_alg);
 }
 
-module_init(init);
-module_exit(fini);
+module_init(anubis_mod_init);
+module_exit(anubis_mod_fini);
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Anubis Cryptographic Algorithm");