botp_test.c 4.5 KB

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