1cabdff1aSopenharmony_ci/* 2cabdff1aSopenharmony_ci * This file is part of FFmpeg. 3cabdff1aSopenharmony_ci * 4cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or 5cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 6cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either 7cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 8cabdff1aSopenharmony_ci * 9cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful, 10cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 11cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12cabdff1aSopenharmony_ci * Lesser General Public License for more details. 13cabdff1aSopenharmony_ci * 14cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 15cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software 16cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17cabdff1aSopenharmony_ci */ 18cabdff1aSopenharmony_ci 19cabdff1aSopenharmony_ci#include <stdint.h> 20cabdff1aSopenharmony_ci#include <stdio.h> 21cabdff1aSopenharmony_ci#include <stdlib.h> 22cabdff1aSopenharmony_ci#include <string.h> 23cabdff1aSopenharmony_ci 24cabdff1aSopenharmony_ci#include "libavutil/intreadwrite.h" 25cabdff1aSopenharmony_ci#include "libavutil/mem.h" 26cabdff1aSopenharmony_ci#include "libavutil/xtea.h" 27cabdff1aSopenharmony_ci 28cabdff1aSopenharmony_ci#define XTEA_NUM_TESTS 6 29cabdff1aSopenharmony_ci 30cabdff1aSopenharmony_cistatic const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = { 31cabdff1aSopenharmony_ci { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 32cabdff1aSopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 33cabdff1aSopenharmony_ci { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 34cabdff1aSopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 35cabdff1aSopenharmony_ci { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 36cabdff1aSopenharmony_ci 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 37cabdff1aSopenharmony_ci { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38cabdff1aSopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 39cabdff1aSopenharmony_ci { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40cabdff1aSopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 41cabdff1aSopenharmony_ci { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42cabdff1aSopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } 43cabdff1aSopenharmony_ci}; 44cabdff1aSopenharmony_ci 45cabdff1aSopenharmony_cistatic const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = { 46cabdff1aSopenharmony_ci { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, 47cabdff1aSopenharmony_ci { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 48cabdff1aSopenharmony_ci { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, 49cabdff1aSopenharmony_ci { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, 50cabdff1aSopenharmony_ci { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 51cabdff1aSopenharmony_ci { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } 52cabdff1aSopenharmony_ci}; 53cabdff1aSopenharmony_ci 54cabdff1aSopenharmony_cistatic const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = { 55cabdff1aSopenharmony_ci { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, 56cabdff1aSopenharmony_ci { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, 57cabdff1aSopenharmony_ci { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 58cabdff1aSopenharmony_ci { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, 59cabdff1aSopenharmony_ci { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, 60cabdff1aSopenharmony_ci { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } 61cabdff1aSopenharmony_ci}; 62cabdff1aSopenharmony_ci 63cabdff1aSopenharmony_cistatic void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, 64cabdff1aSopenharmony_ci const uint8_t *ref, int len, uint8_t *iv, int dir, 65cabdff1aSopenharmony_ci const char *test, 66cabdff1aSopenharmony_ci void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int)) 67cabdff1aSopenharmony_ci{ 68cabdff1aSopenharmony_ci crypt(ctx, dst, src, len, iv, dir); 69cabdff1aSopenharmony_ci if (memcmp(dst, ref, 8*len)) { 70cabdff1aSopenharmony_ci int i; 71cabdff1aSopenharmony_ci printf("%s failed\ngot ", test); 72cabdff1aSopenharmony_ci for (i = 0; i < 8*len; i++) 73cabdff1aSopenharmony_ci printf("%02x ", dst[i]); 74cabdff1aSopenharmony_ci printf("\nexpected "); 75cabdff1aSopenharmony_ci for (i = 0; i < 8*len; i++) 76cabdff1aSopenharmony_ci printf("%02x ", ref[i]); 77cabdff1aSopenharmony_ci printf("\n"); 78cabdff1aSopenharmony_ci exit(1); 79cabdff1aSopenharmony_ci } 80cabdff1aSopenharmony_ci} 81cabdff1aSopenharmony_ci 82cabdff1aSopenharmony_ciint main(void) 83cabdff1aSopenharmony_ci{ 84cabdff1aSopenharmony_ci uint8_t buf[16], iv[8]; 85cabdff1aSopenharmony_ci int i, j; 86cabdff1aSopenharmony_ci static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld"; 87cabdff1aSopenharmony_ci uint8_t ct[32]; 88cabdff1aSopenharmony_ci uint8_t pl[32]; 89cabdff1aSopenharmony_ci AVXTEA *ctx = av_xtea_alloc(); 90cabdff1aSopenharmony_ci if (!ctx) 91cabdff1aSopenharmony_ci return 1; 92cabdff1aSopenharmony_ci 93cabdff1aSopenharmony_ci for (i = 0; i < XTEA_NUM_TESTS; i++) { 94cabdff1aSopenharmony_ci av_xtea_init(ctx, xtea_test_key[i]); 95cabdff1aSopenharmony_ci 96cabdff1aSopenharmony_ci test_xtea(ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt); 97cabdff1aSopenharmony_ci test_xtea(ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt); 98cabdff1aSopenharmony_ci 99cabdff1aSopenharmony_ci for (j = 0; j < 4; j++) 100cabdff1aSopenharmony_ci AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j])); 101cabdff1aSopenharmony_ci av_xtea_le_init(ctx, buf); 102cabdff1aSopenharmony_ci for (j = 0; j < 2; j++) { 103cabdff1aSopenharmony_ci AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j])); 104cabdff1aSopenharmony_ci AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j])); 105cabdff1aSopenharmony_ci } 106cabdff1aSopenharmony_ci test_xtea(ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt); 107cabdff1aSopenharmony_ci test_xtea(ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt); 108cabdff1aSopenharmony_ci 109cabdff1aSopenharmony_ci /* encrypt */ 110cabdff1aSopenharmony_ci memcpy(iv, "HALLO123", 8); 111cabdff1aSopenharmony_ci av_xtea_crypt(ctx, ct, src, 4, iv, 0); 112cabdff1aSopenharmony_ci 113cabdff1aSopenharmony_ci /* decrypt into pl */ 114cabdff1aSopenharmony_ci memcpy(iv, "HALLO123", 8); 115cabdff1aSopenharmony_ci test_xtea(ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt); 116cabdff1aSopenharmony_ci 117cabdff1aSopenharmony_ci memcpy(iv, "HALLO123", 8); 118cabdff1aSopenharmony_ci test_xtea(ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt); 119cabdff1aSopenharmony_ci } 120cabdff1aSopenharmony_ci 121cabdff1aSopenharmony_ci printf("Test encryption/decryption success.\n"); 122cabdff1aSopenharmony_ci av_free(ctx); 123cabdff1aSopenharmony_ci 124cabdff1aSopenharmony_ci return 0; 125cabdff1aSopenharmony_ci} 126