bash_test.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. *******************************************************************************
  3. \file bash_test.c
  4. \brief Tests for STB 34.101.77 (bash)
  5. \project bee2/test
  6. \created 2015.09.22
  7. \version 2023.03.30
  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/str.h>
  15. #include <bee2/core/util.h>
  16. #include <bee2/crypto/bash.h>
  17. #include <bee2/crypto/belt.h>
  18. /*
  19. *******************************************************************************
  20. Самотестирование
  21. Тесты из приложения А к СТБ 34.101.77.
  22. *******************************************************************************
  23. */
  24. bool_t bashTest()
  25. {
  26. octet buf[192];
  27. octet hash[64];
  28. octet state[1024];
  29. octet state1[1024];
  30. size_t pos;
  31. // подготовить память
  32. if (sizeof(state) < utilMax(3,
  33. bashF_deep(),
  34. bashHash_keep(),
  35. bashPrg_keep()) ||
  36. sizeof(state) != sizeof(state1))
  37. return FALSE;
  38. // A.2
  39. memCopy(buf, beltH(), 192);
  40. bashF(buf, state);
  41. if (!hexEq(buf,
  42. "8FE727775EA7F140B95BB6A200CBB28C"
  43. "7F0809C0C0BC68B7DC5AEDC841BD94E4"
  44. "03630C301FC255DF5B67DB53EF65E376"
  45. "E8A4D797A6172F2271BA48093173D329"
  46. "C3502AC946767326A2891971392D3F70"
  47. "89959F5D61621238655975E00E2132A0"
  48. "D5018CEEDB17731CCD88FC50151D37C0"
  49. "D4A3359506AEDC2E6109511E7703AFBB"
  50. "014642348D8568AA1A5D9868C4C7E6DF"
  51. "A756B1690C7C2608A2DC136F5997AB8F"
  52. "BB3F4D9F033C87CA6070E117F099C409"
  53. "4972ACD9D976214B7CED8E3F8B6E058E"))
  54. return FALSE;
  55. // A.3.1
  56. bash256Hash(hash, beltH(), 0);
  57. if (!hexEq(hash,
  58. "114C3DFAE373D9BCBC3602D6386F2D6A"
  59. "2059BA1BF9048DBAA5146A6CB775709D"))
  60. return FALSE;
  61. bash256Start(state);
  62. bash256StepH(beltH(), 0, state);
  63. bash256StepG(buf, state);
  64. if (!memEq(hash, buf, 32))
  65. return FALSE;
  66. // A.3.2
  67. bash256Hash(hash, beltH(), 127);
  68. if (!hexEq(hash,
  69. "3D7F4EFA00E9BA33FEED259986567DCF"
  70. "5C6D12D51057A968F14F06CC0F905961"))
  71. return FALSE;
  72. bash256Start(state);
  73. bash256StepH(beltH(), 127, state);
  74. bash256StepG(buf, state);
  75. if (!memEq(hash, buf, 32))
  76. return FALSE;
  77. // A.3.3
  78. bash256Hash(hash, beltH(), 128);
  79. if (!hexEq(hash,
  80. "D7F428311254B8B2D00F7F9EEFBD8F30"
  81. "25FA87C4BABD1BDDBE87E35B7AC80DD6"))
  82. return FALSE;
  83. // A.3.4
  84. bash256Hash(hash, beltH(), 135);
  85. if (!hexEq(hash,
  86. "1393FA1B65172F2D18946AEAE576FA1C"
  87. "F54FDD354A0CB2974A997DC4865D3100"))
  88. return FALSE;
  89. // A.3.5
  90. bash384Hash(hash, beltH(), 95);
  91. if (!hexEq(hash,
  92. "64334AF830D33F63E9ACDFA184E32522"
  93. "103FFF5C6860110A2CD369EDBC04387C"
  94. "501D8F92F749AE4DE15A8305C353D64D"))
  95. return FALSE;
  96. bash384Start(state);
  97. bash384StepH(beltH(), 95, state);
  98. bash384StepG(buf, state);
  99. if (!memEq(hash, buf, 48))
  100. return FALSE;
  101. // A.3.6
  102. bash384Hash(hash, beltH(), 96);
  103. if (!hexEq(hash,
  104. "D06EFBC16FD6C0880CBFC6A4E3D65AB1"
  105. "01FA82826934190FAABEBFBFFEDE93B2"
  106. "2B85EA72A7FB3147A133A5A8FEBD8320"))
  107. return FALSE;
  108. // A.3.7
  109. bash384Hash(hash, beltH(), 108);
  110. if (!hexEq(hash,
  111. "FF763296571E2377E71A1538070CC0DE"
  112. "88888606F32EEE6B082788D246686B00"
  113. "FC05A17405C5517699DA44B7EF5F55AB"))
  114. return FALSE;
  115. // A.3.8
  116. bash512Hash(hash, beltH(), 63);
  117. if (!hexEq(hash,
  118. "2A66C87C189C12E255239406123BDEDB"
  119. "F19955EAF0808B2AD705E249220845E2"
  120. "0F4786FB6765D0B5C48984B1B16556EF"
  121. "19EA8192B985E4233D9C09508D6339E7"))
  122. return FALSE;
  123. bash512Start(state);
  124. bash512StepH(beltH(), 63, state);
  125. bash512StepG(buf, state);
  126. if (!memEq(hash, buf, 64))
  127. return FALSE;
  128. // A.3.9
  129. bash512Hash(hash, beltH(), 64);
  130. if (!hexEq(hash,
  131. "07ABBF8580E7E5A321E9B940F667AE20"
  132. "9E2952CEF557978AE743DB086BAB4885"
  133. "B708233C3F5541DF8AAFC3611482FDE4"
  134. "98E58B3379A6622DAC2664C9C118A162"))
  135. return FALSE;
  136. // A.3.10
  137. bash512Hash(hash, beltH(), 127);
  138. if (!hexEq(hash,
  139. "526073918F97928E9D15508385F42F03"
  140. "ADE3211A23900A30131F8A1E3E1EE21C"
  141. "C09D13CFF6981101235D895746A4643F"
  142. "0AA62B0A7BC98A269E4507A257F0D4EE"))
  143. return FALSE;
  144. // A.3.11
  145. bash512Hash(hash, beltH(), 192);
  146. if (!hexEq(hash,
  147. "8724C7FF8A2A83F22E38CB9763777B96"
  148. "A70ABA3444F214C763D93CD6D19FCFDE"
  149. "6C3D3931857C4FF6CCCD49BD99852FE9"
  150. "EAA7495ECCDD96B571E0EDCF47F89768"))
  151. return FALSE;
  152. // A.4.alpha
  153. bashPrgStart(state, 256, 2, 0, 0, beltH(), 32);
  154. bashPrgAbsorb(beltH() + 32, 95, state);
  155. bashPrgRatchet(state);
  156. bashPrgSqueeze(hash, 16, state);
  157. if (!hexEq(hash,
  158. "71CC358A0D5082173DE04803F7E905CB"))
  159. return FALSE;
  160. // A.4.beta
  161. bashPrgStart(state, 128, 1, beltH() + 128, 16, hash, 16);
  162. memCopy(state1, state, bashPrg_keep());
  163. memCopy(buf, beltH() + 128 + 32, 23);
  164. bashPrgEncr(buf, 23, state);
  165. if (!hexEq(buf,
  166. "51ED3B28D345FFD1AD22815B86ECC17C"
  167. "278C8FE8920214"))
  168. return FALSE;
  169. bashPrgStart(state, 128, 1, beltH() + 128, 16, hash, 16);
  170. bashPrgDecr(buf, 23, state);
  171. if (!memEq(buf, beltH() + 128 + 32, 23))
  172. return FALSE;
  173. // A.4.gamma
  174. bashPrgRestart(beltH() + 128 + 16, 4, 0, 0, state1);
  175. memCopy(state, state1, bashPrg_keep());
  176. memCopy(buf, beltH() + 128 + 32, 23);
  177. bashPrgEncr(buf, 23, state1);
  178. if (!hexEq(buf,
  179. "28FE0998BFC010F13B260685A27AFB36"
  180. "CCF580F753521B"))
  181. return FALSE;
  182. bashPrgDecr(buf, 23, state);
  183. if (!memEq(buf, beltH() + 128 + 32, 23))
  184. return FALSE;
  185. // A.5.1
  186. bashPrgStart(state, 128, 2, 0, 0, 0, 0);
  187. bashPrgAbsorb(beltH(), 0, state);
  188. bashPrgSqueeze(hash, 32, state);
  189. if (!hexEq(hash,
  190. "36FA075EC15721F250B9A641A8CB99A3"
  191. "33A9EE7BA8586D0646CBAC3686C03DF3"))
  192. return FALSE;
  193. // A.5.2
  194. bashPrgStart(state, 128, 2, 0, 0, 0, 0);
  195. bashPrgAbsorb(beltH(), 127, state);
  196. bashPrgSqueeze(hash, 32, state);
  197. if (!hexEq(hash,
  198. "C930FF427307420DA6E4182969AA1FFC"
  199. "3310179B8A0EDB3E20BEC285B568BA17"))
  200. return FALSE;
  201. // A.5.3
  202. bashPrgStart(state, 128, 2, 0, 0, 0, 0);
  203. bashPrgAbsorb(beltH(), 128, state);
  204. bashPrgSqueeze(hash, 32, state);
  205. if (!hexEq(hash,
  206. "92AD1402C2007191F2F7CFAD6A2F8807"
  207. "BB0C50F73DFF95EF1B8AF08504D54007"))
  208. return FALSE;
  209. // A.5.4
  210. bashPrgStart(state, 128, 2, 0, 0, 0, 0);
  211. bashPrgAbsorb(beltH(), 150, state);
  212. bashPrgSqueeze(hash, 32, state);
  213. if (!hexEq(hash,
  214. "48DB61832CA1009003BC0D8BDE67893A"
  215. "9DC683C48A5BC23AC884EB4613B480A6"))
  216. return FALSE;
  217. bashPrgStart(state, 128, 2, 0, 0, 0, 0);
  218. bashPrgAbsorbStart(state);
  219. bashPrgAbsorbStep(beltH(), 0, state);
  220. bashPrgAbsorbStep(beltH(), 50, state);
  221. bashPrgAbsorbStep(beltH() + 50, 50, state);
  222. bashPrgAbsorbStep(beltH() + 100, 50, state);
  223. bashPrgSqueezeStart(state);
  224. bashPrgSqueezeStep(buf, 13, state);
  225. bashPrgSqueezeStep(buf + 13, 32 - 13, state);
  226. if (!memEq(hash, buf, 32))
  227. return FALSE;
  228. // A.5.5
  229. bashPrgStart(state, 192, 1, 0, 0, 0, 0);
  230. bashPrgAbsorb(beltH(), 143, state);
  231. bashPrgSqueeze(hash, 48, state);
  232. if (!hexEq(hash,
  233. "6166032D6713D401A6BC687CCFFF2E60"
  234. "3287143A84C78D2C62C71551E0E2FB2A"
  235. "F6B799EE33B5DECD7F62F190B1FBB052"))
  236. return FALSE;
  237. // A.5.6
  238. bashPrgStart(state, 192, 1, 0, 0, 0, 0);
  239. bashPrgAbsorb(beltH(), 144, state);
  240. bashPrgSqueeze(hash, 48, state);
  241. if (!hexEq(hash,
  242. "8D84C82ECD0AB6468CC451CFC5EEB3B2"
  243. "98DFD381D200DA69FBED5AE67D26BAD5"
  244. "C727E2652A225BF465993043039E338B"))
  245. return FALSE;
  246. // A.5.7
  247. bashPrgStart(state, 192, 1, 0, 0, 0, 0);
  248. bashPrgAbsorb(beltH(), 150, state);
  249. bashPrgSqueeze(hash, 48, state);
  250. if (!hexEq(hash,
  251. "47529F9D499AB6AB8AD72B1754C90C39"
  252. "E7DA237BEB16CDFC00FE87934F5AFC11"
  253. "01862DFA50560F062A4DAC859CC13DBC"))
  254. return FALSE;
  255. // A.6.encr
  256. bashPrgStart(state, 256, 1, beltH(), 16, beltH() + 32, 32);
  257. bashPrgAbsorb(beltH() + 64, 49, state);
  258. memSetZero(buf, 192);
  259. bashPrgEncr(buf, 192, state);
  260. if (!hexEq(buf,
  261. "690673766C3E848CAC7C05169FFB7B77"
  262. "51E52A011040E5602573FAF991044A00"
  263. "4329EEF7BED8E6875830A91854D1BD2E"
  264. "DC6FC2FF37851DBAC249DF400A0549EA"
  265. "2E0C811D499E1FF1E5E32FAE7F0532FA"
  266. "4051D0F9E300D9B1DBF119AC8CFFC48D"
  267. "D3CBF1CA0DBA5DD97481C88DF0BE4127"
  268. "85E40988B31585537948B80F5A9C49E0"
  269. "8DD684A7DCA871C380DFDC4C4DFBE61F"
  270. "50D2D0FBD24D8B9D32974A347247D001"
  271. "BAD5B168440025693967E77394DC088B"
  272. "0ECCFA8D291BA13D44F60B06E2EDB351"))
  273. return FALSE;
  274. bashPrgSqueeze(hash, 32, state);
  275. if (!hexEq(hash,
  276. "CDE5AF6EF9A14B7D0C191B869A6343ED"
  277. "6A4E9AAB4EE00A579E9E682D0EC051E3"))
  278. return FALSE;
  279. // A.6.decr
  280. bashPrgStart(state, 256, 1, beltH(), 16, beltH() + 32, 32);
  281. bashPrgAbsorb(beltH() + 64, 49, state);
  282. bashPrgDecrStart(state);
  283. for (pos = 0; pos < 192; pos += 192 / 6)
  284. bashPrgDecrStep(buf + pos, 192 / 6, state);
  285. if (!memIsZero(buf, 192))
  286. return FALSE;
  287. bashPrgSqueezeStart(state);
  288. bashPrgSqueezeStep(buf, 14, state);
  289. bashPrgSqueezeStep(buf + 14, 32 - 14, state);
  290. if (!memEq(buf, hash, 32))
  291. return FALSE;
  292. // все нормально
  293. return TRUE;
  294. }