FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xtea-test.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 <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "intreadwrite.h"
25 #include "xtea.h"
26 
27 #define XTEA_NUM_TESTS 6
28 
29 static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = {
30  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
31  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
32  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
34  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
35  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
36  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
38  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
40  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
42 };
43 
44 static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = {
45  { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
46  { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
47  { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
48  { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
49  { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
50  { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
51 };
52 
53 static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = {
54  { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
55  { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
56  { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
57  { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
58  { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
59  { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
60 };
61 
62 static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
63  const uint8_t *ref, int len, uint8_t *iv, int dir,
64  const char *test,
65  void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int))
66 {
67  crypt(ctx, dst, src, len, iv, dir);
68  if (memcmp(dst, ref, 8*len)) {
69  int i;
70  printf("%s failed\ngot ", test);
71  for (i = 0; i < 8*len; i++)
72  printf("%02x ", dst[i]);
73  printf("\nexpected ");
74  for (i = 0; i < 8*len; i++)
75  printf("%02x ", ref[i]);
76  printf("\n");
77  exit(1);
78  }
79 }
80 
81 int main(void)
82 {
83  AVXTEA ctx;
84  uint8_t buf[16], iv[8];
85  int i, j;
86  static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld";
87  uint8_t ct[32];
88  uint8_t pl[32];
89 
90  for (i = 0; i < XTEA_NUM_TESTS; i++) {
91  av_xtea_init(&ctx, xtea_test_key[i]);
92 
93  test_xtea(&ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt);
94  test_xtea(&ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt);
95 
96  for (j = 0; j < 4; j++)
97  AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j]));
98  av_xtea_le_init(&ctx, buf);
99  for (j = 0; j < 2; j++) {
100  AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j]));
101  AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j]));
102  }
103  test_xtea(&ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt);
104  test_xtea(&ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt);
105 
106  /* encrypt */
107  memcpy(iv, "HALLO123", 8);
108  av_xtea_crypt(&ctx, ct, src, 4, iv, 0);
109 
110  /* decrypt into pl */
111  memcpy(iv, "HALLO123", 8);
112  test_xtea(&ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt);
113 
114  memcpy(iv, "HALLO123", 8);
115  test_xtea(&ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt);
116  }
117 
118  printf("Test encryption/decryption success.\n");
119 
120  return 0;
121 }
static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8]
Definition: xtea-test.c:53
#define NULL
Definition: coverity.c:32
AVFormatContext * ctx
Definition: movenc-test.c:48
uint8_t
static const uint8_t crypt[]
Definition: des-test.c:37
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
void av_xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context, in big endian format...
Definition: xtea.c:243
static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8]
Definition: xtea-test.c:44
static int ref[MAX_W *MAX_W]
int main(void)
Definition: xtea-test.c:81
void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
Initialize an AVXTEA context.
Definition: xtea.c:42
static void test(const char *pattern, const char *host)
Definition: noproxy-test.c:23
Public header for libavutil XTEA algorithm.
#define src
Definition: vp9dsp.c:530
void av_xtea_le_init(AVXTEA *ctx, const uint8_t key[16])
Initialize an AVXTEA context.
Definition: xtea.c:50
void * buf
Definition: avisynth_c.h:553
#define XTEA_NUM_TESTS
Definition: xtea-test.c:27
static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, const uint8_t *ref, int len, uint8_t *iv, int dir, const char *test, void(*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int))
Definition: xtea-test.c:62
int len
static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16]
Definition: xtea-test.c:29
void av_xtea_le_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context, in little endian format...
Definition: xtea.c:249
Definition: xtea.h:35
#define AV_WL32(p, v)
Definition: intreadwrite.h:426