OTPGenerator.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @file OTPGenerator.c
  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. #include <security/OTPGenerator.h>
  23. int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher)
  24. {
  25. ASSERT(num_otps >= 0)
  26. ASSERT(BEncryption_cipher_valid(cipher))
  27. // init arguments
  28. g->num_otps = num_otps;
  29. // init position
  30. g->position = g->num_otps;
  31. // init calculator
  32. if (!OTPCalculator_Init(&g->calc, g->num_otps, cipher)) {
  33. goto fail0;
  34. }
  35. // init debug object
  36. DebugObject_Init(&g->d_obj);
  37. return 1;
  38. fail0:
  39. return 0;
  40. }
  41. void OTPGenerator_Free (OTPGenerator *g)
  42. {
  43. // free debug object
  44. DebugObject_Free(&g->d_obj);
  45. // free calculator
  46. OTPCalculator_Free(&g->calc);
  47. }
  48. void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv)
  49. {
  50. g->otps = OTPCalculator_Generate(&g->calc, key, iv, 1);
  51. g->position = 0;
  52. }
  53. int OTPGenerator_GetPosition (OTPGenerator *g)
  54. {
  55. return g->position;
  56. }
  57. void OTPGenerator_Reset (OTPGenerator *g)
  58. {
  59. g->position = g->num_otps;
  60. }
  61. otp_t OTPGenerator_GetOTP (OTPGenerator *g)
  62. {
  63. ASSERT(g->position < g->num_otps)
  64. return g->otps[g->position++];
  65. }