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