botp_test.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. *******************************************************************************
  3. \file botp_test.c
  4. \brief Tests for STB 34.101.47/botp
  5. \project bee2/test
  6. \author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
  7. \created 2015.11.06
  8. \version 2019.08.30
  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/belt.h>
  18. #include <bee2/crypto/botp.h>
  19. /*
  20. *******************************************************************************
  21. Самотестирование
  22. Выполняются тесты из приложения А к СТБ 34.101.47-2016.
  23. *******************************************************************************
  24. */
  25. bool_t botpTest()
  26. {
  27. octet ctr[8];
  28. char otp[16], otp1[16], otp2[16], otp3[16];
  29. const char suite[] = "OCRA-1:HOTP-HBELT-8:C-QN08-PHBELT-S064-T1M";
  30. char q[32];
  31. octet p[32];
  32. char p_str[72];
  33. char s_str[136];
  34. tm_time_t t;
  35. octet state[2048];
  36. // создать стек
  37. ASSERT(sizeof(state) >= botpHOTP_keep());
  38. ASSERT(sizeof(state) >= botpTOTP_keep());
  39. ASSERT(sizeof(state) >= botpOCRA_keep());
  40. // HOTP.1
  41. memCopy(ctr, beltH() + 192, 8);
  42. botpHOTPStart(state, 8, beltH() + 128, 32);
  43. botpHOTPStepS(state, ctr);
  44. botpHOTPStepG(ctr, state);
  45. botpHOTPStepR(otp, state);
  46. if (!strEq(otp, "21157984"))
  47. return FALSE;
  48. botpHOTPStepS(state, ctr);
  49. if (!botpHOTPStepV(otp, state))
  50. return FALSE;
  51. botpHOTPRand(otp1, 8, beltH() + 128, 32, ctr);
  52. if (!strEq(otp1, otp) ||
  53. botpHOTPVerify(otp1, beltH() + 128, 32, ctr) != ERR_OK)
  54. return FALSE;
  55. // HOTP.2
  56. botpHOTPStepR(otp2, state);
  57. if (!strEq(otp2, "17877985"))
  58. return FALSE;
  59. // HOTP.3
  60. botpHOTPStepR(otp3, state);
  61. if (!strEq(otp3, "26078636"))
  62. return FALSE;
  63. botpHOTPStepG(ctr, state);
  64. // TOTP.1
  65. t = 1449165288;
  66. ASSERT(t != TIME_ERR);
  67. botpTOTPStart(state, 8, beltH() + 128, 32);
  68. botpTOTPStepR(otp, t / 60, state);
  69. if (!strEq(otp, "97660664"))
  70. return FALSE;
  71. if (!botpTOTPStepV(otp, t / 60, state))
  72. return FALSE;
  73. botpTOTPRand(otp, 8, beltH() + 128, 32, t / 60);
  74. if (!strEq(otp, "97660664") ||
  75. botpTOTPVerify(otp, beltH() + 128, 32, t / 60) != ERR_OK)
  76. return FALSE;
  77. // TOTP.2
  78. t /= 60, ++t, t *= 60;
  79. botpTOTPStart(state, 8, beltH() + 128, 32);
  80. botpTOTPStepR(otp, t / 60, state);
  81. if (!strEq(otp, "94431522"))
  82. return FALSE;
  83. // TOTP.3
  84. t /= 60, t += 2, t *= 60, --t;
  85. botpTOTPStart(state, 8, beltH() + 128, 32);
  86. botpTOTPStepR(otp, t / 60, state);
  87. if (!strEq(otp, "55973851"))
  88. return FALSE;
  89. // OCRA.format
  90. if (botpOCRAStart(state, "OCRA-:HOTP-HBELT-6:C-QN08", beltH(), 32) ||
  91. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-3:C-QN08", beltH(), 32) ||
  92. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-6-QN08", beltH(), 32) ||
  93. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-8:C-QA65", beltH(), 32) ||
  94. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-8:C-QN08-", beltH(), 32) ||
  95. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-8:C-QN08-PSHA", beltH(), 32) ||
  96. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-8:QN08-SA13", beltH(), 32) ||
  97. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-8:QN08-T1N", beltH(), 32) ||
  98. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-8:QN08-T61S", beltH(), 32) ||
  99. botpOCRAStart(state, "OCRA-1:HOTP-HBELT-8:QN08-T51H", beltH(), 32) ||
  100. !botpOCRAStart(state, "OCRA-1:HOTP-HBELT-9:QN08-T8S", beltH(), 32))
  101. return FALSE;
  102. // OCRA.1
  103. beltHash(p, beltH(), 13);
  104. hexFrom(p_str, p, 32);
  105. hexFrom(s_str, beltH(), 64);
  106. botpOCRAStart(state, suite, beltH() + 128, 32);
  107. botpOCRAStepS(state, ctr, p, beltH());
  108. botpOCRAStepG(ctr, state);
  109. t /= 60;
  110. strCopy(q, otp1);
  111. botpOCRAStepR(otp, (const octet*)q, strLen(q), t += 3, state);
  112. if (!strEq(otp, "85199085"))
  113. return FALSE;
  114. botpOCRAStepS(state, ctr, p, beltH());
  115. if (!botpOCRAStepV(otp, (const octet*)q, strLen(q), t, state))
  116. return FALSE;
  117. botpOCRARand(otp, suite, beltH() + 128, 32, (const octet*)q, strLen(q),
  118. ctr, p, beltH(), t);
  119. if (!strEq(otp, "85199085"))
  120. return FALSE;
  121. if (botpOCRAVerify(otp, suite, beltH() + 128, 32, (const octet*)q, strLen(q),
  122. ctr, p, beltH(), t) != ERR_OK)
  123. return FALSE;
  124. // OCRA.2
  125. strCopy(q, otp2);
  126. strCopy(q + strLen(q), otp3);
  127. botpOCRAStepR(otp, (const octet*)q, strLen(q), t += 10, state);
  128. if (!strEq(otp, "89873725"))
  129. return FALSE;
  130. // OCRA.3
  131. strCopy(q, otp3);
  132. strCopy(q + strLen(q), otp2);
  133. botpOCRAStepR(otp, (const octet*)q, strLen(q), ++t, state);
  134. if (!strEq(otp, "21318915"))
  135. return FALSE;
  136. // все нормально
  137. return TRUE;
  138. }