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