Linux Audio

Check our new training course

Loading...
v6.8
 1// SPDX-License-Identifier: GPL-2.0-or-later
 2/*
 3 * Cryptographic API
 4 *
 5 * ARC4 Cipher Algorithm
 6 *
 7 * Jon Oberheide <jon@oberheide.org>
 
 
 
 
 
 
 8 */
 9
10#include <crypto/arc4.h>
11#include <crypto/internal/skcipher.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
 
16
17#define ARC4_ALIGN __alignof__(struct arc4_ctx)
 
 
 
 
 
 
 
18
19static int crypto_arc4_setkey(struct crypto_lskcipher *tfm, const u8 *in_key,
20			      unsigned int key_len)
21{
22	struct arc4_ctx *ctx = crypto_lskcipher_ctx(tfm);
 
23
24	return arc4_setkey(ctx, in_key, key_len);
25}
26
27static int crypto_arc4_crypt(struct crypto_lskcipher *tfm, const u8 *src,
28			     u8 *dst, unsigned nbytes, u8 *siv, u32 flags)
29{
30	struct arc4_ctx *ctx = crypto_lskcipher_ctx(tfm);
31
32	if (!(flags & CRYPTO_LSKCIPHER_FLAG_CONT))
33		memcpy(siv, ctx, sizeof(*ctx));
34
35	ctx = (struct arc4_ctx *)siv;
 
 
 
 
 
 
 
36
37	arc4_crypt(ctx, dst, src, nbytes);
38	return 0;
39}
40
41static int crypto_arc4_init(struct crypto_lskcipher *tfm)
42{
43	pr_warn_ratelimited("\"%s\" (%ld) uses obsolete ecb(arc4) skcipher\n",
44			    current->comm, (unsigned long)current->pid);
45
46	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47}
48
49static struct lskcipher_alg arc4_alg = {
50	.co.base.cra_name		=	"arc4",
51	.co.base.cra_driver_name	=	"arc4-generic",
52	.co.base.cra_priority		=	100,
53	.co.base.cra_blocksize		=	ARC4_BLOCK_SIZE,
54	.co.base.cra_ctxsize		=	sizeof(struct arc4_ctx),
55	.co.base.cra_alignmask		=	ARC4_ALIGN - 1,
56	.co.base.cra_module		=	THIS_MODULE,
57	.co.min_keysize			=	ARC4_MIN_KEY_SIZE,
58	.co.max_keysize			=	ARC4_MAX_KEY_SIZE,
59	.co.statesize			=	sizeof(struct arc4_ctx),
60	.setkey				=	crypto_arc4_setkey,
61	.encrypt			=	crypto_arc4_crypt,
62	.decrypt			=	crypto_arc4_crypt,
63	.init				=	crypto_arc4_init,
64};
65
66static int __init arc4_init(void)
67{
68	return crypto_register_lskcipher(&arc4_alg);
69}
70
 
71static void __exit arc4_exit(void)
72{
73	crypto_unregister_lskcipher(&arc4_alg);
74}
75
76subsys_initcall(arc4_init);
77module_exit(arc4_exit);
78
79MODULE_LICENSE("GPL");
80MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
81MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");
82MODULE_ALIAS_CRYPTO("ecb(arc4)");
v3.5.6
 
  1/*
  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 */
 
 
 
 
 
 14#include <linux/module.h>
 15#include <linux/init.h>
 16#include <linux/crypto.h>
 17
 18#define ARC4_MIN_KEY_SIZE	1
 19#define ARC4_MAX_KEY_SIZE	256
 20#define ARC4_BLOCK_SIZE		1
 21
 22struct arc4_ctx {
 23	u8 S[256];
 24	u8 x, y;
 25};
 26
 27static int arc4_set_key(struct crypto_tfm *tfm, const u8 *in_key,
 28			unsigned int key_len)
 29{
 30	struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
 31	int i, j = 0, k = 0;
 32
 33	ctx->x = 1;
 34	ctx->y = 0;
 
 
 
 
 
 35
 36	for (i = 0; i < 256; i++)
 37		ctx->S[i] = i;
 38
 39	for (i = 0; i < 256; i++) {
 40		u8 a = ctx->S[i];
 41		j = (j + in_key[k] + a) & 0xff;
 42		ctx->S[i] = ctx->S[j];
 43		ctx->S[j] = a;
 44		if (++k >= key_len)
 45			k = 0;
 46	}
 47
 
 48	return 0;
 49}
 50
 51static void arc4_crypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
 52{
 53	struct arc4_ctx *ctx = crypto_tfm_ctx(tfm);
 
 54
 55	u8 *const S = ctx->S;
 56	u8 x = ctx->x;
 57	u8 y = ctx->y;
 58	u8 a, b;
 59
 60	a = S[x];
 61	y = (y + a) & 0xff;
 62	b = S[y];
 63	S[x] = b;
 64	S[y] = a;
 65	x = (x + 1) & 0xff;
 66	*out++ = *in ^ S[(a + b) & 0xff];
 67
 68	ctx->x = x;
 69	ctx->y = y;
 70}
 71
 72static struct crypto_alg arc4_alg = {
 73	.cra_name		=	"arc4",
 74	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
 75	.cra_blocksize		=	ARC4_BLOCK_SIZE,
 76	.cra_ctxsize		=	sizeof(struct arc4_ctx),
 77	.cra_module		=	THIS_MODULE,
 78	.cra_list		=	LIST_HEAD_INIT(arc4_alg.cra_list),
 79	.cra_u			=	{ .cipher = {
 80	.cia_min_keysize	=	ARC4_MIN_KEY_SIZE,
 81	.cia_max_keysize	=	ARC4_MAX_KEY_SIZE,
 82	.cia_setkey		=	arc4_set_key,
 83	.cia_encrypt		=	arc4_crypt,
 84	.cia_decrypt		=	arc4_crypt } }
 
 
 85};
 86
 87static int __init arc4_init(void)
 88{
 89	return crypto_register_alg(&arc4_alg);
 90}
 91
 92
 93static void __exit arc4_exit(void)
 94{
 95	crypto_unregister_alg(&arc4_alg);
 96}
 97
 98module_init(arc4_init);
 99module_exit(arc4_exit);
100
101MODULE_LICENSE("GPL");
102MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
103MODULE_AUTHOR("Jon Oberheide <jon@oberheide.org>");