aes_ctr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
  43. {
  44. memcpy(a->counter, iv, sizeof(a->counter));
  45. a->block_offset = 0;
  46. }
  47. const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a)
  48. {
  49. return a->counter;
  50. }
  51. void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
  52. {
  53. uint32_t iv[2];
  54. iv[0] = av_get_random_seed();
  55. iv[1] = av_get_random_seed();
  56. av_aes_ctr_set_iv(a, (uint8_t*)iv);
  57. }
  58. int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
  59. {
  60. a->aes = av_aes_alloc();
  61. if (!a->aes) {
  62. return AVERROR(ENOMEM);
  63. }
  64. av_aes_init(a->aes, key, 128, 0);
  65. memset(a->counter, 0, sizeof(a->counter));
  66. a->block_offset = 0;
  67. return 0;
  68. }
  69. void av_aes_ctr_free(struct AVAESCTR *a)
  70. {
  71. if (a) {
  72. av_freep(&a->aes);
  73. av_free(a);
  74. }
  75. }
  76. static void av_aes_ctr_increment_be64(uint8_t* counter)
  77. {
  78. uint8_t* cur_pos;
  79. for (cur_pos = counter + 7; cur_pos >= counter; cur_pos--) {
  80. (*cur_pos)++;
  81. if (*cur_pos != 0) {
  82. break;
  83. }
  84. }
  85. }
  86. void av_aes_ctr_increment_iv(struct AVAESCTR *a)
  87. {
  88. av_aes_ctr_increment_be64(a->counter);
  89. memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
  90. a->block_offset = 0;
  91. }
  92. void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
  93. {
  94. const uint8_t* src_end = src + count;
  95. const uint8_t* cur_end_pos;
  96. uint8_t* encrypted_counter_pos;
  97. while (src < src_end) {
  98. if (a->block_offset == 0) {
  99. av_aes_crypt(a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
  100. av_aes_ctr_increment_be64(a->counter + 8);
  101. }
  102. encrypted_counter_pos = a->encrypted_counter + a->block_offset;
  103. cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset;
  104. cur_end_pos = FFMIN(cur_end_pos, src_end);
  105. a->block_offset += cur_end_pos - src;
  106. a->block_offset &= (AES_BLOCK_SIZE - 1);
  107. while (src < cur_end_pos) {
  108. *dst++ = *src++ ^ *encrypted_counter_pos++;
  109. }
  110. }
  111. }