/lib/bearssl-esp8266/src/kdf/shake.c

https://github.com/arendst/Sonoff-Tasmota · C · 590 lines · 516 code · 27 blank · 47 comment · 13 complexity · 04a51ddb2173bb0a0332d1130c784bb1 MD5 · raw file

  1. /*
  2. * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "t_inner.h"
  25. /*
  26. * Round constants.
  27. */
  28. static const uint64_t RC[] PROGMEM = {
  29. 0x0000000000000001, 0x0000000000008082,
  30. 0x800000000000808A, 0x8000000080008000,
  31. 0x000000000000808B, 0x0000000080000001,
  32. 0x8000000080008081, 0x8000000000008009,
  33. 0x000000000000008A, 0x0000000000000088,
  34. 0x0000000080008009, 0x000000008000000A,
  35. 0x000000008000808B, 0x800000000000008B,
  36. 0x8000000000008089, 0x8000000000008003,
  37. 0x8000000000008002, 0x8000000000000080,
  38. 0x000000000000800A, 0x800000008000000A,
  39. 0x8000000080008081, 0x8000000000008080,
  40. 0x0000000080000001, 0x8000000080008008
  41. };
  42. /*
  43. * XOR a block of data into the provided state. This supports only
  44. * blocks whose length is a multiple of 64 bits.
  45. */
  46. static void
  47. xor_block(uint64_t *A, const void *data, size_t rate)
  48. {
  49. size_t u;
  50. for (u = 0; u < rate; u += 8) {
  51. A[u >> 3] ^= br_dec64le((const unsigned char *)data + u);
  52. }
  53. }
  54. /*
  55. * Process a block with the provided data. The data length must be a
  56. * multiple of 8 (in bytes); normally, this is the "rate".
  57. */
  58. static void
  59. process_block(uint64_t *A)
  60. {
  61. uint64_t t0, t1, t2, t3, t4;
  62. uint64_t tt0, tt1, tt2, tt3;
  63. uint64_t t, kt;
  64. uint64_t c0, c1, c2, c3, c4, bnn;
  65. int j;
  66. /*
  67. * Compute the 24 rounds. This loop is partially unrolled (each
  68. * iteration computes two rounds).
  69. */
  70. for (j = 0; j < 24; j += 2) {
  71. tt0 = A[ 1] ^ A[ 6];
  72. tt1 = A[11] ^ A[16];
  73. tt0 ^= A[21] ^ tt1;
  74. tt0 = (tt0 << 1) | (tt0 >> 63);
  75. tt2 = A[ 4] ^ A[ 9];
  76. tt3 = A[14] ^ A[19];
  77. tt0 ^= A[24];
  78. tt2 ^= tt3;
  79. t0 = tt0 ^ tt2;
  80. tt0 = A[ 2] ^ A[ 7];
  81. tt1 = A[12] ^ A[17];
  82. tt0 ^= A[22] ^ tt1;
  83. tt0 = (tt0 << 1) | (tt0 >> 63);
  84. tt2 = A[ 0] ^ A[ 5];
  85. tt3 = A[10] ^ A[15];
  86. tt0 ^= A[20];
  87. tt2 ^= tt3;
  88. t1 = tt0 ^ tt2;
  89. tt0 = A[ 3] ^ A[ 8];
  90. tt1 = A[13] ^ A[18];
  91. tt0 ^= A[23] ^ tt1;
  92. tt0 = (tt0 << 1) | (tt0 >> 63);
  93. tt2 = A[ 1] ^ A[ 6];
  94. tt3 = A[11] ^ A[16];
  95. tt0 ^= A[21];
  96. tt2 ^= tt3;
  97. t2 = tt0 ^ tt2;
  98. tt0 = A[ 4] ^ A[ 9];
  99. tt1 = A[14] ^ A[19];
  100. tt0 ^= A[24] ^ tt1;
  101. tt0 = (tt0 << 1) | (tt0 >> 63);
  102. tt2 = A[ 2] ^ A[ 7];
  103. tt3 = A[12] ^ A[17];
  104. tt0 ^= A[22];
  105. tt2 ^= tt3;
  106. t3 = tt0 ^ tt2;
  107. tt0 = A[ 0] ^ A[ 5];
  108. tt1 = A[10] ^ A[15];
  109. tt0 ^= A[20] ^ tt1;
  110. tt0 = (tt0 << 1) | (tt0 >> 63);
  111. tt2 = A[ 3] ^ A[ 8];
  112. tt3 = A[13] ^ A[18];
  113. tt0 ^= A[23];
  114. tt2 ^= tt3;
  115. t4 = tt0 ^ tt2;
  116. A[ 0] = A[ 0] ^ t0;
  117. A[ 5] = A[ 5] ^ t0;
  118. A[10] = A[10] ^ t0;
  119. A[15] = A[15] ^ t0;
  120. A[20] = A[20] ^ t0;
  121. A[ 1] = A[ 1] ^ t1;
  122. A[ 6] = A[ 6] ^ t1;
  123. A[11] = A[11] ^ t1;
  124. A[16] = A[16] ^ t1;
  125. A[21] = A[21] ^ t1;
  126. A[ 2] = A[ 2] ^ t2;
  127. A[ 7] = A[ 7] ^ t2;
  128. A[12] = A[12] ^ t2;
  129. A[17] = A[17] ^ t2;
  130. A[22] = A[22] ^ t2;
  131. A[ 3] = A[ 3] ^ t3;
  132. A[ 8] = A[ 8] ^ t3;
  133. A[13] = A[13] ^ t3;
  134. A[18] = A[18] ^ t3;
  135. A[23] = A[23] ^ t3;
  136. A[ 4] = A[ 4] ^ t4;
  137. A[ 9] = A[ 9] ^ t4;
  138. A[14] = A[14] ^ t4;
  139. A[19] = A[19] ^ t4;
  140. A[24] = A[24] ^ t4;
  141. A[ 5] = (A[ 5] << 36) | (A[ 5] >> (64 - 36));
  142. A[10] = (A[10] << 3) | (A[10] >> (64 - 3));
  143. A[15] = (A[15] << 41) | (A[15] >> (64 - 41));
  144. A[20] = (A[20] << 18) | (A[20] >> (64 - 18));
  145. A[ 1] = (A[ 1] << 1) | (A[ 1] >> (64 - 1));
  146. A[ 6] = (A[ 6] << 44) | (A[ 6] >> (64 - 44));
  147. A[11] = (A[11] << 10) | (A[11] >> (64 - 10));
  148. A[16] = (A[16] << 45) | (A[16] >> (64 - 45));
  149. A[21] = (A[21] << 2) | (A[21] >> (64 - 2));
  150. A[ 2] = (A[ 2] << 62) | (A[ 2] >> (64 - 62));
  151. A[ 7] = (A[ 7] << 6) | (A[ 7] >> (64 - 6));
  152. A[12] = (A[12] << 43) | (A[12] >> (64 - 43));
  153. A[17] = (A[17] << 15) | (A[17] >> (64 - 15));
  154. A[22] = (A[22] << 61) | (A[22] >> (64 - 61));
  155. A[ 3] = (A[ 3] << 28) | (A[ 3] >> (64 - 28));
  156. A[ 8] = (A[ 8] << 55) | (A[ 8] >> (64 - 55));
  157. A[13] = (A[13] << 25) | (A[13] >> (64 - 25));
  158. A[18] = (A[18] << 21) | (A[18] >> (64 - 21));
  159. A[23] = (A[23] << 56) | (A[23] >> (64 - 56));
  160. A[ 4] = (A[ 4] << 27) | (A[ 4] >> (64 - 27));
  161. A[ 9] = (A[ 9] << 20) | (A[ 9] >> (64 - 20));
  162. A[14] = (A[14] << 39) | (A[14] >> (64 - 39));
  163. A[19] = (A[19] << 8) | (A[19] >> (64 - 8));
  164. A[24] = (A[24] << 14) | (A[24] >> (64 - 14));
  165. bnn = ~A[12];
  166. kt = A[ 6] | A[12];
  167. c0 = A[ 0] ^ kt;
  168. kt = bnn | A[18];
  169. c1 = A[ 6] ^ kt;
  170. kt = A[18] & A[24];
  171. c2 = A[12] ^ kt;
  172. kt = A[24] | A[ 0];
  173. c3 = A[18] ^ kt;
  174. kt = A[ 0] & A[ 6];
  175. c4 = A[24] ^ kt;
  176. A[ 0] = c0;
  177. A[ 6] = c1;
  178. A[12] = c2;
  179. A[18] = c3;
  180. A[24] = c4;
  181. bnn = ~A[22];
  182. kt = A[ 9] | A[10];
  183. c0 = A[ 3] ^ kt;
  184. kt = A[10] & A[16];
  185. c1 = A[ 9] ^ kt;
  186. kt = A[16] | bnn;
  187. c2 = A[10] ^ kt;
  188. kt = A[22] | A[ 3];
  189. c3 = A[16] ^ kt;
  190. kt = A[ 3] & A[ 9];
  191. c4 = A[22] ^ kt;
  192. A[ 3] = c0;
  193. A[ 9] = c1;
  194. A[10] = c2;
  195. A[16] = c3;
  196. A[22] = c4;
  197. bnn = ~A[19];
  198. kt = A[ 7] | A[13];
  199. c0 = A[ 1] ^ kt;
  200. kt = A[13] & A[19];
  201. c1 = A[ 7] ^ kt;
  202. kt = bnn & A[20];
  203. c2 = A[13] ^ kt;
  204. kt = A[20] | A[ 1];
  205. c3 = bnn ^ kt;
  206. kt = A[ 1] & A[ 7];
  207. c4 = A[20] ^ kt;
  208. A[ 1] = c0;
  209. A[ 7] = c1;
  210. A[13] = c2;
  211. A[19] = c3;
  212. A[20] = c4;
  213. bnn = ~A[17];
  214. kt = A[ 5] & A[11];
  215. c0 = A[ 4] ^ kt;
  216. kt = A[11] | A[17];
  217. c1 = A[ 5] ^ kt;
  218. kt = bnn | A[23];
  219. c2 = A[11] ^ kt;
  220. kt = A[23] & A[ 4];
  221. c3 = bnn ^ kt;
  222. kt = A[ 4] | A[ 5];
  223. c4 = A[23] ^ kt;
  224. A[ 4] = c0;
  225. A[ 5] = c1;
  226. A[11] = c2;
  227. A[17] = c3;
  228. A[23] = c4;
  229. bnn = ~A[ 8];
  230. kt = bnn & A[14];
  231. c0 = A[ 2] ^ kt;
  232. kt = A[14] | A[15];
  233. c1 = bnn ^ kt;
  234. kt = A[15] & A[21];
  235. c2 = A[14] ^ kt;
  236. kt = A[21] | A[ 2];
  237. c3 = A[15] ^ kt;
  238. kt = A[ 2] & A[ 8];
  239. c4 = A[21] ^ kt;
  240. A[ 2] = c0;
  241. A[ 8] = c1;
  242. A[14] = c2;
  243. A[15] = c3;
  244. A[21] = c4;
  245. A[ 0] = A[ 0] ^ RC[j + 0];
  246. tt0 = A[ 6] ^ A[ 9];
  247. tt1 = A[ 7] ^ A[ 5];
  248. tt0 ^= A[ 8] ^ tt1;
  249. tt0 = (tt0 << 1) | (tt0 >> 63);
  250. tt2 = A[24] ^ A[22];
  251. tt3 = A[20] ^ A[23];
  252. tt0 ^= A[21];
  253. tt2 ^= tt3;
  254. t0 = tt0 ^ tt2;
  255. tt0 = A[12] ^ A[10];
  256. tt1 = A[13] ^ A[11];
  257. tt0 ^= A[14] ^ tt1;
  258. tt0 = (tt0 << 1) | (tt0 >> 63);
  259. tt2 = A[ 0] ^ A[ 3];
  260. tt3 = A[ 1] ^ A[ 4];
  261. tt0 ^= A[ 2];
  262. tt2 ^= tt3;
  263. t1 = tt0 ^ tt2;
  264. tt0 = A[18] ^ A[16];
  265. tt1 = A[19] ^ A[17];
  266. tt0 ^= A[15] ^ tt1;
  267. tt0 = (tt0 << 1) | (tt0 >> 63);
  268. tt2 = A[ 6] ^ A[ 9];
  269. tt3 = A[ 7] ^ A[ 5];
  270. tt0 ^= A[ 8];
  271. tt2 ^= tt3;
  272. t2 = tt0 ^ tt2;
  273. tt0 = A[24] ^ A[22];
  274. tt1 = A[20] ^ A[23];
  275. tt0 ^= A[21] ^ tt1;
  276. tt0 = (tt0 << 1) | (tt0 >> 63);
  277. tt2 = A[12] ^ A[10];
  278. tt3 = A[13] ^ A[11];
  279. tt0 ^= A[14];
  280. tt2 ^= tt3;
  281. t3 = tt0 ^ tt2;
  282. tt0 = A[ 0] ^ A[ 3];
  283. tt1 = A[ 1] ^ A[ 4];
  284. tt0 ^= A[ 2] ^ tt1;
  285. tt0 = (tt0 << 1) | (tt0 >> 63);
  286. tt2 = A[18] ^ A[16];
  287. tt3 = A[19] ^ A[17];
  288. tt0 ^= A[15];
  289. tt2 ^= tt3;
  290. t4 = tt0 ^ tt2;
  291. A[ 0] = A[ 0] ^ t0;
  292. A[ 3] = A[ 3] ^ t0;
  293. A[ 1] = A[ 1] ^ t0;
  294. A[ 4] = A[ 4] ^ t0;
  295. A[ 2] = A[ 2] ^ t0;
  296. A[ 6] = A[ 6] ^ t1;
  297. A[ 9] = A[ 9] ^ t1;
  298. A[ 7] = A[ 7] ^ t1;
  299. A[ 5] = A[ 5] ^ t1;
  300. A[ 8] = A[ 8] ^ t1;
  301. A[12] = A[12] ^ t2;
  302. A[10] = A[10] ^ t2;
  303. A[13] = A[13] ^ t2;
  304. A[11] = A[11] ^ t2;
  305. A[14] = A[14] ^ t2;
  306. A[18] = A[18] ^ t3;
  307. A[16] = A[16] ^ t3;
  308. A[19] = A[19] ^ t3;
  309. A[17] = A[17] ^ t3;
  310. A[15] = A[15] ^ t3;
  311. A[24] = A[24] ^ t4;
  312. A[22] = A[22] ^ t4;
  313. A[20] = A[20] ^ t4;
  314. A[23] = A[23] ^ t4;
  315. A[21] = A[21] ^ t4;
  316. A[ 3] = (A[ 3] << 36) | (A[ 3] >> (64 - 36));
  317. A[ 1] = (A[ 1] << 3) | (A[ 1] >> (64 - 3));
  318. A[ 4] = (A[ 4] << 41) | (A[ 4] >> (64 - 41));
  319. A[ 2] = (A[ 2] << 18) | (A[ 2] >> (64 - 18));
  320. A[ 6] = (A[ 6] << 1) | (A[ 6] >> (64 - 1));
  321. A[ 9] = (A[ 9] << 44) | (A[ 9] >> (64 - 44));
  322. A[ 7] = (A[ 7] << 10) | (A[ 7] >> (64 - 10));
  323. A[ 5] = (A[ 5] << 45) | (A[ 5] >> (64 - 45));
  324. A[ 8] = (A[ 8] << 2) | (A[ 8] >> (64 - 2));
  325. A[12] = (A[12] << 62) | (A[12] >> (64 - 62));
  326. A[10] = (A[10] << 6) | (A[10] >> (64 - 6));
  327. A[13] = (A[13] << 43) | (A[13] >> (64 - 43));
  328. A[11] = (A[11] << 15) | (A[11] >> (64 - 15));
  329. A[14] = (A[14] << 61) | (A[14] >> (64 - 61));
  330. A[18] = (A[18] << 28) | (A[18] >> (64 - 28));
  331. A[16] = (A[16] << 55) | (A[16] >> (64 - 55));
  332. A[19] = (A[19] << 25) | (A[19] >> (64 - 25));
  333. A[17] = (A[17] << 21) | (A[17] >> (64 - 21));
  334. A[15] = (A[15] << 56) | (A[15] >> (64 - 56));
  335. A[24] = (A[24] << 27) | (A[24] >> (64 - 27));
  336. A[22] = (A[22] << 20) | (A[22] >> (64 - 20));
  337. A[20] = (A[20] << 39) | (A[20] >> (64 - 39));
  338. A[23] = (A[23] << 8) | (A[23] >> (64 - 8));
  339. A[21] = (A[21] << 14) | (A[21] >> (64 - 14));
  340. bnn = ~A[13];
  341. kt = A[ 9] | A[13];
  342. c0 = A[ 0] ^ kt;
  343. kt = bnn | A[17];
  344. c1 = A[ 9] ^ kt;
  345. kt = A[17] & A[21];
  346. c2 = A[13] ^ kt;
  347. kt = A[21] | A[ 0];
  348. c3 = A[17] ^ kt;
  349. kt = A[ 0] & A[ 9];
  350. c4 = A[21] ^ kt;
  351. A[ 0] = c0;
  352. A[ 9] = c1;
  353. A[13] = c2;
  354. A[17] = c3;
  355. A[21] = c4;
  356. bnn = ~A[14];
  357. kt = A[22] | A[ 1];
  358. c0 = A[18] ^ kt;
  359. kt = A[ 1] & A[ 5];
  360. c1 = A[22] ^ kt;
  361. kt = A[ 5] | bnn;
  362. c2 = A[ 1] ^ kt;
  363. kt = A[14] | A[18];
  364. c3 = A[ 5] ^ kt;
  365. kt = A[18] & A[22];
  366. c4 = A[14] ^ kt;
  367. A[18] = c0;
  368. A[22] = c1;
  369. A[ 1] = c2;
  370. A[ 5] = c3;
  371. A[14] = c4;
  372. bnn = ~A[23];
  373. kt = A[10] | A[19];
  374. c0 = A[ 6] ^ kt;
  375. kt = A[19] & A[23];
  376. c1 = A[10] ^ kt;
  377. kt = bnn & A[ 2];
  378. c2 = A[19] ^ kt;
  379. kt = A[ 2] | A[ 6];
  380. c3 = bnn ^ kt;
  381. kt = A[ 6] & A[10];
  382. c4 = A[ 2] ^ kt;
  383. A[ 6] = c0;
  384. A[10] = c1;
  385. A[19] = c2;
  386. A[23] = c3;
  387. A[ 2] = c4;
  388. bnn = ~A[11];
  389. kt = A[ 3] & A[ 7];
  390. c0 = A[24] ^ kt;
  391. kt = A[ 7] | A[11];
  392. c1 = A[ 3] ^ kt;
  393. kt = bnn | A[15];
  394. c2 = A[ 7] ^ kt;
  395. kt = A[15] & A[24];
  396. c3 = bnn ^ kt;
  397. kt = A[24] | A[ 3];
  398. c4 = A[15] ^ kt;
  399. A[24] = c0;
  400. A[ 3] = c1;
  401. A[ 7] = c2;
  402. A[11] = c3;
  403. A[15] = c4;
  404. bnn = ~A[16];
  405. kt = bnn & A[20];
  406. c0 = A[12] ^ kt;
  407. kt = A[20] | A[ 4];
  408. c1 = bnn ^ kt;
  409. kt = A[ 4] & A[ 8];
  410. c2 = A[20] ^ kt;
  411. kt = A[ 8] | A[12];
  412. c3 = A[ 4] ^ kt;
  413. kt = A[12] & A[16];
  414. c4 = A[ 8] ^ kt;
  415. A[12] = c0;
  416. A[16] = c1;
  417. A[20] = c2;
  418. A[ 4] = c3;
  419. A[ 8] = c4;
  420. A[ 0] = A[ 0] ^ RC[j + 1];
  421. t = A[ 5];
  422. A[ 5] = A[18];
  423. A[18] = A[11];
  424. A[11] = A[10];
  425. A[10] = A[ 6];
  426. A[ 6] = A[22];
  427. A[22] = A[20];
  428. A[20] = A[12];
  429. A[12] = A[19];
  430. A[19] = A[15];
  431. A[15] = A[24];
  432. A[24] = A[ 8];
  433. A[ 8] = t;
  434. t = A[ 1];
  435. A[ 1] = A[ 9];
  436. A[ 9] = A[14];
  437. A[14] = A[ 2];
  438. A[ 2] = A[13];
  439. A[13] = A[23];
  440. A[23] = A[ 4];
  441. A[ 4] = A[21];
  442. A[21] = A[16];
  443. A[16] = A[ 3];
  444. A[ 3] = A[17];
  445. A[17] = A[ 7];
  446. A[ 7] = t;
  447. }
  448. }
  449. /* see bearssl_kdf.h */
  450. void
  451. br_shake_init(br_shake_context *sc, int security_level)
  452. {
  453. sc->rate = 200 - (size_t)(security_level >> 2);
  454. sc->dptr = 0;
  455. memset(sc->A, 0, sizeof sc->A);
  456. sc->A[ 1] = ~(uint64_t)0;
  457. sc->A[ 2] = ~(uint64_t)0;
  458. sc->A[ 8] = ~(uint64_t)0;
  459. sc->A[12] = ~(uint64_t)0;
  460. sc->A[17] = ~(uint64_t)0;
  461. sc->A[20] = ~(uint64_t)0;
  462. }
  463. /* see bearssl_kdf.h */
  464. void
  465. br_shake_inject(br_shake_context *sc, const void *data, size_t len)
  466. {
  467. const unsigned char *buf;
  468. size_t rate, dptr;
  469. buf = data;
  470. rate = sc->rate;
  471. dptr = sc->dptr;
  472. while (len > 0) {
  473. size_t clen;
  474. clen = rate - dptr;
  475. if (clen > len) {
  476. clen = len;
  477. }
  478. memcpy(sc->dbuf + dptr, buf, clen);
  479. dptr += clen;
  480. buf += clen;
  481. len -= clen;
  482. if (dptr == rate) {
  483. xor_block(sc->A, sc->dbuf, rate);
  484. process_block(sc->A);
  485. dptr = 0;
  486. }
  487. }
  488. sc->dptr = dptr;
  489. }
  490. /* see bearssl_kdf.h */
  491. void
  492. br_shake_flip(br_shake_context *sc)
  493. {
  494. /*
  495. * We apply padding and pre-XOR the value into the state. We
  496. * set dptr to the end of the buffer, so that first call to
  497. * shake_extract() will process the block.
  498. */
  499. if ((sc->dptr + 1) == sc->rate) {
  500. sc->dbuf[sc->dptr ++] = 0x9F;
  501. } else {
  502. sc->dbuf[sc->dptr ++] = 0x1F;
  503. memset(sc->dbuf + sc->dptr, 0x00, sc->rate - sc->dptr - 1);
  504. sc->dbuf[sc->rate - 1] = 0x80;
  505. sc->dptr = sc->rate;
  506. }
  507. xor_block(sc->A, sc->dbuf, sc->rate);
  508. }
  509. /* see bearssl_kdf.h */
  510. void
  511. br_shake_produce(br_shake_context *sc, void *out, size_t len)
  512. {
  513. unsigned char *buf;
  514. size_t dptr, rate;
  515. buf = out;
  516. dptr = sc->dptr;
  517. rate = sc->rate;
  518. while (len > 0) {
  519. size_t clen;
  520. if (dptr == rate) {
  521. unsigned char *dbuf;
  522. uint64_t *A;
  523. A = sc->A;
  524. dbuf = sc->dbuf;
  525. process_block(A);
  526. br_enc64le(dbuf + 0, A[ 0]);
  527. br_enc64le(dbuf + 8, ~A[ 1]);
  528. br_enc64le(dbuf + 16, ~A[ 2]);
  529. br_enc64le(dbuf + 24, A[ 3]);
  530. br_enc64le(dbuf + 32, A[ 4]);
  531. br_enc64le(dbuf + 40, A[ 5]);
  532. br_enc64le(dbuf + 48, A[ 6]);
  533. br_enc64le(dbuf + 56, A[ 7]);
  534. br_enc64le(dbuf + 64, ~A[ 8]);
  535. br_enc64le(dbuf + 72, A[ 9]);
  536. br_enc64le(dbuf + 80, A[10]);
  537. br_enc64le(dbuf + 88, A[11]);
  538. br_enc64le(dbuf + 96, ~A[12]);
  539. br_enc64le(dbuf + 104, A[13]);
  540. br_enc64le(dbuf + 112, A[14]);
  541. br_enc64le(dbuf + 120, A[15]);
  542. br_enc64le(dbuf + 128, A[16]);
  543. br_enc64le(dbuf + 136, ~A[17]);
  544. br_enc64le(dbuf + 144, A[18]);
  545. br_enc64le(dbuf + 152, A[19]);
  546. br_enc64le(dbuf + 160, ~A[20]);
  547. br_enc64le(dbuf + 168, A[21]);
  548. br_enc64le(dbuf + 176, A[22]);
  549. br_enc64le(dbuf + 184, A[23]);
  550. br_enc64le(dbuf + 192, A[24]);
  551. dptr = 0;
  552. }
  553. clen = rate - dptr;
  554. if (clen > len) {
  555. clen = len;
  556. }
  557. memcpy(buf, sc->dbuf + dptr, clen);
  558. dptr += clen;
  559. buf += clen;
  560. len -= clen;
  561. }
  562. sc->dptr = dptr;
  563. }