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