1cabdff1aSopenharmony_ci/* 2cabdff1aSopenharmony_ci * An implementation of the TwoFish algorithm 3cabdff1aSopenharmony_ci * Copyright (c) 2015 Supraja Meedinti 4cabdff1aSopenharmony_ci * 5cabdff1aSopenharmony_ci * This file is part of FFmpeg. 6cabdff1aSopenharmony_ci * 7cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or 8cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 9cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either 10cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 11cabdff1aSopenharmony_ci * 12cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful, 13cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 14cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15cabdff1aSopenharmony_ci * Lesser General Public License for more details. 16cabdff1aSopenharmony_ci * 17cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 18cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software 19cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20cabdff1aSopenharmony_ci */ 21cabdff1aSopenharmony_ci 22cabdff1aSopenharmony_ci#include "libavutil/log.h" 23cabdff1aSopenharmony_ci#include "libavutil/mem.h" 24cabdff1aSopenharmony_ci#include "libavutil/twofish.h" 25cabdff1aSopenharmony_ci 26cabdff1aSopenharmony_ci#include <stdio.h> 27cabdff1aSopenharmony_ci#include <stdlib.h> 28cabdff1aSopenharmony_ci#include <string.h> 29cabdff1aSopenharmony_ci 30cabdff1aSopenharmony_ciint main(int argc, char *argv[]) 31cabdff1aSopenharmony_ci{ 32cabdff1aSopenharmony_ci uint8_t Key[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff 33cabdff1aSopenharmony_ci }; 34cabdff1aSopenharmony_ci const uint8_t rct[6][16] = { 35cabdff1aSopenharmony_ci {0x9f, 0x58, 0x9f, 0x5c, 0xf6, 0x12, 0x2c, 0x32, 0xb6, 0xbf, 0xec, 0x2f, 0x2a, 0xe8, 0xc3, 0x5a}, 36cabdff1aSopenharmony_ci {0xcf, 0xd1, 0xd2, 0xe5, 0xa9, 0xbe, 0x9c, 0xdf, 0x50, 0x1f, 0x13, 0xb8, 0x92, 0xbd, 0x22, 0x48}, 37cabdff1aSopenharmony_ci {0x37, 0x52, 0x7b, 0xe0, 0x05, 0x23, 0x34, 0xb8, 0x9f, 0x0c, 0xfc, 0xca, 0xe8, 0x7c, 0xfa, 0x20}, 38cabdff1aSopenharmony_ci {0x5d, 0x9d, 0x4e, 0xef, 0xfa, 0x91, 0x51, 0x57, 0x55, 0x24, 0xf1, 0x15, 0x81, 0x5a, 0x12, 0xe0}, 39cabdff1aSopenharmony_ci {0xe7, 0x54, 0x49, 0x21, 0x2b, 0xee, 0xf9, 0xf4, 0xa3, 0x90, 0xbd, 0x86, 0x0a, 0x64, 0x09, 0x41}, 40cabdff1aSopenharmony_ci {0x37, 0xfe, 0x26, 0xff, 0x1c, 0xf6, 0x61, 0x75, 0xf5, 0xdd, 0xf4, 0xc3, 0x3b, 0x97, 0xa2, 0x05} 41cabdff1aSopenharmony_ci }; 42cabdff1aSopenharmony_ci uint8_t temp[32], iv[16], rpt[32] = {0}; 43cabdff1aSopenharmony_ci const int kbits[3] = {128, 192, 256}; 44cabdff1aSopenharmony_ci int i, j, k, err = 0; 45cabdff1aSopenharmony_ci struct AVTWOFISH *cs; 46cabdff1aSopenharmony_ci cs = av_twofish_alloc(); 47cabdff1aSopenharmony_ci if (!cs) 48cabdff1aSopenharmony_ci return 1; 49cabdff1aSopenharmony_ci for (j = 1; j < 3; j++) { 50cabdff1aSopenharmony_ci av_twofish_init(cs, Key, kbits[j]); 51cabdff1aSopenharmony_ci av_twofish_crypt(cs, temp, rpt, 1, NULL, 0); 52cabdff1aSopenharmony_ci for (i = 0; i < 16; i++) { 53cabdff1aSopenharmony_ci if (rct[j][i] != temp[i]) { 54cabdff1aSopenharmony_ci av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]); 55cabdff1aSopenharmony_ci err = 1; 56cabdff1aSopenharmony_ci } 57cabdff1aSopenharmony_ci } 58cabdff1aSopenharmony_ci av_twofish_crypt(cs, temp, rct[j], 1, NULL, 1); 59cabdff1aSopenharmony_ci for (i = 0; i < 16; i++) { 60cabdff1aSopenharmony_ci if (rpt[i] != temp[i]) { 61cabdff1aSopenharmony_ci av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]); 62cabdff1aSopenharmony_ci err = 1; 63cabdff1aSopenharmony_ci } 64cabdff1aSopenharmony_ci } 65cabdff1aSopenharmony_ci } 66cabdff1aSopenharmony_ci for (j = 0; j < 3; j++) { 67cabdff1aSopenharmony_ci memset(Key, 0, sizeof(Key)); 68cabdff1aSopenharmony_ci memset(rpt, 0, sizeof(rpt)); 69cabdff1aSopenharmony_ci for (i = 1; i < 50; i++) { 70cabdff1aSopenharmony_ci av_twofish_init(cs, Key, kbits[j]); 71cabdff1aSopenharmony_ci av_twofish_crypt(cs, temp, rpt, 1, NULL, 0); 72cabdff1aSopenharmony_ci memcpy(Key+16,Key,(kbits[j]-128) >> 3); 73cabdff1aSopenharmony_ci memcpy(Key,rpt,16); 74cabdff1aSopenharmony_ci memcpy(rpt,temp,16); 75cabdff1aSopenharmony_ci av_twofish_crypt(cs, temp, temp, 1, NULL, 1); 76cabdff1aSopenharmony_ci for (k = 0; k < 16; k++) { 77cabdff1aSopenharmony_ci // Need to compare to Key here, because the plaintext comes 78cabdff1aSopenharmony_ci // from rpt but was moved over to Key. 79cabdff1aSopenharmony_ci if (Key[k] != temp[k]) { 80cabdff1aSopenharmony_ci av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", k, Key[k], temp[k]); 81cabdff1aSopenharmony_ci err = 1; 82cabdff1aSopenharmony_ci } 83cabdff1aSopenharmony_ci } 84cabdff1aSopenharmony_ci } 85cabdff1aSopenharmony_ci for (i = 0; i < 16; i++) { 86cabdff1aSopenharmony_ci if (rct[3 + j][i] != rpt[i]) { 87cabdff1aSopenharmony_ci av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[3 + j][i], rpt[i]); 88cabdff1aSopenharmony_ci err = 1; 89cabdff1aSopenharmony_ci } 90cabdff1aSopenharmony_ci } 91cabdff1aSopenharmony_ci } 92cabdff1aSopenharmony_ci memset(rpt, 0, sizeof(rpt)); 93cabdff1aSopenharmony_ci memcpy(iv, "HALLO123HALLO123", 16); 94cabdff1aSopenharmony_ci av_twofish_crypt(cs, temp, rpt, 2, iv, 0); 95cabdff1aSopenharmony_ci memcpy(iv, "HALLO123HALLO123", 16); 96cabdff1aSopenharmony_ci av_twofish_crypt(cs, temp, temp, 2, iv, 1); 97cabdff1aSopenharmony_ci for (i = 0; i < 32; i++) { 98cabdff1aSopenharmony_ci if (rpt[i] != temp[i]) { 99cabdff1aSopenharmony_ci av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]); 100cabdff1aSopenharmony_ci err = 1; 101cabdff1aSopenharmony_ci } 102cabdff1aSopenharmony_ci } 103cabdff1aSopenharmony_ci av_free(cs); 104cabdff1aSopenharmony_ci return err; 105cabdff1aSopenharmony_ci} 106