OTPGenerator.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @file OTPGenerator.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <security/OTPGenerator.h>
  31. static void work_func (OTPGenerator *g)
  32. {
  33. g->otps[!g->cur_calc] = OTPCalculator_Generate(&g->calc[!g->cur_calc], g->tw_key, g->tw_iv, 1);
  34. }
  35. static void work_done_handler (OTPGenerator *g)
  36. {
  37. ASSERT(g->tw_have)
  38. DebugObject_Access(&g->d_obj);
  39. // free work
  40. BThreadWork_Free(&g->tw);
  41. g->tw_have = 0;
  42. // use new OTPs
  43. g->cur_calc = !g->cur_calc;
  44. g->position = 0;
  45. // call handler
  46. g->handler(g->user);
  47. return;
  48. }
  49. int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher, BThreadWorkDispatcher *twd, OTPGenerator_handler handler, void *user)
  50. {
  51. ASSERT(num_otps >= 0)
  52. ASSERT(BEncryption_cipher_valid(cipher))
  53. // init arguments
  54. g->num_otps = num_otps;
  55. g->cipher = cipher;
  56. g->twd = twd;
  57. g->handler = handler;
  58. g->user = user;
  59. // init position
  60. g->position = g->num_otps;
  61. // init calculator
  62. if (!OTPCalculator_Init(&g->calc[0], g->num_otps, g->cipher)) {
  63. goto fail0;
  64. }
  65. // init calculator
  66. if (!OTPCalculator_Init(&g->calc[1], g->num_otps, g->cipher)) {
  67. goto fail1;
  68. }
  69. // set current calculator
  70. g->cur_calc = 0;
  71. // have no work
  72. g->tw_have = 0;
  73. DebugObject_Init(&g->d_obj);
  74. return 1;
  75. fail1:
  76. OTPCalculator_Free(&g->calc[0]);
  77. fail0:
  78. return 0;
  79. }
  80. void OTPGenerator_Free (OTPGenerator *g)
  81. {
  82. DebugObject_Free(&g->d_obj);
  83. // free work
  84. if (g->tw_have) {
  85. BThreadWork_Free(&g->tw);
  86. }
  87. // free calculator
  88. OTPCalculator_Free(&g->calc[1]);
  89. // free calculator
  90. OTPCalculator_Free(&g->calc[0]);
  91. }
  92. void OTPGenerator_SetSeed (OTPGenerator *g, uint8_t *key, uint8_t *iv)
  93. {
  94. DebugObject_Access(&g->d_obj);
  95. // free existing work
  96. if (g->tw_have) {
  97. BThreadWork_Free(&g->tw);
  98. }
  99. // copy key and IV
  100. memcpy(g->tw_key, key, BEncryption_cipher_key_size(g->cipher));
  101. memcpy(g->tw_iv, iv, BEncryption_cipher_block_size(g->cipher));
  102. // start work
  103. BThreadWork_Init(&g->tw, g->twd, (BThreadWork_handler_done)work_done_handler, g, (BThreadWork_work_func)work_func, g);
  104. // set have work
  105. g->tw_have = 1;
  106. }
  107. int OTPGenerator_GetPosition (OTPGenerator *g)
  108. {
  109. DebugObject_Access(&g->d_obj);
  110. return g->position;
  111. }
  112. void OTPGenerator_Reset (OTPGenerator *g)
  113. {
  114. DebugObject_Access(&g->d_obj);
  115. // free existing work
  116. if (g->tw_have) {
  117. BThreadWork_Free(&g->tw);
  118. g->tw_have = 0;
  119. }
  120. g->position = g->num_otps;
  121. }
  122. otp_t OTPGenerator_GetOTP (OTPGenerator *g)
  123. {
  124. ASSERT(g->position < g->num_otps)
  125. DebugObject_Access(&g->d_obj);
  126. return g->otps[g->cur_calc][g->position++];
  127. }