OTPGenerator.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  32. * Object which generates OTPs for use in sending packets.
  33. */
  34. typedef struct {
  35. DebugObject d_obj;
  36. int num_otps;
  37. int position;
  38. OTPCalculator calc;
  39. otp_t *otps;
  40. } OTPGenerator;
  41. /**
  42. * Initializes the generator.
  43. * The object is initialized with number of used OTPs = num_otps.
  44. *
  45. * @param g the object
  46. * @param num_otps number of OTPs to generate from a seed. Must be >=0.
  47. * @param cipher encryption cipher for calculating the OTPs. Must be valid
  48. * according to {@link BEncryption_cipher_valid}.
  49. * @return 1 on success, 0 on failure
  50. */
  51. static int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher) WARN_UNUSED;
  52. /**
  53. * Frees the generator.
  54. *
  55. * @param g the object
  56. */
  57. static void OTPGenerator_Free (OTPGenerator *g);
  58. /**
  59. * Assigns a seed to use for generating OTPs.
  60. * Sets the number of used OTPs to 0.
  61. *
  62. * @param g the object
  63. * @param key encryption key
  64. * @param iv initialization vector
  65. */
  66. static void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv);
  67. /**
  68. * Returns the number of OTPs used up from the current seed so far.
  69. * If there is no seed yet, returns num_otps.
  70. *
  71. * @param g the object
  72. * @return number of used OTPs
  73. */
  74. static int OTPGenerator_GetPosition (OTPGenerator *g);
  75. /**
  76. * Sets the number of used OTPs to num_otps.
  77. *
  78. * @param g the object
  79. */
  80. static void OTPGenerator_Reset (OTPGenerator *g);
  81. /**
  82. * Generates a single OTP.
  83. * The number of used OTPs must be < num_otps.
  84. * The number of used OTPs is incremented.
  85. *
  86. * @param g the object
  87. */
  88. static otp_t OTPGenerator_GetOTP (OTPGenerator *g);
  89. int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher)
  90. {
  91. ASSERT(num_otps >= 0)
  92. ASSERT(BEncryption_cipher_valid(cipher))
  93. // init arguments
  94. g->num_otps = num_otps;
  95. // init position
  96. g->position = g->num_otps;
  97. // init calculator
  98. if (!OTPCalculator_Init(&g->calc, g->num_otps, cipher)) {
  99. goto fail0;
  100. }
  101. // init debug object
  102. DebugObject_Init(&g->d_obj);
  103. return 1;
  104. fail0:
  105. return 0;
  106. }
  107. void OTPGenerator_Free (OTPGenerator *g)
  108. {
  109. // free debug object
  110. DebugObject_Free(&g->d_obj);
  111. // free calculator
  112. OTPCalculator_Free(&g->calc);
  113. }
  114. void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv)
  115. {
  116. g->otps = OTPCalculator_Generate(&g->calc, key, iv, 1);
  117. g->position = 0;
  118. }
  119. int OTPGenerator_GetPosition (OTPGenerator *g)
  120. {
  121. return g->position;
  122. }
  123. void OTPGenerator_Reset (OTPGenerator *g)
  124. {
  125. g->position = g->num_otps;
  126. }
  127. otp_t OTPGenerator_GetOTP (OTPGenerator *g)
  128. {
  129. ASSERT(g->position < g->num_otps)
  130. return g->otps[g->position++];
  131. }
  132. #endif