Move Poly1305 to providers
authorRichard Levitte <levitte@openssl.org>
Sun, 2 Jun 2019 07:33:28 +0000 (09:33 +0200)
committerRichard Levitte <levitte@openssl.org>
Thu, 15 Aug 2019 20:12:25 +0000 (22:12 +0200)
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/8877)

crypto/err/openssl.txt
crypto/poly1305/build.info
crypto/poly1305/poly1305_meth.c [deleted file]
providers/common/include/internal/provider_algs.h
providers/default/defltprov.c
providers/default/macs/build.info
providers/default/macs/poly1305_prov.c [new file with mode: 0644]

index 5b1b765b9526a57c8982a1c37bdfce13ac8210a5..68a95c832b9f3da58ae17f871e0e101724cfd8ac 100644 (file)
@@ -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
index 064c2599d55e66094d7a309ece9400e86484d25e..cab28f4bdb7b37c34c4ba7e7060688b9c9957a9f 100644 (file)
@@ -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 (file)
index f1ade58..0000000
+++ /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 <openssl/evp.h>
-#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
-};
index 9f20417894e63f3a7c256403c00bd6f11711c404..3260d93673e0a76b2c327f0d0e5f1021bb0e889b 100644 (file)
@@ -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[];
index 1f0626164b6dc20b7cfb42142a071c1e4e4726ac..0897557f47f7689ff64c379c623fc5a7a0e2d82c 100644 (file)
@@ -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 }
 };
index edd95695fd9faf4f74b1d899a5de64d6a8403de6..d7014e1009925f65319a9dcd87ed6d787e306e05 100644 (file)
@@ -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 (file)
index 0000000..c64c2b9
--- /dev/null
@@ -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 <openssl/opensslconf.h>
+#ifndef OPENSSL_NO_POLY1305
+
+# include <openssl/core_numbers.h>
+# include <openssl/core_names.h>
+# include <openssl/params.h>
+# include <openssl/evp.h>
+# include <openssl/err.h>
+
+# include "internal/poly1305.h"
+/*
+ * TODO(3.0) when poly1305 has moved entirely to our providers, this
+ * header should be moved to the provider include directory.  For the
+ * moment, crypto/poly1305/poly1305_ameth.c has us stuck.
+ */
+# include "../../../crypto/poly1305/poly1305_local.h"
+
+# include "internal/providercommonerr.h"
+# include "internal/provider_algs.h"
+
+/*
+ * Forward declaration of everything implemented here.  This is not strictly
+ * necessary for the compiler, but provides an assurance that the signatures
+ * of the functions in the dispatch table are correct.
+ */
+static OSSL_OP_mac_newctx_fn poly1305_new;
+static OSSL_OP_mac_dupctx_fn poly1305_dup;
+static OSSL_OP_mac_freectx_fn poly1305_free;
+static OSSL_OP_mac_gettable_params_fn poly1305_gettable_params;
+static OSSL_OP_mac_get_params_fn poly1305_get_params;
+static OSSL_OP_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
+static OSSL_OP_mac_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