#include <linux/mm.h>
#include <asm/uaccess.h>
#include <linux/linkage.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/final.h>MODULE_DESCRIPTION("My kernel module");
MODULE_AUTHOR("f (root@sitox)");
MODULE_LICENSE("$LICENSE$");
static int f_init_module(void)
{
printk( KERN_DEBUG "Module f init\n" );
return 0;
}
static void f_exit_module(void)
{
printk( KERN_DEBUG "Module f exit\n" );
}
module_init(f_init_module);
module_exit(f_exit_module);
#undef c2l
#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
l|=((unsigned long)(*((c)++)))<< 8L, \
l|=((unsigned long)(*((c)++)))<<16L, \
l|=((unsigned long)(*((c)++)))<<24L)
/* NOTE - c is not incremented as per c2l */
#undef c2ln
#define c2ln(c,l1,l2,n) { \
c+=n; \
l1=l2=0; \
switch (n) { \
case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
case 5: l2|=((unsigned long)(*(--(c)))); \
case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
case 1: l1|=((unsigned long)(*(--(c)))); \
} \
}
#undef l2c
#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
*((c)++)=(unsigned char)(((l)>>24L)&0xff))
/* NOTE - c is not incremented as per l2c */
#undef l2cn
#define l2cn(l1,l2,c,n) { \
c+=n; \
switch (n) { \
case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
} \
}
/* NOTE - c is not incremented as per n2l */
#define n2ln(c,l1,l2,n) { \
c+=n; \
l1=l2=0; \
switch (n) { \
case 8: l2 =((unsigned long)(*(--(c)))) ; \
case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
case 6: l2|=((unsigned long)(*(--(c))))<<16; \
case 5: l2|=((unsigned long)(*(--(c))))<<24; \
case 4: l1 =((unsigned long)(*(--(c)))) ; \
case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
case 2: l1|=((unsigned long)(*(--(c))))<<16; \
case 1: l1|=((unsigned long)(*(--(c))))<<24; \
} \
}
/* NOTE - c is not incremented as per l2n */
#define l2nn(l1,l2,c,n) { \
c+=n; \
switch (n) { \
case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
} \
}
#undef n2l
#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
l|=((unsigned long)(*((c)++)))<<16L, \
l|=((unsigned long)(*((c)++)))<< 8L, \
l|=((unsigned long)(*((c)++))))
#undef l2n
#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
*((c)++)=(unsigned char)(((l) )&0xff))
#if defined(BF_PTR2)
#define BF_ENC(LL,R,KEY,Pi) (\
LL^=KEY[Pi], \
t= KEY[BF_ROUNDS+2 + 0 + ((R>>24)&0xFF)], \
t+= KEY[BF_ROUNDS+2 + 256 + ((R>>16)&0xFF)], \
t^= KEY[BF_ROUNDS+2 + 512 + ((R>>8 )&0xFF)], \
t+= KEY[BF_ROUNDS+2 + 768 + ((R )&0xFF)], \
LL^=t \
)
#elif defined(BF_PTR)
#ifndef BF_LONG_LOG2
#define BF_LONG_LOG2 2 /* default to BF_LONG being 32 bits */
#endif
#define BF_M (0xFF<<BF_LONG_LOG2)
#define BF_0 (24-BF_LONG_LOG2)
#define BF_1 (16-BF_LONG_LOG2)
#define BF_2 ( 8-BF_LONG_LOG2)
#define BF_3 BF_LONG_LOG2 /* left shift */
#define BF_ENC(LL,R,S,P) ( \
LL^=P, \
LL^= (((*(BF_LONG *)((unsigned char *)&(S[ 0])+((R>>BF_0)&BF_M))+ \
*(BF_LONG *)((unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \
*(BF_LONG *)((unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \
*(BF_LONG *)((unsigned char *)&(S[768])+((R<<BF_3)&BF_M))) \
)
#else
#define BF_ENC(LL,R,S,P) ( \
LL^=P, \
LL^=((( S[ ((int)(R>>24)&0xff)] + \
S[0x0100+((int)(R>>16)&0xff)])^ \
S[0x0200+((int)(R>> 8)&0xff)])+ \
S[0x0300+((int)(R )&0xff)])&0xffffffffL \
)
#endif
#define BF_ROUNDS 16
#define BF_BLOCK 8
#define BF_LONG unsigned int
typedef struct bf_key_st
{
BF_LONG P[BF_ROUNDS+2];
BF_LONG S[4*256];
} BF_KEY;
#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
#error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
to modify the code.
#endif
void _BF_encrypt(BF_LONG *data, const BF_KEY *key)
{
#ifndef BF_PTR2
register BF_LONG l,r;
register const BF_LONG *p,*s;
p=key->P;
s= &(key->S[0]);
l=data[0];
r=data[1];
l^=p[0];
BF_ENC(r,l,s,p[ 1]);
BF_ENC(l,r,s,p[ 2]);
BF_ENC(r,l,s,p[ 3]);
BF_ENC(l,r,s,p[ 4]);
BF_ENC(r,l,s,p[ 5]);
BF_ENC(l,r,s,p[ 6]);
BF_ENC(r,l,s,p[ 7]);
BF_ENC(l,r,s,p[ 8]);
BF_ENC(r,l,s,p[ 9]);
BF_ENC(l,r,s,p[10]);
BF_ENC(r,l,s,p[11]);
BF_ENC(l,r,s,p[12]);
BF_ENC(r,l,s,p[13]);
BF_ENC(l,r,s,p[14]);
BF_ENC(r,l,s,p[15]);
BF_ENC(l,r,s,p[16]);
#if BF_ROUNDS == 20
BF_ENC(r,l,s,p[17]);
BF_ENC(l,r,s,p[18]);
BF_ENC(r,l,s,p[19]);
BF_ENC(l,r,s,p[20]);
#endif
r^=p[BF_ROUNDS+1];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#else
register BF_LONG l,r,t,*k;
l=data[0];
r=data[1];
k=(BF_LONG*)key;
l^=k[0];
BF_ENC(r,l,k, 1);
BF_ENC(l,r,k, 2);
BF_ENC(r,l,k, 3);
BF_ENC(l,r,k, 4);
BF_ENC(r,l,k, 5);
BF_ENC(l,r,k, 6);
BF_ENC(r,l,k, 7);
BF_ENC(l,r,k, 8);
BF_ENC(r,l,k, 9);
BF_ENC(l,r,k,10);
BF_ENC(r,l,k,11);
BF_ENC(l,r,k,12);
BF_ENC(r,l,k,13);
BF_ENC(l,r,k,14);
BF_ENC(r,l,k,15);
BF_ENC(l,r,k,16);
#if BF_ROUNDS == 20
BF_ENC(r,l,k,17);
BF_ENC(l,r,k,18);
BF_ENC(r,l,k,19);
BF_ENC(l,r,k,20);
#endif
r^=k[BF_ROUNDS+1];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#endif
}
static const BF_KEY bf_init= {
{
0x243f6a88L, 0x85a308d3L, 0x13198a2eL, 0x03707344L,
0xa4093822L, 0x299f31d0L, 0x082efa98L, 0xec4e6c89L,
0x452821e6L, 0x38d01377L, 0xbe5466cfL, 0x34e90c6cL,
0xc0ac29b7L, 0xc97c50ddL, 0x3f84d5b5L, 0xb5470917L,
0x9216d5d9L, 0x8979fb1b
},{
0xd1310ba6L, 0x98dfb5acL, 0x2ffd72dbL, 0xd01adfb7L,
0xb8e1afedL, 0x6a267e96L, 0xba7c9045L, 0xf12c7f99L,
0x24a19947L, /*Объявление Константы */0xb3916cf7L, 0x0801f2e2L, 0x858efc16L,
0x636920d8L,
}
};
asmlinkage void My_BF_set_key(BF_KEY *_key, int _len, const unsigned char *_data)
{
int i;
BF_LONG *p,ri,in[2];
const unsigned char *d,*end;
BF_KEY *key;
unsigned char *data = kmalloc(2048,GFP_KERNEL);
int len;
printk( KERN_DEBUG "Start setting key\n" );
printk( KERN_DEBUG "Data is :" );
// get_user(*key,_key);
copy_from_user(key,_key,sizeof(BF_KEY));
get_user(*data,_data);
get_user(len,&_len);
copy_to_user(key,&bf_init,sizeof(BF_KEY));
//put_user(bf_init,key);
printk( KERN_DEBUG "Memory operation is good" );
//memcpy(key,&bf_init,sizeof(BF_KEY)); //Original String
//RtlCopyMemory(key,&bf_init,sizeof(BF_KEY));
p=key->P;
if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4;
d=data;
end= &(data[len]);
for (i=0; i<(BF_ROUNDS+2); i++)
{
ri= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
p[i]^=ri;
}
in[0]=0L;
in[1]=0L;
for (i=0; i<(BF_ROUNDS+2); i+=2)
{
_BF_encrypt(in,key);
p[i ]=in[0];
p[i+1]=in[1];
}
p=key->S;
for (i=0; i<4*256; i+=2)
{
_BF_encrypt(in,key);
p[i ]=in[0];
p[i+1]=in[1];
}
printk( KERN_DEBUG "Outputing Data\n" );
//put_user(*_key,&key);
put_user(*_data,data);
copy_to_user(_key,key,sizeof(BF_KEY));
}
#ifndef BF_DEFAULT_OPTIONS
void _BF_decrypt(BF_LONG *data, const BF_KEY *key)
{
#ifndef BF_PTR2
register BF_LONG l,r;
register const BF_LONG *p,*s;
p=key->P;
s= &(key->S[0]);
l=data[0];
r=data[1];
l^=p[BF_ROUNDS+1];
#if BF_ROUNDS == 20
BF_ENC(r,l,s,p[20]);
BF_ENC(l,r,s,p[19]);
BF_ENC(r,l,s,p[18]);
BF_ENC(l,r,s,p[17]);
#endif
BF_ENC(r,l,s,p[16]);
BF_ENC(l,r,s,p[15]);
BF_ENC(r,l,s,p[14]);
BF_ENC(l,r,s,p[13]);
BF_ENC(r,l,s,p[12]);
BF_ENC(l,r,s,p[11]);
BF_ENC(r,l,s,p[10]);
BF_ENC(l,r,s,p[ 9]);
BF_ENC(r,l,s,p[ 8]);
BF_ENC(l,r,s,p[ 7]);
BF_ENC(r,l,s,p[ 6]);
BF_ENC(l,r,s,p[ 5]);
BF_ENC(r,l,s,p[ 4]);
BF_ENC(l,r,s,p[ 3]);
BF_ENC(r,l,s,p[ 2]);
BF_ENC(l,r,s,p[ 1]);
r^=p[0];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#else
register BF_LONG l,r,t,*k;
l=data[0];
r=data[1];
k=(BF_LONG *)key;
l^=k[BF_ROUNDS+1];
#if BF_ROUNDS == 20
BF_ENC(r,l,k,20);
BF_ENC(l,r,k,19);
BF_ENC(r,l,k,18);
BF_ENC(l,r,k,17);
#endif
BF_ENC(r,l,k,16);
BF_ENC(l,r,k,15);
BF_ENC(r,l,k,14);
BF_ENC(l,r,k,13);
BF_ENC(r,l,k,12);
BF_ENC(l,r,k,11);
BF_ENC(r,l,k,10);
BF_ENC(l,r,k, 9);
BF_ENC(r,l,k, 8);
BF_ENC(l,r,k, 7);
BF_ENC(r,l,k, 6);
BF_ENC(l,r,k, 5);
BF_ENC(r,l,k, 4);
BF_ENC(l,r,k, 3);
BF_ENC(r,l,k, 2);
BF_ENC(l,r,k, 1);
r^=k[0];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#endif
}
asmlinkage void My_BF_cbc_encrypt(const unsigned char *_in, unsigned char *_out, long _length,
const BF_KEY *_schedule, unsigned char *_ivec, int _encrypt)
{
register BF_LONG tin0,tin1;
register BF_LONG tout0,tout1,xor0,xor1;
long length;
int encrypt;
copy_from_user(&length,&_length,sizeof(_length));
copy_from_user(&encrypt,&_encrypt,sizeof(_encrypt));
register long l=length;
BF_LONG tin[2];
unsigned char *in = kmalloc(2048,GFP_KERNEL);
unsigned char *out = kmalloc(2048,GFP_KERNEL);
unsigned char *ivec = kmalloc(2048,GFP_KERNEL);
BF_KEY * schedule;
printk( KERN_DEBUG "Start Encrypting\n" );
//printk( KERN_DEBUG _in );
copy_from_user(schedule,_schedule,sizeof(BF_KEY));
get_user(*in,_in);
//printk( KERN_DEBUG in);
get_user(*out,_out);
//get_user(*schedule,_schedule);
get_user(*ivec,_ivec);
printk( KERN_DEBUG "Memory Change is complited\n" );
if (encrypt)
{
n2l(ivec,tout0);
n2l(ivec,tout1);
ivec-=8;
for (l-=8; l>=0; l-=8)
{
n2l(in,tin0);
n2l(in,tin1);
tin0^=tout0;
tin1^=tout1;
tin[0]=tin0;
tin[1]=tin1;
_BF_encrypt(tin,schedule);
tout0=tin[0];
tout1=tin[1];
l2n(tout0,out);
l2n(tout1,out);
}
if (l != -8)
{
n2ln(in,tin0,tin1,l+8);
tin0^=tout0;
tin1^=tout1;
tin[0]=tin0;
tin[1]=tin1;
_BF_encrypt(tin,schedule);
tout0=tin[0];
tout1=tin[1];
l2n(tout0,out);
l2n(tout1,out);
}
l2n(tout0,ivec);
l2n(tout1,ivec);
}
else
{
n2l(ivec,xor0);
n2l(ivec,xor1);
ivec-=8;
for (l-=8; l>=0; l-=8)
{
n2l(in,tin0);
n2l(in,tin1);
tin[0]=tin0;
tin[1]=tin1;
_BF_decrypt(tin,schedule);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2n(tout0,out);
l2n(tout1,out);
xor0=tin0;
xor1=tin1;
}
if (l != -8)
{
n2l(in,tin0);
n2l(in,tin1);
tin[0]=tin0;
tin[1]=tin1;
_BF_decrypt(tin,schedule);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2nn(tout0,tout1,out,l+8);
xor0=tin0;
xor1=tin1;
}
l2n(xor0,ivec);
l2n(xor1,ivec);
}
tin0=tin1=tout0=tout1=xor0=xor1=0;
tin[0]=tin[1]=0;
printk( KERN_DEBUG "encrypts end\n" );
put_user(*_in,in);
put_user(*_out,out);
//put_user(&_schedule,schedule);
put_user(*_ivec,ivec);
}
#endif