xref: /dragonfly/crypto/libressl/crypto/evp/e_des.c (revision 961e30ea7dc61d1112b778ea4981eac68129fb86)
1 /* $OpenBSD: e_des.c,v 1.19 2022/09/15 07:04:19 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <limits.h>
60 #include <stdio.h>
61 
62 #include <openssl/opensslconf.h>
63 
64 #ifndef OPENSSL_NO_DES
65 
66 #include <openssl/evp.h>
67 #include <openssl/des.h>
68 #include <openssl/objects.h>
69 
70 #include "evp_locl.h"
71 
72 static int
des_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)73 des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
74     const unsigned char *iv, int enc)
75 {
76           DES_cblock *deskey = (DES_cblock *)key;
77 
78           DES_set_key_unchecked(deskey, ctx->cipher_data);
79           return 1;
80 }
81 
82 static int
des_ctrl(EVP_CIPHER_CTX * c,int type,int arg,void * ptr)83 des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
84 {
85           switch (type) {
86           case EVP_CTRL_RAND_KEY:
87                     if (DES_random_key((DES_cblock *)ptr) == 0)
88                               return 0;
89                     return 1;
90 
91           default:
92                     return -1;
93           }
94 }
95 
96 static int
des_ecb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)97 des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
98     const unsigned char *in, size_t inl)
99 {
100           size_t i, bl;
101 
102           bl = ctx->cipher->block_size;
103 
104           if (inl < bl)
105                     return 1;
106 
107           inl -= bl;
108 
109           for (i = 0; i <= inl; i += bl)
110                     DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i),
111                         ctx->cipher_data, ctx->encrypt);
112 
113           return 1;
114 }
115 
116 static int
des_ofb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)117 des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
118     const unsigned char *in, size_t inl)
119 {
120           size_t chunk = LONG_MAX & ~0xff;
121 
122           while (inl >= chunk) {
123                     DES_ofb64_encrypt(in, out, (long)chunk, ctx->cipher_data,
124                         (DES_cblock *)ctx->iv, &ctx->num);
125                     inl -= chunk;
126                     in += chunk;
127                     out += chunk;
128           }
129           if (inl)
130                     DES_ofb64_encrypt(in, out, (long)inl, ctx->cipher_data,
131                         (DES_cblock *)ctx->iv, &ctx->num);
132           return 1;
133 }
134 
135 static int
des_cbc_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)136 des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
137     const unsigned char *in, size_t inl)
138 {
139           size_t chunk = LONG_MAX & ~0xff;
140 
141           while (inl >= chunk) {
142                     DES_ncbc_encrypt(in, out, (long)chunk, ctx->cipher_data,
143                         (DES_cblock *)ctx->iv, ctx->encrypt);
144                     inl -= chunk;
145                     in += chunk;
146                     out += chunk;
147           }
148           if (inl)
149                     DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data,
150                         (DES_cblock *)ctx->iv, ctx->encrypt);
151           return 1;
152 }
153 
154 static int
des_cfb64_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)155 des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
156     const unsigned char *in, size_t inl)
157 {
158           size_t chunk = LONG_MAX & ~0xff;
159 
160           while (inl >= chunk) {
161                     DES_cfb64_encrypt(in, out, (long)chunk, ctx->cipher_data,
162                         (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt);
163                     inl -= chunk;
164                     in += chunk;
165                     out += chunk;
166           }
167           if (inl)
168                     DES_cfb64_encrypt(in, out, (long)inl, ctx->cipher_data,
169                         (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt);
170           return 1;
171 }
172 
173 /* Although we have a CFB-r implementation for DES, it doesn't pack the right
174    way, so wrap it here */
175 static int
des_cfb1_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)176 des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
177     const unsigned char *in, size_t inl)
178 {
179           unsigned char c[1], d[1];
180           size_t chunk = LONG_MAX / 8;
181           size_t n;
182 
183           if (inl < chunk)
184                     chunk = inl;
185 
186           while (inl && inl >= chunk) {
187                     for (n = 0; n < chunk*8; ++n) {
188                               c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
189                               DES_cfb_encrypt(c, d, 1, 1, ctx->cipher_data,
190                                   (DES_cblock *)ctx->iv, ctx->encrypt);
191                               out[n / 8] = (out[n / 8] &
192                                   ~(0x80 >> (unsigned int)(n % 8))) |
193                                   ((d[0] & 0x80) >> (unsigned int)(n % 8));
194                     }
195                     inl -= chunk;
196                     in += chunk;
197                     out += chunk;
198                     if (inl < chunk)
199                               chunk = inl;
200           }
201 
202           return 1;
203 }
204 
205 static int
des_cfb8_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)206 des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
207     const unsigned char *in, size_t inl)
208 {
209           size_t chunk = LONG_MAX & ~0xff;
210 
211           while (inl >= chunk) {
212                     DES_cfb_encrypt(in, out, 8, (long)chunk,
213                         ctx->cipher_data, (DES_cblock *)ctx->iv, ctx->encrypt);
214                     inl -= chunk;
215                     in += chunk;
216                     out += chunk;
217           }
218           if (inl)
219                     DES_cfb_encrypt(in, out, 8, (long)inl, ctx->cipher_data,
220                         (DES_cblock *)ctx->iv, ctx->encrypt);
221           return 1;
222 }
223 
224 static const EVP_CIPHER des_cbc = {
225           .nid = NID_des_cbc,
226           .block_size = 8,
227           .key_len = 8,
228           .iv_len = 8,
229           .flags = EVP_CIPH_RAND_KEY | EVP_CIPH_CBC_MODE,
230           .init = des_init_key,
231           .do_cipher = des_cbc_cipher,
232           .cleanup = NULL,
233           .ctx_size = sizeof(DES_key_schedule),
234           .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
235           .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
236           .ctrl = des_ctrl,
237           .app_data = NULL,
238 };
239 
240 const EVP_CIPHER *
EVP_des_cbc(void)241 EVP_des_cbc(void)
242 {
243           return &des_cbc;
244 }
245 
246 static const EVP_CIPHER des_cfb64 = {
247           .nid = NID_des_cfb64,
248           .block_size = 1,
249           .key_len = 8,
250           .iv_len = 8,
251           .flags = EVP_CIPH_RAND_KEY | EVP_CIPH_CFB_MODE,
252           .init = des_init_key,
253           .do_cipher = des_cfb64_cipher,
254           .cleanup = NULL,
255           .ctx_size = sizeof(DES_key_schedule),
256           .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
257           .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
258           .ctrl = des_ctrl,
259           .app_data = NULL,
260 };
261 
262 const EVP_CIPHER *
EVP_des_cfb64(void)263 EVP_des_cfb64(void)
264 {
265           return &des_cfb64;
266 }
267 
268 static const EVP_CIPHER des_ofb = {
269           .nid = NID_des_ofb64,
270           .block_size = 1,
271           .key_len = 8,
272           .iv_len = 8,
273           .flags = EVP_CIPH_RAND_KEY | EVP_CIPH_OFB_MODE,
274           .init = des_init_key,
275           .do_cipher = des_ofb_cipher,
276           .cleanup = NULL,
277           .ctx_size = sizeof(DES_key_schedule),
278           .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
279           .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
280           .ctrl = des_ctrl,
281           .app_data = NULL,
282 };
283 
284 const EVP_CIPHER *
EVP_des_ofb(void)285 EVP_des_ofb(void)
286 {
287           return &des_ofb;
288 }
289 
290 static const EVP_CIPHER des_ecb = {
291           .nid = NID_des_ecb,
292           .block_size = 8,
293           .key_len = 8,
294           .iv_len = 0,
295           .flags = EVP_CIPH_RAND_KEY | EVP_CIPH_ECB_MODE,
296           .init = des_init_key,
297           .do_cipher = des_ecb_cipher,
298           .cleanup = NULL,
299           .ctx_size = sizeof(DES_key_schedule),
300           .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
301           .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
302           .ctrl = des_ctrl,
303           .app_data = NULL,
304 };
305 
306 const EVP_CIPHER *
EVP_des_ecb(void)307 EVP_des_ecb(void)
308 {
309           return &des_ecb;
310 }
311 
312 static const EVP_CIPHER des_cfb1 = {
313           .nid = NID_des_cfb1,
314           .block_size = 1,
315           .key_len = 8,
316           .iv_len = 8,
317           .flags = EVP_CIPH_RAND_KEY | EVP_CIPH_CFB_MODE,
318           .init = des_init_key,
319           .do_cipher = des_cfb1_cipher,
320           .cleanup = NULL,
321           .ctx_size = sizeof(DES_key_schedule),
322           .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
323           .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
324           .ctrl = des_ctrl,
325           .app_data = NULL,
326 };
327 
328 const EVP_CIPHER *
EVP_des_cfb1(void)329 EVP_des_cfb1(void)
330 {
331           return &des_cfb1;
332 }
333 
334 static const EVP_CIPHER des_cfb8 = {
335           .nid = NID_des_cfb8,
336           .block_size = 1,
337           .key_len = 8,
338           .iv_len = 8,
339           .flags = EVP_CIPH_RAND_KEY | EVP_CIPH_CFB_MODE,
340           .init = des_init_key,
341           .do_cipher = des_cfb8_cipher,
342           .cleanup = NULL,
343           .ctx_size = sizeof(DES_key_schedule),
344           .set_asn1_parameters = EVP_CIPHER_set_asn1_iv,
345           .get_asn1_parameters = EVP_CIPHER_get_asn1_iv,
346           .ctrl = des_ctrl,
347           .app_data = NULL,
348 };
349 
350 const EVP_CIPHER *
EVP_des_cfb8(void)351 EVP_des_cfb8(void)
352 {
353           return &des_cfb8;
354 }
355 #endif
356