1 /*
2  * Copyright 2019-2024 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 #include <openssl/des.h>
11 #include <openssl/core_dispatch.h>
12 #include "prov/securitycheck.h"
13 #include "crypto/des_platform.h"
14 
15 #define DES_BLOCK_SIZE 8
16 #define TDES_IVLEN 8
17 #define TDES_FLAGS PROV_CIPHER_FLAG_RAND_KEY
18 
19 typedef struct prov_tdes_ctx_st {
20     PROV_CIPHER_CTX base;      /* Must be first */
21     union {
22         OSSL_UNION_ALIGN;
23         DES_key_schedule ks[3];
24     } tks;
25     union {
26         void (*cbc) (const void *, void *, size_t,
27                      const DES_key_schedule *, unsigned char *);
28     } tstream;
29     OSSL_FIPS_IND_DECLARE
30 
31 } PROV_TDES_CTX;
32 
33 #define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags,             \
34                               kbits, blkbits, ivbits, block)                   \
35 static OSSL_FUNC_cipher_newctx_fn tdes_##type##_##lcmode##_newctx;             \
36 static void *tdes_##type##_##lcmode##_newctx(void *provctx)                    \
37 {                                                                              \
38     return ossl_tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits, \
39                        ivbits, flags,                                          \
40                        ossl_prov_cipher_hw_tdes_##type##_##lcmode());          \
41 }                                                                              \
42 static OSSL_FUNC_cipher_get_params_fn tdes_##type##_##lcmode##_get_params;     \
43 static int tdes_##type##_##lcmode##_get_params(OSSL_PARAM params[])            \
44 {                                                                              \
45     return ossl_tdes_get_params(params, EVP_CIPH_##UCMODE##_MODE,              \
46                                 flags, kbits, blkbits, ivbits);                \
47 }                                                                              \
48 const OSSL_DISPATCH ossl_tdes_##type##_##lcmode##_functions[] = {              \
49     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_tdes_einit },        \
50     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_tdes_dinit },        \
51     { OSSL_FUNC_CIPHER_UPDATE,                                                 \
52       (void (*)(void))ossl_cipher_generic_##block##_update },                  \
53     { OSSL_FUNC_CIPHER_FINAL,                                                  \
54       (void (*)(void))ossl_cipher_generic_##block##_final },                   \
55     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },   \
56     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
57       (void (*)(void))tdes_##type##_##lcmode##_newctx },                       \
58     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))ossl_tdes_dupctx },             \
59     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))ossl_tdes_freectx },           \
60     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
61       (void (*)(void))tdes_##type##_##lcmode##_get_params },                   \
62     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
63       (void (*)(void))ossl_cipher_generic_gettable_params },                   \
64     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
65       (void (*)(void))ossl_tdes_get_ctx_params },                              \
66     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
67       (void (*)(void))ossl_tdes_gettable_ctx_params },                         \
68     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
69       (void (*)(void))ossl_tdes_set_ctx_params },                              \
70     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
71       (void (*)(void))ossl_tdes_settable_ctx_params },                         \
72     OSSL_DISPATCH_END                                                          \
73 }
74 
75 void *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
76                        size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw);
77 int ossl_tdes_get_params(OSSL_PARAM params[], unsigned int md, uint64_t flags,
78                          size_t kbits, size_t blkbits, size_t ivbits);
79 
80 OSSL_FUNC_cipher_dupctx_fn ossl_tdes_dupctx;
81 OSSL_FUNC_cipher_freectx_fn ossl_tdes_freectx;
82 OSSL_FUNC_cipher_encrypt_init_fn ossl_tdes_einit;
83 OSSL_FUNC_cipher_decrypt_init_fn ossl_tdes_dinit;
84 OSSL_FUNC_cipher_get_ctx_params_fn ossl_tdes_get_ctx_params;
85 OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_tdes_gettable_ctx_params;
86 OSSL_FUNC_cipher_set_ctx_params_fn ossl_tdes_set_ctx_params;
87 OSSL_FUNC_cipher_settable_ctx_params_fn ossl_tdes_settable_ctx_params;
88 
89 #define PROV_CIPHER_HW_tdes_mode(type, mode)                                   \
90 static const PROV_CIPHER_HW type##_##mode = {                                  \
91     ossl_cipher_hw_tdes_##type##_initkey,                                      \
92     ossl_cipher_hw_tdes_##mode,                                                \
93     ossl_cipher_hw_tdes_copyctx                                                \
94 };                                                                             \
95 const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_##type##_##mode(void)           \
96 {                                                                              \
97     return &type##_##mode;                                                     \
98 }
99 
100 int ossl_cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx,
101                                      const unsigned char *key, size_t keylen);
102 void ossl_cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst,
103                                  const PROV_CIPHER_CTX *src);
104 int ossl_cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
105                             const unsigned char *in, size_t inl);
106 int ossl_cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
107                             const unsigned char *in, size_t len);
108 
109 const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_cbc(void);
110 const PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_ecb(void);
111