FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/des.c"
20 
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "libavutil/time.h"
27 
28 static uint64_t rand64(void)
29 {
30  uint64_t r = rand();
31  r = (r << 32) | rand();
32  return r;
33 }
34 
35 static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
36 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
37 static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 };
38 static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
39 static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
40 static const uint8_t cbc_key[] = {
41  0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
42  0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
43  0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
44 };
45 
46 static int run_test(int cbc, int decrypt)
47 {
48  AVDES d;
49  int delay = cbc && !decrypt ? 2 : 1;
50  uint64_t res;
51  AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
52  AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
53  AV_WB64(tmp, 0x1234567890abcdefULL);
54  av_des_init(&d, cbc_key, 192, decrypt);
55  av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
56  res = AV_RB64(large_buffer[9999 + delay]);
57  if (cbc) {
58  if (decrypt)
59  return res == 0xc5cecf63ecec514cULL;
60  else
61  return res == 0xcb191f85d1ed8439ULL;
62  } else {
63  if (decrypt)
64  return res == 0x8325397644091a0aULL;
65  else
66  return res == 0xdd17e8b8b437d232ULL;
67  }
68 }
69 
70 int main(void)
71 {
72  AVDES d;
73  int i;
74  uint64_t key[3];
75  uint64_t data;
76  uint64_t ct;
77  uint64_t roundkeys[16];
78  srand(av_gettime());
79  key[0] = AV_RB64(test_key);
80  data = AV_RB64(plain);
81  gen_roundkeys(roundkeys, key[0]);
82  if (des_encdec(data, roundkeys, 0) != AV_RB64(crypt)) {
83  printf("Test 1 failed\n");
84  return 1;
85  }
86  av_des_init(&d, test_key, 64, 0);
87  av_des_crypt(&d, tmp, plain, 1, NULL, 0);
88  if (memcmp(tmp, crypt, sizeof(crypt))) {
89  printf("Public API decryption failed\n");
90  return 1;
91  }
92  if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
93  printf("Partial Monte-Carlo test failed\n");
94  return 1;
95  }
96  for (i = 0; i < 1000; i++) {
97  key[0] = rand64();
98  key[1] = rand64();
99  key[2] = rand64();
100  data = rand64();
101  av_des_init(&d, (uint8_t *) key, 192, 0);
102  av_des_crypt(&d, (uint8_t *) &ct, (uint8_t *) &data, 1, NULL, 0);
103  av_des_init(&d, (uint8_t *) key, 192, 1);
104  av_des_crypt(&d, (uint8_t *) &ct, (uint8_t *) &ct, 1, NULL, 1);
105  if (ct != data) {
106  printf("Test 2 failed\n");
107  return 1;
108  }
109  }
110 #ifdef GENTABLES
111  printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
112  for (i = 0; i < 8; i++) {
113  int j;
114  printf(" {");
115  for (j = 0; j < 64; j++) {
116  uint32_t v = S_boxes[i][j >> 1];
117  v = j & 1 ? v >> 4 : v & 0xf;
118  v <<= 28 - 4 * i;
119  v = shuffle(v, P_shuffle, sizeof(P_shuffle));
120  printf((j & 7) == 0 ? "\n " : " ");
121  printf("0x%08X,", v);
122  }
123  printf("\n },\n");
124  }
125  printf("};\n");
126 #endif
127  return 0;
128 }
#define NULL
Definition: coverity.c:32
static void gen_roundkeys(uint64_t K[16], uint64_t key)
Definition: des.c:240
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:322
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static const uint8_t cbc_key[]
Definition: des.c:40
static const uint8_t plain[]
Definition: des.c:36
static const uint8_t crypt[]
Definition: des.c:37
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
static uint64_t rand64(void)
Definition: des.c:28
uint8_t
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:101
int main(void)
Definition: des.c:70
const char * r
Definition: vf_curves.c:111
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, av_unused int decrypt)
Definition: des.c:278
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
static int run_test(int cbc, int decrypt)
Definition: des.c:46
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:87
static uint8_t large_buffer[10002][8]
Definition: des.c:39
static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt)
Definition: des.c:254
static const uint8_t test_key[]
Definition: des.c:35
static uint8_t tmp[8]
Definition: des.c:38
static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len)
Definition: des.c:177
Definition: des.h:33