OTPCalculator.c 3.9 KB

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