2019-06-02 15:33:28 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2019-06-02 15:33:28 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-06-21 07:21:19 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2019-08-28 21:57:56 +08:00
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include <openssl/params.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
2021-02-06 00:40:42 +08:00
|
|
|
#include <openssl/proverr.h>
|
2019-06-02 15:33:28 +08:00
|
|
|
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/poly1305.h"
|
2019-06-02 15:33:28 +08:00
|
|
|
|
2019-10-04 21:20:48 +08:00
|
|
|
#include "prov/implementations.h"
|
2020-09-07 11:03:07 +08:00
|
|
|
#include "prov/providercommon.h"
|
2019-06-02 15:33:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_mac_newctx_fn poly1305_new;
|
|
|
|
static OSSL_FUNC_mac_dupctx_fn poly1305_dup;
|
|
|
|
static OSSL_FUNC_mac_freectx_fn poly1305_free;
|
|
|
|
static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;
|
|
|
|
static OSSL_FUNC_mac_get_params_fn poly1305_get_params;
|
|
|
|
static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
|
|
|
|
static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;
|
|
|
|
static OSSL_FUNC_mac_init_fn poly1305_init;
|
|
|
|
static OSSL_FUNC_mac_update_fn poly1305_update;
|
|
|
|
static OSSL_FUNC_mac_final_fn poly1305_final;
|
2019-06-02 15:33:28 +08:00
|
|
|
|
|
|
|
struct poly1305_data_st {
|
|
|
|
void *provctx;
|
2022-04-12 23:58:23 +08:00
|
|
|
int updated;
|
2019-06-02 15:33:28 +08:00
|
|
|
POLY1305 poly1305; /* Poly1305 data */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *poly1305_new(void *provctx)
|
|
|
|
{
|
2020-09-07 11:03:07 +08:00
|
|
|
struct poly1305_data_st *ctx;
|
2019-06-02 15:33:28 +08:00
|
|
|
|
2020-09-07 11:03:07 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
|
|
|
ctx = OPENSSL_zalloc(sizeof(*ctx));
|
2019-12-12 05:34:46 +08:00
|
|
|
if (ctx != NULL)
|
|
|
|
ctx->provctx = provctx;
|
2019-06-02 15:33:28 +08:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void poly1305_free(void *vmacctx)
|
|
|
|
{
|
|
|
|
OPENSSL_free(vmacctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *poly1305_dup(void *vsrc)
|
|
|
|
{
|
|
|
|
struct poly1305_data_st *src = vsrc;
|
2020-09-07 11:03:07 +08:00
|
|
|
struct poly1305_data_st *dst;
|
2019-06-02 15:33:28 +08:00
|
|
|
|
2020-09-07 11:03:07 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
2022-04-21 23:33:26 +08:00
|
|
|
dst = OPENSSL_malloc(sizeof(*dst));
|
2019-06-02 15:33:28 +08:00
|
|
|
if (dst == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2022-04-21 23:33:26 +08:00
|
|
|
*dst = *src;
|
2019-06-02 15:33:28 +08:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t poly1305_size(void)
|
|
|
|
{
|
|
|
|
return POLY1305_DIGEST_SIZE;
|
|
|
|
}
|
|
|
|
|
2021-02-25 11:54:55 +08:00
|
|
|
static int poly1305_setkey(struct poly1305_data_st *ctx,
|
|
|
|
const unsigned char *key, size_t keylen)
|
2019-06-02 15:33:28 +08:00
|
|
|
{
|
2021-02-25 11:54:55 +08:00
|
|
|
if (keylen != POLY1305_KEY_SIZE) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Poly1305_Init(&ctx->poly1305, key);
|
2022-04-21 23:33:26 +08:00
|
|
|
ctx->updated = 0;
|
2021-02-25 11:54:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int poly1305_init(void *vmacctx, const unsigned char *key,
|
|
|
|
size_t keylen, const OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
struct poly1305_data_st *ctx = vmacctx;
|
|
|
|
|
2019-06-02 15:33:28 +08:00
|
|
|
/* initialize the context in MAC_ctrl function */
|
2021-02-25 11:54:55 +08:00
|
|
|
if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))
|
|
|
|
return 0;
|
|
|
|
if (key != NULL)
|
|
|
|
return poly1305_setkey(ctx, key, keylen);
|
2022-04-12 23:58:23 +08:00
|
|
|
/* no reinitialization of context with the same key is allowed */
|
|
|
|
return ctx->updated == 0;
|
2019-06-02 15:33:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int poly1305_update(void *vmacctx, const unsigned char *data,
|
|
|
|
size_t datalen)
|
|
|
|
{
|
|
|
|
struct poly1305_data_st *ctx = vmacctx;
|
|
|
|
|
2022-04-12 23:58:23 +08:00
|
|
|
ctx->updated = 1;
|
2020-08-31 21:44:17 +08:00
|
|
|
if (datalen == 0)
|
|
|
|
return 1;
|
|
|
|
|
2019-06-02 15:33:28 +08:00
|
|
|
/* 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;
|
|
|
|
|
2020-09-07 11:03:07 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2022-04-12 23:58:23 +08:00
|
|
|
ctx->updated = 1;
|
2019-06-02 15:33:28 +08:00
|
|
|
Poly1305_Final(&ctx->poly1305, out);
|
2020-08-05 13:26:48 +08:00
|
|
|
*outl = poly1305_size();
|
2019-06-02 15:33:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const OSSL_PARAM known_gettable_params[] = {
|
2019-08-22 18:50:00 +08:00
|
|
|
OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
|
2019-06-02 15:33:28 +08:00
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
2020-08-05 11:23:32 +08:00
|
|
|
static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
|
2019-06-02 15:33:28 +08:00
|
|
|
{
|
|
|
|
return known_gettable_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int poly1305_get_params(OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
OSSL_PARAM *p;
|
|
|
|
|
2019-08-22 18:50:00 +08:00
|
|
|
if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
|
2019-06-02 15:33:28 +08:00
|
|
|
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
|
|
|
|
};
|
2021-02-23 09:02:49 +08:00
|
|
|
static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
|
|
|
|
ossl_unused void *provctx)
|
2019-06-02 15:33:28 +08:00
|
|
|
{
|
|
|
|
return known_settable_ctx_params;
|
|
|
|
}
|
|
|
|
|
2019-08-16 15:04:29 +08:00
|
|
|
static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
|
2019-06-02 15:33:28 +08:00
|
|
|
{
|
|
|
|
struct poly1305_data_st *ctx = vmacctx;
|
2021-02-25 11:54:55 +08:00
|
|
|
const OSSL_PARAM *p;
|
|
|
|
|
|
|
|
if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL
|
|
|
|
&& !poly1305_setkey(ctx, p->data, p->data_size))
|
|
|
|
return 0;
|
2019-06-02 15:33:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:28:29 +08:00
|
|
|
const OSSL_DISPATCH ossl_poly1305_functions[] = {
|
2019-06-02 15:33:28 +08:00
|
|
|
{ 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 },
|
2019-08-16 15:04:29 +08:00
|
|
|
{ OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
|
2023-04-19 22:08:22 +08:00
|
|
|
OSSL_DISPATCH_END
|
2019-06-02 15:33:28 +08:00
|
|
|
};
|