]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/e_des.c
2 * Copyright 1995-2021 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
11 * DES low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #ifndef OPENSSL_NO_DES
19 # include <openssl/evp.h>
20 # include <openssl/objects.h>
21 # include "crypto/evp.h"
22 # include <openssl/des.h>
23 # include <openssl/rand.h>
24 # include "evp_local.h"
32 void (*cbc
) (const void *, void *, size_t,
33 const DES_key_schedule
*, unsigned char *);
37 # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
38 /* ----------^^^ this is not a typo, just a way to detect that
39 * assembler support was in general requested... */
40 # include "crypto/sparc_arch.h"
42 # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES)
44 void des_t4_key_expand(const void *key
, DES_key_schedule
*ks
);
45 void des_t4_cbc_encrypt(const void *inp
, void *out
, size_t len
,
46 const DES_key_schedule
*ks
, unsigned char iv
[8]);
47 void des_t4_cbc_decrypt(const void *inp
, void *out
, size_t len
,
48 const DES_key_schedule
*ks
, unsigned char iv
[8]);
51 static int des_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
52 const unsigned char *iv
, int enc
);
53 static int des_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
);
56 * Because of various casts and different names can't use
57 * IMPLEMENT_BLOCK_CIPHER
60 static int des_ecb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
61 const unsigned char *in
, size_t inl
)
63 BLOCK_CIPHER_ecb_loop()
64 DES_ecb_encrypt((DES_cblock
*)(in
+ i
), (DES_cblock
*)(out
+ i
),
65 EVP_CIPHER_CTX_get_cipher_data(ctx
),
66 EVP_CIPHER_CTX_is_encrypting(ctx
));
70 static int des_ofb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
71 const unsigned char *in
, size_t inl
)
73 while (inl
>= EVP_MAXCHUNK
) {
74 int num
= EVP_CIPHER_CTX_get_num(ctx
);
75 DES_ofb64_encrypt(in
, out
, (long)EVP_MAXCHUNK
,
76 EVP_CIPHER_CTX_get_cipher_data(ctx
),
77 (DES_cblock
*)ctx
->iv
, &num
);
78 EVP_CIPHER_CTX_set_num(ctx
, num
);
84 int num
= EVP_CIPHER_CTX_get_num(ctx
);
85 DES_ofb64_encrypt(in
, out
, (long)inl
,
86 EVP_CIPHER_CTX_get_cipher_data(ctx
),
87 (DES_cblock
*)ctx
->iv
, &num
);
88 EVP_CIPHER_CTX_set_num(ctx
, num
);
93 static int des_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
94 const unsigned char *in
, size_t inl
)
96 EVP_DES_KEY
*dat
= (EVP_DES_KEY
*) EVP_CIPHER_CTX_get_cipher_data(ctx
);
98 if (dat
->stream
.cbc
!= NULL
) {
99 (*dat
->stream
.cbc
) (in
, out
, inl
, &dat
->ks
.ks
, ctx
->iv
);
102 while (inl
>= EVP_MAXCHUNK
) {
103 DES_ncbc_encrypt(in
, out
, (long)EVP_MAXCHUNK
,
104 EVP_CIPHER_CTX_get_cipher_data(ctx
),
105 (DES_cblock
*)ctx
->iv
,
106 EVP_CIPHER_CTX_is_encrypting(ctx
));
112 DES_ncbc_encrypt(in
, out
, (long)inl
,
113 EVP_CIPHER_CTX_get_cipher_data(ctx
),
114 (DES_cblock
*)ctx
->iv
,
115 EVP_CIPHER_CTX_is_encrypting(ctx
));
119 static int des_cfb64_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
120 const unsigned char *in
, size_t inl
)
122 while (inl
>= EVP_MAXCHUNK
) {
123 int num
= EVP_CIPHER_CTX_get_num(ctx
);
124 DES_cfb64_encrypt(in
, out
, (long)EVP_MAXCHUNK
,
125 EVP_CIPHER_CTX_get_cipher_data(ctx
),
126 (DES_cblock
*)ctx
->iv
, &num
,
127 EVP_CIPHER_CTX_is_encrypting(ctx
));
128 EVP_CIPHER_CTX_set_num(ctx
, num
);
134 int num
= EVP_CIPHER_CTX_get_num(ctx
);
135 DES_cfb64_encrypt(in
, out
, (long)inl
,
136 EVP_CIPHER_CTX_get_cipher_data(ctx
),
137 (DES_cblock
*)ctx
->iv
, &num
,
138 EVP_CIPHER_CTX_is_encrypting(ctx
));
139 EVP_CIPHER_CTX_set_num(ctx
, num
);
145 * Although we have a CFB-r implementation for DES, it doesn't pack the right
146 * way, so wrap it here
148 static int des_cfb1_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
149 const unsigned char *in
, size_t inl
)
151 size_t n
, chunk
= EVP_MAXCHUNK
/ 8;
153 unsigned char d
[1] = { 0 }; /* Appease Coverity */
158 while (inl
&& inl
>= chunk
) {
159 for (n
= 0; n
< chunk
* 8; ++n
) {
160 c
[0] = (in
[n
/ 8] & (1 << (7 - n
% 8))) ? 0x80 : 0;
161 DES_cfb_encrypt(c
, d
, 1, 1, EVP_CIPHER_CTX_get_cipher_data(ctx
),
162 (DES_cblock
*)ctx
->iv
,
163 EVP_CIPHER_CTX_is_encrypting(ctx
));
165 (out
[n
/ 8] & ~(0x80 >> (unsigned int)(n
% 8))) |
166 ((d
[0] & 0x80) >> (unsigned int)(n
% 8));
178 static int des_cfb8_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
179 const unsigned char *in
, size_t inl
)
181 while (inl
>= EVP_MAXCHUNK
) {
182 DES_cfb_encrypt(in
, out
, 8, (long)EVP_MAXCHUNK
,
183 EVP_CIPHER_CTX_get_cipher_data(ctx
),
184 (DES_cblock
*)ctx
->iv
,
185 EVP_CIPHER_CTX_is_encrypting(ctx
));
191 DES_cfb_encrypt(in
, out
, 8, (long)inl
,
192 EVP_CIPHER_CTX_get_cipher_data(ctx
),
193 (DES_cblock
*)ctx
->iv
,
194 EVP_CIPHER_CTX_is_encrypting(ctx
));
198 BLOCK_CIPHER_defs(des
, EVP_DES_KEY
, NID_des
, 8, 8, 8, 64,
199 EVP_CIPH_RAND_KEY
, des_init_key
, NULL
,
200 EVP_CIPHER_set_asn1_iv
, EVP_CIPHER_get_asn1_iv
, des_ctrl
)
202 BLOCK_CIPHER_def_cfb(des
, EVP_DES_KEY
, NID_des
, 8, 8, 1,
203 EVP_CIPH_RAND_KEY
, des_init_key
, NULL
,
204 EVP_CIPHER_set_asn1_iv
, EVP_CIPHER_get_asn1_iv
, des_ctrl
)
206 BLOCK_CIPHER_def_cfb(des
, EVP_DES_KEY
, NID_des
, 8, 8, 8,
207 EVP_CIPH_RAND_KEY
, des_init_key
, NULL
,
208 EVP_CIPHER_set_asn1_iv
, EVP_CIPHER_get_asn1_iv
, des_ctrl
)
210 static int des_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
211 const unsigned char *iv
, int enc
)
213 DES_cblock
*deskey
= (DES_cblock
*)key
;
214 EVP_DES_KEY
*dat
= (EVP_DES_KEY
*) EVP_CIPHER_CTX_get_cipher_data(ctx
);
216 dat
->stream
.cbc
= NULL
;
217 # if defined(SPARC_DES_CAPABLE)
218 if (SPARC_DES_CAPABLE
) {
219 int mode
= EVP_CIPHER_CTX_get_mode(ctx
);
221 if (mode
== EVP_CIPH_CBC_MODE
) {
222 des_t4_key_expand(key
, &dat
->ks
.ks
);
223 dat
->stream
.cbc
= enc
? des_t4_cbc_encrypt
: des_t4_cbc_decrypt
;
228 DES_set_key_unchecked(deskey
, EVP_CIPHER_CTX_get_cipher_data(ctx
));
232 static int des_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
236 case EVP_CTRL_RAND_KEY
:
237 if (RAND_priv_bytes(ptr
, 8) <= 0)
239 DES_set_odd_parity((DES_cblock
*)ptr
);