]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/e_des.c
9d8af99d92e935056b5462b293cf94b82a4fadaa
2 * Copyright 1995-2020 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>
31 void (*cbc
) (const void *, void *, size_t,
32 const DES_key_schedule
*, unsigned char *);
36 # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
37 /* ----------^^^ this is not a typo, just a way to detect that
38 * assembler support was in general requested... */
39 # include "sparc_arch.h"
41 extern unsigned int OPENSSL_sparcv9cap_P
[];
43 # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES)
45 void des_t4_key_expand(const void *key
, DES_key_schedule
*ks
);
46 void des_t4_cbc_encrypt(const void *inp
, void *out
, size_t len
,
47 const DES_key_schedule
*ks
, unsigned char iv
[8]);
48 void des_t4_cbc_decrypt(const void *inp
, void *out
, size_t len
,
49 const DES_key_schedule
*ks
, unsigned char iv
[8]);
52 static int des_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
53 const unsigned char *iv
, int enc
);
54 static int des_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
);
57 * Because of various casts and different names can't use
58 * IMPLEMENT_BLOCK_CIPHER
61 static int des_ecb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
62 const unsigned char *in
, size_t inl
)
64 BLOCK_CIPHER_ecb_loop()
65 DES_ecb_encrypt((DES_cblock
*)(in
+ i
), (DES_cblock
*)(out
+ i
),
66 EVP_CIPHER_CTX_get_cipher_data(ctx
),
67 EVP_CIPHER_CTX_encrypting(ctx
));
71 static int des_ofb_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
72 const unsigned char *in
, size_t inl
)
74 while (inl
>= EVP_MAXCHUNK
) {
75 int num
= EVP_CIPHER_CTX_num(ctx
);
76 DES_ofb64_encrypt(in
, out
, (long)EVP_MAXCHUNK
,
77 EVP_CIPHER_CTX_get_cipher_data(ctx
),
78 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
), &num
);
79 EVP_CIPHER_CTX_set_num(ctx
, num
);
85 int num
= EVP_CIPHER_CTX_num(ctx
);
86 DES_ofb64_encrypt(in
, out
, (long)inl
,
87 EVP_CIPHER_CTX_get_cipher_data(ctx
),
88 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
), &num
);
89 EVP_CIPHER_CTX_set_num(ctx
, num
);
94 static int des_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
95 const unsigned char *in
, size_t inl
)
97 EVP_DES_KEY
*dat
= (EVP_DES_KEY
*) EVP_CIPHER_CTX_get_cipher_data(ctx
);
99 if (dat
->stream
.cbc
!= NULL
) {
100 (*dat
->stream
.cbc
) (in
, out
, inl
, &dat
->ks
.ks
,
101 EVP_CIPHER_CTX_iv_noconst(ctx
));
104 while (inl
>= EVP_MAXCHUNK
) {
105 DES_ncbc_encrypt(in
, out
, (long)EVP_MAXCHUNK
,
106 EVP_CIPHER_CTX_get_cipher_data(ctx
),
107 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
),
108 EVP_CIPHER_CTX_encrypting(ctx
));
114 DES_ncbc_encrypt(in
, out
, (long)inl
,
115 EVP_CIPHER_CTX_get_cipher_data(ctx
),
116 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
),
117 EVP_CIPHER_CTX_encrypting(ctx
));
121 static int des_cfb64_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
122 const unsigned char *in
, size_t inl
)
124 while (inl
>= EVP_MAXCHUNK
) {
125 int num
= EVP_CIPHER_CTX_num(ctx
);
126 DES_cfb64_encrypt(in
, out
, (long)EVP_MAXCHUNK
,
127 EVP_CIPHER_CTX_get_cipher_data(ctx
),
128 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
), &num
,
129 EVP_CIPHER_CTX_encrypting(ctx
));
130 EVP_CIPHER_CTX_set_num(ctx
, num
);
136 int num
= EVP_CIPHER_CTX_num(ctx
);
137 DES_cfb64_encrypt(in
, out
, (long)inl
,
138 EVP_CIPHER_CTX_get_cipher_data(ctx
),
139 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
), &num
,
140 EVP_CIPHER_CTX_encrypting(ctx
));
141 EVP_CIPHER_CTX_set_num(ctx
, num
);
147 * Although we have a CFB-r implementation for DES, it doesn't pack the right
148 * way, so wrap it here
150 static int des_cfb1_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
151 const unsigned char *in
, size_t inl
)
153 size_t n
, chunk
= EVP_MAXCHUNK
/ 8;
154 unsigned char c
[1], d
[1];
159 while (inl
&& inl
>= chunk
) {
160 for (n
= 0; n
< chunk
* 8; ++n
) {
161 c
[0] = (in
[n
/ 8] & (1 << (7 - n
% 8))) ? 0x80 : 0;
162 DES_cfb_encrypt(c
, d
, 1, 1, EVP_CIPHER_CTX_get_cipher_data(ctx
),
163 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
),
164 EVP_CIPHER_CTX_encrypting(ctx
));
166 (out
[n
/ 8] & ~(0x80 >> (unsigned int)(n
% 8))) |
167 ((d
[0] & 0x80) >> (unsigned int)(n
% 8));
179 static int des_cfb8_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
180 const unsigned char *in
, size_t inl
)
182 while (inl
>= EVP_MAXCHUNK
) {
183 DES_cfb_encrypt(in
, out
, 8, (long)EVP_MAXCHUNK
,
184 EVP_CIPHER_CTX_get_cipher_data(ctx
),
185 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
),
186 EVP_CIPHER_CTX_encrypting(ctx
));
192 DES_cfb_encrypt(in
, out
, 8, (long)inl
,
193 EVP_CIPHER_CTX_get_cipher_data(ctx
),
194 (DES_cblock
*)EVP_CIPHER_CTX_iv_noconst(ctx
),
195 EVP_CIPHER_CTX_encrypting(ctx
));
199 BLOCK_CIPHER_defs(des
, EVP_DES_KEY
, NID_des
, 8, 8, 8, 64,
200 EVP_CIPH_RAND_KEY
, des_init_key
, NULL
,
201 EVP_CIPHER_set_asn1_iv
, EVP_CIPHER_get_asn1_iv
, des_ctrl
)
203 BLOCK_CIPHER_def_cfb(des
, EVP_DES_KEY
, NID_des
, 8, 8, 1,
204 EVP_CIPH_RAND_KEY
, des_init_key
, NULL
,
205 EVP_CIPHER_set_asn1_iv
, EVP_CIPHER_get_asn1_iv
, des_ctrl
)
207 BLOCK_CIPHER_def_cfb(des
, EVP_DES_KEY
, NID_des
, 8, 8, 8,
208 EVP_CIPH_RAND_KEY
, des_init_key
, NULL
,
209 EVP_CIPHER_set_asn1_iv
, EVP_CIPHER_get_asn1_iv
, des_ctrl
)
211 static int des_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
212 const unsigned char *iv
, int enc
)
214 DES_cblock
*deskey
= (DES_cblock
*)key
;
215 EVP_DES_KEY
*dat
= (EVP_DES_KEY
*) EVP_CIPHER_CTX_get_cipher_data(ctx
);
217 dat
->stream
.cbc
= NULL
;
218 # if defined(SPARC_DES_CAPABLE)
219 if (SPARC_DES_CAPABLE
) {
220 int mode
= EVP_CIPHER_CTX_mode(ctx
);
222 if (mode
== EVP_CIPH_CBC_MODE
) {
223 des_t4_key_expand(key
, &dat
->ks
.ks
);
224 dat
->stream
.cbc
= enc
? des_t4_cbc_encrypt
: des_t4_cbc_decrypt
;
229 DES_set_key_unchecked(deskey
, EVP_CIPHER_CTX_get_cipher_data(ctx
));
233 static int des_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
237 case EVP_CTRL_RAND_KEY
:
238 if (RAND_priv_bytes(ptr
, 8) <= 0)
240 DES_set_odd_parity((DES_cblock
*)ptr
);