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/lfg.h" | ||
23 | #include "libavutil/log.h" | ||
24 | #include "libavutil/mem_internal.h" | ||
25 | #include "libavutil/aes_ctr.h" | ||
26 | |||
27 | static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { | ||
28 | 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f, | ||
29 | 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f, | ||
30 | 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f, | ||
31 | 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f | ||
32 | }; | ||
33 | |||
34 | static const DECLARE_ALIGNED(8, uint8_t, encrypted)[] = { | ||
35 | 0x95, 0xcd, 0x9a, 0x8a, 0x83, 0xa2, 0x1a, 0x84, 0x92, 0xed, | ||
36 | 0xd6, 0xf2, 0x57, 0x2f, 0x61, 0x98, 0xbc, 0x20, 0x98, 0xee, | ||
37 | 0x6c, 0xed, 0x53, 0xae, 0x2f, 0xc4, 0x18, 0x7c, 0xeb, 0x62, | ||
38 | 0xbb, 0x3a, 0x71, 0x24, 0x22, 0x8c, 0xd9, 0xfa, 0xee, 0x10 | ||
39 | }; | ||
40 | |||
41 | static const DECLARE_ALIGNED(8, uint8_t, fixed_iv)[] = { | ||
42 | 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef | ||
43 | }; | ||
44 | |||
45 | static const DECLARE_ALIGNED(8, uint8_t, fixed_key)[] = { | ||
46 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, | ||
47 | 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 | ||
48 | }; | ||
49 | |||
50 | static DECLARE_ALIGNED(8, uint32_t, key)[4]; | ||
51 | |||
52 | static DECLARE_ALIGNED(8, uint8_t, tmp)[40]; | ||
53 | |||
54 | 1 | int main (void) | |
55 | { | ||
56 | 1 | int ret = 1; | |
57 | AVLFG lfg; | ||
58 | struct AVAESCTR *ae, *ad; | ||
59 | const uint8_t *iv, *k; | ||
60 | |||
61 | 1 | av_lfg_init(&lfg, av_get_random_seed()); | |
62 | |||
63 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (int i = 0; i < 2; i++) { |
64 | 2 | ae = av_aes_ctr_alloc(); | |
65 | 2 | ad = av_aes_ctr_alloc(); | |
66 | |||
67 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!ae || !ad) |
68 | ✗ | goto ERROR; | |
69 | |||
70 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (i) |
71 | 1 | k = fixed_key; | |
72 | else { | ||
73 | // Note: av_random_bytes() should be used in a real world scenario, | ||
74 | // but since that function can fail, av_get_random_seed() is used | ||
75 | // here for the purpose of this test, as its output is sufficient. | ||
76 | 1 | key[0] = av_get_random_seed(); | |
77 | 1 | key[1] = av_get_random_seed(); | |
78 | 1 | key[2] = av_get_random_seed(); | |
79 | 1 | key[3] = av_get_random_seed(); | |
80 | 1 | k = (uint8_t *)key; | |
81 | } | ||
82 | |||
83 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (av_aes_ctr_init(ae, k) < 0) |
84 | ✗ | goto ERROR; | |
85 | |||
86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (av_aes_ctr_init(ad, k) < 0) |
87 | ✗ | goto ERROR; | |
88 | |||
89 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (i) |
90 | 1 | av_aes_ctr_set_iv(ae, fixed_iv); | |
91 | else | ||
92 | 1 | av_aes_ctr_set_random_iv(ae); | |
93 | 2 | iv = av_aes_ctr_get_iv(ae); | |
94 | 2 | av_aes_ctr_set_full_iv(ad, iv); | |
95 | |||
96 | 2 | uint8_t *dst = tmp; | |
97 | 2 | const uint8_t *src = plain; | |
98 | 2 | int left = sizeof(plain); | |
99 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
10 | while (left > 0) { |
100 | 8 | int count = (av_lfg_get(&lfg) % left) + 1; | |
101 | 8 | av_aes_ctr_crypt(ae, dst, src, count); | |
102 | 8 | dst += count; | |
103 | 8 | src += count; | |
104 | 8 | left -= count; | |
105 | } | ||
106 |
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) { |
107 | ✗ | av_log(NULL, AV_LOG_ERROR, "test failed\n"); | |
108 | ✗ | goto ERROR; | |
109 | } | ||
110 | |||
111 | 2 | dst = tmp; | |
112 | 2 | left = sizeof(plain); | |
113 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
|
11 | while (left > 0) { |
114 | 9 | int count = (av_lfg_get(&lfg) % left) + 1; | |
115 | 9 | av_aes_ctr_crypt(ad, dst, dst, count); | |
116 | 9 | dst += count; | |
117 | 9 | left -= count; | |
118 | } | ||
119 | |||
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (memcmp(tmp, plain, sizeof(tmp)) != 0){ |
121 | ✗ | av_log(NULL, AV_LOG_ERROR, "test failed\n"); | |
122 | ✗ | goto ERROR; | |
123 | } | ||
124 | |||
125 | 2 | av_aes_ctr_free(ae); | |
126 | 2 | av_aes_ctr_free(ad); | |
127 | 2 | ae = ad = NULL; | |
128 | } | ||
129 | |||
130 | 1 | av_log(NULL, AV_LOG_INFO, "test passed\n"); | |
131 | 1 | ret = 0; | |
132 | |||
133 | 1 | ERROR: | |
134 | 1 | av_aes_ctr_free(ae); | |
135 | 1 | av_aes_ctr_free(ad); | |
136 | 1 | return ret; | |
137 | } | ||
138 |