]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/mac_lib.c
constify *_dup() and *i2d_*() and related functions as far as possible, introducing...
[thirdparty/openssl.git] / crypto / evp / mac_lib.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 <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
51 int EVP_MAC_CTX_copy(EVP_MAC_CTX *dst, const EVP_MAC_CTX *src)
52 {
53 EVP_MAC_IMPL *macdata;
54
55 if (src->data != NULL && !dst->meth->copy(dst->data, src->data))
56 return 0;
57
58 macdata = dst->data;
59 *dst = *src;
60 dst->data = macdata;
61
62 return 1;
63 }
64
65 const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
66 {
67 return ctx->meth;
68 }
69
70 size_t EVP_MAC_size(EVP_MAC_CTX *ctx)
71 {
72 if (ctx->data != NULL)
73 return ctx->meth->size(ctx->data);
74 /* If the MAC hasn't been initialized yet, we return zero */
75 return 0;
76 }
77
78 int EVP_MAC_init(EVP_MAC_CTX *ctx)
79 {
80 return ctx->meth->init(ctx->data);
81 }
82
83 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
84 {
85 return ctx->meth->update(ctx->data, data, datalen);
86 }
87
88 int EVP_MAC_final(EVP_MAC_CTX *ctx, unsigned char *out, size_t *poutlen)
89 {
90 int l = ctx->meth->size(ctx->data);
91
92 if (l < 0)
93 return 0;
94 if (poutlen != NULL)
95 *poutlen = l;
96 if (out == NULL)
97 return 1;
98 return ctx->meth->final(ctx->data, out);
99 }
100
101 int EVP_MAC_ctrl(EVP_MAC_CTX *ctx, int cmd, ...)
102 {
103 int ok = -1;
104 va_list args;
105
106 va_start(args, cmd);
107 ok = EVP_MAC_vctrl(ctx, cmd, args);
108 va_end(args);
109
110 if (ok == -2)
111 EVPerr(EVP_F_EVP_MAC_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
112
113 return ok;
114 }
115
116 int EVP_MAC_vctrl(EVP_MAC_CTX *ctx, int cmd, va_list args)
117 {
118 int ok = 1;
119
120 if (ctx == NULL || ctx->meth == NULL)
121 return -2;
122
123 switch (cmd) {
124 #if 0
125 case ...:
126 /* code */
127 ok = 1;
128 break;
129 #endif
130 default:
131 if (ctx->meth->ctrl != NULL)
132 ok = ctx->meth->ctrl(ctx->data, cmd, args);
133 else
134 ok = -2;
135 break;
136 }
137
138 return ok;
139 }
140
141 int EVP_MAC_ctrl_str(EVP_MAC_CTX *ctx, const char *type, const char *value)
142 {
143 int ok = 1;
144
145 if (ctx == NULL || ctx->meth == NULL || ctx->meth->ctrl_str == NULL) {
146 EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
147 return -2;
148 }
149
150 ok = ctx->meth->ctrl_str(ctx->data, type, value);
151
152 if (ok == -2)
153 EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
154 return ok;
155 }
156
157 int EVP_MAC_str2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value)
158 {
159 size_t len;
160
161 len = strlen(value);
162 if (len > INT_MAX)
163 return -1;
164 return EVP_MAC_ctrl(ctx, cmd, value, len);
165 }
166
167 int EVP_MAC_hex2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *hex)
168 {
169 unsigned char *bin;
170 long binlen;
171 int rv = -1;
172
173 bin = OPENSSL_hexstr2buf(hex, &binlen);
174 if (bin == NULL)
175 return 0;
176 if (binlen <= INT_MAX)
177 rv = EVP_MAC_ctrl(ctx, cmd, bin, (size_t)binlen);
178 OPENSSL_free(bin);
179 return rv;
180 }
181
182 int EVP_MAC_nid(const EVP_MAC *mac)
183 {
184 return mac->type;
185 }