Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/des/des.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/des.h>
5
6 #include <stdlib.h>
7
8 #include "internal.h"
9
10
11 /* IP and FP
12 * The problem is more of a geometric problem that random bit fiddling.
13 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
14 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
15 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
16 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
17
18 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
19 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
20 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
21 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
22
23 The output has been subject to swaps of the form
24 0 1 -> 3 1 but the odd and even bits have been put into
25 2 3 2 0
26 different words. The main trick is to remember that
27 t=((l>>size)^r)&(mask);
28 r^=t;
29 l^=(t<<size);
30 can be used to swap and move bits between words.
31
32 So l = 0 1 2 3 r = 16 17 18 19
33 4 5 6 7 20 21 22 23
34 8 9 10 11 24 25 26 27
35 12 13 14 15 28 29 30 31
36 becomes (for size == 2 and mask == 0x3333)
37 t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
38 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
39 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
40 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
41
42 Thanks for hints from Richard Outerbridge - he told me IP&FP
43 could be done in 15 xor, 10 shifts and 5 ands.
44 When I finally started to think of the problem in 2D
45 I first got ~42 operations without xors. When I remembered
46 how to use xors :-) I got it to its final state.
47 */
48 #define PERM_OP(a, b, t, n, m) \
49 do { \
50 (t) = ((((a) >> (n)) ^ (b)) & (m)); \
51 (b) ^= (t); \
52 (a) ^= ((t) << (n)); \
53 } while (0)
54
55 #define IP(l, r) \
56 do { \
57 uint32_t tt; \
58 PERM_OP(r, l, tt, 4, 0x0f0f0f0fL); \
59 PERM_OP(l, r, tt, 16, 0x0000ffffL); \
60 PERM_OP(r, l, tt, 2, 0x33333333L); \
61 PERM_OP(l, r, tt, 8, 0x00ff00ffL); \
62 PERM_OP(r, l, tt, 1, 0x55555555L); \
63 } while (0)
64
65 #define FP(l, r) \
66 do { \
67 uint32_t tt; \
68 PERM_OP(l, r, tt, 1, 0x55555555L); \
69 PERM_OP(r, l, tt, 8, 0x00ff00ffL); \
70 PERM_OP(l, r, tt, 2, 0x33333333L); \
71 PERM_OP(r, l, tt, 16, 0x0000ffffL); \
72 PERM_OP(l, r, tt, 4, 0x0f0f0f0fL); \
73 } while (0)
74
75 #define LOAD_DATA(ks, R, S, u, t, E0, E1) \
76 do { \
77 (u) = (R) ^ (ks)->subkeys[S][0]; \
78 (t) = (R) ^ (ks)->subkeys[S][1]; \
79 } while (0)
80
81 #define D_ENCRYPT(ks, LL, R, S) \
82 do { \
83 LOAD_DATA(ks, R, S, u, t, E0, E1); \
84 t = CRYPTO_rotr_u32(t, 4); \
85 (LL) ^= \
86 DES_SPtrans[0][(u >> 2L) & 0x3f] ^ DES_SPtrans[2][(u >> 10L) & 0x3f] ^ \
87 DES_SPtrans[4][(u >> 18L) & 0x3f] ^ \
88 DES_SPtrans[6][(u >> 26L) & 0x3f] ^ DES_SPtrans[1][(t >> 2L) & 0x3f] ^ \
89 DES_SPtrans[3][(t >> 10L) & 0x3f] ^ \
90 DES_SPtrans[5][(t >> 18L) & 0x3f] ^ DES_SPtrans[7][(t >> 26L) & 0x3f]; \
91 } while (0)
92
93 #define ITERATIONS 16
94 #define HALF_ITERATIONS 8
95
96 static const uint32_t des_skb[8][64] = {
97 { // for C bits (numbered as per FIPS 46) 1 2 3 4 5 6
98 0x00000000, 0x00000010, 0x20000000, 0x20000010, 0x00010000,
99 0x00010010, 0x20010000, 0x20010010, 0x00000800, 0x00000810,
100 0x20000800, 0x20000810, 0x00010800, 0x00010810, 0x20010800,
101 0x20010810, 0x00000020, 0x00000030, 0x20000020, 0x20000030,
102 0x00010020, 0x00010030, 0x20010020, 0x20010030, 0x00000820,
103 0x00000830, 0x20000820, 0x20000830, 0x00010820, 0x00010830,
104 0x20010820, 0x20010830, 0x00080000, 0x00080010, 0x20080000,
105 0x20080010, 0x00090000, 0x00090010, 0x20090000, 0x20090010,
106 0x00080800, 0x00080810, 0x20080800, 0x20080810, 0x00090800,
107 0x00090810, 0x20090800, 0x20090810, 0x00080020, 0x00080030,
108 0x20080020, 0x20080030, 0x00090020, 0x00090030, 0x20090020,
109 0x20090030, 0x00080820, 0x00080830, 0x20080820, 0x20080830,
110 0x00090820, 0x00090830, 0x20090820, 0x20090830, },
111 { // for C bits (numbered as per FIPS 46) 7 8 10 11 12 13
112 0x00000000, 0x02000000, 0x00002000, 0x02002000, 0x00200000,
113 0x02200000, 0x00202000, 0x02202000, 0x00000004, 0x02000004,
114 0x00002004, 0x02002004, 0x00200004, 0x02200004, 0x00202004,
115 0x02202004, 0x00000400, 0x02000400, 0x00002400, 0x02002400,
116 0x00200400, 0x02200400, 0x00202400, 0x02202400, 0x00000404,
117 0x02000404, 0x00002404, 0x02002404, 0x00200404, 0x02200404,
118 0x00202404, 0x02202404, 0x10000000, 0x12000000, 0x10002000,
119 0x12002000, 0x10200000, 0x12200000, 0x10202000, 0x12202000,
120 0x10000004, 0x12000004, 0x10002004, 0x12002004, 0x10200004,
121 0x12200004, 0x10202004, 0x12202004, 0x10000400, 0x12000400,
122 0x10002400, 0x12002400, 0x10200400, 0x12200400, 0x10202400,
123 0x12202400, 0x10000404, 0x12000404, 0x10002404, 0x12002404,
124 0x10200404, 0x12200404, 0x10202404, 0x12202404, },
125 { // for C bits (numbered as per FIPS 46) 14 15 16 17 19 20
126 0x00000000, 0x00000001, 0x00040000, 0x00040001, 0x01000000,
127 0x01000001, 0x01040000, 0x01040001, 0x00000002, 0x00000003,
128 0x00040002, 0x00040003, 0x01000002, 0x01000003, 0x01040002,
129 0x01040003, 0x00000200, 0x00000201, 0x00040200, 0x00040201,
130 0x01000200, 0x01000201, 0x01040200, 0x01040201, 0x00000202,
131 0x00000203, 0x00040202, 0x00040203, 0x01000202, 0x01000203,
132 0x01040202, 0x01040203, 0x08000000, 0x08000001, 0x08040000,
133 0x08040001, 0x09000000, 0x09000001, 0x09040000, 0x09040001,
134 0x08000002, 0x08000003, 0x08040002, 0x08040003, 0x09000002,
135 0x09000003, 0x09040002, 0x09040003, 0x08000200, 0x08000201,
136 0x08040200, 0x08040201, 0x09000200, 0x09000201, 0x09040200,
137 0x09040201, 0x08000202, 0x08000203, 0x08040202, 0x08040203,
138 0x09000202, 0x09000203, 0x09040202, 0x09040203, },
139 { // for C bits (numbered as per FIPS 46) 21 23 24 26 27 28
140 0x00000000, 0x00100000, 0x00000100, 0x00100100, 0x00000008,
141 0x00100008, 0x00000108, 0x00100108, 0x00001000, 0x00101000,
142 0x00001100, 0x00101100, 0x00001008, 0x00101008, 0x00001108,
143 0x00101108, 0x04000000, 0x04100000, 0x04000100, 0x04100100,
144 0x04000008, 0x04100008, 0x04000108, 0x04100108, 0x04001000,
145 0x04101000, 0x04001100, 0x04101100, 0x04001008, 0x04101008,
146 0x04001108, 0x04101108, 0x00020000, 0x00120000, 0x00020100,
147 0x00120100, 0x00020008, 0x00120008, 0x00020108, 0x00120108,
148 0x00021000, 0x00121000, 0x00021100, 0x00121100, 0x00021008,
149 0x00121008, 0x00021108, 0x00121108, 0x04020000, 0x04120000,
150 0x04020100, 0x04120100, 0x04020008, 0x04120008, 0x04020108,
151 0x04120108, 0x04021000, 0x04121000, 0x04021100, 0x04121100,
152 0x04021008, 0x04121008, 0x04021108, 0x04121108, },
153 { // for D bits (numbered as per FIPS 46) 1 2 3 4 5 6
154 0x00000000, 0x10000000, 0x00010000, 0x10010000, 0x00000004,
155 0x10000004, 0x00010004, 0x10010004, 0x20000000, 0x30000000,
156 0x20010000, 0x30010000, 0x20000004, 0x30000004, 0x20010004,
157 0x30010004, 0x00100000, 0x10100000, 0x00110000, 0x10110000,
158 0x00100004, 0x10100004, 0x00110004, 0x10110004, 0x20100000,
159 0x30100000, 0x20110000, 0x30110000, 0x20100004, 0x30100004,
160 0x20110004, 0x30110004, 0x00001000, 0x10001000, 0x00011000,
161 0x10011000, 0x00001004, 0x10001004, 0x00011004, 0x10011004,
162 0x20001000, 0x30001000, 0x20011000, 0x30011000, 0x20001004,
163 0x30001004, 0x20011004, 0x30011004, 0x00101000, 0x10101000,
164 0x00111000, 0x10111000, 0x00101004, 0x10101004, 0x00111004,
165 0x10111004, 0x20101000, 0x30101000, 0x20111000, 0x30111000,
166 0x20101004, 0x30101004, 0x20111004, 0x30111004, },
167 { // for D bits (numbered as per FIPS 46) 8 9 11 12 13 14
168 0x00000000, 0x08000000, 0x00000008, 0x08000008, 0x00000400,
169 0x08000400, 0x00000408, 0x08000408, 0x00020000, 0x08020000,
170 0x00020008, 0x08020008, 0x00020400, 0x08020400, 0x00020408,
171 0x08020408, 0x00000001, 0x08000001, 0x00000009, 0x08000009,
172 0x00000401, 0x08000401, 0x00000409, 0x08000409, 0x00020001,
173 0x08020001, 0x00020009, 0x08020009, 0x00020401, 0x08020401,
174 0x00020409, 0x08020409, 0x02000000, 0x0A000000, 0x02000008,
175 0x0A000008, 0x02000400, 0x0A000400, 0x02000408, 0x0A000408,
176 0x02020000, 0x0A020000, 0x02020008, 0x0A020008, 0x02020400,
177 0x0A020400, 0x02020408, 0x0A020408, 0x02000001, 0x0A000001,
178 0x02000009, 0x0A000009, 0x02000401, 0x0A000401, 0x02000409,
179 0x0A000409, 0x02020001, 0x0A020001, 0x02020009, 0x0A020009,
180 0x02020401, 0x0A020401, 0x02020409, 0x0A020409, },
181 { // for D bits (numbered as per FIPS 46) 16 17 18 19 20 21
182 0x00000000, 0x00000100, 0x00080000, 0x00080100, 0x01000000,
183 0x01000100, 0x01080000, 0x01080100, 0x00000010, 0x00000110,
184 0x00080010, 0x00080110, 0x01000010, 0x01000110, 0x01080010,
185 0x01080110, 0x00200000, 0x00200100, 0x00280000, 0x00280100,
186 0x01200000, 0x01200100, 0x01280000, 0x01280100, 0x00200010,
187 0x00200110, 0x00280010, 0x00280110, 0x01200010, 0x01200110,
188 0x01280010, 0x01280110, 0x00000200, 0x00000300, 0x00080200,
189 0x00080300, 0x01000200, 0x01000300, 0x01080200, 0x01080300,
190 0x00000210, 0x00000310, 0x00080210, 0x00080310, 0x01000210,
191 0x01000310, 0x01080210, 0x01080310, 0x00200200, 0x00200300,
192 0x00280200, 0x00280300, 0x01200200, 0x01200300, 0x01280200,
193 0x01280300, 0x00200210, 0x00200310, 0x00280210, 0x00280310,
194 0x01200210, 0x01200310, 0x01280210, 0x01280310, },
195 { // for D bits (numbered as per FIPS 46) 22 23 24 25 27 28
196 0x00000000, 0x04000000, 0x00040000, 0x04040000, 0x00000002,
197 0x04000002, 0x00040002, 0x04040002, 0x00002000, 0x04002000,
198 0x00042000, 0x04042000, 0x00002002, 0x04002002, 0x00042002,
199 0x04042002, 0x00000020, 0x04000020, 0x00040020, 0x04040020,
200 0x00000022, 0x04000022, 0x00040022, 0x04040022, 0x00002020,
201 0x04002020, 0x00042020, 0x04042020, 0x00002022, 0x04002022,
202 0x00042022, 0x04042022, 0x00000800, 0x04000800, 0x00040800,
203 0x04040800, 0x00000802, 0x04000802, 0x00040802, 0x04040802,
204 0x00002800, 0x04002800, 0x00042800, 0x04042800, 0x00002802,
205 0x04002802, 0x00042802, 0x04042802, 0x00000820, 0x04000820,
206 0x00040820, 0x04040820, 0x00000822, 0x04000822, 0x00040822,
207 0x04040822, 0x00002820, 0x04002820, 0x00042820, 0x04042820,
208 0x00002822, 0x04002822, 0x00042822, 0x04042822, }};
209
210 static const uint32_t DES_SPtrans[8][64] = {
211 { // nibble 0
212 0x02080800, 0x00080000, 0x02000002, 0x02080802, 0x02000000,
213 0x00080802, 0x00080002, 0x02000002, 0x00080802, 0x02080800,
214 0x02080000, 0x00000802, 0x02000802, 0x02000000, 0x00000000,
215 0x00080002, 0x00080000, 0x00000002, 0x02000800, 0x00080800,
216 0x02080802, 0x02080000, 0x00000802, 0x02000800, 0x00000002,
217 0x00000800, 0x00080800, 0x02080002, 0x00000800, 0x02000802,
218 0x02080002, 0x00000000, 0x00000000, 0x02080802, 0x02000800,
219 0x00080002, 0x02080800, 0x00080000, 0x00000802, 0x02000800,
220 0x02080002, 0x00000800, 0x00080800, 0x02000002, 0x00080802,
221 0x00000002, 0x02000002, 0x02080000, 0x02080802, 0x00080800,
222 0x02080000, 0x02000802, 0x02000000, 0x00000802, 0x00080002,
223 0x00000000, 0x00080000, 0x02000000, 0x02000802, 0x02080800,
224 0x00000002, 0x02080002, 0x00000800, 0x00080802, },
225 { // nibble 1
226 0x40108010, 0x00000000, 0x00108000, 0x40100000, 0x40000010,
227 0x00008010, 0x40008000, 0x00108000, 0x00008000, 0x40100010,
228 0x00000010, 0x40008000, 0x00100010, 0x40108000, 0x40100000,
229 0x00000010, 0x00100000, 0x40008010, 0x40100010, 0x00008000,
230 0x00108010, 0x40000000, 0x00000000, 0x00100010, 0x40008010,
231 0x00108010, 0x40108000, 0x40000010, 0x40000000, 0x00100000,
232 0x00008010, 0x40108010, 0x00100010, 0x40108000, 0x40008000,
233 0x00108010, 0x40108010, 0x00100010, 0x40000010, 0x00000000,
234 0x40000000, 0x00008010, 0x00100000, 0x40100010, 0x00008000,
235 0x40000000, 0x00108010, 0x40008010, 0x40108000, 0x00008000,
236 0x00000000, 0x40000010, 0x00000010, 0x40108010, 0x00108000,
237 0x40100000, 0x40100010, 0x00100000, 0x00008010, 0x40008000,
238 0x40008010, 0x00000010, 0x40100000, 0x00108000, },
239 { // nibble 2
240 0x04000001, 0x04040100, 0x00000100, 0x04000101, 0x00040001,
241 0x04000000, 0x04000101, 0x00040100, 0x04000100, 0x00040000,
242 0x04040000, 0x00000001, 0x04040101, 0x00000101, 0x00000001,
243 0x04040001, 0x00000000, 0x00040001, 0x04040100, 0x00000100,
244 0x00000101, 0x04040101, 0x00040000, 0x04000001, 0x04040001,
245 0x04000100, 0x00040101, 0x04040000, 0x00040100, 0x00000000,
246 0x04000000, 0x00040101, 0x04040100, 0x00000100, 0x00000001,
247 0x00040000, 0x00000101, 0x00040001, 0x04040000, 0x04000101,
248 0x00000000, 0x04040100, 0x00040100, 0x04040001, 0x00040001,
249 0x04000000, 0x04040101, 0x00000001, 0x00040101, 0x04000001,
250 0x04000000, 0x04040101, 0x00040000, 0x04000100, 0x04000101,
251 0x00040100, 0x04000100, 0x00000000, 0x04040001, 0x00000101,
252 0x04000001, 0x00040101, 0x00000100, 0x04040000, },
253 { // nibble 3
254 0x00401008, 0x10001000, 0x00000008, 0x10401008, 0x00000000,
255 0x10400000, 0x10001008, 0x00400008, 0x10401000, 0x10000008,
256 0x10000000, 0x00001008, 0x10000008, 0x00401008, 0x00400000,
257 0x10000000, 0x10400008, 0x00401000, 0x00001000, 0x00000008,
258 0x00401000, 0x10001008, 0x10400000, 0x00001000, 0x00001008,
259 0x00000000, 0x00400008, 0x10401000, 0x10001000, 0x10400008,
260 0x10401008, 0x00400000, 0x10400008, 0x00001008, 0x00400000,
261 0x10000008, 0x00401000, 0x10001000, 0x00000008, 0x10400000,
262 0x10001008, 0x00000000, 0x00001000, 0x00400008, 0x00000000,
263 0x10400008, 0x10401000, 0x00001000, 0x10000000, 0x10401008,
264 0x00401008, 0x00400000, 0x10401008, 0x00000008, 0x10001000,
265 0x00401008, 0x00400008, 0x00401000, 0x10400000, 0x10001008,
266 0x00001008, 0x10000000, 0x10000008, 0x10401000, },
267 { // nibble 4
268 0x08000000, 0x00010000, 0x00000400, 0x08010420, 0x08010020,
269 0x08000400, 0x00010420, 0x08010000, 0x00010000, 0x00000020,
270 0x08000020, 0x00010400, 0x08000420, 0x08010020, 0x08010400,
271 0x00000000, 0x00010400, 0x08000000, 0x00010020, 0x00000420,
272 0x08000400, 0x00010420, 0x00000000, 0x08000020, 0x00000020,
273 0x08000420, 0x08010420, 0x00010020, 0x08010000, 0x00000400,
274 0x00000420, 0x08010400, 0x08010400, 0x08000420, 0x00010020,
275 0x08010000, 0x00010000, 0x00000020, 0x08000020, 0x08000400,
276 0x08000000, 0x00010400, 0x08010420, 0x00000000, 0x00010420,
277 0x08000000, 0x00000400, 0x00010020, 0x08000420, 0x00000400,
278 0x00000000, 0x08010420, 0x08010020, 0x08010400, 0x00000420,
279 0x00010000, 0x00010400, 0x08010020, 0x08000400, 0x00000420,
280 0x00000020, 0x00010420, 0x08010000, 0x08000020, },
281 { // nibble 5
282 0x80000040, 0x00200040, 0x00000000, 0x80202000, 0x00200040,
283 0x00002000, 0x80002040, 0x00200000, 0x00002040, 0x80202040,
284 0x00202000, 0x80000000, 0x80002000, 0x80000040, 0x80200000,
285 0x00202040, 0x00200000, 0x80002040, 0x80200040, 0x00000000,
286 0x00002000, 0x00000040, 0x80202000, 0x80200040, 0x80202040,
287 0x80200000, 0x80000000, 0x00002040, 0x00000040, 0x00202000,
288 0x00202040, 0x80002000, 0x00002040, 0x80000000, 0x80002000,
289 0x00202040, 0x80202000, 0x00200040, 0x00000000, 0x80002000,
290 0x80000000, 0x00002000, 0x80200040, 0x00200000, 0x00200040,
291 0x80202040, 0x00202000, 0x00000040, 0x80202040, 0x00202000,
292 0x00200000, 0x80002040, 0x80000040, 0x80200000, 0x00202040,
293 0x00000000, 0x00002000, 0x80000040, 0x80002040, 0x80202000,
294 0x80200000, 0x00002040, 0x00000040, 0x80200040, },
295 { // nibble 6
296 0x00004000, 0x00000200, 0x01000200, 0x01000004, 0x01004204,
297 0x00004004, 0x00004200, 0x00000000, 0x01000000, 0x01000204,
298 0x00000204, 0x01004000, 0x00000004, 0x01004200, 0x01004000,
299 0x00000204, 0x01000204, 0x00004000, 0x00004004, 0x01004204,
300 0x00000000, 0x01000200, 0x01000004, 0x00004200, 0x01004004,
301 0x00004204, 0x01004200, 0x00000004, 0x00004204, 0x01004004,
302 0x00000200, 0x01000000, 0x00004204, 0x01004000, 0x01004004,
303 0x00000204, 0x00004000, 0x00000200, 0x01000000, 0x01004004,
304 0x01000204, 0x00004204, 0x00004200, 0x00000000, 0x00000200,
305 0x01000004, 0x00000004, 0x01000200, 0x00000000, 0x01000204,
306 0x01000200, 0x00004200, 0x00000204, 0x00004000, 0x01004204,
307 0x01000000, 0x01004200, 0x00000004, 0x00004004, 0x01004204,
308 0x01000004, 0x01004200, 0x01004000, 0x00004004, },
309 { // nibble 7
310 0x20800080, 0x20820000, 0x00020080, 0x00000000, 0x20020000,
311 0x00800080, 0x20800000, 0x20820080, 0x00000080, 0x20000000,
312 0x00820000, 0x00020080, 0x00820080, 0x20020080, 0x20000080,
313 0x20800000, 0x00020000, 0x00820080, 0x00800080, 0x20020000,
314 0x20820080, 0x20000080, 0x00000000, 0x00820000, 0x20000000,
315 0x00800000, 0x20020080, 0x20800080, 0x00800000, 0x00020000,
316 0x20820000, 0x00000080, 0x00800000, 0x00020000, 0x20000080,
317 0x20820080, 0x00020080, 0x20000000, 0x00000000, 0x00820000,
318 0x20800080, 0x20020080, 0x20020000, 0x00800080, 0x20820000,
319 0x00000080, 0x00800080, 0x20020000, 0x20820080, 0x00800000,
320 0x20800000, 0x20000080, 0x00820000, 0x00020080, 0x20020080,
321 0x20800000, 0x00000080, 0x20820000, 0x00820080, 0x00000000,
322 0x20000000, 0x20800080, 0x00020000, 0x00820080, }};
323
324 #define HPERM_OP(a, t, n, m) \
325 ((t) = ((((a) << (16 - (n))) ^ (a)) & (m)), \
326 (a) = (a) ^ (t) ^ ((t) >> (16 - (n))))
327
328 void DES_set_key_unchecked(const DES_cblock *key, DES_key_schedule *schedule) {
329 DES_set_key_ex(key->bytes, schedule);
330 }
331
332 void DES_set_key_ex(const uint8_t key[8], DES_key_schedule *schedule) {
333 static const int shifts2[16] = {0, 0, 1, 1, 1, 1, 1, 1,
334 0, 1, 1, 1, 1, 1, 1, 0};
335 uint32_t c, d, t, s, t2;
336 const uint8_t *in;
337 int i;
338
339 in = key;
340
341 c2l(in, c);
342 c2l(in, d);
343
344 // do PC1 in 47 simple operations :-)
345 // Thanks to John Fletcher ([email protected])
346 // for the inspiration. :-)
347 PERM_OP(d, c, t, 4, 0x0f0f0f0f);
348 HPERM_OP(c, t, -2, 0xcccc0000);
349 HPERM_OP(d, t, -2, 0xcccc0000);
350 PERM_OP(d, c, t, 1, 0x55555555);
351 PERM_OP(c, d, t, 8, 0x00ff00ff);
352 PERM_OP(d, c, t, 1, 0x55555555);
353 d = (((d & 0x000000ff) << 16) | (d & 0x0000ff00) |
354 ((d & 0x00ff0000) >> 16) | ((c & 0xf0000000) >> 4));
355 c &= 0x0fffffff;
356
357 for (i = 0; i < ITERATIONS; i++) {
358 if (shifts2[i]) {
359 c = ((c >> 2) | (c << 26));
360 d = ((d >> 2) | (d << 26));
361 } else {
362 c = ((c >> 1) | (c << 27));
363 d = ((d >> 1) | (d << 27));
364 }
365 c &= 0x0fffffff;
366 d &= 0x0fffffff;
367 // could be a few less shifts but I am to lazy at this
368 // point in time to investigate
369 s = des_skb[0][(c) & 0x3f] |
370 des_skb[1][((c >> 6) & 0x03) | ((c >> 7) & 0x3c)] |
371 des_skb[2][((c >> 13) & 0x0f) | ((c >> 14) & 0x30)] |
372 des_skb[3][((c >> 20) & 0x01) | ((c >> 21) & 0x06) |
373 ((c >> 22) & 0x38)];
374 t = des_skb[4][(d) & 0x3f] |
375 des_skb[5][((d >> 7) & 0x03) | ((d >> 8) & 0x3c)] |
376 des_skb[6][(d >> 15) & 0x3f] |
377 des_skb[7][((d >> 21) & 0x0f) | ((d >> 22) & 0x30)];
378
379 // table contained 0213 4657
380 t2 = ((t << 16) | (s & 0x0000ffff)) & 0xffffffff;
381 schedule->subkeys[i][0] = CRYPTO_rotr_u32(t2, 30);
382
383 t2 = ((s >> 16) | (t & 0xffff0000));
384 schedule->subkeys[i][1] = CRYPTO_rotr_u32(t2, 26);
385 }
386 }
387
388 // SP 800-67r2 section 2, the last bit of each byte in DES_cblock.bytes is used
389 // for parity. The parity bits should be set to the complement of the modulo 2
390 // sum of the previous seven bits
391 static int DES_check_key_parity(const DES_cblock *key) {
392 uint8_t result = UINT8_MAX;
393
394 for (size_t i = 0; i < DES_KEY_SZ; i++) {
395 uint8_t b = key->bytes[i];
396 b ^= b >> 4;
397 b ^= b >> 2;
398 b ^= b >> 1;
399 result &= constant_time_eq_8(b & 1, 1);
400 }
401 return result & 1;
402 }
403
404 int DES_set_key(const DES_cblock *key, DES_key_schedule *schedule)
405 {
406 int result = 0;
407
408 if (!DES_check_key_parity(key)) {
409 result = -1;
410 }
411 if (DES_is_weak_key(key)) {
412 result = -2;
413 }
414 DES_set_key_unchecked(key, schedule);
415 return result;
416 }
417
418 int DES_key_sched(const DES_cblock *key, DES_key_schedule *schedule) {
419 return DES_set_key(key, schedule);
420 }
421
422 static const uint8_t kOddParity[256] = {
423 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14,
424 14, 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28,
425 31, 31, 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44,
426 44, 47, 47, 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59,
427 61, 61, 62, 62, 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74,
428 74, 76, 76, 79, 79, 81, 81, 82, 82, 84, 84, 87, 87, 88, 88,
429 91, 91, 93, 93, 94, 94, 97, 97, 98, 98, 100, 100, 103, 103, 104,
430 104, 107, 107, 109, 109, 110, 110, 112, 112, 115, 115, 117, 117, 118, 118,
431 121, 121, 122, 122, 124, 124, 127, 127, 128, 128, 131, 131, 133, 133, 134,
432 134, 137, 137, 138, 138, 140, 140, 143, 143, 145, 145, 146, 146, 148, 148,
433 151, 151, 152, 152, 155, 155, 157, 157, 158, 158, 161, 161, 162, 162, 164,
434 164, 167, 167, 168, 168, 171, 171, 173, 173, 174, 174, 176, 176, 179, 179,
435 181, 181, 182, 182, 185, 185, 186, 186, 188, 188, 191, 191, 193, 193, 194,
436 194, 196, 196, 199, 199, 200, 200, 203, 203, 205, 205, 206, 206, 208, 208,
437 211, 211, 213, 213, 214, 214, 217, 217, 218, 218, 220, 220, 223, 223, 224,
438 224, 227, 227, 229, 229, 230, 230, 233, 233, 234, 234, 236, 236, 239, 239,
439 241, 241, 242, 242, 244, 244, 247, 247, 248, 248, 251, 251, 253, 253, 254,
440 254
441 };
442
443 void DES_set_odd_parity(DES_cblock *key) {
444 unsigned i;
445
446 for (i = 0; i < DES_KEY_SZ; i++) {
447 key->bytes[i] = kOddParity[key->bytes[i]];
448 }
449 }
450
451 // Weak keys have unintended behaviors which may hurt the security of their use
452 // see SP 800-67r2 section 3.3.2
453 static const DES_cblock weak_keys[] = {
454 // Weak keys: encryption is equal to decryption (encrypting twice produces the original plaintext)
455 {{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}},
456 {{0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE}},
457 {{0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E}},
458 {{0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1}},
459 // Semi-weak keys: encryption with one of these keys is equal to encryption with a different key
460 {{0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE}},
461 {{0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01}},
462 {{0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1}},
463 {{0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E}},
464 {{0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1}},
465 {{0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01}},
466 {{0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE}},
467 {{0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E}},
468 {{0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E}},
469 {{0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01}},
470 {{0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE}},
471 {{0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1}}
472 };
473
474 int DES_is_weak_key(const DES_cblock *key)
475 {
476 crypto_word_t result = 0;
477 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(weak_keys); i++) {
478 int match = CRYPTO_memcmp(&weak_keys[i], key, sizeof(DES_cblock));
479 result |= constant_time_is_zero_w(match);
480 }
481 return (int)(result & 1);
482 }
483
484 static void DES_encrypt1(uint32_t data[2], const DES_key_schedule *ks,
485 int enc) {
486 uint32_t l, r, t, u;
487
488 r = data[0];
489 l = data[1];
490
491 IP(r, l);
492 // Things have been modified so that the initial rotate is done outside
493 // the loop. This required the DES_SPtrans values in sp.h to be
494 // rotated 1 bit to the right. One perl script later and things have a
495 // 5% speed up on a sparc2. Thanks to Richard Outerbridge
496 // <[email protected]> for pointing this out.
497 // clear the top bits on machines with 8byte longs
498 // shift left by 2
499 r = CRYPTO_rotr_u32(r, 29);
500 l = CRYPTO_rotr_u32(l, 29);
501
502 // I don't know if it is worth the effort of loop unrolling the
503 // inner loop
504 if (enc) {
505 D_ENCRYPT(ks, l, r, 0);
506 D_ENCRYPT(ks, r, l, 1);
507 D_ENCRYPT(ks, l, r, 2);
508 D_ENCRYPT(ks, r, l, 3);
509 D_ENCRYPT(ks, l, r, 4);
510 D_ENCRYPT(ks, r, l, 5);
511 D_ENCRYPT(ks, l, r, 6);
512 D_ENCRYPT(ks, r, l, 7);
513 D_ENCRYPT(ks, l, r, 8);
514 D_ENCRYPT(ks, r, l, 9);
515 D_ENCRYPT(ks, l, r, 10);
516 D_ENCRYPT(ks, r, l, 11);
517 D_ENCRYPT(ks, l, r, 12);
518 D_ENCRYPT(ks, r, l, 13);
519 D_ENCRYPT(ks, l, r, 14);
520 D_ENCRYPT(ks, r, l, 15);
521 } else {
522 D_ENCRYPT(ks, l, r, 15);
523 D_ENCRYPT(ks, r, l, 14);
524 D_ENCRYPT(ks, l, r, 13);
525 D_ENCRYPT(ks, r, l, 12);
526 D_ENCRYPT(ks, l, r, 11);
527 D_ENCRYPT(ks, r, l, 10);
528 D_ENCRYPT(ks, l, r, 9);
529 D_ENCRYPT(ks, r, l, 8);
530 D_ENCRYPT(ks, l, r, 7);
531 D_ENCRYPT(ks, r, l, 6);
532 D_ENCRYPT(ks, l, r, 5);
533 D_ENCRYPT(ks, r, l, 4);
534 D_ENCRYPT(ks, l, r, 3);
535 D_ENCRYPT(ks, r, l, 2);
536 D_ENCRYPT(ks, l, r, 1);
537 D_ENCRYPT(ks, r, l, 0);
538 }
539
540 // rotate and clear the top bits on machines with 8byte longs
541 l = CRYPTO_rotr_u32(l, 3);
542 r = CRYPTO_rotr_u32(r, 3);
543
544 FP(r, l);
545 data[0] = l;
546 data[1] = r;
547 }
548
549 static void DES_encrypt2(uint32_t data[2], const DES_key_schedule *ks,
550 int enc) {
551 uint32_t l, r, t, u;
552
553 r = data[0];
554 l = data[1];
555
556 // Things have been modified so that the initial rotate is done outside the
557 // loop. This required the DES_SPtrans values in sp.h to be rotated 1 bit to
558 // the right. One perl script later and things have a 5% speed up on a
559 // sparc2. Thanks to Richard Outerbridge <[email protected]> for
560 // pointing this out.
561 // clear the top bits on machines with 8byte longs
562 r = CRYPTO_rotr_u32(r, 29);
563 l = CRYPTO_rotr_u32(l, 29);
564
565 // I don't know if it is worth the effort of loop unrolling the
566 // inner loop
567 if (enc) {
568 D_ENCRYPT(ks, l, r, 0);
569 D_ENCRYPT(ks, r, l, 1);
570 D_ENCRYPT(ks, l, r, 2);
571 D_ENCRYPT(ks, r, l, 3);
572 D_ENCRYPT(ks, l, r, 4);
573 D_ENCRYPT(ks, r, l, 5);
574 D_ENCRYPT(ks, l, r, 6);
575 D_ENCRYPT(ks, r, l, 7);
576 D_ENCRYPT(ks, l, r, 8);
577 D_ENCRYPT(ks, r, l, 9);
578 D_ENCRYPT(ks, l, r, 10);
579 D_ENCRYPT(ks, r, l, 11);
580 D_ENCRYPT(ks, l, r, 12);
581 D_ENCRYPT(ks, r, l, 13);
582 D_ENCRYPT(ks, l, r, 14);
583 D_ENCRYPT(ks, r, l, 15);
584 } else {
585 D_ENCRYPT(ks, l, r, 15);
586 D_ENCRYPT(ks, r, l, 14);
587 D_ENCRYPT(ks, l, r, 13);
588 D_ENCRYPT(ks, r, l, 12);
589 D_ENCRYPT(ks, l, r, 11);
590 D_ENCRYPT(ks, r, l, 10);
591 D_ENCRYPT(ks, l, r, 9);
592 D_ENCRYPT(ks, r, l, 8);
593 D_ENCRYPT(ks, l, r, 7);
594 D_ENCRYPT(ks, r, l, 6);
595 D_ENCRYPT(ks, l, r, 5);
596 D_ENCRYPT(ks, r, l, 4);
597 D_ENCRYPT(ks, l, r, 3);
598 D_ENCRYPT(ks, r, l, 2);
599 D_ENCRYPT(ks, l, r, 1);
600 D_ENCRYPT(ks, r, l, 0);
601 }
602 // rotate and clear the top bits on machines with 8byte longs
603 data[0] = CRYPTO_rotr_u32(l, 3);
604 data[1] = CRYPTO_rotr_u32(r, 3);
605 }
606
607 void DES_encrypt3(uint32_t data[2], const DES_key_schedule *ks1,
608 const DES_key_schedule *ks2, const DES_key_schedule *ks3) {
609 uint32_t l, r;
610
611 l = data[0];
612 r = data[1];
613 IP(l, r);
614 data[0] = l;
615 data[1] = r;
616 DES_encrypt2(data, ks1, DES_ENCRYPT);
617 DES_encrypt2(data, ks2, DES_DECRYPT);
618 DES_encrypt2(data, ks3, DES_ENCRYPT);
619 l = data[0];
620 r = data[1];
621 FP(r, l);
622 data[0] = l;
623 data[1] = r;
624 }
625
626 void DES_decrypt3(uint32_t data[2], const DES_key_schedule *ks1,
627 const DES_key_schedule *ks2, const DES_key_schedule *ks3) {
628 uint32_t l, r;
629
630 l = data[0];
631 r = data[1];
632 IP(l, r);
633 data[0] = l;
634 data[1] = r;
635 DES_encrypt2(data, ks3, DES_DECRYPT);
636 DES_encrypt2(data, ks2, DES_ENCRYPT);
637 DES_encrypt2(data, ks1, DES_DECRYPT);
638 l = data[0];
639 r = data[1];
640 FP(r, l);
641 data[0] = l;
642 data[1] = r;
643 }
644
645 void DES_ecb_encrypt(const DES_cblock *in_block, DES_cblock *out_block,
646 const DES_key_schedule *schedule, int is_encrypt) {
647 DES_ecb_encrypt_ex(in_block->bytes, out_block->bytes, schedule, is_encrypt);
648 }
649
650 void DES_ecb_encrypt_ex(const uint8_t in[8], uint8_t out[8],
651 const DES_key_schedule *schedule, int is_encrypt) {
652 uint32_t ll[2];
653 ll[0] = CRYPTO_load_u32_le(in);
654 ll[1] = CRYPTO_load_u32_le(in + 4);
655 DES_encrypt1(ll, schedule, is_encrypt);
656 CRYPTO_store_u32_le(out, ll[0]);
657 CRYPTO_store_u32_le(out + 4, ll[1]);
658 }
659
660 void DES_ncbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
661 const DES_key_schedule *schedule, DES_cblock *ivec,
662 int enc) {
663 DES_ncbc_encrypt_ex(in, out, len, schedule, ivec->bytes, enc);
664 }
665
666 void DES_ncbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len,
667 const DES_key_schedule *schedule, uint8_t ivec[8],
668 int enc) {
669 uint32_t tin0, tin1;
670 uint32_t tout0, tout1, xor0, xor1;
671 uint32_t tin[2];
672 unsigned char *iv;
673
674 iv = ivec;
675
676 if (enc) {
677 c2l(iv, tout0);
678 c2l(iv, tout1);
679 for (; len >= 8; len -= 8) {
680 c2l(in, tin0);
681 c2l(in, tin1);
682 tin0 ^= tout0;
683 tin[0] = tin0;
684 tin1 ^= tout1;
685 tin[1] = tin1;
686 DES_encrypt1(tin, schedule, DES_ENCRYPT);
687 tout0 = tin[0];
688 l2c(tout0, out);
689 tout1 = tin[1];
690 l2c(tout1, out);
691 }
692 if (len != 0) {
693 c2ln(in, tin0, tin1, len);
694 tin0 ^= tout0;
695 tin[0] = tin0;
696 tin1 ^= tout1;
697 tin[1] = tin1;
698 DES_encrypt1(tin, schedule, DES_ENCRYPT);
699 tout0 = tin[0];
700 l2c(tout0, out);
701 tout1 = tin[1];
702 l2c(tout1, out);
703 }
704 iv = ivec;
705 l2c(tout0, iv);
706 l2c(tout1, iv);
707 } else {
708 c2l(iv, xor0);
709 c2l(iv, xor1);
710 for (; len >= 8; len -= 8) {
711 c2l(in, tin0);
712 tin[0] = tin0;
713 c2l(in, tin1);
714 tin[1] = tin1;
715 DES_encrypt1(tin, schedule, DES_DECRYPT);
716 tout0 = tin[0] ^ xor0;
717 tout1 = tin[1] ^ xor1;
718 l2c(tout0, out);
719 l2c(tout1, out);
720 xor0 = tin0;
721 xor1 = tin1;
722 }
723 if (len != 0) {
724 c2l(in, tin0);
725 tin[0] = tin0;
726 c2l(in, tin1);
727 tin[1] = tin1;
728 DES_encrypt1(tin, schedule, DES_DECRYPT);
729 tout0 = tin[0] ^ xor0;
730 tout1 = tin[1] ^ xor1;
731 l2cn(tout0, tout1, out, len);
732 xor0 = tin0;
733 xor1 = tin1;
734 }
735 iv = ivec;
736 l2c(xor0, iv);
737 l2c(xor1, iv);
738 }
739 tin[0] = tin[1] = 0;
740 }
741
742 void DES_ecb3_encrypt(const DES_cblock *input, DES_cblock *output,
743 const DES_key_schedule *ks1, const DES_key_schedule *ks2,
744 const DES_key_schedule *ks3, int enc) {
745 DES_ecb3_encrypt_ex(input->bytes, output->bytes, ks1, ks2, ks3, enc);
746 }
747
748 void DES_ecb3_encrypt_ex(const uint8_t in[8], uint8_t out[8],
749 const DES_key_schedule *ks1,
750 const DES_key_schedule *ks2,
751 const DES_key_schedule *ks3, int enc) {
752 uint32_t ll[2];
753 ll[0] = CRYPTO_load_u32_le(in);
754 ll[1] = CRYPTO_load_u32_le(in + 4);
755 if (enc) {
756 DES_encrypt3(ll, ks1, ks2, ks3);
757 } else {
758 DES_decrypt3(ll, ks1, ks2, ks3);
759 }
760 CRYPTO_store_u32_le(out, ll[0]);
761 CRYPTO_store_u32_le(out + 4, ll[1]);
762 }
763
764 void DES_ede3_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
765 const DES_key_schedule *ks1,
766 const DES_key_schedule *ks2,
767 const DES_key_schedule *ks3, DES_cblock *ivec,
768 int enc) {
769 DES_ede3_cbc_encrypt_ex(in, out, len, ks1, ks2, ks3, ivec->bytes, enc);
770 }
771
772 void DES_ede3_cbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len,
773 const DES_key_schedule *ks1,
774 const DES_key_schedule *ks2,
775 const DES_key_schedule *ks3, uint8_t ivec[8],
776 int enc) {
777 uint32_t tin0, tin1;
778 uint32_t tout0, tout1, xor0, xor1;
779 uint32_t tin[2];
780 uint8_t *iv;
781
782 iv = ivec;
783
784 if (enc) {
785 c2l(iv, tout0);
786 c2l(iv, tout1);
787 for (; len >= 8; len -= 8) {
788 c2l(in, tin0);
789 c2l(in, tin1);
790 tin0 ^= tout0;
791 tin1 ^= tout1;
792
793 tin[0] = tin0;
794 tin[1] = tin1;
795 DES_encrypt3(tin, ks1, ks2, ks3);
796 tout0 = tin[0];
797 tout1 = tin[1];
798
799 l2c(tout0, out);
800 l2c(tout1, out);
801 }
802 if (len != 0) {
803 c2ln(in, tin0, tin1, len);
804 tin0 ^= tout0;
805 tin1 ^= tout1;
806
807 tin[0] = tin0;
808 tin[1] = tin1;
809 DES_encrypt3(tin, ks1, ks2, ks3);
810 tout0 = tin[0];
811 tout1 = tin[1];
812
813 l2c(tout0, out);
814 l2c(tout1, out);
815 }
816 iv = ivec;
817 l2c(tout0, iv);
818 l2c(tout1, iv);
819 } else {
820 uint32_t t0, t1;
821
822 c2l(iv, xor0);
823 c2l(iv, xor1);
824 for (; len >= 8; len -= 8) {
825 c2l(in, tin0);
826 c2l(in, tin1);
827
828 t0 = tin0;
829 t1 = tin1;
830
831 tin[0] = tin0;
832 tin[1] = tin1;
833 DES_decrypt3(tin, ks1, ks2, ks3);
834 tout0 = tin[0];
835 tout1 = tin[1];
836
837 tout0 ^= xor0;
838 tout1 ^= xor1;
839 l2c(tout0, out);
840 l2c(tout1, out);
841 xor0 = t0;
842 xor1 = t1;
843 }
844 if (len != 0) {
845 c2l(in, tin0);
846 c2l(in, tin1);
847
848 t0 = tin0;
849 t1 = tin1;
850
851 tin[0] = tin0;
852 tin[1] = tin1;
853 DES_decrypt3(tin, ks1, ks2, ks3);
854 tout0 = tin[0];
855 tout1 = tin[1];
856
857 tout0 ^= xor0;
858 tout1 ^= xor1;
859 l2cn(tout0, tout1, out, len);
860 xor0 = t0;
861 xor1 = t1;
862 }
863
864 iv = ivec;
865 l2c(xor0, iv);
866 l2c(xor1, iv);
867 }
868
869 tin[0] = tin[1] = 0;
870 }
871
872 void DES_ede2_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
873 const DES_key_schedule *ks1,
874 const DES_key_schedule *ks2,
875 DES_cblock *ivec,
876 int enc) {
877 DES_ede3_cbc_encrypt(in, out, len, ks1, ks2, ks1, ivec, enc);
878 }