Counter Strike : Global Offensive Source Code
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.

434 lines
8.3 KiB

  1. // private header for Serpent and Sosemanuk
  2. NAMESPACE_BEGIN(CryptoPP)
  3. // linear transformation
  4. #define LT(i,a,b,c,d,e) {\
  5. a = rotlFixed(a, 13); \
  6. c = rotlFixed(c, 3); \
  7. d = rotlFixed(d ^ c ^ (a << 3), 7); \
  8. b = rotlFixed(b ^ a ^ c, 1); \
  9. a = rotlFixed(a ^ b ^ d, 5); \
  10. c = rotlFixed(c ^ d ^ (b << 7), 22);}
  11. // inverse linear transformation
  12. #define ILT(i,a,b,c,d,e) {\
  13. c = rotrFixed(c, 22); \
  14. a = rotrFixed(a, 5); \
  15. c ^= d ^ (b << 7); \
  16. a ^= b ^ d; \
  17. b = rotrFixed(b, 1); \
  18. d = rotrFixed(d, 7) ^ c ^ (a << 3); \
  19. b ^= a ^ c; \
  20. c = rotrFixed(c, 3); \
  21. a = rotrFixed(a, 13);}
  22. // order of output from S-box functions
  23. #define beforeS0(f) f(0,a,b,c,d,e)
  24. #define afterS0(f) f(1,b,e,c,a,d)
  25. #define afterS1(f) f(2,c,b,a,e,d)
  26. #define afterS2(f) f(3,a,e,b,d,c)
  27. #define afterS3(f) f(4,e,b,d,c,a)
  28. #define afterS4(f) f(5,b,a,e,c,d)
  29. #define afterS5(f) f(6,a,c,b,e,d)
  30. #define afterS6(f) f(7,a,c,d,b,e)
  31. #define afterS7(f) f(8,d,e,b,a,c)
  32. // order of output from inverse S-box functions
  33. #define beforeI7(f) f(8,a,b,c,d,e)
  34. #define afterI7(f) f(7,d,a,b,e,c)
  35. #define afterI6(f) f(6,a,b,c,e,d)
  36. #define afterI5(f) f(5,b,d,e,c,a)
  37. #define afterI4(f) f(4,b,c,e,a,d)
  38. #define afterI3(f) f(3,a,b,e,c,d)
  39. #define afterI2(f) f(2,b,d,e,c,a)
  40. #define afterI1(f) f(1,a,b,c,e,d)
  41. #define afterI0(f) f(0,a,d,b,e,c)
  42. // The instruction sequences for the S-box functions
  43. // come from Dag Arne Osvik's paper "Speeding up Serpent".
  44. #define S0(i, r0, r1, r2, r3, r4) \
  45. { \
  46. r3 ^= r0; \
  47. r4 = r1; \
  48. r1 &= r3; \
  49. r4 ^= r2; \
  50. r1 ^= r0; \
  51. r0 |= r3; \
  52. r0 ^= r4; \
  53. r4 ^= r3; \
  54. r3 ^= r2; \
  55. r2 |= r1; \
  56. r2 ^= r4; \
  57. r4 = ~r4; \
  58. r4 |= r1; \
  59. r1 ^= r3; \
  60. r1 ^= r4; \
  61. r3 |= r0; \
  62. r1 ^= r3; \
  63. r4 ^= r3; \
  64. }
  65. #define I0(i, r0, r1, r2, r3, r4) \
  66. { \
  67. r2 = ~r2; \
  68. r4 = r1; \
  69. r1 |= r0; \
  70. r4 = ~r4; \
  71. r1 ^= r2; \
  72. r2 |= r4; \
  73. r1 ^= r3; \
  74. r0 ^= r4; \
  75. r2 ^= r0; \
  76. r0 &= r3; \
  77. r4 ^= r0; \
  78. r0 |= r1; \
  79. r0 ^= r2; \
  80. r3 ^= r4; \
  81. r2 ^= r1; \
  82. r3 ^= r0; \
  83. r3 ^= r1; \
  84. r2 &= r3; \
  85. r4 ^= r2; \
  86. }
  87. #define S1(i, r0, r1, r2, r3, r4) \
  88. { \
  89. r0 = ~r0; \
  90. r2 = ~r2; \
  91. r4 = r0; \
  92. r0 &= r1; \
  93. r2 ^= r0; \
  94. r0 |= r3; \
  95. r3 ^= r2; \
  96. r1 ^= r0; \
  97. r0 ^= r4; \
  98. r4 |= r1; \
  99. r1 ^= r3; \
  100. r2 |= r0; \
  101. r2 &= r4; \
  102. r0 ^= r1; \
  103. r1 &= r2; \
  104. r1 ^= r0; \
  105. r0 &= r2; \
  106. r0 ^= r4; \
  107. }
  108. #define I1(i, r0, r1, r2, r3, r4) \
  109. { \
  110. r4 = r1; \
  111. r1 ^= r3; \
  112. r3 &= r1; \
  113. r4 ^= r2; \
  114. r3 ^= r0; \
  115. r0 |= r1; \
  116. r2 ^= r3; \
  117. r0 ^= r4; \
  118. r0 |= r2; \
  119. r1 ^= r3; \
  120. r0 ^= r1; \
  121. r1 |= r3; \
  122. r1 ^= r0; \
  123. r4 = ~r4; \
  124. r4 ^= r1; \
  125. r1 |= r0; \
  126. r1 ^= r0; \
  127. r1 |= r4; \
  128. r3 ^= r1; \
  129. }
  130. #define S2(i, r0, r1, r2, r3, r4) \
  131. { \
  132. r4 = r0; \
  133. r0 &= r2; \
  134. r0 ^= r3; \
  135. r2 ^= r1; \
  136. r2 ^= r0; \
  137. r3 |= r4; \
  138. r3 ^= r1; \
  139. r4 ^= r2; \
  140. r1 = r3; \
  141. r3 |= r4; \
  142. r3 ^= r0; \
  143. r0 &= r1; \
  144. r4 ^= r0; \
  145. r1 ^= r3; \
  146. r1 ^= r4; \
  147. r4 = ~r4; \
  148. }
  149. #define I2(i, r0, r1, r2, r3, r4) \
  150. { \
  151. r2 ^= r3; \
  152. r3 ^= r0; \
  153. r4 = r3; \
  154. r3 &= r2; \
  155. r3 ^= r1; \
  156. r1 |= r2; \
  157. r1 ^= r4; \
  158. r4 &= r3; \
  159. r2 ^= r3; \
  160. r4 &= r0; \
  161. r4 ^= r2; \
  162. r2 &= r1; \
  163. r2 |= r0; \
  164. r3 = ~r3; \
  165. r2 ^= r3; \
  166. r0 ^= r3; \
  167. r0 &= r1; \
  168. r3 ^= r4; \
  169. r3 ^= r0; \
  170. }
  171. #define S3(i, r0, r1, r2, r3, r4) \
  172. { \
  173. r4 = r0; \
  174. r0 |= r3; \
  175. r3 ^= r1; \
  176. r1 &= r4; \
  177. r4 ^= r2; \
  178. r2 ^= r3; \
  179. r3 &= r0; \
  180. r4 |= r1; \
  181. r3 ^= r4; \
  182. r0 ^= r1; \
  183. r4 &= r0; \
  184. r1 ^= r3; \
  185. r4 ^= r2; \
  186. r1 |= r0; \
  187. r1 ^= r2; \
  188. r0 ^= r3; \
  189. r2 = r1; \
  190. r1 |= r3; \
  191. r1 ^= r0; \
  192. }
  193. #define I3(i, r0, r1, r2, r3, r4) \
  194. { \
  195. r4 = r2; \
  196. r2 ^= r1; \
  197. r1 &= r2; \
  198. r1 ^= r0; \
  199. r0 &= r4; \
  200. r4 ^= r3; \
  201. r3 |= r1; \
  202. r3 ^= r2; \
  203. r0 ^= r4; \
  204. r2 ^= r0; \
  205. r0 |= r3; \
  206. r0 ^= r1; \
  207. r4 ^= r2; \
  208. r2 &= r3; \
  209. r1 |= r3; \
  210. r1 ^= r2; \
  211. r4 ^= r0; \
  212. r2 ^= r4; \
  213. }
  214. #define S4(i, r0, r1, r2, r3, r4) \
  215. { \
  216. r1 ^= r3; \
  217. r3 = ~r3; \
  218. r2 ^= r3; \
  219. r3 ^= r0; \
  220. r4 = r1; \
  221. r1 &= r3; \
  222. r1 ^= r2; \
  223. r4 ^= r3; \
  224. r0 ^= r4; \
  225. r2 &= r4; \
  226. r2 ^= r0; \
  227. r0 &= r1; \
  228. r3 ^= r0; \
  229. r4 |= r1; \
  230. r4 ^= r0; \
  231. r0 |= r3; \
  232. r0 ^= r2; \
  233. r2 &= r3; \
  234. r0 = ~r0; \
  235. r4 ^= r2; \
  236. }
  237. #define I4(i, r0, r1, r2, r3, r4) \
  238. { \
  239. r4 = r2; \
  240. r2 &= r3; \
  241. r2 ^= r1; \
  242. r1 |= r3; \
  243. r1 &= r0; \
  244. r4 ^= r2; \
  245. r4 ^= r1; \
  246. r1 &= r2; \
  247. r0 = ~r0; \
  248. r3 ^= r4; \
  249. r1 ^= r3; \
  250. r3 &= r0; \
  251. r3 ^= r2; \
  252. r0 ^= r1; \
  253. r2 &= r0; \
  254. r3 ^= r0; \
  255. r2 ^= r4; \
  256. r2 |= r3; \
  257. r3 ^= r0; \
  258. r2 ^= r1; \
  259. }
  260. #define S5(i, r0, r1, r2, r3, r4) \
  261. { \
  262. r0 ^= r1; \
  263. r1 ^= r3; \
  264. r3 = ~r3; \
  265. r4 = r1; \
  266. r1 &= r0; \
  267. r2 ^= r3; \
  268. r1 ^= r2; \
  269. r2 |= r4; \
  270. r4 ^= r3; \
  271. r3 &= r1; \
  272. r3 ^= r0; \
  273. r4 ^= r1; \
  274. r4 ^= r2; \
  275. r2 ^= r0; \
  276. r0 &= r3; \
  277. r2 = ~r2; \
  278. r0 ^= r4; \
  279. r4 |= r3; \
  280. r2 ^= r4; \
  281. }
  282. #define I5(i, r0, r1, r2, r3, r4) \
  283. { \
  284. r1 = ~r1; \
  285. r4 = r3; \
  286. r2 ^= r1; \
  287. r3 |= r0; \
  288. r3 ^= r2; \
  289. r2 |= r1; \
  290. r2 &= r0; \
  291. r4 ^= r3; \
  292. r2 ^= r4; \
  293. r4 |= r0; \
  294. r4 ^= r1; \
  295. r1 &= r2; \
  296. r1 ^= r3; \
  297. r4 ^= r2; \
  298. r3 &= r4; \
  299. r4 ^= r1; \
  300. r3 ^= r0; \
  301. r3 ^= r4; \
  302. r4 = ~r4; \
  303. }
  304. #define S6(i, r0, r1, r2, r3, r4) \
  305. { \
  306. r2 = ~r2; \
  307. r4 = r3; \
  308. r3 &= r0; \
  309. r0 ^= r4; \
  310. r3 ^= r2; \
  311. r2 |= r4; \
  312. r1 ^= r3; \
  313. r2 ^= r0; \
  314. r0 |= r1; \
  315. r2 ^= r1; \
  316. r4 ^= r0; \
  317. r0 |= r3; \
  318. r0 ^= r2; \
  319. r4 ^= r3; \
  320. r4 ^= r0; \
  321. r3 = ~r3; \
  322. r2 &= r4; \
  323. r2 ^= r3; \
  324. }
  325. #define I6(i, r0, r1, r2, r3, r4) \
  326. { \
  327. r0 ^= r2; \
  328. r4 = r2; \
  329. r2 &= r0; \
  330. r4 ^= r3; \
  331. r2 = ~r2; \
  332. r3 ^= r1; \
  333. r2 ^= r3; \
  334. r4 |= r0; \
  335. r0 ^= r2; \
  336. r3 ^= r4; \
  337. r4 ^= r1; \
  338. r1 &= r3; \
  339. r1 ^= r0; \
  340. r0 ^= r3; \
  341. r0 |= r2; \
  342. r3 ^= r1; \
  343. r4 ^= r0; \
  344. }
  345. #define S7(i, r0, r1, r2, r3, r4) \
  346. { \
  347. r4 = r2; \
  348. r2 &= r1; \
  349. r2 ^= r3; \
  350. r3 &= r1; \
  351. r4 ^= r2; \
  352. r2 ^= r1; \
  353. r1 ^= r0; \
  354. r0 |= r4; \
  355. r0 ^= r2; \
  356. r3 ^= r1; \
  357. r2 ^= r3; \
  358. r3 &= r0; \
  359. r3 ^= r4; \
  360. r4 ^= r2; \
  361. r2 &= r0; \
  362. r4 = ~r4; \
  363. r2 ^= r4; \
  364. r4 &= r0; \
  365. r1 ^= r3; \
  366. r4 ^= r1; \
  367. }
  368. #define I7(i, r0, r1, r2, r3, r4) \
  369. { \
  370. r4 = r2; \
  371. r2 ^= r0; \
  372. r0 &= r3; \
  373. r2 = ~r2; \
  374. r4 |= r3; \
  375. r3 ^= r1; \
  376. r1 |= r0; \
  377. r0 ^= r2; \
  378. r2 &= r4; \
  379. r1 ^= r2; \
  380. r2 ^= r0; \
  381. r0 |= r2; \
  382. r3 &= r4; \
  383. r0 ^= r3; \
  384. r4 ^= r1; \
  385. r3 ^= r4; \
  386. r4 |= r0; \
  387. r3 ^= r2; \
  388. r4 ^= r2; \
  389. }
  390. // key xor
  391. #define KX(r, a, b, c, d, e) {\
  392. a ^= k[4 * r + 0]; \
  393. b ^= k[4 * r + 1]; \
  394. c ^= k[4 * r + 2]; \
  395. d ^= k[4 * r + 3];}
  396. #define LK(r, a, b, c, d, e) {\
  397. a = k[(8-r)*4 + 0]; \
  398. b = k[(8-r)*4 + 1]; \
  399. c = k[(8-r)*4 + 2]; \
  400. d = k[(8-r)*4 + 3];}
  401. #define SK(r, a, b, c, d, e) {\
  402. k[(8-r)*4 + 4] = a; \
  403. k[(8-r)*4 + 5] = b; \
  404. k[(8-r)*4 + 6] = c; \
  405. k[(8-r)*4 + 7] = d;}
  406. void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen);
  407. NAMESPACE_END