/*
|
/*
|
* ncpsign_kernel.c
|
* ncpsign_kernel.c
|
*
|
*
|
* Arne de Bruijn (arne@knoware.nl), 1997
|
* Arne de Bruijn (arne@knoware.nl), 1997
|
*
|
*
|
*/
|
*/
|
|
|
#include <linux/config.h>
|
#include <linux/config.h>
|
|
|
#ifdef CONFIG_NCPFS_PACKET_SIGNING
|
#ifdef CONFIG_NCPFS_PACKET_SIGNING
|
|
|
#if 0
|
#if 0
|
#ifdef MODULE
|
#ifdef MODULE
|
#include <linux/module.h>
|
#include <linux/module.h>
|
#include <linux/version.h>
|
#include <linux/version.h>
|
#endif
|
#endif
|
#endif
|
#endif
|
|
|
#include <linux/string.h>
|
#include <linux/string.h>
|
#include <linux/ncp.h>
|
#include <linux/ncp.h>
|
#if 0
|
#if 0
|
#include <linux/ncp_fs.h>
|
#include <linux/ncp_fs.h>
|
#include <linux/ncp_fs_sb.h>
|
#include <linux/ncp_fs_sb.h>
|
#endif
|
#endif
|
#include "ncpsign_kernel.h"
|
#include "ncpsign_kernel.h"
|
|
|
#define rol32(i,c) (((((i)&0xffffffff)<<c)&0xffffffff)| \
|
#define rol32(i,c) (((((i)&0xffffffff)<<c)&0xffffffff)| \
|
(((i)&0xffffffff)>>(32-c)))
|
(((i)&0xffffffff)>>(32-c)))
|
/* i386: 32-bit, little endian, handles mis-alignment */
|
/* i386: 32-bit, little endian, handles mis-alignment */
|
#ifdef __i386__
|
#ifdef __i386__
|
#define GET_LE32(p) (*(int *)(p))
|
#define GET_LE32(p) (*(int *)(p))
|
#define PUT_LE32(p,v) { *(int *)(p)=v; }
|
#define PUT_LE32(p,v) { *(int *)(p)=v; }
|
#else
|
#else
|
/* from include/ncplib.h */
|
/* from include/ncplib.h */
|
#define BVAL(buf,pos) (((__u8 *)(buf))[pos])
|
#define BVAL(buf,pos) (((__u8 *)(buf))[pos])
|
#define PVAL(buf,pos) ((unsigned)BVAL(buf,pos))
|
#define PVAL(buf,pos) ((unsigned)BVAL(buf,pos))
|
#define BSET(buf,pos,val) (BVAL(buf,pos) = (val))
|
#define BSET(buf,pos,val) (BVAL(buf,pos) = (val))
|
|
|
static inline word
|
static inline word
|
WVAL_LH(__u8 * buf, int pos)
|
WVAL_LH(__u8 * buf, int pos)
|
{
|
{
|
return PVAL(buf, pos) | PVAL(buf, pos + 1) << 8;
|
return PVAL(buf, pos) | PVAL(buf, pos + 1) << 8;
|
}
|
}
|
static inline dword
|
static inline dword
|
DVAL_LH(__u8 * buf, int pos)
|
DVAL_LH(__u8 * buf, int pos)
|
{
|
{
|
return WVAL_LH(buf, pos) | WVAL_LH(buf, pos + 2) << 16;
|
return WVAL_LH(buf, pos) | WVAL_LH(buf, pos + 2) << 16;
|
}
|
}
|
static inline void
|
static inline void
|
WSET_LH(__u8 * buf, int pos, word val)
|
WSET_LH(__u8 * buf, int pos, word val)
|
{
|
{
|
BSET(buf, pos, val & 0xff);
|
BSET(buf, pos, val & 0xff);
|
BSET(buf, pos + 1, val >> 8);
|
BSET(buf, pos + 1, val >> 8);
|
}
|
}
|
static inline void
|
static inline void
|
DSET_LH(__u8 * buf, int pos, dword val)
|
DSET_LH(__u8 * buf, int pos, dword val)
|
{
|
{
|
WSET_LH(buf, pos, val & 0xffff);
|
WSET_LH(buf, pos, val & 0xffff);
|
WSET_LH(buf, pos + 2, val >> 16);
|
WSET_LH(buf, pos + 2, val >> 16);
|
}
|
}
|
|
|
#define GET_LE32(p) DVAL_LH(p,0)
|
#define GET_LE32(p) DVAL_LH(p,0)
|
#define PUT_LE32(p,v) DSET_LH(p,0,v)
|
#define PUT_LE32(p,v) DSET_LH(p,0,v)
|
#endif
|
#endif
|
|
|
#define min(a,b) ((a)<(b)?(a):(b))
|
#define min(a,b) ((a)<(b)?(a):(b))
|
|
|
static void nwsign(char *r_data1, char *r_data2, char *outdata) {
|
static void nwsign(char *r_data1, char *r_data2, char *outdata) {
|
int i;
|
int i;
|
unsigned int w0,w1,w2,w3;
|
unsigned int w0,w1,w2,w3;
|
static int rbit[4]={0, 2, 1, 3};
|
static int rbit[4]={0, 2, 1, 3};
|
#ifdef __i386__
|
#ifdef __i386__
|
unsigned int *data2=(int *)r_data2;
|
unsigned int *data2=(int *)r_data2;
|
#else
|
#else
|
unsigned int data2[16];
|
unsigned int data2[16];
|
for (i=0;i<16;i++)
|
for (i=0;i<16;i++)
|
data2[i]=GET_LE32(r_data2+(i<<2));
|
data2[i]=GET_LE32(r_data2+(i<<2));
|
#endif
|
#endif
|
w0=GET_LE32(r_data1);
|
w0=GET_LE32(r_data1);
|
w1=GET_LE32(r_data1+4);
|
w1=GET_LE32(r_data1+4);
|
w2=GET_LE32(r_data1+8);
|
w2=GET_LE32(r_data1+8);
|
w3=GET_LE32(r_data1+12);
|
w3=GET_LE32(r_data1+12);
|
for (i=0;i<16;i+=4) {
|
for (i=0;i<16;i+=4) {
|
w0=rol32(w0 + ((w1 & w2) | ((~w1) & w3)) + data2[i+0],3);
|
w0=rol32(w0 + ((w1 & w2) | ((~w1) & w3)) + data2[i+0],3);
|
w3=rol32(w3 + ((w0 & w1) | ((~w0) & w2)) + data2[i+1],7);
|
w3=rol32(w3 + ((w0 & w1) | ((~w0) & w2)) + data2[i+1],7);
|
w2=rol32(w2 + ((w3 & w0) | ((~w3) & w1)) + data2[i+2],11);
|
w2=rol32(w2 + ((w3 & w0) | ((~w3) & w1)) + data2[i+2],11);
|
w1=rol32(w1 + ((w2 & w3) | ((~w2) & w0)) + data2[i+3],19);
|
w1=rol32(w1 + ((w2 & w3) | ((~w2) & w0)) + data2[i+3],19);
|
}
|
}
|
for (i=0;i<4;i++) {
|
for (i=0;i<4;i++) {
|
w0=rol32(w0 + (((w2 | w3) & w1) | (w2 & w3)) + 0x5a827999 + data2[i+0],3);
|
w0=rol32(w0 + (((w2 | w3) & w1) | (w2 & w3)) + 0x5a827999 + data2[i+0],3);
|
w3=rol32(w3 + (((w1 | w2) & w0) | (w1 & w2)) + 0x5a827999 + data2[i+4],5);
|
w3=rol32(w3 + (((w1 | w2) & w0) | (w1 & w2)) + 0x5a827999 + data2[i+4],5);
|
w2=rol32(w2 + (((w0 | w1) & w3) | (w0 & w1)) + 0x5a827999 + data2[i+8],9);
|
w2=rol32(w2 + (((w0 | w1) & w3) | (w0 & w1)) + 0x5a827999 + data2[i+8],9);
|
w1=rol32(w1 + (((w3 | w0) & w2) | (w3 & w0)) + 0x5a827999 + data2[i+12],13);
|
w1=rol32(w1 + (((w3 | w0) & w2) | (w3 & w0)) + 0x5a827999 + data2[i+12],13);
|
}
|
}
|
for (i=0;i<4;i++) {
|
for (i=0;i<4;i++) {
|
w0=rol32(w0 + ((w1 ^ w2) ^ w3) + 0x6ed9eba1 + data2[rbit[i]+0],3);
|
w0=rol32(w0 + ((w1 ^ w2) ^ w3) + 0x6ed9eba1 + data2[rbit[i]+0],3);
|
w3=rol32(w3 + ((w0 ^ w1) ^ w2) + 0x6ed9eba1 + data2[rbit[i]+8],9);
|
w3=rol32(w3 + ((w0 ^ w1) ^ w2) + 0x6ed9eba1 + data2[rbit[i]+8],9);
|
w2=rol32(w2 + ((w3 ^ w0) ^ w1) + 0x6ed9eba1 + data2[rbit[i]+4],11);
|
w2=rol32(w2 + ((w3 ^ w0) ^ w1) + 0x6ed9eba1 + data2[rbit[i]+4],11);
|
w1=rol32(w1 + ((w2 ^ w3) ^ w0) + 0x6ed9eba1 + data2[rbit[i]+12],15);
|
w1=rol32(w1 + ((w2 ^ w3) ^ w0) + 0x6ed9eba1 + data2[rbit[i]+12],15);
|
}
|
}
|
PUT_LE32(outdata,(w0+GET_LE32(r_data1)) & 0xffffffff);
|
PUT_LE32(outdata,(w0+GET_LE32(r_data1)) & 0xffffffff);
|
PUT_LE32(outdata+4,(w1+GET_LE32(r_data1+4)) & 0xffffffff);
|
PUT_LE32(outdata+4,(w1+GET_LE32(r_data1+4)) & 0xffffffff);
|
PUT_LE32(outdata+8,(w2+GET_LE32(r_data1+8)) & 0xffffffff);
|
PUT_LE32(outdata+8,(w2+GET_LE32(r_data1+8)) & 0xffffffff);
|
PUT_LE32(outdata+12,(w3+GET_LE32(r_data1+12)) & 0xffffffff);
|
PUT_LE32(outdata+12,(w3+GET_LE32(r_data1+12)) & 0xffffffff);
|
}
|
}
|
|
|
/* Make a signature for the current packet and add it at the end of the */
|
/* Make a signature for the current packet and add it at the end of the */
|
/* packet. */
|
/* packet. */
|
void sign_packet(struct ncp_server *server, int *size) {
|
void sign_packet(struct ncp_server *server, int *size) {
|
char data[64];
|
char data[64];
|
|
|
memset(data,0,64);
|
memset(data,0,64);
|
memcpy(data,server->sign_root,8);
|
memcpy(data,server->sign_root,8);
|
PUT_LE32(data+8,(*size));
|
PUT_LE32(data+8,(*size));
|
memcpy(data+12,server->packet+sizeof(struct ncp_request_header)-1,
|
memcpy(data+12,server->packet+sizeof(struct ncp_request_header)-1,
|
min((*size)-sizeof(struct ncp_request_header)+1,52));
|
min((*size)-sizeof(struct ncp_request_header)+1,52));
|
|
|
nwsign(server->sign_last,data,server->sign_last);
|
nwsign(server->sign_last,data,server->sign_last);
|
|
|
memcpy(server->packet+(*size),server->sign_last,8);
|
memcpy(server->packet+(*size),server->sign_last,8);
|
(*size)+=8;
|
(*size)+=8;
|
}
|
}
|
|
|
#endif /* CONFIG_NCPFS_PACKET_SIGNING */
|
#endif /* CONFIG_NCPFS_PACKET_SIGNING */
|
|
|
|
|