OTPChecker.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <system/DebugObject.h>
  34. #include <threadwork/BThreadWork.h>
  35. struct OTPChecker_entry {
  36. otp_t otp;
  37. int avail;
  38. };
  39. #include <generated/bstruct_OTPChecker.h>
  40. /**
  41. * Handler called when OTP generation for a seed is finished and new OTPs
  42. * can be recognized.
  43. *
  44. * @param user as in {@link OTPChecker_Init}
  45. */
  46. typedef void (*OTPChecker_handler) (void *user);
  47. /**
  48. * Object that checks OTPs agains known seeds.
  49. */
  50. typedef struct {
  51. BThreadWorkDispatcher *twd;
  52. OTPChecker_handler handler;
  53. void *user;
  54. int num_otps;
  55. int cipher;
  56. int num_entries;
  57. int num_tables;
  58. int tables_used;
  59. int next_table;
  60. OTPCalculator calc;
  61. oc_tablesParams tables_params;
  62. oc_tables *tables;
  63. int tw_have;
  64. BThreadWork tw;
  65. uint8_t tw_key[BENCRYPTION_MAX_KEY_SIZE];
  66. uint8_t tw_iv[BENCRYPTION_MAX_BLOCK_SIZE];
  67. DebugObject d_obj;
  68. } OTPChecker;
  69. /**
  70. * Initializes the checker.
  71. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  72. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  73. *
  74. * @param mc the object
  75. * @param num_otps number of OTPs to generate from a seed. Must be >0.
  76. * @param cipher encryption cipher for calculating the OTPs. Must be valid
  77. * according to {@link BEncryption_cipher_valid}.
  78. * @param num_tables number of tables to keep, each for one seed. Must be >0.
  79. * @param twd thread work dispatcher
  80. * @return 1 on success, 0 on failure
  81. */
  82. int OTPChecker_Init (OTPChecker *mc, int num_otps, int cipher, int num_tables, BThreadWorkDispatcher *twd) WARN_UNUSED;
  83. /**
  84. * Frees the checker.
  85. *
  86. * @param mc the object
  87. */
  88. void OTPChecker_Free (OTPChecker *mc);
  89. /**
  90. * Starts generating OTPs to recognize for a seed.
  91. * OTPs for this seed will not be recognized until the {@link OTPChecker_handler} handler is called.
  92. * If OTPs are still being generated for a previous seed, it will be forgotten.
  93. *
  94. * @param mc the object
  95. * @param seed_id seed identifier
  96. * @param key encryption key
  97. * @param iv initialization vector
  98. */
  99. void OTPChecker_AddSeed (OTPChecker *mc, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  100. /**
  101. * Removes all active seeds.
  102. *
  103. * @param mc the object
  104. */
  105. void OTPChecker_RemoveSeeds (OTPChecker *mc);
  106. /**
  107. * Checks an OTP.
  108. *
  109. * @param mc the object
  110. * @param seed_id identifer of seed whom the OTP is claimed to belong to
  111. * @param otp OTP to check
  112. * @return 1 if the OTP is valid, 0 if not
  113. */
  114. int OTPChecker_CheckOTP (OTPChecker *mc, uint16_t seed_id, otp_t otp);
  115. /**
  116. * Sets handlers.
  117. *
  118. * @param mc the object
  119. * @param handler handler to call when generation of new OTPs is complete,
  120. * after {@link OTPChecker_AddSeed} was called.
  121. * @param user argument to handler
  122. */
  123. void OTPChecker_SetHandlers (OTPChecker *mc, OTPChecker_handler handler, void *user);
  124. #endif