FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/cast5.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 32 41 78.0%
Functions: 1 1 100.0%
Branches: 21 26 80.8%

Line Branch Exec Source
1 /*
2 * An implementation of the CAST128 algorithm as mentioned in RFC2144
3 * Copyright (c) 2014 Supraja Meedinti
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 <stddef.h>
23 #include <string.h>
24
25 #include "libavutil/cast5.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mem.h"
28
29 1 int main(int argc, char** argv)
30 {
31
32 static const uint8_t Key[3][16] = {
33 {0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9a},
34 {0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x23, 0x45},
35 {0x01, 0x23, 0x45, 0x67, 0x12}
36 };
37 static const uint8_t rpt[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
38 static const uint8_t rct[3][8] = {
39 {0x23, 0x8b, 0x4f, 0xe5, 0x84, 0x7e, 0x44, 0xb2},
40 {0xeb, 0x6a, 0x71, 0x1a, 0x2c, 0x02, 0x27, 0x1b},
41 {0x7a, 0xc8, 0x16, 0xd1, 0x6e, 0x9b, 0x30, 0x2e}
42 };
43 static const uint8_t rct2[2][16] = {
44 {0xee, 0xa9, 0xd0, 0xa2, 0x49, 0xfd, 0x3b, 0xa6, 0xb3, 0x43, 0x6f, 0xb8, 0x9d, 0x6d, 0xca, 0x92},
45 {0xb2, 0xc9, 0x5e, 0xb0, 0x0c, 0x31, 0xad, 0x71, 0x80, 0xac, 0x05, 0xb8, 0xe8, 0x3d, 0x69, 0x6e}
46 };
47 static const uint8_t iv[8] = {0xee, 0xa9, 0xd0, 0xa2, 0x49, 0xfd, 0x3b, 0xa6};
48 static uint8_t rpt2[2][16];
49 1 int i, j, err = 0;
50 static const int key_bits[3] = {128, 80, 40};
51 uint8_t temp[8];
52 struct AVCAST5 *cs;
53 1 cs = av_cast5_alloc();
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cs)
55 return 1;
56
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (j = 0; j < 3; j++){
57
58 3 av_cast5_init(cs, Key[j], key_bits[j]);
59 3 av_cast5_crypt(cs, temp, rpt, 1, 0);
60
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 for (i = 0;i < 8; i++){
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (rct[j][i] != temp[i]){
62 av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]);
63 err = 1;
64 }
65 }
66
67 3 av_cast5_crypt(cs, temp, rct[j], 1, 1);
68
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 for (i =0; i < 8; i++) {
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (rpt[i] != temp[i]) {
70 av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
71 err = 1;
72 }
73 }
74 }
75 1 memcpy(rpt2[0], Key[0], 16);
76 1 memcpy(rpt2[1], Key[0], 16);
77
2/2
✓ Branch 0 taken 1000000 times.
✓ Branch 1 taken 1 times.
1000001 for (i = 0; i < 1000000; i++){
78 1000000 av_cast5_init(cs, rpt2[1], 128);
79 1000000 av_cast5_crypt(cs, rpt2[0], rpt2[0], 2, 0);
80 1000000 av_cast5_init(cs, rpt2[0], 128);
81 1000000 av_cast5_crypt(cs, rpt2[1], rpt2[1], 2, 0);
82 }
83
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (j = 0; j < 2; j++) {
84
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
34 for (i = 0; i < 16; i++) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (rct2[j][i] != rpt2[j][i]) {
86 av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct2[j][i], rpt2[j][i]);
87 err = 1;
88 }
89 }
90 }
91
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (j = 0; j < 3; j++) {
92
93 3 av_cast5_init(cs, Key[j], key_bits[j]);
94 3 memcpy(temp, iv, 8);
95 3 av_cast5_crypt2(cs, rpt2[0], rct2[0], 2, temp, 0);
96 3 memcpy(temp, iv, 8);
97 3 av_cast5_crypt2(cs, rpt2[0], rpt2[0], 2, temp, 1);
98
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (i = 0; i < 16; i++) {
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (rct2[0][i] != rpt2[0][i]) {
100 av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct2[0][i], rpt2[0][i]);
101 err = 1;
102 }
103 }
104 }
105 1 av_free(cs);
106 1 return err;
107 }
108