From: Richard Levitte Date: Sun, 2 Jun 2019 07:33:28 +0000 (+0200) Subject: Move Poly1305 to providers X-Git-Tag: openssl-3.0.0-alpha1~1596 X-Git-Url: https://git.openssl.org/?p=openssl.git;a=commitdiff_plain;h=ae0b6b92038b3f6e3a0e2f354cd900f96bce4d8b;ds=inline Move Poly1305 to providers Reviewed-by: Matt Caswell Reviewed-by: Shane Lontis (Merged from https://github.com/openssl/openssl/pull/8877) --- diff --git a/crypto/err/openssl.txt b/crypto/err/openssl.txt index 5b1b765b95..68a95c832b 100644 --- a/crypto/err/openssl.txt +++ b/crypto/err/openssl.txt @@ -1162,6 +1162,7 @@ PROV_F_BLAKE2_MAC_INIT:115:blake2_mac_init PROV_F_BLAKE2_MAC_SET_PARAMS:116:blake2_mac_set_params PROV_F_GMAC_SET_PARAMS:117:gmac_set_params PROV_F_KMAC_SET_PARAMS:118:kmac_set_params +PROV_F_POLY1305_SET_PARAMS:119:poly1305_set_params PROV_F_PROV_AES_KEY_GENERIC_INIT:113:PROV_AES_KEY_generic_init PROV_F_TRAILINGDATA:114:trailingdata PROV_F_UNPADBLOCK:100:unpadblock diff --git a/crypto/poly1305/build.info b/crypto/poly1305/build.info index 064c2599d5..cab28f4bdb 100644 --- a/crypto/poly1305/build.info +++ b/crypto/poly1305/build.info @@ -29,7 +29,7 @@ IF[{- !$disabled{asm} -}] ENDIF ENDIF -SOURCE[../../libcrypto]=poly1305_ameth.c poly1305_meth.c poly1305.c $POLY1305ASM +SOURCE[../../libcrypto]=poly1305_ameth.c poly1305.c $POLY1305ASM DEFINE[../../libcrypto]=$POLY1305DEF GENERATE[poly1305-sparcv9.S]=asm/poly1305-sparcv9.pl $(PERLASM_SCHEME) diff --git a/crypto/poly1305/poly1305_meth.c b/crypto/poly1305/poly1305_meth.c deleted file mode 100644 index f1ade58b40..0000000000 --- a/crypto/poly1305/poly1305_meth.c +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. - * - * Licensed under the Apache License 2.0 (the "License"). You may not use - * this file except in compliance with the License. You can obtain a copy - * in the file LICENSE in the source distribution or at - * https://www.openssl.org/source/license.html - */ -#include -#include "internal/evp_int.h" -#include "internal/poly1305.h" -#include "internal/cryptlib.h" -#include "poly1305_local.h" - -/* typedef EVP_MAC_IMPL */ -struct evp_mac_impl_st { - POLY1305 *ctx; /* poly1305 context */ -}; - -static EVP_MAC_IMPL *poly1305_new(void) -{ - EVP_MAC_IMPL *ctx; - - if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL - || (ctx->ctx = OPENSSL_zalloc(sizeof(POLY1305))) == NULL) { - OPENSSL_free(ctx); - return 0; - } - return ctx; -} - -static void poly1305_free(EVP_MAC_IMPL *ctx) -{ - if (ctx != NULL) { - OPENSSL_free(ctx->ctx); - OPENSSL_free(ctx); - } -} - -static EVP_MAC_IMPL *poly1305_dup(const EVP_MAC_IMPL *src) -{ - EVP_MAC_IMPL *dst; - - dst = poly1305_new(); - if (dst == NULL) - return NULL; - - *dst->ctx = *src->ctx; - - return dst; -} - -static size_t poly1305_size(EVP_MAC_IMPL *ctx) -{ - return POLY1305_DIGEST_SIZE; -} - -static int poly1305_init(EVP_MAC_IMPL *ctx) -{ - /* initialize the context in MAC_ctrl function */ - return 1; -} - -static int poly1305_update(EVP_MAC_IMPL *ctx, const unsigned char *data, - size_t datalen) -{ - POLY1305 *poly_ctx = ctx->ctx; - - /* poly1305 has nothing to return in its update function */ - Poly1305_Update(poly_ctx, data, datalen); - return 1; -} - -static int poly1305_final(EVP_MAC_IMPL *ctx, unsigned char *out) -{ - POLY1305 *poly_ctx = ctx->ctx; - - Poly1305_Final(poly_ctx, out); - return 1; -} - -static int poly1305_ctrl(EVP_MAC_IMPL *ctx, int cmd, va_list args) -{ - POLY1305 *poly_ctx = ctx->ctx; - unsigned char *key; - size_t keylen; - - switch (cmd) { - case EVP_MAC_CTRL_SET_KEY: - key = va_arg(args, unsigned char *); - keylen = va_arg(args, size_t); - - if (keylen != POLY1305_KEY_SIZE) { - EVPerr(EVP_F_POLY1305_CTRL, EVP_R_INVALID_KEY_LENGTH); - return 0; - } - Poly1305_Init(poly_ctx, key); - return 1; - default: - return -2; - } - return 1; -} - -static int poly1305_ctrl_int(EVP_MAC_IMPL *ctx, int cmd, ...) -{ - int rv; - va_list args; - - va_start(args, cmd); - rv = poly1305_ctrl(ctx, cmd, args); - va_end(args); - - return rv; -} - -static int poly1305_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen) -{ - return poly1305_ctrl_int(ctx, cmd, buf, buflen); -} - -static int poly1305_ctrl_str(EVP_MAC_IMPL *ctx, - const char *type, const char *value) -{ - if (value == NULL) - return 0; - if (strcmp(type, "key") == 0) - return EVP_str2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY, - value); - if (strcmp(type, "hexkey") == 0) - return EVP_hex2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY, - value); - return -2; -} - -const EVP_MAC poly1305_meth = { - EVP_MAC_POLY1305, - poly1305_new, - poly1305_dup, - poly1305_free, - poly1305_size, - poly1305_init, - poly1305_update, - poly1305_final, - poly1305_ctrl, - poly1305_ctrl_str -}; diff --git a/providers/common/include/internal/provider_algs.h b/providers/common/include/internal/provider_algs.h index 9f20417894..3260d93673 100644 --- a/providers/common/include/internal/provider_algs.h +++ b/providers/common/include/internal/provider_algs.h @@ -75,6 +75,7 @@ extern const OSSL_DISPATCH hmac_functions[]; extern const OSSL_DISPATCH kmac128_functions[]; extern const OSSL_DISPATCH kmac256_functions[]; extern const OSSL_DISPATCH siphash_functions[]; +extern const OSSL_DISPATCH poly1305_functions[]; /* Key management */ extern const OSSL_DISPATCH dh_keymgmt_functions[]; diff --git a/providers/default/defltprov.c b/providers/default/defltprov.c index 1f0626164b..0897557f47 100644 --- a/providers/default/defltprov.c +++ b/providers/default/defltprov.c @@ -141,6 +141,9 @@ static const OSSL_ALGORITHM deflt_macs[] = { { "KMAC256", "default=yes", kmac256_functions }, #ifndef OPENSSL_NO_SIPHASH { "SipHash", "default=yes", siphash_functions }, +#endif +#ifndef OPENSSL_NO_POLY1305 + { "Poly1305", "default=yes", poly1305_functions }, #endif { NULL, NULL, NULL } }; diff --git a/providers/default/macs/build.info b/providers/default/macs/build.info index edd95695fd..d7014e1009 100644 --- a/providers/default/macs/build.info +++ b/providers/default/macs/build.info @@ -1,3 +1,4 @@ LIBS=../../../libcrypto -SOURCE[../../../libcrypto]=blake2b_mac.c blake2s_mac.c siphash_prov.c +SOURCE[../../../libcrypto]=\ + blake2b_mac.c blake2s_mac.c siphash_prov.c poly1305_prov.c INCLUDE[../../../libcrypto]=. ../../../crypto diff --git a/providers/default/macs/poly1305_prov.c b/providers/default/macs/poly1305_prov.c new file mode 100644 index 0000000000..c64c2b9e9a --- /dev/null +++ b/providers/default/macs/poly1305_prov.c @@ -0,0 +1,169 @@ +/* + * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include +#ifndef OPENSSL_NO_POLY1305 + +# include +# include +# include +# include +# include + +# include "internal/poly1305.h" +/* + * TODO(3.0) when poly1305 has moved entirely to our providers, this + * header should be moved to the provider include directory. For the + * moment, crypto/poly1305/poly1305_ameth.c has us stuck. + */ +# include "../../../crypto/poly1305/poly1305_local.h" + +# include "internal/providercommonerr.h" +# include "internal/provider_algs.h" + +/* + * Forward declaration of everything implemented here. This is not strictly + * necessary for the compiler, but provides an assurance that the signatures + * of the functions in the dispatch table are correct. + */ +static OSSL_OP_mac_newctx_fn poly1305_new; +static OSSL_OP_mac_dupctx_fn poly1305_dup; +static OSSL_OP_mac_freectx_fn poly1305_free; +static OSSL_OP_mac_gettable_params_fn poly1305_gettable_params; +static OSSL_OP_mac_get_params_fn poly1305_get_params; +static OSSL_OP_mac_settable_ctx_params_fn poly1305_settable_ctx_params; +static OSSL_OP_mac_ctx_set_params_fn poly1305_ctx_set_params; +static OSSL_OP_mac_init_fn poly1305_init; +static OSSL_OP_mac_update_fn poly1305_update; +static OSSL_OP_mac_final_fn poly1305_final; + +struct poly1305_data_st { + void *provctx; + POLY1305 poly1305; /* Poly1305 data */ +}; + +static size_t poly1305_size(void); + +static void *poly1305_new(void *provctx) +{ + struct poly1305_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); + + ctx->provctx = provctx; + return ctx; +} + +static void poly1305_free(void *vmacctx) +{ + OPENSSL_free(vmacctx); +} + +static void *poly1305_dup(void *vsrc) +{ + struct poly1305_data_st *src = vsrc; + struct poly1305_data_st *dst = poly1305_new(src->provctx); + + if (dst == NULL) + return NULL; + + dst->poly1305 = src->poly1305; + return dst; +} + +static size_t poly1305_size(void) +{ + return POLY1305_DIGEST_SIZE; +} + +static int poly1305_init(void *vmacctx) +{ + /* initialize the context in MAC_ctrl function */ + return 1; +} + +static int poly1305_update(void *vmacctx, const unsigned char *data, + size_t datalen) +{ + struct poly1305_data_st *ctx = vmacctx; + + /* poly1305 has nothing to return in its update function */ + Poly1305_Update(&ctx->poly1305, data, datalen); + return 1; +} + +static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl, + size_t outsize) +{ + struct poly1305_data_st *ctx = vmacctx; + + Poly1305_Final(&ctx->poly1305, out); + return 1; +} + +static const OSSL_PARAM known_gettable_params[] = { + OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN, NULL), + OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */ + OSSL_PARAM_END +}; +static const OSSL_PARAM *poly1305_gettable_params(void) +{ + return known_gettable_params; +} + +static int poly1305_get_params(OSSL_PARAM params[]) +{ + OSSL_PARAM *p; + + if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_OUTLEN)) != NULL + || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL) + return OSSL_PARAM_set_size_t(p, poly1305_size()); + + return 1; +} + +static const OSSL_PARAM known_settable_ctx_params[] = { + OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), + OSSL_PARAM_END +}; +static const OSSL_PARAM *poly1305_settable_ctx_params(void) +{ + return known_settable_ctx_params; +} + +static int poly1305_ctx_set_params(void *vmacctx, const OSSL_PARAM *params) +{ + struct poly1305_data_st *ctx = vmacctx; + const OSSL_PARAM *p = NULL; + + if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { + if (p->data_type != OSSL_PARAM_OCTET_STRING + || p->data_size != POLY1305_KEY_SIZE) { + ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); + return 0; + } + Poly1305_Init(&ctx->poly1305, p->data); + } + return 1; +} + +const OSSL_DISPATCH poly1305_functions[] = { + { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new }, + { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup }, + { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free }, + { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init }, + { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update }, + { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final }, + { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params }, + { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params }, + { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, + (void (*)(void))poly1305_settable_ctx_params }, + { OSSL_FUNC_MAC_CTX_SET_PARAMS, (void (*)(void))poly1305_ctx_set_params }, + { 0, NULL } +}; + +#endif