OTPCalculator.c 3.1 KB

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