FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/aes_ctr.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 46 51 90.2%
Functions: 9 10 90.0%
Branches: 7 10 70.0%

Line Branch Exec Source
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
22 #include <string.h>
23
24 #include "aes_ctr.h"
25 #include "aes.h"
26 #include "aes_internal.h"
27 #include "macros.h"
28 #include "mem.h"
29 #include "random_seed.h"
30
31 #define AES_BLOCK_SIZE (16)
32
33 typedef struct AVAESCTR {
34 uint8_t counter[AES_BLOCK_SIZE];
35 uint8_t encrypted_counter[AES_BLOCK_SIZE];
36 int block_offset;
37 AVAES aes;
38 } AVAESCTR;
39
40 5 struct AVAESCTR *av_aes_ctr_alloc(void)
41 {
42 5 return av_mallocz(sizeof(struct AVAESCTR));
43 }
44
45 1 void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv)
46 {
47 1 memcpy(a->counter, iv, AES_CTR_IV_SIZE);
48 1 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
49 1 a->block_offset = 0;
50 1 }
51
52 147 void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
53 {
54 147 memcpy(a->counter, iv, sizeof(a->counter));
55 147 a->block_offset = 0;
56 147 }
57
58 1 const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a)
59 {
60 1 return a->counter;
61 }
62
63 1 void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
64 {
65 uint32_t iv[2];
66
67 1 iv[0] = av_get_random_seed();
68 1 iv[1] = av_get_random_seed();
69
70 1 av_aes_ctr_set_iv(a, (uint8_t*)iv);
71 1 }
72
73 5 int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
74 {
75 5 av_aes_init(&a->aes, key, 128, 0);
76
77 5 memset(a->counter, 0, sizeof(a->counter));
78 5 a->block_offset = 0;
79
80 5 return 0;
81 }
82
83 839 void av_aes_ctr_free(struct AVAESCTR *a)
84 {
85 839 av_free(a);
86 839 }
87
88 5183 static void av_aes_ctr_increment_be64(uint8_t* counter)
89 {
90 uint8_t* cur_pos;
91
92
1/2
✓ Branch 0 taken 5183 times.
✗ Branch 1 not taken.
5183 for (cur_pos = counter + 7; cur_pos >= counter; cur_pos--) {
93 5183 (*cur_pos)++;
94
1/2
✓ Branch 0 taken 5183 times.
✗ Branch 1 not taken.
5183 if (*cur_pos != 0) {
95 5183 break;
96 }
97 }
98 5183 }
99
100 void av_aes_ctr_increment_iv(struct AVAESCTR *a)
101 {
102 av_aes_ctr_increment_be64(a->counter);
103 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
104 a->block_offset = 0;
105 }
106
107 148 void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
108 {
109 148 const uint8_t* src_end = src + count;
110 const uint8_t* cur_end_pos;
111 uint8_t* encrypted_counter_pos;
112
113
2/2
✓ Branch 0 taken 5183 times.
✓ Branch 1 taken 148 times.
5331 while (src < src_end) {
114
1/2
✓ Branch 0 taken 5183 times.
✗ Branch 1 not taken.
5183 if (a->block_offset == 0) {
115 5183 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
116
117 5183 av_aes_ctr_increment_be64(a->counter + 8);
118 }
119
120 5183 encrypted_counter_pos = a->encrypted_counter + a->block_offset;
121 5183 cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset;
122 5183 cur_end_pos = FFMIN(cur_end_pos, src_end);
123
124 5183 a->block_offset += cur_end_pos - src;
125 5183 a->block_offset &= (AES_BLOCK_SIZE - 1);
126
127
2/2
✓ Branch 0 taken 82027 times.
✓ Branch 1 taken 5183 times.
87210 while (src < cur_end_pos) {
128 82027 *dst++ = *src++ ^ *encrypted_counter_pos++;
129 }
130 }
131 148 }
132