2019-09-23 12:35:16 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2019-09-23 12:35:16 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-01-16 11:50:03 +08:00
|
|
|
/*
|
|
|
|
* DES low level APIs are deprecated for public use, but still ok for internal
|
|
|
|
* use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2021-02-06 00:40:42 +08:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/proverr.h>
|
2019-10-04 20:44:42 +08:00
|
|
|
#include "prov/ciphercommon.h"
|
2019-09-23 12:35:16 +08:00
|
|
|
#include "cipher_des.h"
|
2019-10-04 21:20:48 +08:00
|
|
|
#include "prov/implementations.h"
|
2020-09-08 10:56:34 +08:00
|
|
|
#include "prov/providercommon.h"
|
2019-09-23 12:35:16 +08:00
|
|
|
|
2020-12-17 14:39:57 +08:00
|
|
|
#define DES_FLAGS 0
|
2019-09-23 12:35:16 +08:00
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_cipher_freectx_fn des_freectx;
|
|
|
|
static OSSL_FUNC_cipher_encrypt_init_fn des_einit;
|
|
|
|
static OSSL_FUNC_cipher_decrypt_init_fn des_dinit;
|
|
|
|
static OSSL_FUNC_cipher_get_ctx_params_fn des_get_ctx_params;
|
|
|
|
static OSSL_FUNC_cipher_gettable_ctx_params_fn des_gettable_ctx_params;
|
2019-09-23 12:35:16 +08:00
|
|
|
|
|
|
|
static void *des_newctx(void *provctx, size_t kbits, size_t blkbits,
|
|
|
|
size_t ivbits, unsigned int mode, uint64_t flags,
|
|
|
|
const PROV_CIPHER_HW *hw)
|
|
|
|
{
|
2020-09-08 10:56:34 +08:00
|
|
|
PROV_DES_CTX *ctx;
|
2019-09-23 12:35:16 +08:00
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx = OPENSSL_zalloc(sizeof(*ctx));
|
2019-09-23 12:35:16 +08:00
|
|
|
if (ctx != NULL)
|
2020-09-29 15:40:26 +08:00
|
|
|
ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,
|
|
|
|
hw, provctx);
|
2019-09-23 12:35:16 +08:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2020-04-10 01:58:02 +08:00
|
|
|
static void *des_dupctx(void *ctx)
|
|
|
|
{
|
|
|
|
PROV_DES_CTX *in = (PROV_DES_CTX *)ctx;
|
2020-09-08 10:56:34 +08:00
|
|
|
PROV_DES_CTX *ret;
|
2020-04-10 01:58:02 +08:00
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = OPENSSL_malloc(sizeof(*ret));
|
2020-04-10 01:58:02 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
in->base.hw->copyctx(&ret->base, &in->base);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-23 12:35:16 +08:00
|
|
|
static void des_freectx(void *vctx)
|
|
|
|
{
|
2019-10-08 07:19:10 +08:00
|
|
|
PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
|
2019-09-23 12:35:16 +08:00
|
|
|
|
2020-09-29 15:40:26 +08:00
|
|
|
ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
|
2019-09-23 12:35:16 +08:00
|
|
|
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int des_init(void *vctx, const unsigned char *key, size_t keylen,
|
2021-03-02 20:45:34 +08:00
|
|
|
const unsigned char *iv, size_t ivlen,
|
|
|
|
const OSSL_PARAM params[], int enc)
|
2019-09-23 12:35:16 +08:00
|
|
|
{
|
|
|
|
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
|
|
|
|
2020-07-22 08:40:55 +08:00
|
|
|
ctx->num = 0;
|
2020-08-05 06:45:29 +08:00
|
|
|
ctx->bufsz = 0;
|
2019-09-23 12:35:16 +08:00
|
|
|
ctx->enc = enc;
|
|
|
|
|
|
|
|
if (iv != NULL) {
|
2020-09-29 15:40:26 +08:00
|
|
|
if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
|
2019-09-23 12:35:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key != NULL) {
|
|
|
|
if (keylen != ctx->keylen) {
|
2021-02-06 01:51:37 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
2019-09-23 12:35:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-03-02 20:45:34 +08:00
|
|
|
if (!ctx->hw->init(ctx, key, keylen))
|
|
|
|
return 0;
|
2019-09-23 12:35:16 +08:00
|
|
|
}
|
2021-03-02 20:45:34 +08:00
|
|
|
return ossl_cipher_generic_set_ctx_params(ctx, params);
|
2019-09-23 12:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int des_einit(void *vctx, const unsigned char *key, size_t keylen,
|
2021-03-02 20:45:34 +08:00
|
|
|
const unsigned char *iv, size_t ivlen,
|
|
|
|
const OSSL_PARAM params[])
|
2019-09-23 12:35:16 +08:00
|
|
|
{
|
2021-03-02 20:45:34 +08:00
|
|
|
return des_init(vctx, key, keylen, iv, ivlen, params, 1);
|
2019-09-23 12:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int des_dinit(void *vctx, const unsigned char *key, size_t keylen,
|
2021-03-02 20:45:34 +08:00
|
|
|
const unsigned char *iv, size_t ivlen,
|
|
|
|
const OSSL_PARAM params[])
|
2019-09-23 12:35:16 +08:00
|
|
|
{
|
2021-03-02 20:45:34 +08:00
|
|
|
return des_init(vctx, key, keylen, iv, ivlen, params, 0);
|
2019-09-23 12:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int des_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
DES_cblock *deskey = ptr;
|
|
|
|
size_t kl = ctx->keylen;
|
|
|
|
|
2020-01-16 00:34:55 +08:00
|
|
|
if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
|
2019-09-23 12:35:16 +08:00
|
|
|
return 0;
|
|
|
|
DES_set_odd_parity(deskey);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(des)
|
|
|
|
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
|
|
|
|
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(des)
|
|
|
|
|
|
|
|
static int des_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
|
|
|
OSSL_PARAM *p;
|
|
|
|
|
2020-09-29 15:40:26 +08:00
|
|
|
if (!ossl_cipher_generic_get_ctx_params(vctx, params))
|
2019-09-23 12:35:16 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
|
|
|
|
if (p != NULL && !des_generatekey(ctx, p->data)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define IMPLEMENT_des_cipher(type, lcmode, UCMODE, flags, \
|
|
|
|
kbits, blkbits, ivbits, block) \
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_cipher_newctx_fn type##_##lcmode##_newctx; \
|
2019-09-23 12:35:16 +08:00
|
|
|
static void *des_##lcmode##_newctx(void *provctx) \
|
|
|
|
{ \
|
|
|
|
return des_newctx(provctx, kbits, blkbits, ivbits, \
|
|
|
|
EVP_CIPH_##UCMODE##_MODE, flags, \
|
Fix external symbols in the provider cipher implementations.
Partial fix for #12964
This add ossl_ names for the following symbols.
chacha20_dinit, chacha20_einit, chacha20_initctx,
ccm_cipher, ccm_dinit, ccm_einit, ccm_generic_auth_decrypt, ccm_generic_auth_encrypt,
ccm_generic_gettag, ccm_generic_setaad, ccm_generic_setiv, ccm_get_ctx_params,
ccm_initctx, ccm_set_ctx_params, ccm_stream_final, ccm_stream_update
gcm_aad_update, gcm_cipher, gcm_cipher_final, gcm_cipher_update
gcm_dinit, gcm_einit, gcm_get_ctx_params, gcm_initctx, gcm_one_shot
gcm_set_ctx_params, gcm_setiv, gcm_stream_final, gcm_stream_update
tdes_dinit, tdes_dupctx, tdes_einit, tdes_freectx
tdes_get_ctx_params, tdes_gettable_ctx_params, tdes_newctx
PROV_CIPHER_HW_des_*,
padblock, unpadblock, tlsunpadblock, fillblock, trailingdata
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14209)
2021-02-17 15:54:29 +08:00
|
|
|
ossl_prov_cipher_hw_des_##lcmode()); \
|
2019-09-23 12:35:16 +08:00
|
|
|
} \
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_cipher_get_params_fn des_##lcmode##_get_params; \
|
2019-09-23 12:35:16 +08:00
|
|
|
static int des_##lcmode##_get_params(OSSL_PARAM params[]) \
|
|
|
|
{ \
|
2020-09-29 15:40:26 +08:00
|
|
|
return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
|
|
|
|
flags, kbits, blkbits, ivbits); \
|
2019-09-23 12:35:16 +08:00
|
|
|
} \
|
2020-09-28 10:28:29 +08:00
|
|
|
const OSSL_DISPATCH ossl_##des_##lcmode##_functions[] = { \
|
2019-09-23 12:35:16 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))des_einit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))des_dinit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_UPDATE, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_##block##_update }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##block##_final },\
|
|
|
|
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
|
2019-09-23 12:35:16 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_NEWCTX, \
|
|
|
|
(void (*)(void))des_##lcmode##_newctx }, \
|
2020-04-10 01:58:02 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))des_dupctx }, \
|
2019-09-23 12:35:16 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
|
|
|
(void (*)(void))des_##lcmode##_get_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_gettable_params }, \
|
2019-09-23 12:35:16 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))des_get_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
|
|
|
|
(void (*)(void))des_gettable_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_set_ctx_params }, \
|
2019-09-23 12:35:16 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
|
2019-09-23 12:35:16 +08:00
|
|
|
{ 0, NULL } \
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:28:29 +08:00
|
|
|
/* ossl_des_ecb_functions */
|
2019-09-23 12:35:16 +08:00
|
|
|
IMPLEMENT_des_cipher(des, ecb, ECB, DES_FLAGS, 64, 64, 0, block);
|
2020-09-28 10:28:29 +08:00
|
|
|
/* ossl_des_cbc_functions */
|
2019-09-23 12:35:16 +08:00
|
|
|
IMPLEMENT_des_cipher(des, cbc, CBC, DES_FLAGS, 64, 64, 64, block);
|
2020-09-28 10:28:29 +08:00
|
|
|
/* ossl_des_ofb64_functions */
|
2019-09-23 12:35:16 +08:00
|
|
|
IMPLEMENT_des_cipher(des, ofb64, OFB, DES_FLAGS, 64, 8, 64, stream);
|
2020-09-28 10:28:29 +08:00
|
|
|
/* ossl_des_cfb64_functions */
|
2019-09-23 12:35:16 +08:00
|
|
|
IMPLEMENT_des_cipher(des, cfb64, CFB, DES_FLAGS, 64, 8, 64, stream);
|
2020-09-28 10:28:29 +08:00
|
|
|
/* ossl_des_cfb1_functions */
|
2019-09-23 12:35:16 +08:00
|
|
|
IMPLEMENT_des_cipher(des, cfb1, CFB, DES_FLAGS, 64, 8, 64, stream);
|
2020-09-28 10:28:29 +08:00
|
|
|
/* ossl_des_cfb8_functions */
|
2019-09-23 12:35:16 +08:00
|
|
|
IMPLEMENT_des_cipher(des, cfb8, CFB, DES_FLAGS, 64, 8, 64, stream);
|