2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 EVP_MAC_IMPL *poly1305_dup(const EVP_MAC_IMPL *src)
48 *dst->ctx = *src->ctx;
53 static size_t poly1305_size(EVP_MAC_IMPL *ctx)
55 return POLY1305_DIGEST_SIZE;
58 static int poly1305_init(EVP_MAC_IMPL *ctx)
60 /* initialize the context in MAC_ctrl function */
64 static int poly1305_update(EVP_MAC_IMPL *ctx, const unsigned char *data,
67 POLY1305 *poly_ctx = ctx->ctx;
69 /* poly1305 has nothing to return in its update function */
70 Poly1305_Update(poly_ctx, data, datalen);
74 static int poly1305_final(EVP_MAC_IMPL *ctx, unsigned char *out)
76 POLY1305 *poly_ctx = ctx->ctx;
78 Poly1305_Final(poly_ctx, out);
82 static int poly1305_ctrl(EVP_MAC_IMPL *ctx, int cmd, va_list args)
84 POLY1305 *poly_ctx = ctx->ctx;
89 case EVP_MAC_CTRL_SET_KEY:
90 key = va_arg(args, unsigned char *);
91 keylen = va_arg(args, size_t);
93 if (keylen != POLY1305_KEY_SIZE) {
94 EVPerr(EVP_F_POLY1305_CTRL, EVP_R_INVALID_KEY_LENGTH);
97 Poly1305_Init(poly_ctx, key);
105 static int poly1305_ctrl_int(EVP_MAC_IMPL *ctx, int cmd, ...)
111 rv = poly1305_ctrl(ctx, cmd, args);
117 static int poly1305_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
119 return poly1305_ctrl_int(ctx, cmd, buf, buflen);
122 static int poly1305_ctrl_str(EVP_MAC_IMPL *ctx,
123 const char *type, const char *value)
127 if (strcmp(type, "key") == 0)
128 return EVP_str2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
130 if (strcmp(type, "hexkey") == 0)
131 return EVP_hex2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
136 const EVP_MAC poly1305_meth = {