1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <openssl/des.h> 11e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h> 12e1051a39Sopenharmony_ci#include "crypto/des_platform.h" 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci#define DES_BLOCK_SIZE 8 15e1051a39Sopenharmony_ci#define TDES_IVLEN 8 16e1051a39Sopenharmony_ci#define TDES_FLAGS PROV_CIPHER_FLAG_RAND_KEY 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_citypedef struct prov_tdes_ctx_st { 19e1051a39Sopenharmony_ci PROV_CIPHER_CTX base; /* Must be first */ 20e1051a39Sopenharmony_ci union { 21e1051a39Sopenharmony_ci OSSL_UNION_ALIGN; 22e1051a39Sopenharmony_ci DES_key_schedule ks[3]; 23e1051a39Sopenharmony_ci } tks; 24e1051a39Sopenharmony_ci union { 25e1051a39Sopenharmony_ci void (*cbc) (const void *, void *, size_t, 26e1051a39Sopenharmony_ci const DES_key_schedule *, unsigned char *); 27e1051a39Sopenharmony_ci } tstream; 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ci} PROV_TDES_CTX; 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ci#define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \ 32e1051a39Sopenharmony_ci kbits, blkbits, ivbits, block) \ 33e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_newctx_fn tdes_##type##_##lcmode##_newctx; \ 34e1051a39Sopenharmony_cistatic void *tdes_##type##_##lcmode##_newctx(void *provctx) \ 35e1051a39Sopenharmony_ci{ \ 36e1051a39Sopenharmony_ci return ossl_tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits, \ 37e1051a39Sopenharmony_ci ivbits, flags, \ 38e1051a39Sopenharmony_ci ossl_prov_cipher_hw_tdes_##type##_##lcmode()); \ 39e1051a39Sopenharmony_ci} \ 40e1051a39Sopenharmony_cistatic OSSL_FUNC_cipher_get_params_fn tdes_##type##_##lcmode##_get_params; \ 41e1051a39Sopenharmony_cistatic int tdes_##type##_##lcmode##_get_params(OSSL_PARAM params[]) \ 42e1051a39Sopenharmony_ci{ \ 43e1051a39Sopenharmony_ci return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \ 44e1051a39Sopenharmony_ci flags, kbits, blkbits, ivbits); \ 45e1051a39Sopenharmony_ci} \ 46e1051a39Sopenharmony_ciconst OSSL_DISPATCH ossl_tdes_##type##_##lcmode##_functions[] = { \ 47e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_tdes_einit }, \ 48e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_tdes_dinit }, \ 49e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_UPDATE, \ 50e1051a39Sopenharmony_ci (void (*)(void))ossl_cipher_generic_##block##_update }, \ 51e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_FINAL, \ 52e1051a39Sopenharmony_ci (void (*)(void))ossl_cipher_generic_##block##_final }, \ 53e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \ 54e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_NEWCTX, \ 55e1051a39Sopenharmony_ci (void (*)(void))tdes_##type##_##lcmode##_newctx }, \ 56e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))ossl_tdes_dupctx }, \ 57e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))ossl_tdes_freectx }, \ 58e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_GET_PARAMS, \ 59e1051a39Sopenharmony_ci (void (*)(void))tdes_##type##_##lcmode##_get_params }, \ 60e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ 61e1051a39Sopenharmony_ci (void (*)(void))ossl_cipher_generic_gettable_params }, \ 62e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ 63e1051a39Sopenharmony_ci (void (*)(void))ossl_tdes_get_ctx_params }, \ 64e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ 65e1051a39Sopenharmony_ci (void (*)(void))ossl_tdes_gettable_ctx_params }, \ 66e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ 67e1051a39Sopenharmony_ci (void (*)(void))ossl_cipher_generic_set_ctx_params }, \ 68e1051a39Sopenharmony_ci { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ 69e1051a39Sopenharmony_ci (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \ 70e1051a39Sopenharmony_ci { 0, NULL } \ 71e1051a39Sopenharmony_ci} 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_civoid *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits, 74e1051a39Sopenharmony_ci size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw); 75e1051a39Sopenharmony_ciOSSL_FUNC_cipher_dupctx_fn ossl_tdes_dupctx; 76e1051a39Sopenharmony_ciOSSL_FUNC_cipher_freectx_fn ossl_tdes_freectx; 77e1051a39Sopenharmony_ciOSSL_FUNC_cipher_encrypt_init_fn ossl_tdes_einit; 78e1051a39Sopenharmony_ciOSSL_FUNC_cipher_decrypt_init_fn ossl_tdes_dinit; 79e1051a39Sopenharmony_ciOSSL_FUNC_cipher_get_ctx_params_fn ossl_tdes_get_ctx_params; 80e1051a39Sopenharmony_ciOSSL_FUNC_cipher_gettable_ctx_params_fn ossl_tdes_gettable_ctx_params; 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ci#define PROV_CIPHER_HW_tdes_mode(type, mode) \ 83e1051a39Sopenharmony_cistatic const PROV_CIPHER_HW type##_##mode = { \ 84e1051a39Sopenharmony_ci ossl_cipher_hw_tdes_##type##_initkey, \ 85e1051a39Sopenharmony_ci ossl_cipher_hw_tdes_##mode, \ 86e1051a39Sopenharmony_ci ossl_cipher_hw_tdes_copyctx \ 87e1051a39Sopenharmony_ci}; \ 88e1051a39Sopenharmony_ciconst PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_##type##_##mode(void) \ 89e1051a39Sopenharmony_ci{ \ 90e1051a39Sopenharmony_ci return &type##_##mode; \ 91e1051a39Sopenharmony_ci} 92e1051a39Sopenharmony_ci 93e1051a39Sopenharmony_ciint ossl_cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, 94e1051a39Sopenharmony_ci const unsigned char *key, size_t keylen); 95e1051a39Sopenharmony_civoid ossl_cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst, 96e1051a39Sopenharmony_ci const PROV_CIPHER_CTX *src); 97e1051a39Sopenharmony_ciint ossl_cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out, 98e1051a39Sopenharmony_ci const unsigned char *in, size_t inl); 99e1051a39Sopenharmony_ciint ossl_cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out, 100e1051a39Sopenharmony_ci const unsigned char *in, size_t len); 101e1051a39Sopenharmony_ci 102e1051a39Sopenharmony_ciconst PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_cbc(void); 103e1051a39Sopenharmony_ciconst PROV_CIPHER_HW *ossl_prov_cipher_hw_tdes_ede3_ecb(void); 104