Latest update.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
SUBDIRS=digests
|
||||
SUBDIRS=digests macs ciphers
|
||||
SUBDIRS=digests kdfs macs ciphers
|
||||
LIBS=../../libcrypto
|
||||
SOURCE[../../libcrypto]=\
|
||||
defltprov.c
|
||||
INCLUDE[../../libcrypto]=include
|
||||
@@ -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)
|
||||
+206
-14
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <openssl/opensslconf.h>
|
||||
#include <openssl/core.h>
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
@@ -16,18 +17,18 @@
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/* Functions provided by the core */
|
||||
static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
|
||||
static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
|
||||
static OSSL_core_get_params_fn *c_get_params = NULL;
|
||||
|
||||
/* Parameters we provide to the core */
|
||||
static const OSSL_ITEM deflt_param_types[] = {
|
||||
{ OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
|
||||
{ OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
|
||||
{ OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
|
||||
{ 0, NULL }
|
||||
static const OSSL_PARAM deflt_param_types[] = {
|
||||
OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
|
||||
OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
|
||||
OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
static const OSSL_ITEM *deflt_get_param_types(const OSSL_PROVIDER *prov)
|
||||
static const OSSL_PARAM *deflt_gettable_params(const OSSL_PROVIDER *prov)
|
||||
{
|
||||
return deflt_param_types;
|
||||
}
|
||||
@@ -64,8 +65,12 @@ static const OSSL_ALGORITHM deflt_digests[] = {
|
||||
{ "SHA3-384", "default=yes", sha3_384_functions },
|
||||
{ "SHA3-512", "default=yes", sha3_512_functions },
|
||||
|
||||
{ "KMAC128", "default=yes", keccak_kmac_128_functions },
|
||||
{ "KMAC256", "default=yes", keccak_kmac_256_functions },
|
||||
/*
|
||||
* KECCAK_KMAC128 and KECCAK_KMAC256 as hashes are mostly useful for
|
||||
* the KMAC128 and KMAC256.
|
||||
*/
|
||||
{ "KECCAK_KMAC128", "default=yes", keccak_kmac_128_functions },
|
||||
{ "KECCAK_KMAC256", "default=yes", keccak_kmac_256_functions },
|
||||
|
||||
{ "SHAKE128", "default=yes", shake_128_functions },
|
||||
{ "SHAKE256", "default=yes", shake_256_functions },
|
||||
@@ -84,8 +89,6 @@ static const OSSL_ALGORITHM deflt_digests[] = {
|
||||
{ "MD5-SHA1", "default=yes", md5_sha1_functions },
|
||||
#endif /* OPENSSL_NO_MD5 */
|
||||
|
||||
/*{ "UNDEF", "default=yes", nullmd_functions }, */
|
||||
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
@@ -111,6 +114,185 @@ static const OSSL_ALGORITHM deflt_ciphers[] = {
|
||||
{ "AES-256-CTR", "default=yes", aes256ctr_functions },
|
||||
{ "AES-192-CTR", "default=yes", aes192ctr_functions },
|
||||
{ "AES-128-CTR", "default=yes", aes128ctr_functions },
|
||||
{ "AES-256-XTS", "default=yes", aes256xts_functions },
|
||||
{ "AES-128-XTS", "default=yes", aes128xts_functions },
|
||||
#ifndef OPENSSL_NO_OCB
|
||||
{ "AES-256-OCB", "default=yes", aes256ocb_functions },
|
||||
{ "AES-192-OCB", "default=yes", aes192ocb_functions },
|
||||
{ "AES-128-OCB", "default=yes", aes128ocb_functions },
|
||||
#endif /* OPENSSL_NO_OCB */
|
||||
/* TODO(3.0) Add aliases when they are supported */
|
||||
{ "id-aes256-GCM", "default=yes", aes256gcm_functions },
|
||||
{ "id-aes192-GCM", "default=yes", aes192gcm_functions },
|
||||
{ "id-aes128-GCM", "default=yes", aes128gcm_functions },
|
||||
{ "id-aes256-CCM", "default=yes", aes256ccm_functions },
|
||||
{ "id-aes192-CCM", "default=yes", aes192ccm_functions },
|
||||
{ "id-aes128-CCM", "default=yes", aes128ccm_functions },
|
||||
{ "id-aes256-wrap", "default=yes", aes256wrap_functions },
|
||||
{ "id-aes192-wrap", "default=yes", aes192wrap_functions },
|
||||
{ "id-aes128-wrap", "default=yes", aes128wrap_functions },
|
||||
{ "id-aes256-wrap-pad", "default=yes", aes256wrappad_functions },
|
||||
{ "id-aes192-wrap-pad", "default=yes", aes192wrappad_functions },
|
||||
{ "id-aes128-wrap-pad", "default=yes", aes128wrappad_functions },
|
||||
#ifndef OPENSSL_NO_ARIA
|
||||
{ "ARIA-256-GCM", "default=yes", aria256gcm_functions },
|
||||
{ "ARIA-192-GCM", "default=yes", aria192gcm_functions },
|
||||
{ "ARIA-128-GCM", "default=yes", aria128gcm_functions },
|
||||
{ "ARIA-256-CCM", "default=yes", aria256ccm_functions },
|
||||
{ "ARIA-192-CCM", "default=yes", aria192ccm_functions },
|
||||
{ "ARIA-128-CCM", "default=yes", aria128ccm_functions },
|
||||
{ "ARIA-256-ECB", "default=yes", aria256ecb_functions },
|
||||
{ "ARIA-192-ECB", "default=yes", aria192ecb_functions },
|
||||
{ "ARIA-128-ECB", "default=yes", aria128ecb_functions },
|
||||
{ "ARIA-256-CBC", "default=yes", aria256cbc_functions },
|
||||
{ "ARIA-192-CBC", "default=yes", aria192cbc_functions },
|
||||
{ "ARIA-128-CBC", "default=yes", aria128cbc_functions },
|
||||
{ "ARIA-256-OFB", "default=yes", aria256ofb_functions },
|
||||
{ "ARIA-192-OFB", "default=yes", aria192ofb_functions },
|
||||
{ "ARIA-128-OFB", "default=yes", aria128ofb_functions },
|
||||
{ "ARIA-256-CFB", "default=yes", aria256cfb_functions },
|
||||
{ "ARIA-192-CFB", "default=yes", aria192cfb_functions },
|
||||
{ "ARIA-128-CFB", "default=yes", aria128cfb_functions },
|
||||
{ "ARIA-256-CFB1", "default=yes", aria256cfb1_functions },
|
||||
{ "ARIA-192-CFB1", "default=yes", aria192cfb1_functions },
|
||||
{ "ARIA-128-CFB1", "default=yes", aria128cfb1_functions },
|
||||
{ "ARIA-256-CFB8", "default=yes", aria256cfb8_functions },
|
||||
{ "ARIA-192-CFB8", "default=yes", aria192cfb8_functions },
|
||||
{ "ARIA-128-CFB8", "default=yes", aria128cfb8_functions },
|
||||
{ "ARIA-256-CTR", "default=yes", aria256ctr_functions },
|
||||
{ "ARIA-192-CTR", "default=yes", aria192ctr_functions },
|
||||
{ "ARIA-128-CTR", "default=yes", aria128ctr_functions },
|
||||
#endif /* OPENSSL_NO_ARIA */
|
||||
#ifndef OPENSSL_NO_CAMELLIA
|
||||
{ "CAMELLIA-256-ECB", "default=yes", camellia256ecb_functions },
|
||||
{ "CAMELLIA-192-ECB", "default=yes", camellia192ecb_functions },
|
||||
{ "CAMELLIA-128-ECB", "default=yes", camellia128ecb_functions },
|
||||
{ "CAMELLIA-256-CBC", "default=yes", camellia256cbc_functions },
|
||||
{ "CAMELLIA-192-CBC", "default=yes", camellia192cbc_functions },
|
||||
{ "CAMELLIA-128-CBC", "default=yes", camellia128cbc_functions },
|
||||
{ "CAMELLIA-256-OFB", "default=yes", camellia256ofb_functions },
|
||||
{ "CAMELLIA-192-OFB", "default=yes", camellia192ofb_functions },
|
||||
{ "CAMELLIA-128-OFB", "default=yes", camellia128ofb_functions },
|
||||
{ "CAMELLIA-256-CFB", "default=yes", camellia256cfb_functions },
|
||||
{ "CAMELLIA-192-CFB", "default=yes", camellia192cfb_functions },
|
||||
{ "CAMELLIA-128-CFB", "default=yes", camellia128cfb_functions },
|
||||
{ "CAMELLIA-256-CFB1", "default=yes", camellia256cfb1_functions },
|
||||
{ "CAMELLIA-192-CFB1", "default=yes", camellia192cfb1_functions },
|
||||
{ "CAMELLIA-128-CFB1", "default=yes", camellia128cfb1_functions },
|
||||
{ "CAMELLIA-256-CFB8", "default=yes", camellia256cfb8_functions },
|
||||
{ "CAMELLIA-192-CFB8", "default=yes", camellia192cfb8_functions },
|
||||
{ "CAMELLIA-128-CFB8", "default=yes", camellia128cfb8_functions },
|
||||
{ "CAMELLIA-256-CTR", "default=yes", camellia256ctr_functions },
|
||||
{ "CAMELLIA-192-CTR", "default=yes", camellia192ctr_functions },
|
||||
{ "CAMELLIA-128-CTR", "default=yes", camellia128ctr_functions },
|
||||
#endif /* OPENSSL_NO_CAMELLIA */
|
||||
#ifndef OPENSSL_NO_DES
|
||||
{ "DES-EDE3", "default=yes", tdes_ede3_ecb_functions },
|
||||
{ "DES-EDE3-CBC", "default=yes", tdes_ede3_cbc_functions },
|
||||
{ "DES-EDE3-OFB", "default=yes", tdes_ede3_ofb_functions },
|
||||
{ "DES-EDE3-CFB", "default=yes", tdes_ede3_cfb_functions },
|
||||
{ "DES-EDE3-CFB8", "default=yes", tdes_ede3_cfb8_functions },
|
||||
{ "DES-EDE3-CFB1", "default=yes", tdes_ede3_cfb1_functions },
|
||||
{ "DES-EDE", "default=yes", tdes_ede2_ecb_functions },
|
||||
{ "DES-EDE-CBC", "default=yes", tdes_ede2_cbc_functions },
|
||||
{ "DES-EDE-OFB", "default=yes", tdes_ede2_ofb_functions },
|
||||
{ "DES-EDE-CFB", "default=yes", tdes_ede2_cfb_functions },
|
||||
{ "DESX-CBC", "default=yes", tdes_desx_cbc_functions },
|
||||
{ "id-smime-alg-CMS3DESwrap", "default=yes", tdes_wrap_cbc_functions },
|
||||
#endif /* OPENSSL_NO_DES */
|
||||
#ifndef OPENSSL_NO_BF
|
||||
{ "BF-ECB", "default=yes", blowfish128ecb_functions },
|
||||
{ "BF-CBC", "default=yes", blowfish128cbc_functions },
|
||||
{ "BF-OFB", "default=yes", blowfish64ofb64_functions },
|
||||
{ "BF-CFB", "default=yes", blowfish64cfb64_functions },
|
||||
#endif /* OPENSSL_NO_BF */
|
||||
#ifndef OPENSSL_NO_IDEA
|
||||
{ "IDEA-ECB", "default=yes", idea128ecb_functions },
|
||||
{ "IDEA-CBC", "default=yes", idea128cbc_functions },
|
||||
{ "IDEA-OFB", "default=yes", idea128ofb64_functions },
|
||||
{ "IDEA-CFB", "default=yes", idea128cfb64_functions },
|
||||
#endif /* OPENSSL_NO_IDEA */
|
||||
#ifndef OPENSSL_NO_CAST
|
||||
{ "CAST5-ECB", "default=yes", cast5128ecb_functions },
|
||||
{ "CAST5-CBC", "default=yes", cast5128cbc_functions },
|
||||
{ "CAST5-OFB", "default=yes", cast564ofb64_functions },
|
||||
{ "CAST5-CFB", "default=yes", cast564cfb64_functions },
|
||||
#endif /* OPENSSL_NO_CAST */
|
||||
#ifndef OPENSSL_NO_SEED
|
||||
{ "SEED-ECB", "default=yes", seed128ecb_functions },
|
||||
{ "SEED-CBC", "default=yes", seed128cbc_functions },
|
||||
{ "SEED-OFB", "default=yes", seed128ofb128_functions },
|
||||
{ "SEED-CFB", "default=yes", seed128cfb128_functions },
|
||||
#endif /* OPENSSL_NO_SEED */
|
||||
#ifndef OPENSSL_NO_SM4
|
||||
{ "SM4-ECB", "default=yes", sm4128ecb_functions },
|
||||
{ "SM4-CBC", "default=yes", sm4128cbc_functions },
|
||||
{ "SM4-CTR", "default=yes", sm4128ctr_functions },
|
||||
{ "SM4-OFB", "default=yes", sm4128ofb128_functions },
|
||||
{ "SM4-CFB", "default=yes", sm4128cfb128_functions },
|
||||
#endif /* OPENSSL_NO_SM4 */
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
static const OSSL_ALGORITHM deflt_macs[] = {
|
||||
#ifndef OPENSSL_NO_BLAKE2
|
||||
{ "BLAKE2BMAC", "default=yes", blake2bmac_functions },
|
||||
{ "BLAKE2SMAC", "default=yes", blake2smac_functions },
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_CMAC
|
||||
{ "CMAC", "default=yes", cmac_functions },
|
||||
#endif
|
||||
{ "GMAC", "default=yes", gmac_functions },
|
||||
{ "HMAC", "default=yes", hmac_functions },
|
||||
{ "KMAC128", "default=yes", kmac128_functions },
|
||||
{ "KMAC256", "default=yes", kmac256_functions },
|
||||
#ifndef OPENSSL_NO_SIPHASH
|
||||
{ "SipHash", "default=yes", siphash_functions },
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_POLY1305
|
||||
{ "Poly1305", "default=yes", poly1305_functions },
|
||||
#endif
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
static const OSSL_ALGORITHM deflt_kdfs[] = {
|
||||
{ OSSL_KDF_NAME_HKDF, "default=yes", kdf_hkdf_functions },
|
||||
{ OSSL_KDF_NAME_SSKDF, "default=yes", kdf_sskdf_functions },
|
||||
{ OSSL_KDF_NAME_PBKDF2, "default=yes", kdf_pbkdf2_functions },
|
||||
{ OSSL_KDF_NAME_SSHKDF, "default=yes", kdf_sshkdf_functions },
|
||||
{ OSSL_KDF_NAME_X963KDF, "default=yes", kdf_x963_kdf_functions },
|
||||
{ OSSL_KDF_NAME_TLS1_PRF, "default=yes", kdf_tls1_prf_functions },
|
||||
#ifndef OPENSSL_NO_CMS
|
||||
{ OSSL_KDF_NAME_X942KDF, "default=yes", kdf_x942_kdf_functions },
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_SCRYPT
|
||||
{ OSSL_KDF_NAME_SCRYPT, "default=yes", kdf_scrypt_functions },
|
||||
#endif
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
static const OSSL_ALGORITHM deflt_keyexch[] = {
|
||||
#ifndef OPENSSL_NO_DH
|
||||
{ "dhKeyAgreement", "default=yes", dh_keyexch_functions },
|
||||
#endif
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
static const OSSL_ALGORITHM deflt_signature[] = {
|
||||
#ifndef OPENSSL_NO_DSA
|
||||
{ "DSA", "default=yes", dsa_signature_functions },
|
||||
#endif
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
|
||||
static const OSSL_ALGORITHM deflt_keymgmt[] = {
|
||||
#ifndef OPENSSL_NO_DH
|
||||
{ "dhKeyAgreement", "default=yes", dh_keymgmt_functions },
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_DSA
|
||||
{ "DSA", "default=yes", dsa_keymgmt_functions },
|
||||
#endif
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
@@ -124,13 +306,23 @@ static const OSSL_ALGORITHM *deflt_query(OSSL_PROVIDER *prov,
|
||||
return deflt_digests;
|
||||
case OSSL_OP_CIPHER:
|
||||
return deflt_ciphers;
|
||||
case OSSL_OP_MAC:
|
||||
return deflt_macs;
|
||||
case OSSL_OP_KDF:
|
||||
return deflt_kdfs;
|
||||
case OSSL_OP_KEYMGMT:
|
||||
return deflt_keymgmt;
|
||||
case OSSL_OP_KEYEXCH:
|
||||
return deflt_keyexch;
|
||||
case OSSL_OP_SIGNATURE:
|
||||
return deflt_signature;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Functions we provide to the core */
|
||||
static const OSSL_DISPATCH deflt_dispatch_table[] = {
|
||||
{ OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))deflt_get_param_types },
|
||||
{ OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
|
||||
{ OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
|
||||
{ OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
|
||||
{ 0, NULL }
|
||||
@@ -147,8 +339,8 @@ int ossl_default_provider_init(const OSSL_PROVIDER *provider,
|
||||
|
||||
for (; in->function_id != 0; in++) {
|
||||
switch (in->function_id) {
|
||||
case OSSL_FUNC_CORE_GET_PARAM_TYPES:
|
||||
c_get_param_types = OSSL_get_core_get_param_types(in);
|
||||
case OSSL_FUNC_CORE_GETTABLE_PARAMS:
|
||||
c_gettable_params = OSSL_get_core_gettable_params(in);
|
||||
break;
|
||||
case OSSL_FUNC_CORE_GET_PARAMS:
|
||||
c_get_params = OSSL_get_core_get_params(in);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include "internal/blake2.h"
|
||||
#include "internal/core_mkdigest.h"
|
||||
#include "internal/digestcommon.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
OSSL_OP_digest_init_fn blake2s256_init;
|
||||
@@ -31,10 +31,12 @@ int blake2b512_init(void *ctx)
|
||||
return blake2b_init((BLAKE2B_CTX *)ctx, &P);
|
||||
}
|
||||
|
||||
OSSL_FUNC_DIGEST_CONSTRUCT(blake2s256, BLAKE2S_CTX,
|
||||
BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH,
|
||||
/* blake2s256_functions */
|
||||
IMPLEMENT_digest_functions(blake2s256, BLAKE2S_CTX,
|
||||
BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0,
|
||||
blake2s256_init, blake2s_update, blake2s_final)
|
||||
|
||||
OSSL_FUNC_DIGEST_CONSTRUCT(blake2b512, BLAKE2B_CTX,
|
||||
BLAKE2B_BLOCKBYTES, BLAKE2B_DIGEST_LENGTH,
|
||||
/* blake2b512_functions */
|
||||
IMPLEMENT_digest_functions(blake2b512, BLAKE2B_CTX,
|
||||
BLAKE2B_BLOCKBYTES, BLAKE2B_DIGEST_LENGTH, 0,
|
||||
blake2b512_init, blake2b_update, blake2b_final)
|
||||
@@ -18,7 +18,6 @@
|
||||
#include <string.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include "blake2_impl.h"
|
||||
|
||||
#include "internal/blake2.h"
|
||||
|
||||
static const uint64_t blake2b_IV[8] =
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
SOURCE[../../../libcrypto]=\
|
||||
null_prov.c
|
||||
|
||||
IF[{- !$disabled{blake2} -}]
|
||||
SOURCE[../../../libcrypto]=\
|
||||
|
||||
@@ -9,9 +9,10 @@
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/md5.h>
|
||||
#include "internal/core_mkdigest.h"
|
||||
#include "internal/digestcommon.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
OSSL_FUNC_DIGEST_CONSTRUCT(md5, MD5_CTX,
|
||||
MD5_CBLOCK, MD5_DIGEST_LENGTH,
|
||||
/* md5_functions */
|
||||
IMPLEMENT_digest_functions(md5, MD5_CTX,
|
||||
MD5_CBLOCK, MD5_DIGEST_LENGTH, 0,
|
||||
MD5_Init, MD5_Update, MD5_Final)
|
||||
@@ -7,20 +7,30 @@
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include "internal/core_mkdigest.h"
|
||||
#include "internal/md5_sha1.h"
|
||||
#include "internal/digestcommon.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static OSSL_OP_digest_set_params_fn md5_sha1_set_params;
|
||||
static OSSL_OP_digest_set_ctx_params_fn md5_sha1_set_ctx_params;
|
||||
static OSSL_OP_digest_settable_ctx_params_fn md5_sha1_settable_ctx_params;
|
||||
|
||||
static const OSSL_PARAM known_md5_sha1_settable_ctx_params[] = {
|
||||
{OSSL_DIGEST_PARAM_SSL3_MS, OSSL_PARAM_OCTET_STRING, NULL, 0, 0},
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
static const OSSL_PARAM *md5_sha1_settable_ctx_params(void)
|
||||
{
|
||||
return known_md5_sha1_settable_ctx_params;
|
||||
}
|
||||
|
||||
/* Special set_params method for SSL3 */
|
||||
static int md5_sha1_set_params(void *vctx, const OSSL_PARAM params[])
|
||||
static int md5_sha1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *p;
|
||||
MD5_SHA1_CTX *ctx = (MD5_SHA1_CTX *)vctx;
|
||||
@@ -34,7 +44,8 @@ static int md5_sha1_set_params(void *vctx, const OSSL_PARAM params[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(md5_sha1, MD5_SHA1_CTX,
|
||||
MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH,
|
||||
md5_sha1_init, md5_sha1_update, md5_sha1_final,
|
||||
md5_sha1_set_params)
|
||||
/* md5_sha1_functions */
|
||||
IMPLEMENT_digest_functions_with_settable_ctx(
|
||||
md5_sha1, MD5_SHA1_CTX, MD5_SHA1_CBLOCK, MD5_SHA1_DIGEST_LENGTH, 0,
|
||||
md5_sha1_init, md5_sha1_update, md5_sha1_final,
|
||||
md5_sha1_settable_ctx_params, md5_sha1_set_ctx_params)
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* 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/core_numbers.h>
|
||||
#include <openssl/whrlpool.h>
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
static int nullmd_dummy = 1;
|
||||
|
||||
static OSSL_OP_digest_init_fn nullmd_init;
|
||||
static OSSL_OP_digest_update_fn nullmd_update;
|
||||
static OSSL_OP_digest_final_fn nullmd_final;
|
||||
static OSSL_OP_digest_newctx_fn nullmd_newctx;
|
||||
static OSSL_OP_digest_freectx_fn nullmd_freectx;
|
||||
static OSSL_OP_digest_dupctx_fn nullmd_dupctx;
|
||||
static OSSL_OP_digest_size_fn nullmd_size;
|
||||
static OSSL_OP_digest_block_size_fn nullmd_block_size;
|
||||
|
||||
static size_t nullmd_block_size(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t nullmd_size(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int nullmd_init(void *vctx)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nullmd_update(void *vctx, const unsigned char *inp, size_t bytes)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nullmd_final(void *ctx, unsigned char *out, size_t *outl, size_t outsz)
|
||||
{
|
||||
*outl = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void *nullmd_newctx(void *prov_ctx)
|
||||
{
|
||||
return &nullmd_dummy;
|
||||
}
|
||||
|
||||
static void nullmd_freectx(void *vctx)
|
||||
{
|
||||
}
|
||||
|
||||
static void *nullmd_dupctx(void *ctx)
|
||||
{
|
||||
return &nullmd_dummy;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH nullmd_functions[] = {
|
||||
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))nullmd_newctx },
|
||||
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))nullmd_init },
|
||||
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))nullmd_update },
|
||||
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void))nullmd_final },
|
||||
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))nullmd_freectx },
|
||||
{ OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))nullmd_dupctx },
|
||||
{ OSSL_FUNC_DIGEST_SIZE, (void (*)(void))nullmd_size },
|
||||
{ OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))nullmd_block_size },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -9,9 +9,10 @@
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include "internal/sm3.h"
|
||||
#include "internal/core_mkdigest.h"
|
||||
#include "internal/digestcommon.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
OSSL_FUNC_DIGEST_CONSTRUCT(sm3, SM3_CTX,
|
||||
SM3_CBLOCK, SM3_DIGEST_LENGTH,
|
||||
/* sm3_functions */
|
||||
IMPLEMENT_digest_functions(sm3, SM3_CTX,
|
||||
SM3_CBLOCK, SM3_DIGEST_LENGTH, 0,
|
||||
sm3_init, sm3_update, sm3_final)
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/* TODO(3.0) Move this header into provider when dependencies are removed */
|
||||
#ifndef HEADER_BLAKE2_H
|
||||
# define HEADER_BLAKE2_H
|
||||
|
||||
# include <openssl/opensslconf.h>
|
||||
|
||||
# include <openssl/e_os2.h>
|
||||
# include <stddef.h>
|
||||
|
||||
# define BLAKE2S_BLOCKBYTES 64
|
||||
# define BLAKE2S_OUTBYTES 32
|
||||
# define BLAKE2S_KEYBYTES 32
|
||||
# define BLAKE2S_SALTBYTES 8
|
||||
# define BLAKE2S_PERSONALBYTES 8
|
||||
|
||||
# define BLAKE2B_BLOCKBYTES 128
|
||||
# define BLAKE2B_OUTBYTES 64
|
||||
# define BLAKE2B_KEYBYTES 64
|
||||
# define BLAKE2B_SALTBYTES 16
|
||||
# define BLAKE2B_PERSONALBYTES 16
|
||||
|
||||
struct blake2s_param_st {
|
||||
uint8_t digest_length; /* 1 */
|
||||
uint8_t key_length; /* 2 */
|
||||
uint8_t fanout; /* 3 */
|
||||
uint8_t depth; /* 4 */
|
||||
uint8_t leaf_length[4];/* 8 */
|
||||
uint8_t node_offset[6];/* 14 */
|
||||
uint8_t node_depth; /* 15 */
|
||||
uint8_t inner_length; /* 16 */
|
||||
uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */
|
||||
uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */
|
||||
};
|
||||
|
||||
typedef struct blake2s_param_st BLAKE2S_PARAM;
|
||||
|
||||
struct blake2s_ctx_st {
|
||||
uint32_t h[8];
|
||||
uint32_t t[2];
|
||||
uint32_t f[2];
|
||||
uint8_t buf[BLAKE2S_BLOCKBYTES];
|
||||
size_t buflen;
|
||||
size_t outlen;
|
||||
};
|
||||
|
||||
struct blake2b_param_st {
|
||||
uint8_t digest_length; /* 1 */
|
||||
uint8_t key_length; /* 2 */
|
||||
uint8_t fanout; /* 3 */
|
||||
uint8_t depth; /* 4 */
|
||||
uint8_t leaf_length[4];/* 8 */
|
||||
uint8_t node_offset[8];/* 16 */
|
||||
uint8_t node_depth; /* 17 */
|
||||
uint8_t inner_length; /* 18 */
|
||||
uint8_t reserved[14]; /* 32 */
|
||||
uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
|
||||
uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
|
||||
};
|
||||
|
||||
typedef struct blake2b_param_st BLAKE2B_PARAM;
|
||||
|
||||
struct blake2b_ctx_st {
|
||||
uint64_t h[8];
|
||||
uint64_t t[2];
|
||||
uint64_t f[2];
|
||||
uint8_t buf[BLAKE2B_BLOCKBYTES];
|
||||
size_t buflen;
|
||||
size_t outlen;
|
||||
};
|
||||
|
||||
#define BLAKE2B_DIGEST_LENGTH 64
|
||||
#define BLAKE2S_DIGEST_LENGTH 32
|
||||
|
||||
typedef struct blake2s_ctx_st BLAKE2S_CTX;
|
||||
typedef struct blake2b_ctx_st BLAKE2B_CTX;
|
||||
|
||||
int blake2s256_init(void *ctx);
|
||||
int blake2b512_init(void *ctx);
|
||||
|
||||
int blake2b_init(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P);
|
||||
int blake2b_init_key(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P, const void *key);
|
||||
int blake2b_update(BLAKE2B_CTX *c, const void *data, size_t datalen);
|
||||
int blake2b_final(unsigned char *md, BLAKE2B_CTX *c);
|
||||
|
||||
/*
|
||||
* These setters are internal and do not check the validity of their parameters.
|
||||
* See blake2b_mac_ctrl for validation logic.
|
||||
*/
|
||||
|
||||
void blake2b_param_init(BLAKE2B_PARAM *P);
|
||||
void blake2b_param_set_digest_length(BLAKE2B_PARAM *P, uint8_t outlen);
|
||||
void blake2b_param_set_key_length(BLAKE2B_PARAM *P, uint8_t keylen);
|
||||
void blake2b_param_set_personal(BLAKE2B_PARAM *P, const uint8_t *personal, size_t length);
|
||||
void blake2b_param_set_salt(BLAKE2B_PARAM *P, const uint8_t *salt, size_t length);
|
||||
|
||||
int blake2s_init(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P);
|
||||
int blake2s_init_key(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P, const void *key);
|
||||
int blake2s_update(BLAKE2S_CTX *c, const void *data, size_t datalen);
|
||||
int blake2s_final(unsigned char *md, BLAKE2S_CTX *c);
|
||||
|
||||
void blake2s_param_init(BLAKE2S_PARAM *P);
|
||||
void blake2s_param_set_digest_length(BLAKE2S_PARAM *P, uint8_t outlen);
|
||||
void blake2s_param_set_key_length(BLAKE2S_PARAM *P, uint8_t keylen);
|
||||
void blake2s_param_set_personal(BLAKE2S_PARAM *P, const uint8_t *personal, size_t length);
|
||||
void blake2s_param_set_salt(BLAKE2S_PARAM *P, const uint8_t *salt, size_t length);
|
||||
|
||||
#endif /* HEADER_BLAKE2_H */
|
||||
@@ -0,0 +1,3 @@
|
||||
LIBS=../../../libcrypto
|
||||
SOURCE[../../../libcrypto]=scrypt.c sshkdf.c x942kdf.c
|
||||
INCLUDE[../../../libcrypto]=. ../../../crypto
|
||||
@@ -0,0 +1,463 @@
|
||||
/*
|
||||
* Copyright 2017-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 <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/kdf.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include "internal/evp_int.h"
|
||||
#include "internal/numbers.h"
|
||||
#include "internal/provider_algs.h"
|
||||
#include "internal/provider_ctx.h"
|
||||
#include "internal/providercommonerr.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SCRYPT
|
||||
|
||||
static OSSL_OP_kdf_newctx_fn kdf_scrypt_new;
|
||||
static OSSL_OP_kdf_freectx_fn kdf_scrypt_free;
|
||||
static OSSL_OP_kdf_reset_fn kdf_scrypt_reset;
|
||||
static OSSL_OP_kdf_derive_fn kdf_scrypt_derive;
|
||||
static OSSL_OP_kdf_settable_ctx_params_fn kdf_scrypt_settable_ctx_params;
|
||||
static OSSL_OP_kdf_set_ctx_params_fn kdf_scrypt_set_ctx_params;
|
||||
|
||||
static int scrypt_alg(const char *pass, size_t passlen,
|
||||
const unsigned char *salt, size_t saltlen,
|
||||
uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
|
||||
unsigned char *key, size_t keylen, EVP_MD *sha256);
|
||||
|
||||
typedef struct {
|
||||
void *provctx;
|
||||
unsigned char *pass;
|
||||
size_t pass_len;
|
||||
unsigned char *salt;
|
||||
size_t salt_len;
|
||||
uint64_t N;
|
||||
uint64_t r, p;
|
||||
uint64_t maxmem_bytes;
|
||||
EVP_MD *sha256;
|
||||
} KDF_SCRYPT;
|
||||
|
||||
static void kdf_scrypt_init(KDF_SCRYPT *ctx);
|
||||
|
||||
static void *kdf_scrypt_new(void *provctx)
|
||||
{
|
||||
KDF_SCRYPT *ctx;
|
||||
|
||||
ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
if (ctx == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
ctx->provctx = provctx;
|
||||
ctx->sha256 = EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(provctx),
|
||||
"sha256", NULL);
|
||||
if (ctx->sha256 == NULL) {
|
||||
OPENSSL_free(ctx);
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOAD_SHA256);
|
||||
return NULL;
|
||||
}
|
||||
kdf_scrypt_init(ctx);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void kdf_scrypt_free(void *vctx)
|
||||
{
|
||||
KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
|
||||
|
||||
EVP_MD_meth_free(ctx->sha256);
|
||||
kdf_scrypt_reset(ctx);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
static void kdf_scrypt_reset(void *vctx)
|
||||
{
|
||||
KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
|
||||
|
||||
OPENSSL_free(ctx->salt);
|
||||
OPENSSL_clear_free(ctx->pass, ctx->pass_len);
|
||||
kdf_scrypt_init(ctx);
|
||||
}
|
||||
|
||||
static void kdf_scrypt_init(KDF_SCRYPT *ctx)
|
||||
{
|
||||
/* Default values are the most conservative recommendation given in the
|
||||
* original paper of C. Percival. Derivation uses roughly 1 GiB of memory
|
||||
* for this parameter choice (approx. 128 * r * N * p bytes).
|
||||
*/
|
||||
ctx->N = 1 << 20;
|
||||
ctx->r = 8;
|
||||
ctx->p = 1;
|
||||
ctx->maxmem_bytes = 1025 * 1024 * 1024;
|
||||
}
|
||||
|
||||
static int scrypt_set_membuf(unsigned char **buffer, size_t *buflen,
|
||||
const OSSL_PARAM *p)
|
||||
{
|
||||
OPENSSL_clear_free(*buffer, *buflen);
|
||||
if (p->data_size == 0) {
|
||||
if ((*buffer = OPENSSL_malloc(1)) == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
} else if (p->data != NULL) {
|
||||
*buffer = NULL;
|
||||
if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int kdf_scrypt_derive(void *vctx, unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
|
||||
|
||||
if (ctx->pass == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->salt == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return scrypt_alg((char *)ctx->pass, ctx->pass_len, ctx->salt,
|
||||
ctx->salt_len, ctx->N, ctx->r, ctx->p,
|
||||
ctx->maxmem_bytes, key, keylen, ctx->sha256);
|
||||
}
|
||||
|
||||
static int is_power_of_two(uint64_t value)
|
||||
{
|
||||
return (value != 0) && ((value & (value - 1)) == 0);
|
||||
}
|
||||
|
||||
static int kdf_scrypt_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *p;
|
||||
KDF_SCRYPT *ctx = vctx;
|
||||
uint64_t u64_value;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
|
||||
if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
|
||||
if (!scrypt_set_membuf(&ctx->salt, &ctx->salt_len, p))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_N))
|
||||
!= NULL) {
|
||||
if (!OSSL_PARAM_get_uint64(p, &u64_value)
|
||||
|| u64_value <= 1
|
||||
|| !is_power_of_two(u64_value))
|
||||
return 0;
|
||||
ctx->N = u64_value;
|
||||
}
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_R))
|
||||
!= NULL) {
|
||||
if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
|
||||
return 0;
|
||||
ctx->r = u64_value;
|
||||
}
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_P))
|
||||
!= NULL) {
|
||||
if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
|
||||
return 0;
|
||||
ctx->p = u64_value;
|
||||
}
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_MAXMEM))
|
||||
!= NULL) {
|
||||
if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
|
||||
return 0;
|
||||
ctx->maxmem_bytes = u64_value;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *kdf_scrypt_settable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
|
||||
OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_N, NULL),
|
||||
OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_R, NULL),
|
||||
OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_P, NULL),
|
||||
OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
return known_settable_ctx_params;
|
||||
}
|
||||
|
||||
static int kdf_scrypt_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
|
||||
return OSSL_PARAM_set_size_t(p, SIZE_MAX);
|
||||
return -2;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *kdf_scrypt_gettable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
return known_gettable_ctx_params;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH kdf_scrypt_functions[] = {
|
||||
{ OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_scrypt_new },
|
||||
{ OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_scrypt_free },
|
||||
{ OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_scrypt_reset },
|
||||
{ OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_scrypt_derive },
|
||||
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
|
||||
(void(*)(void))kdf_scrypt_settable_ctx_params },
|
||||
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_scrypt_set_ctx_params },
|
||||
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
|
||||
(void(*)(void))kdf_scrypt_gettable_ctx_params },
|
||||
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_scrypt_get_ctx_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
|
||||
static void salsa208_word_specification(uint32_t inout[16])
|
||||
{
|
||||
int i;
|
||||
uint32_t x[16];
|
||||
|
||||
memcpy(x, inout, sizeof(x));
|
||||
for (i = 8; i > 0; i -= 2) {
|
||||
x[4] ^= R(x[0] + x[12], 7);
|
||||
x[8] ^= R(x[4] + x[0], 9);
|
||||
x[12] ^= R(x[8] + x[4], 13);
|
||||
x[0] ^= R(x[12] + x[8], 18);
|
||||
x[9] ^= R(x[5] + x[1], 7);
|
||||
x[13] ^= R(x[9] + x[5], 9);
|
||||
x[1] ^= R(x[13] + x[9], 13);
|
||||
x[5] ^= R(x[1] + x[13], 18);
|
||||
x[14] ^= R(x[10] + x[6], 7);
|
||||
x[2] ^= R(x[14] + x[10], 9);
|
||||
x[6] ^= R(x[2] + x[14], 13);
|
||||
x[10] ^= R(x[6] + x[2], 18);
|
||||
x[3] ^= R(x[15] + x[11], 7);
|
||||
x[7] ^= R(x[3] + x[15], 9);
|
||||
x[11] ^= R(x[7] + x[3], 13);
|
||||
x[15] ^= R(x[11] + x[7], 18);
|
||||
x[1] ^= R(x[0] + x[3], 7);
|
||||
x[2] ^= R(x[1] + x[0], 9);
|
||||
x[3] ^= R(x[2] + x[1], 13);
|
||||
x[0] ^= R(x[3] + x[2], 18);
|
||||
x[6] ^= R(x[5] + x[4], 7);
|
||||
x[7] ^= R(x[6] + x[5], 9);
|
||||
x[4] ^= R(x[7] + x[6], 13);
|
||||
x[5] ^= R(x[4] + x[7], 18);
|
||||
x[11] ^= R(x[10] + x[9], 7);
|
||||
x[8] ^= R(x[11] + x[10], 9);
|
||||
x[9] ^= R(x[8] + x[11], 13);
|
||||
x[10] ^= R(x[9] + x[8], 18);
|
||||
x[12] ^= R(x[15] + x[14], 7);
|
||||
x[13] ^= R(x[12] + x[15], 9);
|
||||
x[14] ^= R(x[13] + x[12], 13);
|
||||
x[15] ^= R(x[14] + x[13], 18);
|
||||
}
|
||||
for (i = 0; i < 16; ++i)
|
||||
inout[i] += x[i];
|
||||
OPENSSL_cleanse(x, sizeof(x));
|
||||
}
|
||||
|
||||
static void scryptBlockMix(uint32_t *B_, uint32_t *B, uint64_t r)
|
||||
{
|
||||
uint64_t i, j;
|
||||
uint32_t X[16], *pB;
|
||||
|
||||
memcpy(X, B + (r * 2 - 1) * 16, sizeof(X));
|
||||
pB = B;
|
||||
for (i = 0; i < r * 2; i++) {
|
||||
for (j = 0; j < 16; j++)
|
||||
X[j] ^= *pB++;
|
||||
salsa208_word_specification(X);
|
||||
memcpy(B_ + (i / 2 + (i & 1) * r) * 16, X, sizeof(X));
|
||||
}
|
||||
OPENSSL_cleanse(X, sizeof(X));
|
||||
}
|
||||
|
||||
static void scryptROMix(unsigned char *B, uint64_t r, uint64_t N,
|
||||
uint32_t *X, uint32_t *T, uint32_t *V)
|
||||
{
|
||||
unsigned char *pB;
|
||||
uint32_t *pV;
|
||||
uint64_t i, k;
|
||||
|
||||
/* Convert from little endian input */
|
||||
for (pV = V, i = 0, pB = B; i < 32 * r; i++, pV++) {
|
||||
*pV = *pB++;
|
||||
*pV |= *pB++ << 8;
|
||||
*pV |= *pB++ << 16;
|
||||
*pV |= (uint32_t)*pB++ << 24;
|
||||
}
|
||||
|
||||
for (i = 1; i < N; i++, pV += 32 * r)
|
||||
scryptBlockMix(pV, pV - 32 * r, r);
|
||||
|
||||
scryptBlockMix(X, V + (N - 1) * 32 * r, r);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
uint32_t j;
|
||||
j = X[16 * (2 * r - 1)] % N;
|
||||
pV = V + 32 * r * j;
|
||||
for (k = 0; k < 32 * r; k++)
|
||||
T[k] = X[k] ^ *pV++;
|
||||
scryptBlockMix(X, T, r);
|
||||
}
|
||||
/* Convert output to little endian */
|
||||
for (i = 0, pB = B; i < 32 * r; i++) {
|
||||
uint32_t xtmp = X[i];
|
||||
*pB++ = xtmp & 0xff;
|
||||
*pB++ = (xtmp >> 8) & 0xff;
|
||||
*pB++ = (xtmp >> 16) & 0xff;
|
||||
*pB++ = (xtmp >> 24) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef SIZE_MAX
|
||||
# define SIZE_MAX ((size_t)-1)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Maximum power of two that will fit in uint64_t: this should work on
|
||||
* most (all?) platforms.
|
||||
*/
|
||||
|
||||
#define LOG2_UINT64_MAX (sizeof(uint64_t) * 8 - 1)
|
||||
|
||||
/*
|
||||
* Maximum value of p * r:
|
||||
* p <= ((2^32-1) * hLen) / MFLen =>
|
||||
* p <= ((2^32-1) * 32) / (128 * r) =>
|
||||
* p * r <= (2^30-1)
|
||||
*/
|
||||
|
||||
#define SCRYPT_PR_MAX ((1 << 30) - 1)
|
||||
|
||||
static int scrypt_alg(const char *pass, size_t passlen,
|
||||
const unsigned char *salt, size_t saltlen,
|
||||
uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
|
||||
unsigned char *key, size_t keylen, EVP_MD *sha256)
|
||||
{
|
||||
int rv = 0;
|
||||
unsigned char *B;
|
||||
uint32_t *X, *V, *T;
|
||||
uint64_t i, Blen, Vlen;
|
||||
|
||||
/* Sanity check parameters */
|
||||
/* initial check, r,p must be non zero, N >= 2 and a power of 2 */
|
||||
if (r == 0 || p == 0 || N < 2 || (N & (N - 1)))
|
||||
return 0;
|
||||
/* Check p * r < SCRYPT_PR_MAX avoiding overflow */
|
||||
if (p > SCRYPT_PR_MAX / r) {
|
||||
EVPerr(EVP_F_SCRYPT_ALG, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Need to check N: if 2^(128 * r / 8) overflows limit this is
|
||||
* automatically satisfied since N <= UINT64_MAX.
|
||||
*/
|
||||
|
||||
if (16 * r <= LOG2_UINT64_MAX) {
|
||||
if (N >= (((uint64_t)1) << (16 * r))) {
|
||||
EVPerr(EVP_F_SCRYPT_ALG, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Memory checks: check total allocated buffer size fits in uint64_t */
|
||||
|
||||
/*
|
||||
* B size in section 5 step 1.S
|
||||
* Note: we know p * 128 * r < UINT64_MAX because we already checked
|
||||
* p * r < SCRYPT_PR_MAX
|
||||
*/
|
||||
Blen = p * 128 * r;
|
||||
/*
|
||||
* Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would
|
||||
* have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.]
|
||||
*/
|
||||
if (Blen > INT_MAX) {
|
||||
EVPerr(EVP_F_SCRYPT_ALG, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
|
||||
* This is combined size V, X and T (section 4)
|
||||
*/
|
||||
i = UINT64_MAX / (32 * sizeof(uint32_t));
|
||||
if (N + 2 > i / r) {
|
||||
EVPerr(EVP_F_SCRYPT_ALG, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
Vlen = 32 * r * (N + 2) * sizeof(uint32_t);
|
||||
|
||||
/* check total allocated size fits in uint64_t */
|
||||
if (Blen > UINT64_MAX - Vlen) {
|
||||
EVPerr(EVP_F_SCRYPT_ALG, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check that the maximum memory doesn't exceed a size_t limits */
|
||||
if (maxmem > SIZE_MAX)
|
||||
maxmem = SIZE_MAX;
|
||||
|
||||
if (Blen + Vlen > maxmem) {
|
||||
EVPerr(EVP_F_SCRYPT_ALG, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* If no key return to indicate parameters are OK */
|
||||
if (key == NULL)
|
||||
return 1;
|
||||
|
||||
B = OPENSSL_malloc((size_t)(Blen + Vlen));
|
||||
if (B == NULL) {
|
||||
EVPerr(EVP_F_SCRYPT_ALG, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
X = (uint32_t *)(B + Blen);
|
||||
T = X + 32 * r;
|
||||
V = T + 32 * r;
|
||||
if (PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, 1, sha256,
|
||||
(int)Blen, B) == 0)
|
||||
goto err;
|
||||
|
||||
for (i = 0; i < p; i++)
|
||||
scryptROMix(B + 128 * r * i, r, N, X, T, V);
|
||||
|
||||
if (PKCS5_PBKDF2_HMAC(pass, passlen, B, (int)Blen, 1, sha256,
|
||||
keylen, key) == 0)
|
||||
goto err;
|
||||
rv = 1;
|
||||
err:
|
||||
if (rv == 0)
|
||||
EVPerr(EVP_F_SCRYPT_ALG, EVP_R_PBKDF2_ERROR);
|
||||
|
||||
OPENSSL_clear_free(B, (size_t)(Blen + Vlen));
|
||||
return rv;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,280 @@
|
||||
/*
|
||||
* Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (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 <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/kdf.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/numbers.h"
|
||||
#include "internal/evp_int.h"
|
||||
#include "internal/provider_ctx.h"
|
||||
#include "internal/providercommonerr.h"
|
||||
#include "internal/provider_algs.h"
|
||||
# include "internal/provider_util.h"
|
||||
|
||||
/* See RFC 4253, Section 7.2 */
|
||||
static OSSL_OP_kdf_newctx_fn kdf_sshkdf_new;
|
||||
static OSSL_OP_kdf_freectx_fn kdf_sshkdf_free;
|
||||
static OSSL_OP_kdf_reset_fn kdf_sshkdf_reset;
|
||||
static OSSL_OP_kdf_derive_fn kdf_sshkdf_derive;
|
||||
static OSSL_OP_kdf_settable_ctx_params_fn kdf_sshkdf_settable_ctx_params;
|
||||
static OSSL_OP_kdf_set_ctx_params_fn kdf_sshkdf_set_ctx_params;
|
||||
static OSSL_OP_kdf_gettable_ctx_params_fn kdf_sshkdf_gettable_ctx_params;
|
||||
static OSSL_OP_kdf_get_ctx_params_fn kdf_sshkdf_get_ctx_params;
|
||||
|
||||
static int SSHKDF(const EVP_MD *evp_md,
|
||||
const unsigned char *key, size_t key_len,
|
||||
const unsigned char *xcghash, size_t xcghash_len,
|
||||
const unsigned char *session_id, size_t session_id_len,
|
||||
char type, unsigned char *okey, size_t okey_len);
|
||||
|
||||
typedef struct {
|
||||
void *provctx;
|
||||
PROV_DIGEST digest;
|
||||
unsigned char *key; /* K */
|
||||
size_t key_len;
|
||||
unsigned char *xcghash; /* H */
|
||||
size_t xcghash_len;
|
||||
char type; /* X */
|
||||
unsigned char *session_id;
|
||||
size_t session_id_len;
|
||||
} KDF_SSHKDF;
|
||||
|
||||
static void *kdf_sshkdf_new(void *provctx)
|
||||
{
|
||||
KDF_SSHKDF *ctx;
|
||||
|
||||
if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
ctx->provctx = provctx;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void kdf_sshkdf_free(void *vctx)
|
||||
{
|
||||
KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
|
||||
|
||||
kdf_sshkdf_reset(ctx);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
static void kdf_sshkdf_reset(void *vctx)
|
||||
{
|
||||
KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
|
||||
|
||||
ossl_prov_digest_reset(&ctx->digest);
|
||||
OPENSSL_clear_free(ctx->key, ctx->key_len);
|
||||
OPENSSL_clear_free(ctx->xcghash, ctx->xcghash_len);
|
||||
OPENSSL_clear_free(ctx->session_id, ctx->session_id_len);
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len,
|
||||
const OSSL_PARAM *p)
|
||||
{
|
||||
OPENSSL_clear_free(*dst, *dst_len);
|
||||
*dst = NULL;
|
||||
return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
|
||||
}
|
||||
|
||||
static int kdf_sshkdf_derive(void *vctx, unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
|
||||
const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
|
||||
|
||||
if (md == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->key == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->xcghash == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_XCGHASH);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->session_id == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SESSION_ID);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->type == 0) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_TYPE);
|
||||
return 0;
|
||||
}
|
||||
return SSHKDF(md, ctx->key, ctx->key_len,
|
||||
ctx->xcghash, ctx->xcghash_len,
|
||||
ctx->session_id, ctx->session_id_len,
|
||||
ctx->type, key, keylen);
|
||||
}
|
||||
|
||||
static int kdf_sshkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *p;
|
||||
KDF_SSHKDF *ctx = vctx;
|
||||
OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
|
||||
int t;
|
||||
|
||||
if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
|
||||
if (!sshkdf_set_membuf(&ctx->key, &ctx->key_len, p))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_XCGHASH))
|
||||
!= NULL)
|
||||
if (!sshkdf_set_membuf(&ctx->xcghash, &ctx->xcghash_len, p))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_SESSION_ID))
|
||||
!= NULL)
|
||||
if (!sshkdf_set_membuf(&ctx->session_id, &ctx->session_id_len, p))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_TYPE))
|
||||
!= NULL) {
|
||||
if (p->data == NULL || p->data_size == 0)
|
||||
return 0;
|
||||
t = *(unsigned char *)p->data;
|
||||
if (t < 65 || t > 70) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
ctx->type = (char)t;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *kdf_sshkdf_settable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
|
||||
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID, NULL, 0),
|
||||
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
return known_settable_ctx_params;
|
||||
}
|
||||
|
||||
static int kdf_sshkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
|
||||
return OSSL_PARAM_set_size_t(p, SIZE_MAX);
|
||||
return -2;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *kdf_sshkdf_gettable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
return known_gettable_ctx_params;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH kdf_sshkdf_functions[] = {
|
||||
{ OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_sshkdf_new },
|
||||
{ OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_sshkdf_free },
|
||||
{ OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_sshkdf_reset },
|
||||
{ OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_sshkdf_derive },
|
||||
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
|
||||
(void(*)(void))kdf_sshkdf_settable_ctx_params },
|
||||
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_set_ctx_params },
|
||||
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
|
||||
(void(*)(void))kdf_sshkdf_gettable_ctx_params },
|
||||
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_get_ctx_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
static int SSHKDF(const EVP_MD *evp_md,
|
||||
const unsigned char *key, size_t key_len,
|
||||
const unsigned char *xcghash, size_t xcghash_len,
|
||||
const unsigned char *session_id, size_t session_id_len,
|
||||
char type, unsigned char *okey, size_t okey_len)
|
||||
{
|
||||
EVP_MD_CTX *md = NULL;
|
||||
unsigned char digest[EVP_MAX_MD_SIZE];
|
||||
unsigned int dsize = 0;
|
||||
size_t cursize = 0;
|
||||
int ret = 0;
|
||||
|
||||
md = EVP_MD_CTX_new();
|
||||
if (md == NULL)
|
||||
return 0;
|
||||
|
||||
if (!EVP_DigestInit_ex(md, evp_md, NULL))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestUpdate(md, key, key_len))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestUpdate(md, &type, 1))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestUpdate(md, session_id, session_id_len))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestFinal_ex(md, digest, &dsize))
|
||||
goto out;
|
||||
|
||||
if (okey_len < dsize) {
|
||||
memcpy(okey, digest, okey_len);
|
||||
ret = 1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
memcpy(okey, digest, dsize);
|
||||
|
||||
for (cursize = dsize; cursize < okey_len; cursize += dsize) {
|
||||
|
||||
if (!EVP_DigestInit_ex(md, evp_md, NULL))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestUpdate(md, key, key_len))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestUpdate(md, okey, cursize))
|
||||
goto out;
|
||||
|
||||
if (!EVP_DigestFinal_ex(md, digest, &dsize))
|
||||
goto out;
|
||||
|
||||
if (okey_len < cursize + dsize) {
|
||||
memcpy(okey + cursize, digest, okey_len - cursize);
|
||||
ret = 1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
memcpy(okey + cursize, digest, dsize);
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
out:
|
||||
EVP_MD_CTX_free(md);
|
||||
OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,424 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. 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 "e_os.h"
|
||||
|
||||
#ifndef OPENSSL_NO_CMS
|
||||
|
||||
# include <stdlib.h>
|
||||
# include <stdarg.h>
|
||||
# include <string.h>
|
||||
# include <openssl/hmac.h>
|
||||
# include <openssl/cms.h>
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/kdf.h>
|
||||
# include <openssl/x509.h>
|
||||
# include <openssl/obj_mac.h>
|
||||
# include <openssl/core_names.h>
|
||||
# include "internal/cryptlib.h"
|
||||
# include "internal/numbers.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include "internal/provider_ctx.h"
|
||||
# include "internal/providercommonerr.h"
|
||||
# include "internal/provider_algs.h"
|
||||
# include "internal/provider_util.h"
|
||||
|
||||
# define X942KDF_MAX_INLEN (1 << 30)
|
||||
|
||||
static OSSL_OP_kdf_newctx_fn x942kdf_new;
|
||||
static OSSL_OP_kdf_freectx_fn x942kdf_free;
|
||||
static OSSL_OP_kdf_reset_fn x942kdf_reset;
|
||||
static OSSL_OP_kdf_derive_fn x942kdf_derive;
|
||||
static OSSL_OP_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
|
||||
static OSSL_OP_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
|
||||
static OSSL_OP_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
|
||||
static OSSL_OP_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
|
||||
|
||||
typedef struct {
|
||||
void *provctx;
|
||||
PROV_DIGEST digest;
|
||||
unsigned char *secret;
|
||||
size_t secret_len;
|
||||
int cek_nid;
|
||||
unsigned char *ukm;
|
||||
size_t ukm_len;
|
||||
size_t dkm_len;
|
||||
} KDF_X942;
|
||||
|
||||
/* A table of allowed wrapping algorithms and the associated output lengths */
|
||||
static const struct {
|
||||
int nid;
|
||||
size_t keklen; /* size in bytes */
|
||||
} kek_algs[] = {
|
||||
{ NID_id_smime_alg_CMS3DESwrap, 24 },
|
||||
{ NID_id_smime_alg_CMSRC2wrap, 16 },
|
||||
{ NID_id_aes128_wrap, 16 },
|
||||
{ NID_id_aes192_wrap, 24 },
|
||||
{ NID_id_aes256_wrap, 32 },
|
||||
{ NID_id_camellia128_wrap, 16 },
|
||||
{ NID_id_camellia192_wrap, 24 },
|
||||
{ NID_id_camellia256_wrap, 32 }
|
||||
};
|
||||
|
||||
/* Skip past an ASN1 structure: for OBJECT skip content octets too */
|
||||
static int skip_asn1(unsigned char **pp, long *plen, int exptag)
|
||||
{
|
||||
int i, tag, xclass;
|
||||
long tmplen;
|
||||
const unsigned char *q = *pp;
|
||||
|
||||
i = ASN1_get_object(&q, &tmplen, &tag, &xclass, *plen);
|
||||
if ((i & 0x80) != 0 || tag != exptag || xclass != V_ASN1_UNIVERSAL)
|
||||
return 0;
|
||||
if (tag == V_ASN1_OBJECT)
|
||||
q += tmplen;
|
||||
*pp = (unsigned char *)q;
|
||||
*plen -= q - *pp;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Encode the other info structure.
|
||||
*
|
||||
* RFC2631 Section 2.1.2 Contains the following definition for otherinfo
|
||||
*
|
||||
* OtherInfo ::= SEQUENCE {
|
||||
* keyInfo KeySpecificInfo,
|
||||
* partyAInfo [0] OCTET STRING OPTIONAL,
|
||||
* suppPubInfo [2] OCTET STRING
|
||||
* }
|
||||
*
|
||||
* KeySpecificInfo ::= SEQUENCE {
|
||||
* algorithm OBJECT IDENTIFIER,
|
||||
* counter OCTET STRING SIZE (4..4)
|
||||
* }
|
||||
*
|
||||
* |nid| is the algorithm object identifier.
|
||||
* |keylen| is the length (in bytes) of the generated KEK. It is stored into
|
||||
* suppPubInfo (in bits).
|
||||
* |ukm| is the optional user keying material that is stored into partyAInfo. It
|
||||
* can be NULL.
|
||||
* |ukmlen| is the user keying material length (in bytes).
|
||||
* |der| is the returned encoded data. It must be freed by the caller.
|
||||
* |der_len| is the returned size of the encoded data.
|
||||
* |out_ctr| returns a pointer to the counter data which is embedded inside the
|
||||
* encoded data. This allows the counter bytes to be updated without re-encoding.
|
||||
*
|
||||
* Returns: 1 if successfully encoded, or 0 otherwise.
|
||||
* Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
|
||||
*/
|
||||
static int x942_encode_otherinfo(int nid, size_t keylen,
|
||||
const unsigned char *ukm, size_t ukmlen,
|
||||
unsigned char **der, size_t *der_len,
|
||||
unsigned char **out_ctr)
|
||||
{
|
||||
unsigned char *p, *encoded = NULL;
|
||||
int ret = 0, encoded_len;
|
||||
long tlen;
|
||||
/* "magic" value to check offset is sane */
|
||||
static unsigned char ctr[4] = { 0x00, 0x00, 0x00, 0x01 };
|
||||
X509_ALGOR *ksi = NULL;
|
||||
ASN1_OBJECT *alg_oid = NULL;
|
||||
ASN1_OCTET_STRING *ctr_oct = NULL, *ukm_oct = NULL;
|
||||
|
||||
/* set the KeySpecificInfo - which contains an algorithm oid and counter */
|
||||
ksi = X509_ALGOR_new();
|
||||
alg_oid = OBJ_dup(OBJ_nid2obj(nid));
|
||||
ctr_oct = ASN1_OCTET_STRING_new();
|
||||
if (ksi == NULL
|
||||
|| alg_oid == NULL
|
||||
|| ctr_oct == NULL
|
||||
|| !ASN1_OCTET_STRING_set(ctr_oct, ctr, sizeof(ctr))
|
||||
|| !X509_ALGOR_set0(ksi, alg_oid, V_ASN1_OCTET_STRING, ctr_oct))
|
||||
goto err;
|
||||
/* NULL these as they now belong to ksi */
|
||||
alg_oid = NULL;
|
||||
ctr_oct = NULL;
|
||||
|
||||
/* Set the optional partyAInfo */
|
||||
if (ukm != NULL) {
|
||||
ukm_oct = ASN1_OCTET_STRING_new();
|
||||
if (ukm_oct == NULL)
|
||||
goto err;
|
||||
ASN1_OCTET_STRING_set(ukm_oct, (unsigned char *)ukm, ukmlen);
|
||||
}
|
||||
/* Generate the OtherInfo DER data */
|
||||
encoded_len = CMS_SharedInfo_encode(&encoded, ksi, ukm_oct, keylen);
|
||||
if (encoded_len <= 0)
|
||||
goto err;
|
||||
|
||||
/* Parse the encoded data to find the offset of the counter data */
|
||||
p = encoded;
|
||||
tlen = (long)encoded_len;
|
||||
if (skip_asn1(&p, &tlen, V_ASN1_SEQUENCE)
|
||||
&& skip_asn1(&p, &tlen, V_ASN1_SEQUENCE)
|
||||
&& skip_asn1(&p, &tlen, V_ASN1_OBJECT)
|
||||
&& skip_asn1(&p, &tlen, V_ASN1_OCTET_STRING)
|
||||
&& CRYPTO_memcmp(p, ctr, 4) == 0) {
|
||||
*out_ctr = p;
|
||||
*der = encoded;
|
||||
*der_len = (size_t)encoded_len;
|
||||
ret = 1;
|
||||
}
|
||||
err:
|
||||
if (ret != 1)
|
||||
OPENSSL_free(encoded);
|
||||
ASN1_OCTET_STRING_free(ctr_oct);
|
||||
ASN1_OCTET_STRING_free(ukm_oct);
|
||||
ASN1_OBJECT_free(alg_oid);
|
||||
X509_ALGOR_free(ksi);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
|
||||
const unsigned char *z, size_t z_len,
|
||||
const unsigned char *other, size_t other_len,
|
||||
unsigned char *ctr,
|
||||
unsigned char *derived_key, size_t derived_key_len)
|
||||
{
|
||||
int ret = 0, hlen;
|
||||
size_t counter, out_len, len = derived_key_len;
|
||||
unsigned char mac[EVP_MAX_MD_SIZE];
|
||||
unsigned char *out = derived_key;
|
||||
EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
|
||||
|
||||
if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
|
||||
|| derived_key_len > X942KDF_MAX_INLEN
|
||||
|| derived_key_len == 0) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hlen = EVP_MD_size(kdf_md);
|
||||
if (hlen <= 0)
|
||||
return 0;
|
||||
out_len = (size_t)hlen;
|
||||
|
||||
ctx = EVP_MD_CTX_create();
|
||||
ctx_init = EVP_MD_CTX_create();
|
||||
if (ctx == NULL || ctx_init == NULL)
|
||||
goto end;
|
||||
|
||||
if (!EVP_DigestInit(ctx_init, kdf_md))
|
||||
goto end;
|
||||
|
||||
for (counter = 1;; counter++) {
|
||||
/* updating the ctr modifies 4 bytes in the 'other' buffer */
|
||||
ctr[0] = (unsigned char)((counter >> 24) & 0xff);
|
||||
ctr[1] = (unsigned char)((counter >> 16) & 0xff);
|
||||
ctr[2] = (unsigned char)((counter >> 8) & 0xff);
|
||||
ctr[3] = (unsigned char)(counter & 0xff);
|
||||
|
||||
if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
|
||||
|| !EVP_DigestUpdate(ctx, z, z_len)
|
||||
|| !EVP_DigestUpdate(ctx, other, other_len))
|
||||
goto end;
|
||||
if (len >= out_len) {
|
||||
if (!EVP_DigestFinal_ex(ctx, out, NULL))
|
||||
goto end;
|
||||
out += out_len;
|
||||
len -= out_len;
|
||||
if (len == 0)
|
||||
break;
|
||||
} else {
|
||||
if (!EVP_DigestFinal_ex(ctx, mac, NULL))
|
||||
goto end;
|
||||
memcpy(out, mac, len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret = 1;
|
||||
end:
|
||||
EVP_MD_CTX_free(ctx);
|
||||
EVP_MD_CTX_free(ctx_init);
|
||||
OPENSSL_cleanse(mac, sizeof(mac));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *x942kdf_new(void *provctx)
|
||||
{
|
||||
KDF_X942 *ctx;
|
||||
|
||||
if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
ctx->provctx = provctx;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void x942kdf_reset(void *vctx)
|
||||
{
|
||||
KDF_X942 *ctx = (KDF_X942 *)vctx;
|
||||
|
||||
ossl_prov_digest_reset(&ctx->digest);
|
||||
OPENSSL_clear_free(ctx->secret, ctx->secret_len);
|
||||
OPENSSL_clear_free(ctx->ukm, ctx->ukm_len);
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static void x942kdf_free(void *vctx)
|
||||
{
|
||||
KDF_X942 *ctx = (KDF_X942 *)vctx;
|
||||
|
||||
x942kdf_reset(ctx);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
|
||||
const OSSL_PARAM *p)
|
||||
{
|
||||
if (p->data_size == 0 || p->data == NULL)
|
||||
return 1;
|
||||
|
||||
OPENSSL_free(*out);
|
||||
*out = NULL;
|
||||
return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
|
||||
}
|
||||
|
||||
static size_t x942kdf_size(KDF_X942 *ctx)
|
||||
{
|
||||
int len;
|
||||
const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
|
||||
|
||||
if (md == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
|
||||
return 0;
|
||||
}
|
||||
len = EVP_MD_size(md);
|
||||
return (len <= 0) ? 0 : (size_t)len;
|
||||
}
|
||||
|
||||
static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
|
||||
{
|
||||
KDF_X942 *ctx = (KDF_X942 *)vctx;
|
||||
const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
|
||||
int ret = 0;
|
||||
unsigned char *ctr;
|
||||
unsigned char *der = NULL;
|
||||
size_t der_len = 0;
|
||||
|
||||
if (ctx->secret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
|
||||
return 0;
|
||||
}
|
||||
if (md == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->cek_nid == NID_undef) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->ukm != NULL && ctx->ukm_len >= X942KDF_MAX_INLEN) {
|
||||
/*
|
||||
* Note the ukm length MUST be 512 bits.
|
||||
* For backwards compatibility the old check is being done.
|
||||
*/
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INAVLID_UKM_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
if (keylen != ctx->dkm_len) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
|
||||
return 0;
|
||||
}
|
||||
/* generate the otherinfo der */
|
||||
if (!x942_encode_otherinfo(ctx->cek_nid, ctx->dkm_len,
|
||||
ctx->ukm, ctx->ukm_len,
|
||||
&der, &der_len, &ctr)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
|
||||
return 0;
|
||||
}
|
||||
ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
|
||||
der, der_len, ctr, key, keylen);
|
||||
OPENSSL_free(der);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *p;
|
||||
KDF_X942 *ctx = vctx;
|
||||
OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
|
||||
size_t i;
|
||||
|
||||
if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
|
||||
|| (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
|
||||
if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM)) != NULL)
|
||||
if (!x942kdf_set_buffer(&ctx->ukm, &ctx->ukm_len, p))
|
||||
return 0;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG)) != NULL) {
|
||||
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
||||
return 0;
|
||||
ctx->cek_nid = OBJ_sn2nid(p->data);
|
||||
for (i = 0; i < OSSL_NELEM(kek_algs); i++)
|
||||
if (kek_algs[i].nid == ctx->cek_nid)
|
||||
goto cek_found;
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
|
||||
return 0;
|
||||
cek_found:
|
||||
ctx->dkm_len = kek_algs[i].keklen;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *x942kdf_settable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
|
||||
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
|
||||
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
return known_settable_ctx_params;
|
||||
}
|
||||
|
||||
static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
||||
{
|
||||
KDF_X942 *ctx = (KDF_X942 *)vctx;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
|
||||
return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
|
||||
return -2;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *x942kdf_gettable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
return known_gettable_ctx_params;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH kdf_x942_kdf_functions[] = {
|
||||
{ OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
|
||||
{ OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
|
||||
{ OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
|
||||
{ OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
|
||||
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
|
||||
(void(*)(void))x942kdf_settable_ctx_params },
|
||||
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
|
||||
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
|
||||
(void(*)(void))x942kdf_gettable_ctx_params },
|
||||
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
#endif /* OPENSSL_NO_CMS */
|
||||
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* Copyright 2018 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/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/params.h>
|
||||
|
||||
#include "internal/blake2.h"
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/providercommonerr.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/*
|
||||
* Forward declaration of everything implemented here. This is not strictly
|
||||
* necessary for the compiler, but provides an assurance that the signatures
|
||||
* of the functions in the dispatch table are correct.
|
||||
*/
|
||||
static OSSL_OP_mac_newctx_fn blake2_mac_new;
|
||||
static OSSL_OP_mac_dupctx_fn blake2_mac_dup;
|
||||
static OSSL_OP_mac_freectx_fn blake2_mac_free;
|
||||
static OSSL_OP_mac_gettable_ctx_params_fn blake2_gettable_ctx_params;
|
||||
static OSSL_OP_mac_get_ctx_params_fn blake2_get_ctx_params;
|
||||
static OSSL_OP_mac_settable_ctx_params_fn blake2_mac_settable_ctx_params;
|
||||
static OSSL_OP_mac_set_ctx_params_fn blake2_mac_set_ctx_params;
|
||||
static OSSL_OP_mac_init_fn blake2_mac_init;
|
||||
static OSSL_OP_mac_update_fn blake2_mac_update;
|
||||
static OSSL_OP_mac_final_fn blake2_mac_final;
|
||||
|
||||
struct blake2_mac_data_st {
|
||||
BLAKE2_CTX ctx;
|
||||
BLAKE2_PARAM params;
|
||||
unsigned char key[BLAKE2_KEYBYTES];
|
||||
};
|
||||
|
||||
static size_t blake2_mac_size(void *vmacctx);
|
||||
|
||||
static void *blake2_mac_new(void *unused_provctx)
|
||||
{
|
||||
struct blake2_mac_data_st *macctx = OPENSSL_zalloc(sizeof(*macctx));
|
||||
|
||||
if (macctx != NULL) {
|
||||
BLAKE2_PARAM_INIT(&macctx->params);
|
||||
/* ctx initialization is deferred to BLAKE2b_Init() */
|
||||
}
|
||||
return macctx;
|
||||
}
|
||||
|
||||
static void *blake2_mac_dup(void *vsrc)
|
||||
{
|
||||
struct blake2_mac_data_st *dst;
|
||||
struct blake2_mac_data_st *src = vsrc;
|
||||
|
||||
dst = OPENSSL_zalloc(sizeof(*dst));
|
||||
if (dst == NULL)
|
||||
return NULL;
|
||||
|
||||
*dst = *src;
|
||||
return dst;
|
||||
}
|
||||
|
||||
static void blake2_mac_free(void *vmacctx)
|
||||
{
|
||||
struct blake2_mac_data_st *macctx = vmacctx;
|
||||
|
||||
if (macctx != NULL) {
|
||||
OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
|
||||
OPENSSL_free(macctx);
|
||||
}
|
||||
}
|
||||
|
||||
static int blake2_mac_init(void *vmacctx)
|
||||
{
|
||||
struct blake2_mac_data_st *macctx = vmacctx;
|
||||
|
||||
/* Check key has been set */
|
||||
if (macctx->params.key_length == 0) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key);
|
||||
}
|
||||
|
||||
static int blake2_mac_update(void *vmacctx,
|
||||
const unsigned char *data, size_t datalen)
|
||||
{
|
||||
struct blake2_mac_data_st *macctx = vmacctx;
|
||||
|
||||
return BLAKE2_UPDATE(&macctx->ctx, data, datalen);
|
||||
}
|
||||
|
||||
static int blake2_mac_final(void *vmacctx,
|
||||
unsigned char *out, size_t *outl,
|
||||
size_t outsize)
|
||||
{
|
||||
struct blake2_mac_data_st *macctx = vmacctx;
|
||||
|
||||
return BLAKE2_FINAL(out, &macctx->ctx);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *blake2_gettable_ctx_params(void)
|
||||
{
|
||||
return known_gettable_ctx_params;
|
||||
}
|
||||
|
||||
static int blake2_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
|
||||
return OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_MAC_PARAM_SALT, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *blake2_mac_settable_ctx_params()
|
||||
{
|
||||
return known_settable_ctx_params;
|
||||
}
|
||||
|
||||
/*
|
||||
* ALL parameters should be set before init().
|
||||
*/
|
||||
static int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct blake2_mac_data_st *macctx = vmacctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
|
||||
size_t size;
|
||||
|
||||
if (!OSSL_PARAM_get_size_t(p, &size)
|
||||
|| size < 1
|
||||
|| size > BLAKE2_OUTBYTES) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_NOT_XOF_OR_INVALID_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
BLAKE2_PARAM_SET_DIGEST_LENGTH(&macctx->params, (uint8_t)size);
|
||||
}
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
|
||||
size_t len;
|
||||
void *key_p = macctx->key;
|
||||
|
||||
if (!OSSL_PARAM_get_octet_string(p, &key_p, BLAKE2_KEYBYTES, &len)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
/* Pad with zeroes at the end */
|
||||
memset(macctx->key + len, 0, BLAKE2_KEYBYTES - len);
|
||||
|
||||
BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)len);
|
||||
}
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM))
|
||||
!= NULL) {
|
||||
/*
|
||||
* The OSSL_PARAM API doesn't provide direct pointer use, so we
|
||||
* must handle the OSSL_PARAM structure ourselves here
|
||||
*/
|
||||
if (p->data_size > BLAKE2_PERSONALBYTES) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p->data, p->data_size);
|
||||
}
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SALT)) != NULL) {
|
||||
/*
|
||||
* The OSSL_PARAM API doesn't provide direct pointer use, so we
|
||||
* must handle the OSSL_PARAM structure ourselves here as well
|
||||
*/
|
||||
if (p->data_size > BLAKE2_SALTBYTES) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
BLAKE2_PARAM_SET_SALT(&macctx->params, p->data, p->data_size);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static size_t blake2_mac_size(void *vmacctx)
|
||||
{
|
||||
struct blake2_mac_data_st *macctx = vmacctx;
|
||||
|
||||
return macctx->params.digest_length;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH BLAKE2_FUNCTIONS[] = {
|
||||
{ OSSL_FUNC_MAC_NEWCTX, (void (*)(void))blake2_mac_new },
|
||||
{ OSSL_FUNC_MAC_DUPCTX, (void (*)(void))blake2_mac_dup },
|
||||
{ OSSL_FUNC_MAC_FREECTX, (void (*)(void))blake2_mac_free },
|
||||
{ OSSL_FUNC_MAC_INIT, (void (*)(void))blake2_mac_init },
|
||||
{ OSSL_FUNC_MAC_UPDATE, (void (*)(void))blake2_mac_update },
|
||||
{ OSSL_FUNC_MAC_FINAL, (void (*)(void))blake2_mac_final },
|
||||
{ OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))blake2_gettable_ctx_params },
|
||||
{ OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))blake2_get_ctx_params },
|
||||
{ OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))blake2_mac_settable_ctx_params },
|
||||
{ OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))blake2_mac_set_ctx_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2018 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
|
||||
*/
|
||||
|
||||
/* Constants */
|
||||
#define BLAKE2_CTX BLAKE2B_CTX
|
||||
#define BLAKE2_PARAM BLAKE2B_PARAM
|
||||
#define BLAKE2_KEYBYTES BLAKE2B_KEYBYTES
|
||||
#define BLAKE2_OUTBYTES BLAKE2B_OUTBYTES
|
||||
#define BLAKE2_PERSONALBYTES BLAKE2B_PERSONALBYTES
|
||||
#define BLAKE2_SALTBYTES BLAKE2B_SALTBYTES
|
||||
|
||||
/* Function names */
|
||||
#define BLAKE2_PARAM_INIT blake2b_param_init
|
||||
#define BLAKE2_INIT_KEY blake2b_init_key
|
||||
#define BLAKE2_UPDATE blake2b_update
|
||||
#define BLAKE2_FINAL blake2b_final
|
||||
#define BLAKE2_PARAM_SET_DIGEST_LENGTH blake2b_param_set_digest_length
|
||||
#define BLAKE2_PARAM_SET_KEY_LENGTH blake2b_param_set_key_length
|
||||
#define BLAKE2_PARAM_SET_PERSONAL blake2b_param_set_personal
|
||||
#define BLAKE2_PARAM_SET_SALT blake2b_param_set_salt
|
||||
|
||||
/* OSSL_DISPATCH symbol */
|
||||
#define BLAKE2_FUNCTIONS blake2bmac_functions
|
||||
|
||||
#include "blake2_mac_impl.c"
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2018 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
|
||||
*/
|
||||
|
||||
/* Constants */
|
||||
#define BLAKE2_CTX BLAKE2S_CTX
|
||||
#define BLAKE2_PARAM BLAKE2S_PARAM
|
||||
#define BLAKE2_KEYBYTES BLAKE2S_KEYBYTES
|
||||
#define BLAKE2_OUTBYTES BLAKE2S_OUTBYTES
|
||||
#define BLAKE2_PERSONALBYTES BLAKE2S_PERSONALBYTES
|
||||
#define BLAKE2_SALTBYTES BLAKE2S_SALTBYTES
|
||||
|
||||
/* Function names */
|
||||
#define BLAKE2_PARAM_INIT blake2s_param_init
|
||||
#define BLAKE2_INIT_KEY blake2s_init_key
|
||||
#define BLAKE2_UPDATE blake2s_update
|
||||
#define BLAKE2_FINAL blake2s_final
|
||||
#define BLAKE2_PARAM_SET_DIGEST_LENGTH blake2s_param_set_digest_length
|
||||
#define BLAKE2_PARAM_SET_KEY_LENGTH blake2s_param_set_key_length
|
||||
#define BLAKE2_PARAM_SET_PERSONAL blake2s_param_set_personal
|
||||
#define BLAKE2_PARAM_SET_SALT blake2s_param_set_salt
|
||||
|
||||
/* OSSL_DISPATCH symbol */
|
||||
#define BLAKE2_FUNCTIONS blake2smac_functions
|
||||
|
||||
#include "blake2_mac_impl.c"
|
||||
@@ -0,0 +1,15 @@
|
||||
LIBS=../../../libcrypto
|
||||
|
||||
IF[{- !$disabled{blake2} -}]
|
||||
SOURCE[../../../libcrypto]=blake2b_mac.c blake2s_mac.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{siphash} -}]
|
||||
SOURCE[../../../libcrypto]=siphash_prov.c
|
||||
ENDIF
|
||||
|
||||
IF[{- !$disabled{poly1305} -}]
|
||||
SOURCE[../../../libcrypto]=poly1305_prov.c
|
||||
ENDIF
|
||||
|
||||
INCLUDE[../../../libcrypto]=. ../../../crypto
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright 2018 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/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include "internal/poly1305.h"
|
||||
/*
|
||||
* TODO(3.0) when poly1305 has moved entirely to our providers, this
|
||||
* header should be moved to the provider include directory. For the
|
||||
* moment, crypto/poly1305/poly1305_ameth.c has us stuck.
|
||||
*/
|
||||
#include "../../../crypto/poly1305/poly1305_local.h"
|
||||
|
||||
#include "internal/providercommonerr.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/*
|
||||
* Forward declaration of everything implemented here. This is not strictly
|
||||
* necessary for the compiler, but provides an assurance that the signatures
|
||||
* of the functions in the dispatch table are correct.
|
||||
*/
|
||||
static OSSL_OP_mac_newctx_fn poly1305_new;
|
||||
static OSSL_OP_mac_dupctx_fn poly1305_dup;
|
||||
static OSSL_OP_mac_freectx_fn poly1305_free;
|
||||
static OSSL_OP_mac_gettable_params_fn poly1305_gettable_params;
|
||||
static OSSL_OP_mac_get_params_fn poly1305_get_params;
|
||||
static OSSL_OP_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
|
||||
static OSSL_OP_mac_set_ctx_params_fn poly1305_set_ctx_params;
|
||||
static OSSL_OP_mac_init_fn poly1305_init;
|
||||
static OSSL_OP_mac_update_fn poly1305_update;
|
||||
static OSSL_OP_mac_final_fn poly1305_final;
|
||||
|
||||
struct poly1305_data_st {
|
||||
void *provctx;
|
||||
POLY1305 poly1305; /* Poly1305 data */
|
||||
};
|
||||
|
||||
static size_t poly1305_size(void);
|
||||
|
||||
static void *poly1305_new(void *provctx)
|
||||
{
|
||||
struct poly1305_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
ctx->provctx = provctx;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void poly1305_free(void *vmacctx)
|
||||
{
|
||||
OPENSSL_free(vmacctx);
|
||||
}
|
||||
|
||||
static void *poly1305_dup(void *vsrc)
|
||||
{
|
||||
struct poly1305_data_st *src = vsrc;
|
||||
struct poly1305_data_st *dst = poly1305_new(src->provctx);
|
||||
|
||||
if (dst == NULL)
|
||||
return NULL;
|
||||
|
||||
dst->poly1305 = src->poly1305;
|
||||
return dst;
|
||||
}
|
||||
|
||||
static size_t poly1305_size(void)
|
||||
{
|
||||
return POLY1305_DIGEST_SIZE;
|
||||
}
|
||||
|
||||
static int poly1305_init(void *vmacctx)
|
||||
{
|
||||
/* initialize the context in MAC_ctrl function */
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int poly1305_update(void *vmacctx, const unsigned char *data,
|
||||
size_t datalen)
|
||||
{
|
||||
struct poly1305_data_st *ctx = vmacctx;
|
||||
|
||||
/* poly1305 has nothing to return in its update function */
|
||||
Poly1305_Update(&ctx->poly1305, data, datalen);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
|
||||
size_t outsize)
|
||||
{
|
||||
struct poly1305_data_st *ctx = vmacctx;
|
||||
|
||||
Poly1305_Final(&ctx->poly1305, out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_gettable_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *poly1305_gettable_params(void)
|
||||
{
|
||||
return known_gettable_params;
|
||||
}
|
||||
|
||||
static int poly1305_get_params(OSSL_PARAM params[])
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
|
||||
return OSSL_PARAM_set_size_t(p, poly1305_size());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *poly1305_settable_ctx_params(void)
|
||||
{
|
||||
return known_settable_ctx_params;
|
||||
}
|
||||
|
||||
static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
|
||||
{
|
||||
struct poly1305_data_st *ctx = vmacctx;
|
||||
const OSSL_PARAM *p = NULL;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
|
||||
if (p->data_type != OSSL_PARAM_OCTET_STRING
|
||||
|| p->data_size != POLY1305_KEY_SIZE) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
Poly1305_Init(&ctx->poly1305, p->data);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH poly1305_functions[] = {
|
||||
{ OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
|
||||
{ OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
|
||||
{ OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
|
||||
{ OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
|
||||
{ OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
|
||||
{ OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
|
||||
{ OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
|
||||
{ OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
|
||||
{ OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))poly1305_settable_ctx_params },
|
||||
{ OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Copyright 2018 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 <string.h>
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include "internal/siphash.h"
|
||||
/*
|
||||
* TODO(3.0) when siphash has moved entirely to our providers, this
|
||||
* header should be moved to the provider include directory. For the
|
||||
* moment, crypto/siphash/siphash_ameth.c has us stuck.
|
||||
*/
|
||||
#include "../../../crypto/siphash/siphash_local.h"
|
||||
|
||||
#include "internal/providercommonerr.h"
|
||||
#include "internal/provider_algs.h"
|
||||
|
||||
/*
|
||||
* Forward declaration of everything implemented here. This is not strictly
|
||||
* necessary for the compiler, but provides an assurance that the signatures
|
||||
* of the functions in the dispatch table are correct.
|
||||
*/
|
||||
static OSSL_OP_mac_newctx_fn siphash_new;
|
||||
static OSSL_OP_mac_dupctx_fn siphash_dup;
|
||||
static OSSL_OP_mac_freectx_fn siphash_free;
|
||||
static OSSL_OP_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
|
||||
static OSSL_OP_mac_get_ctx_params_fn siphash_get_ctx_params;
|
||||
static OSSL_OP_mac_settable_ctx_params_fn siphash_settable_params;
|
||||
static OSSL_OP_mac_set_ctx_params_fn siphash_set_params;
|
||||
static OSSL_OP_mac_size_fn siphash_size;
|
||||
static OSSL_OP_mac_init_fn siphash_init;
|
||||
static OSSL_OP_mac_update_fn siphash_update;
|
||||
static OSSL_OP_mac_final_fn siphash_final;
|
||||
|
||||
struct siphash_data_st {
|
||||
void *provctx;
|
||||
SIPHASH siphash; /* Siphash data */
|
||||
};
|
||||
|
||||
static void *siphash_new(void *provctx)
|
||||
{
|
||||
struct siphash_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
ctx->provctx = provctx;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void siphash_free(void *vmacctx)
|
||||
{
|
||||
OPENSSL_free(vmacctx);
|
||||
}
|
||||
|
||||
static void *siphash_dup(void *vsrc)
|
||||
{
|
||||
struct siphash_data_st *ssrc = vsrc;
|
||||
struct siphash_data_st *sdst = siphash_new(ssrc->provctx);
|
||||
|
||||
if (sdst == NULL)
|
||||
return NULL;
|
||||
|
||||
sdst->siphash = ssrc->siphash;
|
||||
return sdst;
|
||||
}
|
||||
|
||||
static size_t siphash_size(void *vmacctx)
|
||||
{
|
||||
struct siphash_data_st *ctx = vmacctx;
|
||||
|
||||
return SipHash_hash_size(&ctx->siphash);
|
||||
}
|
||||
|
||||
static int siphash_init(void *vmacctx)
|
||||
{
|
||||
/* Not much to do here, actual initialization happens through controls */
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int siphash_update(void *vmacctx, const unsigned char *data,
|
||||
size_t datalen)
|
||||
{
|
||||
struct siphash_data_st *ctx = vmacctx;
|
||||
|
||||
SipHash_Update(&ctx->siphash, data, datalen);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
|
||||
size_t outsize)
|
||||
{
|
||||
struct siphash_data_st *ctx = vmacctx;
|
||||
size_t hlen = siphash_size(ctx);
|
||||
|
||||
if (outsize < hlen)
|
||||
return 0;
|
||||
|
||||
*outl = hlen;
|
||||
return SipHash_Final(&ctx->siphash, out, hlen);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *siphash_gettable_ctx_params(void)
|
||||
{
|
||||
return known_gettable_ctx_params;
|
||||
}
|
||||
|
||||
static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
|
||||
return OSSL_PARAM_set_size_t(p, siphash_size(vmacctx));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
|
||||
OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *siphash_settable_params(void)
|
||||
{
|
||||
return known_settable_ctx_params;
|
||||
}
|
||||
|
||||
static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
|
||||
{
|
||||
struct siphash_data_st *ctx = vmacctx;
|
||||
const OSSL_PARAM *p = NULL;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
|
||||
size_t size;
|
||||
|
||||
if (!OSSL_PARAM_get_size_t(p, &size)
|
||||
|| !SipHash_set_hash_size(&ctx->siphash, size))
|
||||
return 0;
|
||||
}
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
|
||||
if (p->data_type != OSSL_PARAM_OCTET_STRING
|
||||
|| p->data_size != SIPHASH_KEY_SIZE
|
||||
|| !SipHash_Init(&ctx->siphash, p->data, 0, 0))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH siphash_functions[] = {
|
||||
{ OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
|
||||
{ OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
|
||||
{ OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
|
||||
{ OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
|
||||
{ OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
|
||||
{ OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
|
||||
{ OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))siphash_gettable_ctx_params },
|
||||
{ OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
|
||||
{ OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))siphash_settable_params },
|
||||
{ OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
Reference in New Issue
Block a user