Latest update.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
LIBS=../../../libcrypto
|
||||
|
||||
IF[{- !$disabled{des} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_tdes_default.c cipher_tdes_default_hw.c \
|
||||
cipher_tdes_wrap.c cipher_tdes_wrap_hw.c \
|
||||
cipher_desx.c cipher_desx_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{aria} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_aria.c cipher_aria_hw.c \
|
||||
cipher_aria_gcm.c cipher_aria_gcm_hw.c \
|
||||
cipher_aria_ccm.c cipher_aria_ccm_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{camellia} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_camellia.c cipher_camellia_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{bf} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_blowfish.c cipher_blowfish_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{idea} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_idea.c cipher_idea_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{cast} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_cast5.c cipher_cast5_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{seed} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_seed.c cipher_seed_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{sm4} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_sm4.c cipher_sm4_hw.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{ocb} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
cipher_aes_ocb.c cipher_aes_ocb_hw.c
|
||||
ENDIF
|
||||
|
||||
INCLUDE[../../../libcrypto]=. ../../../crypto
|
||||
@@ -0,0 +1,491 @@
|
||||
/*
|
||||
* 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 "cipher_aes_ocb.h"
|
||||
#include "internal/providercommonerr.h"
|
||||
#include "internal/ciphers/cipher_aead.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
#define AES_OCB_FLAGS AEAD_FLAGS
|
||||
|
||||
#define OCB_DEFAULT_TAG_LEN 16
|
||||
#define OCB_DEFAULT_IV_LEN 12
|
||||
#define OCB_MIN_IV_LEN 1
|
||||
#define OCB_MAX_IV_LEN 15
|
||||
|
||||
PROV_CIPHER_FUNC(int, ocb_cipher, (PROV_AES_OCB_CTX *ctx,
|
||||
const unsigned char *in, unsigned char *out,
|
||||
size_t nextblock));
|
||||
/* forward declarations */
|
||||
static OSSL_OP_cipher_encrypt_init_fn aes_ocb_einit;
|
||||
static OSSL_OP_cipher_decrypt_init_fn aes_ocb_dinit;
|
||||
static OSSL_OP_cipher_update_fn aes_ocb_block_update;
|
||||
static OSSL_OP_cipher_final_fn aes_ocb_block_final;
|
||||
static OSSL_OP_cipher_cipher_fn aes_ocb_cipher;
|
||||
static OSSL_OP_cipher_freectx_fn aes_ocb_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn aes_ocb_dupctx;
|
||||
static OSSL_OP_cipher_get_ctx_params_fn aes_ocb_get_ctx_params;
|
||||
static OSSL_OP_cipher_set_ctx_params_fn aes_ocb_set_ctx_params;
|
||||
|
||||
/*
|
||||
* The following methods could be moved into PROV_AES_OCB_HW if
|
||||
* multiple hardware implementations are ever needed.
|
||||
*/
|
||||
static ossl_inline int aes_generic_ocb_setiv(PROV_AES_OCB_CTX *ctx,
|
||||
const unsigned char *iv,
|
||||
size_t ivlen, size_t taglen)
|
||||
{
|
||||
return (CRYPTO_ocb128_setiv(&ctx->ocb, iv, ivlen, taglen) == 1);
|
||||
}
|
||||
|
||||
static ossl_inline int aes_generic_ocb_setaad(PROV_AES_OCB_CTX *ctx,
|
||||
const unsigned char *aad,
|
||||
size_t alen)
|
||||
{
|
||||
return CRYPTO_ocb128_aad(&ctx->ocb, aad, alen) == 1;
|
||||
}
|
||||
|
||||
static ossl_inline int aes_generic_ocb_gettag(PROV_AES_OCB_CTX *ctx,
|
||||
unsigned char *tag, size_t tlen)
|
||||
{
|
||||
return CRYPTO_ocb128_tag(&ctx->ocb, tag, tlen) > 0;
|
||||
}
|
||||
|
||||
static ossl_inline int aes_generic_ocb_final(PROV_AES_OCB_CTX *ctx)
|
||||
{
|
||||
return (CRYPTO_ocb128_finish(&ctx->ocb, ctx->tag, ctx->taglen) == 0);
|
||||
}
|
||||
|
||||
static ossl_inline void aes_generic_ocb_cleanup(PROV_AES_OCB_CTX *ctx)
|
||||
{
|
||||
CRYPTO_ocb128_cleanup(&ctx->ocb);
|
||||
}
|
||||
|
||||
static ossl_inline int aes_generic_ocb_cipher(PROV_AES_OCB_CTX *ctx,
|
||||
const unsigned char *in,
|
||||
unsigned char *out, size_t len)
|
||||
{
|
||||
if (ctx->base.enc) {
|
||||
if (!CRYPTO_ocb128_encrypt(&ctx->ocb, in, out, len))
|
||||
return 0;
|
||||
} else {
|
||||
if (!CRYPTO_ocb128_decrypt(&ctx->ocb, in, out, len))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ossl_inline int aes_generic_ocb_copy_ctx(PROV_AES_OCB_CTX *dst,
|
||||
PROV_AES_OCB_CTX *src)
|
||||
{
|
||||
return (!CRYPTO_ocb128_copy_ctx(&dst->ocb, &src->ocb,
|
||||
&src->ksenc.ks, &src->ksdec.ks));
|
||||
}
|
||||
|
||||
/*-
|
||||
* Provider dispatch functions
|
||||
*/
|
||||
static int aes_ocb_init(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen, int enc)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
|
||||
ctx->base.enc = enc;
|
||||
|
||||
if (iv != NULL) {
|
||||
if (ivlen != ctx->base.ivlen) {
|
||||
/* IV len must be 1 to 15 */
|
||||
if (ivlen < OCB_MIN_IV_LEN || ivlen > OCB_MAX_IV_LEN) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
ctx->base.ivlen = ivlen;
|
||||
}
|
||||
memcpy(ctx->base.iv, iv, ivlen);
|
||||
ctx->iv_state = IV_STATE_BUFFERED;
|
||||
}
|
||||
if (key != NULL) {
|
||||
if (keylen != ctx->base.keylen) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
return ctx->base.hw->init(&ctx->base, key, keylen);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aes_ocb_einit(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen)
|
||||
{
|
||||
return aes_ocb_init(vctx, key, keylen, iv, ivlen, 1);
|
||||
}
|
||||
|
||||
static int aes_ocb_dinit(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen)
|
||||
{
|
||||
return aes_ocb_init(vctx, key, keylen, iv, ivlen, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Because of the way OCB works, both the AAD and data are buffered in the
|
||||
* same way. Only the last block can be a partial block.
|
||||
*/
|
||||
static int aes_ocb_block_update_internal(PROV_AES_OCB_CTX *ctx,
|
||||
unsigned char *buf, size_t *bufsz,
|
||||
unsigned char *out, size_t *outl,
|
||||
size_t outsize, const unsigned char *in,
|
||||
size_t inl, OSSL_ocb_cipher_fn ciph)
|
||||
{
|
||||
size_t nextblocks = fillblock(buf, bufsz, AES_BLOCK_SIZE, &in, &inl);
|
||||
size_t outlint = 0;
|
||||
|
||||
if (*bufsz == AES_BLOCK_SIZE) {
|
||||
if (outsize < AES_BLOCK_SIZE) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
if (!ciph(ctx, buf, out, AES_BLOCK_SIZE)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
|
||||
return 0;
|
||||
}
|
||||
*bufsz = 0;
|
||||
outlint = AES_BLOCK_SIZE;
|
||||
out += AES_BLOCK_SIZE;
|
||||
}
|
||||
if (nextblocks > 0) {
|
||||
outlint += nextblocks;
|
||||
if (outsize < outlint) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
if (!ciph(ctx, in, out, nextblocks)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
|
||||
return 0;
|
||||
}
|
||||
in += nextblocks;
|
||||
inl -= nextblocks;
|
||||
}
|
||||
if (!trailingdata(buf, bufsz, AES_BLOCK_SIZE, &in, &inl)) {
|
||||
/* PROVerr already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
*outl = outlint;
|
||||
return inl == 0;
|
||||
}
|
||||
|
||||
/* A wrapper function that has the same signature as cipher */
|
||||
static int cipher_updateaad(PROV_AES_OCB_CTX *ctx, const unsigned char *in,
|
||||
unsigned char *out, size_t len)
|
||||
{
|
||||
return aes_generic_ocb_setaad(ctx, in, len);
|
||||
}
|
||||
|
||||
static int update_iv(PROV_AES_OCB_CTX *ctx)
|
||||
{
|
||||
if (ctx->iv_state == IV_STATE_FINISHED
|
||||
|| ctx->iv_state == IV_STATE_UNINITIALISED)
|
||||
return 0;
|
||||
if (ctx->iv_state == IV_STATE_BUFFERED) {
|
||||
if (!aes_generic_ocb_setiv(ctx, ctx->base.iv, ctx->base.ivlen,
|
||||
ctx->taglen))
|
||||
return 0;
|
||||
ctx->iv_state = IV_STATE_COPIED;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aes_ocb_block_update(void *vctx, unsigned char *out, size_t *outl,
|
||||
size_t outsize, const unsigned char *in,
|
||||
size_t inl)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
unsigned char *buf;
|
||||
size_t *buflen;
|
||||
OSSL_ocb_cipher_fn fn;
|
||||
|
||||
if (!ctx->key_set || !update_iv(ctx))
|
||||
return 0;
|
||||
|
||||
/* Are we dealing with AAD or normal data here? */
|
||||
if (out == NULL) {
|
||||
buf = ctx->aad_buf;
|
||||
buflen = &ctx->aad_buf_len;
|
||||
fn = cipher_updateaad;
|
||||
} else {
|
||||
buf = ctx->data_buf;
|
||||
buflen = &ctx->data_buf_len;
|
||||
fn = aes_generic_ocb_cipher;
|
||||
}
|
||||
return aes_ocb_block_update_internal(ctx, buf, buflen, out, outl, outsize,
|
||||
in, inl, fn);
|
||||
}
|
||||
|
||||
static int aes_ocb_block_final(void *vctx, unsigned char *out, size_t *outl,
|
||||
size_t outsize)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
|
||||
/* If no block_update has run then the iv still needs to be set */
|
||||
if (!ctx->key_set || !update_iv(ctx))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* Empty the buffer of any partial block that we might have been provided,
|
||||
* both for data and AAD
|
||||
*/
|
||||
*outl = 0;
|
||||
if (ctx->data_buf_len > 0) {
|
||||
if (!aes_generic_ocb_cipher(ctx, ctx->data_buf, out, ctx->data_buf_len))
|
||||
return 0;
|
||||
*outl = ctx->data_buf_len;
|
||||
ctx->data_buf_len = 0;
|
||||
}
|
||||
if (ctx->aad_buf_len > 0) {
|
||||
if (!aes_generic_ocb_setaad(ctx, ctx->aad_buf, ctx->aad_buf_len))
|
||||
return 0;
|
||||
ctx->aad_buf_len = 0;
|
||||
}
|
||||
if (ctx->base.enc) {
|
||||
/* If encrypting then just get the tag */
|
||||
if (!aes_generic_ocb_gettag(ctx, ctx->tag, ctx->taglen))
|
||||
return 0;
|
||||
} else {
|
||||
/* If decrypting then verify */
|
||||
if (ctx->taglen == 0)
|
||||
return 0;
|
||||
if (!aes_generic_ocb_final(ctx))
|
||||
return 0;
|
||||
}
|
||||
/* Don't reuse the IV */
|
||||
ctx->iv_state = IV_STATE_FINISHED;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void *aes_ocb_newctx(void *provctx, size_t kbits, size_t blkbits,
|
||||
size_t ivbits, unsigned int mode, uint64_t flags)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
if (ctx != NULL) {
|
||||
cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,
|
||||
PROV_CIPHER_HW_aes_ocb(kbits), NULL);
|
||||
ctx->taglen = OCB_DEFAULT_TAG_LEN;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void aes_ocb_freectx(void *vctx)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
|
||||
aes_generic_ocb_cleanup(ctx);
|
||||
OPENSSL_cleanse(ctx->base.iv, sizeof(ctx->base.iv));
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *aes_ocb_dupctx(void *vctx)
|
||||
{
|
||||
PROV_AES_OCB_CTX *in = (PROV_AES_OCB_CTX *)vctx;
|
||||
PROV_AES_OCB_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
if (!aes_generic_ocb_copy_ctx(ret, in))
|
||||
OPENSSL_free(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int aes_ocb_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
const OSSL_PARAM *p;
|
||||
size_t sz;
|
||||
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
|
||||
if (p != NULL) {
|
||||
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (p->data == NULL) {
|
||||
/* Tag len must be 0 to 16 */
|
||||
if (p->data_size > OCB_MAX_TAG_LEN)
|
||||
return 0;
|
||||
ctx->taglen = p->data_size;
|
||||
} else {
|
||||
if (p->data_size != ctx->taglen || ctx->base.enc)
|
||||
return 0;
|
||||
memcpy(ctx->tag, p->data, p->data_size);
|
||||
}
|
||||
}
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
|
||||
if (p != NULL) {
|
||||
if (!OSSL_PARAM_get_size_t(p, &sz)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
/* IV len must be 1 to 15 */
|
||||
if (sz < OCB_MIN_IV_LEN || sz > OCB_MAX_IV_LEN)
|
||||
return 0;
|
||||
ctx->base.ivlen = sz;
|
||||
}
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
|
||||
if (p != NULL) {
|
||||
size_t keylen;
|
||||
|
||||
if (!OSSL_PARAM_get_size_t(p, &keylen)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->base.keylen != keylen) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aes_ocb_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
|
||||
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.ivlen)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
|
||||
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.keylen)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
|
||||
if (p != NULL) {
|
||||
if (!OSSL_PARAM_set_size_t(p, ctx->taglen)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
|
||||
if (p != NULL) {
|
||||
if (ctx->base.ivlen != p->data_size) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
if (!OSSL_PARAM_set_octet_string(p, ctx->base.iv, ctx->base.ivlen)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
|
||||
if (p != NULL) {
|
||||
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (!ctx->base.enc || p->data_size != ctx->taglen) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAGLEN);
|
||||
return 0;
|
||||
}
|
||||
memcpy(p->data, ctx->tag, ctx->taglen);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM cipher_ocb_known_gettable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
|
||||
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
|
||||
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
|
||||
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *cipher_ocb_gettable_ctx_params(void)
|
||||
{
|
||||
return cipher_ocb_known_gettable_ctx_params;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM cipher_ocb_known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
|
||||
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL),
|
||||
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *cipher_ocb_settable_ctx_params(void)
|
||||
{
|
||||
return cipher_ocb_known_settable_ctx_params;
|
||||
}
|
||||
|
||||
static int aes_ocb_cipher(void *vctx, unsigned char *out, size_t *outl,
|
||||
size_t outsize, const unsigned char *in, size_t inl)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
|
||||
if (outsize < inl) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!aes_generic_ocb_cipher(ctx, in, out, inl)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*outl = inl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define IMPLEMENT_cipher(mode, UCMODE, flags, kbits, blkbits, ivbits) \
|
||||
static OSSL_OP_cipher_get_params_fn aes_##kbits##_##mode##_get_params; \
|
||||
static int aes_##kbits##_##mode##_get_params(OSSL_PARAM params[]) \
|
||||
{ \
|
||||
return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
|
||||
flags, kbits, blkbits, ivbits); \
|
||||
} \
|
||||
static OSSL_OP_cipher_newctx_fn aes_##kbits##_##mode##_newctx; \
|
||||
static void *aes_##kbits##_##mode##_newctx(void *provctx) \
|
||||
{ \
|
||||
return aes_##mode##_newctx(provctx, kbits, blkbits, ivbits, \
|
||||
EVP_CIPH_##UCMODE##_MODE, flags); \
|
||||
} \
|
||||
const OSSL_DISPATCH aes##kbits##mode##_functions[] = { \
|
||||
{ OSSL_FUNC_CIPHER_NEWCTX, \
|
||||
(void (*)(void))aes_##kbits##_##mode##_newctx }, \
|
||||
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \
|
||||
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \
|
||||
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_block_update }, \
|
||||
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_block_final }, \
|
||||
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_ocb_cipher }, \
|
||||
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx }, \
|
||||
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_##mode##_dupctx }, \
|
||||
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
||||
(void (*)(void))aes_##kbits##_##mode##_get_params }, \
|
||||
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
|
||||
(void (*)(void))aes_##mode##_get_ctx_params }, \
|
||||
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
|
||||
(void (*)(void))aes_##mode##_set_ctx_params }, \
|
||||
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
|
||||
(void (*)(void))cipher_generic_gettable_params }, \
|
||||
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
|
||||
(void (*)(void))cipher_ocb_gettable_ctx_params }, \
|
||||
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
|
||||
(void (*)(void))cipher_ocb_settable_ctx_params }, \
|
||||
{ 0, NULL } \
|
||||
}
|
||||
|
||||
IMPLEMENT_cipher(ocb, OCB, AES_OCB_FLAGS, 256, 128, OCB_DEFAULT_IV_LEN * 8);
|
||||
IMPLEMENT_cipher(ocb, OCB, AES_OCB_FLAGS, 192, 128, OCB_DEFAULT_IV_LEN * 8);
|
||||
IMPLEMENT_cipher(ocb, OCB, AES_OCB_FLAGS, 128, 128, OCB_DEFAULT_IV_LEN * 8);
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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/aes.h>
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
|
||||
#define OCB_MAX_TAG_LEN AES_BLOCK_SIZE
|
||||
#define OCB_MAX_DATA_LEN AES_BLOCK_SIZE
|
||||
#define OCB_MAX_AAD_LEN AES_BLOCK_SIZE
|
||||
|
||||
typedef struct prov_aes_ocb_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
AES_KEY ks;
|
||||
} ksenc; /* AES key schedule to use for encryption/aad */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
AES_KEY ks;
|
||||
} ksdec; /* AES key schedule to use for decryption */
|
||||
OCB128_CONTEXT ocb;
|
||||
unsigned int iv_state; /* set to one of IV_STATE_XXX */
|
||||
unsigned int key_set : 1;
|
||||
size_t taglen;
|
||||
size_t data_buf_len;
|
||||
size_t aad_buf_len;
|
||||
unsigned char tag[OCB_MAX_TAG_LEN];
|
||||
unsigned char data_buf[OCB_MAX_DATA_LEN]; /* Store partial data blocks */
|
||||
unsigned char aad_buf[OCB_MAX_AAD_LEN]; /* Store partial AAD blocks */
|
||||
} PROV_AES_OCB_CTX;
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aes_ocb(size_t keybits);
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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 "cipher_aes_ocb.h"
|
||||
|
||||
#define OCB_SET_KEY_FN(fn_set_enc_key, fn_set_dec_key, \
|
||||
fn_block_enc, fn_block_dec, \
|
||||
fn_stream_enc, fn_stream_dec) \
|
||||
fn_set_enc_key(key, keylen * 8, &ctx->ksenc.ks); \
|
||||
fn_set_dec_key(key, keylen * 8, &ctx->ksdec.ks); \
|
||||
if (!CRYPTO_ocb128_init(&ctx->ocb, &ctx->ksenc.ks, &ctx->ksdec.ks, \
|
||||
(block128_f)fn_block_enc, (block128_f)fn_block_dec, \
|
||||
ctx->base.enc ? (ocb128_f)fn_stream_enc : \
|
||||
(ocb128_f)fn_stream_dec)) \
|
||||
return 0; \
|
||||
ctx->key_set = 1
|
||||
|
||||
|
||||
static int cipher_hw_aes_ocb_generic_initkey(PROV_CIPHER_CTX *vctx,
|
||||
const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
|
||||
/*
|
||||
* We set both the encrypt and decrypt key here because decrypt
|
||||
* needs both. (i.e- AAD uses encrypt).
|
||||
*/
|
||||
# ifdef HWAES_CAPABLE
|
||||
if (HWAES_CAPABLE) {
|
||||
OCB_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_set_decrypt_key,
|
||||
HWAES_encrypt, HWAES_decrypt,
|
||||
HWAES_ocb_encrypt, HWAES_ocb_decrypt);
|
||||
}
|
||||
# endif
|
||||
# ifdef VPAES_CAPABLE
|
||||
if (VPAES_CAPABLE) {
|
||||
OCB_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_set_decrypt_key,
|
||||
vpaes_encrypt, vpaes_decrypt, NULL, NULL);
|
||||
} else
|
||||
# endif
|
||||
{
|
||||
OCB_SET_KEY_FN(AES_set_encrypt_key, AES_set_decrypt_key,
|
||||
AES_encrypt, AES_decrypt, NULL, NULL);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
# if defined(AESNI_CAPABLE)
|
||||
|
||||
static int cipher_hw_aes_ocb_aesni_initkey(PROV_CIPHER_CTX *vctx,
|
||||
const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
|
||||
OCB_SET_KEY_FN(aesni_set_encrypt_key, aesni_set_decrypt_key,
|
||||
aesni_encrypt, aesni_decrypt,
|
||||
aesni_ocb_encrypt, aesni_ocb_decrypt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_declare() \
|
||||
static const PROV_CIPHER_HW aesni_ocb = { \
|
||||
cipher_hw_aes_ocb_aesni_initkey, \
|
||||
NULL \
|
||||
};
|
||||
# define PROV_CIPHER_HW_select() \
|
||||
if (AESNI_CAPABLE) \
|
||||
return &aesni_ocb;
|
||||
|
||||
#elif defined(SPARC_AES_CAPABLE)
|
||||
|
||||
static int cipher_hw_aes_ocb_t4_initkey(PROV_CIPHER_CTX *vctx,
|
||||
const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
|
||||
|
||||
OCB_SET_KEY_FN(aes_t4_set_encrypt_key, aes_t4_set_decrypt_key,
|
||||
aes_t4_encrypt, aes_t4_decrypt, NULL, NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_declare() \
|
||||
static const PROV_CIPHER_HW aes_t4_ocb = { \
|
||||
cipher_hw_aes_ocb_t4_initkey, \
|
||||
NULL \
|
||||
};
|
||||
# define PROV_CIPHER_HW_select() \
|
||||
if (SPARC_AES_CAPABLE) \
|
||||
return &aes_t4_ocb;
|
||||
#else
|
||||
# define PROV_CIPHER_HW_declare()
|
||||
# define PROV_CIPHER_HW_select()
|
||||
# endif
|
||||
|
||||
static const PROV_CIPHER_HW aes_generic_ocb = {
|
||||
cipher_hw_aes_ocb_generic_initkey,
|
||||
NULL
|
||||
};
|
||||
PROV_CIPHER_HW_declare()
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aes_ocb(size_t keybits)
|
||||
{
|
||||
PROV_CIPHER_HW_select()
|
||||
return &aes_generic_ocb;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for ARIA cipher modes ecb, cbc, ofb, cfb, ctr */
|
||||
|
||||
#include "cipher_aria.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn aria_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn aria_dupctx;
|
||||
|
||||
static void aria_freectx(void *vctx)
|
||||
{
|
||||
PROV_ARIA_CTX *ctx = (PROV_ARIA_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *aria_dupctx(void *ctx)
|
||||
{
|
||||
PROV_ARIA_CTX *in = (PROV_ARIA_CTX *)ctx;
|
||||
PROV_ARIA_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* aria256ecb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ecb, ECB, 0, 256, 128, 0, block)
|
||||
/* aria192ecb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ecb, ECB, 0, 192, 128, 0, block)
|
||||
/* aria128ecb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ecb, ECB, 0, 128, 128, 0, block)
|
||||
/* aria256cbc_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cbc, CBC, 0, 256, 128, 128, block)
|
||||
/* aria192cbc_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cbc, CBC, 0, 192, 128, 128, block)
|
||||
/* aria128cbc_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cbc, CBC, 0, 128, 128, 128, block)
|
||||
/* aria256ofb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ofb, OFB, 0, 256, 8, 128, stream)
|
||||
/* aria192ofb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ofb, OFB, 0, 192, 8, 128, stream)
|
||||
/* aria128ofb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ofb, OFB, 0, 128, 8, 128, stream)
|
||||
/* aria256cfb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb, CFB, 0, 256, 8, 128, stream)
|
||||
/* aria192cfb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb, CFB, 0, 192, 8, 128, stream)
|
||||
/* aria128cfb_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb, CFB, 0, 128, 8, 128, stream)
|
||||
/* aria256cfb1_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb1, CFB, 0, 256, 8, 128, stream)
|
||||
/* aria192cfb1_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb1, CFB, 0, 192, 8, 128, stream)
|
||||
/* aria128cfb1_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb1, CFB, 0, 128, 8, 128, stream)
|
||||
/* aria256cfb8_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb8, CFB, 0, 256, 8, 128, stream)
|
||||
/* aria192cfb8_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb8, CFB, 0, 192, 8, 128, stream)
|
||||
/* aria128cfb8_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, cfb8, CFB, 0, 128, 8, 128, stream)
|
||||
/* aria256ctr_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ctr, CTR, 0, 256, 8, 128, stream)
|
||||
/* aria192ctr_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ctr, CTR, 0, 192, 8, 128, stream)
|
||||
/* aria128ctr_functions */
|
||||
IMPLEMENT_generic_cipher(aria, ARIA, ctr, CTR, 0, 128, 8, 128, stream)
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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 "internal/aria.h"
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
|
||||
typedef struct prov_aria_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
ARIA_KEY ks;
|
||||
} ks;
|
||||
} PROV_ARIA_CTX;
|
||||
|
||||
|
||||
# define PROV_CIPHER_HW_aria_ofb PROV_CIPHER_HW_aria_ofb128
|
||||
# define PROV_CIPHER_HW_aria_cfb PROV_CIPHER_HW_aria_cfb128
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_ecb(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_cbc(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_ofb128(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_cfb128(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_cfb1(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_cfb8(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_ctr(size_t keybits);
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for ARIA CCM mode */
|
||||
|
||||
#include "cipher_aria_ccm.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn aria_ccm_freectx;
|
||||
|
||||
static void *aria_ccm_newctx(void *provctx, size_t keybits)
|
||||
{
|
||||
PROV_ARIA_CCM_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
if (ctx != NULL)
|
||||
ccm_initctx(&ctx->base, keybits, PROV_ARIA_HW_ccm(keybits));
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void aria_ccm_freectx(void *vctx)
|
||||
{
|
||||
PROV_ARIA_CCM_CTX *ctx = (PROV_ARIA_CCM_CTX *)vctx;
|
||||
|
||||
ccm_finalctx((PROV_CCM_CTX *)ctx);
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
/* aria128ccm functions */
|
||||
IMPLEMENT_aead_cipher(aria, ccm, CCM, AEAD_FLAGS, 128, 8, 96);
|
||||
/* aria192ccm functions */
|
||||
IMPLEMENT_aead_cipher(aria, ccm, CCM, AEAD_FLAGS, 192, 8, 96);
|
||||
/* aria256ccm functions */
|
||||
IMPLEMENT_aead_cipher(aria, ccm, CCM, AEAD_FLAGS, 256, 8, 96);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 "internal/aria.h"
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
#include "internal/ciphers/cipher_ccm.h"
|
||||
|
||||
typedef struct prov_aria_ccm_ctx_st {
|
||||
PROV_CCM_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
ARIA_KEY ks;
|
||||
} ks; /* ARIA key schedule to use */
|
||||
} PROV_ARIA_CCM_CTX;
|
||||
|
||||
const PROV_CCM_HW *PROV_ARIA_HW_ccm(size_t keylen);
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*-
|
||||
* Generic support for ARIA CCM.
|
||||
*/
|
||||
|
||||
#include "cipher_aria_ccm.h"
|
||||
|
||||
static int ccm_aria_initkey(PROV_CCM_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_ARIA_CCM_CTX *actx = (PROV_ARIA_CCM_CTX *)ctx;
|
||||
|
||||
aria_set_encrypt_key(key, keylen * 8, &actx->ks.ks);
|
||||
CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ks.ks,
|
||||
(block128_f)aria_encrypt);
|
||||
ctx->str = NULL;
|
||||
ctx->key_set = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const PROV_CCM_HW ccm_aria = {
|
||||
ccm_aria_initkey,
|
||||
ccm_generic_setiv,
|
||||
ccm_generic_setaad,
|
||||
ccm_generic_auth_encrypt,
|
||||
ccm_generic_auth_decrypt,
|
||||
ccm_generic_gettag
|
||||
};
|
||||
const PROV_CCM_HW *PROV_ARIA_HW_ccm(size_t keybits)
|
||||
{
|
||||
return &ccm_aria;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for ARIA GCM mode */
|
||||
|
||||
#include "cipher_aria_gcm.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static void *aria_gcm_newctx(void *provctx, size_t keybits)
|
||||
{
|
||||
PROV_ARIA_GCM_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
if (ctx != NULL)
|
||||
gcm_initctx(provctx, &ctx->base, keybits, PROV_ARIA_HW_gcm(keybits), 4);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn aria_gcm_freectx;
|
||||
static void aria_gcm_freectx(void *vctx)
|
||||
{
|
||||
PROV_ARIA_GCM_CTX *ctx = (PROV_ARIA_GCM_CTX *)vctx;
|
||||
|
||||
gcm_deinitctx((PROV_GCM_CTX *)ctx);
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
/* aria128gcm_functions */
|
||||
IMPLEMENT_aead_cipher(aria, gcm, GCM, AEAD_FLAGS, 128, 8, 96);
|
||||
/* aria192gcm_functions */
|
||||
IMPLEMENT_aead_cipher(aria, gcm, GCM, AEAD_FLAGS, 192, 8, 96);
|
||||
/* aria256gcm_functions */
|
||||
IMPLEMENT_aead_cipher(aria, gcm, GCM, AEAD_FLAGS, 256, 8, 96);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 "internal/aria.h"
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
#include "internal/ciphers/cipher_gcm.h"
|
||||
|
||||
typedef struct prov_aria_gcm_ctx_st {
|
||||
PROV_GCM_CTX base; /* must be first entry in struct */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
ARIA_KEY ks;
|
||||
} ks;
|
||||
} PROV_ARIA_GCM_CTX;
|
||||
|
||||
const PROV_GCM_HW *PROV_ARIA_HW_gcm(size_t keybits);
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*-
|
||||
* Generic support for ARIA GCM.
|
||||
*/
|
||||
|
||||
#include "cipher_aria_gcm.h"
|
||||
|
||||
static int aria_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
PROV_ARIA_GCM_CTX *actx = (PROV_ARIA_GCM_CTX *)ctx;
|
||||
ARIA_KEY *ks = &actx->ks.ks;
|
||||
|
||||
GCM_HW_SET_KEY_CTR_FN(ks, aria_set_encrypt_key, aria_encrypt, NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aria_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
|
||||
size_t len, unsigned char *out)
|
||||
{
|
||||
if (ctx->enc) {
|
||||
if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
|
||||
return 0;
|
||||
} else {
|
||||
if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const PROV_GCM_HW aria_gcm = {
|
||||
aria_gcm_initkey,
|
||||
gcm_setiv,
|
||||
gcm_aad_update,
|
||||
aria_cipher_update,
|
||||
gcm_cipher_final,
|
||||
gcm_one_shot
|
||||
};
|
||||
const PROV_GCM_HW *PROV_ARIA_HW_gcm(size_t keybits)
|
||||
{
|
||||
return &aria_gcm;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2001-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 "cipher_aria.h"
|
||||
|
||||
static int cipher_hw_aria_initkey(PROV_CIPHER_CTX *dat,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
int ret, mode = dat->mode;
|
||||
PROV_ARIA_CTX *adat = (PROV_ARIA_CTX *)dat;
|
||||
ARIA_KEY *ks = &adat->ks.ks;
|
||||
|
||||
if (dat->enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE))
|
||||
ret = aria_set_encrypt_key(key, keylen * 8, ks);
|
||||
else
|
||||
ret = aria_set_decrypt_key(key, keylen * 8, ks);
|
||||
if (ret < 0) {
|
||||
ERR_raise(ERR_LIB_PROV, EVP_R_ARIA_KEY_SETUP_FAILED);
|
||||
return 0;
|
||||
}
|
||||
dat->ks = ks;
|
||||
dat->block = (block128_f)aria_encrypt;
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_aria_mode(mode) \
|
||||
static const PROV_CIPHER_HW aria_##mode = { \
|
||||
cipher_hw_aria_initkey, \
|
||||
cipher_hw_chunked_##mode \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_aria_##mode(size_t keybits) \
|
||||
{ \
|
||||
return &aria_##mode; \
|
||||
}
|
||||
|
||||
PROV_CIPHER_HW_aria_mode(cbc)
|
||||
PROV_CIPHER_HW_aria_mode(ecb)
|
||||
PROV_CIPHER_HW_aria_mode(ofb128)
|
||||
PROV_CIPHER_HW_aria_mode(cfb128)
|
||||
PROV_CIPHER_HW_aria_mode(cfb1)
|
||||
PROV_CIPHER_HW_aria_mode(cfb8)
|
||||
PROV_CIPHER_HW_aria_mode(ctr)
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for Blowfish cipher modes ecb, cbc, ofb, cfb */
|
||||
|
||||
#include "cipher_blowfish.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn blowfish_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn blowfish_dupctx;
|
||||
|
||||
static void blowfish_freectx(void *vctx)
|
||||
{
|
||||
PROV_BLOWFISH_CTX *ctx = (PROV_BLOWFISH_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *blowfish_dupctx(void *ctx)
|
||||
{
|
||||
PROV_BLOWFISH_CTX *in = (PROV_BLOWFISH_CTX *)ctx;
|
||||
PROV_BLOWFISH_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* bf_ecb_functions */
|
||||
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
|
||||
/* bf_cbc_functions */
|
||||
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
|
||||
/* bf_ofb_functions */
|
||||
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
|
||||
/* bf_cfb_functions */
|
||||
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, cfb64, CFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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/blowfish.h>
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
|
||||
typedef struct prov_blowfish_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
BF_KEY ks;
|
||||
} ks;
|
||||
} PROV_BLOWFISH_CTX;
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_blowfish_cbc(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_blowfish_ecb(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_blowfish_ofb64(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_blowfish_cfb64(size_t keybits);
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 "cipher_blowfish.h"
|
||||
|
||||
static int cipher_hw_blowfish_initkey(PROV_CIPHER_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_BLOWFISH_CTX *bctx = (PROV_BLOWFISH_CTX *)ctx;
|
||||
|
||||
BF_set_key(&bctx->ks.ks, keylen, key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_blowfish_mode(mode, UCMODE) \
|
||||
IMPLEMENT_CIPHER_HW_##UCMODE(mode, blowfish, PROV_BLOWFISH_CTX, BF_KEY, \
|
||||
BF_##mode) \
|
||||
static const PROV_CIPHER_HW bf_##mode = { \
|
||||
cipher_hw_blowfish_initkey, \
|
||||
cipher_hw_blowfish_##mode##_cipher \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_blowfish_##mode(size_t keybits) \
|
||||
{ \
|
||||
return &bf_##mode; \
|
||||
}
|
||||
|
||||
PROV_CIPHER_HW_blowfish_mode(cbc, CBC)
|
||||
PROV_CIPHER_HW_blowfish_mode(ecb, ECB)
|
||||
PROV_CIPHER_HW_blowfish_mode(ofb64, OFB)
|
||||
PROV_CIPHER_HW_blowfish_mode(cfb64, CFB)
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for CAMELLIA cipher modes ecb, cbc, ofb, cfb, ctr */
|
||||
|
||||
#include "cipher_camellia.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn camellia_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn camellia_dupctx;
|
||||
|
||||
static void camellia_freectx(void *vctx)
|
||||
{
|
||||
PROV_CAMELLIA_CTX *ctx = (PROV_CAMELLIA_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *camellia_dupctx(void *ctx)
|
||||
{
|
||||
PROV_CAMELLIA_CTX *in = (PROV_CAMELLIA_CTX *)ctx;
|
||||
PROV_CAMELLIA_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* camellia256ecb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ecb, ECB, 0, 256, 128, 0, block)
|
||||
/* camellia192ecb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ecb, ECB, 0, 192, 128, 0, block)
|
||||
/* camellia128ecb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ecb, ECB, 0, 128, 128, 0, block)
|
||||
/* camellia256cbc_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cbc, CBC, 0, 256, 128, 128, block)
|
||||
/* camellia192cbc_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cbc, CBC, 0, 192, 128, 128, block)
|
||||
/* camellia128cbc_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cbc, CBC, 0, 128, 128, 128, block)
|
||||
/* camellia256ofb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ofb, OFB, 0, 256, 8, 128, stream)
|
||||
/* camellia192ofb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ofb, OFB, 0, 192, 8, 128, stream)
|
||||
/* camellia128ofb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ofb, OFB, 0, 128, 8, 128, stream)
|
||||
/* camellia256cfb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb, CFB, 0, 256, 8, 128, stream)
|
||||
/* camellia192cfb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb, CFB, 0, 192, 8, 128, stream)
|
||||
/* camellia128cfb_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb, CFB, 0, 128, 8, 128, stream)
|
||||
/* camellia256cfb1_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb1, CFB, 0, 256, 8, 128, stream)
|
||||
/* camellia192cfb1_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb1, CFB, 0, 192, 8, 128, stream)
|
||||
/* camellia128cfb1_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb1, CFB, 0, 128, 8, 128, stream)
|
||||
/* camellia256cfb8_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb8, CFB, 0, 256, 8, 128, stream)
|
||||
/* camellia192cfb8_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb8, CFB, 0, 192, 8, 128, stream)
|
||||
/* camellia128cfb8_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, cfb8, CFB, 0, 128, 8, 128, stream)
|
||||
/* camellia256ctr_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ctr, CTR, 0, 256, 8, 128, stream)
|
||||
/* camellia192ctr_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ctr, CTR, 0, 192, 8, 128, stream)
|
||||
/* camellia128ctr_functions */
|
||||
IMPLEMENT_generic_cipher(camellia, CAMELLIA, ctr, CTR, 0, 128, 8, 128, stream)
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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/camellia.h"
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
|
||||
typedef struct prov_camellia_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
CAMELLIA_KEY ks;
|
||||
} ks;
|
||||
} PROV_CAMELLIA_CTX;
|
||||
|
||||
#define PROV_CIPHER_HW_camellia_ofb PROV_CIPHER_HW_camellia_ofb128
|
||||
#define PROV_CIPHER_HW_camellia_cfb PROV_CIPHER_HW_camellia_cfb128
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_ecb(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_cbc(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_ofb128(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_cfb128(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_cfb1(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_cfb8(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_ctr(size_t keybits);
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2001-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 "cipher_camellia.h"
|
||||
#include <openssl/camellia.h>
|
||||
|
||||
static int cipher_hw_camellia_initkey(PROV_CIPHER_CTX *dat,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
int ret, mode = dat->mode;
|
||||
PROV_CAMELLIA_CTX *adat = (PROV_CAMELLIA_CTX *)dat;
|
||||
CAMELLIA_KEY *ks = &adat->ks.ks;
|
||||
|
||||
dat->ks = ks;
|
||||
ret = Camellia_set_key(key, keylen * 8, ks);
|
||||
if (ret < 0) {
|
||||
ERR_raise(ERR_LIB_PROV, EVP_R_ARIA_KEY_SETUP_FAILED);
|
||||
return 0;
|
||||
}
|
||||
if (dat->enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE)) {
|
||||
dat->block = (block128_f) Camellia_encrypt;
|
||||
dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
|
||||
(cbc128_f) Camellia_cbc_encrypt : NULL;
|
||||
} else {
|
||||
dat->block = (block128_f) Camellia_decrypt;
|
||||
dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
|
||||
(cbc128_f) Camellia_cbc_encrypt : NULL;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
# if defined(SPARC_CMLL_CAPABLE)
|
||||
# include "cipher_camellia_hw_t4.inc"
|
||||
# else
|
||||
/* The generic case */
|
||||
# define PROV_CIPHER_HW_declare(mode)
|
||||
# define PROV_CIPHER_HW_select(mode)
|
||||
# endif /* SPARC_CMLL_CAPABLE */
|
||||
|
||||
#define PROV_CIPHER_HW_camellia_mode(mode) \
|
||||
static const PROV_CIPHER_HW camellia_##mode = { \
|
||||
cipher_hw_camellia_initkey, \
|
||||
cipher_hw_generic_##mode \
|
||||
}; \
|
||||
PROV_CIPHER_HW_declare(mode) \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_camellia_##mode(size_t keybits) \
|
||||
{ \
|
||||
PROV_CIPHER_HW_select(mode) \
|
||||
return &camellia_##mode; \
|
||||
}
|
||||
|
||||
PROV_CIPHER_HW_camellia_mode(cbc)
|
||||
PROV_CIPHER_HW_camellia_mode(ecb)
|
||||
PROV_CIPHER_HW_camellia_mode(ofb128)
|
||||
PROV_CIPHER_HW_camellia_mode(cfb128)
|
||||
PROV_CIPHER_HW_camellia_mode(cfb1)
|
||||
PROV_CIPHER_HW_camellia_mode(cfb8)
|
||||
PROV_CIPHER_HW_camellia_mode(ctr)
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2001-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
|
||||
*/
|
||||
|
||||
/*-
|
||||
* Fujitsu SPARC64 X support for camellia modes.
|
||||
* This file is included by cipher_camellia_hw.c
|
||||
*/
|
||||
|
||||
static int cipher_hw_camellia_t4_initkey(PROV_CIPHER_CTX *dat,
|
||||
const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
int ret = 0, bits, mode = dat->mode;
|
||||
PROV_CAMELLIA_CTX *adat = (PROV_CAMELLIA_CTX *)dat;
|
||||
CAMELLIA_KEY *ks = &adat->ks.ks;
|
||||
|
||||
dat->ks = ks;
|
||||
bits = keylen * 8;
|
||||
|
||||
cmll_t4_set_key(key, bits, ks);
|
||||
|
||||
if (dat->enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE)) {
|
||||
dat->block = (block128_f) cmll_t4_encrypt;
|
||||
switch (bits) {
|
||||
case 128:
|
||||
if (mode == EVP_CIPH_CBC_MODE)
|
||||
dat->stream.cbc = (cbc128_f) cmll128_t4_cbc_encrypt;
|
||||
else if (mode == EVP_CIPH_CTR_MODE)
|
||||
dat->stream.ctr = (ctr128_f) cmll128_t4_ctr32_encrypt;
|
||||
else
|
||||
dat->stream.cbc = NULL;
|
||||
break;
|
||||
case 192:
|
||||
case 256:
|
||||
if (mode == EVP_CIPH_CBC_MODE)
|
||||
dat->stream.cbc = (cbc128_f) cmll256_t4_cbc_encrypt;
|
||||
else if (mode == EVP_CIPH_CTR_MODE)
|
||||
dat->stream.ctr = (ctr128_f) cmll256_t4_ctr32_encrypt;
|
||||
else
|
||||
dat->stream.cbc = NULL;
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
dat->block = (block128_f) cmll_t4_decrypt;
|
||||
switch (bits) {
|
||||
case 128:
|
||||
dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
|
||||
(cbc128_f) cmll128_t4_cbc_decrypt : NULL;
|
||||
break;
|
||||
case 192:
|
||||
case 256:
|
||||
dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
|
||||
(cbc128_f) cmll256_t4_cbc_decrypt : NULL;
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
ERR_raise(ERR_LIB_PROV, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define PROV_CIPHER_HW_declare(mode) \
|
||||
static const PROV_CIPHER_HW t4_camellia_##mode = { \
|
||||
cipher_hw_camellia_t4_initkey, \
|
||||
cipher_hw_generic_##mode \
|
||||
};
|
||||
#define PROV_CIPHER_HW_select(mode) \
|
||||
if (SPARC_CMLL_CAPABLE) \
|
||||
return &t4_camellia_##mode;
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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/cast.h>
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
|
||||
typedef struct prov_cast_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
CAST_KEY ks;
|
||||
} ks;
|
||||
} PROV_CAST_CTX;
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_cast5_cbc(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_cast5_ecb(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_cast5_ofb64(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_cast5_cfb64(size_t keybits);
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for cast cipher modes ecb, cbc, ofb, cfb */
|
||||
|
||||
#include "cipher_cast.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn cast5_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn cast5_dupctx;
|
||||
|
||||
static void cast5_freectx(void *vctx)
|
||||
{
|
||||
PROV_CAST_CTX *ctx = (PROV_CAST_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *cast5_dupctx(void *ctx)
|
||||
{
|
||||
PROV_CAST_CTX *in = (PROV_CAST_CTX *)ctx;
|
||||
PROV_CAST_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* cast5128ecb_functions */
|
||||
IMPLEMENT_generic_cipher(cast5, CAST, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
|
||||
/* cast5128cbc_functions */
|
||||
IMPLEMENT_generic_cipher(cast5, CAST, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
|
||||
/* cast564ofb64_functions */
|
||||
IMPLEMENT_generic_cipher(cast5, CAST, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
|
||||
/* cast564cfb64_functions */
|
||||
IMPLEMENT_generic_cipher(cast5, CAST, cfb64, CFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 "cipher_cast.h"
|
||||
|
||||
static int cipher_hw_cast5_initkey(PROV_CIPHER_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_CAST_CTX *bctx = (PROV_CAST_CTX *)ctx;
|
||||
|
||||
CAST_set_key(&(bctx->ks.ks), keylen, key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_cast_mode(mode, UCMODE) \
|
||||
IMPLEMENT_CIPHER_HW_##UCMODE(mode, cast5, PROV_CAST_CTX, CAST_KEY, \
|
||||
CAST_##mode) \
|
||||
static const PROV_CIPHER_HW cast5_##mode = { \
|
||||
cipher_hw_cast5_initkey, \
|
||||
cipher_hw_cast5_##mode##_cipher \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_cast5_##mode(size_t keybits) \
|
||||
{ \
|
||||
return &cast5_##mode; \
|
||||
}
|
||||
|
||||
PROV_CIPHER_HW_cast_mode(cbc, CBC)
|
||||
PROV_CIPHER_HW_cast_mode(ecb, ECB)
|
||||
PROV_CIPHER_HW_cast_mode(ofb64, OFB)
|
||||
PROV_CIPHER_HW_cast_mode(cfb64, CFB)
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* 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 "cipher_tdes_default.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/* desx_cbc_functions */
|
||||
IMPLEMENT_tdes_cipher(desx, DESX, cbc, CBC, TDES_FLAGS, 64*3, 64, 64, block);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 1995-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 "cipher_tdes_default.h"
|
||||
|
||||
/*
|
||||
* Note the PROV_TDES_CTX has been used for the DESX cipher, just to reduce
|
||||
* code size.
|
||||
*/
|
||||
#define ks1 tks.ks[0]
|
||||
#define ks2 tks.ks[1].ks[0].cblock
|
||||
#define ks3 tks.ks[2].ks[0].cblock
|
||||
|
||||
static int cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
|
||||
DES_cblock *deskey = (DES_cblock *)key;
|
||||
|
||||
DES_set_key_unchecked(deskey, &tctx->ks1);
|
||||
memcpy(&tctx->ks2, &key[8], 8);
|
||||
memcpy(&tctx->ks3, &key[16], 8);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
|
||||
|
||||
while (inl >= MAXCHUNK) {
|
||||
DES_xcbc_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1,
|
||||
(DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
|
||||
ctx->enc);
|
||||
inl -= MAXCHUNK;
|
||||
in += MAXCHUNK;
|
||||
out += MAXCHUNK;
|
||||
}
|
||||
if (inl > 0)
|
||||
DES_xcbc_encrypt(in, out, (long)inl, &tctx->ks1,
|
||||
(DES_cblock *)ctx->iv, &tctx->ks2, &tctx->ks3,
|
||||
ctx->enc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const PROV_CIPHER_HW desx_cbc =
|
||||
{
|
||||
cipher_hw_desx_cbc_initkey,
|
||||
cipher_hw_desx_cbc
|
||||
};
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_desx_cbc(void)
|
||||
{
|
||||
return &desx_cbc;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for Idea cipher modes ecb, cbc, ofb, cfb */
|
||||
|
||||
#include "cipher_idea.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn idea_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn idea_dupctx;
|
||||
|
||||
static void idea_freectx(void *vctx)
|
||||
{
|
||||
PROV_IDEA_CTX *ctx = (PROV_IDEA_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *idea_dupctx(void *ctx)
|
||||
{
|
||||
PROV_IDEA_CTX *in = (PROV_IDEA_CTX *)ctx;
|
||||
PROV_IDEA_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* idea128ecb_functions */
|
||||
IMPLEMENT_generic_cipher(idea, IDEA, ecb, ECB, 0, 128, 64, 0, block)
|
||||
/* idea128cbc_functions */
|
||||
IMPLEMENT_generic_cipher(idea, IDEA, cbc, CBC, 0, 128, 64, 64, block)
|
||||
/* idea128ofb64_functions */
|
||||
IMPLEMENT_generic_cipher(idea, IDEA, ofb64, OFB, 0, 128, 8, 64, stream)
|
||||
/* idea128cfb64_functions */
|
||||
IMPLEMENT_generic_cipher(idea, IDEA, cfb64, CFB, 0, 128, 8, 64, stream)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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/idea.h>
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
|
||||
typedef struct prov_idea_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
IDEA_KEY_SCHEDULE ks;
|
||||
} ks;
|
||||
} PROV_IDEA_CTX;
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_idea_cbc(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_idea_ecb(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_idea_ofb64(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_idea_cfb64(size_t keybits);
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 "cipher_idea.h"
|
||||
|
||||
static int cipher_hw_idea_initkey(PROV_CIPHER_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_IDEA_CTX *ictx = (PROV_IDEA_CTX *)ctx;
|
||||
IDEA_KEY_SCHEDULE *ks = &(ictx->ks.ks);
|
||||
|
||||
if (ctx->enc
|
||||
|| ctx->mode == EVP_CIPH_OFB_MODE
|
||||
|| ctx->mode == EVP_CIPH_CFB_MODE) {
|
||||
IDEA_set_encrypt_key(key, ks);
|
||||
} else {
|
||||
IDEA_KEY_SCHEDULE tmp;
|
||||
|
||||
IDEA_set_encrypt_key(key, &tmp);
|
||||
IDEA_set_decrypt_key(&tmp, ks);
|
||||
OPENSSL_cleanse((unsigned char *)&tmp, sizeof(IDEA_KEY_SCHEDULE));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, fname) \
|
||||
IMPLEMENT_CIPHER_HW_##UCMODE(mode, idea, PROV_IDEA_CTX, IDEA_KEY_SCHEDULE, \
|
||||
fname) \
|
||||
static const PROV_CIPHER_HW idea_##mode = { \
|
||||
cipher_hw_idea_initkey, \
|
||||
cipher_hw_idea_##mode##_cipher \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_idea_##mode(size_t keybits) \
|
||||
{ \
|
||||
return &idea_##mode; \
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_idea_mode(mode, UCMODE) \
|
||||
PROV_CIPHER_HW_idea_mode_ex(mode, UCMODE, IDEA_##mode)
|
||||
|
||||
PROV_CIPHER_HW_idea_mode(cbc, CBC)
|
||||
PROV_CIPHER_HW_idea_mode(ofb64, OFB)
|
||||
PROV_CIPHER_HW_idea_mode(cfb64, CFB)
|
||||
/*
|
||||
* IDEA_ecb_encrypt() does not have a enc parameter - so we create a macro
|
||||
* that ignores this parameter when IMPLEMENT_CIPHER_HW_ecb() is called.
|
||||
*/
|
||||
#define IDEA2_ecb_encrypt(in, out, ks, enc) IDEA_ecb_encrypt(in, out, ks)
|
||||
|
||||
PROV_CIPHER_HW_idea_mode_ex(ecb, ECB, IDEA2_ecb)
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for Seed cipher modes ecb, cbc, ofb, cfb */
|
||||
|
||||
#include "cipher_seed.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/* TODO (3.0) Figure out what flags are required */
|
||||
#define SEED_FLAGS EVP_CIPH_FLAG_DEFAULT_ASN1
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn seed_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn seed_dupctx;
|
||||
|
||||
static void seed_freectx(void *vctx)
|
||||
{
|
||||
PROV_SEED_CTX *ctx = (PROV_SEED_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *seed_dupctx(void *ctx)
|
||||
{
|
||||
PROV_SEED_CTX *in = (PROV_SEED_CTX *)ctx;
|
||||
PROV_SEED_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* seed128ecb_functions */
|
||||
IMPLEMENT_generic_cipher(seed, SEED, ecb, ECB, SEED_FLAGS, 128, 128, 0, block)
|
||||
/* seed128cbc_functions */
|
||||
IMPLEMENT_generic_cipher(seed, SEED, cbc, CBC, SEED_FLAGS, 128, 128, 128, block)
|
||||
/* seed128ofb128_functions */
|
||||
IMPLEMENT_generic_cipher(seed, SEED, ofb128, OFB, SEED_FLAGS, 128, 8, 128, stream)
|
||||
/* seed128cfb128_functions */
|
||||
IMPLEMENT_generic_cipher(seed, SEED, cfb128, CFB, SEED_FLAGS, 128, 8, 128, stream)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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/seed.h>
|
||||
#include "internal/ciphers/ciphercommon.h"
|
||||
|
||||
typedef struct prov_seed_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
SEED_KEY_SCHEDULE ks;
|
||||
} ks;
|
||||
} PROV_SEED_CTX;
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_seed_cbc(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_seed_ecb(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_seed_ofb128(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_seed_cfb128(size_t keybits);
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 "cipher_seed.h"
|
||||
|
||||
static int cipher_hw_seed_initkey(PROV_CIPHER_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_SEED_CTX *sctx = (PROV_SEED_CTX *)ctx;
|
||||
|
||||
SEED_set_key(key, &(sctx->ks.ks));
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_seed_mode(mode, UCMODE) \
|
||||
IMPLEMENT_CIPHER_HW_##UCMODE(mode, seed, PROV_SEED_CTX, SEED_KEY_SCHEDULE, \
|
||||
SEED_##mode) \
|
||||
static const PROV_CIPHER_HW seed_##mode = { \
|
||||
cipher_hw_seed_initkey, \
|
||||
cipher_hw_seed_##mode##_cipher \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_seed_##mode(size_t keybits) \
|
||||
{ \
|
||||
return &seed_##mode; \
|
||||
}
|
||||
|
||||
PROV_CIPHER_HW_seed_mode(cbc, CBC)
|
||||
PROV_CIPHER_HW_seed_mode(ecb, ECB)
|
||||
PROV_CIPHER_HW_seed_mode(ofb128, OFB)
|
||||
PROV_CIPHER_HW_seed_mode(cfb128, CFB)
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* Dispatch functions for cast cipher modes ecb, cbc, ofb, cfb */
|
||||
|
||||
#include "cipher_sm4.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/* TODO (3.0) Figure out what flags to pass */
|
||||
#define SM4_FLAGS EVP_CIPH_FLAG_DEFAULT_ASN1
|
||||
|
||||
static OSSL_OP_cipher_freectx_fn sm4_freectx;
|
||||
static OSSL_OP_cipher_dupctx_fn sm4_dupctx;
|
||||
|
||||
static void sm4_freectx(void *vctx)
|
||||
{
|
||||
PROV_SM4_CTX *ctx = (PROV_SM4_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void *sm4_dupctx(void *ctx)
|
||||
{
|
||||
PROV_SM4_CTX *in = (PROV_SM4_CTX *)ctx;
|
||||
PROV_SM4_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
*ret = *in;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* sm4128ecb_functions */
|
||||
IMPLEMENT_generic_cipher(sm4, SM4, ecb, ECB, SM4_FLAGS, 128, 128, 0, block)
|
||||
/* sm4128cbc_functions */
|
||||
IMPLEMENT_generic_cipher(sm4, SM4, cbc, CBC, SM4_FLAGS, 128, 128, 128, block)
|
||||
/* sm4128ctr_functions */
|
||||
IMPLEMENT_generic_cipher(sm4, SM4, ctr, CTR, SM4_FLAGS, 128, 8, 128, stream)
|
||||
/* sm4128ofb128_functions */
|
||||
IMPLEMENT_generic_cipher(sm4, SM4, ofb128, OFB, SM4_FLAGS, 128, 8, 128, stream)
|
||||
/* sm4128cfb128_functions */
|
||||
IMPLEMENT_generic_cipher(sm4, SM4, cfb128, CFB, SM4_FLAGS, 128, 8, 128, stream)
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 "internal/ciphers/ciphercommon.h"
|
||||
#include "internal/sm4.h"
|
||||
|
||||
typedef struct prov_cast_ctx_st {
|
||||
PROV_CIPHER_CTX base; /* Must be first */
|
||||
union {
|
||||
OSSL_UNION_ALIGN;
|
||||
SM4_KEY ks;
|
||||
} ks;
|
||||
} PROV_SM4_CTX;
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_sm4_cbc(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_sm4_ecb(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_sm4_ctr(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_sm4_ofb128(size_t keybits);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_sm4_cfb128(size_t keybits);
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 "cipher_sm4.h"
|
||||
|
||||
static int cipher_hw_sm4_initkey(PROV_CIPHER_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_SM4_CTX *sctx = (PROV_SM4_CTX *)ctx;
|
||||
SM4_KEY *ks = &sctx->ks.ks;
|
||||
|
||||
SM4_set_key(key, ks);
|
||||
ctx->ks = ks;
|
||||
if (ctx->enc
|
||||
|| (ctx->mode != EVP_CIPH_ECB_MODE
|
||||
&& ctx->mode != EVP_CIPH_CBC_MODE))
|
||||
ctx->block = (block128_f)SM4_encrypt;
|
||||
else
|
||||
ctx->block = (block128_f)SM4_decrypt;
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define PROV_CIPHER_HW_sm4_mode(mode) \
|
||||
static const PROV_CIPHER_HW sm4_##mode = { \
|
||||
cipher_hw_sm4_initkey, \
|
||||
cipher_hw_chunked_##mode \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_sm4_##mode(size_t keybits) \
|
||||
{ \
|
||||
return &sm4_##mode; \
|
||||
}
|
||||
|
||||
PROV_CIPHER_HW_sm4_mode(cbc)
|
||||
PROV_CIPHER_HW_sm4_mode(ecb)
|
||||
PROV_CIPHER_HW_sm4_mode(ofb128)
|
||||
PROV_CIPHER_HW_sm4_mode(cfb128)
|
||||
PROV_CIPHER_HW_sm4_mode(ctr)
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 "cipher_tdes_default.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/* tdes_ede3_ofb_functions */
|
||||
IMPLEMENT_tdes_cipher(ede3, EDE3, ofb, OFB, TDES_FLAGS, 64*3, 8, 64, stream);
|
||||
/* tdes_ede3_cfb_functions */
|
||||
IMPLEMENT_tdes_cipher(ede3, EDE3, cfb, CFB, TDES_FLAGS, 64*3, 8, 64, stream);
|
||||
/* tdes_ede3_cfb1_functions */
|
||||
IMPLEMENT_tdes_cipher(ede3, EDE3, cfb1, CFB, TDES_FLAGS, 64*3, 8, 64, stream);
|
||||
/* tdes_ede3_cfb8_functions */
|
||||
IMPLEMENT_tdes_cipher(ede3, EDE3, cfb8, CFB, TDES_FLAGS, 64*3, 8, 64, stream);
|
||||
|
||||
/* tdes_ede2_ecb_functions */
|
||||
IMPLEMENT_tdes_cipher(ede2, EDE2, ecb, ECB, TDES_FLAGS, 64*2, 64, 64, block);
|
||||
/* tdes_ede2_cbc_functions */
|
||||
IMPLEMENT_tdes_cipher(ede2, EDE2, cbc, CBC, TDES_FLAGS, 64*2, 64, 64, block);
|
||||
/* tdes_ede2_ofb_functions */
|
||||
IMPLEMENT_tdes_cipher(ede2, EDE2, ofb, OFB, TDES_FLAGS, 64*2, 8, 64, stream);
|
||||
/* tdes_ede2_cfb_functions */
|
||||
IMPLEMENT_tdes_cipher(ede2, EDE2, cfb, CFB, TDES_FLAGS, 64*2, 8, 64, stream);
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 1995-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 "internal/ciphers/ciphercommon.h"
|
||||
#include "internal/ciphers/cipher_tdes.h"
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_ofb(void);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_cfb(void);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_cfb1(void);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede3_cfb8(void);
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede2_cbc(void);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede2_ecb(void);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede2_ofb(void);
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_ede2_cfb(void);
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_desx_cbc(void);
|
||||
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_wrap_cbc(void);
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 1995-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 "cipher_tdes_default.h"
|
||||
|
||||
#define ks1 tks.ks[0]
|
||||
#define ks2 tks.ks[1]
|
||||
#define ks3 tks.ks[2]
|
||||
|
||||
static int cipher_hw_tdes_ede2_initkey(PROV_CIPHER_CTX *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
|
||||
DES_cblock *deskey = (DES_cblock *)key;
|
||||
|
||||
tctx->tstream.cbc = NULL;
|
||||
# if defined(SPARC_DES_CAPABLE)
|
||||
if (SPARC_DES_CAPABLE) {
|
||||
if (ctx->mode == EVP_CIPH_CBC_MODE) {
|
||||
des_t4_key_expand(&deskey[0], &tctx->ks1);
|
||||
des_t4_key_expand(&deskey[1], &tctx->ks2);
|
||||
memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
|
||||
tctx->tstream.cbc = ctx->enc ? des_t4_ede3_cbc_encrypt :
|
||||
des_t4_ede3_cbc_decrypt;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
# endif
|
||||
DES_set_key_unchecked(&deskey[0], &tctx->ks1);
|
||||
DES_set_key_unchecked(&deskey[1], &tctx->ks2);
|
||||
memcpy(&tctx->ks3, &tctx->ks1, sizeof(tctx->ks1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cipher_hw_tdes_ofb(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
|
||||
int num = ctx->num;
|
||||
|
||||
while (inl >= MAXCHUNK) {
|
||||
DES_ede3_ofb64_encrypt(in, out, (long)MAXCHUNK, &tctx->ks1, &tctx->ks2,
|
||||
&tctx->ks3, (DES_cblock *)ctx->iv, &num);
|
||||
inl -= MAXCHUNK;
|
||||
in += MAXCHUNK;
|
||||
out += MAXCHUNK;
|
||||
}
|
||||
if (inl > 0) {
|
||||
DES_ede3_ofb64_encrypt(in, out, (long)inl, &tctx->ks1, &tctx->ks2,
|
||||
&tctx->ks3, (DES_cblock *)ctx->iv, &num);
|
||||
}
|
||||
ctx->num = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cipher_hw_tdes_cfb(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
|
||||
int num = ctx->num;
|
||||
|
||||
while (inl >= MAXCHUNK) {
|
||||
|
||||
DES_ede3_cfb64_encrypt(in, out, (long)MAXCHUNK,
|
||||
&tctx->ks1, &tctx->ks2, &tctx->ks3,
|
||||
(DES_cblock *)ctx->iv, &num, ctx->enc);
|
||||
inl -= MAXCHUNK;
|
||||
in += MAXCHUNK;
|
||||
out += MAXCHUNK;
|
||||
}
|
||||
if (inl > 0) {
|
||||
DES_ede3_cfb64_encrypt(in, out, (long)inl,
|
||||
&tctx->ks1, &tctx->ks2, &tctx->ks3,
|
||||
(DES_cblock *)ctx->iv, &num, ctx->enc);
|
||||
}
|
||||
ctx->num = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Although we have a CFB-r implementation for 3-DES, it doesn't pack the
|
||||
* right way, so wrap it here
|
||||
*/
|
||||
static int cipher_hw_tdes_cfb1(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
|
||||
size_t n;
|
||||
unsigned char c[1], d[1];
|
||||
|
||||
if ((ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) == 0)
|
||||
inl *= 8;
|
||||
for (n = 0; n < inl; ++n) {
|
||||
c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
|
||||
DES_ede3_cfb_encrypt(c, d, 1, 1,
|
||||
&tctx->ks1, &tctx->ks2, &tctx->ks3,
|
||||
(DES_cblock *)ctx->iv, ctx->enc);
|
||||
out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
|
||||
| ((d[0] & 0x80) >> (unsigned int)(n % 8));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cipher_hw_tdes_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = (PROV_TDES_CTX *)ctx;
|
||||
|
||||
while (inl >= MAXCHUNK) {
|
||||
DES_ede3_cfb_encrypt(in, out, 8, (long)MAXCHUNK,
|
||||
&tctx->ks1, &tctx->ks2, &tctx->ks3,
|
||||
(DES_cblock *)ctx->iv, ctx->enc);
|
||||
inl -= MAXCHUNK;
|
||||
in += MAXCHUNK;
|
||||
out += MAXCHUNK;
|
||||
}
|
||||
if (inl > 0)
|
||||
DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
|
||||
&tctx->ks1, &tctx->ks2, &tctx->ks3,
|
||||
(DES_cblock *)ctx->iv, ctx->enc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
PROV_CIPHER_HW_tdes_mode(ede3, ofb)
|
||||
PROV_CIPHER_HW_tdes_mode(ede3, cfb)
|
||||
PROV_CIPHER_HW_tdes_mode(ede3, cfb1)
|
||||
PROV_CIPHER_HW_tdes_mode(ede3, cfb8)
|
||||
|
||||
PROV_CIPHER_HW_tdes_mode(ede2, ecb)
|
||||
PROV_CIPHER_HW_tdes_mode(ede2, cbc)
|
||||
PROV_CIPHER_HW_tdes_mode(ede2, ofb)
|
||||
PROV_CIPHER_HW_tdes_mode(ede2, cfb)
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright 1995-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/sha.h>
|
||||
#include "cipher_tdes_default.h"
|
||||
#include "internal/evp_int.h"
|
||||
#include "internal/rand_int.h"
|
||||
#include "internal/provider_algs.h"
|
||||
#include "internal/providercommonerr.h"
|
||||
|
||||
/* TODO (3.0) Figure out what flags are requred */
|
||||
#define TDES_WRAP_FLAGS (EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV \
|
||||
| EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_FLAG_DEFAULT_ASN1)
|
||||
|
||||
|
||||
static OSSL_OP_cipher_update_fn tdes_wrap_update;
|
||||
static OSSL_OP_cipher_cipher_fn tdes_wrap_cipher;
|
||||
|
||||
static const unsigned char wrap_iv[8] =
|
||||
{
|
||||
0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
|
||||
};
|
||||
|
||||
static int des_ede3_unwrap(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
unsigned char icv[8], iv[TDES_IVLEN], sha1tmp[SHA_DIGEST_LENGTH];
|
||||
int rv = -1;
|
||||
|
||||
if (inl < 24)
|
||||
return -1;
|
||||
if (out == NULL)
|
||||
return inl - 16;
|
||||
|
||||
memcpy(ctx->iv, wrap_iv, 8);
|
||||
/* Decrypt first block which will end up as icv */
|
||||
ctx->hw->cipher(ctx, icv, in, 8);
|
||||
/* Decrypt central blocks */
|
||||
/*
|
||||
* If decrypting in place move whole output along a block so the next
|
||||
* des_ede_cbc_cipher is in place.
|
||||
*/
|
||||
if (out == in) {
|
||||
memmove(out, out + 8, inl - 8);
|
||||
in -= 8;
|
||||
}
|
||||
ctx->hw->cipher(ctx, out, in + 8, inl - 16);
|
||||
/* Decrypt final block which will be IV */
|
||||
ctx->hw->cipher(ctx, iv, in + inl - 8, 8);
|
||||
/* Reverse order of everything */
|
||||
BUF_reverse(icv, NULL, 8);
|
||||
BUF_reverse(out, NULL, inl - 16);
|
||||
BUF_reverse(ctx->iv, iv, 8);
|
||||
/* Decrypt again using new IV */
|
||||
ctx->hw->cipher(ctx, out, out, inl - 16);
|
||||
ctx->hw->cipher(ctx, icv, icv, 8);
|
||||
/* Work out SHA1 hash of first portion */
|
||||
SHA1(out, inl - 16, sha1tmp);
|
||||
|
||||
if (!CRYPTO_memcmp(sha1tmp, icv, 8))
|
||||
rv = inl - 16;
|
||||
OPENSSL_cleanse(icv, 8);
|
||||
OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
|
||||
OPENSSL_cleanse(iv, 8);
|
||||
OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
|
||||
if (rv == -1)
|
||||
OPENSSL_cleanse(out, inl - 16);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int des_ede3_wrap(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
unsigned char sha1tmp[SHA_DIGEST_LENGTH];
|
||||
size_t ivlen = TDES_IVLEN;
|
||||
size_t icvlen = TDES_IVLEN;
|
||||
size_t len = inl + ivlen + icvlen;
|
||||
|
||||
if (out == NULL)
|
||||
return len;
|
||||
|
||||
/* Copy input to output buffer + 8 so we have space for IV */
|
||||
memmove(out + ivlen, in, inl);
|
||||
/* Work out ICV */
|
||||
SHA1(in, inl, sha1tmp);
|
||||
memcpy(out + inl + ivlen, sha1tmp, icvlen);
|
||||
OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
|
||||
/* Generate random IV */
|
||||
if (rand_bytes_ex(ctx->libctx, ctx->iv, ivlen) <= 0)
|
||||
return 0;
|
||||
memcpy(out, ctx->iv, ivlen);
|
||||
/* Encrypt everything after IV in place */
|
||||
ctx->hw->cipher(ctx, out + ivlen, out + ivlen, inl + ivlen);
|
||||
BUF_reverse(out, NULL, len);
|
||||
memcpy(ctx->iv, wrap_iv, ivlen);
|
||||
ctx->hw->cipher(ctx, out, out, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
static int tdes_wrap_cipher_internal(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
/*
|
||||
* Sanity check input length: we typically only wrap keys so EVP_MAXCHUNK
|
||||
* is more than will ever be needed. Also input length must be a multiple
|
||||
* of 8 bits.
|
||||
*/
|
||||
if (inl >= EVP_MAXCHUNK || inl % 8)
|
||||
return -1;
|
||||
if (ctx->enc)
|
||||
return des_ede3_wrap(ctx, out, in, inl);
|
||||
else
|
||||
return des_ede3_unwrap(ctx, out, in, inl);
|
||||
}
|
||||
|
||||
static int tdes_wrap_cipher(void *vctx,
|
||||
unsigned char *out, size_t *outl, size_t outsize,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
||||
int ret;
|
||||
|
||||
*outl = 0;
|
||||
if (outsize < inl) {
|
||||
PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = tdes_wrap_cipher_internal(ctx, out, in, inl);
|
||||
if (ret <= 0)
|
||||
return 0;
|
||||
|
||||
*outl = ret;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int tdes_wrap_update(void *vctx, unsigned char *out, size_t *outl,
|
||||
size_t outsize, const unsigned char *in,
|
||||
size_t inl)
|
||||
{
|
||||
*outl = 0;
|
||||
if (outsize < inl) {
|
||||
PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!tdes_wrap_cipher(vctx, out, outl, outsize, in, inl)) {
|
||||
PROVerr(0, PROV_R_CIPHER_OPERATION_FAILED);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
# define IMPLEMENT_WRAP_CIPHER(flags, kbits, blkbits, ivbits) \
|
||||
static OSSL_OP_cipher_newctx_fn tdes_wrap_newctx; \
|
||||
static void *tdes_wrap_newctx(void *provctx) \
|
||||
{ \
|
||||
return tdes_newctx(provctx, EVP_CIPH_WRAP_MODE, kbits, blkbits, ivbits, \
|
||||
flags, PROV_CIPHER_HW_tdes_wrap_cbc()); \
|
||||
} \
|
||||
static OSSL_OP_cipher_get_params_fn tdes_wrap_get_params; \
|
||||
static int tdes_wrap_get_params(OSSL_PARAM params[]) \
|
||||
{ \
|
||||
return cipher_generic_get_params(params, EVP_CIPH_WRAP_MODE, flags, \
|
||||
kbits, blkbits, ivbits); \
|
||||
} \
|
||||
const OSSL_DISPATCH tdes_wrap_cbc_functions[] = \
|
||||
{ \
|
||||
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) tdes_einit }, \
|
||||
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) tdes_dinit }, \
|
||||
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))tdes_wrap_cipher }, \
|
||||
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))tdes_wrap_newctx }, \
|
||||
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))tdes_freectx }, \
|
||||
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))tdes_wrap_update }, \
|
||||
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_stream_final }, \
|
||||
{ OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))tdes_wrap_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 } \
|
||||
}
|
||||
|
||||
/* tdes_wrap_cbc_functions */
|
||||
IMPLEMENT_WRAP_CIPHER(TDES_WRAP_FLAGS, 64*3, 64, 0);
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 1995-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 "cipher_tdes_default.h"
|
||||
|
||||
#define cipher_hw_tdes_wrap_initkey cipher_hw_tdes_ede3_initkey
|
||||
|
||||
PROV_CIPHER_HW_tdes_mode(wrap, cbc)
|
||||
Reference in New Issue
Block a user