2016-03-17 20:13:17 +02:00
|
|
|
/*
|
2016-05-11 20:10:10 +02:00
|
|
|
* This file is part of FFmpeg.
|
2016-03-17 20:13:17 +02:00
|
|
|
*
|
2016-05-11 20:10:10 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2016-03-17 20:13:17 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2016-05-11 20:10:10 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2016-03-17 20:13:17 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2016-05-11 20:10:10 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2016-03-17 20:13:17 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-04-20 11:40:40 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2016-05-17 15:16:53 +02:00
|
|
|
#include "libavutil/mem.h"
|
2016-04-20 11:40:40 +02:00
|
|
|
#include "libavutil/xtea.h"
|
2016-03-17 20:13:17 +02:00
|
|
|
|
|
|
|
#define XTEA_NUM_TESTS 6
|
|
|
|
|
|
|
|
static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = {
|
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
|
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
|
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = {
|
|
|
|
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
|
|
|
|
{ 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
|
|
|
|
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
|
|
|
|
{ 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = {
|
|
|
|
{ 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
|
|
|
|
{ 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
|
|
|
|
{ 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
|
|
|
|
{ 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
|
|
|
|
};
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
crypt(ctx, dst, src, len, iv, dir);
|
|
|
|
if (memcmp(dst, ref, 8*len)) {
|
|
|
|
int i;
|
|
|
|
printf("%s failed\ngot ", test);
|
|
|
|
for (i = 0; i < 8*len; i++)
|
|
|
|
printf("%02x ", dst[i]);
|
|
|
|
printf("\nexpected ");
|
|
|
|
for (i = 0; i < 8*len; i++)
|
|
|
|
printf("%02x ", ref[i]);
|
|
|
|
printf("\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[16], iv[8];
|
|
|
|
int i, j;
|
2016-05-11 20:10:10 +02:00
|
|
|
static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld";
|
2016-03-17 20:13:17 +02:00
|
|
|
uint8_t ct[32];
|
|
|
|
uint8_t pl[32];
|
2016-05-17 15:16:53 +02:00
|
|
|
AVXTEA *ctx = av_xtea_alloc();
|
|
|
|
if (!ctx)
|
|
|
|
return 1;
|
2016-03-17 20:13:17 +02:00
|
|
|
|
|
|
|
for (i = 0; i < XTEA_NUM_TESTS; i++) {
|
2016-05-17 15:16:53 +02:00
|
|
|
av_xtea_init(ctx, xtea_test_key[i]);
|
2016-03-17 20:13:17 +02:00
|
|
|
|
2016-05-17 15:16:53 +02:00
|
|
|
test_xtea(ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt);
|
|
|
|
test_xtea(ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt);
|
2016-03-17 20:13:17 +02:00
|
|
|
|
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j]));
|
2016-05-17 15:16:53 +02:00
|
|
|
av_xtea_le_init(ctx, buf);
|
2016-03-17 20:13:17 +02:00
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j]));
|
|
|
|
AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j]));
|
|
|
|
}
|
2016-05-17 15:16:53 +02:00
|
|
|
test_xtea(ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt);
|
|
|
|
test_xtea(ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt);
|
2016-03-17 20:13:17 +02:00
|
|
|
|
|
|
|
/* encrypt */
|
|
|
|
memcpy(iv, "HALLO123", 8);
|
2016-05-17 15:16:53 +02:00
|
|
|
av_xtea_crypt(ctx, ct, src, 4, iv, 0);
|
2016-03-17 20:13:17 +02:00
|
|
|
|
|
|
|
/* decrypt into pl */
|
|
|
|
memcpy(iv, "HALLO123", 8);
|
2016-05-17 15:16:53 +02:00
|
|
|
test_xtea(ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt);
|
2016-03-17 20:13:17 +02:00
|
|
|
|
|
|
|
memcpy(iv, "HALLO123", 8);
|
2016-05-17 15:16:53 +02:00
|
|
|
test_xtea(ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt);
|
2016-03-17 20:13:17 +02:00
|
|
|
}
|
2016-05-11 20:10:10 +02:00
|
|
|
|
2016-03-17 20:13:17 +02:00
|
|
|
printf("Test encryption/decryption success.\n");
|
2016-05-17 15:16:53 +02:00
|
|
|
av_free(ctx);
|
2016-03-17 20:13:17 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|