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