OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [crypto/] [arc4.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/*
/*
 * Cryptographic API
 * Cryptographic API
 *
 *
 * ARC4 Cipher Algorithm
 * ARC4 Cipher Algorithm
 *
 *
 * Jon Oberheide <jon@focalhost.com>
 * Jon Oberheide <jon@focalhost.com>
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * 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
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * (at your option) any later version.
 *
 *
 */
 */
#include <linux/module.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/init.h>
#include <linux/crypto.h>
#include <linux/crypto.h>
 
 
#define ARC4_MIN_KEY_SIZE       1
#define ARC4_MIN_KEY_SIZE       1
#define ARC4_MAX_KEY_SIZE       256
#define ARC4_MAX_KEY_SIZE       256
#define ARC4_BLOCK_SIZE         1
#define ARC4_BLOCK_SIZE         1
 
 
struct arc4_ctx {
struct arc4_ctx {
        u8 S[256];
        u8 S[256];
        u8 x, y;
        u8 x, y;
};
};
 
 
static int arc4_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
static int arc4_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
{
{
        struct arc4_ctx *ctx = ctx_arg;
        struct arc4_ctx *ctx = ctx_arg;
        int i, j = 0, k = 0;
        int i, j = 0, k = 0;
 
 
        ctx->x = 1;
        ctx->x = 1;
        ctx->y = 0;
        ctx->y = 0;
 
 
        for(i = 0; i < 256; i++)
        for(i = 0; i < 256; i++)
                ctx->S[i] = i;
                ctx->S[i] = i;
 
 
        for(i = 0; i < 256; i++)
        for(i = 0; i < 256; i++)
        {
        {
                u8 a = ctx->S[i];
                u8 a = ctx->S[i];
                j = (j + in_key[k] + a) & 0xff;
                j = (j + in_key[k] + a) & 0xff;
                ctx->S[i] = ctx->S[j];
                ctx->S[i] = ctx->S[j];
                ctx->S[j] = a;
                ctx->S[j] = a;
                if(++k >= key_len)
                if(++k >= key_len)
                        k = 0;
                        k = 0;
        }
        }
 
 
        return 0;
        return 0;
}
}
 
 
static void arc4_crypt(void *ctx_arg, u8 *out, const u8 *in)
static void arc4_crypt(void *ctx_arg, u8 *out, const u8 *in)
{
{
        struct arc4_ctx *ctx = ctx_arg;
        struct arc4_ctx *ctx = ctx_arg;
 
 
        u8 *const S = ctx->S;
        u8 *const S = ctx->S;
        u8 x = ctx->x;
        u8 x = ctx->x;
        u8 y = ctx->y;
        u8 y = ctx->y;
        u8 a, b;
        u8 a, b;
 
 
        a = S[x];
        a = S[x];
        y = (y + a) & 0xff;
        y = (y + a) & 0xff;
        b = S[y];
        b = S[y];
        S[x] = b;
        S[x] = b;
        S[y] = a;
        S[y] = a;
        x = (x + 1) & 0xff;
        x = (x + 1) & 0xff;
        *out++ = *in ^ S[(a + b) & 0xff];
        *out++ = *in ^ S[(a + b) & 0xff];
 
 
        ctx->x = x;
        ctx->x = x;
        ctx->y = y;
        ctx->y = y;
}
}
 
 
static struct crypto_alg arc4_alg = {
static struct crypto_alg arc4_alg = {
        .cra_name               =       "arc4",
        .cra_name               =       "arc4",
        .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
        .cra_flags              =       CRYPTO_ALG_TYPE_CIPHER,
        .cra_blocksize          =       ARC4_BLOCK_SIZE,
        .cra_blocksize          =       ARC4_BLOCK_SIZE,
        .cra_ctxsize            =       sizeof(struct arc4_ctx),
        .cra_ctxsize            =       sizeof(struct arc4_ctx),
        .cra_module             =       THIS_MODULE,
        .cra_module             =       THIS_MODULE,
        .cra_list               =       LIST_HEAD_INIT(arc4_alg.cra_list),
        .cra_list               =       LIST_HEAD_INIT(arc4_alg.cra_list),
        .cra_u                  =       { .cipher = {
        .cra_u                  =       { .cipher = {
        .cia_min_keysize        =       ARC4_MIN_KEY_SIZE,
        .cia_min_keysize        =       ARC4_MIN_KEY_SIZE,
        .cia_max_keysize        =       ARC4_MAX_KEY_SIZE,
        .cia_max_keysize        =       ARC4_MAX_KEY_SIZE,
        .cia_setkey             =       arc4_set_key,
        .cia_setkey             =       arc4_set_key,
        .cia_encrypt            =       arc4_crypt,
        .cia_encrypt            =       arc4_crypt,
        .cia_decrypt            =       arc4_crypt } }
        .cia_decrypt            =       arc4_crypt } }
};
};
 
 
static int __init arc4_init(void)
static int __init arc4_init(void)
{
{
        return crypto_register_alg(&arc4_alg);
        return crypto_register_alg(&arc4_alg);
}
}
 
 
 
 
static void __exit arc4_exit(void)
static void __exit arc4_exit(void)
{
{
        crypto_unregister_alg(&arc4_alg);
        crypto_unregister_alg(&arc4_alg);
}
}
 
 
module_init(arc4_init);
module_init(arc4_init);
module_exit(arc4_exit);
module_exit(arc4_exit);
 
 
MODULE_LICENSE("GPL");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
MODULE_DESCRIPTION("ARC4 Cipher Algorithm");
MODULE_AUTHOR("Jon Oberheide <jon@focalhost.com>");
MODULE_AUTHOR("Jon Oberheide <jon@focalhost.com>");
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.