Browse code

libavutil: Added twofish symmetric block cipher

Signed-off-by: Supraja Meedinti <supraja0493@gmail.com>
Reviewed-by: Giorgio Vazzana <mywing81@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Supraja Meedinti authored on 2015/01/26 03:39:20
Showing 4 changed files
... ...
@@ -16,6 +16,7 @@ version <next>:
16 16
 - Closed caption Decoder
17 17
 - fspp, uspp, pp7 MPlayer postprocessing filters ported to native filters
18 18
 - showpalette filter
19
+- Twofish symmetric block cipher
19 20
 
20 21
 
21 22
 version 2.5:
... ...
@@ -60,6 +60,7 @@ HEADERS = adler32.h                                                     \
60 60
           time.h                                                        \
61 61
           timecode.h                                                    \
62 62
           timestamp.h                                                   \
63
+          twofish.h                                                     \
63 64
           version.h                                                     \
64 65
           xtea.h                                                        \
65 66
 
... ...
@@ -129,6 +130,7 @@ OBJS = adler32.o                                                        \
129 129
        time.o                                                           \
130 130
        timecode.o                                                       \
131 131
        tree.o                                                           \
132
+       twofish.o                                                        \
132 133
        utils.o                                                          \
133 134
        xga_font_data.o                                                  \
134 135
        xtea.o                                                           \
... ...
@@ -184,6 +186,7 @@ TESTPROGS = adler32                                                     \
184 184
             sha512                                                      \
185 185
             softfloat                                                   \
186 186
             tree                                                        \
187
+            twofish                                                     \
187 188
             utf8                                                        \
188 189
             xtea                                                        \
189 190
 
190 191
new file mode 100644
... ...
@@ -0,0 +1,375 @@
0
+/*
1
+ * An implementation of the TwoFish algorithm
2
+ * Copyright (c) 2015 Supraja Meedinti
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+#include "twofish.h"
21
+#include "common.h"
22
+#include "intreadwrite.h"
23
+#include "attributes.h"
24
+
25
+#define LR(x, n) ((x) << (n) | (x) >> (32 - (n)))
26
+#define RR(x, n) ((x) >> (n) | (x) << (32 - (n)))
27
+
28
+typedef struct AVTWOFISH {
29
+    uint32_t K[40];
30
+    uint32_t S[4];
31
+    int ksize;
32
+} AVTWOFISH;
33
+
34
+static const uint8_t MD1[256] = {
35
+    0x00, 0x5b, 0xb6, 0xed, 0x05, 0x5e, 0xb3, 0xe8, 0x0a, 0x51, 0xbc, 0xe7, 0x0f, 0x54, 0xb9, 0xe2,
36
+    0x14, 0x4f, 0xa2, 0xf9, 0x11, 0x4a, 0xa7, 0xfc, 0x1e, 0x45, 0xa8, 0xf3, 0x1b, 0x40, 0xad, 0xf6,
37
+    0x28, 0x73, 0x9e, 0xc5, 0x2d, 0x76, 0x9b, 0xc0, 0x22, 0x79, 0x94, 0xcf, 0x27, 0x7c, 0x91, 0xca,
38
+    0x3c, 0x67, 0x8a, 0xd1, 0x39, 0x62, 0x8f, 0xd4, 0x36, 0x6d, 0x80, 0xdb, 0x33, 0x68, 0x85, 0xde,
39
+    0x50, 0x0b, 0xe6, 0xbd, 0x55, 0x0e, 0xe3, 0xb8, 0x5a, 0x01, 0xec, 0xb7, 0x5f, 0x04, 0xe9, 0xb2,
40
+    0x44, 0x1f, 0xf2, 0xa9, 0x41, 0x1a, 0xf7, 0xac, 0x4e, 0x15, 0xf8, 0xa3, 0x4b, 0x10, 0xfd, 0xa6,
41
+    0x78, 0x23, 0xce, 0x95, 0x7d, 0x26, 0xcb, 0x90, 0x72, 0x29, 0xc4, 0x9f, 0x77, 0x2c, 0xc1, 0x9a,
42
+    0x6c, 0x37, 0xda, 0x81, 0x69, 0x32, 0xdf, 0x84, 0x66, 0x3d, 0xd0, 0x8b, 0x63, 0x38, 0xd5, 0x8e,
43
+    0xa0, 0xfb, 0x16, 0x4d, 0xa5, 0xfe, 0x13, 0x48, 0xaa, 0xf1, 0x1c, 0x47, 0xaf, 0xf4, 0x19, 0x42,
44
+    0xb4, 0xef, 0x02, 0x59, 0xb1, 0xea, 0x07, 0x5c, 0xbe, 0xe5, 0x08, 0x53, 0xbb, 0xe0, 0x0d, 0x56,
45
+    0x88, 0xd3, 0x3e, 0x65, 0x8d, 0xd6, 0x3b, 0x60, 0x82, 0xd9, 0x34, 0x6f, 0x87, 0xdc, 0x31, 0x6a,
46
+    0x9c, 0xc7, 0x2a, 0x71, 0x99, 0xc2, 0x2f, 0x74, 0x96, 0xcd, 0x20, 0x7b, 0x93, 0xc8, 0x25, 0x7e,
47
+    0xf0, 0xab, 0x46, 0x1d, 0xf5, 0xae, 0x43, 0x18, 0xfa, 0xa1, 0x4c, 0x17, 0xff, 0xa4, 0x49, 0x12,
48
+    0xe4, 0xbf, 0x52, 0x09, 0xe1, 0xba, 0x57, 0x0c, 0xee, 0xb5, 0x58, 0x03, 0xeb, 0xb0, 0x5d, 0x06,
49
+    0xd8, 0x83, 0x6e, 0x35, 0xdd, 0x86, 0x6b, 0x30, 0xd2, 0x89, 0x64, 0x3f, 0xd7, 0x8c, 0x61, 0x3a,
50
+    0xcc, 0x97, 0x7a, 0x21, 0xc9, 0x92, 0x7f, 0x24, 0xc6, 0x9d, 0x70, 0x2b, 0xc3, 0x98, 0x75, 0x2e
51
+};
52
+
53
+static const uint8_t MD2[256] = {
54
+    0x00, 0xef, 0xb7, 0x58, 0x07, 0xe8, 0xb0, 0x5f, 0x0e, 0xe1, 0xb9, 0x56, 0x09, 0xe6, 0xbe, 0x51,
55
+    0x1c, 0xf3, 0xab, 0x44, 0x1b, 0xf4, 0xac, 0x43, 0x12, 0xfd, 0xa5, 0x4a, 0x15, 0xfa, 0xa2, 0x4d,
56
+    0x38, 0xd7, 0x8f, 0x60, 0x3f, 0xd0, 0x88, 0x67, 0x36, 0xd9, 0x81, 0x6e, 0x31, 0xde, 0x86, 0x69,
57
+    0x24, 0xcb, 0x93, 0x7c, 0x23, 0xcc, 0x94, 0x7b, 0x2a, 0xc5, 0x9d, 0x72, 0x2d, 0xc2, 0x9a, 0x75,
58
+    0x70, 0x9f, 0xc7, 0x28, 0x77, 0x98, 0xc0, 0x2f, 0x7e, 0x91, 0xc9, 0x26, 0x79, 0x96, 0xce, 0x21,
59
+    0x6c, 0x83, 0xdb, 0x34, 0x6b, 0x84, 0xdc, 0x33, 0x62, 0x8d, 0xd5, 0x3a, 0x65, 0x8a, 0xd2, 0x3d,
60
+    0x48, 0xa7, 0xff, 0x10, 0x4f, 0xa0, 0xf8, 0x17, 0x46, 0xa9, 0xf1, 0x1e, 0x41, 0xae, 0xf6, 0x19,
61
+    0x54, 0xbb, 0xe3, 0x0c, 0x53, 0xbc, 0xe4, 0x0b, 0x5a, 0xb5, 0xed, 0x02, 0x5d, 0xb2, 0xea, 0x05,
62
+    0xe0, 0x0f, 0x57, 0xb8, 0xe7, 0x08, 0x50, 0xbf, 0xee, 0x01, 0x59, 0xb6, 0xe9, 0x06, 0x5e, 0xb1,
63
+    0xfc, 0x13, 0x4b, 0xa4, 0xfb, 0x14, 0x4c, 0xa3, 0xf2, 0x1d, 0x45, 0xaa, 0xf5, 0x1a, 0x42, 0xad,
64
+    0xd8, 0x37, 0x6f, 0x80, 0xdf, 0x30, 0x68, 0x87, 0xd6, 0x39, 0x61, 0x8e, 0xd1, 0x3e, 0x66, 0x89,
65
+    0xc4, 0x2b, 0x73, 0x9c, 0xc3, 0x2c, 0x74, 0x9b, 0xca, 0x25, 0x7d, 0x92, 0xcd, 0x22, 0x7a, 0x95,
66
+    0x90, 0x7f, 0x27, 0xc8, 0x97, 0x78, 0x20, 0xcf, 0x9e, 0x71, 0x29, 0xc6, 0x99, 0x76, 0x2e, 0xc1,
67
+    0x8c, 0x63, 0x3b, 0xd4, 0x8b, 0x64, 0x3c, 0xd3, 0x82, 0x6d, 0x35, 0xda, 0x85, 0x6a, 0x32, 0xdd,
68
+    0xa8, 0x47, 0x1f, 0xf0, 0xaf, 0x40, 0x18, 0xf7, 0xa6, 0x49, 0x11, 0xfe, 0xa1, 0x4e, 0x16, 0xf9,
69
+    0xb4, 0x5b, 0x03, 0xec, 0xb3, 0x5c, 0x04, 0xeb, 0xba, 0x55, 0x0d, 0xe2, 0xbd, 0x52, 0x0a, 0xe5
70
+};
71
+
72
+static const uint8_t q0[256] = {
73
+    0xa9, 0x67, 0xb3, 0xe8, 0x04, 0xfd, 0xa3, 0x76, 0x9a, 0x92, 0x80, 0x78, 0xe4, 0xdd, 0xd1, 0x38,
74
+    0x0d, 0xc6, 0x35, 0x98, 0x18, 0xf7, 0xec, 0x6c, 0x43, 0x75, 0x37, 0x26, 0xfa, 0x13, 0x94, 0x48,
75
+    0xf2, 0xd0, 0x8b, 0x30, 0x84, 0x54, 0xdf, 0x23, 0x19, 0x5b, 0x3d, 0x59, 0xf3, 0xae, 0xa2, 0x82,
76
+    0x63, 0x01, 0x83, 0x2e, 0xd9, 0x51, 0x9b, 0x7c, 0xa6, 0xeb, 0xa5, 0xbe, 0x16, 0x0c, 0xe3, 0x61,
77
+    0xc0, 0x8c, 0x3a, 0xf5, 0x73, 0x2c, 0x25, 0x0b, 0xbb, 0x4e, 0x89, 0x6b, 0x53, 0x6a, 0xb4, 0xf1,
78
+    0xe1, 0xe6, 0xbd, 0x45, 0xe2, 0xf4, 0xb6, 0x66, 0xcc, 0x95, 0x03, 0x56, 0xd4, 0x1c, 0x1e, 0xd7,
79
+    0xfb, 0xc3, 0x8e, 0xb5, 0xe9, 0xcf, 0xbf, 0xba, 0xea, 0x77, 0x39, 0xaf, 0x33, 0xc9, 0x62, 0x71,
80
+    0x81, 0x79, 0x09, 0xad, 0x24, 0xcd, 0xf9, 0xd8, 0xe5, 0xc5, 0xb9, 0x4d, 0x44, 0x08, 0x86, 0xe7,
81
+    0xa1, 0x1d, 0xaa, 0xed, 0x06, 0x70, 0xb2, 0xd2, 0x41, 0x7b, 0xa0, 0x11, 0x31, 0xc2, 0x27, 0x90,
82
+    0x20, 0xf6, 0x60, 0xff, 0x96, 0x5c, 0xb1, 0xab, 0x9e, 0x9c, 0x52, 0x1b, 0x5f, 0x93, 0x0a, 0xef,
83
+    0x91, 0x85, 0x49, 0xee, 0x2d, 0x4f, 0x8f, 0x3b, 0x47, 0x87, 0x6d, 0x46, 0xd6, 0x3e, 0x69, 0x64,
84
+    0x2a, 0xce, 0xcb, 0x2f, 0xfc, 0x97, 0x05, 0x7a, 0xac, 0x7f, 0xd5, 0x1a, 0x4b, 0x0e, 0xa7, 0x5a,
85
+    0x28, 0x14, 0x3f, 0x29, 0x88, 0x3c, 0x4c, 0x02, 0xb8, 0xda, 0xb0, 0x17, 0x55, 0x1f, 0x8a, 0x7d,
86
+    0x57, 0xc7, 0x8d, 0x74, 0xb7, 0xc4, 0x9f, 0x72, 0x7e, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34,
87
+    0x6e, 0x50, 0xde, 0x68, 0x65, 0xbc, 0xdb, 0xf8, 0xc8, 0xa8, 0x2b, 0x40, 0xdc, 0xfe, 0x32, 0xa4,
88
+    0xca, 0x10, 0x21, 0xf0, 0xd3, 0x5d, 0x0f, 0x00, 0x6f, 0x9d, 0x36, 0x42, 0x4a, 0x5e, 0xc1, 0xe0
89
+};
90
+
91
+static const uint8_t q1[256] = {
92
+    0x75, 0xf3, 0xc6, 0xf4, 0xdb, 0x7b, 0xfb, 0xc8, 0x4a, 0xd3, 0xe6, 0x6b, 0x45, 0x7d, 0xe8, 0x4b,
93
+    0xd6, 0x32, 0xd8, 0xfd, 0x37, 0x71, 0xf1, 0xe1, 0x30, 0x0f, 0xf8, 0x1b, 0x87, 0xfa, 0x06, 0x3f,
94
+    0x5e, 0xba, 0xae, 0x5b, 0x8a, 0x00, 0xbc, 0x9d, 0x6d, 0xc1, 0xb1, 0x0e, 0x80, 0x5d, 0xd2, 0xd5,
95
+    0xa0, 0x84, 0x07, 0x14, 0xb5, 0x90, 0x2c, 0xa3, 0xb2, 0x73, 0x4c, 0x54, 0x92, 0x74, 0x36, 0x51,
96
+    0x38, 0xb0, 0xbd, 0x5a, 0xfc, 0x60, 0x62, 0x96, 0x6c, 0x42, 0xf7, 0x10, 0x7c, 0x28, 0x27, 0x8c,
97
+    0x13, 0x95, 0x9c, 0xc7, 0x24, 0x46, 0x3b, 0x70, 0xca, 0xe3, 0x85, 0xcb, 0x11, 0xd0, 0x93, 0xb8,
98
+    0xa6, 0x83, 0x20, 0xff, 0x9f, 0x77, 0xc3, 0xcc, 0x03, 0x6f, 0x08, 0xbf, 0x40, 0xe7, 0x2b, 0xe2,
99
+    0x79, 0x0c, 0xaa, 0x82, 0x41, 0x3a, 0xea, 0xb9, 0xe4, 0x9a, 0xa4, 0x97, 0x7e, 0xda, 0x7a, 0x17,
100
+    0x66, 0x94, 0xa1, 0x1d, 0x3d, 0xf0, 0xde, 0xb3, 0x0b, 0x72, 0xa7, 0x1c, 0xef, 0xd1, 0x53, 0x3e,
101
+    0x8f, 0x33, 0x26, 0x5f, 0xec, 0x76, 0x2a, 0x49, 0x81, 0x88, 0xee, 0x21, 0xc4, 0x1a, 0xeb, 0xd9,
102
+    0xc5, 0x39, 0x99, 0xcd, 0xad, 0x31, 0x8b, 0x01, 0x18, 0x23, 0xdd, 0x1f, 0x4e, 0x2d, 0xf9, 0x48,
103
+    0x4f, 0xf2, 0x65, 0x8e, 0x78, 0x5c, 0x58, 0x19, 0x8d, 0xe5, 0x98, 0x57, 0x67, 0x7f, 0x05, 0x64,
104
+    0xaf, 0x63, 0xb6, 0xfe, 0xf5, 0xb7, 0x3c, 0xa5, 0xce, 0xe9, 0x68, 0x44, 0xe0, 0x4d, 0x43, 0x69,
105
+    0x29, 0x2e, 0xac, 0x15, 0x59, 0xa8, 0x0a, 0x9e, 0x6e, 0x47, 0xdf, 0x34, 0x35, 0x6a, 0xcf, 0xdc,
106
+    0x22, 0xc9, 0xc0, 0x9b, 0x89, 0xd4, 0xed, 0xab, 0x12, 0xa2, 0x0d, 0x52, 0xbb, 0x02, 0x2f, 0xa9,
107
+    0xd7, 0x61, 0x1e, 0xb4, 0x50, 0x04, 0xf6, 0xc2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xbe, 0x91
108
+};
109
+
110
+struct AVTWOFISH *av_twofish_alloc(void)
111
+{
112
+    return av_mallocz(sizeof(struct AVTWOFISH));
113
+}
114
+
115
+const int av_twofish_size = sizeof(AVTWOFISH);
116
+
117
+static uint8_t gfmul(uint8_t a, uint8_t b)
118
+{
119
+    uint8_t r = 0, t;
120
+    while (a && b) {
121
+        if (a & 1)
122
+            r = r ^ b;
123
+        t = b & 0x80;
124
+        b = b << 1;
125
+        if (t)
126
+            b = b ^ 0x4d;
127
+        a = a >> 1;
128
+    }
129
+    return r;
130
+}
131
+
132
+static uint32_t tf_RS(uint32_t k0, uint32_t k1)
133
+{
134
+    uint8_t s[4], m[8];
135
+    AV_WL32(m, k0);
136
+    AV_WL32(m + 4, k1);
137
+    s[0] = gfmul(0x01, m[0]) ^ gfmul(0xa4, m[1]) ^ gfmul(0x55, m[2]) ^ gfmul(0x87, m[3]) ^ gfmul(0x5a, m[4]) ^ gfmul(0x58, m[5]) ^ gfmul(0xdb, m[6]) ^ gfmul(0x9e, m[7]);
138
+    s[1] = gfmul(0xa4, m[0]) ^ gfmul(0x56, m[1]) ^ gfmul(0x82, m[2]) ^ gfmul(0xf3, m[3]) ^ gfmul(0x1e, m[4]) ^ gfmul(0xc6, m[5]) ^ gfmul(0x68, m[6]) ^ gfmul(0xe5, m[7]);
139
+    s[2] = gfmul(0x02, m[0]) ^ gfmul(0xa1, m[1]) ^ gfmul(0xfc, m[2]) ^ gfmul(0xc1, m[3]) ^ gfmul(0x47, m[4]) ^ gfmul(0xae, m[5]) ^ gfmul(0x3d, m[6]) ^ gfmul(0x19, m[7]);
140
+    s[3] = gfmul(0xa4, m[0]) ^ gfmul(0x55, m[1]) ^ gfmul(0x87, m[2]) ^ gfmul(0x5a, m[3]) ^ gfmul(0x58, m[4]) ^ gfmul(0xdb, m[5]) ^ gfmul(0x9e, m[6]) ^ gfmul(0x03, m[7]);
141
+    return AV_RL32(s);
142
+}
143
+
144
+static uint32_t tf_h(uint32_t X, uint32_t L[4], int k)
145
+{
146
+    uint8_t y[4], l[4];
147
+    AV_WL32(y, X);
148
+    if (k == 4) {
149
+        AV_WL32(l, L[3]);
150
+        y[0] = q1[y[0]] ^ l[0];
151
+        y[1] = q0[y[1]] ^ l[1];
152
+        y[2] = q0[y[2]] ^ l[2];
153
+        y[3] = q1[y[3]] ^ l[3];
154
+    }
155
+    if (k >= 3) {
156
+        AV_WL32(l, L[2]);
157
+        y[0] = q1[y[0]] ^ l[0];
158
+        y[1] = q1[y[1]] ^ l[1];
159
+        y[2] = q0[y[2]] ^ l[2];
160
+        y[3] = q0[y[3]] ^ l[3];
161
+    }
162
+    AV_WL32(l, L[1]);
163
+    y[0] = q1[q0[q0[y[0]] ^ l[0]] ^ (L[0] & 0xff)];
164
+    y[1] = q0[q0[q1[y[1]] ^ l[1]] ^ ((L[0] >> 8) & 0xff)];
165
+    y[2] = q1[q1[q0[y[2]] ^ l[2]] ^ ((L[0] >> 16) & 0xff)];
166
+    y[3] = q0[q1[q1[y[3]] ^ l[3]] ^ (L[0] >> 24)];
167
+
168
+    l[0] = y[0] ^ MD2[y[1]] ^ MD1[y[2]] ^ MD1[y[3]];
169
+    l[1] = MD1[y[0]] ^ MD2[y[1]] ^ MD2[y[2]] ^ y[3];
170
+    l[2] = MD2[y[0]] ^ MD1[y[1]] ^ y[2] ^ MD2[y[3]];
171
+    l[3] = MD2[y[0]] ^ y[1] ^ MD2[y[2]] ^ MD1[y[3]];
172
+
173
+    return AV_RL32(l);
174
+}
175
+
176
+static void twofish_encrypt(AVTWOFISH *cs, uint8_t *dst, const uint8_t *src)
177
+{
178
+    uint32_t P[4], t0, t1;
179
+    int i;
180
+    P[0] = AV_RL32(src) ^ cs->K[0];
181
+    P[1] = AV_RL32(src + 4) ^ cs->K[1];
182
+    P[2] = AV_RL32(src + 8) ^ cs->K[2];
183
+    P[3] = AV_RL32(src + 12) ^ cs->K[3];
184
+    for (i = 0; i < 16; i += 2) {
185
+        t0 = tf_h(P[0], cs->S, cs->ksize);
186
+        t1 = tf_h(LR(P[1], 8), cs->S, cs->ksize);
187
+        P[2] = RR(P[2] ^ (t0 + t1 + cs->K[2 * i + 8]), 1);
188
+        P[3] = LR(P[3], 1) ^ (t0 + 2 * t1 + cs->K[2 * i + 9]);
189
+        t0 = tf_h(P[2], cs->S, cs->ksize);
190
+        t1 = tf_h(LR(P[3], 8), cs->S, cs->ksize);
191
+        P[0] = RR(P[0] ^ (t0 + t1 + cs->K[2 * i + 10]), 1);
192
+        P[1] = LR(P[1], 1) ^ (t0 + 2 * t1 + cs->K[2 * i + 11]);
193
+    }
194
+    P[2] ^= cs->K[4];
195
+    P[3] ^= cs->K[5];
196
+    P[0] ^= cs->K[6];
197
+    P[1] ^= cs->K[7];
198
+    AV_WL32(dst, P[2]);
199
+    AV_WL32(dst + 4, P[3]);
200
+    AV_WL32(dst + 8, P[0]);
201
+    AV_WL32(dst + 12, P[1]);
202
+}
203
+
204
+static void twofish_decrypt(AVTWOFISH *cs, uint8_t *dst, const uint8_t *src, uint8_t *iv)
205
+{
206
+    uint32_t P[4], t0, t1;
207
+    int i;
208
+    P[2] = AV_RL32(src) ^ cs->K[4];
209
+    P[3] = AV_RL32(src + 4) ^ cs->K[5];
210
+    P[0] = AV_RL32(src + 8) ^ cs->K[6];
211
+    P[1] = AV_RL32(src + 12) ^ cs->K[7];
212
+    for (i = 15; i >= 0; i -= 2) {
213
+        t0 = tf_h(P[2], cs->S, cs->ksize);
214
+        t1 = tf_h(LR(P[3], 8), cs->S, cs->ksize);
215
+        P[0] = LR(P[0], 1) ^ (t0 + t1 + cs->K[2 * i + 8]);
216
+        P[1] = RR(P[1] ^ (t0 + 2 * t1 + cs->K[2 * i + 9]), 1);
217
+        t0 = tf_h(P[0], cs->S, cs->ksize);
218
+        t1 = tf_h(LR(P[1], 8), cs->S, cs->ksize);
219
+        P[2] = LR(P[2], 1) ^ (t0 + t1 + cs->K[2 * i + 6]);
220
+        P[3] = RR(P[3] ^ (t0 + 2 * t1 + cs->K[2 * i + 7]), 1);
221
+    }
222
+    P[0] ^= cs->K[0];
223
+    P[1] ^= cs->K[1];
224
+    P[2] ^= cs->K[2];
225
+    P[3] ^= cs->K[3];
226
+    if (iv) {
227
+        P[0] ^= AV_RL32(iv);
228
+        P[1] ^= AV_RL32(iv + 4);
229
+        P[2] ^= AV_RL32(iv + 8);
230
+        P[3] ^= AV_RL32(iv + 12);
231
+        memcpy(iv, src, 16);
232
+    }
233
+    AV_WL32(dst, P[2]);
234
+    AV_WL32(dst + 4, P[3]);
235
+    AV_WL32(dst + 8, P[0]);
236
+    AV_WL32(dst + 12, P[1]);
237
+}
238
+
239
+av_cold int av_twofish_init(AVTWOFISH *cs, const uint8_t *key, int key_bits)
240
+{
241
+    int i;
242
+    uint8_t keypad[32];
243
+    uint32_t Key[8], Me[4], Mo[4], A, B;
244
+    const uint32_t rho = 0x01010101;
245
+    if (key_bits < 0)
246
+        return -1;
247
+    if (key_bits <= 128) {
248
+        cs->ksize = 2;
249
+    } else if (key_bits <= 192) {
250
+        cs->ksize = 3;
251
+    } else {
252
+        cs->ksize = 4;
253
+    }
254
+    memset(keypad, 0, sizeof(keypad));
255
+    if (key_bits <= 256) {
256
+        memcpy(keypad, key, key_bits >> 3);
257
+    } else {
258
+        memcpy(keypad, key, 32);
259
+    }
260
+    for (i = 0; i < 2 * cs->ksize ; i++)
261
+        Key[i] = AV_RL32(keypad + 4 * i);
262
+    for (i = 0; i < cs->ksize; i++) {
263
+        Me[i] = Key[2 * i];
264
+        Mo[i] = Key[2 * i + 1];
265
+        cs->S[cs->ksize - i - 1] = tf_RS(Me[i], Mo[i]);
266
+    }
267
+    for (i = 0; i < 20; i++) {
268
+        A = tf_h((2 * i) * rho, Me, cs->ksize);
269
+        B = tf_h((2 * i + 1) * rho, Mo, cs->ksize);
270
+        B = LR(B, 8);
271
+        cs->K[2 * i] = A + B;
272
+        cs->K[2 * i + 1] = LR((A + (2 * B)), 9);
273
+    }
274
+    if (cs->ksize << 6 != key_bits) {
275
+        return 1;
276
+    } else {
277
+        return 0;
278
+    }
279
+}
280
+
281
+void av_twofish_crypt(AVTWOFISH *cs, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
282
+{
283
+    int i;
284
+    while (count--) {
285
+        if (decrypt) {
286
+            twofish_decrypt(cs, dst, src, iv);
287
+        } else {
288
+            if (iv) {
289
+                for (i = 0; i < 16; i++)
290
+                    dst[i] = src[i] ^ iv[i];
291
+                twofish_encrypt(cs, dst, dst);
292
+                memcpy(iv, dst, 16);
293
+            } else {
294
+                twofish_encrypt(cs, dst, src);
295
+            }
296
+        }
297
+        src = src + 16;
298
+        dst = dst + 16;
299
+    }
300
+}
301
+
302
+#ifdef TEST
303
+#include<stdio.h>
304
+#include<stdlib.h>
305
+#include"log.h"
306
+
307
+int main(int argc, char *argv[])
308
+{
309
+    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
310
+    };
311
+    const uint8_t rct[6][16] = {
312
+        {0x9f, 0x58, 0x9f, 0x5c, 0xf6, 0x12, 0x2c, 0x32, 0xb6, 0xbf, 0xec, 0x2f, 0x2a, 0xe8, 0xc3, 0x5a},
313
+        {0xcf, 0xd1, 0xd2, 0xe5, 0xa9, 0xbe, 0x9c, 0xdf, 0x50, 0x1f, 0x13, 0xb8, 0x92, 0xbd, 0x22, 0x48},
314
+        {0x37, 0x52, 0x7b, 0xe0, 0x05, 0x23, 0x34, 0xb8, 0x9f, 0x0c, 0xfc, 0xca, 0xe8, 0x7c, 0xfa, 0x20},
315
+        {0x5d, 0x9d, 0x4e, 0xef, 0xfa, 0x91, 0x51, 0x57, 0x55, 0x24, 0xf1, 0x15, 0x81, 0x5a, 0x12, 0xe0},
316
+        {0xe7, 0x54, 0x49, 0x21, 0x2b, 0xee, 0xf9, 0xf4, 0xa3, 0x90, 0xbd, 0x86, 0x0a, 0x64, 0x09, 0x41},
317
+        {0x37, 0xfe, 0x26, 0xff, 0x1c, 0xf6, 0x61, 0x75, 0xf5, 0xdd, 0xf4, 0xc3, 0x3b, 0x97, 0xa2, 0x05}
318
+    };
319
+    uint8_t temp[32], iv[16], rpt[32] = {0};
320
+    const int kbits[3] = {128, 192, 256};
321
+    int i, j, err = 0;
322
+    AVTWOFISH *cs;
323
+    cs = av_twofish_alloc();
324
+    if (!cs)
325
+        return 1;
326
+    for (j = 1; j < 3; j++) {
327
+        av_twofish_init(cs, Key, kbits[j]);
328
+        av_twofish_crypt(cs, temp, rpt, 1, NULL, 0);
329
+        for (i = 0; i < 16; i++) {
330
+            if (rct[j][i] != temp[i]) {
331
+                av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]);
332
+                err = 1;
333
+            }
334
+        }
335
+        av_twofish_crypt(cs, temp, rct[j], 1, NULL, 1);
336
+        for (i = 0; i < 16; i++) {
337
+            if (rpt[i] != temp[i]) {
338
+                av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
339
+                err = 1;
340
+            }
341
+        }
342
+    }
343
+    for (j = 0; j < 3; j++) {
344
+        memset(Key, 0, sizeof(Key));
345
+        memset(rpt, 0, sizeof(rpt));
346
+        for (i = 1; i < 50; i++) {
347
+            av_twofish_init(cs, Key, kbits[j]);
348
+            av_twofish_crypt(cs, temp, rpt, 1, NULL, 0);
349
+            memcpy(Key+16,Key,(kbits[j]-128) >> 3);
350
+            memcpy(Key,rpt,16);
351
+            memcpy(rpt,temp,16);
352
+        }
353
+        for (i = 0; i < 16; i++) {
354
+            if (rct[3 + j][i] != temp[i]) {
355
+                av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[3 + j][i], temp[i]);
356
+                err = 1;
357
+            }
358
+        }
359
+    }
360
+    memset(rpt, 0, sizeof(rpt));
361
+    memcpy(iv, "HALLO123HALLO123", 16);
362
+    av_twofish_crypt(cs, temp, rpt, 2, iv, 0);
363
+    memcpy(iv, "HALLO123HALLO123", 16);
364
+    av_twofish_crypt(cs, temp, temp, 2, iv, 1);
365
+    for (i = 0; i < 32; i++) {
366
+        if (rpt[i] != temp[i]) {
367
+            av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
368
+            err = 1;
369
+        }
370
+    }
371
+    av_free(cs);
372
+    return err;
373
+}
374
+#endif
0 375
new file mode 100644
... ...
@@ -0,0 +1,70 @@
0
+/*
1
+ * An implementation of the TwoFish algorithm
2
+ * Copyright (c) 2015 Supraja Meedinti
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVUTIL_TWOFISH_H
22
+#define AVUTIL_TWOFISH_H
23
+
24
+#include <stdint.h>
25
+
26
+
27
+/**
28
+  * @file
29
+  * @brief Public header for libavutil TWOFISH algorithm
30
+  * @defgroup lavu_twofish TWOFISH
31
+  * @ingroup lavu_crypto
32
+  * @{
33
+  */
34
+
35
+extern const int av_twofish_size;
36
+
37
+struct AVTWOFISH;
38
+
39
+/**
40
+  * Allocate an AVTWOFISH context
41
+  * To free the struct: av_free(ptr)
42
+  */
43
+struct AVTWOFISH *av_twofish_alloc(void);
44
+
45
+/**
46
+  * Initialize an AVTWOFISH context.
47
+  *
48
+  * @param ctx an AVTWOFISH context
49
+  * @param key a key of size ranging from 1 to 32 bytes used for encryption/decryption
50
+  * @param key_bits number of keybits: 128, 192, 256 If less than the required, padded with zeroes to nearest valid value; return value is 0 if key_bits is 128/192/256, -1 if less than 0, 1 otherwise
51
+ */
52
+int av_twofish_init(struct AVTWOFISH *ctx, const uint8_t *key, int key_bits);
53
+
54
+/**
55
+  * Encrypt or decrypt a buffer using a previously initialized context
56
+  *
57
+  * @param ctx an AVTWOFISH context
58
+  * @param dst destination array, can be equal to src
59
+  * @param src source array, can be equal to dst
60
+  * @param count number of 16 byte blocks
61
+  * @paran iv initialization vector for CBC mode, NULL for ECB mode
62
+  * @param decrypt 0 for encryption, 1 for decryption
63
+ */
64
+void av_twofish_crypt(struct AVTWOFISH *ctx, uint8_t *dst, const uint8_t *src, int count, uint8_t* iv, int decrypt);
65
+
66
+/**
67
+ * @}
68
+ */
69
+#endif /* AVUTIL_TWOFISH_H */