OTPChecker.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file OTPChecker.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Object that checks OTPs agains known seeds.
  25. */
  26. #ifndef BADVPN_SECURITY_OTPCHECKER_H
  27. #define BADVPN_SECURITY_OTPCHECKER_H
  28. #include <stdint.h>
  29. #include <misc/balign.h>
  30. #include <misc/debug.h>
  31. #include <misc/modadd.h>
  32. #include <security/OTPCalculator.h>
  33. #include <base/DebugObject.h>
  34. #include <threadwork/BThreadWork.h>
  35. struct OTPChecker_entry {
  36. otp_t otp;
  37. int avail;
  38. };
  39. struct OTPChecker_table {
  40. uint16_t id;
  41. struct OTPChecker_entry *entries;
  42. };
  43. /**
  44. * Handler called when OTP generation for a seed is finished and new OTPs
  45. * can be recognized.
  46. *
  47. * @param user as in {@link OTPChecker_Init}
  48. */
  49. typedef void (*OTPChecker_handler) (void *user);
  50. /**
  51. * Object that checks OTPs agains known seeds.
  52. */
  53. typedef struct {
  54. BThreadWorkDispatcher *twd;
  55. OTPChecker_handler handler;
  56. void *user;
  57. int num_otps;
  58. int cipher;
  59. int num_entries;
  60. int num_tables;
  61. int tables_used;
  62. int next_table;
  63. OTPCalculator calc;
  64. struct OTPChecker_table *tables;
  65. struct OTPChecker_entry *entries;
  66. int tw_have;
  67. BThreadWork tw;
  68. uint8_t tw_key[BENCRYPTION_MAX_KEY_SIZE];
  69. uint8_t tw_iv[BENCRYPTION_MAX_BLOCK_SIZE];
  70. DebugObject d_obj;
  71. } OTPChecker;
  72. /**
  73. * Initializes the checker.
  74. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  75. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  76. *
  77. * @param mc the object
  78. * @param num_otps number of OTPs to generate from a seed. Must be >0.
  79. * @param cipher encryption cipher for calculating the OTPs. Must be valid
  80. * according to {@link BEncryption_cipher_valid}.
  81. * @param num_tables number of tables to keep, each for one seed. Must be >0.
  82. * @param twd thread work dispatcher
  83. * @return 1 on success, 0 on failure
  84. */
  85. int OTPChecker_Init (OTPChecker *mc, int num_otps, int cipher, int num_tables, BThreadWorkDispatcher *twd) WARN_UNUSED;
  86. /**
  87. * Frees the checker.
  88. *
  89. * @param mc the object
  90. */
  91. void OTPChecker_Free (OTPChecker *mc);
  92. /**
  93. * Starts generating OTPs to recognize for a seed.
  94. * OTPs for this seed will not be recognized until the {@link OTPChecker_handler} handler is called.
  95. * If OTPs are still being generated for a previous seed, it will be forgotten.
  96. *
  97. * @param mc the object
  98. * @param seed_id seed identifier
  99. * @param key encryption key
  100. * @param iv initialization vector
  101. */
  102. void OTPChecker_AddSeed (OTPChecker *mc, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  103. /**
  104. * Removes all active seeds.
  105. *
  106. * @param mc the object
  107. */
  108. void OTPChecker_RemoveSeeds (OTPChecker *mc);
  109. /**
  110. * Checks an OTP.
  111. *
  112. * @param mc the object
  113. * @param seed_id identifer of seed whom the OTP is claimed to belong to
  114. * @param otp OTP to check
  115. * @return 1 if the OTP is valid, 0 if not
  116. */
  117. int OTPChecker_CheckOTP (OTPChecker *mc, uint16_t seed_id, otp_t otp);
  118. /**
  119. * Sets handlers.
  120. *
  121. * @param mc the object
  122. * @param handler handler to call when generation of new OTPs is complete,
  123. * after {@link OTPChecker_AddSeed} was called.
  124. * @param user argument to handler
  125. */
  126. void OTPChecker_SetHandlers (OTPChecker *mc, OTPChecker_handler handler, void *user);
  127. #endif