Fix gcc-7 warnings.
[openssl.git] / crypto / des / des_enc.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/crypto.h>
11 #include "des_locl.h"
12 #include "spr.h"
13
14 void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
15 {
16     register DES_LONG l, r, t, u;
17     register DES_LONG *s;
18
19     r = data[0];
20     l = data[1];
21
22     IP(r, l);
23     /*
24      * Things have been modified so that the initial rotate is done outside
25      * the loop.  This required the DES_SPtrans values in sp.h to be rotated
26      * 1 bit to the right. One perl script later and things have a 5% speed
27      * up on a sparc2. Thanks to Richard Outerbridge
28      * <71755.204@CompuServe.COM> for pointing this out.
29      */
30     /* clear the top bits on machines with 8byte longs */
31     /* shift left by 2 */
32     r = ROTATE(r, 29) & 0xffffffffL;
33     l = ROTATE(l, 29) & 0xffffffffL;
34
35     s = ks->ks->deslong;
36     /*
37      * I don't know if it is worth the effort of loop unrolling the inner
38      * loop
39      */
40     if (enc) {
41         D_ENCRYPT(l, r, 0);     /* 1 */
42         D_ENCRYPT(r, l, 2);     /* 2 */
43         D_ENCRYPT(l, r, 4);     /* 3 */
44         D_ENCRYPT(r, l, 6);     /* 4 */
45         D_ENCRYPT(l, r, 8);     /* 5 */
46         D_ENCRYPT(r, l, 10);    /* 6 */
47         D_ENCRYPT(l, r, 12);    /* 7 */
48         D_ENCRYPT(r, l, 14);    /* 8 */
49         D_ENCRYPT(l, r, 16);    /* 9 */
50         D_ENCRYPT(r, l, 18);    /* 10 */
51         D_ENCRYPT(l, r, 20);    /* 11 */
52         D_ENCRYPT(r, l, 22);    /* 12 */
53         D_ENCRYPT(l, r, 24);    /* 13 */
54         D_ENCRYPT(r, l, 26);    /* 14 */
55         D_ENCRYPT(l, r, 28);    /* 15 */
56         D_ENCRYPT(r, l, 30);    /* 16 */
57     } else {
58         D_ENCRYPT(l, r, 30);    /* 16 */
59         D_ENCRYPT(r, l, 28);    /* 15 */
60         D_ENCRYPT(l, r, 26);    /* 14 */
61         D_ENCRYPT(r, l, 24);    /* 13 */
62         D_ENCRYPT(l, r, 22);    /* 12 */
63         D_ENCRYPT(r, l, 20);    /* 11 */
64         D_ENCRYPT(l, r, 18);    /* 10 */
65         D_ENCRYPT(r, l, 16);    /* 9 */
66         D_ENCRYPT(l, r, 14);    /* 8 */
67         D_ENCRYPT(r, l, 12);    /* 7 */
68         D_ENCRYPT(l, r, 10);    /* 6 */
69         D_ENCRYPT(r, l, 8);     /* 5 */
70         D_ENCRYPT(l, r, 6);     /* 4 */
71         D_ENCRYPT(r, l, 4);     /* 3 */
72         D_ENCRYPT(l, r, 2);     /* 2 */
73         D_ENCRYPT(r, l, 0);     /* 1 */
74     }
75
76     /* rotate and clear the top bits on machines with 8byte longs */
77     l = ROTATE(l, 3) & 0xffffffffL;
78     r = ROTATE(r, 3) & 0xffffffffL;
79
80     FP(r, l);
81     data[0] = l;
82     data[1] = r;
83     l = r = t = u = 0;
84 }
85
86 void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
87 {
88     register DES_LONG l, r, t, u;
89     register DES_LONG *s;
90
91     r = data[0];
92     l = data[1];
93
94     /*
95      * Things have been modified so that the initial rotate is done outside
96      * the loop.  This required the DES_SPtrans values in sp.h to be rotated
97      * 1 bit to the right. One perl script later and things have a 5% speed
98      * up on a sparc2. Thanks to Richard Outerbridge
99      * <71755.204@CompuServe.COM> for pointing this out.
100      */
101     /* clear the top bits on machines with 8byte longs */
102     r = ROTATE(r, 29) & 0xffffffffL;
103     l = ROTATE(l, 29) & 0xffffffffL;
104
105     s = ks->ks->deslong;
106     /*
107      * I don't know if it is worth the effort of loop unrolling the inner
108      * loop
109      */
110     if (enc) {
111         D_ENCRYPT(l, r, 0);     /* 1 */
112         D_ENCRYPT(r, l, 2);     /* 2 */
113         D_ENCRYPT(l, r, 4);     /* 3 */
114         D_ENCRYPT(r, l, 6);     /* 4 */
115         D_ENCRYPT(l, r, 8);     /* 5 */
116         D_ENCRYPT(r, l, 10);    /* 6 */
117         D_ENCRYPT(l, r, 12);    /* 7 */
118         D_ENCRYPT(r, l, 14);    /* 8 */
119         D_ENCRYPT(l, r, 16);    /* 9 */
120         D_ENCRYPT(r, l, 18);    /* 10 */
121         D_ENCRYPT(l, r, 20);    /* 11 */
122         D_ENCRYPT(r, l, 22);    /* 12 */
123         D_ENCRYPT(l, r, 24);    /* 13 */
124         D_ENCRYPT(r, l, 26);    /* 14 */
125         D_ENCRYPT(l, r, 28);    /* 15 */
126         D_ENCRYPT(r, l, 30);    /* 16 */
127     } else {
128         D_ENCRYPT(l, r, 30);    /* 16 */
129         D_ENCRYPT(r, l, 28);    /* 15 */
130         D_ENCRYPT(l, r, 26);    /* 14 */
131         D_ENCRYPT(r, l, 24);    /* 13 */
132         D_ENCRYPT(l, r, 22);    /* 12 */
133         D_ENCRYPT(r, l, 20);    /* 11 */
134         D_ENCRYPT(l, r, 18);    /* 10 */
135         D_ENCRYPT(r, l, 16);    /* 9 */
136         D_ENCRYPT(l, r, 14);    /* 8 */
137         D_ENCRYPT(r, l, 12);    /* 7 */
138         D_ENCRYPT(l, r, 10);    /* 6 */
139         D_ENCRYPT(r, l, 8);     /* 5 */
140         D_ENCRYPT(l, r, 6);     /* 4 */
141         D_ENCRYPT(r, l, 4);     /* 3 */
142         D_ENCRYPT(l, r, 2);     /* 2 */
143         D_ENCRYPT(r, l, 0);     /* 1 */
144     }
145     /* rotate and clear the top bits on machines with 8byte longs */
146     data[0] = ROTATE(l, 3) & 0xffffffffL;
147     data[1] = ROTATE(r, 3) & 0xffffffffL;
148     l = r = t = u = 0;
149 }
150
151 void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
152                   DES_key_schedule *ks2, DES_key_schedule *ks3)
153 {
154     register DES_LONG l, r;
155
156     l = data[0];
157     r = data[1];
158     IP(l, r);
159     data[0] = l;
160     data[1] = r;
161     DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT);
162     DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT);
163     DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT);
164     l = data[0];
165     r = data[1];
166     FP(r, l);
167     data[0] = l;
168     data[1] = r;
169 }
170
171 void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
172                   DES_key_schedule *ks2, DES_key_schedule *ks3)
173 {
174     register DES_LONG l, r;
175
176     l = data[0];
177     r = data[1];
178     IP(l, r);
179     data[0] = l;
180     data[1] = r;
181     DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT);
182     DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT);
183     DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT);
184     l = data[0];
185     r = data[1];
186     FP(r, l);
187     data[0] = l;
188     data[1] = r;
189 }
190
191 #ifndef DES_DEFAULT_OPTIONS
192
193 # undef CBC_ENC_C__DONT_UPDATE_IV
194 # include "ncbc_enc.c"          /* DES_ncbc_encrypt */
195
196 void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
197                           long length, DES_key_schedule *ks1,
198                           DES_key_schedule *ks2, DES_key_schedule *ks3,
199                           DES_cblock *ivec, int enc)
200 {
201     register DES_LONG tin0, tin1;
202     register DES_LONG tout0, tout1, xor0, xor1;
203     register const unsigned char *in;
204     unsigned char *out;
205     register long l = length;
206     DES_LONG tin[2];
207     unsigned char *iv;
208
209     in = input;
210     out = output;
211     iv = &(*ivec)[0];
212
213     if (enc) {
214         c2l(iv, tout0);
215         c2l(iv, tout1);
216         for (l -= 8; l >= 0; l -= 8) {
217             c2l(in, tin0);
218             c2l(in, tin1);
219             tin0 ^= tout0;
220             tin1 ^= tout1;
221
222             tin[0] = tin0;
223             tin[1] = tin1;
224             DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
225             tout0 = tin[0];
226             tout1 = tin[1];
227
228             l2c(tout0, out);
229             l2c(tout1, out);
230         }
231         if (l != -8) {
232             c2ln(in, tin0, tin1, l + 8);
233             tin0 ^= tout0;
234             tin1 ^= tout1;
235
236             tin[0] = tin0;
237             tin[1] = tin1;
238             DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
239             tout0 = tin[0];
240             tout1 = tin[1];
241
242             l2c(tout0, out);
243             l2c(tout1, out);
244         }
245         iv = &(*ivec)[0];
246         l2c(tout0, iv);
247         l2c(tout1, iv);
248     } else {
249         register DES_LONG t0, t1;
250
251         c2l(iv, xor0);
252         c2l(iv, xor1);
253         for (l -= 8; l >= 0; l -= 8) {
254             c2l(in, tin0);
255             c2l(in, tin1);
256
257             t0 = tin0;
258             t1 = tin1;
259
260             tin[0] = tin0;
261             tin[1] = tin1;
262             DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
263             tout0 = tin[0];
264             tout1 = tin[1];
265
266             tout0 ^= xor0;
267             tout1 ^= xor1;
268             l2c(tout0, out);
269             l2c(tout1, out);
270             xor0 = t0;
271             xor1 = t1;
272         }
273         if (l != -8) {
274             c2l(in, tin0);
275             c2l(in, tin1);
276
277             t0 = tin0;
278             t1 = tin1;
279
280             tin[0] = tin0;
281             tin[1] = tin1;
282             DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
283             tout0 = tin[0];
284             tout1 = tin[1];
285
286             tout0 ^= xor0;
287             tout1 ^= xor1;
288             l2cn(tout0, tout1, out, l + 8);
289             xor0 = t0;
290             xor1 = t1;
291         }
292
293         iv = &(*ivec)[0];
294         l2c(xor0, iv);
295         l2c(xor1, iv);
296     }
297     tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
298     tin[0] = tin[1] = 0;
299 }
300
301 #endif                          /* DES_DEFAULT_OPTIONS */