Tolerate 0 byte input length for Update functions
[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     if (datalen == 0)
86         return 1;
87     return ctx->meth->update(ctx->data, data, datalen);
88 }
89
90 int EVP_MAC_final(EVP_MAC_CTX *ctx, unsigned char *out, size_t *poutlen)
91 {
92     int l = ctx->meth->size(ctx->data);
93
94     if (l < 0)
95         return 0;
96     if (poutlen != NULL)
97         *poutlen = l;
98     if (out == NULL)
99         return 1;
100     return ctx->meth->final(ctx->data, out);
101 }
102
103 int EVP_MAC_ctrl(EVP_MAC_CTX *ctx, int cmd, ...)
104 {
105     int ok = -1;
106     va_list args;
107
108     va_start(args, cmd);
109     ok = EVP_MAC_vctrl(ctx, cmd, args);
110     va_end(args);
111
112     if (ok == -2)
113         EVPerr(EVP_F_EVP_MAC_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
114
115     return ok;
116 }
117
118 int EVP_MAC_vctrl(EVP_MAC_CTX *ctx, int cmd, va_list args)
119 {
120     int ok = 1;
121
122     if (ctx == NULL || ctx->meth == NULL)
123         return -2;
124
125     switch (cmd) {
126 #if 0
127     case ...:
128         /* code */
129         ok = 1;
130         break;
131 #endif
132     default:
133         if (ctx->meth->ctrl != NULL)
134             ok = ctx->meth->ctrl(ctx->data, cmd, args);
135         else
136             ok = -2;
137         break;
138     }
139
140     return ok;
141 }
142
143 int EVP_MAC_ctrl_str(EVP_MAC_CTX *ctx, const char *type, const char *value)
144 {
145     int ok = 1;
146
147     if (ctx == NULL || ctx->meth == NULL || ctx->meth->ctrl_str == NULL) {
148         EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
149         return -2;
150     }
151
152     ok = ctx->meth->ctrl_str(ctx->data, type, value);
153
154     if (ok == -2)
155         EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
156     return ok;
157 }
158
159 int EVP_MAC_str2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value)
160 {
161     size_t len;
162
163     len = strlen(value);
164     if (len > INT_MAX)
165         return -1;
166     return EVP_MAC_ctrl(ctx, cmd, value, len);
167 }
168
169 int EVP_MAC_hex2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *hex)
170 {
171     unsigned char *bin;
172     long binlen;
173     int rv = -1;
174
175     bin = OPENSSL_hexstr2buf(hex, &binlen);
176     if (bin == NULL)
177         return 0;
178     if (binlen <= INT_MAX)
179         rv = EVP_MAC_ctrl(ctx, cmd, bin, (size_t)binlen);
180     OPENSSL_free(bin);
181     return rv;
182 }
183
184 int EVP_MAC_nid(const EVP_MAC *mac)
185 {
186     return mac->type;
187 }