CTR, HASH and HMAC DRBGs in provider
[openssl.git] / crypto / evp / e_des.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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
16 #include <stdio.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
25 typedef struct {
26     union {
27         OSSL_UNION_ALIGN;
28         DES_key_schedule ks;
29     } ks;
30     union {
31         void (*cbc) (const void *, void *, size_t,
32                      const DES_key_schedule *, unsigned char *);
33     } stream;
34 } EVP_DES_KEY;
35
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"
40
41 extern unsigned int OPENSSL_sparcv9cap_P[];
42
43 #  define SPARC_DES_CAPABLE       (OPENSSL_sparcv9cap_P[1] & CFR_DES)
44
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]);
50 # endif
51
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);
55
56 /*
57  * Because of various casts and different names can't use
58  * IMPLEMENT_BLOCK_CIPHER
59  */
60
61 static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
62                           const unsigned char *in, size_t inl)
63 {
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));
68     return 1;
69 }
70
71 static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
72                           const unsigned char *in, size_t inl)
73 {
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);
80         inl -= EVP_MAXCHUNK;
81         in += EVP_MAXCHUNK;
82         out += EVP_MAXCHUNK;
83     }
84     if (inl) {
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);
90     }
91     return 1;
92 }
93
94 static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
95                           const unsigned char *in, size_t inl)
96 {
97     EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx);
98
99     if (dat->stream.cbc != NULL) {
100         (*dat->stream.cbc) (in, out, inl, &dat->ks.ks,
101                             EVP_CIPHER_CTX_iv_noconst(ctx));
102         return 1;
103     }
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));
109         inl -= EVP_MAXCHUNK;
110         in += EVP_MAXCHUNK;
111         out += EVP_MAXCHUNK;
112     }
113     if (inl)
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));
118     return 1;
119 }
120
121 static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
122                             const unsigned char *in, size_t inl)
123 {
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);
131         inl -= EVP_MAXCHUNK;
132         in += EVP_MAXCHUNK;
133         out += EVP_MAXCHUNK;
134     }
135     if (inl) {
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);
142     }
143     return 1;
144 }
145
146 /*
147  * Although we have a CFB-r implementation for DES, it doesn't pack the right
148  * way, so wrap it here
149  */
150 static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
151                            const unsigned char *in, size_t inl)
152 {
153     size_t n, chunk = EVP_MAXCHUNK / 8;
154     unsigned char c[1], d[1];
155
156     if (inl < chunk)
157         chunk = inl;
158
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));
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     }
175
176     return 1;
177 }
178
179 static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
180                            const unsigned char *in, size_t inl)
181 {
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));
187         inl -= EVP_MAXCHUNK;
188         in += EVP_MAXCHUNK;
189         out += EVP_MAXCHUNK;
190     }
191     if (inl)
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));
196     return 1;
197 }
198
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)
202
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)
206
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)
210
211 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
212                         const unsigned char *iv, int enc)
213 {
214     DES_cblock *deskey = (DES_cblock *)key;
215     EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx);
216
217     dat->stream.cbc = NULL;
218 # if defined(SPARC_DES_CAPABLE)
219     if (SPARC_DES_CAPABLE) {
220         int mode = EVP_CIPHER_CTX_mode(ctx);
221
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
229     DES_set_key_unchecked(deskey, EVP_CIPHER_CTX_get_cipher_data(ctx));
230     return 1;
231 }
232
233 static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
234 {
235
236     switch (type) {
237     case EVP_CTRL_RAND_KEY:
238         if (RAND_priv_bytes(ptr, 8) <= 0)
239             return 0;
240         DES_set_odd_parity((DES_cblock *)ptr);
241         return 1;
242
243     default:
244         return -1;
245     }
246 }
247
248 #endif