OTPGenerator.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <string.h>
  23. #include <security/OTPGenerator.h>
  24. static void work_func (OTPGenerator *g)
  25. {
  26. g->otps[!g->cur_calc] = OTPCalculator_Generate(&g->calc[!g->cur_calc], g->tw_key, g->tw_iv, 1);
  27. }
  28. static void work_done_handler (OTPGenerator *g)
  29. {
  30. ASSERT(g->tw_have)
  31. DebugObject_Access(&g->d_obj);
  32. // free work
  33. BThreadWork_Free(&g->tw);
  34. g->tw_have = 0;
  35. // use new OTPs
  36. g->cur_calc = !g->cur_calc;
  37. g->position = 0;
  38. // call handler
  39. g->handler(g->user);
  40. return;
  41. }
  42. int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher, BThreadWorkDispatcher *twd, OTPGenerator_handler handler, void *user)
  43. {
  44. ASSERT(num_otps >= 0)
  45. ASSERT(BEncryption_cipher_valid(cipher))
  46. // init arguments
  47. g->num_otps = num_otps;
  48. g->cipher = cipher;
  49. g->twd = twd;
  50. g->handler = handler;
  51. g->user = user;
  52. // init position
  53. g->position = g->num_otps;
  54. // init calculator
  55. if (!OTPCalculator_Init(&g->calc[0], g->num_otps, g->cipher)) {
  56. goto fail0;
  57. }
  58. // init calculator
  59. if (!OTPCalculator_Init(&g->calc[1], g->num_otps, g->cipher)) {
  60. goto fail1;
  61. }
  62. // set current calculator
  63. g->cur_calc = 0;
  64. // have no work
  65. g->tw_have = 0;
  66. DebugObject_Init(&g->d_obj);
  67. return 1;
  68. fail1:
  69. OTPCalculator_Free(&g->calc[0]);
  70. fail0:
  71. return 0;
  72. }
  73. void OTPGenerator_Free (OTPGenerator *g)
  74. {
  75. DebugObject_Free(&g->d_obj);
  76. // free work
  77. if (g->tw_have) {
  78. BThreadWork_Free(&g->tw);
  79. }
  80. // free calculator
  81. OTPCalculator_Free(&g->calc[1]);
  82. // free calculator
  83. OTPCalculator_Free(&g->calc[0]);
  84. }
  85. void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv)
  86. {
  87. DebugObject_Access(&g->d_obj);
  88. // free existing work
  89. if (g->tw_have) {
  90. BThreadWork_Free(&g->tw);
  91. }
  92. // copy key and IV
  93. memcpy(g->tw_key, key, BEncryption_cipher_key_size(g->cipher));
  94. memcpy(g->tw_iv, iv, BEncryption_cipher_block_size(g->cipher));
  95. // start work
  96. BThreadWork_Init(&g->tw, g->twd, (BThreadWork_handler_done)work_done_handler, g, (BThreadWork_work_func)work_func, g);
  97. // set have work
  98. g->tw_have = 1;
  99. }
  100. int OTPGenerator_GetPosition (OTPGenerator *g)
  101. {
  102. DebugObject_Access(&g->d_obj);
  103. return g->position;
  104. }
  105. void OTPGenerator_Reset (OTPGenerator *g)
  106. {
  107. DebugObject_Access(&g->d_obj);
  108. // free existing work
  109. if (g->tw_have) {
  110. BThreadWork_Free(&g->tw);
  111. g->tw_have = 0;
  112. }
  113. g->position = g->num_otps;
  114. }
  115. otp_t OTPGenerator_GetOTP (OTPGenerator *g)
  116. {
  117. ASSERT(g->position < g->num_otps)
  118. DebugObject_Access(&g->d_obj);
  119. return g->otps[g->cur_calc][g->position++];
  120. }