dstu_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. *******************************************************************************
  3. \file dstu_test.c
  4. \brief Tests for DSTU 4145-2002 (Ukraine)
  5. \project bee2/test
  6. \created 2012.03.01
  7. \version 2024.02.29
  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/prng.h>
  15. #include <bee2/core/util.h>
  16. #include <bee2/crypto/dstu.h>
  17. /*
  18. *******************************************************************************
  19. COMBO-генератор
  20. *******************************************************************************
  21. */
  22. #define combo_rng prngCOMBOStepR
  23. /*
  24. *******************************************************************************
  25. Самотестирование
  26. -# Выполняются тесты из приложения Б к ДСТУ 4145-2002.
  27. -# Дополнительно проверяются кривые в полиномиальном базисе, заданные
  28. в приложении Г.
  29. \warning Ошибка в примере Б.1: x-координата открытого ключа должна
  30. заканчиваться на ...BDC2DA (в примере BD2DA)
  31. *******************************************************************************
  32. */
  33. bool_t dstuTest()
  34. {
  35. dstu_params params[1];
  36. octet buf[DSTU_SIZE];
  37. octet privkey[DSTU_SIZE];
  38. octet pubkey[2 * DSTU_SIZE];
  39. octet hash[32];
  40. octet sig[2 * DSTU_SIZE];
  41. size_t ld;
  42. octet state[512];
  43. // подготовить память
  44. if (sizeof(state) < prngEcho_keep() ||
  45. sizeof(state) < prngCOMBO_keep())
  46. return FALSE;
  47. // тест Б.1 [загрузка параметров]
  48. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.0") != ERR_OK ||
  49. dstuParamsVal(params) != ERR_OK)
  50. return FALSE;
  51. // тест Б.1 [генерация ключей]
  52. hexToRev(buf,
  53. "0183F60FDF7951FF47D67193F8D073790C1C"
  54. "9B5A3E");
  55. prngEchoStart(state, buf, memNonZeroSize(params->n, O_OF_B(163)));
  56. if (dstuKeypairGen(privkey, pubkey, params, prngEchoStepR,
  57. state) != ERR_OK ||
  58. !hexEqRev(privkey,
  59. "0183F60FDF7951FF47D67193F8D07379"
  60. "0C1C9B5A3E") ||
  61. !hexEqRev(pubkey,
  62. "057DE7FDE023FF929CB6AC785CE4B79C"
  63. "F64ABDC2DA") ||
  64. !hexEqRev(pubkey + O_OF_B(163),
  65. "03E85444324BCF06AD85ABF6AD7B5F34"
  66. "770532B9AA"))
  67. return FALSE;
  68. // тест Б.1 [выработка ЭЦП]
  69. ld = 512;
  70. hexToRev(hash,
  71. "003A2EB95B7180166DDF73532EEB76ED"
  72. "AEF52247FF");
  73. hexToRev(buf,
  74. "01025E40BD97DB012B7A1D79DE8E1293"
  75. "2D247F61C6");
  76. if (dstuSign(sig, params, ld, hash, 21, privkey, prngEchoStepR,
  77. state) != ERR_OK ||
  78. !hexEqRev(sig,
  79. "000000000000000000000002100D8695"
  80. "7331832B8E8C230F5BD6A332B3615ACA"
  81. "00000000000000000000000274EA2C0C"
  82. "AA014A0D80A424F59ADE7A93068D08A7"))
  83. return FALSE;
  84. // тест Б.1 [проверка ЭЦП]
  85. if (dstuVerify(params, ld, hash, 21, sig, pubkey) != ERR_OK)
  86. return FALSE;
  87. sig[0] ^= 1;
  88. if (dstuVerify(params, ld, hash, 21, sig, pubkey) == ERR_OK)
  89. return FALSE;
  90. // создать генератор COMBO
  91. prngCOMBOStart(state, utilNonce32());
  92. // максимальная длина ЭЦП
  93. ld = B_OF_O(2 * DSTU_SIZE);
  94. // сгенерировать hash
  95. combo_rng(hash, 32, state);
  96. // проверить кривую dstu_163pb
  97. if (dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  98. dstuPointVal(params, params->P) != ERR_OK ||
  99. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  100. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  101. !memEq(params->P, pubkey, 2 * O_OF_B(163)) ||
  102. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  103. state) != ERR_OK ||
  104. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  105. state) != ERR_OK ||
  106. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  107. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  108. return FALSE;
  109. // проверить кривую dstu_167pb
  110. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.1") != ERR_OK ||
  111. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  112. dstuParamsVal(params) != ERR_OK ||
  113. dstuPointVal(params, params->P) != ERR_OK ||
  114. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  115. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  116. !memEq(params->P, pubkey, 2 * O_OF_B(167)) ||
  117. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  118. state) != ERR_OK ||
  119. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  120. state) != ERR_OK ||
  121. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  122. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  123. return FALSE;
  124. // проверить кривую dstu_173pb
  125. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.2") != ERR_OK ||
  126. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  127. dstuParamsVal(params) != ERR_OK ||
  128. dstuPointVal(params, params->P) != ERR_OK ||
  129. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  130. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  131. !memEq(params->P, pubkey, 2 * O_OF_B(173)) ||
  132. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  133. state) != ERR_OK ||
  134. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  135. state) != ERR_OK ||
  136. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  137. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  138. return FALSE;
  139. // проверить кривую dstu_179pb
  140. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.3") != ERR_OK ||
  141. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  142. dstuParamsVal(params) != ERR_OK ||
  143. dstuPointVal(params, params->P) != ERR_OK ||
  144. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  145. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  146. !memEq(params->P, pubkey, 2 * O_OF_B(179)) ||
  147. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  148. state) != ERR_OK ||
  149. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  150. state) != ERR_OK ||
  151. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  152. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  153. return FALSE;
  154. // проверить кривую dstu_191pb
  155. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.4") != ERR_OK ||
  156. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  157. dstuParamsVal(params) != ERR_OK ||
  158. dstuPointVal(params, params->P) != ERR_OK ||
  159. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  160. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  161. !memEq(params->P, pubkey, 2 * O_OF_B(191)) ||
  162. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  163. state) != ERR_OK ||
  164. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  165. state) != ERR_OK ||
  166. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  167. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  168. return FALSE;
  169. // проверить кривую dstu_233pb
  170. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.5") != ERR_OK ||
  171. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  172. dstuParamsVal(params) != ERR_OK ||
  173. dstuPointVal(params, params->P) != ERR_OK ||
  174. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  175. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  176. !memEq(params->P, pubkey, 2 * O_OF_B(233)) ||
  177. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  178. state) != ERR_OK ||
  179. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  180. state) != ERR_OK ||
  181. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  182. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  183. return FALSE;
  184. // проверить кривую dstu_257pb
  185. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.6") != ERR_OK ||
  186. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  187. dstuParamsVal(params) != ERR_OK ||
  188. dstuPointVal(params, params->P) != ERR_OK ||
  189. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  190. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  191. !memEq(params->P, pubkey, 2 * O_OF_B(257)) ||
  192. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  193. state) != ERR_OK ||
  194. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  195. state) != ERR_OK ||
  196. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  197. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  198. return FALSE;
  199. // проверить кривую dstu_307pb
  200. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.7") != ERR_OK ||
  201. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  202. dstuPointVal(params, params->P) != ERR_OK ||
  203. dstuParamsVal(params) != ERR_OK ||
  204. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  205. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  206. dstuPointVal(params, pubkey) != ERR_OK ||
  207. !memEq(params->P, pubkey, 2 * O_OF_B(307)) ||
  208. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  209. state) != ERR_OK ||
  210. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  211. state) != ERR_OK ||
  212. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  213. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  214. return FALSE;
  215. // проверить кривую dstu_367pb
  216. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.8") != ERR_OK ||
  217. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  218. dstuPointVal(params, params->P) != ERR_OK ||
  219. dstuParamsVal(params) != ERR_OK ||
  220. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  221. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  222. dstuPointVal(params, pubkey) != ERR_OK ||
  223. !memEq(params->P, pubkey, 2 * O_OF_B(367)) ||
  224. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  225. state) != ERR_OK ||
  226. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  227. state) != ERR_OK ||
  228. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  229. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  230. return FALSE;
  231. // проверить кривую dstu_431pb
  232. if (dstuParamsStd(params, "1.2.804.2.1.1.1.1.3.1.1.1.2.9") != ERR_OK ||
  233. dstuPointGen(params->P, params, prngCOMBOStepR, state) != ERR_OK ||
  234. dstuPointVal(params, params->P) != ERR_OK ||
  235. dstuParamsVal(params) != ERR_OK ||
  236. dstuPointCompress(pubkey, params, params->P) != ERR_OK ||
  237. dstuPointRecover(pubkey, params, pubkey) != ERR_OK ||
  238. !memEq(params->P, pubkey, 2 * O_OF_B(431)) ||
  239. dstuKeypairGen(privkey, pubkey, params, prngCOMBOStepR,
  240. state) != ERR_OK ||
  241. dstuSign(sig, params, ld, hash, 32, privkey, prngCOMBOStepR,
  242. state) != ERR_OK ||
  243. dstuVerify(params, ld, hash, 32, sig, pubkey) != ERR_OK ||
  244. (sig[0] ^= 1, dstuVerify(params, ld, hash, 32, sig, pubkey) == ERR_OK))
  245. return FALSE;
  246. // все нормально
  247. return TRUE;
  248. }