2019-08-26 15:05:08 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/des.h>
|
|
|
|
#include <openssl/core_numbers.h>
|
2019-12-19 20:33:35 +08:00
|
|
|
#include "crypto/des_platform.h"
|
2019-08-26 15:05:08 +08:00
|
|
|
|
|
|
|
#define DES_BLOCK_SIZE 8
|
|
|
|
#define TDES_IVLEN 8
|
|
|
|
|
|
|
|
/* TODO(3.0) Figure out what flags need to be here */
|
2019-09-25 15:07:20 +08:00
|
|
|
#define TDES_FLAGS (EVP_CIPH_RAND_KEY)
|
2019-08-26 15:05:08 +08:00
|
|
|
|
|
|
|
typedef struct prov_tdes_ctx_st {
|
|
|
|
PROV_CIPHER_CTX base; /* Must be first */
|
|
|
|
union {
|
|
|
|
OSSL_UNION_ALIGN;
|
|
|
|
DES_key_schedule ks[3];
|
|
|
|
} tks;
|
|
|
|
union {
|
|
|
|
void (*cbc) (const void *, void *, size_t,
|
|
|
|
const DES_key_schedule *, unsigned char *);
|
|
|
|
} tstream;
|
|
|
|
|
|
|
|
} PROV_TDES_CTX;
|
|
|
|
|
2020-04-10 01:58:02 +08:00
|
|
|
#define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \
|
2019-08-26 15:05:08 +08:00
|
|
|
kbits, blkbits, ivbits, block) \
|
|
|
|
static OSSL_OP_cipher_newctx_fn tdes_##type##_##lcmode##_newctx; \
|
|
|
|
static void *tdes_##type##_##lcmode##_newctx(void *provctx) \
|
|
|
|
{ \
|
|
|
|
return tdes_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, kbits, blkbits, \
|
2019-09-15 18:06:28 +08:00
|
|
|
ivbits, flags, PROV_CIPHER_HW_tdes_##type##_##lcmode());\
|
2019-08-26 15:05:08 +08:00
|
|
|
} \
|
|
|
|
static OSSL_OP_cipher_get_params_fn tdes_##type##_##lcmode##_get_params; \
|
|
|
|
static int tdes_##type##_##lcmode##_get_params(OSSL_PARAM params[]) \
|
|
|
|
{ \
|
|
|
|
return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \
|
|
|
|
kbits, blkbits, ivbits); \
|
|
|
|
} \
|
|
|
|
const OSSL_DISPATCH tdes_##type##_##lcmode##_functions[] = { \
|
|
|
|
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))tdes_einit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))tdes_dinit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_UPDATE, \
|
|
|
|
(void (*)(void))cipher_generic_##block##_update }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##block##_final },\
|
|
|
|
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_NEWCTX, \
|
|
|
|
(void (*)(void))tdes_##type##_##lcmode##_newctx }, \
|
2020-04-10 01:58:02 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))tdes_dupctx }, \
|
2019-08-26 15:05:08 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))tdes_freectx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
|
|
|
(void (*)(void))tdes_##type##_##lcmode##_get_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
|
|
|
|
(void (*)(void))cipher_generic_gettable_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))tdes_get_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
|
|
|
|
(void (*)(void))tdes_gettable_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
|
|
|
|
(void (*)(void))cipher_generic_set_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
|
|
|
|
(void (*)(void))cipher_generic_settable_ctx_params }, \
|
|
|
|
{ 0, NULL } \
|
|
|
|
}
|
|
|
|
|
|
|
|
void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
|
2019-09-15 18:06:28 +08:00
|
|
|
size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw);
|
2020-04-10 01:58:02 +08:00
|
|
|
OSSL_OP_cipher_dupctx_fn tdes_dupctx;
|
2019-08-26 15:05:08 +08:00
|
|
|
OSSL_OP_cipher_freectx_fn tdes_freectx;
|
|
|
|
OSSL_OP_cipher_encrypt_init_fn tdes_einit;
|
|
|
|
OSSL_OP_cipher_decrypt_init_fn tdes_dinit;
|
|
|
|
OSSL_OP_cipher_get_ctx_params_fn tdes_get_ctx_params;
|
|
|
|
OSSL_OP_cipher_gettable_ctx_params_fn tdes_gettable_ctx_params;
|
|
|
|
|
2020-04-10 01:58:02 +08:00
|
|
|
#define PROV_CIPHER_HW_tdes_mode(type, mode) \
|
2019-08-26 15:05:08 +08:00
|
|
|
static const PROV_CIPHER_HW type##_##mode = { \
|
|
|
|
cipher_hw_tdes_##type##_initkey, \
|
2020-04-10 01:58:02 +08:00
|
|
|
cipher_hw_tdes_##mode, \
|
|
|
|
cipher_hw_tdes_copyctx \
|
2019-08-26 15:05:08 +08:00
|
|
|
}; \
|
|
|
|
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_##type##_##mode(void) \
|
|
|
|
{ \
|
|
|
|
return &type##_##mode; \
|
|
|
|
}
|
|
|
|
|
|
|
|
int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key,
|
|
|
|
size_t keylen);
|
2020-04-10 01:58:02 +08:00
|
|
|
void cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src);
|
2019-08-26 15:05:08 +08:00
|
|
|
int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
|
|
|
const unsigned char *in, size_t inl);
|
|
|
|
int cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
|
|
|
const unsigned char *in, size_t len);
|
|
|
|
|
|
|
|
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_cbc(void);
|
|
|
|
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_ecb(void);
|