2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include <openssl/crypto.h>
11 #include "des_local.h"
14 void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
16 register DES_LONG l, r, t, u;
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 for pointing this out.
29 /* clear the top bits on machines with 8byte longs */
31 r = ROTATE(r, 29) & 0xffffffffL;
32 l = ROTATE(l, 29) & 0xffffffffL;
36 * I don't know if it is worth the effort of loop unrolling the inner
40 D_ENCRYPT(l, r, 0); /* 1 */
41 D_ENCRYPT(r, l, 2); /* 2 */
42 D_ENCRYPT(l, r, 4); /* 3 */
43 D_ENCRYPT(r, l, 6); /* 4 */
44 D_ENCRYPT(l, r, 8); /* 5 */
45 D_ENCRYPT(r, l, 10); /* 6 */
46 D_ENCRYPT(l, r, 12); /* 7 */
47 D_ENCRYPT(r, l, 14); /* 8 */
48 D_ENCRYPT(l, r, 16); /* 9 */
49 D_ENCRYPT(r, l, 18); /* 10 */
50 D_ENCRYPT(l, r, 20); /* 11 */
51 D_ENCRYPT(r, l, 22); /* 12 */
52 D_ENCRYPT(l, r, 24); /* 13 */
53 D_ENCRYPT(r, l, 26); /* 14 */
54 D_ENCRYPT(l, r, 28); /* 15 */
55 D_ENCRYPT(r, l, 30); /* 16 */
57 D_ENCRYPT(l, r, 30); /* 16 */
58 D_ENCRYPT(r, l, 28); /* 15 */
59 D_ENCRYPT(l, r, 26); /* 14 */
60 D_ENCRYPT(r, l, 24); /* 13 */
61 D_ENCRYPT(l, r, 22); /* 12 */
62 D_ENCRYPT(r, l, 20); /* 11 */
63 D_ENCRYPT(l, r, 18); /* 10 */
64 D_ENCRYPT(r, l, 16); /* 9 */
65 D_ENCRYPT(l, r, 14); /* 8 */
66 D_ENCRYPT(r, l, 12); /* 7 */
67 D_ENCRYPT(l, r, 10); /* 6 */
68 D_ENCRYPT(r, l, 8); /* 5 */
69 D_ENCRYPT(l, r, 6); /* 4 */
70 D_ENCRYPT(r, l, 4); /* 3 */
71 D_ENCRYPT(l, r, 2); /* 2 */
72 D_ENCRYPT(r, l, 0); /* 1 */
75 /* rotate and clear the top bits on machines with 8byte longs */
76 l = ROTATE(l, 3) & 0xffffffffL;
77 r = ROTATE(r, 3) & 0xffffffffL;
85 void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
87 register DES_LONG l, r, t, u;
94 * Things have been modified so that the initial rotate is done outside
95 * the loop. This required the DES_SPtrans values in sp.h to be rotated
96 * 1 bit to the right. One perl script later and things have a 5% speed
97 * up on a sparc2. Thanks to Richard Outerbridge for pointing this out.
99 /* clear the top bits on machines with 8byte longs */
100 r = ROTATE(r, 29) & 0xffffffffL;
101 l = ROTATE(l, 29) & 0xffffffffL;
105 * I don't know if it is worth the effort of loop unrolling the inner
109 D_ENCRYPT(l, r, 0); /* 1 */
110 D_ENCRYPT(r, l, 2); /* 2 */
111 D_ENCRYPT(l, r, 4); /* 3 */
112 D_ENCRYPT(r, l, 6); /* 4 */
113 D_ENCRYPT(l, r, 8); /* 5 */
114 D_ENCRYPT(r, l, 10); /* 6 */
115 D_ENCRYPT(l, r, 12); /* 7 */
116 D_ENCRYPT(r, l, 14); /* 8 */
117 D_ENCRYPT(l, r, 16); /* 9 */
118 D_ENCRYPT(r, l, 18); /* 10 */
119 D_ENCRYPT(l, r, 20); /* 11 */
120 D_ENCRYPT(r, l, 22); /* 12 */
121 D_ENCRYPT(l, r, 24); /* 13 */
122 D_ENCRYPT(r, l, 26); /* 14 */
123 D_ENCRYPT(l, r, 28); /* 15 */
124 D_ENCRYPT(r, l, 30); /* 16 */
126 D_ENCRYPT(l, r, 30); /* 16 */
127 D_ENCRYPT(r, l, 28); /* 15 */
128 D_ENCRYPT(l, r, 26); /* 14 */
129 D_ENCRYPT(r, l, 24); /* 13 */
130 D_ENCRYPT(l, r, 22); /* 12 */
131 D_ENCRYPT(r, l, 20); /* 11 */
132 D_ENCRYPT(l, r, 18); /* 10 */
133 D_ENCRYPT(r, l, 16); /* 9 */
134 D_ENCRYPT(l, r, 14); /* 8 */
135 D_ENCRYPT(r, l, 12); /* 7 */
136 D_ENCRYPT(l, r, 10); /* 6 */
137 D_ENCRYPT(r, l, 8); /* 5 */
138 D_ENCRYPT(l, r, 6); /* 4 */
139 D_ENCRYPT(r, l, 4); /* 3 */
140 D_ENCRYPT(l, r, 2); /* 2 */
141 D_ENCRYPT(r, l, 0); /* 1 */
143 /* rotate and clear the top bits on machines with 8byte longs */
144 data[0] = ROTATE(l, 3) & 0xffffffffL;
145 data[1] = ROTATE(r, 3) & 0xffffffffL;
149 void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
150 DES_key_schedule *ks2, DES_key_schedule *ks3)
152 register DES_LONG l, r;
159 DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT);
160 DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT);
161 DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT);
169 void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
170 DES_key_schedule *ks2, DES_key_schedule *ks3)
172 register DES_LONG l, r;
179 DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT);
180 DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT);
181 DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT);
189 #ifndef DES_DEFAULT_OPTIONS
191 # undef CBC_ENC_C__DONT_UPDATE_IV
192 # include "ncbc_enc.c" /* DES_ncbc_encrypt */
194 void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
195 long length, DES_key_schedule *ks1,
196 DES_key_schedule *ks2, DES_key_schedule *ks3,
197 DES_cblock *ivec, int enc)
199 register DES_LONG tin0, tin1;
200 register DES_LONG tout0, tout1, xor0, xor1;
201 register const unsigned char *in;
203 register long l = length;
214 for (l -= 8; l >= 0; l -= 8) {
222 DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
230 c2ln(in, tin0, tin1, l + 8);
236 DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
247 register DES_LONG t0, t1;
251 for (l -= 8; l >= 0; l -= 8) {
260 DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
280 DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
286 l2cn(tout0, tout1, out, l + 8);
295 tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
299 #endif /* DES_DEFAULT_OPTIONS */