]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/poly1305/poly1305_meth.c
Add poly1305 MAC support
[thirdparty/openssl.git] / crypto / poly1305 / poly1305_meth.c
1 /*
2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
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"
14
15 /* typedef EVP_MAC_IMPL */
16 struct evp_mac_impl_st {
17 POLY1305 *ctx; /* poly1305 context */
18 };
19
20 static EVP_MAC_IMPL *poly1305_new(void)
21 {
22 EVP_MAC_IMPL *ctx;
23
24 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL
25 || (ctx->ctx = OPENSSL_zalloc(sizeof(POLY1305))) == NULL) {
26 OPENSSL_free(ctx);
27 return 0;
28 }
29 return ctx;
30 }
31
32 static void poly1305_free(EVP_MAC_IMPL *ctx)
33 {
34 if (ctx != NULL) {
35 OPENSSL_free(ctx->ctx);
36 OPENSSL_free(ctx);
37 }
38 }
39
40 static int poly1305_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src)
41 {
42 *dst->ctx = *src->ctx;
43
44 return 1;
45 }
46
47 static size_t poly1305_size(EVP_MAC_IMPL *ctx)
48 {
49 return POLY1305_DIGEST_SIZE;
50 }
51
52 static int poly1305_init(EVP_MAC_IMPL *ctx)
53 {
54 /* initialize the context in MAC_ctrl function */
55 return 1;
56 }
57
58 static int poly1305_update(EVP_MAC_IMPL *ctx, const unsigned char *data,
59 size_t datalen)
60 {
61 POLY1305 *poly_ctx = ctx->ctx;
62
63 /* poly1305 has nothing to return in its update function */
64 Poly1305_Update(poly_ctx, data, datalen);
65 return 1;
66 }
67
68 static int poly1305_final(EVP_MAC_IMPL *ctx, unsigned char *out)
69 {
70 POLY1305 *poly_ctx = ctx->ctx;
71
72 Poly1305_Final(poly_ctx, out);
73 return 1;
74 }
75
76 static int poly1305_ctrl(EVP_MAC_IMPL *ctx, int cmd, va_list args)
77 {
78 POLY1305 *poly_ctx = ctx->ctx;
79 unsigned char *key;
80 size_t keylen;
81
82 switch (cmd) {
83 case EVP_MAC_CTRL_SET_KEY:
84 key = va_arg(args, unsigned char *);
85 keylen = va_arg(args, size_t);
86
87 if (keylen != POLY1305_KEY_SIZE) {
88 EVPerr(EVP_F_POLY1305_CTRL, EVP_R_INVALID_KEY_LENGTH);
89 return 0;
90 }
91 Poly1305_Init(poly_ctx, key);
92 return 1;
93 default:
94 return -2;
95 }
96 return 1;
97 }
98
99 static int poly1305_ctrl_int(EVP_MAC_IMPL *ctx, int cmd, ...)
100 {
101 int rv;
102 va_list args;
103
104 va_start(args, cmd);
105 rv = poly1305_ctrl(ctx, cmd, args);
106 va_end(args);
107
108 return rv;
109 }
110
111 static int poly1305_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
112 {
113 return poly1305_ctrl_int(ctx, cmd, buf, buflen);
114 }
115
116 static int poly1305_ctrl_str(EVP_MAC_IMPL *ctx,
117 const char *type, const char *value)
118 {
119 if (value == NULL)
120 return 0;
121 if (strcmp(type, "key") == 0)
122 return EVP_str2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
123 value);
124 if (strcmp(type, "hexkey") == 0)
125 return EVP_hex2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
126 value);
127 return -2;
128 }
129
130 const EVP_MAC poly1305_meth = {
131 EVP_MAC_POLY1305,
132 poly1305_new,
133 poly1305_copy,
134 poly1305_free,
135 poly1305_size,
136 poly1305_init,
137 poly1305_update,
138 poly1305_final,
139 poly1305_ctrl,
140 poly1305_ctrl_str
141 };