2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
9 #include <openssl/evp.h>
10 #include "internal/evp_int.h"
11 #include "internal/poly1305.h"
12 #include "internal/cryptlib.h"
13 #include "poly1305_local.h"
15 /* typedef EVP_MAC_IMPL */
16 struct evp_mac_impl_st {
17 POLY1305 *ctx; /* poly1305 context */
20 static EVP_MAC_IMPL *poly1305_new(void)
24 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL
25 || (ctx->ctx = OPENSSL_zalloc(sizeof(POLY1305))) == NULL) {
32 static void poly1305_free(EVP_MAC_IMPL *ctx)
35 OPENSSL_free(ctx->ctx);
40 static int poly1305_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src)
42 *dst->ctx = *src->ctx;
47 static size_t poly1305_size(EVP_MAC_IMPL *ctx)
49 return POLY1305_DIGEST_SIZE;
52 static int poly1305_init(EVP_MAC_IMPL *ctx)
54 /* initialize the context in MAC_ctrl function */
58 static int poly1305_update(EVP_MAC_IMPL *ctx, const unsigned char *data,
61 POLY1305 *poly_ctx = ctx->ctx;
63 /* poly1305 has nothing to return in its update function */
64 Poly1305_Update(poly_ctx, data, datalen);
68 static int poly1305_final(EVP_MAC_IMPL *ctx, unsigned char *out)
70 POLY1305 *poly_ctx = ctx->ctx;
72 Poly1305_Final(poly_ctx, out);
76 static int poly1305_ctrl(EVP_MAC_IMPL *ctx, int cmd, va_list args)
78 POLY1305 *poly_ctx = ctx->ctx;
83 case EVP_MAC_CTRL_SET_KEY:
84 key = va_arg(args, unsigned char *);
85 keylen = va_arg(args, size_t);
87 if (keylen != POLY1305_KEY_SIZE) {
88 EVPerr(EVP_F_POLY1305_CTRL, EVP_R_INVALID_KEY_LENGTH);
91 Poly1305_Init(poly_ctx, key);
99 static int poly1305_ctrl_int(EVP_MAC_IMPL *ctx, int cmd, ...)
105 rv = poly1305_ctrl(ctx, cmd, args);
111 static int poly1305_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
113 return poly1305_ctrl_int(ctx, cmd, buf, buflen);
116 static int poly1305_ctrl_str(EVP_MAC_IMPL *ctx,
117 const char *type, const char *value)
121 if (strcmp(type, "key") == 0)
122 return EVP_str2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
124 if (strcmp(type, "hexkey") == 0)
125 return EVP_hex2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
130 const EVP_MAC poly1305_meth = {