OTPGenerator.h 4.5 KB

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