]>
Commit | Line | Data |
---|---|---|
4a42e264 | 1 | /* |
33388b44 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
4a42e264 SL |
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> | |
23c48d94 | 11 | #include <openssl/core_dispatch.h> |
cc731bc3 | 12 | #include "crypto/des_platform.h" |
4a42e264 SL |
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 */ | |
6ef81d38 | 18 | #define TDES_FLAGS (EVP_CIPH_RAND_KEY) |
4a42e264 SL |
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 | ||
abfc73f3 | 33 | #define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \ |
4a42e264 | 34 | kbits, blkbits, ivbits, block) \ |
363b1e5d | 35 | static OSSL_FUNC_cipher_newctx_fn tdes_##type##_##lcmode##_newctx; \ |
4a42e264 SL |
36 | static void *tdes_##type##_##lcmode##_newctx(void *provctx) \ |
37 | { \ | |
38 | return tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits, \ | |
55c7dc79 | 39 | ivbits, flags, PROV_CIPHER_HW_tdes_##type##_##lcmode());\ |
4a42e264 | 40 | } \ |
363b1e5d | 41 | static OSSL_FUNC_cipher_get_params_fn tdes_##type##_##lcmode##_get_params; \ |
4a42e264 SL |
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 }, \ | |
abfc73f3 | 56 | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))tdes_dupctx }, \ |
4a42e264 SL |
57 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))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))cipher_generic_gettable_params }, \ | |
62 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))tdes_get_ctx_params }, \ | |
63 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ | |
64 | (void (*)(void))tdes_gettable_ctx_params }, \ | |
65 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ | |
66 | (void (*)(void))cipher_generic_set_ctx_params }, \ | |
67 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ | |
68 | (void (*)(void))cipher_generic_settable_ctx_params }, \ | |
69 | { 0, NULL } \ | |
70 | } | |
71 | ||
72 | void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits, | |
55c7dc79 | 73 | size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw); |
363b1e5d DMSP |
74 | OSSL_FUNC_cipher_dupctx_fn tdes_dupctx; |
75 | OSSL_FUNC_cipher_freectx_fn tdes_freectx; | |
76 | OSSL_FUNC_cipher_encrypt_init_fn tdes_einit; | |
77 | OSSL_FUNC_cipher_decrypt_init_fn tdes_dinit; | |
78 | OSSL_FUNC_cipher_get_ctx_params_fn tdes_get_ctx_params; | |
79 | OSSL_FUNC_cipher_gettable_ctx_params_fn tdes_gettable_ctx_params; | |
4a42e264 | 80 | |
abfc73f3 | 81 | #define PROV_CIPHER_HW_tdes_mode(type, mode) \ |
4a42e264 SL |
82 | static const PROV_CIPHER_HW type##_##mode = { \ |
83 | cipher_hw_tdes_##type##_initkey, \ | |
abfc73f3 PS |
84 | cipher_hw_tdes_##mode, \ |
85 | cipher_hw_tdes_copyctx \ | |
4a42e264 SL |
86 | }; \ |
87 | const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_##type##_##mode(void) \ | |
88 | { \ | |
89 | return &type##_##mode; \ | |
90 | } | |
91 | ||
92 | int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key, | |
93 | size_t keylen); | |
abfc73f3 | 94 | void cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src); |
4a42e264 SL |
95 | int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out, |
96 | const unsigned char *in, size_t inl); | |
97 | int cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out, | |
98 | const unsigned char *in, size_t len); | |
99 | ||
100 | const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_cbc(void); | |
101 | const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_ecb(void); |