]> git.hungrycats.org Git - linux/commitdiff
[CRYPTO]: Add ARC4 module.
authorJon Oberheide <jon@focalhost.com>
Wed, 3 Mar 2004 16:15:04 +0000 (08:15 -0800)
committerStephen Hemminger <shemminger@osdl.org>
Wed, 3 Mar 2004 16:15:04 +0000 (08:15 -0800)
Documentation/crypto/api-intro.txt
crypto/Kconfig
crypto/Makefile
crypto/arc4.c [new file with mode: 0644]
crypto/tcrypt.c
crypto/tcrypt.h

index 65b64659546e234960a2059d6a0624798bbe952f..96260622b3d3a12ca1b026e8e17b0be9e53f9fdf 100644 (file)
@@ -186,6 +186,7 @@ Original developers of the crypto algorithms:
   Dag Arne Osvik (Serpent)
   Brian Gladman (AES)
   Kartikey Mahendra Bhatt (CAST6)
+  Jon Oberheide (ARC4)
 
 SHA1 algorithm contributors:
   Jean-Francois Dive
index 9421881f65ca752bf9d11905e739e64b67d0ac69..89441a40a1934ffaa2f63eb4b97692db9902e12d 100644 (file)
@@ -140,6 +140,16 @@ config CRYPTO_CAST6
          The CAST6 encryption algorithm (synonymous with CAST-256) is
          described in RFC2612.
 
+config CRYPTO_ARC4
+       tristate "ARC4 cipher algorithm"
+       depends on CRYPTO
+       help
+         ARC4 cipher algorithm.
+
+         This is a stream cipher using keys ranging from 8 bits to 2048
+         bits in length.  ARC4 is commonly used in protocols such as WEP 
+         and SSL.
+
 config CRYPTO_DEFLATE
        tristate "Deflate compression algorithm"
        depends on CRYPTO
index 515c0f1aac61e482787053f7b9292515abc5ece9..fbec5fe71127c146ea9b73c0a3e6faec89d747de 100644 (file)
@@ -21,6 +21,7 @@ obj-$(CONFIG_CRYPTO_SERPENT) += serpent.o
 obj-$(CONFIG_CRYPTO_AES) += aes.o
 obj-$(CONFIG_CRYPTO_CAST5) += cast5.o
 obj-$(CONFIG_CRYPTO_CAST6) += cast6.o
+obj-$(CONFIG_CRYPTO_ARC4) += arc4.o
 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
 
 obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o
diff --git a/crypto/arc4.c b/crypto/arc4.c
new file mode 100644 (file)
index 0000000..71a8447
--- /dev/null
@@ -0,0 +1,106 @@
+/* 
+ * Cryptographic API
+ *
+ * ARC4 Cipher Algorithm
+ *
+ * Jon Oberheide <jon@focalhost.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ */
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/crypto.h>
+
+#define ARC4_MIN_KEY_SIZE      1
+#define ARC4_MAX_KEY_SIZE      256
+#define ARC4_BLOCK_SIZE                1
+
+struct arc4_ctx {
+       u8 S[256];
+       u8 x, y;
+};
+
+static int arc4_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
+{
+       struct arc4_ctx *ctx = ctx_arg;
+       int i, j = 0, k = 0;
+
+       ctx->x = 1;
+       ctx->y = 0;
+
+       for(i = 0; i < 256; i++)
+               ctx->S[i] = i;
+
+       for(i = 0; i < 256; i++)
+       {
+               u8 a = ctx->S[i];
+               j = (j + in_key[k] + a) & 0xff;
+               ctx->S[i] = ctx->S[j];
+               ctx->S[j] = a;
+               if(++k >= key_len)
+                       k = 0;
+       }
+
+       /* TODO: dump the first 768 bytes generated as recommended
+          by Ilya Mironov (http://eprint.iacr.org/2002/067/) to 
+          increase the statistical strength of the state table */
+
+       return 0;
+}
+
+static void arc4_crypt(void *ctx_arg, u8 *out, const u8 *in)
+{
+       struct arc4_ctx *ctx = ctx_arg;
+
+       u8 *const S = ctx->S;
+       u8 x = ctx->x;
+       u8 y = ctx->y;
+
+       u8 a = S[x];
+       y = (y + a) & 0xff;
+       u8 b = S[y];
+       S[x] = b;
+       S[y] = a;
+       x = (x + 1) & 0xff;
+       *out++ = *in ^ S[(a + b) & 0xff];
+
+       ctx->x = x;
+       ctx->y = y;
+}
+
+static struct crypto_alg arc4_alg = {
+       .cra_name               =       "arc4",
+       .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
+       .cra_blocksize          =       ARC4_BLOCK_SIZE,
+       .cra_ctxsize            =       sizeof(struct arc4_ctx),
+       .cra_module             =       THIS_MODULE,
+       .cra_list               =       LIST_HEAD_INIT(arc4_alg.cra_list),
+       .cra_u                  =       { .cipher = {
+       .cia_min_keysize        =       ARC4_MIN_KEY_SIZE,
+       .cia_max_keysize        =       ARC4_MAX_KEY_SIZE,
+       .cia_setkey             =       arc4_set_key,
+       .cia_encrypt            =       arc4_crypt,
+       .cia_decrypt            =       arc4_crypt } }
+};
+
+static int __init arc4_init(void)
+{
+       return crypto_register_alg(&arc4_alg);
+}
+
+
+static void __exit arc4_exit(void)
+{
+       crypto_unregister_alg(&arc4_alg);
+}
+
+module_init(arc4_init);
+module_exit(arc4_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
+MODULE_AUTHOR("Jon Oberheide <jon@focalhost.com>");
index 3c1c0ae075b57a6bb0e6b55b2b910dda2ae018c2..12324fab47661a07d8e948746544bdf66a518de5 100644 (file)
@@ -61,7 +61,7 @@ static char *tvmem;
 static char *check[] = {
        "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
        "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6", 
-       "deflate", NULL
+       "arc4", "deflate", NULL
 };
 
 static void
@@ -556,6 +556,10 @@ do_test(void)
                test_cipher ("cast6", MODE_ECB, ENCRYPT, cast6_enc_tv_template, CAST6_ENC_TEST_VECTORS);
                test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
 
+               //ARC4
+               test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
+               test_cipher ("arc4x", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
+
                test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
                test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
                test_deflate();         
@@ -638,6 +642,11 @@ do_test(void)
                test_cipher ("cast6", MODE_ECB, DECRYPT, cast6_dec_tv_template, CAST6_DEC_TEST_VECTORS);
                break;
 
+       case 16:
+               test_cipher ("arc4", MODE_ECB, ENCRYPT, arc4_enc_tv_template, ARC4_ENC_TEST_VECTORS);
+               test_cipher ("arc4", MODE_ECB, DECRYPT, arc4_dec_tv_template, ARC4_DEC_TEST_VECTORS);
+               break;
+
 #ifdef CONFIG_CRYPTO_HMAC
        case 100:
                test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
index 95b93769e38db6ac59b3baa7a2eecd28d16543e0..0880165b1400fb925a340ab19c6a088ea1d033de 100644 (file)
@@ -1488,6 +1488,147 @@ struct cipher_testvec cast5_dec_tv_template[] =
        },
 };
 
+/* 
+ * ARC4 test vectors from OpenSSL 
+ */
+#define ARC4_ENC_TEST_VECTORS  7
+#define ARC4_DEC_TEST_VECTORS  7
+
+struct cipher_testvec arc4_enc_tv_template[] =
+{
+       {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .klen   = 8,
+               .input  = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .ilen   = 8,
+               .result = { 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96 },
+               .rlen   = 8,
+       }, {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .klen   = 8,
+               .input  = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .ilen   = 8,
+               .result = { 0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79 },
+               .rlen   = 8,
+       }, {
+               .key    = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .klen   = 8,
+               .input  = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .ilen   = 8,
+               .result = { 0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a },
+               .rlen   = 8,
+       }, {
+               .key    = { 0xef, 0x01, 0x23, 0x45},
+               .klen   = 4,
+               .input  = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00, 0x00, 0x00 },
+               .ilen   = 20,
+               .result = { 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
+                           0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba,
+                           0x36, 0xb6, 0x78, 0x58 },
+               .rlen   = 20,
+       }, {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .klen   = 8,
+               .input  = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
+                           0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
+                           0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
+                           0x12, 0x34, 0x56, 0x78 },
+               .ilen   = 28,
+               .result = { 0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89,
+                           0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c,
+                           0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87,
+                           0x40, 0x01, 0x1e, 0xcf },
+               .rlen   = 28,
+       }, {
+               .key    = { 0xef, 0x01, 0x23, 0x45 },
+               .klen   = 4,
+               .input  = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00 },
+               .ilen   = 10,
+               .result = { 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
+                           0xbd, 0x61 },
+               .rlen   = 10,
+       }, {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
+                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .klen   = 16,
+               .input  = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
+               .ilen   = 8,
+               .result = { 0x69, 0x72, 0x36, 0x59, 0x1B, 0x52, 0x42, 0xB1 },
+               .rlen   = 8,
+       },
+};
+
+struct cipher_testvec arc4_dec_tv_template[] =
+{
+       {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .klen   = 8,
+               .input  = { 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96 },
+               .ilen   = 8,
+               .result = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .rlen   = 8,
+       }, {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .klen   = 8,
+               .input  = { 0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79 },
+               .ilen   = 8,
+               .result = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .rlen   = 8,
+       }, {
+               .key    = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .klen   = 8,
+               .input  = { 0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a },
+               .ilen   = 8,
+               .result = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .rlen   = 8,
+       }, {
+               .key    = { 0xef, 0x01, 0x23, 0x45},
+               .klen   = 4,
+               .input  = { 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
+                           0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba,
+                           0x36, 0xb6, 0x78, 0x58 },
+               .ilen   = 20,
+               .result = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00, 0x00, 0x00 },
+               .rlen   = 20,
+       }, {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef },
+               .klen   = 8,
+               .input  = { 0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89,
+                           0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c,
+                           0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87,
+                           0x40, 0x01, 0x1e, 0xcf },
+               .ilen   = 28,
+               .result = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
+                           0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
+                           0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
+                           0x12, 0x34, 0x56, 0x78 },
+               .rlen   = 28,
+       }, {
+               .key    = { 0xef, 0x01, 0x23, 0x45 },
+               .klen   = 4,
+               .input  = { 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
+                           0xbd, 0x61 },
+               .ilen   = 10,
+               .result = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00 },
+               .rlen   = 10,
+       }, {
+               .key    = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
+                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+               .klen   = 16,
+               .input  = { 0x69, 0x72, 0x36, 0x59, 0x1B, 0x52, 0x42, 0xB1 },
+               .ilen   = 8,
+               .result = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
+               .rlen   = 8,
+       },
+};
+
+
 /*
  * Compression stuff.
  */