OTPGenerator.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 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. 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. 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. int OTPGenerator_GetPosition (OTPGenerator *g);
  75. /**
  76. * Sets the number of used OTPs to num_otps.
  77. *
  78. * @param g the object
  79. */
  80. 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. otp_t OTPGenerator_GetOTP (OTPGenerator *g);
  89. #endif