OTPChecker.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file OTPChecker.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object that checks OTPs agains known seeds.
  32. */
  33. #ifndef BADVPN_SECURITY_OTPCHECKER_H
  34. #define BADVPN_SECURITY_OTPCHECKER_H
  35. #include <stdint.h>
  36. #include <misc/balign.h>
  37. #include <misc/debug.h>
  38. #include <misc/modadd.h>
  39. #include <security/OTPCalculator.h>
  40. #include <base/DebugObject.h>
  41. #include <threadwork/BThreadWork.h>
  42. struct OTPChecker_entry {
  43. otp_t otp;
  44. int avail;
  45. };
  46. struct OTPChecker_table {
  47. uint16_t id;
  48. struct OTPChecker_entry *entries;
  49. };
  50. /**
  51. * Handler called when OTP generation for a seed is finished and new OTPs
  52. * can be recognized.
  53. *
  54. * @param user as in {@link OTPChecker_Init}
  55. */
  56. typedef void (*OTPChecker_handler) (void *user);
  57. /**
  58. * Object that checks OTPs agains known seeds.
  59. */
  60. typedef struct {
  61. BThreadWorkDispatcher *twd;
  62. OTPChecker_handler handler;
  63. void *user;
  64. int num_otps;
  65. int cipher;
  66. int num_entries;
  67. int num_tables;
  68. int tables_used;
  69. int next_table;
  70. OTPCalculator calc;
  71. struct OTPChecker_table *tables;
  72. struct OTPChecker_entry *entries;
  73. int tw_have;
  74. BThreadWork tw;
  75. uint8_t tw_key[BENCRYPTION_MAX_KEY_SIZE];
  76. uint8_t tw_iv[BENCRYPTION_MAX_BLOCK_SIZE];
  77. DebugObject d_obj;
  78. } OTPChecker;
  79. /**
  80. * Initializes the checker.
  81. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  82. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  83. *
  84. * @param mc the object
  85. * @param num_otps number of OTPs to generate from a seed. Must be >0.
  86. * @param cipher encryption cipher for calculating the OTPs. Must be valid
  87. * according to {@link BEncryption_cipher_valid}.
  88. * @param num_tables number of tables to keep, each for one seed. Must be >0.
  89. * @param twd thread work dispatcher
  90. * @return 1 on success, 0 on failure
  91. */
  92. int OTPChecker_Init (OTPChecker *mc, int num_otps, int cipher, int num_tables, BThreadWorkDispatcher *twd) WARN_UNUSED;
  93. /**
  94. * Frees the checker.
  95. *
  96. * @param mc the object
  97. */
  98. void OTPChecker_Free (OTPChecker *mc);
  99. /**
  100. * Starts generating OTPs to recognize for a seed.
  101. * OTPs for this seed will not be recognized until the {@link OTPChecker_handler} handler is called.
  102. * If OTPs are still being generated for a previous seed, it will be forgotten.
  103. *
  104. * @param mc the object
  105. * @param seed_id seed identifier
  106. * @param key encryption key
  107. * @param iv initialization vector
  108. */
  109. void OTPChecker_AddSeed (OTPChecker *mc, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  110. /**
  111. * Removes all active seeds.
  112. *
  113. * @param mc the object
  114. */
  115. void OTPChecker_RemoveSeeds (OTPChecker *mc);
  116. /**
  117. * Checks an OTP.
  118. *
  119. * @param mc the object
  120. * @param seed_id identifer of seed whom the OTP is claimed to belong to
  121. * @param otp OTP to check
  122. * @return 1 if the OTP is valid, 0 if not
  123. */
  124. int OTPChecker_CheckOTP (OTPChecker *mc, uint16_t seed_id, otp_t otp);
  125. /**
  126. * Sets handlers.
  127. *
  128. * @param mc the object
  129. * @param handler handler to call when generation of new OTPs is complete,
  130. * after {@link OTPChecker_AddSeed} was called.
  131. * @param user argument to handler
  132. */
  133. void OTPChecker_SetHandlers (OTPChecker *mc, OTPChecker_handler handler, void *user);
  134. #endif