Providers: move all digests
[openssl.git] / providers / implementations / macs / poly1305_prov.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
10 #include <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15
16 #include "crypto/poly1305.h"
17 /*
18  * TODO(3.0) when poly1305 has moved entirely to our providers, this
19  * header should be moved to the provider include directory.  For the
20  * moment, crypto/poly1305/poly1305_ameth.c has us stuck.
21  */
22 #include "../../../crypto/poly1305/poly1305_local.h"
23
24 #include "internal/providercommonerr.h"
25 #include "internal/provider_algs.h"
26
27 /*
28  * Forward declaration of everything implemented here.  This is not strictly
29  * necessary for the compiler, but provides an assurance that the signatures
30  * of the functions in the dispatch table are correct.
31  */
32 static OSSL_OP_mac_newctx_fn poly1305_new;
33 static OSSL_OP_mac_dupctx_fn poly1305_dup;
34 static OSSL_OP_mac_freectx_fn poly1305_free;
35 static OSSL_OP_mac_gettable_params_fn poly1305_gettable_params;
36 static OSSL_OP_mac_get_params_fn poly1305_get_params;
37 static OSSL_OP_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
38 static OSSL_OP_mac_set_ctx_params_fn poly1305_set_ctx_params;
39 static OSSL_OP_mac_init_fn poly1305_init;
40 static OSSL_OP_mac_update_fn poly1305_update;
41 static OSSL_OP_mac_final_fn poly1305_final;
42
43 struct poly1305_data_st {
44     void *provctx;
45     POLY1305 poly1305;           /* Poly1305 data */
46 };
47
48 static size_t poly1305_size(void);
49
50 static void *poly1305_new(void *provctx)
51 {
52     struct poly1305_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
53
54     ctx->provctx = provctx;
55     return ctx;
56 }
57
58 static void poly1305_free(void *vmacctx)
59 {
60     OPENSSL_free(vmacctx);
61 }
62
63 static void *poly1305_dup(void *vsrc)
64 {
65     struct poly1305_data_st *src = vsrc;
66     struct poly1305_data_st *dst = poly1305_new(src->provctx);
67
68     if (dst == NULL)
69         return NULL;
70
71     dst->poly1305 = src->poly1305;
72     return dst;
73 }
74
75 static size_t poly1305_size(void)
76 {
77     return POLY1305_DIGEST_SIZE;
78 }
79
80 static int poly1305_init(void *vmacctx)
81 {
82     /* initialize the context in MAC_ctrl function */
83     return 1;
84 }
85
86 static int poly1305_update(void *vmacctx, const unsigned char *data,
87                        size_t datalen)
88 {
89     struct poly1305_data_st *ctx = vmacctx;
90
91     /* poly1305 has nothing to return in its update function */
92     Poly1305_Update(&ctx->poly1305, data, datalen);
93     return 1;
94 }
95
96 static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
97                           size_t outsize)
98 {
99     struct poly1305_data_st *ctx = vmacctx;
100
101     Poly1305_Final(&ctx->poly1305, out);
102     return 1;
103 }
104
105 static const OSSL_PARAM known_gettable_params[] = {
106     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
107     OSSL_PARAM_END
108 };
109 static const OSSL_PARAM *poly1305_gettable_params(void)
110 {
111     return known_gettable_params;
112 }
113
114 static int poly1305_get_params(OSSL_PARAM params[])
115 {
116     OSSL_PARAM *p;
117
118     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
119         return OSSL_PARAM_set_size_t(p, poly1305_size());
120
121     return 1;
122 }
123
124 static const OSSL_PARAM known_settable_ctx_params[] = {
125     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
126     OSSL_PARAM_END
127 };
128 static const OSSL_PARAM *poly1305_settable_ctx_params(void)
129 {
130     return known_settable_ctx_params;
131 }
132
133 static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
134 {
135     struct poly1305_data_st *ctx = vmacctx;
136     const OSSL_PARAM *p = NULL;
137
138     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
139         if (p->data_type != OSSL_PARAM_OCTET_STRING
140             || p->data_size != POLY1305_KEY_SIZE) {
141             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
142             return 0;
143         }
144         Poly1305_Init(&ctx->poly1305, p->data);
145     }
146     return 1;
147 }
148
149 const OSSL_DISPATCH poly1305_functions[] = {
150     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
151     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
152     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
153     { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
154     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
155     { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
156     { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
157     { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
158     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
159       (void (*)(void))poly1305_settable_ctx_params },
160     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
161     { 0, NULL }
162 };