Clean away unnecessary length related OSSL_PARAM key names
[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 # include "evp_local.h"
25
26 typedef struct {
27     union {
28         OSSL_UNION_ALIGN;
29         DES_key_schedule ks;
30     } ks;
31     union {
32         void (*cbc) (const void *, void *, size_t,
33                      const DES_key_schedule *, unsigned char *);
34     } stream;
35 } EVP_DES_KEY;
36
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 "sparc_arch.h"
41
42 extern unsigned int OPENSSL_sparcv9cap_P[];
43
44 #  define SPARC_DES_CAPABLE       (OPENSSL_sparcv9cap_P[1] & CFR_DES)
45
46 void des_t4_key_expand(const void *key, DES_key_schedule *ks);
47 void des_t4_cbc_encrypt(const void *inp, void *out, size_t len,
48                         const DES_key_schedule *ks, unsigned char iv[8]);
49 void des_t4_cbc_decrypt(const void *inp, void *out, size_t len,
50                         const DES_key_schedule *ks, unsigned char iv[8]);
51 # endif
52
53 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
54                         const unsigned char *iv, int enc);
55 static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
56
57 /*
58  * Because of various casts and different names can't use
59  * IMPLEMENT_BLOCK_CIPHER
60  */
61
62 static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
63                           const unsigned char *in, size_t inl)
64 {
65     BLOCK_CIPHER_ecb_loop()
66         DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i),
67                         EVP_CIPHER_CTX_get_cipher_data(ctx),
68                         EVP_CIPHER_CTX_encrypting(ctx));
69     return 1;
70 }
71
72 static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
73                           const unsigned char *in, size_t inl)
74 {
75     while (inl >= EVP_MAXCHUNK) {
76         int num = EVP_CIPHER_CTX_num(ctx);
77         DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK,
78                           EVP_CIPHER_CTX_get_cipher_data(ctx),
79                           (DES_cblock *)ctx->iv, &num);
80         EVP_CIPHER_CTX_set_num(ctx, num);
81         inl -= EVP_MAXCHUNK;
82         in += EVP_MAXCHUNK;
83         out += EVP_MAXCHUNK;
84     }
85     if (inl) {
86         int num = EVP_CIPHER_CTX_num(ctx);
87         DES_ofb64_encrypt(in, out, (long)inl,
88                           EVP_CIPHER_CTX_get_cipher_data(ctx),
89                           (DES_cblock *)ctx->iv, &num);
90         EVP_CIPHER_CTX_set_num(ctx, num);
91     }
92     return 1;
93 }
94
95 static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
96                           const unsigned char *in, size_t inl)
97 {
98     EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx);
99
100     if (dat->stream.cbc != NULL) {
101         (*dat->stream.cbc) (in, out, inl, &dat->ks.ks, ctx->iv);
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 *)ctx->iv,
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 *)ctx->iv,
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 *)ctx->iv, &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 *)ctx->iv, &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 *)ctx->iv,
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 *)ctx->iv,
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 *)ctx->iv,
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