FFmpeg
des.c
Go to the documentation of this file.
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 "libavutil/mem_internal.h"
20 #include "libavutil/timer.h"
21 
22 #include "libavutil/des.c"
23 
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "libavutil/time.h"
30 
31 static uint64_t rand64(void)
32 {
33  uint64_t r = rand();
34  r = (r << 32) | rand();
35  return r;
36 }
37 
38 static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
39 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
40 static const DECLARE_ALIGNED(8, uint8_t, crypt_ref)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 };
41 static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
42 static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
43 static const uint8_t cbc_key[] = {
44  0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
45  0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
46  0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
47 };
48 
49 static int run_test(int cbc, int decrypt)
50 {
51  AVDES d;
52  int delay = cbc && !decrypt ? 2 : 1;
53  uint64_t res;
54  AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
55  AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
56  AV_WB64(tmp, 0x1234567890abcdefULL);
57  av_des_init(&d, cbc_key, 192, decrypt);
58  av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
59  res = AV_RB64(large_buffer[9999 + delay]);
60  if (cbc) {
61  if (decrypt)
62  return res == 0xc5cecf63ecec514cULL;
63  else
64  return res == 0xcb191f85d1ed8439ULL;
65  } else {
66  if (decrypt)
67  return res == 0x8325397644091a0aULL;
68  else
69  return res == 0xdd17e8b8b437d232ULL;
70  }
71 }
72 
73 union word_byte {
74  uint64_t word;
75  uint8_t byte[8];
76 };
77 
78 int main(void)
79 {
80  AVDES d;
81  int i;
82  union word_byte key[3], data, ct;
83  uint64_t roundkeys[16];
84  srand(av_gettime());
85  key[0].word = AV_RB64(test_key);
86  data.word = AV_RB64(plain);
87  gen_roundkeys(roundkeys, key[0].word);
88  if (des_encdec(data.word, roundkeys, 0) != AV_RB64(crypt_ref)) {
89  printf("Test 1 failed\n");
90  return 1;
91  }
92  av_des_init(&d, test_key, 64, 0);
93  av_des_crypt(&d, tmp, plain, 1, NULL, 0);
94  if (memcmp(tmp, crypt_ref, sizeof(crypt_ref))) {
95  printf("Public API decryption failed\n");
96  return 1;
97  }
98  if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
99  printf("Partial Monte-Carlo test failed\n");
100  return 1;
101  }
102  for (i = 0; i < 1000; i++) {
103  key[0].word = rand64();
104  key[1].word = rand64();
105  key[2].word = rand64();
106  data.word = rand64();
107  av_des_init(&d, key[0].byte, 192, 0);
108  av_des_crypt(&d, ct.byte, data.byte, 1, NULL, 0);
109  av_des_init(&d, key[0].byte, 192, 1);
110  av_des_crypt(&d, ct.byte, ct.byte, 1, NULL, 1);
111  if (ct.word != data.word) {
112  printf("Test 2 failed\n");
113  return 1;
114  }
115  }
116 #ifdef GENTABLES
117  printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
118  for (i = 0; i < 8; i++) {
119  int j;
120  printf(" {");
121  for (j = 0; j < 64; j++) {
122  uint32_t v = S_boxes[i][j >> 1];
123  v = j & 1 ? v >> 4 : v & 0xf;
124  v <<= 28 - 4 * i;
125  v = shuffle(v, P_shuffle, sizeof(P_shuffle));
126  printf((j & 7) == 0 ? "\n " : " ");
127  printf("0x%08X,", v);
128  }
129  printf("\n },\n");
130  }
131  printf("};\n");
132 #endif
133  return 0;
134 }
av_des_init
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, av_unused int decrypt)
Definition: des.c:280
plain
static const uint8_t plain[]
Definition: des.c:39
crypt_ref
static const uint8_t crypt_ref[]
Definition: des.c:40
r
const char * r
Definition: vf_curves.c:127
mem_internal.h
rand64
static uint64_t rand64(void)
Definition: des.c:31
av_des_crypt
void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypts / decrypts using the DES algorithm.
Definition: des.c:324
data
const char data[16]
Definition: mxf.c:149
test_key
static const uint8_t test_key[]
Definition: des.c:38
word_byte::word
uint64_t word
Definition: des.c:74
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:429
gen_roundkeys
static void gen_roundkeys(uint64_t K[16], uint64_t key)
Definition: des.c:242
key
const char * key
Definition: hwcontext_opencl.c:189
tmp
static uint8_t tmp[8]
Definition: des.c:41
NULL
#define NULL
Definition: coverity.c:32
large_buffer
static uint8_t large_buffer[10002][8]
Definition: des.c:42
word_byte
Definition: des.c:73
timer.h
time.h
des.c
run_test
static int run_test(int cbc, int decrypt)
Definition: des.c:49
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
printf
printf("static const uint8_t my_array[100] = {\n")
cbc_key
static const uint8_t cbc_key[]
Definition: des.c:43
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
main
int main(void)
Definition: des.c:78
des_encdec
static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt)
Definition: des.c:256
AVDES
Definition: des.h:33
word_byte::byte
uint8_t byte[8]
Definition: des.c:75
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
shuffle
static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len)
Definition: des.c:179
AV_RB64
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:95