OTPCalculator.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @file OTPCalculator.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/OTPCalculator.h>
  23. int OTPCalculator_Init (OTPCalculator *calc, int num_otps, int cipher)
  24. {
  25. ASSERT(num_otps >= 0)
  26. ASSERT(BEncryption_cipher_valid(cipher))
  27. // init arguments
  28. calc->num_otps = num_otps;
  29. calc->cipher = cipher;
  30. // remember block size
  31. calc->block_size = BEncryption_cipher_block_size(calc->cipher);
  32. // calculate number of blocks
  33. calc->num_blocks = BDIVIDE_UP(calc->num_otps * sizeof(otp_t), calc->block_size);
  34. // allocate buffer
  35. calc->data = malloc(calc->num_blocks * calc->block_size);
  36. if (!calc->data) {
  37. goto fail0;
  38. }
  39. // init debug object
  40. DebugObject_Init(&calc->d_obj);
  41. return 1;
  42. fail0:
  43. return 0;
  44. }
  45. void OTPCalculator_Free (OTPCalculator *calc)
  46. {
  47. // free debug object
  48. DebugObject_Free(&calc->d_obj);
  49. // free buffer
  50. free(calc->data);
  51. }
  52. otp_t * OTPCalculator_Generate (OTPCalculator *calc, uint8_t *key, uint8_t *iv, int shuffle)
  53. {
  54. ASSERT(shuffle == 0 || shuffle == 1)
  55. // copy IV so it can be updated
  56. uint8_t iv_work[calc->block_size];
  57. memcpy(iv_work, iv, calc->block_size);
  58. // create zero block
  59. uint8_t zero[calc->block_size];
  60. memset(zero, 0, calc->block_size);
  61. // init encryptor
  62. BEncryption encryptor;
  63. BEncryption_Init(&encryptor, BENCRYPTION_MODE_ENCRYPT, calc->cipher, key);
  64. // encrypt zero blocks
  65. for (int i = 0; i < calc->num_blocks; i++) {
  66. BEncryption_Encrypt(&encryptor, zero, (uint8_t *)calc->data + i * calc->block_size, calc->block_size, iv_work);
  67. }
  68. // free encryptor
  69. BEncryption_Free(&encryptor);
  70. // shuffle if requested
  71. if (shuffle) {
  72. int i = 0;
  73. while (i < calc->num_otps) {
  74. uint16_t ints[256];
  75. BRandom_randomize((uint8_t *)ints, sizeof(ints));
  76. for (int j = 0; j < 256 && i < calc->num_otps; j++) {
  77. int newIndex = i + (ints[j] % (calc->num_otps - i));
  78. otp_t temp = calc->data[i];
  79. calc->data[i] = calc->data[newIndex];
  80. calc->data[newIndex] = temp;
  81. i++;
  82. }
  83. }
  84. }
  85. return calc->data;
  86. }