OTPChecker.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * @param handler handler to call when generation of new OTPs is complete,
  81. * after {@link OTPChecker_AddSeed} was called.
  82. * @param user argument to handler
  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, OTPChecker_handler handler, void *user) 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. #endif