bash_test.c 8.6 KB

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