OTPCalculator.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @file OTPCalculator.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 calculates OTPs.
  25. */
  26. #ifndef BADVPN_SECURITY_OTPCALCULATOR_H
  27. #define BADVPN_SECURITY_OTPCALCULATOR_H
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <misc/balign.h>
  31. #include <misc/debug.h>
  32. #include <security/BRandom.h>
  33. #include <security/BEncryption.h>
  34. #include <base/DebugObject.h>
  35. /**
  36. * Type for an OTP.
  37. */
  38. typedef uint32_t otp_t;
  39. /**
  40. * Object that calculates OTPs.
  41. */
  42. typedef struct {
  43. DebugObject d_obj;
  44. int num_otps;
  45. int cipher;
  46. int block_size;
  47. int num_blocks;
  48. otp_t *data;
  49. } OTPCalculator;
  50. /**
  51. * Initializes the calculator.
  52. * {@link BSecurity_GlobalInitThreadSafe} must have been done if this object
  53. * will be used from a non-main thread.
  54. *
  55. * @param calc the object
  56. * @param num_otps number of OTPs to generate from a seed. Must be >=0.
  57. * @param cipher encryption cipher for calculating the OTPs. Must be valid
  58. * according to {@link BEncryption_cipher_valid}.
  59. * @return 1 on success, 0 on failure
  60. */
  61. int OTPCalculator_Init (OTPCalculator *calc, int num_otps, int cipher) WARN_UNUSED;
  62. /**
  63. * Frees the calculator.
  64. *
  65. * @param calc the object
  66. */
  67. void OTPCalculator_Free (OTPCalculator *calc);
  68. /**
  69. * Generates OTPs from the given key and IV.
  70. *
  71. * @param calc the object
  72. * @param key encryption key
  73. * @param iv initialization vector
  74. * @param shuffle whether to shuffle the OTPs. Must be 1 or 0.
  75. * @return pointer to an array of 32-bit OPTs. Constains as many OTPs as was specified
  76. * in {@link OTPCalculator_Init}. Valid until the next generation or
  77. * until the object is freed.
  78. */
  79. otp_t * OTPCalculator_Generate (OTPCalculator *calc, uint8_t *key, uint8_t *iv, int shuffle);
  80. #endif