]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
39147079 | 2 | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
c6fec81b P |
10 | /* |
11 | * DES low level APIs are deprecated for public use, but still ok for internal | |
12 | * use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
9e9e8cb6 | 18 | #ifndef OPENSSL_NO_DES |
0f113f3e MC |
19 | # include <openssl/evp.h> |
20 | # include <openssl/objects.h> | |
25f2138b | 21 | # include "crypto/evp.h" |
0f113f3e MC |
22 | # include <openssl/des.h> |
23 | # include <openssl/rand.h> | |
d02b48c6 | 24 | |
0f113f3e MC |
25 | typedef struct { |
26 | union { | |
39147079 | 27 | OSSL_UNION_ALIGN; |
0f113f3e MC |
28 | DES_key_schedule ks; |
29 | } ks; | |
30 | union { | |
7687f525 AP |
31 | void (*cbc) (const void *, void *, size_t, |
32 | const DES_key_schedule *, unsigned char *); | |
0f113f3e MC |
33 | } stream; |
34 | } EVP_DES_KEY; | |
c5d975a7 | 35 | |
0f113f3e | 36 | # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__)) |
7687f525 | 37 | /* ----------^^^ this is not a typo, just a way to detect that |
c5d975a7 | 38 | * assembler support was in general requested... */ |
0f113f3e | 39 | # include "sparc_arch.h" |
c5d975a7 AP |
40 | |
41 | extern unsigned int OPENSSL_sparcv9cap_P[]; | |
42 | ||
0f113f3e | 43 | # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES) |
c5d975a7 | 44 | |
0f113f3e MC |
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, | |
7687f525 | 47 | const DES_key_schedule *ks, unsigned char iv[8]); |
0f113f3e | 48 | void des_t4_cbc_decrypt(const void *inp, void *out, size_t len, |
7687f525 | 49 | const DES_key_schedule *ks, unsigned char iv[8]); |
0f113f3e | 50 | # endif |
c5d975a7 | 51 | |
1921eaad | 52 | static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e | 53 | const unsigned char *iv, int enc); |
216659eb | 54 | static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); |
d02b48c6 | 55 | |
0f113f3e MC |
56 | /* |
57 | * Because of various casts and different names can't use | |
58 | * IMPLEMENT_BLOCK_CIPHER | |
59 | */ | |
edf0bfb5 | 60 | |
1921eaad | 61 | static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 62 | const unsigned char *in, size_t inl) |
57ae2e24 | 63 | { |
0f113f3e MC |
64 | BLOCK_CIPHER_ecb_loop() |
65 | DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), | |
44ab2dfd | 66 | EVP_CIPHER_CTX_get_cipher_data(ctx), |
6435f0f6 | 67 | EVP_CIPHER_CTX_encrypting(ctx)); |
0f113f3e | 68 | return 1; |
57ae2e24 | 69 | } |
d02b48c6 | 70 | |
1921eaad | 71 | static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 72 | const unsigned char *in, size_t inl) |
57ae2e24 | 73 | { |
0f113f3e | 74 | while (inl >= EVP_MAXCHUNK) { |
6435f0f6 RL |
75 | int num = EVP_CIPHER_CTX_num(ctx); |
76 | DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, | |
44ab2dfd | 77 | EVP_CIPHER_CTX_get_cipher_data(ctx), |
6435f0f6 RL |
78 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num); |
79 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e MC |
80 | inl -= EVP_MAXCHUNK; |
81 | in += EVP_MAXCHUNK; | |
82 | out += EVP_MAXCHUNK; | |
83 | } | |
6435f0f6 RL |
84 | if (inl) { |
85 | int num = EVP_CIPHER_CTX_num(ctx); | |
44ab2dfd MC |
86 | DES_ofb64_encrypt(in, out, (long)inl, |
87 | EVP_CIPHER_CTX_get_cipher_data(ctx), | |
6435f0f6 RL |
88 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num); |
89 | EVP_CIPHER_CTX_set_num(ctx, num); | |
90 | } | |
0f113f3e | 91 | return 1; |
57ae2e24 DSH |
92 | } |
93 | ||
1921eaad | 94 | static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 95 | const unsigned char *in, size_t inl) |
57ae2e24 | 96 | { |
44ab2dfd | 97 | EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx); |
c5d975a7 | 98 | |
7687f525 AP |
99 | if (dat->stream.cbc != NULL) { |
100 | (*dat->stream.cbc) (in, out, inl, &dat->ks.ks, | |
101 | EVP_CIPHER_CTX_iv_noconst(ctx)); | |
0f113f3e MC |
102 | return 1; |
103 | } | |
104 | while (inl >= EVP_MAXCHUNK) { | |
7687f525 | 105 | DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, |
44ab2dfd | 106 | EVP_CIPHER_CTX_get_cipher_data(ctx), |
7687f525 AP |
107 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), |
108 | EVP_CIPHER_CTX_encrypting(ctx)); | |
0f113f3e MC |
109 | inl -= EVP_MAXCHUNK; |
110 | in += EVP_MAXCHUNK; | |
111 | out += EVP_MAXCHUNK; | |
112 | } | |
113 | if (inl) | |
44ab2dfd MC |
114 | DES_ncbc_encrypt(in, out, (long)inl, |
115 | EVP_CIPHER_CTX_get_cipher_data(ctx), | |
7687f525 AP |
116 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), |
117 | EVP_CIPHER_CTX_encrypting(ctx)); | |
0f113f3e | 118 | return 1; |
57ae2e24 DSH |
119 | } |
120 | ||
8c6336b0 | 121 | static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e | 122 | const unsigned char *in, size_t inl) |
57ae2e24 | 123 | { |
0f113f3e | 124 | while (inl >= EVP_MAXCHUNK) { |
6435f0f6 RL |
125 | int num = EVP_CIPHER_CTX_num(ctx); |
126 | DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, | |
44ab2dfd | 127 | EVP_CIPHER_CTX_get_cipher_data(ctx), |
6435f0f6 RL |
128 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num, |
129 | EVP_CIPHER_CTX_encrypting(ctx)); | |
130 | EVP_CIPHER_CTX_set_num(ctx, num); | |
0f113f3e MC |
131 | inl -= EVP_MAXCHUNK; |
132 | in += EVP_MAXCHUNK; | |
133 | out += EVP_MAXCHUNK; | |
134 | } | |
6435f0f6 RL |
135 | if (inl) { |
136 | int num = EVP_CIPHER_CTX_num(ctx); | |
44ab2dfd MC |
137 | DES_cfb64_encrypt(in, out, (long)inl, |
138 | EVP_CIPHER_CTX_get_cipher_data(ctx), | |
6435f0f6 RL |
139 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), &num, |
140 | EVP_CIPHER_CTX_encrypting(ctx)); | |
141 | EVP_CIPHER_CTX_set_num(ctx, num); | |
142 | } | |
0f113f3e | 143 | return 1; |
57ae2e24 DSH |
144 | } |
145 | ||
0f113f3e MC |
146 | /* |
147 | * Although we have a CFB-r implementation for DES, it doesn't pack the right | |
148 | * way, so wrap it here | |
149 | */ | |
8c6336b0 | 150 | static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e MC |
151 | const unsigned char *in, size_t inl) |
152 | { | |
153 | size_t n, chunk = EVP_MAXCHUNK / 8; | |
154 | unsigned char c[1], d[1]; | |
8c6336b0 | 155 | |
0f113f3e MC |
156 | if (inl < chunk) |
157 | chunk = inl; | |
b444ac3e | 158 | |
0f113f3e MC |
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; | |
44ab2dfd | 162 | DES_cfb_encrypt(c, d, 1, 1, EVP_CIPHER_CTX_get_cipher_data(ctx), |
7687f525 AP |
163 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), |
164 | EVP_CIPHER_CTX_encrypting(ctx)); | |
0f113f3e MC |
165 | out[n / 8] = |
166 | (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) | | |
167 | ((d[0] & 0x80) >> (unsigned int)(n % 8)); | |
168 | } | |
169 | inl -= chunk; | |
170 | in += chunk; | |
171 | out += chunk; | |
172 | if (inl < chunk) | |
173 | chunk = inl; | |
174 | } | |
b444ac3e | 175 | |
8c6336b0 | 176 | return 1; |
0f113f3e | 177 | } |
8c6336b0 AP |
178 | |
179 | static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |
0f113f3e MC |
180 | const unsigned char *in, size_t inl) |
181 | { | |
182 | while (inl >= EVP_MAXCHUNK) { | |
7687f525 | 183 | DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, |
44ab2dfd | 184 | EVP_CIPHER_CTX_get_cipher_data(ctx), |
7687f525 AP |
185 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), |
186 | EVP_CIPHER_CTX_encrypting(ctx)); | |
0f113f3e MC |
187 | inl -= EVP_MAXCHUNK; |
188 | in += EVP_MAXCHUNK; | |
189 | out += EVP_MAXCHUNK; | |
190 | } | |
b444ac3e | 191 | if (inl) |
44ab2dfd MC |
192 | DES_cfb_encrypt(in, out, 8, (long)inl, |
193 | EVP_CIPHER_CTX_get_cipher_data(ctx), | |
7687f525 AP |
194 | (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx), |
195 | EVP_CIPHER_CTX_encrypting(ctx)); | |
8c6336b0 | 196 | return 1; |
0f113f3e | 197 | } |
8c6336b0 | 198 | |
c5d975a7 | 199 | BLOCK_CIPHER_defs(des, EVP_DES_KEY, NID_des, 8, 8, 8, 64, |
0f113f3e MC |
200 | EVP_CIPH_RAND_KEY, des_init_key, NULL, |
201 | EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl) | |
d02b48c6 | 202 | |
0f113f3e MC |
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) | |
8c6336b0 | 206 | |
0f113f3e MC |
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) | |
edf0bfb5 | 210 | |
1921eaad | 211 | static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
212 | const unsigned char *iv, int enc) |
213 | { | |
214 | DES_cblock *deskey = (DES_cblock *)key; | |
44ab2dfd | 215 | EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx); |
c5d975a7 | 216 | |
0f113f3e MC |
217 | dat->stream.cbc = NULL; |
218 | # if defined(SPARC_DES_CAPABLE) | |
219 | if (SPARC_DES_CAPABLE) { | |
ecdd0ff7 | 220 | int mode = EVP_CIPHER_CTX_mode(ctx); |
c5d975a7 | 221 | |
0f113f3e MC |
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; | |
225 | return 1; | |
226 | } | |
227 | } | |
228 | # endif | |
44ab2dfd | 229 | DES_set_key_unchecked(deskey, EVP_CIPHER_CTX_get_cipher_data(ctx)); |
0f113f3e MC |
230 | return 1; |
231 | } | |
57ae2e24 | 232 | |
216659eb | 233 | static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
0f113f3e MC |
234 | { |
235 | ||
236 | switch (type) { | |
237 | case EVP_CTRL_RAND_KEY: | |
4cffafe9 | 238 | if (RAND_priv_bytes(ptr, 8) <= 0) |
0f113f3e MC |
239 | return 0; |
240 | DES_set_odd_parity((DES_cblock *)ptr); | |
241 | return 1; | |
216659eb | 242 | |
0f113f3e MC |
243 | default: |
244 | return -1; | |
245 | } | |
246 | } | |
216659eb | 247 | |
d02f751c | 248 | #endif |