]>
Commit | Line | Data |
---|---|---|
567db2c1 RL |
1 | /* |
2 | * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. | |
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> | |
14 | #include <openssl/ossl_typ.h> | |
15 | #include "internal/nelem.h" | |
16 | #include "internal/evp_int.h" | |
17 | #include "evp_locl.h" | |
18 | ||
19 | EVP_MAC_CTX *EVP_MAC_CTX_new_id(int id) | |
20 | { | |
21 | const EVP_MAC *mac = EVP_get_macbynid(id); | |
22 | ||
23 | if (mac == NULL) | |
24 | return NULL; | |
25 | return EVP_MAC_CTX_new(mac); | |
26 | } | |
27 | ||
28 | EVP_MAC_CTX *EVP_MAC_CTX_new(const EVP_MAC *mac) | |
29 | { | |
30 | EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX)); | |
31 | ||
32 | if (ctx == NULL || (ctx->data = mac->new()) == NULL) { | |
33 | EVPerr(EVP_F_EVP_MAC_CTX_NEW, ERR_R_MALLOC_FAILURE); | |
34 | OPENSSL_free(ctx); | |
35 | ctx = NULL; | |
36 | } else { | |
37 | ctx->meth = mac; | |
38 | } | |
39 | return ctx; | |
40 | } | |
41 | ||
42 | void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx) | |
43 | { | |
44 | if (ctx != NULL && ctx->data != NULL) { | |
45 | ctx->meth->free(ctx->data); | |
46 | ctx->data = NULL; | |
47 | } | |
48 | OPENSSL_free(ctx); | |
49 | } | |
50 | ||
be5fc053 | 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) { | |
60 | EVPerr(EVP_F_EVP_MAC_CTX_DUP, ERR_R_MALLOC_FAILURE); | |
61 | return NULL; | |
62 | } | |
567db2c1 | 63 | |
567db2c1 | 64 | *dst = *src; |
567db2c1 | 65 | |
be5fc053 KR |
66 | dst->data = src->meth->dup(src->data); |
67 | if (dst->data == NULL) { | |
68 | EVP_MAC_CTX_free(dst); | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | return dst; | |
567db2c1 RL |
73 | } |
74 | ||
75 | const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx) | |
76 | { | |
77 | return ctx->meth; | |
78 | } | |
79 | ||
80 | size_t EVP_MAC_size(EVP_MAC_CTX *ctx) | |
81 | { | |
82 | if (ctx->data != NULL) | |
83 | return ctx->meth->size(ctx->data); | |
84 | /* If the MAC hasn't been initialized yet, we return zero */ | |
85 | return 0; | |
86 | } | |
87 | ||
88 | int EVP_MAC_init(EVP_MAC_CTX *ctx) | |
89 | { | |
90 | return ctx->meth->init(ctx->data); | |
91 | } | |
92 | ||
93 | int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen) | |
94 | { | |
a8274ea3 MC |
95 | if (datalen == 0) |
96 | return 1; | |
567db2c1 RL |
97 | return ctx->meth->update(ctx->data, data, datalen); |
98 | } | |
99 | ||
100 | int EVP_MAC_final(EVP_MAC_CTX *ctx, unsigned char *out, size_t *poutlen) | |
101 | { | |
102 | int l = ctx->meth->size(ctx->data); | |
103 | ||
104 | if (l < 0) | |
105 | return 0; | |
106 | if (poutlen != NULL) | |
107 | *poutlen = l; | |
108 | if (out == NULL) | |
109 | return 1; | |
110 | return ctx->meth->final(ctx->data, out); | |
111 | } | |
112 | ||
113 | int EVP_MAC_ctrl(EVP_MAC_CTX *ctx, int cmd, ...) | |
114 | { | |
115 | int ok = -1; | |
116 | va_list args; | |
117 | ||
118 | va_start(args, cmd); | |
119 | ok = EVP_MAC_vctrl(ctx, cmd, args); | |
120 | va_end(args); | |
121 | ||
122 | if (ok == -2) | |
123 | EVPerr(EVP_F_EVP_MAC_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
124 | ||
125 | return ok; | |
126 | } | |
127 | ||
128 | int EVP_MAC_vctrl(EVP_MAC_CTX *ctx, int cmd, va_list args) | |
129 | { | |
130 | int ok = 1; | |
131 | ||
132 | if (ctx == NULL || ctx->meth == NULL) | |
133 | return -2; | |
134 | ||
135 | switch (cmd) { | |
136 | #if 0 | |
137 | case ...: | |
138 | /* code */ | |
139 | ok = 1; | |
140 | break; | |
141 | #endif | |
142 | default: | |
143 | if (ctx->meth->ctrl != NULL) | |
144 | ok = ctx->meth->ctrl(ctx->data, cmd, args); | |
145 | else | |
146 | ok = -2; | |
147 | break; | |
148 | } | |
149 | ||
150 | return ok; | |
151 | } | |
152 | ||
153 | int EVP_MAC_ctrl_str(EVP_MAC_CTX *ctx, const char *type, const char *value) | |
154 | { | |
155 | int ok = 1; | |
156 | ||
157 | if (ctx == NULL || ctx->meth == NULL || ctx->meth->ctrl_str == NULL) { | |
158 | EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
159 | return -2; | |
160 | } | |
161 | ||
162 | ok = ctx->meth->ctrl_str(ctx->data, type, value); | |
163 | ||
164 | if (ok == -2) | |
165 | EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
166 | return ok; | |
167 | } | |
168 | ||
169 | int EVP_MAC_str2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value) | |
170 | { | |
171 | size_t len; | |
172 | ||
173 | len = strlen(value); | |
174 | if (len > INT_MAX) | |
175 | return -1; | |
176 | return EVP_MAC_ctrl(ctx, cmd, value, len); | |
177 | } | |
178 | ||
179 | int EVP_MAC_hex2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *hex) | |
180 | { | |
181 | unsigned char *bin; | |
182 | long binlen; | |
183 | int rv = -1; | |
184 | ||
185 | bin = OPENSSL_hexstr2buf(hex, &binlen); | |
186 | if (bin == NULL) | |
187 | return 0; | |
188 | if (binlen <= INT_MAX) | |
189 | rv = EVP_MAC_ctrl(ctx, cmd, bin, (size_t)binlen); | |
190 | OPENSSL_free(bin); | |
191 | return rv; | |
192 | } | |
193 | ||
194 | int EVP_MAC_nid(const EVP_MAC *mac) | |
195 | { | |
196 | return mac->type; | |
197 | } |