]>
Commit | Line | Data |
---|---|---|
567db2c1 | 1 | /* |
33388b44 | 2 | * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved. |
567db2c1 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
567db2c1 RL |
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 <string.h> | |
11 | #include <stdarg.h> | |
12 | #include <openssl/evp.h> | |
13 | #include <openssl/err.h> | |
e74bd290 RL |
14 | #include <openssl/core.h> |
15 | #include <openssl/core_names.h> | |
50cd4768 | 16 | #include <openssl/types.h> |
567db2c1 | 17 | #include "internal/nelem.h" |
25f2138b | 18 | #include "crypto/evp.h" |
e74bd290 | 19 | #include "internal/provider.h" |
706457b7 | 20 | #include "evp_local.h" |
567db2c1 | 21 | |
865adf97 | 22 | EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac) |
567db2c1 RL |
23 | { |
24 | EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX)); | |
25 | ||
e74bd290 RL |
26 | if (ctx == NULL |
27 | || (ctx->data = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL | |
28 | || !EVP_MAC_up_ref(mac)) { | |
865adf97 | 29 | EVPerr(EVP_F_EVP_MAC_CTX_NEW, ERR_R_MALLOC_FAILURE); |
e74bd290 RL |
30 | if (ctx != NULL) |
31 | mac->freectx(ctx->data); | |
567db2c1 RL |
32 | OPENSSL_free(ctx); |
33 | ctx = NULL; | |
34 | } else { | |
35 | ctx->meth = mac; | |
36 | } | |
37 | return ctx; | |
38 | } | |
39 | ||
865adf97 | 40 | void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx) |
567db2c1 | 41 | { |
e74bd290 RL |
42 | if (ctx != NULL) { |
43 | ctx->meth->freectx(ctx->data); | |
567db2c1 | 44 | ctx->data = NULL; |
e74bd290 RL |
45 | /* refcnt-- */ |
46 | EVP_MAC_free(ctx->meth); | |
567db2c1 RL |
47 | } |
48 | OPENSSL_free(ctx); | |
49 | } | |
50 | ||
865adf97 | 51 | EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src) |
567db2c1 | 52 | { |
7ed66e26 | 53 | EVP_MAC_CTX *dst; |
567db2c1 | 54 | |
7ed66e26 | 55 | if (src->data == NULL) |
be5fc053 KR |
56 | return NULL; |
57 | ||
58 | dst = OPENSSL_malloc(sizeof(*dst)); | |
59 | if (dst == NULL) { | |
865adf97 | 60 | EVPerr(EVP_F_EVP_MAC_CTX_DUP, ERR_R_MALLOC_FAILURE); |
be5fc053 KR |
61 | return NULL; |
62 | } | |
567db2c1 | 63 | |
567db2c1 | 64 | *dst = *src; |
e74bd290 | 65 | if (!EVP_MAC_up_ref(dst->meth)) { |
865adf97 | 66 | EVPerr(EVP_F_EVP_MAC_CTX_DUP, ERR_R_MALLOC_FAILURE); |
e74bd290 RL |
67 | OPENSSL_free(dst); |
68 | return NULL; | |
69 | } | |
567db2c1 | 70 | |
e74bd290 | 71 | dst->data = src->meth->dupctx(src->data); |
be5fc053 | 72 | if (dst->data == NULL) { |
865adf97 | 73 | EVP_MAC_CTX_free(dst); |
be5fc053 KR |
74 | return NULL; |
75 | } | |
76 | ||
77 | return dst; | |
567db2c1 RL |
78 | } |
79 | ||
865adf97 | 80 | EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx) |
567db2c1 RL |
81 | { |
82 | return ctx->meth; | |
83 | } | |
84 | ||
85 | size_t EVP_MAC_size(EVP_MAC_CTX *ctx) | |
86 | { | |
e74bd290 RL |
87 | size_t sz = 0; |
88 | ||
89 | if (ctx->data != NULL) { | |
90 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
91 | ||
703170d4 | 92 | params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &sz); |
92d9d0ae RL |
93 | if (ctx->meth->get_ctx_params != NULL) { |
94 | if (ctx->meth->get_ctx_params(ctx->data, params)) | |
e74bd290 RL |
95 | return sz; |
96 | } else if (ctx->meth->get_params != NULL) { | |
97 | if (ctx->meth->get_params(params)) | |
98 | return sz; | |
99 | } | |
100 | } | |
101 | /* | |
102 | * If the MAC hasn't been initialized yet, or there is no size to get, | |
103 | * we return zero | |
104 | */ | |
567db2c1 RL |
105 | return 0; |
106 | } | |
107 | ||
108 | int EVP_MAC_init(EVP_MAC_CTX *ctx) | |
109 | { | |
110 | return ctx->meth->init(ctx->data); | |
111 | } | |
112 | ||
113 | int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen) | |
114 | { | |
115 | return ctx->meth->update(ctx->data, data, datalen); | |
116 | } | |
117 | ||
e74bd290 RL |
118 | int EVP_MAC_final(EVP_MAC_CTX *ctx, |
119 | unsigned char *out, size_t *outl, size_t outsize) | |
567db2c1 | 120 | { |
5f6a0b2f | 121 | size_t l; |
a85c9021 | 122 | int res = 1; |
567db2c1 | 123 | |
a85c9021 P |
124 | if (out != NULL) |
125 | res = ctx->meth->final(ctx->data, out, &l, outsize); | |
5f6a0b2f P |
126 | else |
127 | l = EVP_MAC_size(ctx); | |
e74bd290 RL |
128 | if (outl != NULL) |
129 | *outl = l; | |
a85c9021 | 130 | return res; |
567db2c1 RL |
131 | } |
132 | ||
e74bd290 RL |
133 | /* |
134 | * The {get,set}_params functions return 1 if there is no corresponding | |
135 | * function in the implementation. This is the same as if there was one, | |
136 | * but it didn't recognise any of the given params, i.e. nothing in the | |
137 | * bag of parameters was useful. | |
138 | */ | |
139 | int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]) | |
567db2c1 | 140 | { |
e74bd290 RL |
141 | if (mac->get_params != NULL) |
142 | return mac->get_params(params); | |
143 | return 1; | |
567db2c1 RL |
144 | } |
145 | ||
865adf97 | 146 | int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]) |
567db2c1 | 147 | { |
92d9d0ae RL |
148 | if (ctx->meth->get_ctx_params != NULL) |
149 | return ctx->meth->get_ctx_params(ctx->data, params); | |
e74bd290 | 150 | return 1; |
567db2c1 RL |
151 | } |
152 | ||
865adf97 | 153 | int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]) |
567db2c1 | 154 | { |
92d9d0ae RL |
155 | if (ctx->meth->set_ctx_params != NULL) |
156 | return ctx->meth->set_ctx_params(ctx->data, params); | |
e74bd290 | 157 | return 1; |
567db2c1 | 158 | } |
251e610c | 159 | |
506cb0f6 RL |
160 | int EVP_MAC_number(const EVP_MAC *mac) |
161 | { | |
162 | return mac->name_id; | |
163 | } | |
164 | ||
c9452d74 P |
165 | const char *EVP_MAC_name(const EVP_MAC *mac) |
166 | { | |
167 | if (mac->prov != NULL) | |
168 | return evp_first_name(mac->prov, mac->name_id); | |
169 | return NULL; | |
170 | } | |
171 | ||
251e610c RL |
172 | int EVP_MAC_is_a(const EVP_MAC *mac, const char *name) |
173 | { | |
e4a1d023 | 174 | return evp_is_a(mac->prov, mac->name_id, NULL, name); |
251e610c | 175 | } |
f651c727 RL |
176 | |
177 | void EVP_MAC_names_do_all(const EVP_MAC *mac, | |
178 | void (*fn)(const char *name, void *data), | |
179 | void *data) | |
180 | { | |
181 | if (mac->prov != NULL) | |
182 | evp_names_do_all(mac->prov, mac->name_id, fn, data); | |
183 | } |