belt_test.c 26 KB

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