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