Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ncpsign_kernel.c |
| 3 | * |
| 4 | * Arne de Bruijn (arne@knoware.nl), 1997 |
| 5 | * |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 "ncpsign_kernel.h" |
| 15 | |
| 16 | /* i386: 32-bit, little endian, handles mis-alignment */ |
| 17 | #ifdef __i386__ |
| 18 | #define GET_LE32(p) (*(int *)(p)) |
| 19 | #define PUT_LE32(p,v) { *(int *)(p)=v; } |
| 20 | #else |
| 21 | /* from include/ncplib.h */ |
| 22 | #define BVAL(buf,pos) (((__u8 *)(buf))[pos]) |
| 23 | #define PVAL(buf,pos) ((unsigned)BVAL(buf,pos)) |
| 24 | #define BSET(buf,pos,val) (BVAL(buf,pos) = (val)) |
| 25 | |
| 26 | static inline __u16 |
| 27 | WVAL_LH(__u8 * buf, int pos) |
| 28 | { |
| 29 | return PVAL(buf, pos) | PVAL(buf, pos + 1) << 8; |
| 30 | } |
| 31 | static inline __u32 |
| 32 | DVAL_LH(__u8 * buf, int pos) |
| 33 | { |
| 34 | return WVAL_LH(buf, pos) | WVAL_LH(buf, pos + 2) << 16; |
| 35 | } |
| 36 | static inline void |
| 37 | WSET_LH(__u8 * buf, int pos, __u16 val) |
| 38 | { |
| 39 | BSET(buf, pos, val & 0xff); |
| 40 | BSET(buf, pos + 1, val >> 8); |
| 41 | } |
| 42 | static inline void |
| 43 | DSET_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 | |
| 53 | static 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 Harrison | eee3754f | 2008-04-28 02:14:01 -0700 | [diff] [blame] | 58 | unsigned int *data2=(unsigned int *)r_data2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #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. */ |
| 94 | void __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 | |
| 109 | int 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 | |