pfok_test.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. *******************************************************************************
  3. \file pfok_test.c
  4. \brief Tests for Draft of RD_RB (pfok)
  5. \project bee2/test
  6. \created 2014.07.08
  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/str.h>
  16. #include <bee2/core/util.h>
  17. #include <bee2/math/ww.h>
  18. #include <bee2/math/zz.h>
  19. #include <bee2/crypto/pfok.h>
  20. /*
  21. *******************************************************************************
  22. Функция интерфейса pfok_on_q_i
  23. *******************************************************************************
  24. */
  25. #include <stdio.h>
  26. static void _on_q(const word q[], size_t n, size_t num)
  27. {
  28. if (num)
  29. printf("\rq%u", (unsigned)num);
  30. else
  31. printf("\n");
  32. }
  33. static void _on_q_silent(const word q[], size_t n, size_t num)
  34. {
  35. }
  36. /*
  37. *******************************************************************************
  38. Самотестирование
  39. Реализованы тесты из Методики НИИ ППМИ.
  40. \remark Тесты PFOK.GENP.2-4, реализованные в функции pfokTestParamsStd(),
  41. выполняются очень долго и поэтому заблокированы.
  42. *******************************************************************************
  43. */
  44. bool_t pfokTestParamsTest()
  45. {
  46. pfok_seed seed[1];
  47. pfok_seed seed1[1];
  48. pfok_params params[1];
  49. pfok_params params1[1];
  50. // проверка затравочных параметров
  51. memSetZero(seed1, sizeof(pfok_seed));
  52. if (pfokSeedVal(seed1) == ERR_OK ||
  53. pfokParamsStd(params, seed, "test") != ERR_OK ||
  54. pfokSeedVal(seed) != ERR_OK)
  55. return FALSE;
  56. seed1->l = seed->l;
  57. // доопределение затравочных параметров
  58. memCopy(seed1->zi, seed->zi, sizeof(seed->zi));
  59. if (pfokSeedAdj(seed1) != ERR_OK ||
  60. !memEq(seed, seed1, sizeof(pfok_seed)) ||
  61. (seed1->li[5] = 0, pfokSeedVal(seed1)) == ERR_OK ||
  62. (seed1->li[5] = SIZE_MAX / 5 - 1, pfokSeedVal(seed1)) == ERR_OK)
  63. return FALSE;
  64. memSetZero(seed1->li, sizeof(seed1->li));
  65. if (pfokSeedAdj(seed1) != ERR_OK ||
  66. pfokSeedVal(seed1) != ERR_OK ||
  67. !memEq(seed, seed1, sizeof(pfok_seed)))
  68. return FALSE;
  69. // тест PFOK.GENP.1
  70. if (pfokParamsStd(params, 0, "test") != ERR_OK ||
  71. pfokParamsGen(params1, seed, _on_q_silent) != ERR_OK ||
  72. pfokParamsVal(params1) != ERR_OK ||
  73. params1->l != params->l ||
  74. params1->r != params->r ||
  75. params1->n != params->n ||
  76. !memEq(params1->p, params->p, sizeof(params->p)))
  77. return FALSE;
  78. // все нормально
  79. return TRUE;
  80. }
  81. bool_t pfokTestParamsStd()
  82. {
  83. pfok_params params[1];
  84. pfok_params params1[1];
  85. pfok_seed seed[1];
  86. // тест PFOK.GENP.2
  87. if (pfokParamsStd(params, seed, "1.2.112.0.2.0.1176.2.3.3.2") != ERR_OK ||
  88. pfokParamsVal(params) != ERR_OK ||
  89. pfokParamsGen(params1, seed, _on_q) != ERR_OK ||
  90. params1->l != params->l || params1->r != params->r ||
  91. params1->n != params->n ||
  92. !memEq(params1->p, params->p, sizeof(params->p)))
  93. return FALSE;
  94. // тест PFOK.GENP.3
  95. if (pfokParamsStd(params, seed, "1.2.112.0.2.0.1176.2.3.6.2") != ERR_OK ||
  96. pfokParamsGen(params1, seed, _on_q) != ERR_OK ||
  97. params1->l != params->l || params1->r != params->r ||
  98. params1->n != params->n ||
  99. !memEq(params1->p, params->p, sizeof(params->p)))
  100. return FALSE;
  101. // тест PFOK.GENP.4
  102. if (pfokParamsStd(params, seed, "1.2.112.0.2.0.1176.2.3.10.2") != ERR_OK ||
  103. pfokParamsGen(params1, seed, _on_q) != ERR_OK ||
  104. params1->l != params->l || params1->r != params->r ||
  105. params1->n != params->n ||
  106. !memEq(params1->p, params->p, sizeof(params->p)))
  107. return FALSE;
  108. // все нормально
  109. return TRUE;
  110. }
  111. bool_t pfokTest()
  112. {
  113. pfok_params params[1];
  114. pfok_seed seed[1];
  115. octet combo_state[128];
  116. octet ua[O_OF_B(130)];
  117. octet xa[O_OF_B(130)];
  118. octet vb[O_OF_B(638)];
  119. octet yb[O_OF_B(638)];
  120. octet key[32];
  121. // подготовить память
  122. if (sizeof(combo_state) < prngCOMBO_keep())
  123. return FALSE;
  124. // тест PFOK.GENP.1
  125. if (!pfokTestParamsTest())
  126. return FALSE;
  127. // тест PFOK.GENG.1
  128. if (pfokParamsStd(params, 0, "test") != ERR_OK ||
  129. pfokParamsVal(params) != ERR_OK ||
  130. (params->g[0] += 2) == 0 ||
  131. pfokParamsVal(params) == ERR_OK)
  132. return FALSE;
  133. // тест PFOK.GENG.2
  134. if (pfokParamsStd(params, 0, "1.2.112.0.2.0.1176.2.3.3.2") != ERR_OK ||
  135. pfokParamsVal(params) != ERR_OK ||
  136. (params->g[0] += 3) == 0 ||
  137. pfokParamsVal(params) == ERR_OK)
  138. return FALSE;
  139. // тест PFOK.GENG.3
  140. if (pfokParamsStd(params, 0, "1.2.112.0.2.0.1176.2.3.6.2") != ERR_OK ||
  141. pfokParamsVal(params) != ERR_OK ||
  142. (params->g[0] += 1) == 0 ||
  143. pfokParamsVal(params) == ERR_OK)
  144. return FALSE;
  145. // тест PFOK.GENG.4
  146. if (pfokParamsStd(params, 0, "1.2.112.0.2.0.1176.2.3.10.2") != ERR_OK ||
  147. pfokParamsVal(params) != ERR_OK ||
  148. (params->g[0] += 1) == 0 ||
  149. pfokParamsVal(params) == ERR_OK)
  150. return FALSE;
  151. // загрузить параметры "test"
  152. if (pfokParamsStd(params, seed, "test") != ERR_OK)
  153. return FALSE;
  154. // сгенерировать ключи
  155. prngCOMBOStart(combo_state, utilNonce32());
  156. if (pfokKeypairGen(ua, vb, params, prngCOMBOStepR, combo_state) != ERR_OK ||
  157. pfokPubkeyVal(params, vb) != ERR_OK ||
  158. pfokPubkeyCalc(yb, params, ua) != ERR_OK ||
  159. !memEq(vb, yb, O_OF_B(params->l)))
  160. return FALSE;
  161. // тест PFOK.ANON.1
  162. hexToRev(ua,
  163. "01"
  164. "1D4665B357DB361D106E32E353CD534B");
  165. hexToRev(vb,
  166. "0739539C2AE25B53A05C8D16A14351D8"
  167. "EA86A1DD1893E08EE4A266F970E0243F"
  168. "8DF27F738F64E99E262E337792E5DD84"
  169. "7CF2A83362C6EC3C024E47313AA49A1E"
  170. "0A2E637AD35E31EB5F034D889B666701");
  171. if (pfokPubkeyVal(params, vb) != ERR_OK ||
  172. pfokDH(key, params, ua, vb) != ERR_OK ||
  173. !hexEqRev(key,
  174. "777BB35E950D3080C1E896BE4172DBD0"
  175. "61423D3BFEF78F15E3F7A7F2FF7A242B"))
  176. return FALSE;
  177. // тест PFOK.ANON.2
  178. hexToRev(ua,
  179. "00"
  180. "0530110167E1443819A8662A0FAB7AC0");
  181. hexToRev(vb,
  182. "1590312CBACB7B21FC0B173DC100AC5D"
  183. "8692E04813CA2F87A5763E3F4940B10C"
  184. "DF3F2B3ECDF28BE4BEA9363B07A8A8A3"
  185. "BFDDE074DCF36D669A56931D083FC3BE"
  186. "46D02CC8EF719EF66AE47F57BEAE8E02");
  187. if (pfokPubkeyVal(params, vb) != ERR_OK ||
  188. pfokDH(key, params, ua, vb) != ERR_OK ||
  189. !hexEqRev(key,
  190. "46FA834B28D5E5D4183E28646AFFE806"
  191. "803E4C865CB99B1C423B0F1C78DE758D"))
  192. return FALSE;
  193. // тест PFOK.AUTH.1
  194. hexToRev(xa,
  195. "00"
  196. "78E7101B4A8F421D2AF5740D6ED27680");
  197. hexToRev(yb,
  198. "193E5E1E0839091BC7ABBDD09E8D2298"
  199. "8812D37EDEB39E077130A244888BE1A7"
  200. "53337AB5743C898D1CFC947430813448"
  201. "16AF5189A4E84D5B6EA310F72534D2E5"
  202. "E531B579CEA862EAB0251A3C20F0EC1D");
  203. hexToRev(ua,
  204. "01"
  205. "27E33C0D7595566570936FEF0AA53A24");
  206. hexToRev(vb,
  207. "0947264BEFA107E99616F347B6A05C62"
  208. "D7F5F26804D848FC4A7D81915F4546DD"
  209. "22949C07131D84F8B5A73A60ED61BC6E"
  210. "158E9B83F38C1EE6AD97F2BF771AA4FF"
  211. "B10A38298498D943995697FD0F65284C");
  212. if (pfokPubkeyVal(params, yb) != ERR_OK ||
  213. pfokPubkeyVal(params, vb) != ERR_OK ||
  214. pfokMTI(key, params, xa, ua, yb, vb) != ERR_OK ||
  215. !hexEqRev(key,
  216. "EA92D5BCEC18BB44514E096748DB3E21"
  217. "D6E7B9C97D604699BEA7D3B96C87E18B"))
  218. return FALSE;
  219. // тест PFOK.AUTH.2
  220. hexToRev(xa,
  221. "00"
  222. "05773C812D6F2A002D4E3EAC643C2CF3");
  223. hexToRev(yb,
  224. "221CBFEB62F4AA3204D349B3D57E45E4"
  225. "C9BA601483CF9DDE4DD1AE1CC2694149"
  226. "F08765C5CCAEBD44B7B7D0F1783F9FDD"
  227. "2929523E1CEF2A46FBD419C5E5E2E712"
  228. "4099B405E0B90A5FB15A56F439DA47D1");
  229. hexToRev(ua,
  230. "01"
  231. "3BB0377B3C0E55577A0D4A43627C6EC2");
  232. hexToRev(vb,
  233. "2740ECD0631257DD8124DC38CFAC3DEF"
  234. "7162503B7F7C8DEC6478408B225D4C05"
  235. "56E566AF50661CE2F46662FC66DC429A"
  236. "CCF65D95E4F90BDCD08A11957C898EE2"
  237. "C2B77231929ACE9649B2C184CC9D8104");
  238. if (pfokPubkeyVal(params, yb) != ERR_OK ||
  239. pfokPubkeyVal(params, vb) != ERR_OK ||
  240. pfokMTI(key, params, xa, ua, yb, vb) != ERR_OK ||
  241. !hexEqRev(key,
  242. "5A4C323604206C8898BF6C234F75A537"
  243. "DF75E9A249D87F1E55CBD7B40C4FDAFA"))
  244. return FALSE;
  245. // все нормально
  246. return TRUE;
  247. }