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