ncpsign_kernel.c 3.6 KB

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