tests: Move all test programs to a subdirectory
[libav.git] / libavutil / tests / xtea.c
CommitLineData
d12b5b2f
DB
1/*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; 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
a6a750c7
DB
24#include "libavutil/intreadwrite.h"
25#include "libavutil/xtea.h"
d12b5b2f
DB
26
27#define XTEA_NUM_TESTS 6
28
29static 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
44static 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
53static 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
62static 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
81int main(void)
82{
83 AVXTEA ctx;
84 uint8_t buf[16], iv[8];
85 int i, j;
f2422b58 86 static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld";
d12b5b2f
DB
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 printf("Test encryption/decryption success.\n");
118
119 return 0;
120}