pfok_test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 2022.06.07
  8. \license This program is released under the GNU General Public License
  9. version 3. See Copyright Notices in bee2/info.h.
  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. printf("\rq%u", (unsigned)num);
  29. }
  30. static void _on_q_silent(const word q[], size_t n, size_t num)
  31. {
  32. }
  33. /*
  34. *******************************************************************************
  35. Самотестирование
  36. Реализованы тесты из Методики НИИ ППМИ.
  37. \remark Тесты PFOK.GENP.2-4, реализованные в функции pfokTestStdParams(),
  38. выполняются очень долго и поэтому заблокированы.
  39. *******************************************************************************
  40. */
  41. bool_t pfokTestTestParams()
  42. {
  43. pfok_params params[1];
  44. pfok_params params1[1];
  45. pfok_seed seed[1];
  46. // тест PFOK.GENP.1
  47. if (pfokStdParams(params, seed, "test") != ERR_OK ||
  48. pfokGenParams(params1, seed, _on_q_silent) != ERR_OK ||
  49. pfokValParams(params1) != ERR_OK ||
  50. !memEq(params->p, params1->p, O_OF_B(params->l)) ||
  51. params->l != params1->l || params->r != params1->r)
  52. return FALSE;
  53. // все нормально
  54. return TRUE;
  55. }
  56. bool_t pfokTestStdParams()
  57. {
  58. pfok_params params[1];
  59. pfok_params params1[1];
  60. pfok_seed seed[1];
  61. // тест PFOK.GENP.2
  62. if (pfokStdParams(params, seed, "1.2.112.0.2.0.1176.2.3.3.2") != ERR_OK ||
  63. pfokValParams(params) != ERR_OK ||
  64. pfokGenParams(params1, seed, _on_q) != ERR_OK ||
  65. !memEq(params->p, params1->p, O_OF_B(params->l)) ||
  66. params->l != params1->l || params->r != params1->r)
  67. return FALSE;
  68. // тест PFOK.GENP.3
  69. if (pfokStdParams(params, seed, "1.2.112.0.2.0.1176.2.3.6.2") != ERR_OK ||
  70. pfokGenParams(params1, seed, _on_q) != ERR_OK ||
  71. !memEq(params->p, params1->p, O_OF_B(params->l)) ||
  72. params->l != params1->l || params->r != params1->r)
  73. return FALSE;
  74. // тест PFOK.GENP.4
  75. if (pfokStdParams(params, seed, "1.2.112.0.2.0.1176.2.3.10.2") != ERR_OK ||
  76. pfokGenParams(params1, seed, _on_q) != ERR_OK ||
  77. !memEq(params->p, params1->p, O_OF_B(params->l)) ||
  78. params->l != params1->l || params->r != params1->r)
  79. return FALSE;
  80. // все нормально
  81. return TRUE;
  82. }
  83. bool_t pfokTest()
  84. {
  85. pfok_params params[1];
  86. octet combo_state[128];
  87. octet ua[O_OF_B(130)];
  88. octet xa[O_OF_B(130)];
  89. octet vb[O_OF_B(638)];
  90. octet yb[O_OF_B(638)];
  91. octet key[32];
  92. // тест PFOK.GENP.1
  93. if (!pfokTestTestParams())
  94. return FALSE;
  95. // тест PFOK.GENG.1
  96. if (pfokStdParams(params, 0, "test") != ERR_OK ||
  97. pfokValParams(params) != ERR_OK ||
  98. (params->g[0] += 2) == 0 ||
  99. pfokValParams(params) == ERR_OK)
  100. return FALSE;
  101. // тест PFOK.GENG.2
  102. if (pfokStdParams(params, 0, "1.2.112.0.2.0.1176.2.3.3.2") != ERR_OK ||
  103. pfokValParams(params) != ERR_OK ||
  104. (params->g[0] += 3) == 0 ||
  105. pfokValParams(params) == ERR_OK)
  106. return FALSE;
  107. // тест PFOK.GENG.3
  108. if (pfokStdParams(params, 0, "1.2.112.0.2.0.1176.2.3.6.2") != ERR_OK ||
  109. pfokValParams(params) != ERR_OK ||
  110. (params->g[0] += 1) == 0 ||
  111. pfokValParams(params) == ERR_OK)
  112. return FALSE;
  113. // тест PFOK.GENG.4
  114. if (pfokStdParams(params, 0, "1.2.112.0.2.0.1176.2.3.10.2") != ERR_OK ||
  115. pfokValParams(params) != ERR_OK ||
  116. (params->g[0] += 1) == 0 ||
  117. pfokValParams(params) == ERR_OK)
  118. return FALSE;
  119. // загрузить параметры "test"
  120. if (pfokStdParams(params, 0, "test") != ERR_OK)
  121. return FALSE;
  122. // сгенерировать ключи
  123. ASSERT(prngCOMBO_keep() <= sizeof(combo_state));
  124. prngCOMBOStart(combo_state, utilNonce32());
  125. if (pfokGenKeypair(ua, vb, params, prngCOMBOStepR, combo_state) != ERR_OK ||
  126. pfokValPubkey(params, vb) != ERR_OK ||
  127. pfokCalcPubkey(yb, params, ua) != ERR_OK ||
  128. !memEq(vb, yb, O_OF_B(params->l)))
  129. return FALSE;
  130. // тест PFOK.ANON.1
  131. hexToRev(ua,
  132. "01"
  133. "1D4665B357DB361D106E32E353CD534B");
  134. hexToRev(vb,
  135. "0739539C2AE25B53A05C8D16A14351D8"
  136. "EA86A1DD1893E08EE4A266F970E0243F"
  137. "8DF27F738F64E99E262E337792E5DD84"
  138. "7CF2A83362C6EC3C024E47313AA49A1E"
  139. "0A2E637AD35E31EB5F034D889B666701");
  140. if (pfokValPubkey(params, vb) != ERR_OK ||
  141. pfokDH(key, params, ua, vb) != ERR_OK ||
  142. !hexEqRev(key,
  143. "777BB35E950D3080C1E896BE4172DBD0"
  144. "61423D3BFEF78F15E3F7A7F2FF7A242B"))
  145. return FALSE;
  146. // тест PFOK.ANON.2
  147. hexToRev(ua,
  148. "00"
  149. "0530110167E1443819A8662A0FAB7AC0");
  150. hexToRev(vb,
  151. "1590312CBACB7B21FC0B173DC100AC5D"
  152. "8692E04813CA2F87A5763E3F4940B10C"
  153. "DF3F2B3ECDF28BE4BEA9363B07A8A8A3"
  154. "BFDDE074DCF36D669A56931D083FC3BE"
  155. "46D02CC8EF719EF66AE47F57BEAE8E02");
  156. if (pfokValPubkey(params, vb) != ERR_OK ||
  157. pfokDH(key, params, ua, vb) != ERR_OK ||
  158. !hexEqRev(key,
  159. "46FA834B28D5E5D4183E28646AFFE806"
  160. "803E4C865CB99B1C423B0F1C78DE758D"))
  161. return FALSE;
  162. // тест PFOK.AUTH.1
  163. hexToRev(xa,
  164. "00"
  165. "78E7101B4A8F421D2AF5740D6ED27680");
  166. hexToRev(yb,
  167. "193E5E1E0839091BC7ABBDD09E8D2298"
  168. "8812D37EDEB39E077130A244888BE1A7"
  169. "53337AB5743C898D1CFC947430813448"
  170. "16AF5189A4E84D5B6EA310F72534D2E5"
  171. "E531B579CEA862EAB0251A3C20F0EC1D");
  172. hexToRev(ua,
  173. "01"
  174. "27E33C0D7595566570936FEF0AA53A24");
  175. hexToRev(vb,
  176. "0947264BEFA107E99616F347B6A05C62"
  177. "D7F5F26804D848FC4A7D81915F4546DD"
  178. "22949C07131D84F8B5A73A60ED61BC6E"
  179. "158E9B83F38C1EE6AD97F2BF771AA4FF"
  180. "B10A38298498D943995697FD0F65284C");
  181. if (pfokValPubkey(params, yb) != ERR_OK ||
  182. pfokValPubkey(params, vb) != ERR_OK ||
  183. pfokMTI(key, params, xa, ua, yb, vb) != ERR_OK ||
  184. !hexEqRev(key,
  185. "EA92D5BCEC18BB44514E096748DB3E21"
  186. "D6E7B9C97D604699BEA7D3B96C87E18B"))
  187. return FALSE;
  188. // тест PFOK.AUTH.2
  189. hexToRev(xa,
  190. "00"
  191. "05773C812D6F2A002D4E3EAC643C2CF3");
  192. hexToRev(yb,
  193. "221CBFEB62F4AA3204D349B3D57E45E4"
  194. "C9BA601483CF9DDE4DD1AE1CC2694149"
  195. "F08765C5CCAEBD44B7B7D0F1783F9FDD"
  196. "2929523E1CEF2A46FBD419C5E5E2E712"
  197. "4099B405E0B90A5FB15A56F439DA47D1");
  198. hexToRev(ua,
  199. "01"
  200. "3BB0377B3C0E55577A0D4A43627C6EC2");
  201. hexToRev(vb,
  202. "2740ECD0631257DD8124DC38CFAC3DEF"
  203. "7162503B7F7C8DEC6478408B225D4C05"
  204. "56E566AF50661CE2F46662FC66DC429A"
  205. "CCF65D95E4F90BDCD08A11957C898EE2"
  206. "C2B77231929ACE9649B2C184CC9D8104");
  207. if (pfokValPubkey(params, yb) != ERR_OK ||
  208. pfokValPubkey(params, vb) != ERR_OK ||
  209. pfokMTI(key, params, xa, ua, yb, vb) != ERR_OK ||
  210. !hexEqRev(key,
  211. "5A4C323604206C8898BF6C234F75A537"
  212. "DF75E9A249D87F1E55CBD7B40C4FDAFA"))
  213. return FALSE;
  214. // все нормально
  215. return TRUE;
  216. }