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 | |
13 | #define DES_BLOCK_SIZE 8 |
14 | #define TDES_IVLEN 8 |
15 | |
16 | /* TODO(3.0) Figure out what flags need to be here */ |
17 | #define TDES_FLAGS (EVP_CIPH_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 | |
30 | } PROV_TDES_CTX; |
31 | |
32 | #define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \ |
33 | kbits, blkbits, ivbits, block) \ |
34 | static OSSL_OP_cipher_newctx_fn tdes_##type##_##lcmode##_newctx; \ |
35 | static void *tdes_##type##_##lcmode##_newctx(void *provctx) \ |
36 | { \ |
37 | return tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits, \ |
38 | ivbits, flags, PROV_CIPHER_HW_tdes_##type##_##lcmode());\ |
39 | } \ |
40 | static OSSL_OP_cipher_get_params_fn tdes_##type##_##lcmode##_get_params; \ |
41 | static int tdes_##type##_##lcmode##_get_params(OSSL_PARAM params[]) \ |
42 | { \ |
43 | return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \ |
44 | kbits, blkbits, ivbits); \ |
45 | } \ |
46 | const OSSL_DISPATCH tdes_##type##_##lcmode##_functions[] = { \ |
47 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))tdes_einit }, \ |
48 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))tdes_dinit }, \ |
49 | { OSSL_FUNC_CIPHER_UPDATE, \ |
50 | (void (*)(void))cipher_generic_##block##_update }, \ |
51 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##block##_final },\ |
52 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \ |
53 | { OSSL_FUNC_CIPHER_NEWCTX, \ |
54 | (void (*)(void))tdes_##type##_##lcmode##_newctx }, \ |
55 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))tdes_freectx }, \ |
56 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ |
57 | (void (*)(void))tdes_##type##_##lcmode##_get_params }, \ |
58 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ |
59 | (void (*)(void))cipher_generic_gettable_params }, \ |
60 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))tdes_get_ctx_params }, \ |
61 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ |
62 | (void (*)(void))tdes_gettable_ctx_params }, \ |
63 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ |
64 | (void (*)(void))cipher_generic_set_ctx_params }, \ |
65 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ |
66 | (void (*)(void))cipher_generic_settable_ctx_params }, \ |
67 | { 0, NULL } \ |
68 | } |
69 | |
70 | void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits, |
71 | size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw); |
72 | OSSL_OP_cipher_freectx_fn tdes_freectx; |
73 | OSSL_OP_cipher_encrypt_init_fn tdes_einit; |
74 | OSSL_OP_cipher_decrypt_init_fn tdes_dinit; |
75 | OSSL_OP_cipher_get_ctx_params_fn tdes_get_ctx_params; |
76 | OSSL_OP_cipher_gettable_ctx_params_fn tdes_gettable_ctx_params; |
77 | |
78 | #define PROV_CIPHER_HW_tdes_mode(type, mode) \ |
79 | static const PROV_CIPHER_HW type##_##mode = { \ |
80 | cipher_hw_tdes_##type##_initkey, \ |
81 | cipher_hw_tdes_##mode \ |
82 | }; \ |
83 | const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_##type##_##mode(void) \ |
84 | { \ |
85 | return &type##_##mode; \ |
86 | } |
87 | |
88 | int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key, |
89 | size_t keylen); |
90 | int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out, |
91 | const unsigned char *in, size_t inl); |
92 | int cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out, |
93 | const unsigned char *in, size_t len); |
94 | |
95 | const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_cbc(void); |
96 | const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_ecb(void); |
97 | |