Latest update.

This commit is contained in:
2019-10-17 23:54:38 +09:00
parent 41a23ae6f6
commit ee84d0dd84
1357 changed files with 41111 additions and 9603 deletions
+146 -19
View File
@@ -1,25 +1,152 @@
SUBDIRS=common default
# We place all implementations in static libraries, and then let the
# provider mains pilfer what they want through symbol resolution when
# linking.
#
# The non-legacy implementations (libimplementations) must be made FIPS
# agnostic as much as possible, as well as the common building blocks
# (libcommon). The legacy implementations (liblegacy) will never be
# part of the FIPS provider.
#
# If there is anything that isn't FIPS agnostic, it should be set aside
# in its own source file, which is then included directly into other
# static libraries geared for FIPS and non-FIPS providers, and built
# separately.
#
# libcommon.a Contains common building blocks, potentially
# needed both by non-legacy and legacy code.
#
# libimplementations.a Contains all non-legacy implementations.
# liblegacy.a Contains all legacy implementaions.
#
# libfips.a Contains all things needed to support
# FIPS implementations, such as code from
# crypto/ and object files that contain
# FIPS-specific code. FIPS_MODE is defined
# for this library. The FIPS module uses
# this.
# libnonfips.a Corresponds to libfips.a, but built with
# FIPS_MODE undefined. The default and legacy
# providers use this.
SUBDIRS=common implementations
INCLUDE[../libcrypto]=common/include
IF[{- !$disabled{fips} -}]
SUBDIRS=fips
MODULES=fips
IF[{- defined $target{shared_defflag} -}]
SOURCE[fips]=fips.ld
GENERATE[fips.ld]=../util/providers.num
ENDIF
INCLUDE[fips]=.. ../include ../crypto/include common/include
DEFINE[fips]=FIPS_MODE
# Libraries we're dealing with
$LIBCOMMON=libcommon.a
$LIBIMPLEMENTATIONS=libimplementations.a
$LIBLEGACY=liblegacy.a
$LIBNONFIPS=libnonfips.a
$LIBFIPS=libfips.a
# Enough of our implementations include prov/ciphercommon.h (present in
# providers/common/include), which includes crypto/ciphermode_platform.h
# (present in include), which in turn may include very internal header
# files in crypto/, so let's have a common include list for them all.
$COMMON_INCLUDES=../crypto ../include common/include
INCLUDE[$LIBCOMMON]=$COMMON_INCLUDES
INCLUDE[$LIBIMPLEMENTATIONS]=.. $COMMON_INCLUDES implementations/include
INCLUDE[$LIBLEGACY]=$COMMON_INCLUDES implementations/include
INCLUDE[$LIBNONFIPS]=$COMMON_INCLUDES
INCLUDE[$LIBFIPS]=.. $COMMON_INCLUDES
DEFINE[$LIBFIPS]=FIPS_MODE
# Weak dependencies to provide library order information.
# We make it weak so they aren't both used always; what is
# actually used is determined by non-weak dependencies.
DEPEND[$LIBIMPLEMENTATIONS]{weak}=$LIBFIPS $LIBNONFIPS
DEPEND[$LIBCOMMON]{weak}=$LIBFIPS
# Strong dependencies. This ensures that any time libimplementations
# is used, libcommon gets included as well.
DEPEND[$LIBIMPLEMENTATIONS]=$LIBCOMMON
DEPEND[$LIBNONFIPS]=../libcrypto
# It's tempting to make libcommon depend on ../libcrypto. However,
# since the FIPS provider module must NOT depend on ../libcrypto, we
# need to set that dependency up specifically for the final products
# that use $LIBCOMMON or anything that depends on it.
# Libraries common to all providers, must be built regardless
LIBS{noinst}=$LIBCOMMON
# Libraries that are common for all non-FIPS providers, must be built regardless
LIBS{noinst}=$LIBNONFIPS $LIBIMPLEMENTATIONS
#
# Default provider stuff
#
# Because the default provider is built in, it means that libcrypto must
# include all the object files that are needed (we do that indirectly,
# by using the appropriate libraries as source). Note that for shared
# libraries, SOURCEd libraries are considered as if the where specified
# with DEPEND.
$DEFAULTGOAL=../libcrypto
SOURCE[$DEFAULTGOAL]=$LIBIMPLEMENTATIONS $LIBNONFIPS
SOURCE[$DEFAULTGOAL]=defltprov.c
# Some legacy implementations depend on provider header files
INCLUDE[../libcrypto]=implementations/include
LIBS=$DEFAULTGOAL
#
# FIPS provider stuff
#
# We define it this way to ensure that configdata.pm will have all the
# necessary information even if we don't build the module. This will allow
# us to make all kinds of checks on the source, based on what we specify in
# diverse build.info files. libfips.a, fips.so and their sources aren't
# built unless the proper LIBS or MODULES statement has been seen, so we
# have those and only those within a condition.
SUBDIRS=fips
$FIPSGOAL=fips
DEPEND[$FIPSGOAL]=$LIBIMPLEMENTATIONS $LIBFIPS
INCLUDE[$FIPSGOAL]=../include
IF[{- defined $target{shared_defflag} -}]
SOURCE[$FIPSGOAL]=fips.ld
GENERATE[fips.ld]=../util/providers.num
ENDIF
IF[{- !$disabled{legacy} -}]
SUBDIRS=legacy
MODULES=legacy
IF[{- defined $target{shared_defflag} -}]
SOURCE[legacy]=legacy.ld
GENERATE[legacy.ld]=../util/providers.num
ENDIF
INCLUDE[legacy]=.. ../include ../crypto/include common/include
DEPEND[legacy]=../libcrypto
IF[{- !$disabled{fips} -}]
# This is the trigger to actually build the FIPS module. Without these
# statements, the final build file will not have a trace of it.
MODULES=$FIPSGOAL
LIBS{noinst}=$LIBFIPS
ENDIF
#
# Legacy provider stuff
#
IF[{- !$disabled{legacy} -}]
# The legacy implementation library
LIBS{noinst}=$LIBLEGACY
DEPEND[$LIBLEGACY]=$LIBCOMMON $LIBNONFIPS
# The Legacy provider
IF[{- $disabled{module} -}]
# Become built in
# In this case, we need to do the same thing a for the default provider,
# and make the liblegacy object files end up in libcrypto. We could also
# just say that for the built-in legacy, we put the source directly in
# libcrypto instead of going via liblegacy, but that makes writing the
# implementation specific build.info files harder to write, so we don't.
$LEGACYGOAL=../libcrypto
SOURCE[$LEGACYGOAL]=$LIBLEGACY
DEFINE[$LIBLEGACY]=STATIC_LEGACY
DEFINE[$LEGACYGOAL]=STATIC_LEGACY
ELSE
# Become a module
# In this case, we can work with dependencies
$LEGACYGOAL=legacy
MODULES=$LEGACYGOAL
DEPEND[$LEGACYGOAL]=$LIBLEGACY
IF[{- defined $target{shared_defflag} -}]
SOURCE[legacy]=legacy.ld
GENERATE[legacy.ld]=../util/providers.num
ENDIF
ENDIF
# Common things that are valid no matter what form the Legacy provider
# takes.
SOURCE[$LEGACYGOAL]=legacyprov.c
INCLUDE[$LEGACYGOAL]=../include implementations/include
ENDIF
+5 -4
View File
@@ -1,5 +1,6 @@
SUBDIRS=digests ciphers macs kdfs exchange keymgmt signature
$COMMON=provider_util.c
SUBDIRS=digests ciphers
SOURCE[../../libcrypto]=$COMMON provider_err.c provlib.c
SOURCE[../fips]=$COMMON
SOURCE[../libcommon.a]=provider_err.c provlib.c
$FIPSCOMMON=provider_util.c
SOURCE[../libnonfips.a]=$FIPSCOMMON
SOURCE[../libfips.a]=$FIPSCOMMON
+2 -2
View File
@@ -8,8 +8,8 @@
*/
#include <assert.h>
#include "cipher_locl.h"
#include "internal/providercommonerr.h"
#include "cipher_local.h"
#include "prov/providercommonerr.h"
/*
* Fills a single block of buffered data from the input, and returns the amount
+4 -20
View File
@@ -1,21 +1,5 @@
LIBS=../../../libcrypto
IF[{- !$disabled{des} -}]
$COMMON_DES=cipher_tdes.c cipher_tdes_hw.c
ENDIF
$COMMON=cipher_common.c cipher_common_hw.c block.c \
cipher_aes.c cipher_aes_hw.c \
cipher_aes_xts.c cipher_aes_xts_hw.c \
# This source is common building blocks for all ciphers in all our providers.
SOURCE[../../libcommon.a]=\
cipher_common.c cipher_common_hw.c block.c \
cipher_gcm.c cipher_gcm_hw.c \
cipher_aes_gcm.c cipher_aes_gcm_hw.c \
cipher_ccm.c cipher_ccm_hw.c \
cipher_aes_ccm.c cipher_aes_ccm_hw.c \
cipher_aes_wrp.c \
$COMMON_DES
SOURCE[../../../libcrypto]=$COMMON
INCLUDE[../../../libcrypto]=. ../../../crypto
SOURCE[../../fips]=$COMMON
INCLUDE[../../fips]=. ../../../crypto
cipher_ccm.c cipher_ccm_hw.c
+5 -10
View File
@@ -9,9 +9,9 @@
/* Dispatch functions for ccm mode */
#include "cipher_locl.h"
#include "internal/ciphers/cipher_ccm.h"
#include "internal/providercommonerr.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_ccm.h"
#include "prov/providercommonerr.h"
static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
size_t *padlen, const unsigned char *in,
@@ -213,7 +213,6 @@ static int ccm_init(void *vctx, const unsigned char *key, size_t keylen,
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
return 0;
}
memcpy(ctx->iv, iv, ivlen);
ctx->iv_set = 1;
}
@@ -279,11 +278,11 @@ int ccm_cipher(void *vctx,
if (outsize < inl) {
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
return -1;
return 0;
}
if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
return -1;
return 0;
*outl = inl;
return 1;
@@ -420,7 +419,3 @@ void ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
ctx->hw = hw;
}
void ccm_finalctx(PROV_CCM_CTX *ctx)
{
OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
}
+2 -2
View File
@@ -7,8 +7,8 @@
* https://www.openssl.org/source/license.html
*/
#include "internal/ciphers/ciphercommon.h"
#include "internal/ciphers/cipher_ccm.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_ccm.h"
int ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce,
size_t nlen, size_t mlen)
+22 -20
View File
@@ -11,9 +11,9 @@
* Generic dispatch table functions for ciphers.
*/
#include "cipher_locl.h"
#include "internal/provider_ctx.h"
#include "internal/providercommonerr.h"
#include "cipher_local.h"
#include "prov/provider_ctx.h"
#include "prov/providercommonerr.h"
/*-
* Generic cipher functions for OSSL_PARAM gettables and settables
@@ -68,16 +68,8 @@ int cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(cipher_generic)
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(cipher_generic)
static const OSSL_PARAM cipher_known_settable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL),
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
OSSL_PARAM_END
};
const OSSL_PARAM *cipher_generic_settable_ctx_params(void)
{
return cipher_known_settable_ctx_params;
}
CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(cipher_generic)
CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(cipher_generic)
/*-
* AEAD cipher functions for OSSL_PARAM gettables and settables
@@ -117,11 +109,8 @@ static int cipher_generic_init_internal(PROV_CIPHER_CTX *ctx,
ctx->enc = enc ? 1 : 0;
if (iv != NULL && ctx->mode != EVP_CIPH_ECB_MODE) {
if (ivlen != ctx->ivlen) {
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
if (!cipher_generic_initiv(ctx, iv, ivlen))
return 0;
}
memcpy(ctx->iv, iv, ctx->ivlen);
}
if (key != NULL) {
if ((ctx->flags & EVP_CIPH_VARIABLE_LENGTH) == 0) {
@@ -330,8 +319,8 @@ int cipher_generic_get_ctx_params(void *vctx, OSSL_PARAM params[])
}
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
if (p != NULL
&& !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)
&& !OSSL_PARAM_set_octet_string(p, &ctx->iv, ctx->ivlen)) {
&& !OSSL_PARAM_set_octet_ptr(p, &ctx->oiv, ctx->ivlen)
&& !OSSL_PARAM_set_octet_string(p, &ctx->oiv, ctx->ivlen)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
@@ -345,7 +334,6 @@ int cipher_generic_get_ctx_params(void *vctx, OSSL_PARAM params[])
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
return 1;
}
@@ -387,6 +375,20 @@ int cipher_generic_set_ctx_params(void *vctx, const OSSL_PARAM params[])
return 1;
}
int cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
size_t ivlen)
{
if (ivlen != ctx->ivlen
|| ivlen > sizeof(ctx->iv)) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
return 0;
}
ctx->iv_set = 1;
memcpy(ctx->iv, iv, ivlen);
memcpy(ctx->oiv, iv, ivlen);
return 1;
}
void cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
size_t ivbits, unsigned int mode, uint64_t flags,
const PROV_CIPHER_HW *hw, void *provctx)
+1 -1
View File
@@ -7,7 +7,7 @@
* https://www.openssl.org/source/license.html
*/
#include "cipher_locl.h"
#include "prov/ciphercommon.h"
/*-
* The generic cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
+8 -13
View File
@@ -9,11 +9,11 @@
/* Dispatch functions for gcm mode */
#include "cipher_locl.h"
#include "internal/ciphers/cipher_gcm.h"
#include "internal/providercommonerr.h"
#include "internal/rand_int.h"
#include "internal/provider_ctx.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_gcm.h"
#include "prov/providercommonerr.h"
#include "crypto/rand.h"
#include "prov/provider_ctx.h"
static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len);
static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
@@ -38,11 +38,6 @@ void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
}
void gcm_deinitctx(PROV_GCM_CTX *ctx)
{
OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
}
static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
const unsigned char *iv, size_t ivlen, int enc)
{
@@ -56,7 +51,7 @@ static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
return 0;
}
ctx->ivlen = ivlen;
memcpy(ctx->iv, iv, ctx->ivlen);
memcpy(ctx->iv, iv, ivlen);
ctx->iv_state = IV_STATE_BUFFERED;
}
@@ -268,11 +263,11 @@ int gcm_cipher(void *vctx,
if (outsize < inl) {
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
return -1;
return 0;
}
if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
return -1;
return 0;
*outl = inl;
return 1;
+2 -2
View File
@@ -7,8 +7,8 @@
* https://www.openssl.org/source/license.html
*/
#include "cipher_locl.h"
#include "internal/ciphers/cipher_gcm.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_gcm.h"
int gcm_setiv(PROV_GCM_CTX *ctx, const unsigned char *iv, size_t ivlen)
+13
View File
@@ -0,0 +1,13 @@
/*
* 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 "prov/ciphercommon.h"
void padblock(unsigned char *buf, size_t *buflen, size_t blocksize);
int unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize);
-29
View File
@@ -1,29 +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 "internal/ciphers/ciphercommon.h"
#define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \
static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
#define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \
OSSL_PARAM_END \
}; \
const OSSL_PARAM * name##_gettable_ctx_params(void) \
{ \
return name##_known_gettable_ctx_params; \
}
void padblock(unsigned char *buf, size_t *buflen, size_t blocksize);
int unpadblock(unsigned char *buf, size_t *buflen, size_t blocksize);
+2 -5
View File
@@ -1,5 +1,2 @@
$COMMON=sha2_prov.c sha3_prov.c digest_common.c
SOURCE[../../../libcrypto]=$COMMON
SOURCE[../../fips]=$COMMON
SOURCE[../../legacy]= digest_common.c
# This source is common for all digests in all our providers.
SOURCE[../../libcommon.a]=digest_common.c
+2 -2
View File
@@ -8,8 +8,8 @@
*/
#include "openssl/err.h"
#include "internal/digestcommon.h"
#include "internal/providercommonerr.h"
#include "prov/digestcommon.h"
#include "prov/providercommonerr.h"
int digest_default_get_params(OSSL_PARAM params[], size_t blksz, size_t paramsz,
unsigned long flags)
-7
View File
@@ -1,7 +0,0 @@
LIBS=../../../libcrypto
IF[{- !$disabled{dh} -}]
SOURCE[../../../libcrypto]=\
dh_exch.c
ENDIF
@@ -10,10 +10,11 @@
#define UNINITIALISED_SIZET ((size_t)-1)
/* TODO(3.0) Figure out what flags are really needed */
#define AEAD_FLAGS (EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1 \
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
| EVP_CIPH_CUSTOM_COPY)
#define AEAD_FLAGS (EVP_CIPH_FLAG_AEAD_CIPHER \
| EVP_CIPH_CUSTOM_IV \
| EVP_CIPH_ALWAYS_CALL_INIT \
| EVP_CIPH_CTRL_INIT \
| EVP_CIPH_CUSTOM_COPY)
#define IMPLEMENT_aead_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits) \
static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lc##_get_params; \
@@ -121,7 +121,6 @@ OSSL_OP_cipher_update_fn ccm_stream_update;
OSSL_OP_cipher_final_fn ccm_stream_final;
OSSL_OP_cipher_cipher_fn ccm_cipher;
void ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw);
void ccm_finalctx(PROV_CCM_CTX *ctx);
int ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce,
size_t nlen, size_t mlen);
@@ -139,7 +139,6 @@ OSSL_OP_cipher_set_ctx_params_fn gcm_set_ctx_params;
OSSL_OP_cipher_cipher_fn gcm_cipher;
OSSL_OP_cipher_update_fn gcm_stream_update;
OSSL_OP_cipher_final_fn gcm_stream_final;
void gcm_deinitctx(PROV_GCM_CTX *ctx);
void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
const PROV_GCM_HW *hw, size_t ivlen_min);
@@ -12,8 +12,8 @@
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include "internal/cryptlib.h"
#include "internal/modes_int.h"
#include "internal/ciphermode_platform.h"
#include "crypto/modes.h"
#include "crypto/ciphermode_platform.h"
#define MAXCHUNK ((size_t)1 << (sizeof(long) * 8 - 2))
#define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4))
@@ -40,12 +40,13 @@ struct prov_cipher_ctx_st {
} stream;
unsigned int mode;
size_t keylen; /* key size (in bytes) */
size_t keylen; /* key size (in bytes) */
size_t ivlen;
size_t blocksize;
size_t bufsz; /* Number of bytes in buf */
unsigned int pad : 1; /* Whether padding should be used or not */
unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */
size_t bufsz; /* Number of bytes in buf */
unsigned int pad : 1; /* Whether padding should be used or not */
unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */
unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */
/*
* num contains the number of bytes of |iv| which are valid for modes that
@@ -54,6 +55,8 @@ struct prov_cipher_ctx_st {
unsigned int num;
uint64_t flags;
/* The original value of the iv */
unsigned char oiv[GENERIC_BLOCK_SIZE];
/* Buffer of partial blocks processed via update calls */
unsigned char buf[GENERIC_BLOCK_SIZE];
unsigned char iv[GENERIC_BLOCK_SIZE];
@@ -88,25 +91,8 @@ void cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
size_t ivbits, unsigned int mode, uint64_t flags,
const PROV_CIPHER_HW *hw, void *provctx);
#define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
blkbits, ivbits, typ) \
static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
static int alg##_##kbits##_##lcmode##_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 alg##_##kbits##_##lcmode##_newctx; \
static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
{ \
PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
if (ctx != NULL) { \
cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
EVP_CIPH_##UCMODE##_MODE, flags, \
PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \
} \
return ctx; \
} \
#define IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,\
blkbits, ivbits, typ) \
const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \
{ OSSL_FUNC_CIPHER_NEWCTX, \
(void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
@@ -132,6 +118,28 @@ const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \
{ 0, NULL } \
};
#define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
blkbits, ivbits, typ) \
static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
static int alg##_##kbits##_##lcmode##_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 alg##_##kbits##_##lcmode##_newctx; \
static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
{ \
PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
if (ctx != NULL) { \
cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
EVP_CIPH_##UCMODE##_MODE, flags, \
PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \
} \
return ctx; \
} \
IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \
blkbits, ivbits, typ)
PROV_CIPHER_HW_FN cipher_hw_generic_cbc;
PROV_CIPHER_HW_FN cipher_hw_generic_ecb;
PROV_CIPHER_HW_FN cipher_hw_generic_ofb128;
@@ -225,6 +233,38 @@ static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
return 1; \
}
#define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \
static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
#define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \
OSSL_PARAM_END \
}; \
const OSSL_PARAM * name##_gettable_ctx_params(void) \
{ \
return name##_known_gettable_ctx_params; \
}
#define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name) \
static const OSSL_PARAM name##_known_settable_ctx_params[] = { \
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
#define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name) \
OSSL_PARAM_END \
}; \
const OSSL_PARAM * name##_settable_ctx_params(void) \
{ \
return name##_known_settable_ctx_params; \
}
int cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
size_t ivlen);
size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
const unsigned char **in, size_t *inlen);
int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
@@ -7,8 +7,8 @@
* https://www.openssl.org/source/license.html
*/
#ifndef OSSL_DIGESTCOMMON_H
# define OSSL_DIGESTCOMMON_H
#ifndef OSSL_PROVIDERS_DIGESTCOMMON_H
# define OSSL_PROVIDERS_DIGESTCOMMON_H
# include <openssl/core_numbers.h>
# include <openssl/core_names.h>
@@ -100,4 +100,4 @@ int digest_default_get_params(OSSL_PARAM params[], size_t blksz, size_t paramsz,
}
# endif
#endif /* OSSL_DIGESTCOMMON_H */
#endif /* OSSL_PROVIDERS_DIGESTCOMMON_H */
@@ -8,8 +8,8 @@
* https://www.openssl.org/source/license.html
*/
#ifndef HEADER_PROVERR_H
# define HEADER_PROVERR_H
#ifndef OPENSSL_PROVERR_H
# define OPENSSL_PROVERR_H
# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
@@ -69,13 +69,16 @@ int ERR_load_PROV_strings(void);
# define PROV_R_INVALID_KEYLEN 117
# define PROV_R_INVALID_KEY_LEN 124
# define PROV_R_INVALID_KEY_LENGTH 105
# define PROV_R_INVALID_MAC 151
# define PROV_R_INVALID_MODE 125
# define PROV_R_INVALID_MODE_INT 126
# define PROV_R_INVALID_SALT_LENGTH 112
# define PROV_R_INVALID_SEED_LENGTH 154
# define PROV_R_INVALID_TAG 110
# define PROV_R_INVALID_TAGLEN 118
# define PROV_R_MISSING_CEK_ALG 144
# define PROV_R_MISSING_KEY 128
# define PROV_R_MISSING_MAC 150
# define PROV_R_MISSING_MESSAGE_DIGEST 129
# define PROV_R_MISSING_PASS 130
# define PROV_R_MISSING_SALT 131
@@ -93,7 +96,9 @@ int ERR_load_PROV_strings(void);
# define PROV_R_UNABLE_TO_LOAD_SHA1 143
# define PROV_R_UNABLE_TO_LOAD_SHA256 147
# define PROV_R_UNSUPPORTED_CEK_ALG 145
# define PROV_R_UNSUPPORTED_KEY_SIZE 153
# define PROV_R_UNSUPPORTED_MAC_TYPE 137
# define PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS 152
# define PROV_R_VALUE_ERROR 138
# define PROV_R_WRONG_FINAL_BLOCK_LENGTH 107
# define PROV_R_WRONG_OUTPUT_BUFFER_SIZE 139
-13
View File
@@ -1,13 +0,0 @@
$COMMON=tls1_prf.c hkdf.c pbkdf2.c sskdf.c
LIBS=../../../libcrypto
SOURCE[../../../libcrypto]=$COMMON
INCLUDE[../../../libcrypto]=. ../../../crypto
IF[{- !$disabled{fips} -}]
MODULES=../../fips
SOURCE[../../fips]=$COMMON
INCLUDE[../../fips]=. ../../../crypto
ENDIF
-9
View File
@@ -1,9 +0,0 @@
LIBS=../../../libcrypto
IF[{- !$disabled{dh} -}]
SOURCE[../../../libcrypto]=\
dh_kmgmt.c
ENDIF
IF[{- !$disabled{dsa} -}]
SOURCE[../../../libcrypto]=\
dsa_kmgmt.c
ENDIF
-88
View File
@@ -1,88 +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/core_names.h>
#include <openssl/bn.h>
#include <openssl/dh.h>
#include <openssl/params.h>
#include "internal/provider_algs.h"
static OSSL_OP_keymgmt_importkey_fn dh_importkey;
static int params_to_key(DH *dh, const OSSL_PARAM params[])
{
const OSSL_PARAM *param_p, *param_g, *param_priv_key, *param_pub_key;
BIGNUM *p = NULL, *g = NULL, *priv_key = NULL, *pub_key = NULL;
if (dh == NULL)
return 0;
param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
param_priv_key =
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_KEY);
param_pub_key =
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PUB_KEY);
/*
* DH documentation says that a public key must be present if a
* private key is present.
* We want to have at least a public key either way, so we end up
* requiring it unconditionally.
*/
if (param_pub_key == NULL)
return 0;
if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
|| (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g))
|| (param_priv_key != NULL
&& !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|| !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
goto err;
if (!DH_set0_pqg(dh, p, NULL, g))
goto err;
p = g = NULL;
if (!DH_set0_key(dh, pub_key, priv_key))
goto err;
priv_key = pub_key = NULL;
return 1;
err:
BN_free(p);
BN_free(g);
BN_free(priv_key);
BN_free(pub_key);
return 0;
}
static void *dh_importkey(void *provctx, const OSSL_PARAM params[])
{
DH *dh;
if ((dh = DH_new()) == NULL
|| !params_to_key(dh, params)) {
DH_free(dh);
dh = NULL;
}
return dh;
}
const OSSL_DISPATCH dh_keymgmt_functions[] = {
/*
* TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
* implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
*/
{ OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dh_importkey },
{ OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DH_free },
{ 0, NULL }
};
-91
View File
@@ -1,91 +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/core_names.h>
#include <openssl/bn.h>
#include <openssl/dsa.h>
#include <openssl/params.h>
#include "internal/provider_algs.h"
static OSSL_OP_keymgmt_importkey_fn dsa_importkey;
static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
{
const OSSL_PARAM *param_p, *param_q, *param_g, *param_priv_key;
const OSSL_PARAM *param_pub_key;
BIGNUM *p = NULL, *q = NULL, *g = NULL, *priv_key = NULL, *pub_key = NULL;
if (dsa == NULL)
return 0;
param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
param_priv_key =
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PRIV_KEY);
param_pub_key =
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DSA_PUB_KEY);
/*
* DSA documentation says that a public key must be present if a private key
* is.
*/
if (param_priv_key != NULL && param_pub_key == NULL)
return 0;
if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
|| (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
|| (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g))
|| (param_priv_key != NULL
&& !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|| (param_pub_key != NULL
&& !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
goto err;
if (!DSA_set0_pqg(dsa, p, q, g))
goto err;
p = q = g = NULL;
if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
goto err;
priv_key = pub_key = NULL;
return 1;
err:
BN_free(p);
BN_free(q);
BN_free(g);
BN_free(priv_key);
BN_free(pub_key);
return 0;
}
static void *dsa_importkey(void *provctx, const OSSL_PARAM params[])
{
DSA *dsa;
if ((dsa = DSA_new()) == NULL
|| !params_to_key(dsa, params)) {
DSA_free(dsa);
dsa = NULL;
}
return dsa;
}
const OSSL_DISPATCH dsa_keymgmt_functions[] = {
/*
* TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
* implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
*/
{ OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dsa_importkey },
{ OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DSA_free },
{ 0, NULL }
};
-15
View File
@@ -1,15 +0,0 @@
$COMMON=gmac_prov.c hmac_prov.c kmac_prov.c
IF[{- !$disabled{cmac} -}]
$COMMON=$COMMON cmac_prov.c
ENDIF
LIBS=../../../libcrypto
SOURCE[../../../libcrypto]=$COMMON
INCLUDE[../../../libcrypto]=. ../../../crypto
IF[{- !$disabled{fips} -}]
MODULES=../../fips
SOURCE[../../fips]=$COMMON
INCLUDE[../../fips]=. ../../../crypto
ENDIF
+9 -1
View File
@@ -9,7 +9,7 @@
*/
#include <openssl/err.h>
#include "internal/providercommonerr.h"
#include "prov/providercommonerr.h"
#ifndef OPENSSL_NO_ERR
@@ -44,14 +44,18 @@ static const ERR_STRING_DATA PROV_str_reasons[] = {
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_KEY_LEN), "invalid key len"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_KEY_LENGTH),
"invalid key length"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_MAC), "invalid mac"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_MODE), "invalid mode"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_MODE_INT), "invalid mode int"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_SALT_LENGTH),
"invalid salt length"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_SEED_LENGTH),
"invalid seed length"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_TAG), "invalid tag"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_TAGLEN), "invalid taglen"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_MISSING_CEK_ALG), "missing cek alg"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_MISSING_KEY), "missing key"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_MISSING_MAC), "missing mac"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_MISSING_MESSAGE_DIGEST),
"missing message digest"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_MISSING_PASS), "missing pass"},
@@ -76,8 +80,12 @@ static const ERR_STRING_DATA PROV_str_reasons[] = {
"unable to load sha256"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_UNSUPPORTED_CEK_ALG),
"unsupported cek alg"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_UNSUPPORTED_KEY_SIZE),
"unsupported key size"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_UNSUPPORTED_MAC_TYPE),
"unsupported mac type"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS),
"unsupported number of rounds"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_VALUE_ERROR), "value error"},
{ERR_PACK(ERR_LIB_PROV, 0, PROV_R_WRONG_FINAL_BLOCK_LENGTH),
"wrong final block length"},
+5 -5
View File
@@ -9,7 +9,7 @@
#include <openssl/evp.h>
#include <openssl/core_names.h>
#include "internal/provider_util.h"
#include "prov/provider_util.h"
void ossl_prov_cipher_reset(PROV_CIPHER *pc)
{
@@ -46,7 +46,7 @@ static int load_common(const OSSL_PARAM params[], const char **propquery,
/* TODO legacy stuff, to be removed */
/* Inside the FIPS module, we don't support legacy ciphers */
#if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
p = OSSL_PARAM_locate_const(params, "engine");
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING)
return 0;
@@ -214,17 +214,17 @@ int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
(char *)mdname, 0);
if (ciphername != NULL)
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
(char *)ciphername, 0);
if (properties != NULL)
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
(char *)properties, 0);
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE)) != NULL) {
if ((p = OSSL_PARAM_locate_const(params, "engine")) != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING)
return 0;
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE,
*mp++ = OSSL_PARAM_construct_utf8_string("engine",
p->data, p->data_size);
}
#endif
+1 -1
View File
@@ -8,7 +8,7 @@
*/
#include <openssl/objects.h>
#include "internal/providercommon.h"
#include "prov/providercommon.h"
/*
* The FIPS provider has its own version of this in fipsprov.c because it does
-7
View File
@@ -1,7 +0,0 @@
LIBS=../../../libcrypto
IF[{- !$disabled{dsa} -}]
SOURCE[../../../libcrypto]=\
dsa.c
ENDIF
-207
View File
@@ -1,207 +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/crypto.h>
#include <openssl/core_numbers.h>
#include <openssl/core_names.h>
#include <openssl/dsa.h>
#include <openssl/params.h>
#include "internal/provider_algs.h"
static OSSL_OP_signature_newctx_fn dsa_newctx;
static OSSL_OP_signature_sign_init_fn dsa_signature_init;
static OSSL_OP_signature_verify_init_fn dsa_signature_init;
static OSSL_OP_signature_sign_fn dsa_sign;
static OSSL_OP_signature_freectx_fn dsa_freectx;
static OSSL_OP_signature_dupctx_fn dsa_dupctx;
static OSSL_OP_signature_get_ctx_params_fn dsa_get_ctx_params;
static OSSL_OP_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;
static OSSL_OP_signature_set_ctx_params_fn dsa_set_ctx_params;
static OSSL_OP_signature_settable_ctx_params_fn dsa_settable_ctx_params;
/*
* What's passed as an actual key is defined by the KEYMGMT interface.
* We happen to know that our KEYMGMT simply passes DSA structures, so
* we use that here too.
*/
typedef struct {
DSA *dsa;
size_t mdsize;
/* Should be big enough */
char mdname[80];
} PROV_DSA_CTX;
static void *dsa_newctx(void *provctx)
{
return OPENSSL_zalloc(sizeof(PROV_DSA_CTX));
}
static int dsa_signature_init(void *vpdsactx, void *vdsa)
{
PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
if (pdsactx == NULL || vdsa == NULL || !DSA_up_ref(vdsa))
return 0;
DSA_free(pdsactx->dsa);
pdsactx->dsa = vdsa;
return 1;
}
static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
size_t sigsize, const unsigned char *tbs, size_t tbslen)
{
PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
int ret;
unsigned int sltmp;
size_t dsasize = DSA_size(pdsactx->dsa);
if (sig == NULL) {
*siglen = dsasize;
return 1;
}
if (sigsize < (size_t)dsasize)
return 0;
if (pdsactx->mdsize != 0 && tbslen != pdsactx->mdsize)
return 0;
ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa);
if (ret <= 0)
return 0;
*siglen = sltmp;
return 1;
}
static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
const unsigned char *tbs, size_t tbslen)
{
PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
if (pdsactx->mdsize != 0 && tbslen != pdsactx->mdsize)
return 0;
return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
}
static void dsa_freectx(void *vpdsactx)
{
PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
DSA_free(pdsactx->dsa);
OPENSSL_free(pdsactx);
}
static void *dsa_dupctx(void *vpdsactx)
{
PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
PROV_DSA_CTX *dstctx;
dstctx = OPENSSL_zalloc(sizeof(*srcctx));
if (dstctx == NULL)
return NULL;
*dstctx = *srcctx;
if (dstctx->dsa != NULL && !DSA_up_ref(dstctx->dsa)) {
OPENSSL_free(dstctx);
return NULL;
}
return dstctx;
}
static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
{
PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
OSSL_PARAM *p;
if (pdsactx == NULL || params == NULL)
return 0;
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
if (p != NULL && !OSSL_PARAM_set_size_t(p, pdsactx->mdsize))
return 0;
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))
return 0;
return 1;
}
static const OSSL_PARAM known_gettable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
OSSL_PARAM_END
};
static const OSSL_PARAM *dsa_gettable_ctx_params(void)
{
return known_gettable_ctx_params;
}
static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
{
PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
const OSSL_PARAM *p;
char *mdname;
if (pdsactx == NULL || params == NULL)
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
if (p != NULL && !OSSL_PARAM_get_size_t(p, &pdsactx->mdsize))
return 0;
/*
* We never actually use the mdname, but we do support getting it later.
* This can be useful for applications that want to know the MD that they
* previously set.
*/
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
mdname = pdsactx->mdname;
if (p != NULL
&& !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(pdsactx->mdname)))
return 0;
return 1;
}
static const OSSL_PARAM known_settable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
OSSL_PARAM_END
};
static const OSSL_PARAM *dsa_settable_ctx_params(void)
{
return known_settable_ctx_params;
}
const OSSL_DISPATCH dsa_signature_functions[] = {
{ OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
{ OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_signature_init },
{ OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
{ OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_signature_init },
{ OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
{ OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
{ OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
(void (*)(void))dsa_gettable_ctx_params },
{ OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
{ OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
(void (*)(void))dsa_settable_ctx_params },
{ 0, NULL }
};
-6
View File
@@ -1,6 +0,0 @@
SUBDIRS=digests macs ciphers
SUBDIRS=digests kdfs macs ciphers
LIBS=../../libcrypto
SOURCE[../../libcrypto]=\
defltprov.c
INCLUDE[../../libcrypto]=include
-53
View File
@@ -1,53 +0,0 @@
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 \
cipher_des.c cipher_des_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
-15
View File
@@ -1,15 +0,0 @@
IF[{- !$disabled{blake2} -}]
SOURCE[../../../libcrypto]=\
blake2_prov.c blake2b_prov.c blake2s_prov.c
ENDIF
IF[{- !$disabled{sm3} -}]
SOURCE[../../../libcrypto]=\
sm3_prov.c
ENDIF
IF[{- !$disabled{md5} -}]
SOURCE[../../../libcrypto]=\
md5_prov.c md5_sha1_prov.c
ENDIF
-3
View File
@@ -1,3 +0,0 @@
LIBS=../../../libcrypto
SOURCE[../../../libcrypto]=scrypt.c sshkdf.c x942kdf.c
INCLUDE[../../../libcrypto]=. ../../../crypto
-15
View File
@@ -1,15 +0,0 @@
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
@@ -14,7 +14,7 @@
#include <openssl/core_numbers.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include "internal/provider_algs.h"
#include "prov/implementations.h"
/* Functions provided by the core */
static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
@@ -50,16 +50,43 @@ static int deflt_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
return 1;
}
/*
* For the algorithm names, we use the following formula for our primary
* names:
*
* ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
*
* VERSION is only present if there are multiple versions of
* an alg (MD2, MD4, MD5). It may be omitted if there is only
* one version (if a subsequent version is released in the future,
* we can always change the canonical name, and add the old name
* as an alias).
*
* SUBNAME may be present where we are combining multiple
* algorithms together, e.g. MD5-SHA1.
*
* SIZE is only present if multiple versions of an algorithm exist
* with different sizes (e.g. AES-128-CBC, AES-256-CBC)
*
* MODE is only present where applicable.
*
* We add diverse other names where applicable, such as the names that
* NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
* we have used historically.
*/
static const OSSL_ALGORITHM deflt_digests[] = {
{ "SHA1", "default=yes", sha1_functions },
{ "SHA224", "default=yes", sha224_functions },
{ "SHA256", "default=yes", sha256_functions },
{ "SHA384", "default=yes", sha384_functions },
{ "SHA512", "default=yes", sha512_functions },
{ "SHA512-224", "default=yes", sha512_224_functions },
{ "SHA512-256", "default=yes", sha512_256_functions },
/* Our primary name:NIST name[:our older names] */
{ "SHA1:SHA-1", "default=yes", sha1_functions },
{ "SHA2-224:SHA-224:SHA224", "default=yes", sha224_functions },
{ "SHA2-256:SHA-256:SHA256", "default=yes", sha256_functions },
{ "SHA2-384:SHA-384:SHA384", "default=yes", sha384_functions },
{ "SHA2-512:SHA-512:SHA512", "default=yes", sha512_functions },
{ "SHA2-512/224:SHA-512/224:SHA512-224", "default=yes",
sha512_224_functions },
{ "SHA2-512/256:SHA-512/256:SHA512-256", "default=yes",
sha512_256_functions },
/* We agree with NIST here, so one name only */
{ "SHA3-224", "default=yes", sha3_224_functions },
{ "SHA3-256", "default=yes", sha3_256_functions },
{ "SHA3-384", "default=yes", sha3_384_functions },
@@ -72,12 +99,20 @@ static const OSSL_ALGORITHM deflt_digests[] = {
{ "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 },
/* Our primary name:NIST name */
{ "SHAKE-128:SHAKE128", "default=yes", shake_128_functions },
{ "SHAKE-256:SHAKE256", "default=yes", shake_256_functions },
#ifndef OPENSSL_NO_BLAKE2
{ "BLAKE2s256", "default=yes", blake2s256_functions },
{ "BLAKE2b512", "default=yes", blake2b512_functions },
/*
* https://blake2.net/ doesn't specify size variants,
* but mentions that Bouncy Castle uses the names
* BLAKE2b-160, BLAKE2b-256, BLAKE2b-384, and BLAKE2b-512
* If we assume that "2b" and "2s" are versions, that pattern
* fits with ours. We also add our historical names.
*/
{ "BLAKE2s-256:BLAKE2s256", "default=yes", blake2s256_functions },
{ "BLAKE2b-512:BLAKE2b512", "default=yes", blake2b512_functions },
#endif /* OPENSSL_NO_BLAKE2 */
#ifndef OPENSSL_NO_SM3
@@ -121,19 +156,24 @@ static const OSSL_ALGORITHM deflt_ciphers[] = {
{ "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 },
{ "AES-256-GCM:id-aes256-GCM", "default=yes", aes256gcm_functions },
{ "AES-192-GCM:id-aes192-GCM", "default=yes", aes192gcm_functions },
{ "AES-128-GCM:id-aes128-GCM", "default=yes", aes128gcm_functions },
{ "AES-256-CCM:id-aes256-CCM", "default=yes", aes256ccm_functions },
{ "AES-192-CCM:id-aes192-CCM", "default=yes", aes192ccm_functions },
{ "AES-128-CCM:id-aes128-CCM", "default=yes", aes128ccm_functions },
{ "AES-256-WRAP:id-aes256-wrap:AES256-WRAP", "default=yes",
aes256wrap_functions },
{ "AES-192-WRAP:id-aes192-wrap:AES192-WRAP", "default=yes",
aes192wrap_functions },
{ "AES-128-WRAP:id-aes128-wrap:AES128-WRAP", "default=yes",
aes128wrap_functions },
{ "AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD", "default=yes",
aes256wrappad_functions },
{ "AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD", "default=yes",
aes192wrappad_functions },
{ "AES-128-WRAP-PAD:id-aes128-wrap-pad: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 },
@@ -144,9 +184,9 @@ static const OSSL_ALGORITHM deflt_ciphers[] = {
{ "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-CBC:ARIA256", "default=yes", aria256cbc_functions },
{ "ARIA-192-CBC:ARIA192", "default=yes", aria192cbc_functions },
{ "ARIA-128-CBC:ARIA128", "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 },
@@ -167,9 +207,9 @@ static const OSSL_ALGORITHM deflt_ciphers[] = {
{ "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-CBC:CAMELLIA256", "default=yes", camellia256cbc_functions },
{ "CAMELLIA-192-CBC:CAMELLIA192", "default=yes", camellia192cbc_functions },
{ "CAMELLIA-128-CBC:CAMELLIA128", "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 },
@@ -187,20 +227,20 @@ static const OSSL_ALGORITHM deflt_ciphers[] = {
{ "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-ECB:DES-EDE3", "default=yes", tdes_ede3_ecb_functions },
{ "DES-EDE3-CBC:DES3", "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-ECB: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 },
{ "DESX-CBC:DESX", "default=yes", tdes_desx_cbc_functions },
{ "DES3-WRAP:id-smime-alg-CMS3DESwrap", "default=yes", tdes_wrap_cbc_functions },
{ "DES-ECB", "default=yes", des_ecb_functions },
{ "DES-CBC", "default=yes", des_cbc_functions },
{ "DES-CBC:DES", "default=yes", des_cbc_functions },
{ "DES-OFB", "default=yes", des_ofb64_functions },
{ "DES-CFB", "default=yes", des_cfb64_functions },
{ "DES-CFB1", "default=yes", des_cfb1_functions },
@@ -208,42 +248,66 @@ static const OSSL_ALGORITHM deflt_ciphers[] = {
#endif /* OPENSSL_NO_DES */
#ifndef OPENSSL_NO_BF
{ "BF-ECB", "default=yes", blowfish128ecb_functions },
{ "BF-CBC", "default=yes", blowfish128cbc_functions },
{ "BF-CBC:BF:BLOWFISH", "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 },
{ "IDEA-CBC:IDEA", "default=yes", idea128cbc_functions },
{ "IDEA-OFB:IDEA-OFB64", "default=yes", idea128ofb64_functions },
{ "IDEA-CFB:IDEA-CFB64", "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-CBC:CAST-CBC:CAST", "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 },
{ "SEED-CBC:SEED", "default=yes", seed128cbc_functions },
{ "SEED-OFB:SEED-OFB128", "default=yes", seed128ofb128_functions },
{ "SEED-CFB:SEED-CFB128", "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-CBC:SM4", "default=yes", sm4128cbc_functions },
{ "SM4-CTR", "default=yes", sm4128ctr_functions },
{ "SM4-OFB", "default=yes", sm4128ofb128_functions },
{ "SM4-CFB", "default=yes", sm4128cfb128_functions },
{ "SM4-OFB:SM4-OFB128", "default=yes", sm4128ofb128_functions },
{ "SM4-CFB:SM4-CFB128", "default=yes", sm4128cfb128_functions },
#endif /* OPENSSL_NO_SM4 */
#ifndef OPENSSL_NO_RC4
{ "RC4", "default=yes", rc4128_functions },
{ "RC4-40", "default=yes", rc440_functions },
#endif /* OPENSSL_NO_RC4 */
#ifndef OPENSSL_NO_RC5
{ "RC5-ECB", "default=yes", rc5128ecb_functions },
{ "RC5-CBC", "default=yes", rc5128cbc_functions },
{ "RC5-OFB", "default=yes", rc5128ofb64_functions },
{ "RC5-CFB", "default=yes", rc5128cfb64_functions },
#endif /* OPENSSL_NO_RC5 */
#ifndef OPENSSL_NO_RC2
{ "RC2-ECB", "default=yes", rc2128ecb_functions },
{ "RC2-CBC", "default=yes", rc2128cbc_functions },
{ "RC2-40-CBC", "default=yes", rc240cbc_functions },
{ "RC2-64-CBC", "default=yes", rc264cbc_functions },
{ "RC2-CFB", "default=yes", rc2128cfb128_functions },
{ "RC2-OFB", "default=yes", rc2128ofb128_functions },
#endif /* OPENSSL_NO_RC2 */
#ifndef OPENSSL_NO_CHACHA
{ "ChaCha20", "default=yes", chacha20_functions },
# ifndef OPENSSL_NO_POLY1305
{ "ChaCha20-Poly1305", "default=yes", chacha20_poly1305_functions },
# endif /* OPENSSL_NO_POLY1305 */
#endif /* OPENSSL_NO_CHACHA */
{ NULL, NULL, NULL }
};
static const OSSL_ALGORITHM deflt_macs[] = {
#ifndef OPENSSL_NO_BLAKE2
{ "BLAKE2BMAC", "default=yes", blake2bmac_functions },
{ "BLAKE2SMAC", "default=yes", blake2smac_functions },
{ "BLAKE2bMAC", "default=yes", blake2bmac_functions },
{ "BLAKE2sMAC", "default=yes", blake2smac_functions },
#endif
#ifndef OPENSSL_NO_CMAC
{ "CMAC", "default=yes", cmac_functions },
@@ -268,25 +332,26 @@ static const OSSL_ALGORITHM deflt_kdfs[] = {
{ 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 },
{ OSSL_KDF_NAME_KBKDF, "default=yes", kdf_kbkdf_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 },
{ "SCRYPT:id-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 },
{ "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 },
{ "DSA:dsaEncryption", "default=yes", dsa_signature_functions },
#endif
{ NULL, NULL, NULL }
};
@@ -294,7 +359,7 @@ static const OSSL_ALGORITHM deflt_signature[] = {
static const OSSL_ALGORITHM deflt_keymgmt[] = {
#ifndef OPENSSL_NO_DH
{ "dhKeyAgreement", "default=yes", dh_keymgmt_functions },
{ "DH", "default=yes", dh_keymgmt_functions },
#endif
#ifndef OPENSSL_NO_DSA
{ "DSA", "default=yes", dsa_keymgmt_functions },
+1
View File
@@ -1,2 +1,3 @@
SOURCE[../fips]=fipsprov.c selftest.c
INCLUDE[../fips]=../implementations/include ../common/include
+82 -47
View File
@@ -25,10 +25,10 @@
#include "internal/cryptlib.h"
#include "internal/property.h"
#include "internal/evp_int.h"
#include "internal/provider_algs.h"
#include "internal/provider_ctx.h"
#include "internal/providercommon.h"
#include "crypto/evp.h"
#include "prov/implementations.h"
#include "prov/provider_ctx.h"
#include "prov/providercommon.h"
#include "selftest.h"
extern OSSL_core_thread_start_fn *c_thread_start;
@@ -227,19 +227,19 @@ const char *ossl_prov_util_nid_to_name(int nid)
switch (nid) {
/* Digests */
case NID_sha1:
return "SHA224";
return "SHA1";
case NID_sha224:
return "SHA224";
return "SHA-224";
case NID_sha256:
return "SHA256";
return "SHA-256";
case NID_sha384:
return "SHA384";
return "SHA-384";
case NID_sha512:
return "SHA512";
return "SHA-512";
case NID_sha512_224:
return "SHA512-224";
return "SHA-512/224";
case NID_sha512_256:
return "SHA512-256";
return "SHA-512/256";
case NID_sha3_224:
return "SHA3-224";
case NID_sha3_256:
@@ -272,31 +272,30 @@ const char *ossl_prov_util_nid_to_name(int nid)
return "AES-256-XTS";
case NID_aes_128_xts:
return "AES-128-XTS";
/* TODO(3.0) Change these when we have aliases */
case NID_aes_256_gcm:
return "id-aes256-GCM";
return "AES-256-GCM";
case NID_aes_192_gcm:
return "id-aes192-GCM";
return "AES-192-GCM";
case NID_aes_128_gcm:
return "id-aes128-GCM";
return "AES-128-GCM";
case NID_aes_256_ccm:
return "id-aes256-CCM";
return "AES-256-CCM";
case NID_aes_192_ccm:
return "id-aes192-CCM";
return "AES-192-CCM";
case NID_aes_128_ccm:
return "id-aes128-CCM";
return "AES-128-CCM";
case NID_id_aes256_wrap:
return "id-aes256-wrap";
return "AES-256-WRAP";
case NID_id_aes192_wrap:
return "id-aes192-wrap";
return "AES-192-WRAP";
case NID_id_aes128_wrap:
return "id-aes128-wrap";
return "AES-128-WRAP";
case NID_id_aes256_wrap_pad:
return "id-aes256-wrap-pad";
return "AES-256-WRAP-PAD";
case NID_id_aes192_wrap_pad:
return "id-aes192-wrap-pad";
return "AES-192-WRAP-PAD";
case NID_id_aes128_wrap_pad:
return "id-aes128-wrap-pad";
return "AES-128-WRAP-PAD";
case NID_des_ede3_ecb:
return "DES-EDE3";
case NID_des_ede3_cbc:
@@ -308,14 +307,43 @@ const char *ossl_prov_util_nid_to_name(int nid)
return NULL;
}
/*
* For the algorithm names, we use the following formula for our primary
* names:
*
* ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
*
* VERSION is only present if there are multiple versions of
* an alg (MD2, MD4, MD5). It may be omitted if there is only
* one version (if a subsequent version is released in the future,
* we can always change the canonical name, and add the old name
* as an alias).
*
* SUBNAME may be present where we are combining multiple
* algorithms together, e.g. MD5-SHA1.
*
* SIZE is only present if multiple versions of an algorithm exist
* with different sizes (e.g. AES-128-CBC, AES-256-CBC)
*
* MODE is only present where applicable.
*
* We add diverse other names where applicable, such as the names that
* NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
* we have used historically.
*/
static const OSSL_ALGORITHM fips_digests[] = {
{ "SHA1", "fips=yes", sha1_functions },
{ "SHA224", "fips=yes", sha224_functions },
{ "SHA256", "fips=yes", sha256_functions },
{ "SHA384", "fips=yes", sha384_functions },
{ "SHA512", "fips=yes", sha512_functions },
{ "SHA512-224", "fips=yes", sha512_224_functions },
{ "SHA512-256", "fips=yes", sha512_256_functions },
/* Our primary name:NiST name[:our older names] */
{ "SHA1:SHA-1", "fips=yes", sha1_functions },
{ "SHA2-224:SHA-224:SHA224", "fips=yes", sha224_functions },
{ "SHA2-256:SHA-256:SHA256", "fips=yes", sha256_functions },
{ "SHA2-384:SHA-384:SHA384", "fips=yes", sha384_functions },
{ "SHA2-512:SHA-512:SHA512", "fips=yes", sha512_functions },
{ "SHA2-512/224:SHA-512/224:SHA512-224", "fips=yes",
sha512_224_functions },
{ "SHA2-512/256:SHA-512/256:SHA512-256", "fips=yes",
sha512_256_functions },
/* We agree with NIST here, so one name only */
{ "SHA3-224", "fips=yes", sha3_224_functions },
{ "SHA3-256", "fips=yes", sha3_256_functions },
{ "SHA3-384", "fips=yes", sha3_384_functions },
@@ -331,6 +359,7 @@ static const OSSL_ALGORITHM fips_digests[] = {
};
static const OSSL_ALGORITHM fips_ciphers[] = {
/* Our primary name[:ASN.1 OID name][:our older names] */
{ "AES-256-ECB", "fips=yes", aes256ecb_functions },
{ "AES-192-ECB", "fips=yes", aes192ecb_functions },
{ "AES-128-ECB", "fips=yes", aes128ecb_functions },
@@ -342,22 +371,27 @@ static const OSSL_ALGORITHM fips_ciphers[] = {
{ "AES-128-CTR", "fips=yes", aes128ctr_functions },
{ "AES-256-XTS", "fips=yes", aes256xts_functions },
{ "AES-128-XTS", "fips=yes", aes128xts_functions },
/* TODO(3.0) Add aliases for these ciphers */
{ "id-aes256-GCM", "fips=yes", aes256gcm_functions },
{ "id-aes192-GCM", "fips=yes", aes192gcm_functions },
{ "id-aes128-GCM", "fips=yes", aes128gcm_functions },
{ "id-aes256-CCM", "fips=yes", aes256ccm_functions },
{ "id-aes192-CCM", "fips=yes", aes192ccm_functions },
{ "id-aes128-CCM", "fips=yes", aes128ccm_functions },
{ "id-aes256-wrap", "fips=yes", aes256wrap_functions },
{ "id-aes192-wrap", "fips=yes", aes192wrap_functions },
{ "id-aes128-wrap", "fips=yes", aes128wrap_functions },
{ "id-aes256-wrap-pad", "fips=yes", aes256wrappad_functions },
{ "id-aes192-wrap-pad", "fips=yes", aes192wrappad_functions },
{ "id-aes128-wrap-pad", "fips=yes", aes128wrappad_functions },
{ "AES-256-GCM:id-aes256-GCM", "fips=yes", aes256gcm_functions },
{ "AES-192-GCM:id-aes192-GCM", "fips=yes", aes192gcm_functions },
{ "AES-128-GCM:id-aes128-GCM", "fips=yes", aes128gcm_functions },
{ "AES-256-CCM:id-aes256-CCM", "fips=yes", aes256ccm_functions },
{ "AES-192-CCM:id-aes192-CCM", "fips=yes", aes192ccm_functions },
{ "AES-128-CCM:id-aes128-CCM", "fips=yes", aes128ccm_functions },
{ "AES-256-WRAP:id-aes256-wrap:AES256-WRAP", "fips=yes",
aes256wrap_functions },
{ "AES-192-WRAP:id-aes192-wrap:AES192-WRAP", "fips=yes",
aes192wrap_functions },
{ "AES-128-WRAP:id-aes128-wrap:AES128-WRAP", "fips=yes",
aes128wrap_functions },
{ "AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD", "fips=yes",
aes256wrappad_functions },
{ "AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD", "fips=yes",
aes192wrappad_functions },
{ "AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD", "fips=yes",
aes128wrappad_functions },
#ifndef OPENSSL_NO_DES
{ "DES-EDE3", "fips=yes", tdes_ede3_ecb_functions },
{ "DES-EDE3-CBC", "fips=yes", tdes_ede3_cbc_functions },
{ "DES-EDE3-ECB:DES-EDE3", "fips=yes", tdes_ede3_ecb_functions },
{ "DES-EDE3-CBC:DES3", "fips=yes", tdes_ede3_cbc_functions },
#endif /* OPENSSL_NO_DES */
{ NULL, NULL, NULL }
};
@@ -378,7 +412,8 @@ static const OSSL_ALGORITHM fips_kdfs[] = {
{ OSSL_KDF_NAME_SSKDF, "fips=yes", kdf_sskdf_functions },
{ OSSL_KDF_NAME_PBKDF2, "fips=yes", kdf_pbkdf2_functions },
{ OSSL_KDF_NAME_TLS1_PRF, "fips=yes", kdf_tls1_prf_functions },
{ NULL, NULL, NULL }
{ OSSL_KDF_NAME_KBKDF, "fips=yes", kdf_kbkdf_functions },
{ NULL, NULL, NULL }
};
static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
+4 -3
View File
@@ -141,9 +141,10 @@ end:
OPENSSL_free(module_checksum);
OPENSSL_free(indicator_checksum);
(*st->bio_free_cb)(bio_indicator);
(*st->bio_free_cb)(bio_module);
if (st != NULL) {
(*st->bio_free_cb)(bio_indicator);
(*st->bio_free_cb)(bio_module);
}
FIPS_state = ok ? FIPS_STATE_RUNNING : FIPS_STATE_ERROR;
return ok;
+1 -1
View File
@@ -8,7 +8,7 @@
*/
#include <openssl/core_numbers.h>
#include <openssl/ossl_typ.h>
#include <openssl/types.h>
typedef struct self_test_post_params_st {
/* FIPS module integrity check parameters */
+1
View File
@@ -0,0 +1 @@
SUBDIRS=digests ciphers macs kdfs exchange keymgmt signature
@@ -0,0 +1,114 @@
# We make separate GOAL variables for each algorithm, to make it easy to
# switch each to the Legacy provider when needed.
#
# $TDES_1_GOAL and $TDES_2_GOAL separate FIPSable and non-FIPSable TDES.
# The latter may become legacy sooner, so it's comfortable to have two
# variables already now, to switch the non-FIPSable TDES to legacy if needed.
$AES_GOAL=../../libimplementations.a
$TDES_1_GOAL=../../libimplementations.a
$TDES_2_GOAL=../../libimplementations.a
$DES_GOAL=../../libimplementations.a
$ARIA_GOAL=../../libimplementations.a
$CAMELLIA_GOAL=../../libimplementations.a
$BLOWFISH_GOAL=../../libimplementations.a
$IDEA_GOAL=../../libimplementations.a
$CAST5_GOAL=../../libimplementations.a
$SEED_GOAL=../../libimplementations.a
$SM4_GOAL=../../libimplementations.a
$RC4_GOAL=../../libimplementations.a
$RC5_GOAL=../../libimplementations.a
$RC2_GOAL=../../libimplementations.a
$CHACHA_GOAL=../../libimplementations.a
$CHACHAPOLY_GOAL=../../libimplementations.a
IF[{- !$disabled{des} -}]
SOURCE[$TDES_1_GOAL]=cipher_tdes.c cipher_tdes_hw.c
ENDIF
SOURCE[$AES_GOAL]=\
cipher_aes.c cipher_aes_hw.c \
cipher_aes_xts.c cipher_aes_xts_hw.c \
cipher_aes_gcm.c cipher_aes_gcm_hw.c \
cipher_aes_ccm.c cipher_aes_ccm_hw.c \
cipher_aes_wrp.c
# Extra code to satisfy the FIPS and non-FIPS separation.
# When the AES-xxx-XTS moves to legacy, this can be removed.
SOURCE[../../libfips.a]=cipher_aes_xts_fips.c
SOURCE[../../libnonfips.a]=cipher_aes_xts_fips.c
IF[{- !$disabled{des} -}]
SOURCE[$TDES_2_GOAL]=\
cipher_tdes_default.c cipher_tdes_default_hw.c \
cipher_tdes_wrap.c cipher_tdes_wrap_hw.c
SOURCE[$DES_GOAL]=\
cipher_desx.c cipher_desx_hw.c \
cipher_des.c cipher_des_hw.c
ENDIF
IF[{- !$disabled{aria} -}]
SOURCE[$ARIA_GOAL]=\
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[$CAMELLIA_GOAL]=\
cipher_camellia.c cipher_camellia_hw.c
ENDIF
IF[{- !$disabled{bf} -}]
SOURCE[$BLOWFISH_GOAL]=\
cipher_blowfish.c cipher_blowfish_hw.c
ENDIF
IF[{- !$disabled{idea} -}]
SOURCE[$IDEA_GOAL]=\
cipher_idea.c cipher_idea_hw.c
ENDIF
IF[{- !$disabled{cast} -}]
SOURCE[$CAST5_GOAL]=\
cipher_cast5.c cipher_cast5_hw.c
ENDIF
IF[{- !$disabled{seed} -}]
SOURCE[$SEED_GOAL]=\
cipher_seed.c cipher_seed_hw.c
ENDIF
IF[{- !$disabled{sm4} -}]
SOURCE[$SM4_GOAL]=\
cipher_sm4.c cipher_sm4_hw.c
ENDIF
IF[{- !$disabled{ocb} -}]
SOURCE[$AES_GOAL]=\
cipher_aes_ocb.c cipher_aes_ocb_hw.c
ENDIF
IF[{- !$disabled{rc4} -}]
SOURCE[$RC4_GOAL]=\
cipher_rc4.c cipher_rc4_hw.c
ENDIF
IF[{- !$disabled{rc5} -}]
SOURCE[$RC5_GOAL]=\
cipher_rc5.c cipher_rc5_hw.c
ENDIF
IF[{- !$disabled{rc2} -}]
SOURCE[$RC2_GOAL]=\
cipher_rc2.c cipher_rc2_hw.c
ENDIF
IF[{- !$disabled{chacha} -}]
SOURCE[$CHACHA_GOAL]=\
cipher_chacha20.c cipher_chacha20_hw.c
IF[{- !$disabled{poly1305} -}]
SOURCE[$CHACHAPOLY_GOAL]=\
cipher_chacha20_poly1305.c cipher_chacha20_poly1305_hw.c
ENDIF
ENDIF
@@ -10,7 +10,7 @@
/* Dispatch functions for AES cipher modes ecb, cbc, ofb, cfb, ctr */
#include "cipher_aes.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
static OSSL_OP_cipher_freectx_fn aes_freectx;
static OSSL_OP_cipher_dupctx_fn aes_dupctx;
@@ -8,7 +8,7 @@
*/
#include <openssl/aes.h>
#include "internal/ciphers/ciphercommon.h"
#include "prov/ciphercommon.h"
typedef struct prov_aes_ctx_st {
PROV_CIPHER_CTX base; /* Must be first */
@@ -9,9 +9,9 @@
/* Dispatch functions for AES CCM mode */
#include "cipher_locl.h"
#include "internal/ciphers/cipher_ccm.h"
#include "internal/provider_algs.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_ccm.h"
#include "prov/implementations.h"
static void *aes_ccm_newctx(void *provctx, size_t keybits)
{
@@ -27,7 +27,6 @@ static void aes_ccm_freectx(void *vctx)
{
PROV_AES_CCM_CTX *ctx = (PROV_AES_CCM_CTX *)vctx;
ccm_finalctx((PROV_CCM_CTX *)ctx);
OPENSSL_clear_free(ctx, sizeof(*ctx));
}
@@ -9,8 +9,8 @@
/* AES CCM mode */
#include "cipher_locl.h"
#include "internal/ciphers/cipher_ccm.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_ccm.h"
#define AES_HW_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec) \
fn_set_enc_key(key, keylen * 8, &actx->ccm.ks.ks); \
@@ -9,9 +9,9 @@
/* Dispatch functions for AES GCM mode */
#include "cipher_locl.h"
#include "internal/ciphers/cipher_gcm.h"
#include "internal/provider_algs.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_gcm.h"
#include "prov/implementations.h"
static void *aes_gcm_newctx(void *provctx, size_t keybits)
{
@@ -27,7 +27,6 @@ static void aes_gcm_freectx(void *vctx)
{
PROV_AES_GCM_CTX *ctx = (PROV_AES_GCM_CTX *)vctx;
gcm_deinitctx((PROV_GCM_CTX *)ctx);
OPENSSL_clear_free(ctx, sizeof(*ctx));
}
@@ -9,8 +9,8 @@
/* Dispatch functions for AES GCM mode */
#include "cipher_locl.h"
#include "internal/ciphers/cipher_gcm.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_gcm.h"
static int generic_aes_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
size_t keylen)
@@ -8,7 +8,7 @@
*/
#include "cipher_aes.h"
#include "internal/providercommonerr.h"
#include "prov/providercommonerr.h"
static int cipher_hw_aes_initkey(PROV_CIPHER_CTX *dat,
const unsigned char *key, size_t keylen)
@@ -8,9 +8,9 @@
*/
#include "cipher_aes_ocb.h"
#include "internal/providercommonerr.h"
#include "internal/ciphers/cipher_aead.h"
#include "internal/provider_algs.h"
#include "prov/providercommonerr.h"
#include "prov/cipher_aead.h"
#include "prov/implementations.h"
#define AES_OCB_FLAGS AEAD_FLAGS
@@ -107,7 +107,8 @@ static int aes_ocb_init(void *vctx, const unsigned char *key, size_t keylen,
}
ctx->base.ivlen = ivlen;
}
memcpy(ctx->base.iv, iv, ivlen);
if (!cipher_generic_initiv(&ctx->base, iv, ivlen))
return 0;
ctx->iv_state = IV_STATE_BUFFERED;
}
if (key != NULL) {
@@ -285,9 +286,10 @@ 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));
if (ctx != NULL) {
aes_generic_ocb_cleanup(ctx);
OPENSSL_clear_free(ctx, sizeof(*ctx));
}
}
static void *aes_ocb_dupctx(void *vctx)
@@ -300,8 +302,10 @@ static void *aes_ocb_dupctx(void *vctx)
return NULL;
}
*ret = *in;
if (!aes_generic_ocb_copy_ctx(ret, in))
if (!aes_generic_ocb_copy_ctx(ret, in)) {
OPENSSL_free(ret);
ret = NULL;
}
return ret;
}
@@ -384,7 +388,7 @@ static int aes_ocb_get_ctx_params(void *vctx, OSSL_PARAM params[])
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)) {
if (!OSSL_PARAM_set_octet_string(p, ctx->base.oiv, ctx->base.ivlen)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
@@ -8,7 +8,7 @@
*/
#include <openssl/aes.h>
#include "internal/ciphers/ciphercommon.h"
#include "prov/ciphercommon.h"
#define OCB_MAX_TAG_LEN AES_BLOCK_SIZE
#define OCB_MAX_DATA_LEN AES_BLOCK_SIZE
@@ -37,7 +37,7 @@ static int cipher_hw_aes_ocb_generic_initkey(PROV_CIPHER_CTX *vctx,
OCB_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_set_decrypt_key,
HWAES_encrypt, HWAES_decrypt,
HWAES_ocb_encrypt, HWAES_ocb_decrypt);
}
} else
# endif
# ifdef VPAES_CAPABLE
if (VPAES_CAPABLE) {
@@ -8,17 +8,16 @@
*/
#include "cipher_aes.h"
#include "internal/providercommonerr.h"
#include "internal/provider_algs.h"
#include "prov/providercommonerr.h"
#include "prov/implementations.h"
/* AES wrap with padding has IV length of 4, without padding 8 */
#define AES_WRAP_PAD_IVLEN 4
#define AES_WRAP_NOPAD_IVLEN 8
/* TODO(3.0) Figure out what flags need to be passed */
#define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
#define WRAP_FLAGS (EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV \
| EVP_CIPH_ALWAYS_CALL_INIT)
typedef size_t (*aeswrap_fn)(void *key, const unsigned char *iv,
unsigned char *out, const unsigned char *in,
@@ -36,7 +35,6 @@ typedef struct prov_aes_wrap_ctx_st {
OSSL_UNION_ALIGN;
AES_KEY ks;
} ks;
unsigned int iv_set : 1;
aeswrap_fn wrapfn;
} PROV_AES_WRAP_CTX;
@@ -59,9 +57,7 @@ static void *aes_wrap_newctx(size_t kbits, size_t blkbits,
static void aes_wrap_freectx(void *vctx)
{
PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
OPENSSL_clear_free(wctx, sizeof(*wctx));
}
@@ -80,9 +76,8 @@ static int aes_wrap_init(void *vctx, const unsigned char *key,
wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;
if (iv != NULL) {
ctx->ivlen = ivlen;
memcpy(ctx->iv, iv, ivlen);
wctx->iv_set = 1;
if (!cipher_generic_initiv(ctx, iv, ivlen))
return 0;
}
if (key != NULL) {
if (keylen != ctx->keylen) {
@@ -150,7 +145,7 @@ static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,
}
}
rv = wctx->wrapfn(&wctx->ks.ks, wctx->iv_set ? ctx->iv : NULL, out, in,
rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,
inlen, ctx->block);
return rv ? (int)rv : -1;
}
@@ -8,23 +8,18 @@
*/
#include "cipher_aes_xts.h"
#include "internal/provider_algs.h"
#include "internal/providercommonerr.h"
#include "prov/implementations.h"
#include "prov/providercommonerr.h"
/* TODO (3.0) Figure out what flags need to be set */
#define AES_XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
#define AES_XTS_FLAGS (EVP_CIPH_CUSTOM_IV \
| EVP_CIPH_ALWAYS_CALL_INIT \
| EVP_CIPH_CTRL_INIT \
| EVP_CIPH_CUSTOM_COPY)
#define AES_XTS_IV_BITS 128
#define AES_XTS_BLOCK_BITS 8
#ifdef FIPS_MODE
static const int allow_insecure_decrypt = 0;
#else
static const int allow_insecure_decrypt = 1;
#endif /* FIPS_MODE */
/* forward declarations */
static OSSL_OP_cipher_encrypt_init_fn aes_xts_einit;
static OSSL_OP_cipher_decrypt_init_fn aes_xts_dinit;
@@ -75,12 +70,8 @@ static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
ctx->enc = enc;
if (iv != NULL) {
if (ivlen != ctx->ivlen) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
if (!cipher_generic_initiv(vctx, iv, ivlen))
return 0;
}
memcpy(ctx->iv, iv, ivlen);
xctx->iv_set = 1;
}
if (key != NULL) {
if (keylen != ctx->keylen) {
@@ -154,7 +145,7 @@ static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
if (ctx->xts.key1 == NULL
|| ctx->xts.key2 == NULL
|| !ctx->iv_set
|| !ctx->base.iv_set
|| out == NULL
|| in == NULL
|| inl < AES_BLOCK_SIZE)
@@ -8,7 +8,13 @@
*/
#include <openssl/aes.h>
#include "internal/ciphers/ciphercommon.h"
#include "prov/ciphercommon.h"
/*
* Available in cipher_fips.c, and compiled with different values depending
* on we're in the FIPS module or not.
*/
extern const int allow_insecure_decrypt;
PROV_CIPHER_FUNC(void, xts_stream,
(const unsigned char *in, unsigned char *out, size_t len,
@@ -23,7 +29,6 @@ typedef struct prov_aes_xts_ctx_st {
} ks1, ks2; /* AES key schedules to use */
XTS128_CONTEXT xts;
OSSL_xts_stream_fn stream;
unsigned int iv_set : 1; /* Set if an iv is set */
} PROV_AES_XTS_CTX;
const PROV_CIPHER_HW *PROV_CIPHER_HW_aes_xts(size_t keybits);
@@ -0,0 +1,16 @@
/*
* 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_xts.h"
#ifdef FIPS_MODE
const int allow_insecure_decrypt = 0;
#else
const int allow_insecure_decrypt = 1;
#endif /* FIPS_MODE */
@@ -10,7 +10,7 @@
/* Dispatch functions for ARIA cipher modes ecb, cbc, ofb, cfb, ctr */
#include "cipher_aria.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
static OSSL_OP_cipher_freectx_fn aria_freectx;
static OSSL_OP_cipher_dupctx_fn aria_dupctx;
@@ -7,8 +7,8 @@
* https://www.openssl.org/source/license.html
*/
#include "internal/aria.h"
#include "internal/ciphers/ciphercommon.h"
#include "crypto/aria.h"
#include "prov/ciphercommon.h"
typedef struct prov_aria_ctx_st {
PROV_CIPHER_CTX base; /* Must be first */
@@ -10,7 +10,7 @@
/* Dispatch functions for ARIA CCM mode */
#include "cipher_aria_ccm.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
static OSSL_OP_cipher_freectx_fn aria_ccm_freectx;
@@ -27,7 +27,6 @@ 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));
}
@@ -7,9 +7,9 @@
* https://www.openssl.org/source/license.html
*/
#include "internal/aria.h"
#include "internal/ciphers/ciphercommon.h"
#include "internal/ciphers/cipher_ccm.h"
#include "crypto/aria.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_ccm.h"
typedef struct prov_aria_ccm_ctx_st {
PROV_CCM_CTX base; /* Must be first */
@@ -10,7 +10,7 @@
/* Dispatch functions for ARIA GCM mode */
#include "cipher_aria_gcm.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
static void *aria_gcm_newctx(void *provctx, size_t keybits)
{
@@ -26,7 +26,6 @@ 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));
}
@@ -7,9 +7,9 @@
* https://www.openssl.org/source/license.html
*/
#include "internal/aria.h"
#include "internal/ciphers/ciphercommon.h"
#include "internal/ciphers/cipher_gcm.h"
#include "crypto/aria.h"
#include "prov/ciphercommon.h"
#include "prov/cipher_gcm.h"
typedef struct prov_aria_gcm_ctx_st {
PROV_GCM_CTX base; /* must be first entry in struct */
@@ -10,7 +10,9 @@
/* Dispatch functions for Blowfish cipher modes ecb, cbc, ofb, cfb */
#include "cipher_blowfish.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
#define BF_FLAGS (EVP_CIPH_VARIABLE_LENGTH)
static OSSL_OP_cipher_freectx_fn blowfish_freectx;
static OSSL_OP_cipher_dupctx_fn blowfish_dupctx;
@@ -37,10 +39,10 @@ static void *blowfish_dupctx(void *ctx)
}
/* bf_ecb_functions */
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, ecb, ECB, BF_FLAGS, 128, 64, 0, block)
/* bf_cbc_functions */
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, cbc, CBC, BF_FLAGS, 128, 64, 64, block)
/* bf_ofb_functions */
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, ofb64, OFB, BF_FLAGS, 64, 8, 64, stream)
/* bf_cfb_functions */
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, cfb64, CFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
IMPLEMENT_generic_cipher(blowfish, BLOWFISH, cfb64, CFB, BF_FLAGS, 64, 8, 64, stream)
@@ -8,7 +8,7 @@
*/
#include <openssl/blowfish.h>
#include "internal/ciphers/ciphercommon.h"
#include "prov/ciphercommon.h"
typedef struct prov_blowfish_ctx_st {
PROV_CIPHER_CTX base; /* Must be first */
@@ -10,7 +10,7 @@
/* Dispatch functions for CAMELLIA cipher modes ecb, cbc, ofb, cfb, ctr */
#include "cipher_camellia.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
static OSSL_OP_cipher_freectx_fn camellia_freectx;
static OSSL_OP_cipher_dupctx_fn camellia_dupctx;
@@ -8,7 +8,7 @@
*/
#include "openssl/camellia.h"
#include "internal/ciphers/ciphercommon.h"
#include "prov/ciphercommon.h"
typedef struct prov_camellia_ctx_st {
PROV_CIPHER_CTX base; /* Must be first */
@@ -8,7 +8,7 @@
*/
#include <openssl/cast.h>
#include "internal/ciphers/ciphercommon.h"
#include "prov/ciphercommon.h"
typedef struct prov_cast_ctx_st {
PROV_CIPHER_CTX base; /* Must be first */
@@ -10,7 +10,9 @@
/* Dispatch functions for cast cipher modes ecb, cbc, ofb, cfb */
#include "cipher_cast.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
#define CAST5_FLAGS (EVP_CIPH_VARIABLE_LENGTH)
static OSSL_OP_cipher_freectx_fn cast5_freectx;
static OSSL_OP_cipher_dupctx_fn cast5_dupctx;
@@ -37,10 +39,10 @@ static void *cast5_dupctx(void *ctx)
}
/* cast5128ecb_functions */
IMPLEMENT_generic_cipher(cast5, CAST, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
IMPLEMENT_generic_cipher(cast5, CAST, ecb, ECB, CAST5_FLAGS, 128, 64, 0, block)
/* cast5128cbc_functions */
IMPLEMENT_generic_cipher(cast5, CAST, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
IMPLEMENT_generic_cipher(cast5, CAST, cbc, CBC, CAST5_FLAGS, 128, 64, 64, block)
/* cast564ofb64_functions */
IMPLEMENT_generic_cipher(cast5, CAST, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
IMPLEMENT_generic_cipher(cast5, CAST, ofb64, OFB, CAST5_FLAGS, 64, 8, 64, stream)
/* cast564cfb64_functions */
IMPLEMENT_generic_cipher(cast5, CAST, cfb64, CFB, EVP_CIPH_VARIABLE_LENGTH, 64, 8, 64, stream)
IMPLEMENT_generic_cipher(cast5, CAST, cfb64, CFB, CAST5_FLAGS, 64, 8, 64, stream)
@@ -0,0 +1,187 @@
/*
* 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 chacha20 cipher */
#include "cipher_chacha20.h"
#include "prov/implementations.h"
#include "prov/providercommonerr.h"
#define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
#define CHACHA20_BLKLEN (1)
#define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
/* TODO(3.0) Figure out what flags are required */
#define CHACHA20_FLAGS (EVP_CIPH_CUSTOM_IV | EVP_CIPH_ALWAYS_CALL_INIT)
static OSSL_OP_cipher_newctx_fn chacha20_newctx;
static OSSL_OP_cipher_freectx_fn chacha20_freectx;
static OSSL_OP_cipher_get_params_fn chacha20_get_params;
static OSSL_OP_cipher_get_ctx_params_fn chacha20_get_ctx_params;
static OSSL_OP_cipher_set_ctx_params_fn chacha20_set_ctx_params;
static OSSL_OP_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params;
static OSSL_OP_cipher_settable_ctx_params_fn chacha20_settable_ctx_params;
#define chacha20_cipher cipher_generic_cipher
#define chacha20_update cipher_generic_stream_update
#define chacha20_final cipher_generic_stream_final
#define chacha20_gettable_params cipher_generic_gettable_params
void chacha20_initctx(PROV_CHACHA20_CTX *ctx)
{
cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
CHACHA20_BLKLEN * 8,
CHACHA20_IVLEN * 8,
0, CHACHA20_FLAGS,
PROV_CIPHER_HW_chacha20(CHACHA20_KEYLEN * 8),
NULL);
}
static void *chacha20_newctx(void *provctx)
{
PROV_CHACHA20_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
if (ctx != NULL)
chacha20_initctx(ctx);
return ctx;
}
static void chacha20_freectx(void *vctx)
{
PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
if (ctx != NULL) {
OPENSSL_clear_free(ctx, sizeof(ctx));
}
}
static int chacha20_get_params(OSSL_PARAM params[])
{
return cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
CHACHA20_KEYLEN * 8,
CHACHA20_BLKLEN * 8,
CHACHA20_IVLEN * 8);
}
static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
{
OSSL_PARAM *p;
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_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, CHACHA20_KEYLEN)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
return 1;
}
static const OSSL_PARAM chacha20_known_gettable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
OSSL_PARAM_END
};
const OSSL_PARAM *chacha20_gettable_ctx_params(void)
{
return chacha20_known_gettable_ctx_params;
}
static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
{
const OSSL_PARAM *p;
size_t len;
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
if (p != NULL) {
if (!OSSL_PARAM_get_size_t(p, &len)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
if (len != CHACHA20_KEYLEN) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
return 0;
}
}
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
if (p != NULL) {
if (!OSSL_PARAM_get_size_t(p, &len)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
if (len != CHACHA20_IVLEN) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
return 0;
}
}
return 1;
}
static const OSSL_PARAM chacha20_known_settable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
OSSL_PARAM_END
};
const OSSL_PARAM *chacha20_settable_ctx_params(void)
{
return chacha20_known_settable_ctx_params;
}
int chacha20_einit(void *vctx, const unsigned char *key, size_t keylen,
const unsigned char *iv, size_t ivlen)
{
int ret;
ret= cipher_generic_einit(vctx, key, keylen, iv, ivlen);
if (ret && iv != NULL) {
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
hw->initiv(ctx);
}
return ret;
}
int chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
const unsigned char *iv, size_t ivlen)
{
int ret;
ret= cipher_generic_dinit(vctx, key, keylen, iv, ivlen);
if (ret && iv != NULL) {
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
hw->initiv(ctx);
}
return ret;
}
/* chacha20_functions */
const OSSL_DISPATCH chacha20_functions[] = {
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_einit },
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_dinit },
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final },
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher},
{ OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params },
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS,(void (*)(void))chacha20_gettable_params },
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params },
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
(void (*)(void))chacha20_gettable_ctx_params },
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params },
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
(void (*)(void))chacha20_settable_ctx_params },
{ 0, NULL }
};
@@ -0,0 +1,34 @@
/*
* 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 "include/crypto/chacha.h"
#include "prov/ciphercommon.h"
typedef struct {
PROV_CIPHER_CTX base; /* must be first */
union {
OSSL_UNION_ALIGN;
unsigned int d[CHACHA_KEY_SIZE / 4];
} key;
unsigned int counter[CHACHA_CTR_SIZE / 4];
unsigned char buf[CHACHA_BLK_SIZE];
unsigned int partial_len;
} PROV_CHACHA20_CTX;
typedef struct prov_cipher_hw_chacha20_st {
PROV_CIPHER_HW base; /* must be first */
int (*initiv)(PROV_CIPHER_CTX *ctx);
} PROV_CIPHER_HW_CHACHA20;
const PROV_CIPHER_HW *PROV_CIPHER_HW_chacha20(size_t keybits);
OSSL_OP_cipher_encrypt_init_fn chacha20_einit;
OSSL_OP_cipher_decrypt_init_fn chacha20_dinit;
void chacha20_initctx(PROV_CHACHA20_CTX *ctx);
@@ -0,0 +1,121 @@
/*
* 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
*/
/* chacha20 cipher implementation */
#include "cipher_chacha20.h"
static int chacha20_initkey(PROV_CIPHER_CTX *bctx, const uint8_t *key,
size_t keylen)
{
PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)bctx;
unsigned int i;
if (key != NULL) {
for (i = 0; i < CHACHA_KEY_SIZE; i += 4)
ctx->key.d[i / 4] = CHACHA_U8TOU32(key + i);
}
ctx->partial_len = 0;
return 1;
}
static int chacha20_initiv(PROV_CIPHER_CTX *bctx)
{
PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)bctx;
unsigned int i;
if (bctx->iv_set) {
for (i = 0; i < CHACHA_CTR_SIZE; i += 4)
ctx->counter[i / 4] = CHACHA_U8TOU32(bctx->oiv + i);
}
return 1;
}
static int chacha20_cipher(PROV_CIPHER_CTX *bctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)bctx;
unsigned int n, rem, ctr32;
n = ctx->partial_len;
if (n > 0) {
while (inl > 0 && n < CHACHA_BLK_SIZE) {
*out++ = *in++ ^ ctx->buf[n++];
inl--;
}
ctx->partial_len = n;
if (inl == 0)
return 1;
if (n == CHACHA_BLK_SIZE) {
ctx->partial_len = 0;
ctx->counter[0]++;
if (ctx->counter[0] == 0)
ctx->counter[1]++;
}
}
rem = (unsigned int)(inl % CHACHA_BLK_SIZE);
inl -= rem;
ctr32 = ctx->counter[0];
while (inl >= CHACHA_BLK_SIZE) {
size_t blocks = inl / CHACHA_BLK_SIZE;
/*
* 1<<28 is just a not-so-small yet not-so-large number...
* Below condition is practically never met, but it has to
* be checked for code correctness.
*/
if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
blocks = (1U << 28);
/*
* As ChaCha20_ctr32 operates on 32-bit counter, caller
* has to handle overflow. 'if' below detects the
* overflow, which is then handled by limiting the
* amount of blocks to the exact overflow point...
*/
ctr32 += (unsigned int)blocks;
if (ctr32 < blocks) {
blocks -= ctr32;
ctr32 = 0;
}
blocks *= CHACHA_BLK_SIZE;
ChaCha20_ctr32(out, in, blocks, ctx->key.d, ctx->counter);
inl -= blocks;
in += blocks;
out += blocks;
ctx->counter[0] = ctr32;
if (ctr32 == 0) ctx->counter[1]++;
}
if (rem > 0) {
memset(ctx->buf, 0, sizeof(ctx->buf));
ChaCha20_ctr32(ctx->buf, ctx->buf, CHACHA_BLK_SIZE,
ctx->key.d, ctx->counter);
for (n = 0; n < rem; n++)
out[n] = in[n] ^ ctx->buf[n];
ctx->partial_len = rem;
}
return 1;
}
static const PROV_CIPHER_HW_CHACHA20 chacha20_hw = {
{ chacha20_initkey, chacha20_cipher },
chacha20_initiv
};
const PROV_CIPHER_HW *PROV_CIPHER_HW_chacha20(size_t keybits)
{
return (PROV_CIPHER_HW *)&chacha20_hw;
}
@@ -0,0 +1,314 @@
/*
* 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 chacha20_poly1305 cipher */
#include "cipher_chacha20_poly1305.h"
#include "prov/implementations.h"
#include "prov/providercommonerr.h"
#define CHACHA20_POLY1305_KEYLEN CHACHA_KEY_SIZE
#define CHACHA20_POLY1305_BLKLEN 1
#define CHACHA20_POLY1305_MAX_IVLEN 12
#define CHACHA20_POLY1305_MODE 0
/* TODO(3.0) Figure out what flags are required */
#define CHACHA20_POLY1305_FLAGS (EVP_CIPH_FLAG_AEAD_CIPHER \
| EVP_CIPH_ALWAYS_CALL_INIT \
| EVP_CIPH_CTRL_INIT \
| EVP_CIPH_CUSTOM_COPY \
| EVP_CIPH_FLAG_CUSTOM_CIPHER \
| EVP_CIPH_CUSTOM_IV \
| EVP_CIPH_CUSTOM_IV_LENGTH)
static OSSL_OP_cipher_newctx_fn chacha20_poly1305_newctx;
static OSSL_OP_cipher_freectx_fn chacha20_poly1305_freectx;
static OSSL_OP_cipher_encrypt_init_fn chacha20_poly1305_einit;
static OSSL_OP_cipher_decrypt_init_fn chacha20_poly1305_dinit;
static OSSL_OP_cipher_get_params_fn chacha20_poly1305_get_params;
static OSSL_OP_cipher_get_ctx_params_fn chacha20_poly1305_get_ctx_params;
static OSSL_OP_cipher_set_ctx_params_fn chacha20_poly1305_set_ctx_params;
static OSSL_OP_cipher_cipher_fn chacha20_poly1305_cipher;
static OSSL_OP_cipher_final_fn chacha20_poly1305_final;
static OSSL_OP_cipher_gettable_ctx_params_fn chacha20_poly1305_gettable_ctx_params;
#define chacha20_poly1305_settable_ctx_params cipher_aead_settable_ctx_params
#define chacha20_poly1305_gettable_params cipher_generic_gettable_params
#define chacha20_poly1305_update chacha20_poly1305_cipher
static void *chacha20_poly1305_newctx(void *provctx)
{
PROV_CHACHA20_POLY1305_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
if (ctx != NULL) {
cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8,
CHACHA20_POLY1305_BLKLEN * 8,
CHACHA20_POLY1305_IVLEN * 8,
CHACHA20_POLY1305_MODE,
CHACHA20_POLY1305_FLAGS,
PROV_CIPHER_HW_chacha20_poly1305(
CHACHA20_POLY1305_KEYLEN * 8),
NULL);
ctx->nonce_len = CHACHA20_POLY1305_IVLEN;
ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
chacha20_initctx(&ctx->chacha);
}
return ctx;
}
static void chacha20_poly1305_freectx(void *vctx)
{
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
if (ctx != NULL)
OPENSSL_clear_free(ctx, sizeof(ctx));
}
static int chacha20_poly1305_get_params(OSSL_PARAM params[])
{
return cipher_generic_get_params(params, 0, CHACHA20_POLY1305_FLAGS,
CHACHA20_POLY1305_KEYLEN * 8,
CHACHA20_POLY1305_BLKLEN * 8,
CHACHA20_POLY1305_IVLEN * 8);
}
static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[])
{
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
OSSL_PARAM *p;
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
if (p != NULL) {
if (!OSSL_PARAM_set_size_t(p, ctx->nonce_len)) {
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, CHACHA20_POLY1305_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 && !OSSL_PARAM_set_size_t(p, ctx->tag_len)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
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_SET_PARAMETER);
return 0;
}
if (!ctx->base.enc) {
ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOTSET);
return 0;
}
if (p->data_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAGLEN);
return 0;
}
memcpy(p->data, ctx->tag, p->data_size);
}
return 1;
}
static const OSSL_PARAM chacha20_poly1305_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_AEAD_TAG, NULL, 0),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
OSSL_PARAM_END
};
static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params(void)
{
return chacha20_poly1305_known_gettable_ctx_params;
}
static int chacha20_poly1305_set_ctx_params(void *vctx,
const OSSL_PARAM params[])
{
const OSSL_PARAM *p;
size_t len;
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
(PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
if (p != NULL) {
if (!OSSL_PARAM_get_size_t(p, &len)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
if (len != CHACHA20_POLY1305_KEYLEN) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
return 0;
}
}
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
if (p != NULL) {
if (!OSSL_PARAM_get_size_t(p, &len)) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
if (len == 0 || len > CHACHA20_POLY1305_MAX_IVLEN) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
return 0;
}
ctx->nonce_len = len;
}
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_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAGLEN);
return 0;
}
if (p->data != NULL) {
if (ctx->base.enc) {
ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
return 0;
}
memcpy(ctx->tag, p->data, p->data_size);
}
ctx->tag_len = p->data_size;
}
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
len = hw->tls_init(&ctx->base, p->data, p->data_size);
if (len == 0) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
return 0;
}
ctx->tls_aad_pad_sz = len;
}
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
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 (hw->tls_iv_set_fixed(&ctx->base, p->data, p->data_size) == 0) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
return 0;
}
}
/* ignore OSSL_CIPHER_PARAM_AEAD_MAC_KEY */
return 1;
}
static int chacha20_poly1305_einit(void *vctx, const unsigned char *key,
size_t keylen, const unsigned char *iv,
size_t ivlen)
{
int ret;
ret = cipher_generic_einit(vctx, key, keylen, iv, ivlen);
if (ret && iv != NULL) {
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
(PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
hw->initiv(ctx);
}
return ret;
}
static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key,
size_t keylen, const unsigned char *iv,
size_t ivlen)
{
int ret;
ret = cipher_generic_dinit(vctx, key, keylen, iv, ivlen);
if (ret && iv != NULL) {
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
(PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
hw->initiv(ctx);
}
return ret;
}
static int chacha20_poly1305_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;
PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
(PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
if (outsize < inl) {
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
return 0;
}
if (!hw->aead_cipher(ctx, out, outl, in, inl))
return 0;
*outl = inl;
return 1;
}
static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl,
size_t outsize)
{
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
(PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
if (hw->aead_cipher(ctx, out, outl, NULL, 0) <= 0)
return 0;
*outl = 0;
return 1;
}
/* chacha20_poly1305_functions */
const OSSL_DISPATCH chacha20_poly1305_functions[] = {
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx },
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx },
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit },
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit },
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update },
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final },
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher },
{ OSSL_FUNC_CIPHER_GET_PARAMS,
(void (*)(void))chacha20_poly1305_get_params },
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
(void (*)(void))chacha20_poly1305_gettable_params },
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
(void (*)(void))chacha20_poly1305_get_ctx_params },
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
(void (*)(void))chacha20_poly1305_gettable_ctx_params },
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
(void (*)(void))chacha20_poly1305_set_ctx_params },
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
(void (*)(void))chacha20_poly1305_settable_ctx_params },
{ 0, NULL }
};
@@ -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
*/
/* Dispatch functions for chacha20_poly1305 cipher */
#include "include/crypto/poly1305.h"
#include "cipher_chacha20.h"
#define NO_TLS_PAYLOAD_LENGTH ((size_t)-1)
#define CHACHA20_POLY1305_IVLEN 12
typedef struct {
PROV_CIPHER_CTX base; /* must be first */
PROV_CHACHA20_CTX chacha;
POLY1305 poly1305;
unsigned int nonce[12 / 4];
unsigned char tag[POLY1305_BLOCK_SIZE];
unsigned char tls_aad[POLY1305_BLOCK_SIZE];
struct { uint64_t aad, text; } len;
unsigned int aad : 1;
unsigned int mac_inited : 1;
size_t tag_len, nonce_len;
size_t tls_payload_length;
size_t tls_aad_pad_sz;
} PROV_CHACHA20_POLY1305_CTX;
typedef struct prov_cipher_hw_chacha_aead_st {
PROV_CIPHER_HW base; /* must be first */
int (*aead_cipher)(PROV_CIPHER_CTX *dat, unsigned char *out, size_t *outl,
const unsigned char *in, size_t len);
int (*initiv)(PROV_CIPHER_CTX *ctx);
int (*tls_init)(PROV_CIPHER_CTX *ctx, unsigned char *aad, size_t alen);
int (*tls_iv_set_fixed)(PROV_CIPHER_CTX *ctx, unsigned char *fixed,
size_t flen);
} PROV_CIPHER_HW_CHACHA20_POLY1305;
const PROV_CIPHER_HW *PROV_CIPHER_HW_chacha20_poly1305(size_t keybits);
@@ -0,0 +1,408 @@
/*
* 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
*/
/* chacha20_poly1305 cipher implementation */
#include "cipher_chacha20_poly1305.h"
static int chacha_poly1305_tls_init(PROV_CIPHER_CTX *bctx,
unsigned char *aad, size_t alen)
{
unsigned int len;
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
if (alen != EVP_AEAD_TLS1_AAD_LEN)
return 0;
memcpy(ctx->tls_aad, aad, EVP_AEAD_TLS1_AAD_LEN);
len = aad[EVP_AEAD_TLS1_AAD_LEN - 2] << 8 | aad[EVP_AEAD_TLS1_AAD_LEN - 1];
aad = ctx->tls_aad;
if (!bctx->enc) {
if (len < POLY1305_BLOCK_SIZE)
return 0;
len -= POLY1305_BLOCK_SIZE; /* discount attached tag */
aad[EVP_AEAD_TLS1_AAD_LEN - 2] = (unsigned char)(len >> 8);
aad[EVP_AEAD_TLS1_AAD_LEN - 1] = (unsigned char)len;
}
ctx->tls_payload_length = len;
/* merge record sequence number as per RFC7905 */
ctx->chacha.counter[1] = ctx->nonce[0];
ctx->chacha.counter[2] = ctx->nonce[1] ^ CHACHA_U8TOU32(aad);
ctx->chacha.counter[3] = ctx->nonce[2] ^ CHACHA_U8TOU32(aad+4);
ctx->mac_inited = 0;
return POLY1305_BLOCK_SIZE; /* tag length */
}
static int chacha_poly1305_tls_iv_set_fixed(PROV_CIPHER_CTX *bctx,
unsigned char *fixed, size_t flen)
{
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
if (flen != CHACHA20_POLY1305_IVLEN)
return 0;
ctx->nonce[0] = ctx->chacha.counter[1] = CHACHA_U8TOU32(fixed);
ctx->nonce[1] = ctx->chacha.counter[2] = CHACHA_U8TOU32(fixed + 4);
ctx->nonce[2] = ctx->chacha.counter[3] = CHACHA_U8TOU32(fixed + 8);
return 1;
}
static int chacha20_poly1305_initkey(PROV_CIPHER_CTX *bctx,
const unsigned char *key, size_t keylen)
{
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
ctx->len.aad = 0;
ctx->len.text = 0;
ctx->aad = 0;
ctx->mac_inited = 0;
ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
if (bctx->enc)
return chacha20_einit(&ctx->chacha, key, keylen, NULL, 0);
else
return chacha20_dinit(&ctx->chacha, key, keylen, NULL, 0);
}
static int chacha20_poly1305_initiv(PROV_CIPHER_CTX *bctx)
{
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
unsigned char tempiv[CHACHA_CTR_SIZE] = { 0 };
int ret = 1;
/* pad on the left */
if (ctx->nonce_len <= CHACHA_CTR_SIZE) {
memcpy(tempiv + CHACHA_CTR_SIZE - ctx->nonce_len, bctx->oiv,
ctx->nonce_len);
if (bctx->enc)
ret = chacha20_einit(&ctx->chacha, NULL, 0, tempiv, sizeof(tempiv));
else
ret = chacha20_dinit(&ctx->chacha, NULL, 0, tempiv, sizeof(tempiv));
ctx->nonce[0] = ctx->chacha.counter[1];
ctx->nonce[1] = ctx->chacha.counter[2];
ctx->nonce[2] = ctx->chacha.counter[3];
bctx->iv_set = 1;
}
return ret;
}
#if !defined(OPENSSL_SMALL_FOOTPRINT)
# if defined(POLY1305_ASM) && (defined(__x86_64) || defined(__x86_64__) \
|| defined(_M_AMD64) || defined(_M_X64))
# define XOR128_HELPERS
void *xor128_encrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
void *xor128_decrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
static const unsigned char zero[4 * CHACHA_BLK_SIZE] = { 0 };
# else
static const unsigned char zero[2 * CHACHA_BLK_SIZE] = { 0 };
# endif
static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
unsigned char *out,
size_t *out_padlen,
const unsigned char *in, size_t len)
{
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
POLY1305 *poly = &ctx->poly1305;
size_t tail, tohash_len, buf_len, plen = ctx->tls_payload_length;
unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
const union {
long one;
char little;
} is_endian = { 1 };
if (len != plen + POLY1305_BLOCK_SIZE)
return 0;
buf = storage + ((0 - (size_t)storage) & 15); /* align */
ctr = buf + CHACHA_BLK_SIZE;
tohash = buf + CHACHA_BLK_SIZE - POLY1305_BLOCK_SIZE;
# ifdef XOR128_HELPERS
if (plen <= 3 * CHACHA_BLK_SIZE) {
ctx->chacha.counter[0] = 0;
buf_len = (plen + 2 * CHACHA_BLK_SIZE - 1) & (0 - CHACHA_BLK_SIZE);
ChaCha20_ctr32(buf, zero, buf_len, ctx->chacha.key.d, ctx->chacha.counter);
Poly1305_Init(poly, buf);
ctx->chacha.partial_len = 0;
memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
tohash_len = POLY1305_BLOCK_SIZE;
ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
ctx->len.text = plen;
if (plen) {
if (ctx->enc)
ctr = xor128_encrypt_n_pad(out, in, ctr, plen);
else
ctr = xor128_decrypt_n_pad(out, in, ctr, plen);
in += plen;
out += plen;
tohash_len = (size_t)(ctr - tohash);
}
}
# else
if (plen <= CHACHA_BLK_SIZE) {
size_t i;
ctx->chacha.counter[0] = 0;
ChaCha20_ctr32(buf, zero, (buf_len = 2 * CHACHA_BLK_SIZE),
ctx->chacha.key.d, ctx->chacha.counter);
Poly1305_Init(poly, buf);
ctx->chacha.partial_len = 0;
memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
tohash_len = POLY1305_BLOCK_SIZE;
ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
ctx->len.text = plen;
if (bctx->enc) {
for (i = 0; i < plen; i++)
out[i] = ctr[i] ^= in[i];
} else {
for (i = 0; i < plen; i++) {
unsigned char c = in[i];
out[i] = ctr[i] ^ c;
ctr[i] = c;
}
}
in += i;
out += i;
tail = (0 - i) & (POLY1305_BLOCK_SIZE - 1);
memset(ctr + i, 0, tail);
ctr += i + tail;
tohash_len += i + tail;
}
# endif
else {
ctx->chacha.counter[0] = 0;
ChaCha20_ctr32(buf, zero, (buf_len = CHACHA_BLK_SIZE),
ctx->chacha.key.d, ctx->chacha.counter);
Poly1305_Init(poly, buf);
ctx->chacha.counter[0] = 1;
ctx->chacha.partial_len = 0;
Poly1305_Update(poly, ctx->tls_aad, POLY1305_BLOCK_SIZE);
tohash = ctr;
tohash_len = 0;
ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
ctx->len.text = plen;
if (bctx->enc) {
ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
Poly1305_Update(poly, out, plen);
} else {
Poly1305_Update(poly, in, plen);
ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
}
in += plen;
out += plen;
tail = (0 - plen) & (POLY1305_BLOCK_SIZE - 1);
Poly1305_Update(poly, zero, tail);
}
if (is_endian.little) {
memcpy(ctr, (unsigned char *)&ctx->len, POLY1305_BLOCK_SIZE);
} else {
ctr[0] = (unsigned char)(ctx->len.aad);
ctr[1] = (unsigned char)(ctx->len.aad>>8);
ctr[2] = (unsigned char)(ctx->len.aad>>16);
ctr[3] = (unsigned char)(ctx->len.aad>>24);
ctr[4] = (unsigned char)(ctx->len.aad>>32);
ctr[5] = (unsigned char)(ctx->len.aad>>40);
ctr[6] = (unsigned char)(ctx->len.aad>>48);
ctr[7] = (unsigned char)(ctx->len.aad>>56);
ctr[8] = (unsigned char)(ctx->len.text);
ctr[9] = (unsigned char)(ctx->len.text>>8);
ctr[10] = (unsigned char)(ctx->len.text>>16);
ctr[11] = (unsigned char)(ctx->len.text>>24);
ctr[12] = (unsigned char)(ctx->len.text>>32);
ctr[13] = (unsigned char)(ctx->len.text>>40);
ctr[14] = (unsigned char)(ctx->len.text>>48);
ctr[15] = (unsigned char)(ctx->len.text>>56);
}
tohash_len += POLY1305_BLOCK_SIZE;
Poly1305_Update(poly, tohash, tohash_len);
OPENSSL_cleanse(buf, buf_len);
Poly1305_Final(poly, bctx->enc ? ctx->tag : tohash);
ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
if (bctx->enc) {
memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
} else {
if (CRYPTO_memcmp(tohash, in, POLY1305_BLOCK_SIZE)) {
if (len > POLY1305_BLOCK_SIZE)
memset(out - (len - POLY1305_BLOCK_SIZE), 0,
len - POLY1305_BLOCK_SIZE);
return 0;
}
}
*out_padlen = len;
return 1;
}
#else
static const unsigned char zero[CHACHA_BLK_SIZE] = { 0 };
#endif /* OPENSSL_SMALL_FOOTPRINT */
static int chacha20_poly1305_aead_cipher(PROV_CIPHER_CTX *bctx,
unsigned char *out, size_t *outl,
const unsigned char *in, size_t inl)
{
PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
POLY1305 *poly = &ctx->poly1305;
size_t rem, plen = ctx->tls_payload_length;
size_t olen = 0;
int rv = 0;
const union {
long one;
char little;
} is_endian = { 1 };
if (!ctx->mac_inited) {
#if !defined(OPENSSL_SMALL_FOOTPRINT)
if (plen != NO_TLS_PAYLOAD_LENGTH && out != NULL) {
return chacha20_poly1305_tls_cipher(bctx, out, outl, in, inl);
}
#endif
ctx->chacha.counter[0] = 0;
ChaCha20_ctr32(ctx->chacha.buf, zero, CHACHA_BLK_SIZE,
ctx->chacha.key.d, ctx->chacha.counter);
Poly1305_Init(poly, ctx->chacha.buf);
ctx->chacha.counter[0] = 1;
ctx->chacha.partial_len = 0;
ctx->len.aad = ctx->len.text = 0;
ctx->mac_inited = 1;
if (plen != NO_TLS_PAYLOAD_LENGTH) {
Poly1305_Update(poly, ctx->tls_aad, EVP_AEAD_TLS1_AAD_LEN);
ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
ctx->aad = 1;
}
}
if (in != NULL) { /* aad or text */
if (out == NULL) { /* aad */
Poly1305_Update(poly, in, inl);
ctx->len.aad += inl;
ctx->aad = 1;
goto finish;
} else { /* plain- or ciphertext */
if (ctx->aad) { /* wrap up aad */
if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
ctx->aad = 0;
}
ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
if (plen == NO_TLS_PAYLOAD_LENGTH)
plen = inl;
else if (inl != plen + POLY1305_BLOCK_SIZE)
goto err;
if (bctx->enc) { /* plaintext */
ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
Poly1305_Update(poly, out, plen);
in += plen;
out += plen;
ctx->len.text += plen;
} else { /* ciphertext */
Poly1305_Update(poly, in, plen);
ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
in += plen;
out += plen;
ctx->len.text += plen;
}
}
}
/* explicit final, or tls mode */
if (in == NULL || inl != plen) {
unsigned char temp[POLY1305_BLOCK_SIZE];
if (ctx->aad) { /* wrap up aad */
if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
ctx->aad = 0;
}
if ((rem = (size_t)ctx->len.text % POLY1305_BLOCK_SIZE))
Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
if (is_endian.little) {
Poly1305_Update(poly, (unsigned char *)&ctx->len,
POLY1305_BLOCK_SIZE);
} else {
temp[0] = (unsigned char)(ctx->len.aad);
temp[1] = (unsigned char)(ctx->len.aad>>8);
temp[2] = (unsigned char)(ctx->len.aad>>16);
temp[3] = (unsigned char)(ctx->len.aad>>24);
temp[4] = (unsigned char)(ctx->len.aad>>32);
temp[5] = (unsigned char)(ctx->len.aad>>40);
temp[6] = (unsigned char)(ctx->len.aad>>48);
temp[7] = (unsigned char)(ctx->len.aad>>56);
temp[8] = (unsigned char)(ctx->len.text);
temp[9] = (unsigned char)(ctx->len.text>>8);
temp[10] = (unsigned char)(ctx->len.text>>16);
temp[11] = (unsigned char)(ctx->len.text>>24);
temp[12] = (unsigned char)(ctx->len.text>>32);
temp[13] = (unsigned char)(ctx->len.text>>40);
temp[14] = (unsigned char)(ctx->len.text>>48);
temp[15] = (unsigned char)(ctx->len.text>>56);
Poly1305_Update(poly, temp, POLY1305_BLOCK_SIZE);
}
Poly1305_Final(poly, bctx->enc ? ctx->tag : temp);
ctx->mac_inited = 0;
if (in != NULL && inl != plen) {
if (bctx->enc) {
memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
} else {
if (CRYPTO_memcmp(temp, in, POLY1305_BLOCK_SIZE)) {
memset(out - plen, 0, plen);
goto err;
}
}
}
else if (!bctx->enc) {
if (CRYPTO_memcmp(temp, ctx->tag, ctx->tag_len))
goto err;
}
}
finish:
olen = inl;
rv = 1;
err:
*outl = olen;
return rv;
}
static const PROV_CIPHER_HW_CHACHA20_POLY1305 chacha20poly1305_hw =
{
{ chacha20_poly1305_initkey, NULL },
chacha20_poly1305_aead_cipher,
chacha20_poly1305_initiv,
chacha_poly1305_tls_init,
chacha_poly1305_tls_iv_set_fixed
};
const PROV_CIPHER_HW *PROV_CIPHER_HW_chacha20_poly1305(size_t keybits)
{
return (PROV_CIPHER_HW *)&chacha20poly1305_hw;
}
@@ -7,11 +7,11 @@
* https://www.openssl.org/source/license.html
*/
#include "cipher_locl.h"
#include "prov/ciphercommon.h"
#include "cipher_des.h"
#include "internal/rand_int.h"
#include "internal/provider_algs.h"
#include "internal/providercommonerr.h"
#include "crypto/rand.h"
#include "prov/implementations.h"
#include "prov/providercommonerr.h"
/* TODO(3.0) Figure out what flags need to be here */
#define DES_FLAGS (EVP_CIPH_RAND_KEY)
@@ -36,7 +36,7 @@ static void *des_newctx(void *provctx, size_t kbits, size_t blkbits,
static void des_freectx(void *vctx)
{
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
OPENSSL_clear_free(ctx, sizeof(*ctx));
}
@@ -49,11 +49,8 @@ static int des_init(void *vctx, const unsigned char *key, size_t keylen,
ctx->enc = enc;
if (iv != NULL) {
if (ivlen != ctx->ivlen) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
if (!cipher_generic_initiv(ctx, iv, ivlen))
return 0;
}
memcpy(ctx->iv, iv, ivlen);
}
if (key != NULL) {
@@ -7,7 +7,7 @@
* https://www.openssl.org/source/license.html
*/
#include "internal/ciphers/ciphercommon.h"
#include "prov/ciphercommon.h"
#include "cipher_des.h"
static int cipher_hw_des_initkey(PROV_CIPHER_CTX *ctx,
@@ -8,7 +8,7 @@
*/
#include "cipher_tdes_default.h"
#include "internal/provider_algs.h"
#include "prov/implementations.h"
/* desx_cbc_functions */
IMPLEMENT_tdes_cipher(desx, DESX, cbc, CBC, TDES_FLAGS, 64*3, 64, 64, block);

Some files were not shown because too many files have changed in this diff Show More