belt_test.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. *******************************************************************************
  3. \file belt_test.c
  4. \brief Tests for STB 34.101.31 (belt)
  5. \project bee2/test
  6. \created 2012.06.20
  7. \version 2024.01.25
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/mem.h>
  13. #include <bee2/core/hex.h>
  14. #include <bee2/core/u32.h>
  15. #include <bee2/core/util.h>
  16. #include <bee2/core/word.h>
  17. #include <bee2/crypto/belt.h>
  18. /*
  19. *******************************************************************************
  20. Внутренние функции модуля belt (для тестирования belt-compress)
  21. *******************************************************************************
  22. */
  23. /*
  24. *******************************************************************************
  25. Generating the Belt S-box H
  26. Algorithm:
  27. H[10] = 0,
  28. H[11 + x) % 256] = 0x8E * 0x02^{116 x}, x = 0,1,...,254.
  29. Here octets are interpreted as binary polynomials:
  30. 0x8E = z^7 + z^3 + z^2 + z^1, 0x02 = z.
  31. The multiplication is performed in the ring of such polynomials modulo
  32. f = 0x163 = z^8 + z^7 + z^6 + z + 1.
  33. 1. Since f is irreducible, this factor-ring is a field.
  34. 2. \alpha = 0x02 is a primitive element of the field.
  35. 3. 116 and 255 are coprime and \alpha^116 is also primitive.
  36. 4. Therefore, the elements 0x8E * 0x02^{116 x} are pairwise distinct. They
  37. are also nonzero and H is bijective.
  38. *******************************************************************************
  39. */
  40. static void beltHGen(octet H[256])
  41. {
  42. size_t x, i;
  43. H[10] = 0, H[11] = 0x8E;
  44. for (x = 12; x < 10 + 256; ++x)
  45. {
  46. word t = H[(x - 1) % 256];
  47. for (i = 0; i < 116; ++i)
  48. t = t >> 1 | wordParity(t & 0x63) << 7;
  49. H[x % 256] = (octet)t;
  50. }
  51. }
  52. /*
  53. *******************************************************************************
  54. Специальный тест Zerosum
  55. Тест основан на задаче http://apmi.bsu.by/resources/tasks#60:
  56. найти 128-битовые блоки X_0,..., X_127 такие, что
  57. X_0 ^ ... ^ X_127 ^ Belt_0(X_0) ^ ... ^ Belt_0(X_127) = 0.
  58. *******************************************************************************
  59. */
  60. static const u32 _zerosum[128] = {
  61. 15014,124106,166335,206478,313245,366839,455597,502723,535141,625112,
  62. 659461,752253,801048,897899,943850,1041695,1101266,1170856,1217537,
  63. 1248520,1366084,1421171,1448429,1514215,1573855,1701341,1738016,1781705,
  64. 1837300,1948449,1999650,2089289,2117830,2175758,2249930,2358928,2404262,
  65. 2447467,2552783,2556713,2678348,2705770,2808011,2827994,2948039,2995213,
  66. 3029188,3096649,3170243,3230306,3285991,3350691,3457162,3500592,3539783,
  67. 3636611,3735543,3752463,3814136,3875630,3935109,4002291,4088401,4129247,
  68. 4257830,4266427,4352389,4397389,4470348,4531932,4598961,4691323,4747531,
  69. 4839756,4900773,4958368,5021928,5099836,5164752,5214964,5269476,5356247,
  70. 5391667,5496861,5561223,5601750,5700311,5761736,5812345,5856838,5956987,
  71. 5966502,6059392,6104328,6193021,6233226,6311341,6369016,6475468,6540894,
  72. 6598453,6666092,6711620,6804478,6834201,6932158,6971325,7059579,7089192,
  73. 7188715,7245095,7325355,7367748,7426778,7475903,7599231,7643174,7722266,
  74. 7747291,7832837,7887591,7942192,8043937,8108261,8169299,8233361,8305861,
  75. 8367181,
  76. };
  77. static const u32 _key[8];
  78. static bool_t beltTestZerosum()
  79. {
  80. u32 block[4];
  81. u32 sum[4];
  82. size_t i;
  83. // sum <- 0
  84. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  85. // цикл по X_i
  86. for (i = 0; i < 128; ++i)
  87. {
  88. // sum <- X_i ^ Belt_0(X_i)
  89. block[0] = _zerosum[i];
  90. block[1] = block[2] = block[3] = 0;
  91. beltBlockEncr2(block, _key);
  92. sum[0] ^= _zerosum[i] ^ block[0];
  93. sum[1] ^= block[1];
  94. sum[2] ^= block[2];
  95. sum[3] ^= block[3];
  96. }
  97. // sum == 0?
  98. return sum[0] == 0 && sum[1] == 0 && sum[2] == 0 && sum[3] == 0;
  99. }
  100. /*
  101. *******************************************************************************
  102. Самотестирование
  103. -# Выполняются тесты из приложения A к СТБ 34.101.31 (редакция 2018 года)
  104. и из приложения Б к СТБ 34.101.47.
  105. -# Номера тестов соответствуют номерам таблиц приложений.
  106. -# Дополнительно выполняется тест Zerosum.
  107. *******************************************************************************
  108. */
  109. bool_t beltTest()
  110. {
  111. octet buf[128];
  112. octet buf1[128];
  113. octet mac[8];
  114. octet mac1[8];
  115. octet hash[32];
  116. octet hash1[32];
  117. u32 key[8];
  118. u32 block[4];
  119. octet level[12];
  120. octet state[1024];
  121. size_t count;
  122. // подготовить память
  123. if (sizeof(state) < utilMax(17,
  124. 256,
  125. beltWBL_keep(),
  126. beltCompr_deep(),
  127. beltECB_keep(),
  128. beltCBC_keep(),
  129. beltCFB_keep(),
  130. beltCTR_keep(),
  131. beltMAC_keep(),
  132. beltDWP_keep(),
  133. beltCHE_keep(),
  134. beltKWP_keep(),
  135. beltHash_keep(),
  136. beltBDE_keep(),
  137. beltSDE_keep(),
  138. beltFMT_keep(65536, 17),
  139. beltKRP_keep(),
  140. beltHMAC_keep()))
  141. return FALSE;
  142. // belt-H
  143. beltHGen(state);
  144. if (!memEq(state, beltH(), 256))
  145. return FALSE;
  146. // belt-block: тест A.1-1
  147. memCopy(buf, beltH(), 16);
  148. beltKeyExpand2(key, beltH() + 128, 32);
  149. beltBlockEncr(buf, key);
  150. if (!hexEq(buf,
  151. "69CCA1C93557C9E3D66BC3E0FA88FA6E"))
  152. return FALSE;
  153. beltBlockDecr(buf, key);
  154. if (!memEq(buf, beltH(), 16))
  155. return FALSE;
  156. // belt-block: тест A.1-2
  157. u32From(block, beltH(), 16);
  158. beltBlockEncr2(block, key);
  159. u32To(buf, 16, block);
  160. if (!hexEq(buf,
  161. "69CCA1C93557C9E3D66BC3E0FA88FA6E"))
  162. return FALSE;
  163. beltBlockDecr2(block, key);
  164. u32To(buf, 16, block);
  165. if (!memEq(buf, beltH(), 16))
  166. return FALSE;
  167. // belt-block: тест A.1-3
  168. beltBlockEncr3(block + 0, block + 1, block + 2, block + 3, key);
  169. u32To(buf, 16, block);
  170. if (!hexEq(buf,
  171. "69CCA1C93557C9E3D66BC3E0FA88FA6E"))
  172. return FALSE;
  173. beltBlockDecr3(block + 0, block + 1, block + 2, block + 3, key);
  174. u32To(buf, 16, block);
  175. if (!memEq(buf, beltH(), 16))
  176. return FALSE;
  177. // belt-block: тест A.4
  178. memCopy(buf, beltH() + 64, 16);
  179. beltKeyExpand2(key, beltH() + 128 + 32, 32);
  180. beltBlockDecr(buf, key);
  181. if (!hexEq(buf,
  182. "0DC5300600CAB840B38448E5E993F421"))
  183. return FALSE;
  184. // belt-wblock: тест A.6-1
  185. memCopy(buf, beltH(), 48);
  186. beltWBLStart(state, beltH() + 128, 32);
  187. beltWBLStepE(buf, 48, state);
  188. if (!hexEq(buf,
  189. "49A38EE108D6C742E52B774F00A6EF98"
  190. "B106CBD13EA4FB0680323051BC04DF76"
  191. "E487B055C69BCF541176169F1DC9F6C8"))
  192. return FALSE;
  193. // belt-wblock: тест A.6-2
  194. memCopy(buf, beltH(), 47);
  195. beltWBLStart(state, beltH() + 128, 32);
  196. beltWBLStepE(buf, 47, state);
  197. if (!hexEq(buf,
  198. "F08EF22DCAA06C81FB12721974221CA7"
  199. "AB82C62856FCF2F9FCA006E019A28F16"
  200. "E5821A51F573594625DBAB8F6A5C94"))
  201. return FALSE;
  202. // belt-wblock: тест A.7-1
  203. memCopy(buf, beltH() + 64, 48);
  204. beltWBLStart(state, beltH() + 128 + 32, 32);
  205. beltWBLStepD(buf, 48, state);
  206. if (!hexEq(buf,
  207. "92632EE0C21AD9E09A39343E5C07DAA4"
  208. "889B03F2E6847EB152EC99F7A4D9F154"
  209. "B5EF68D8E4A39E567153DE13D72254EE"))
  210. return FALSE;
  211. // belt-wblock: тест A.7-2
  212. memCopy(buf, beltH() + 64, 36);
  213. beltWBLStart(state, beltH() + 128 + 32, 32);
  214. beltWBLStepD(buf, 36, state);
  215. if (!hexEq(buf,
  216. "DF3F882230BAAFFC92F0566032117231"
  217. "0E3CB2182681EF43102E67175E177BD7"
  218. "5E93E4E8"))
  219. return FALSE;
  220. // belt-wblock: special
  221. beltWBLStart(state, beltH() + 128, 32);
  222. for (count = 32; count <= 128; ++count)
  223. {
  224. memCopy(buf, beltH(), count);
  225. beltWBLStepE(buf, count, state);
  226. memCopy(buf1, buf, count);
  227. beltWBLStepD(buf, count, state);
  228. if (!memEq(buf, beltH(), count))
  229. return FALSE;
  230. beltWBLStepD2(buf1, buf1 + count - 16, count, state);
  231. if (!memEq(buf1, beltH(), count))
  232. return FALSE;
  233. }
  234. // belt-compr: тест A.8
  235. u32From((u32*)buf, beltH(), 32);
  236. u32From((u32*)hash, beltH() + 32, 32);
  237. memSetZero(hash1, 16);
  238. beltCompr2((u32*)hash1, (u32*)hash, (u32*)buf, state);
  239. u32To(hash1, 16, (u32*)hash1);
  240. u32To(hash, 32, (u32*)hash);
  241. if (!hexEq(hash1,
  242. "46FE7425C9B181EB41DFEE3E72163D5A"))
  243. return FALSE;
  244. if (!hexEq(hash,
  245. "ED2F5481D593F40D87FCE37D6BC1A2E1"
  246. "B7D1A2CC975C82D3C0497488C90D99D8"))
  247. return FALSE;
  248. u32From((u32*)hash1, beltH() + 32, 32);
  249. beltCompr((u32*)hash1, (u32*)buf, state);
  250. u32To(hash1, 32, (u32*)hash1);
  251. if (!memEq(hash, hash1, 32))
  252. return FALSE;
  253. // belt-ecb: тест A.9-1
  254. memCopy(buf, beltH(), 48);
  255. beltECBStart(state, beltH() + 128, 32);
  256. beltECBStepE(buf, 32, state);
  257. beltECBStepE(buf + 32, 48 - 32, state);
  258. if (!hexEq(buf,
  259. "69CCA1C93557C9E3D66BC3E0FA88FA6E"
  260. "5F23102EF109710775017F73806DA9DC"
  261. "46FB2ED2CE771F26DCB5E5D1569F9AB0"))
  262. return FALSE;
  263. beltECBEncr(buf1, beltH(), 48, beltH() + 128, 32);
  264. if (!memEq(buf, buf1, 48))
  265. return FALSE;
  266. // belt-ecb: тест A.9-2
  267. memCopy(buf, beltH(), 47);
  268. beltECBStart(state, beltH() + 128, 32);
  269. beltECBStepE(buf, 16, state);
  270. beltECBStepE(buf + 16, 47 - 16, state);
  271. if (!hexEq(buf,
  272. "69CCA1C93557C9E3D66BC3E0FA88FA6E"
  273. "36F00CFED6D1CA1498C12798F4BEB207"
  274. "5F23102EF109710775017F73806DA9"))
  275. return FALSE;
  276. beltECBEncr(buf1, beltH(), 47, beltH() + 128, 32);
  277. if (!memEq(buf, buf1, 47))
  278. return FALSE;
  279. // belt-ecb: тест A.10-1
  280. memCopy(buf, beltH() + 64, 48);
  281. beltECBStart(state, beltH() + 128 + 32, 32);
  282. beltECBStepD(buf, 16, state);
  283. beltECBStepD(buf + 16, 48 - 16, state);
  284. if (!hexEq(buf,
  285. "0DC5300600CAB840B38448E5E993F421"
  286. "E55A239F2AB5C5D5FDB6E81B40938E2A"
  287. "54120CA3E6E19C7AD750FC3531DAEAB7"))
  288. return FALSE;
  289. beltECBDecr(buf1, beltH() + 64, 48, beltH() + 128 + 32, 32);
  290. if (!memEq(buf, buf1, 48))
  291. return FALSE;
  292. // belt-ecb: тест A.10-2
  293. memCopy(buf, beltH() + 64, 36);
  294. beltECBStart(state, beltH() + 128 + 32, 32);
  295. beltECBStepD(buf, 16, state);
  296. beltECBStepD(buf + 16, 36 - 16, state);
  297. if (!hexEq(buf,
  298. "0DC5300600CAB840B38448E5E993F421"
  299. "5780A6E2B69EAFBB258726D7B6718523"
  300. "E55A239F"))
  301. return FALSE;
  302. beltECBDecr(buf1, beltH() + 64, 36, beltH() + 128 + 32, 32);
  303. if (!memEq(buf, buf1, 36))
  304. return FALSE;
  305. // belt-cbc: тест A.11-1
  306. memCopy(buf, beltH(), 48);
  307. beltCBCStart(state, beltH() + 128, 32, beltH() + 192);
  308. beltCBCStepE(buf, 32, state);
  309. beltCBCStepE(buf + 32, 48 - 32, state);
  310. if (!hexEq(buf,
  311. "10116EFAE6AD58EE14852E11DA1B8A74"
  312. "5CF2480E8D03F1C19492E53ED3A70F60"
  313. "657C1EE8C0E0AE5B58388BF8A68E3309"))
  314. return FALSE;
  315. beltCBCEncr(buf1, beltH(), 48, beltH() + 128, 32, beltH() + 192);
  316. if (!memEq(buf, buf1, 48))
  317. return FALSE;
  318. // belt-cbc: тест A.11-2
  319. memCopy(buf, beltH(), 36);
  320. beltCBCStart(state, beltH() + 128, 32, beltH() + 192);
  321. beltCBCStepE(buf, 16, state);
  322. beltCBCStepE(buf + 16, 36 - 16, state);
  323. if (!hexEq(buf,
  324. "10116EFAE6AD58EE14852E11DA1B8A74"
  325. "6A9BBADCAF73F968F875DEDC0A44F6B1"
  326. "5CF2480E"))
  327. return FALSE;
  328. beltCBCEncr(buf1, beltH(), 36, beltH() + 128, 32, beltH() + 192);
  329. if (!memEq(buf, buf1, 36))
  330. return FALSE;
  331. // belt-cbc: тест A.12-1
  332. memCopy(buf, beltH() + 64, 48);
  333. beltCBCStart(state, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  334. beltCBCStepD(buf, 16, state);
  335. beltCBCStepD(buf + 16, 48 - 16, state);
  336. if (!hexEq(buf,
  337. "730894D6158E17CC1600185A8F411CAB"
  338. "0471FF85C83792398D8924EBD57D03DB"
  339. "95B97A9B7907E4B020960455E46176F8"))
  340. return FALSE;
  341. beltCBCDecr(buf1, beltH() + 64, 48, beltH() + 128 + 32, 32,
  342. beltH() + 192 + 16);
  343. if (!memEq(buf, buf1, 48))
  344. return FALSE;
  345. // belt-cbc: тест A.12-2
  346. memCopy(buf, beltH() + 64, 36);
  347. beltCBCStart(state, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  348. beltCBCStepD(buf, 16, state);
  349. beltCBCStepD(buf + 16, 36 - 16, state);
  350. if (!hexEq(buf,
  351. "730894D6158E17CC1600185A8F411CAB"
  352. "B6AB7AF8541CF85755B8EA27239F08D2"
  353. "166646E4"))
  354. return FALSE;
  355. beltCBCDecr(buf1, beltH() + 64, 36, beltH() + 128 + 32, 32,
  356. beltH() + 192 + 16);
  357. if (!memEq(buf, buf1, 36))
  358. return FALSE;
  359. // belt-cfb: тест A.13
  360. memCopy(buf, beltH(), 48);
  361. beltCFBStart(state, beltH() + 128, 32, beltH() + 192);
  362. beltCFBStepE(buf, 16, state);
  363. beltCFBStepE(buf + 16, 3, state);
  364. beltCFBStepE(buf + 16 + 3, 48 - 16 - 3, state);
  365. if (!hexEq(buf,
  366. "C31E490A90EFA374626CC99E4B7B8540"
  367. "A6E48685464A5A06849C9CA769A1B0AE"
  368. "55C2CC5939303EC832DD2FE16C8E5A1B"))
  369. return FALSE;
  370. beltCFBEncr(buf1, beltH(), 48, beltH() + 128, 32, beltH() + 192);
  371. if (!memEq(buf, buf1, 48))
  372. return FALSE;
  373. // belt-cfb: тест A.14
  374. memCopy(buf, beltH() + 64, 48);
  375. beltCFBStart(state, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  376. beltCFBStepD(buf, 15, state);
  377. beltCFBStepD(buf + 15, 7, state);
  378. beltCFBStepD(buf + 15 + 7, 48 - 15 - 7, state);
  379. if (!hexEq(buf,
  380. "FA9D107A86F375EE65CD1DB881224BD0"
  381. "16AFF814938ED39B3361ABB0BF0851B6"
  382. "52244EB06842DD4C94AA4500774E40BB"))
  383. return FALSE;
  384. beltCFBDecr(buf1, beltH() + 64, 48, beltH() + 128 + 32, 32,
  385. beltH() + 192 + 16);
  386. if (!memEq(buf, buf1, 48))
  387. return FALSE;
  388. // belt-ctr: тест A.15
  389. memCopy(buf, beltH(), 48);
  390. beltCTRStart(state, beltH() + 128, 32, beltH() + 192);
  391. beltCTRStepE(buf, 15, state);
  392. beltCTRStepE(buf + 15, 7, state);
  393. beltCTRStepE(buf + 15 + 7, 48 - 15 - 7, state);
  394. if (!hexEq(buf,
  395. "52C9AF96FF50F64435FC43DEF56BD797"
  396. "D5B5B1FF79FB41257AB9CDF6E63E81F8"
  397. "F00341473EAE409833622DE05213773A"))
  398. return FALSE;
  399. beltCTR(buf1, beltH(), 48, beltH() + 128, 32, beltH() + 192);
  400. if (!memEq(buf, buf1, 48))
  401. return FALSE;
  402. // belt-ctr: тест A.16
  403. memCopy(buf, beltH() + 64, 44);
  404. beltCTRStart(state, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  405. beltCTRStepD(buf, 11, state);
  406. beltCTRStepD(buf + 11, 5, state);
  407. beltCTRStepD(buf + 11 + 5, 44 - 11 - 5, state);
  408. if (!hexEq(buf,
  409. "DF181ED008A20F43DCBBB93650DAD34B"
  410. "389CDEE5826D40E2D4BD80F49A93F5D2"
  411. "12F6333166456F169043CC5F"))
  412. return FALSE;
  413. beltCTR(buf1, beltH() + 64, 44, beltH() + 128 + 32, 32,
  414. beltH() + 192 + 16);
  415. if (!memEq(buf, buf1, 44))
  416. return FALSE;
  417. // belt-mac: тест A.17-1
  418. beltMACStart(state, beltH() + 128, 32);
  419. beltMACStepA(beltH(), 13, state);
  420. hexTo(buf, "7260DA60138F96C9");
  421. if (!beltMACStepV(buf, state))
  422. return FALSE;
  423. beltMAC(buf1, beltH(), 13, beltH() + 128, 32);
  424. if (!memEq(buf, buf1, 8))
  425. return FALSE;
  426. // belt-mac: тест A.17-2 [+ инкрементальность]
  427. beltMACStart(state, beltH() + 128, 32);
  428. beltMACStepA(beltH(), 27, state);
  429. beltMACStepG(buf, state);
  430. beltMACStepA(beltH() + 27, 48 - 27, state);
  431. beltMACStepG2(buf, 4, state);
  432. hexTo(buf, "2DAB59771B4B16D0");
  433. if (!beltMACStepV(buf, state) || !beltMACStepV2(buf, 3, state))
  434. return FALSE;
  435. beltMAC(buf1, beltH(), 48, beltH() + 128, 32);
  436. if (!memEq(buf, buf1, 8))
  437. return FALSE;
  438. // belt-dwp: тест A.19-1 [+ инкрементальность]
  439. beltDWPStart(state, beltH() + 128, 32, beltH() + 192);
  440. memCopy(buf, beltH(), 16);
  441. beltDWPStepE(buf, 7, state);
  442. beltDWPStepE(buf + 7, 9, state);
  443. beltDWPStepI(beltH() + 16, 14, state);
  444. beltDWPStepG(mac, state);
  445. beltDWPStepI(beltH() + 16 + 14, 18, state);
  446. beltDWPStepG(mac, state);
  447. beltDWPStepA(buf, 12, state);
  448. beltDWPStepG(mac, state);
  449. beltDWPStepA(buf + 12, 4, state);
  450. beltDWPStepG(mac, state);
  451. if (!hexEq(buf,
  452. "52C9AF96FF50F64435FC43DEF56BD797"))
  453. return FALSE;
  454. if (!hexEq(mac,
  455. "3B2E0AEB2B91854B"))
  456. return FALSE;
  457. if (!beltDWPStepV(mac, state))
  458. return FALSE;
  459. beltDWPWrap(buf1, mac1, beltH(), 16, beltH() + 16, 32,
  460. beltH() + 128, 32, beltH() + 192);
  461. if (!memEq(buf, buf1, 16) || !memEq(mac, mac1, 8))
  462. return FALSE;
  463. // belt-che: тест A.19-2 [+ инкрементальность]
  464. beltCHEStart(state, beltH() + 128, 32, beltH() + 192);
  465. memCopy(buf, beltH(), 15);
  466. beltCHEStepE(buf, 11, state);
  467. beltCHEStepE(buf + 11, 4, state);
  468. beltCHEStepI(beltH() + 16, 14, state);
  469. beltCHEStepG(mac, state);
  470. beltCHEStepI(beltH() + 16 + 14, 18, state);
  471. beltCHEStepG(mac, state);
  472. beltCHEStepA(buf, 12, state);
  473. beltCHEStepG(mac, state);
  474. beltCHEStepA(buf + 12, 3, state);
  475. beltCHEStepG(mac, state);
  476. if (!hexEq(buf,
  477. "BF3DAEAF5D18D2BCC30EA62D2E70A4"))
  478. return FALSE;
  479. if (!hexEq(mac,
  480. "548622B844123FF7"))
  481. return FALSE;
  482. if (!beltCHEStepV(mac, state))
  483. return FALSE;
  484. beltCHEWrap(buf1, mac1, beltH(), 15, beltH() + 16, 32,
  485. beltH() + 128, 32, beltH() + 192);
  486. if (!memEq(buf, buf1, 15) || !memEq(mac, mac1, 8))
  487. return FALSE;
  488. // belt-dwp: тест A.20-1
  489. beltDWPStart(state, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  490. memCopy(buf, beltH() + 64, 16);
  491. beltDWPStepI(beltH() + 64 + 16, 32, state);
  492. beltDWPStepA(buf, 16, state);
  493. beltDWPStepD(buf, 16, state);
  494. beltDWPStepG(mac, state);
  495. if (!hexEq(buf,
  496. "DF181ED008A20F43DCBBB93650DAD34B"))
  497. return FALSE;
  498. if (!hexEq(mac,
  499. "6A2C2C94C4150DC0"))
  500. return FALSE;
  501. if (beltDWPUnwrap(buf1, beltH() + 64, 16, beltH() + 64 + 16, 32,
  502. mac, beltH() + 128 + 32, 32, beltH() + 192 + 16) != ERR_OK ||
  503. !memEq(buf, buf1, 16))
  504. return FALSE;
  505. beltDWPWrap(buf1, mac1, buf, 16, beltH() + 64 + 16, 32,
  506. beltH() + 128 + 32, 32, beltH() + 192 + 16);
  507. if (!memEq(buf1, beltH() + 64, 16) || !memEq(mac, mac1, 8))
  508. return FALSE;
  509. // belt-che: тест A.20-2
  510. beltCHEStart(state, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  511. memCopy(buf, beltH() + 64, 20);
  512. beltCHEStepI(beltH() + 64 + 16, 32, state);
  513. beltCHEStepA(buf, 20, state);
  514. beltCHEStepD(buf, 20, state);
  515. beltCHEStepG(mac, state);
  516. if (!hexEq(buf,
  517. "2BABF43EB37B5398A9068F31A3C758B762F44AA9"))
  518. return FALSE;
  519. if (!hexEq(mac,
  520. "7D9D4F59D40D197D"))
  521. return FALSE;
  522. if (beltCHEUnwrap(buf1, beltH() + 64, 20, beltH() + 64 + 16, 32,
  523. mac, beltH() + 128 + 32, 32, beltH() + 192 + 16) != ERR_OK ||
  524. !memEq(buf, buf1, 20))
  525. return FALSE;
  526. beltCHEWrap(buf1, mac1, buf, 20, beltH() + 64 + 16, 32,
  527. beltH() + 128 + 32, 32, beltH() + 192 + 16);
  528. if (!memEq(buf1, beltH() + 64, 20) || !memEq(mac, mac1, 8))
  529. return FALSE;
  530. // belt-kwp: тест A.21
  531. beltKWPStart(state, beltH() + 128, 32);
  532. memCopy(buf, beltH(), 32);
  533. memCopy(buf + 32, beltH() + 32, 16);
  534. beltKWPStepE(buf, 48, state);
  535. if (!hexEq(buf,
  536. "49A38EE108D6C742E52B774F00A6EF98"
  537. "B106CBD13EA4FB0680323051BC04DF76"
  538. "E487B055C69BCF541176169F1DC9F6C8"))
  539. return FALSE;
  540. beltKWPWrap(buf1, beltH(), 32, beltH() + 32, beltH() + 128, 32);
  541. if (!memEq(buf, buf1, 48))
  542. return FALSE;
  543. // belt-kwp: тест A.22
  544. beltKWPStart(state, beltH() + 128 + 32, 32);
  545. memCopy(buf, beltH() + 64, 48);
  546. beltKWPStepD(buf, 48, state);
  547. if (!hexEq(buf,
  548. "92632EE0C21AD9E09A39343E5C07DAA4"
  549. "889B03F2E6847EB152EC99F7A4D9F154"))
  550. return FALSE;
  551. if (!hexEq(buf + 32,
  552. "B5EF68D8E4A39E567153DE13D72254EE"))
  553. return FALSE;
  554. if (beltKWPUnwrap(buf1, beltH() + 64, 48, (octet*)buf + 32,
  555. beltH() + 128 + 32, 32) != ERR_OK ||
  556. !memEq(buf, buf1, 32))
  557. return FALSE;
  558. // belt-hash: тест A.23-1
  559. beltHashStart(state);
  560. beltHashStepH(beltH(), 13, state);
  561. beltHashStepG(hash, state);
  562. if (!hexEq(hash,
  563. "ABEF9725D4C5A83597A367D14494CC25"
  564. "42F20F659DDFECC961A3EC550CBA8C75"))
  565. return FALSE;
  566. beltHash(hash1, beltH(), 13);
  567. if (!memEq(hash, hash1, 32))
  568. return FALSE;
  569. // belt-hash: тест A.23-2
  570. beltHashStart(state);
  571. beltHashStepH(beltH(), 32, state);
  572. hexTo(hash,
  573. "749E4C3653AECE5E48DB4761227742EB"
  574. "6DBE13F4A80F7BEFF1A9CF8D10EE7786");
  575. if (!beltHashStepV(hash, state) || !beltHashStepV2(hash, 13, state))
  576. return FALSE;
  577. beltHash(hash1, beltH(), 32);
  578. if (!memEq(hash, hash1, 32))
  579. return FALSE;
  580. // belt-hash: тест A.23-3 [+ инкрементальность]
  581. beltHashStart(state);
  582. beltHashStepH(beltH(), 11, state);
  583. beltHashStepG2(hash, 32, state);
  584. beltHashStepH(beltH() + 11, 48 - 11, state);
  585. hexTo(hash,
  586. "9D02EE446FB6A29FE5C982D4B13AF9D3"
  587. "E90861BC4CEF27CF306BFB0B174A154A");
  588. if (!beltHashStepV2(hash, 32, state))
  589. return FALSE;
  590. beltHash(hash1, beltH(), 48);
  591. if (!memEq(hash, hash1, 32))
  592. return FALSE;
  593. // belt-bde: тест A.24-1
  594. memCopy(buf, beltH(), 48);
  595. beltBDEStart(state, beltH() + 128, 32, beltH() + 192);
  596. beltBDEStepE(buf, 32, state);
  597. beltBDEStepE(buf + 32, 48 - 32, state);
  598. if (!hexEq(buf,
  599. "E9CAB32D879CC50C10378EB07C10F263"
  600. "07257E2DBE2B854CBC9F38282D59D6A7"
  601. "7F952001C5D1244F53210A27C216D4BB"))
  602. return FALSE;
  603. beltBDEEncr(buf1, beltH(), 48, beltH() + 128, 32, beltH() + 192);
  604. if (!memEq(buf, buf1, 48))
  605. return FALSE;
  606. beltBDEDecr(buf1, buf1, 48, beltH() + 128, 32, beltH() + 192);
  607. if (!memEq(buf1, beltH(), 48))
  608. return FALSE;
  609. // belt-bde: тест A.25-1
  610. memCopy(buf, beltH() + 64, 48);
  611. beltBDEStart(state, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  612. beltBDEStepD(buf, 16, state);
  613. beltBDEStepD(buf + 16, 48 - 16, state);
  614. if (!hexEq(buf,
  615. "7041BC226352C706D00EA8EF23CFE46A"
  616. "FAE118577D037FACDC36E4ECC1F65746"
  617. "09F236943FB809E1BEE4A1C686C13ACC"))
  618. return FALSE;
  619. beltBDEDecr(buf1, beltH() + 64, 48, beltH() + 128 + 32, 32,
  620. beltH() + 192 + 16);
  621. if (!memEq(buf, buf1, 48))
  622. return FALSE;
  623. beltBDEEncr(buf, buf1, 48, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  624. if (!memEq(buf, beltH() + 64, 48))
  625. return FALSE;
  626. // belt-sde: тест A.24-2
  627. memCopy(buf, beltH(), 48);
  628. beltSDEStart(state, beltH() + 128, 32);
  629. beltSDEStepE(buf, 48, beltH() + 192, state);
  630. if (!hexEq(buf,
  631. "1FCBB01852003D60B66024C508608BAA"
  632. "2C21AF1E884CF31154D3077D4643CF22"
  633. "49EB2F5A68E4BA019D90211A81D690D9"))
  634. return FALSE;
  635. beltSDEEncr(buf1, beltH(), 48, beltH() + 128, 32, beltH() + 192);
  636. if (!memEq(buf, buf1, 48))
  637. return FALSE;
  638. beltSDEDecr(buf1, buf1, 48, beltH() + 128, 32, beltH() + 192);
  639. if (!memEq(buf1, beltH(), 48))
  640. return FALSE;
  641. // belt-sde: тест A.25-2
  642. memCopy(buf, beltH() + 64, 48);
  643. beltSDEStart(state, beltH() + 128 + 32, 32);
  644. beltSDEStepD(buf, 48, beltH() + 192 + 16, state);
  645. if (!hexEq(buf,
  646. "E9FDF3F788657332E6C46FCF5251B8A6"
  647. "D43543A93E3233837DB1571183A6EF4D"
  648. "7FEB5CDF999E1A3F51A5A3381BEB7FA5"))
  649. return FALSE;
  650. beltSDEDecr(buf1, beltH() + 64, 48, beltH() + 128 + 32, 32,
  651. beltH() + 192 + 16);
  652. if (!memEq(buf, buf1, 48))
  653. return FALSE;
  654. beltSDEEncr(buf, buf1, 48, beltH() + 128 + 32, 32, beltH() + 192 + 16);
  655. if (!memEq(buf, beltH() + 64, 48))
  656. return FALSE;
  657. // belt-fmt: тест A.26
  658. {
  659. u16 str[21] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,};
  660. u16 str1[21];
  661. const u16 test_fmt1[] = {6,9,3,4,7,7,0,3,5,2};
  662. const u16 test_fmt2[] = {7,4,6,21,49,55,24,23,22,50,27,39,24,24,17,32,
  663. 57,43,26,5,29};
  664. const u16 test_fmt3[] = { 14290,31359,58054,51842,44653,34762,28652,
  665. 48929,6541,13788,7784,46182,61098,43056,3564,21568,63878};
  666. // субтест 1: belt-block
  667. beltFMTEncr(str1, 10, str, 10, beltH() + 128, 32, beltH() + 192);
  668. if (!memEq(str1, test_fmt1, 10 * 2))
  669. return FALSE;
  670. beltFMTDecr(str1, 10, str1, 10, beltH() + 128, 32, beltH() + 192);
  671. if (!memEq(str, str1, 10 * 2))
  672. return FALSE;
  673. // субтест 2: base58, на стыке belt-block и belt-32block
  674. beltFMTEncr(str1, 58, str, 21, beltH() + 128, 32, beltH() + 192);
  675. if (!memEq(str1, test_fmt2, 21 * 2))
  676. return FALSE;
  677. beltFMTDecr(str1, 58, str1, 21, beltH() + 128, 32, beltH() + 192);
  678. if (!memEq(str, str1, 21 * 2))
  679. return FALSE;
  680. // субтест 3: на стыке belt-32block и belt-wblock
  681. beltFMTEncr(str1, 65536, str, 17, beltH() + 128, 32, beltH() + 192);
  682. if (!memEq(str1, test_fmt3, 17 * 2))
  683. return FALSE;
  684. beltFMTDecr(str1, 65536, str1, 17, beltH() + 128, 32, beltH() + 192);
  685. if (!memEq(str, str1, 17 * 2))
  686. return FALSE;
  687. // другие тесты
  688. beltFMTEncr(str1, 9, str, 9, beltH() + 128, 32, beltH() + 192);
  689. beltFMTDecr(str1, 9, str1, 9, beltH() + 128, 32, beltH() + 192);
  690. if (!memEq(str, str1, 9 * 2))
  691. return FALSE;
  692. beltFMTEncr(str1, 11, str, 11, beltH() + 128, 32, 0);
  693. beltFMTDecr(str1, 11, str1, 11, beltH() + 128, 32, 0);
  694. if (!memEq(str, str1, 11 * 2))
  695. return FALSE;
  696. beltFMTEncr(str1, 256, str, 16, beltH() + 128, 32, beltH() + 192);
  697. beltFMTDecr(str1, 256, str1, 16, beltH() + 128, 32, beltH() + 192);
  698. if (!memEq(str, str1, 16 * 2))
  699. return FALSE;
  700. beltFMTEncr(str1, 257, str, 17, beltH() + 128, 32, beltH() + 192);
  701. beltFMTDecr(str1, 257, str1, 17, beltH() + 128, 32, beltH() + 192);
  702. if (!memEq(str, str1, 17 * 2))
  703. return FALSE;
  704. beltFMTEncr(str1, 49667, str, 9, beltH() + 128, 32, beltH() + 192);
  705. beltFMTDecr(str1, 49667, str1, 9, beltH() + 128, 32, beltH() + 192);
  706. if (!memEq(str, str1, 9 * 2))
  707. return FALSE;
  708. }
  709. // belt-keyexpand: тест A.27-1
  710. beltKeyExpand(buf, beltH() + 128, 16);
  711. if (!hexEq(buf,
  712. "E9DEE72C8F0C0FA62DDB49F46F739647"
  713. "E9DEE72C8F0C0FA62DDB49F46F739647"))
  714. return FALSE;
  715. // belt-keyrep: тест A.27-2
  716. beltKeyExpand2((u32*)buf, beltH() + 128, 24);
  717. u32To(buf, 32, (u32*)buf);
  718. if (!hexEq(buf,
  719. "E9DEE72C8F0C0FA62DDB49F46F739647"
  720. "06075316ED247A374B09A17E8450BF66"))
  721. return FALSE;
  722. // belt-keyrep: тест A.28-1
  723. memSetZero(level, 12);
  724. level[0] = 1;
  725. beltKRPStart(state, beltH() + 128, 32, level);
  726. beltKRPStepG(buf, 16, beltH() + 32, state);
  727. if (!hexEq(buf,
  728. "6BBBC2336670D31AB83DAA90D52C0541"))
  729. return FALSE;
  730. beltKRP(buf1, 16, beltH() + 128, 32, level, beltH() + 32);
  731. if (!memEq(buf, buf1, 16))
  732. return FALSE;
  733. // belt-keyrep: тест A.28-2
  734. beltKRPStepG(buf, 24, beltH() + 32, state);
  735. if (!hexEq(buf,
  736. "9A2532A18CBAF145398D5A95FEEA6C82"
  737. "5B9C197156A00275"))
  738. return FALSE;
  739. beltKRP(buf1, 24, beltH() + 128, 32, level, beltH() + 32);
  740. if (!memEq(buf, buf1, 24))
  741. return FALSE;
  742. // belt-keyrep: тест A.28-3
  743. beltKRPStepG(buf, 32, beltH() + 32, state);
  744. if (!hexEq(buf,
  745. "76E166E6AB21256B6739397B672B8796"
  746. "14B81CF05955FC3AB09343A745C48F77"))
  747. return FALSE;
  748. beltKRP(buf1, 32, beltH() + 128, 32, level, beltH() + 32);
  749. if (!memEq(buf, buf1, 32))
  750. return FALSE;
  751. // belt-hmac: тест Б.1-1
  752. beltHMACStart(state, beltH() + 128, 29);
  753. beltHMACStepA(beltH() + 128 + 64, 32, state);
  754. beltHMACStepG(hash, state);
  755. if (!hexEq(hash,
  756. "D4828E6312B08BB83C9FA6535A463554"
  757. "9E411FD11C0D8289359A1130E930676B"))
  758. return FALSE;
  759. beltHMAC(hash1, beltH() + 128 + 64, 32, beltH() + 128, 29);
  760. if (!memEq(hash, hash1, 32))
  761. return FALSE;
  762. // belt-hmac: тест Б.1-2
  763. beltHMACStart(state, beltH() + 128, 32);
  764. beltHMACStepA(beltH() + 128 + 64, 32, state);
  765. hexTo(hash,
  766. "41FFE8645AEC0612E952D2CDF8DD508F"
  767. "3E4A1D9B53F6A1DB293B19FE76B1879F");
  768. if (!beltHMACStepV(hash, state))
  769. return FALSE;
  770. beltHMAC(hash1, beltH() + 128 + 64, 32, beltH() + 128, 32);
  771. if (!memEq(hash, hash1, 32))
  772. return FALSE;
  773. // belt-hmac: тест Б.1-3 [+ икрементальность]
  774. beltHMACStart(state, beltH() + 128, 42);
  775. beltHMACStepA(beltH() + 128 + 64, 17, state);
  776. beltHMACStepG(hash, state);
  777. beltHMACStepG2(hash, 17, state);
  778. beltHMACStepA(beltH() + 128 + 64 + 17, 32 - 17, state);
  779. hexTo(hash,
  780. "7D01B84D2315C332277B3653D7EC6470"
  781. "7EBA7CDFF7FF70077B1DECBD68F2A144");
  782. if (!beltHMACStepV(hash, state) || !beltHMACStepV2(hash, 23, state))
  783. return FALSE;
  784. beltHMAC(hash1, beltH() + 128 + 64, 32, beltH() + 128, 42);
  785. if (!memEq(hash, hash1, 32))
  786. return FALSE;
  787. // zerosum
  788. if (!beltTestZerosum())
  789. return FALSE;
  790. // все нормально
  791. return TRUE;
  792. }