Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / providers / implementations / ciphers / cipher_tdes.h
1 /*
2  * Copyright 2019 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_numbers.h>
12 #include "crypto/des_platform.h"
13
14 #define DES_BLOCK_SIZE 8
15 #define TDES_IVLEN 8
16
17 /* TODO(3.0) Figure out what flags need to be here */
18 #define TDES_FLAGS (EVP_CIPH_RAND_KEY)
19
20 typedef struct prov_tdes_ctx_st {
21     PROV_CIPHER_CTX base;      /* Must be first */
22     union {
23         OSSL_UNION_ALIGN;
24         DES_key_schedule ks[3];
25     } tks;
26     union {
27         void (*cbc) (const void *, void *, size_t,
28                      const DES_key_schedule *, unsigned char *);
29     } tstream;
30
31 } PROV_TDES_CTX;
32
33 #define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags,             \
34                               kbits, blkbits, ivbits, block)                   \
35 static OSSL_OP_cipher_newctx_fn tdes_##type##_##lcmode##_newctx;               \
36 static void *tdes_##type##_##lcmode##_newctx(void *provctx)                    \
37 {                                                                              \
38     return tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits,      \
39                        ivbits, flags, PROV_CIPHER_HW_tdes_##type##_##lcmode());\
40 }                                                                              \
41 static OSSL_OP_cipher_get_params_fn tdes_##type##_##lcmode##_get_params;       \
42 static int tdes_##type##_##lcmode##_get_params(OSSL_PARAM params[])            \
43 {                                                                              \
44     return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags,  \
45                                      kbits, blkbits, ivbits);                  \
46 }                                                                              \
47 const OSSL_DISPATCH tdes_##type##_##lcmode##_functions[] = {                   \
48     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))tdes_einit },             \
49     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))tdes_dinit },             \
50     { OSSL_FUNC_CIPHER_UPDATE,                                                 \
51       (void (*)(void))cipher_generic_##block##_update },                       \
52     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##block##_final },\
53     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher },        \
54     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
55       (void (*)(void))tdes_##type##_##lcmode##_newctx },                       \
56     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))tdes_freectx },                \
57     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
58       (void (*)(void))tdes_##type##_##lcmode##_get_params },                   \
59     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
60       (void (*)(void))cipher_generic_gettable_params },                        \
61     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))tdes_get_ctx_params },  \
62     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
63       (void (*)(void))tdes_gettable_ctx_params },                              \
64     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
65      (void (*)(void))cipher_generic_set_ctx_params },                          \
66     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
67      (void (*)(void))cipher_generic_settable_ctx_params },                     \
68     { 0, NULL }                                                                \
69 }
70
71 void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
72                   size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw);
73 OSSL_OP_cipher_freectx_fn tdes_freectx;
74 OSSL_OP_cipher_encrypt_init_fn tdes_einit;
75 OSSL_OP_cipher_decrypt_init_fn tdes_dinit;
76 OSSL_OP_cipher_get_ctx_params_fn tdes_get_ctx_params;
77 OSSL_OP_cipher_gettable_ctx_params_fn tdes_gettable_ctx_params;
78
79 #define PROV_CIPHER_HW_tdes_mode(type, mode)                                   \
80 static const PROV_CIPHER_HW type##_##mode = {                                  \
81     cipher_hw_tdes_##type##_initkey,                                           \
82     cipher_hw_tdes_##mode                                                      \
83 };                                                                             \
84 const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_##type##_##mode(void)                \
85 {                                                                              \
86     return &type##_##mode;                                                     \
87 }
88
89 int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key,
90                                 size_t keylen);
91 int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
92                        const unsigned char *in, size_t inl);
93 int cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
94                        const unsigned char *in, size_t len);
95
96 const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_cbc(void);
97 const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_ecb(void);