OTPGenerator.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @file OTPGenerator.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 which generates OTPs for use in sending packets.
  25. */
  26. #ifndef BADVPN_SECURITY_OTPGENERATOR_H
  27. #define BADVPN_SECURITY_OTPGENERATOR_H
  28. #include <misc/debug.h>
  29. #include <security/OTPCalculator.h>
  30. #include <system/DebugObject.h>
  31. #include <threadwork/BThreadWork.h>
  32. /**
  33. * Handler called when OTP generation for a seed is finished.
  34. * The OTP position is reset to zero before the handler is called.
  35. *
  36. * @param user as in {@link OTPGenerator_Init}
  37. */
  38. typedef void (*OTPGenerator_handler) (void *user);
  39. /**
  40. * Object which generates OTPs for use in sending packets.
  41. */
  42. typedef struct {
  43. int num_otps;
  44. int cipher;
  45. BThreadWorkDispatcher *twd;
  46. OTPGenerator_handler handler;
  47. void *user;
  48. int position;
  49. int cur_calc;
  50. OTPCalculator calc[2];
  51. otp_t *otps[2];
  52. int tw_have;
  53. BThreadWork tw;
  54. uint8_t tw_key[BENCRYPTION_MAX_KEY_SIZE];
  55. uint8_t tw_iv[BENCRYPTION_MAX_BLOCK_SIZE];
  56. DebugObject d_obj;
  57. } OTPGenerator;
  58. /**
  59. * Initializes the generator.
  60. * The object is initialized with number of used OTPs = num_otps.
  61. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  62. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  63. *
  64. * @param g the object
  65. * @param num_otps number of OTPs to generate from a seed. Must be >=0.
  66. * @param cipher encryption cipher for calculating the OTPs. Must be valid
  67. * according to {@link BEncryption_cipher_valid}.
  68. * @param twd thread work dispatcher
  69. * @param handler handler to call when generation of new OTPs is complete,
  70. * after {@link OTPGenerator_SetSeed} was called.
  71. * @param user argument to handler
  72. * @return 1 on success, 0 on failure
  73. */
  74. int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher, BThreadWorkDispatcher *twd, OTPGenerator_handler handler, void *user) WARN_UNUSED;
  75. /**
  76. * Frees the generator.
  77. *
  78. * @param g the object
  79. */
  80. void OTPGenerator_Free (OTPGenerator *g);
  81. /**
  82. * Starts generating OTPs for a seed.
  83. * When generation is complete and the new OTPs may be used, the {@link OTPGenerator_handler}
  84. * handler will be called.
  85. * If OTPs are still being generated for a previous seed, it will be forgotten.
  86. * This call by itself does not affect the OTP position; rather the position is set to zero
  87. * before the handler is called.
  88. *
  89. * @param g the object
  90. * @param key encryption key
  91. * @param iv initialization vector
  92. */
  93. void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv);
  94. /**
  95. * Returns the number of OTPs used up from the current seed so far.
  96. * If there is no seed yet, returns num_otps.
  97. *
  98. * @param g the object
  99. * @return number of used OTPs
  100. */
  101. int OTPGenerator_GetPosition (OTPGenerator *g);
  102. /**
  103. * Sets the number of used OTPs to num_otps.
  104. *
  105. * @param g the object
  106. */
  107. void OTPGenerator_Reset (OTPGenerator *g);
  108. /**
  109. * Generates a single OTP.
  110. * The number of used OTPs must be < num_otps.
  111. * The number of used OTPs is incremented.
  112. *
  113. * @param g the object
  114. */
  115. otp_t OTPGenerator_GetOTP (OTPGenerator *g);
  116. #endif