| 1 |
1628 |
jcastillo |
/*
|
| 2 |
|
|
* ncpsign_kernel.c
|
| 3 |
|
|
*
|
| 4 |
|
|
* Arne de Bruijn (arne@knoware.nl), 1997
|
| 5 |
|
|
*
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
|
|
#include <linux/config.h>
|
| 9 |
|
|
|
| 10 |
|
|
#ifdef CONFIG_NCPFS_PACKET_SIGNING
|
| 11 |
|
|
|
| 12 |
|
|
#if 0
|
| 13 |
|
|
#ifdef MODULE
|
| 14 |
|
|
#include <linux/module.h>
|
| 15 |
|
|
#include <linux/version.h>
|
| 16 |
|
|
#endif
|
| 17 |
|
|
#endif
|
| 18 |
|
|
|
| 19 |
|
|
#include <linux/string.h>
|
| 20 |
|
|
#include <linux/ncp.h>
|
| 21 |
|
|
#if 0
|
| 22 |
|
|
#include <linux/ncp_fs.h>
|
| 23 |
|
|
#include <linux/ncp_fs_sb.h>
|
| 24 |
|
|
#endif
|
| 25 |
|
|
#include "ncpsign_kernel.h"
|
| 26 |
|
|
|
| 27 |
|
|
#define rol32(i,c) (((((i)&0xffffffff)<<c)&0xffffffff)| \
|
| 28 |
|
|
(((i)&0xffffffff)>>(32-c)))
|
| 29 |
|
|
/* i386: 32-bit, little endian, handles mis-alignment */
|
| 30 |
|
|
#ifdef __i386__
|
| 31 |
|
|
#define GET_LE32(p) (*(int *)(p))
|
| 32 |
|
|
#define PUT_LE32(p,v) { *(int *)(p)=v; }
|
| 33 |
|
|
#else
|
| 34 |
|
|
/* from include/ncplib.h */
|
| 35 |
|
|
#define BVAL(buf,pos) (((__u8 *)(buf))[pos])
|
| 36 |
|
|
#define PVAL(buf,pos) ((unsigned)BVAL(buf,pos))
|
| 37 |
|
|
#define BSET(buf,pos,val) (BVAL(buf,pos) = (val))
|
| 38 |
|
|
|
| 39 |
|
|
static inline word
|
| 40 |
|
|
WVAL_LH(__u8 * buf, int pos)
|
| 41 |
|
|
{
|
| 42 |
|
|
return PVAL(buf, pos) | PVAL(buf, pos + 1) << 8;
|
| 43 |
|
|
}
|
| 44 |
|
|
static inline dword
|
| 45 |
|
|
DVAL_LH(__u8 * buf, int pos)
|
| 46 |
|
|
{
|
| 47 |
|
|
return WVAL_LH(buf, pos) | WVAL_LH(buf, pos + 2) << 16;
|
| 48 |
|
|
}
|
| 49 |
|
|
static inline void
|
| 50 |
|
|
WSET_LH(__u8 * buf, int pos, word val)
|
| 51 |
|
|
{
|
| 52 |
|
|
BSET(buf, pos, val & 0xff);
|
| 53 |
|
|
BSET(buf, pos + 1, val >> 8);
|
| 54 |
|
|
}
|
| 55 |
|
|
static inline void
|
| 56 |
|
|
DSET_LH(__u8 * buf, int pos, dword val)
|
| 57 |
|
|
{
|
| 58 |
|
|
WSET_LH(buf, pos, val & 0xffff);
|
| 59 |
|
|
WSET_LH(buf, pos + 2, val >> 16);
|
| 60 |
|
|
}
|
| 61 |
|
|
|
| 62 |
|
|
#define GET_LE32(p) DVAL_LH(p,0)
|
| 63 |
|
|
#define PUT_LE32(p,v) DSET_LH(p,0,v)
|
| 64 |
|
|
#endif
|
| 65 |
|
|
|
| 66 |
|
|
#define min(a,b) ((a)<(b)?(a):(b))
|
| 67 |
|
|
|
| 68 |
|
|
static void nwsign(char *r_data1, char *r_data2, char *outdata) {
|
| 69 |
|
|
int i;
|
| 70 |
|
|
unsigned int w0,w1,w2,w3;
|
| 71 |
|
|
static int rbit[4]={0, 2, 1, 3};
|
| 72 |
|
|
#ifdef __i386__
|
| 73 |
|
|
unsigned int *data2=(int *)r_data2;
|
| 74 |
|
|
#else
|
| 75 |
|
|
unsigned int data2[16];
|
| 76 |
|
|
for (i=0;i<16;i++)
|
| 77 |
|
|
data2[i]=GET_LE32(r_data2+(i<<2));
|
| 78 |
|
|
#endif
|
| 79 |
|
|
w0=GET_LE32(r_data1);
|
| 80 |
|
|
w1=GET_LE32(r_data1+4);
|
| 81 |
|
|
w2=GET_LE32(r_data1+8);
|
| 82 |
|
|
w3=GET_LE32(r_data1+12);
|
| 83 |
|
|
for (i=0;i<16;i+=4) {
|
| 84 |
|
|
w0=rol32(w0 + ((w1 & w2) | ((~w1) & w3)) + data2[i+0],3);
|
| 85 |
|
|
w3=rol32(w3 + ((w0 & w1) | ((~w0) & w2)) + data2[i+1],7);
|
| 86 |
|
|
w2=rol32(w2 + ((w3 & w0) | ((~w3) & w1)) + data2[i+2],11);
|
| 87 |
|
|
w1=rol32(w1 + ((w2 & w3) | ((~w2) & w0)) + data2[i+3],19);
|
| 88 |
|
|
}
|
| 89 |
|
|
for (i=0;i<4;i++) {
|
| 90 |
|
|
w0=rol32(w0 + (((w2 | w3) & w1) | (w2 & w3)) + 0x5a827999 + data2[i+0],3);
|
| 91 |
|
|
w3=rol32(w3 + (((w1 | w2) & w0) | (w1 & w2)) + 0x5a827999 + data2[i+4],5);
|
| 92 |
|
|
w2=rol32(w2 + (((w0 | w1) & w3) | (w0 & w1)) + 0x5a827999 + data2[i+8],9);
|
| 93 |
|
|
w1=rol32(w1 + (((w3 | w0) & w2) | (w3 & w0)) + 0x5a827999 + data2[i+12],13);
|
| 94 |
|
|
}
|
| 95 |
|
|
for (i=0;i<4;i++) {
|
| 96 |
|
|
w0=rol32(w0 + ((w1 ^ w2) ^ w3) + 0x6ed9eba1 + data2[rbit[i]+0],3);
|
| 97 |
|
|
w3=rol32(w3 + ((w0 ^ w1) ^ w2) + 0x6ed9eba1 + data2[rbit[i]+8],9);
|
| 98 |
|
|
w2=rol32(w2 + ((w3 ^ w0) ^ w1) + 0x6ed9eba1 + data2[rbit[i]+4],11);
|
| 99 |
|
|
w1=rol32(w1 + ((w2 ^ w3) ^ w0) + 0x6ed9eba1 + data2[rbit[i]+12],15);
|
| 100 |
|
|
}
|
| 101 |
|
|
PUT_LE32(outdata,(w0+GET_LE32(r_data1)) & 0xffffffff);
|
| 102 |
|
|
PUT_LE32(outdata+4,(w1+GET_LE32(r_data1+4)) & 0xffffffff);
|
| 103 |
|
|
PUT_LE32(outdata+8,(w2+GET_LE32(r_data1+8)) & 0xffffffff);
|
| 104 |
|
|
PUT_LE32(outdata+12,(w3+GET_LE32(r_data1+12)) & 0xffffffff);
|
| 105 |
|
|
}
|
| 106 |
|
|
|
| 107 |
|
|
/* Make a signature for the current packet and add it at the end of the */
|
| 108 |
|
|
/* packet. */
|
| 109 |
|
|
void sign_packet(struct ncp_server *server, int *size) {
|
| 110 |
|
|
char data[64];
|
| 111 |
|
|
|
| 112 |
|
|
memset(data,0,64);
|
| 113 |
|
|
memcpy(data,server->sign_root,8);
|
| 114 |
|
|
PUT_LE32(data+8,(*size));
|
| 115 |
|
|
memcpy(data+12,server->packet+sizeof(struct ncp_request_header)-1,
|
| 116 |
|
|
min((*size)-sizeof(struct ncp_request_header)+1,52));
|
| 117 |
|
|
|
| 118 |
|
|
nwsign(server->sign_last,data,server->sign_last);
|
| 119 |
|
|
|
| 120 |
|
|
memcpy(server->packet+(*size),server->sign_last,8);
|
| 121 |
|
|
(*size)+=8;
|
| 122 |
|
|
}
|
| 123 |
|
|
|
| 124 |
|
|
#endif /* CONFIG_NCPFS_PACKET_SIGNING */
|
| 125 |
|
|
|