blob: a6ec90cd8894b7ef58372d2b5174808375ae3e25 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#include <linux/string.h>
13#include <linux/ncp.h>
14#include <linux/bitops.h>
15#include "ncpsign_kernel.h"
16
17/* i386: 32-bit, little endian, handles mis-alignment */
18#ifdef __i386__
19#define GET_LE32(p) (*(int *)(p))
20#define PUT_LE32(p,v) { *(int *)(p)=v; }
21#else
22/* from include/ncplib.h */
23#define BVAL(buf,pos) (((__u8 *)(buf))[pos])
24#define PVAL(buf,pos) ((unsigned)BVAL(buf,pos))
25#define BSET(buf,pos,val) (BVAL(buf,pos) = (val))
26
27static inline __u16
28WVAL_LH(__u8 * buf, int pos)
29{
30 return PVAL(buf, pos) | PVAL(buf, pos + 1) << 8;
31}
32static inline __u32
33DVAL_LH(__u8 * buf, int pos)
34{
35 return WVAL_LH(buf, pos) | WVAL_LH(buf, pos + 2) << 16;
36}
37static inline void
38WSET_LH(__u8 * buf, int pos, __u16 val)
39{
40 BSET(buf, pos, val & 0xff);
41 BSET(buf, pos + 1, val >> 8);
42}
43static inline void
44DSET_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
54static 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=(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. */
95void __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
110int 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