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