Mati Vait | cfa2b54 | 2011-06-08 21:26:00 +0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Cryptographic API |
| 3 | * |
| 4 | * ARC4 Cipher Algorithm |
| 5 | * |
| 6 | * Jon Oberheide <jon@oberheide.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | */ |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 14 | |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 15 | #include <crypto/algapi.h> |
Iuliana Prodan | bd30cf5 | 2019-02-08 15:50:08 +0200 | [diff] [blame] | 16 | #include <crypto/arc4.h> |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 17 | #include <crypto/internal/skcipher.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | struct arc4_ctx { |
Jussi Kivilinna | d366db6 | 2012-06-09 18:25:46 +0300 | [diff] [blame] | 22 | u32 S[256]; |
| 23 | u32 x, y; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 26 | static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 27 | unsigned int key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 29 | struct arc4_ctx *ctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | int i, j = 0, k = 0; |
| 31 | |
| 32 | ctx->x = 1; |
| 33 | ctx->y = 0; |
| 34 | |
Mati Vait | cfa2b54 | 2011-06-08 21:26:00 +0800 | [diff] [blame] | 35 | for (i = 0; i < 256; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | ctx->S[i] = i; |
| 37 | |
Mati Vait | cfa2b54 | 2011-06-08 21:26:00 +0800 | [diff] [blame] | 38 | for (i = 0; i < 256; i++) { |
Jussi Kivilinna | d366db6 | 2012-06-09 18:25:46 +0300 | [diff] [blame] | 39 | u32 a = ctx->S[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | j = (j + in_key[k] + a) & 0xff; |
| 41 | ctx->S[i] = ctx->S[j]; |
| 42 | ctx->S[j] = a; |
Mati Vait | cfa2b54 | 2011-06-08 21:26:00 +0800 | [diff] [blame] | 43 | if (++k >= key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | k = 0; |
| 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 50 | static int arc4_set_key_skcipher(struct crypto_skcipher *tfm, const u8 *in_key, |
| 51 | unsigned int key_len) |
| 52 | { |
| 53 | return arc4_set_key(&tfm->base, in_key, key_len); |
| 54 | } |
| 55 | |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 56 | static void arc4_crypt(struct arc4_ctx *ctx, u8 *out, const u8 *in, |
| 57 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Jussi Kivilinna | d366db6 | 2012-06-09 18:25:46 +0300 | [diff] [blame] | 59 | u32 *const S = ctx->S; |
| 60 | u32 x, y, a, b; |
| 61 | u32 ty, ta, tb; |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 62 | |
| 63 | if (len == 0) |
| 64 | return; |
| 65 | |
| 66 | x = ctx->x; |
| 67 | y = ctx->y; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | a = S[x]; |
| 70 | y = (y + a) & 0xff; |
| 71 | b = S[y]; |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 72 | |
| 73 | do { |
| 74 | S[y] = a; |
| 75 | a = (a + b) & 0xff; |
| 76 | S[x] = b; |
| 77 | x = (x + 1) & 0xff; |
| 78 | ta = S[x]; |
| 79 | ty = (y + ta) & 0xff; |
| 80 | tb = S[ty]; |
| 81 | *out++ = *in++ ^ S[a]; |
| 82 | if (--len == 0) |
| 83 | break; |
| 84 | y = ty; |
| 85 | a = ta; |
| 86 | b = tb; |
| 87 | } while (true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | ctx->x = x; |
| 90 | ctx->y = y; |
| 91 | } |
| 92 | |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 93 | static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
| 94 | { |
| 95 | arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1); |
| 96 | } |
| 97 | |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 98 | static int ecb_arc4_crypt(struct skcipher_request *req) |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 99 | { |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 100 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 101 | struct arc4_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 102 | struct skcipher_walk walk; |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 103 | int err; |
| 104 | |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 105 | err = skcipher_walk_virt(&walk, req, false); |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 106 | |
| 107 | while (walk.nbytes > 0) { |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 108 | arc4_crypt(ctx, walk.dst.virt.addr, walk.src.virt.addr, |
| 109 | walk.nbytes); |
| 110 | err = skcipher_walk_done(&walk, 0); |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return err; |
| 114 | } |
| 115 | |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 116 | static struct crypto_alg arc4_cipher = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .cra_name = "arc4", |
| 118 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 119 | .cra_blocksize = ARC4_BLOCK_SIZE, |
| 120 | .cra_ctxsize = sizeof(struct arc4_ctx), |
| 121 | .cra_module = THIS_MODULE, |
Jussi Kivilinna | ce6dd36 | 2012-06-09 18:25:40 +0300 | [diff] [blame] | 122 | .cra_u = { |
| 123 | .cipher = { |
| 124 | .cia_min_keysize = ARC4_MIN_KEY_SIZE, |
| 125 | .cia_max_keysize = ARC4_MAX_KEY_SIZE, |
| 126 | .cia_setkey = arc4_set_key, |
| 127 | .cia_encrypt = arc4_crypt_one, |
| 128 | .cia_decrypt = arc4_crypt_one, |
| 129 | }, |
| 130 | }, |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static struct skcipher_alg arc4_skcipher = { |
| 134 | .base.cra_name = "ecb(arc4)", |
| 135 | .base.cra_priority = 100, |
| 136 | .base.cra_blocksize = ARC4_BLOCK_SIZE, |
| 137 | .base.cra_ctxsize = sizeof(struct arc4_ctx), |
| 138 | .base.cra_module = THIS_MODULE, |
| 139 | .min_keysize = ARC4_MIN_KEY_SIZE, |
| 140 | .max_keysize = ARC4_MAX_KEY_SIZE, |
| 141 | .setkey = arc4_set_key_skcipher, |
| 142 | .encrypt = ecb_arc4_crypt, |
| 143 | .decrypt = ecb_arc4_crypt, |
| 144 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | static int __init arc4_init(void) |
| 147 | { |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 148 | int err; |
| 149 | |
| 150 | err = crypto_register_alg(&arc4_cipher); |
| 151 | if (err) |
| 152 | return err; |
| 153 | |
| 154 | err = crypto_register_skcipher(&arc4_skcipher); |
| 155 | if (err) |
| 156 | crypto_unregister_alg(&arc4_cipher); |
| 157 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | static void __exit arc4_exit(void) |
| 161 | { |
Eric Biggers | 426bcb5 | 2019-01-03 20:16:23 -0800 | [diff] [blame] | 162 | crypto_unregister_alg(&arc4_cipher); |
| 163 | crypto_unregister_skcipher(&arc4_skcipher); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame^] | 166 | subsys_initcall(arc4_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | module_exit(arc4_exit); |
| 168 | |
| 169 | MODULE_LICENSE("GPL"); |
| 170 | MODULE_DESCRIPTION("ARC4 Cipher Algorithm"); |
| 171 | MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 172 | MODULE_ALIAS_CRYPTO("arc4"); |