bign96_test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. *******************************************************************************
  3. \file bign96_test.c
  4. \brief Tests for bign96 signatures
  5. \project bee2/test
  6. \created 2021.01.20
  7. \version 2024.01.25
  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/util.h>
  15. #include <bee2/crypto/belt.h>
  16. #include <bee2/crypto/bign96.h>
  17. #include <bee2/crypto/brng.h>
  18. /*
  19. *******************************************************************************
  20. brngCTRX: Расширение brngCTR
  21. При инициализации можно передать дополнительное слово X.
  22. *******************************************************************************
  23. */
  24. typedef struct
  25. {
  26. const octet* X; /*< дополнительное слово */
  27. size_t count; /*< размер X в октетах */
  28. size_t offset; /*< текущее смещение в X */
  29. octet state_ex[]; /*< состояние brngCTR */
  30. } brng_ctrx_st;
  31. static size_t brngCTRX_keep()
  32. {
  33. return sizeof(brng_ctrx_st) + brngCTR_keep();
  34. }
  35. static void brngCTRXStart(const octet theta[32], const octet iv[32],
  36. const void* X, size_t count, void* state)
  37. {
  38. brng_ctrx_st* s = (brng_ctrx_st*)state;
  39. ASSERT(memIsValid(s, sizeof(brng_ctrx_st)));
  40. ASSERT(count > 0);
  41. ASSERT(memIsValid(s->state_ex, brngCTR_keep()));
  42. brngCTRStart(s->state_ex, theta, iv);
  43. s->X = (const octet*)X;
  44. s->count = count;
  45. s->offset = 0;
  46. }
  47. static void brngCTRXStepR(void* buf, size_t count, void* stack)
  48. {
  49. brng_ctrx_st* s = (brng_ctrx_st*)stack;
  50. octet* buf1 = (octet*)buf;
  51. size_t count1 = count;
  52. ASSERT(memIsValid(s, sizeof(brng_ctrx_st)));
  53. // заполнить buf
  54. while (count1)
  55. if (count1 < s->count - s->offset)
  56. {
  57. memCopy(buf1, s->X + s->offset, count1);
  58. s->offset += count1;
  59. count1 = 0;
  60. }
  61. else
  62. {
  63. memCopy(buf1, s->X + s->offset, s->count - s->offset);
  64. buf1 += s->count - s->offset;
  65. count1 -= s->count - s->offset;
  66. s->offset = 0;
  67. }
  68. // сгенерировать
  69. brngCTRStepR(buf, count, s->state_ex);
  70. }
  71. /*
  72. *******************************************************************************
  73. Самотестирование
  74. *******************************************************************************
  75. */
  76. bool_t bign96Test()
  77. {
  78. bign_params params[1];
  79. octet oid_der[128];
  80. size_t oid_len;
  81. octet privkey[24];
  82. octet pubkey[48];
  83. octet hash[32];
  84. octet sig[34];
  85. octet brng_state[1024];
  86. // подготовить память
  87. if (sizeof(brng_state) < brngCTRX_keep())
  88. return FALSE;
  89. // проверить параметры
  90. if (bign96ParamsStd(params, "1.2.112.0.2.0.34.101.45.3.0") != ERR_OK ||
  91. bign96ParamsVal(params) != ERR_OK)
  92. return FALSE;
  93. // идентификатор объекта
  94. oid_len = sizeof(oid_der);
  95. if (bignOidToDER(oid_der, &oid_len, "1.2.112.0.2.0.34.101.31.81")
  96. != ERR_OK || oid_len != 11)
  97. return FALSE;
  98. // инициализировать ГПСЧ
  99. brngCTRXStart(beltH() + 128, beltH() + 128 + 64,
  100. beltH(), 8 * 32, brng_state);
  101. // управление ключами
  102. if (bign96KeypairGen(privkey, pubkey, params, brngCTRXStepR, brng_state) !=
  103. ERR_OK)
  104. return FALSE;
  105. if (!hexEq(privkey,
  106. "B1E1CDDFCF5DD7BA278390F292EEB72B"
  107. "661B79922933BFB9") ||
  108. !hexEq(pubkey,
  109. "4CED8FBBA1842BE58B4C0444F359CB14"
  110. "C6F2CE13B710F1172D2C962F53D13115"
  111. "DE14E56D9EB2628C9A884F668059EEA5"))
  112. return FALSE;
  113. if (bign96KeypairVal(params, privkey, pubkey) != ERR_OK)
  114. return FALSE;
  115. if (bign96PubkeyVal(params, pubkey) != ERR_OK)
  116. return FALSE;
  117. if (bign96PubkeyCalc(pubkey, params, privkey) != ERR_OK)
  118. return FALSE;
  119. if (!hexEq(pubkey,
  120. "4CED8FBBA1842BE58B4C0444F359CB14"
  121. "C6F2CE13B710F1172D2C962F53D13115"
  122. "DE14E56D9EB2628C9A884F668059EEA5"))
  123. return FALSE;
  124. // выработка и проверка подписи
  125. if (beltHash(hash, beltH(), 13) != ERR_OK)
  126. return FALSE;
  127. if (bign96Sign(sig, params, oid_der, oid_len, hash, privkey,
  128. brngCTRXStepR, brng_state) != ERR_OK)
  129. return FALSE;
  130. if (!hexEq(sig,
  131. "4981BBDD8721C08FA347B89BD16FDDE6"
  132. "47D310F55474C4182C1CC5BBD5642CC7"
  133. "E1B2"))
  134. return FALSE;
  135. if (bign96Verify(params, oid_der, oid_len, hash, sig, pubkey) != ERR_OK)
  136. return FALSE;
  137. sig[0] ^= 1;
  138. if (bign96Verify(params, oid_der, oid_len, hash, sig, pubkey) == ERR_OK)
  139. return FALSE;
  140. sig[0] ^= 1, pubkey[0] ^= 1;
  141. if (bign96Verify(params, oid_der, oid_len, hash, sig, pubkey) == ERR_OK)
  142. return FALSE;
  143. pubkey[0] ^= 1;
  144. // детерминированная подпись
  145. if (beltHash(hash, beltH(), 13) != ERR_OK)
  146. return FALSE;
  147. if (bign96Sign2(sig, params, oid_der, oid_len, hash, privkey, 0, 0) !=
  148. ERR_OK)
  149. return FALSE;
  150. if (!hexEq(sig,
  151. "D95DEF43F36A4C73D19399B79FB0C692"
  152. "CF44D615CCE5F45D474E7593D30E70B9"
  153. "B0C3"))
  154. return FALSE;
  155. if (bign96Verify(params, oid_der, oid_len, hash, sig, pubkey) != ERR_OK)
  156. return FALSE;
  157. sig[0] ^= 1;
  158. if (bign96Verify(params, oid_der, oid_len, hash, sig, pubkey) == ERR_OK)
  159. return FALSE;
  160. sig[0] ^= 1, pubkey[0] ^= 1;
  161. if (bign96Verify(params, oid_der, oid_len, hash, sig, pubkey) == ERR_OK)
  162. return FALSE;
  163. pubkey[0] ^= 1;
  164. // все нормально
  165. return TRUE;
  166. }