aes_ctr.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * AES-CTR cipher
  3. * Copyright (c) 2015 Eran Kornblau <erankor at gmail dot com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "common.h"
  22. #include "aes_ctr.h"
  23. #include "aes.h"
  24. #include "random_seed.h"
  25. #define AES_BLOCK_SIZE (16)
  26. typedef struct AVAESCTR {
  27. struct AVAES* aes;
  28. uint8_t counter[AES_BLOCK_SIZE];
  29. uint8_t encrypted_counter[AES_BLOCK_SIZE];
  30. int block_offset;
  31. } AVAESCTR;
  32. struct AVAESCTR *av_aes_ctr_alloc(void)
  33. {
  34. return av_mallocz(sizeof(struct AVAESCTR));
  35. }
  36. void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv)
  37. {
  38. memcpy(a->counter, iv, AES_CTR_IV_SIZE);
  39. memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
  40. a->block_offset = 0;
  41. }
  42. const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a)
  43. {
  44. return a->counter;
  45. }
  46. void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
  47. {
  48. uint32_t iv[2];
  49. iv[0] = av_get_random_seed();
  50. iv[1] = av_get_random_seed();
  51. av_aes_ctr_set_iv(a, (uint8_t*)iv);
  52. }
  53. int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
  54. {
  55. a->aes = av_aes_alloc();
  56. if (!a->aes) {
  57. return AVERROR(ENOMEM);
  58. }
  59. av_aes_init(a->aes, key, 128, 0);
  60. memset(a->counter, 0, sizeof(a->counter));
  61. a->block_offset = 0;
  62. return 0;
  63. }
  64. void av_aes_ctr_free(struct AVAESCTR *a)
  65. {
  66. if (a) {
  67. av_freep(&a->aes);
  68. av_free(a);
  69. }
  70. }
  71. static void av_aes_ctr_increment_be64(uint8_t* counter)
  72. {
  73. uint8_t* cur_pos;
  74. for (cur_pos = counter + 7; cur_pos >= counter; cur_pos--) {
  75. (*cur_pos)++;
  76. if (*cur_pos != 0) {
  77. break;
  78. }
  79. }
  80. }
  81. void av_aes_ctr_increment_iv(struct AVAESCTR *a)
  82. {
  83. av_aes_ctr_increment_be64(a->counter);
  84. memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
  85. a->block_offset = 0;
  86. }
  87. void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
  88. {
  89. const uint8_t* src_end = src + count;
  90. const uint8_t* cur_end_pos;
  91. uint8_t* encrypted_counter_pos;
  92. while (src < src_end) {
  93. if (a->block_offset == 0) {
  94. av_aes_crypt(a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
  95. av_aes_ctr_increment_be64(a->counter + 8);
  96. }
  97. encrypted_counter_pos = a->encrypted_counter + a->block_offset;
  98. cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset;
  99. cur_end_pos = FFMIN(cur_end_pos, src_end);
  100. a->block_offset += cur_end_pos - src;
  101. a->block_offset &= (AES_BLOCK_SIZE - 1);
  102. while (src < cur_end_pos) {
  103. *dst++ = *src++ ^ *encrypted_counter_pos++;
  104. }
  105. }
  106. }