FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/aes_ctr.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 33 40 82.5%
Functions: 1 1 100.0%
Branches: 14 20 70.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <string.h>
20
21 #include "libavutil/random_seed.h"
22 #include "libavutil/log.h"
23 #include "libavutil/mem_internal.h"
24 #include "libavutil/aes_ctr.h"
25
26 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {
27 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f,
28 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f
29 };
30
31 static const DECLARE_ALIGNED(8, uint8_t, encrypted)[] = {
32 0x95, 0xcd, 0x9a, 0x8a, 0x83, 0xa2, 0x1a, 0x84, 0x92, 0xed,
33 0xd6, 0xf2, 0x57, 0x2f, 0x61, 0x98, 0xbc, 0x20, 0x98, 0xee
34 };
35
36 static const DECLARE_ALIGNED(8, uint8_t, fixed_iv)[] = {
37 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef
38 };
39
40 static const DECLARE_ALIGNED(8, uint8_t, fixed_key)[] = {
41 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
42 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
43 };
44
45 static DECLARE_ALIGNED(8, uint32_t, key)[4];
46
47 static DECLARE_ALIGNED(8, uint8_t, tmp)[20];
48
49 1 int main (void)
50 {
51 1 int ret = 1;
52 struct AVAESCTR *ae, *ad;
53 const uint8_t *iv, *k;
54
55
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < 2; i++) {
56 2 ae = av_aes_ctr_alloc();
57 2 ad = av_aes_ctr_alloc();
58
59
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!ae || !ad)
60 goto ERROR;
61
62
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (i)
63 1 k = fixed_key;
64 else {
65 // Note: av_random_bytes() should be used in a real world scenario,
66 // but since that function can fail, av_get_random_seed() is used
67 // here for the purpose of this test, as its output is sufficient.
68 1 key[0] = av_get_random_seed();
69 1 key[1] = av_get_random_seed();
70 1 key[2] = av_get_random_seed();
71 1 key[3] = av_get_random_seed();
72 1 k = (uint8_t *)key;
73 }
74
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (av_aes_ctr_init(ae, k) < 0)
76 goto ERROR;
77
78
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (av_aes_ctr_init(ad, k) < 0)
79 goto ERROR;
80
81
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (i)
82 1 av_aes_ctr_set_iv(ae, fixed_iv);
83 else
84 1 av_aes_ctr_set_random_iv(ae);
85 2 iv = av_aes_ctr_get_iv(ae);
86 2 av_aes_ctr_set_full_iv(ad, iv);
87
88 2 av_aes_ctr_crypt(ae, tmp, plain, sizeof(tmp));
89
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 if (i && memcmp(tmp, encrypted, sizeof(tmp)) != 0) {
90 av_log(NULL, AV_LOG_ERROR, "test failed\n");
91 goto ERROR;
92 }
93
94 2 av_aes_ctr_crypt(ad, tmp, tmp, sizeof(tmp));
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (memcmp(tmp, plain, sizeof(tmp)) != 0){
96 av_log(NULL, AV_LOG_ERROR, "test failed\n");
97 goto ERROR;
98 }
99
100 2 av_aes_ctr_free(ae);
101 2 av_aes_ctr_free(ad);
102 2 ae = ad = NULL;
103 }
104
105 1 av_log(NULL, AV_LOG_INFO, "test passed\n");
106 1 ret = 0;
107
108 1 ERROR:
109 1 av_aes_ctr_free(ae);
110 1 av_aes_ctr_free(ad);
111 1 return ret;
112 }
113