You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
6.5 KiB

4 years ago
  1. 'use strict';
  2. exports.readUInt32BE = function readUInt32BE(bytes, off) {
  3. var res = (bytes[0 + off] << 24) |
  4. (bytes[1 + off] << 16) |
  5. (bytes[2 + off] << 8) |
  6. bytes[3 + off];
  7. return res >>> 0;
  8. };
  9. exports.writeUInt32BE = function writeUInt32BE(bytes, value, off) {
  10. bytes[0 + off] = value >>> 24;
  11. bytes[1 + off] = (value >>> 16) & 0xff;
  12. bytes[2 + off] = (value >>> 8) & 0xff;
  13. bytes[3 + off] = value & 0xff;
  14. };
  15. exports.ip = function ip(inL, inR, out, off) {
  16. var outL = 0;
  17. var outR = 0;
  18. for (var i = 6; i >= 0; i -= 2) {
  19. for (var j = 0; j <= 24; j += 8) {
  20. outL <<= 1;
  21. outL |= (inR >>> (j + i)) & 1;
  22. }
  23. for (var j = 0; j <= 24; j += 8) {
  24. outL <<= 1;
  25. outL |= (inL >>> (j + i)) & 1;
  26. }
  27. }
  28. for (var i = 6; i >= 0; i -= 2) {
  29. for (var j = 1; j <= 25; j += 8) {
  30. outR <<= 1;
  31. outR |= (inR >>> (j + i)) & 1;
  32. }
  33. for (var j = 1; j <= 25; j += 8) {
  34. outR <<= 1;
  35. outR |= (inL >>> (j + i)) & 1;
  36. }
  37. }
  38. out[off + 0] = outL >>> 0;
  39. out[off + 1] = outR >>> 0;
  40. };
  41. exports.rip = function rip(inL, inR, out, off) {
  42. var outL = 0;
  43. var outR = 0;
  44. for (var i = 0; i < 4; i++) {
  45. for (var j = 24; j >= 0; j -= 8) {
  46. outL <<= 1;
  47. outL |= (inR >>> (j + i)) & 1;
  48. outL <<= 1;
  49. outL |= (inL >>> (j + i)) & 1;
  50. }
  51. }
  52. for (var i = 4; i < 8; i++) {
  53. for (var j = 24; j >= 0; j -= 8) {
  54. outR <<= 1;
  55. outR |= (inR >>> (j + i)) & 1;
  56. outR <<= 1;
  57. outR |= (inL >>> (j + i)) & 1;
  58. }
  59. }
  60. out[off + 0] = outL >>> 0;
  61. out[off + 1] = outR >>> 0;
  62. };
  63. exports.pc1 = function pc1(inL, inR, out, off) {
  64. var outL = 0;
  65. var outR = 0;
  66. // 7, 15, 23, 31, 39, 47, 55, 63
  67. // 6, 14, 22, 30, 39, 47, 55, 63
  68. // 5, 13, 21, 29, 39, 47, 55, 63
  69. // 4, 12, 20, 28
  70. for (var i = 7; i >= 5; i--) {
  71. for (var j = 0; j <= 24; j += 8) {
  72. outL <<= 1;
  73. outL |= (inR >> (j + i)) & 1;
  74. }
  75. for (var j = 0; j <= 24; j += 8) {
  76. outL <<= 1;
  77. outL |= (inL >> (j + i)) & 1;
  78. }
  79. }
  80. for (var j = 0; j <= 24; j += 8) {
  81. outL <<= 1;
  82. outL |= (inR >> (j + i)) & 1;
  83. }
  84. // 1, 9, 17, 25, 33, 41, 49, 57
  85. // 2, 10, 18, 26, 34, 42, 50, 58
  86. // 3, 11, 19, 27, 35, 43, 51, 59
  87. // 36, 44, 52, 60
  88. for (var i = 1; i <= 3; i++) {
  89. for (var j = 0; j <= 24; j += 8) {
  90. outR <<= 1;
  91. outR |= (inR >> (j + i)) & 1;
  92. }
  93. for (var j = 0; j <= 24; j += 8) {
  94. outR <<= 1;
  95. outR |= (inL >> (j + i)) & 1;
  96. }
  97. }
  98. for (var j = 0; j <= 24; j += 8) {
  99. outR <<= 1;
  100. outR |= (inL >> (j + i)) & 1;
  101. }
  102. out[off + 0] = outL >>> 0;
  103. out[off + 1] = outR >>> 0;
  104. };
  105. exports.r28shl = function r28shl(num, shift) {
  106. return ((num << shift) & 0xfffffff) | (num >>> (28 - shift));
  107. };
  108. var pc2table = [
  109. // inL => outL
  110. 14, 11, 17, 4, 27, 23, 25, 0,
  111. 13, 22, 7, 18, 5, 9, 16, 24,
  112. 2, 20, 12, 21, 1, 8, 15, 26,
  113. // inR => outR
  114. 15, 4, 25, 19, 9, 1, 26, 16,
  115. 5, 11, 23, 8, 12, 7, 17, 0,
  116. 22, 3, 10, 14, 6, 20, 27, 24
  117. ];
  118. exports.pc2 = function pc2(inL, inR, out, off) {
  119. var outL = 0;
  120. var outR = 0;
  121. var len = pc2table.length >>> 1;
  122. for (var i = 0; i < len; i++) {
  123. outL <<= 1;
  124. outL |= (inL >>> pc2table[i]) & 0x1;
  125. }
  126. for (var i = len; i < pc2table.length; i++) {
  127. outR <<= 1;
  128. outR |= (inR >>> pc2table[i]) & 0x1;
  129. }
  130. out[off + 0] = outL >>> 0;
  131. out[off + 1] = outR >>> 0;
  132. };
  133. exports.expand = function expand(r, out, off) {
  134. var outL = 0;
  135. var outR = 0;
  136. outL = ((r & 1) << 5) | (r >>> 27);
  137. for (var i = 23; i >= 15; i -= 4) {
  138. outL <<= 6;
  139. outL |= (r >>> i) & 0x3f;
  140. }
  141. for (var i = 11; i >= 3; i -= 4) {
  142. outR |= (r >>> i) & 0x3f;
  143. outR <<= 6;
  144. }
  145. outR |= ((r & 0x1f) << 1) | (r >>> 31);
  146. out[off + 0] = outL >>> 0;
  147. out[off + 1] = outR >>> 0;
  148. };
  149. var sTable = [
  150. 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
  151. 3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
  152. 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
  153. 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13,
  154. 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
  155. 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
  156. 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
  157. 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9,
  158. 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
  159. 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
  160. 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
  161. 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12,
  162. 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
  163. 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
  164. 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
  165. 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14,
  166. 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
  167. 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
  168. 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
  169. 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3,
  170. 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
  171. 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
  172. 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
  173. 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13,
  174. 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
  175. 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
  176. 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
  177. 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12,
  178. 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
  179. 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
  180. 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
  181. 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
  182. ];
  183. exports.substitute = function substitute(inL, inR) {
  184. var out = 0;
  185. for (var i = 0; i < 4; i++) {
  186. var b = (inL >>> (18 - i * 6)) & 0x3f;
  187. var sb = sTable[i * 0x40 + b];
  188. out <<= 4;
  189. out |= sb;
  190. }
  191. for (var i = 0; i < 4; i++) {
  192. var b = (inR >>> (18 - i * 6)) & 0x3f;
  193. var sb = sTable[4 * 0x40 + i * 0x40 + b];
  194. out <<= 4;
  195. out |= sb;
  196. }
  197. return out >>> 0;
  198. };
  199. var permuteTable = [
  200. 16, 25, 12, 11, 3, 20, 4, 15, 31, 17, 9, 6, 27, 14, 1, 22,
  201. 30, 24, 8, 18, 0, 5, 29, 23, 13, 19, 2, 26, 10, 21, 28, 7
  202. ];
  203. exports.permute = function permute(num) {
  204. var out = 0;
  205. for (var i = 0; i < permuteTable.length; i++) {
  206. out <<= 1;
  207. out |= (num >>> permuteTable[i]) & 0x1;
  208. }
  209. return out >>> 0;
  210. };
  211. exports.padSplit = function padSplit(num, size, group) {
  212. var str = num.toString(2);
  213. while (str.length < size)
  214. str = '0' + str;
  215. var out = [];
  216. for (var i = 0; i < size; i += group)
  217. out.push(str.slice(i, i + group));
  218. return out.join(' ');
  219. };