Ensure HMAC_size() handles errors correctly
[openssl.git] / crypto / hmac / hmac.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/hmac.h>
15 #include <openssl/opensslconf.h>
16 #include "hmac_lcl.h"
17
18 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
19                  const EVP_MD *md, ENGINE *impl)
20 {
21     int i, j, reset = 0;
22     unsigned char pad[HMAC_MAX_MD_CBLOCK];
23
24     /* If we are changing MD then we must have a key */
25     if (md != NULL && md != ctx->md && (key == NULL || len < 0))
26         return 0;
27
28     if (md != NULL) {
29         reset = 1;
30         ctx->md = md;
31     } else if (ctx->md) {
32         md = ctx->md;
33     } else {
34         return 0;
35     }
36
37     if (key != NULL) {
38         reset = 1;
39         j = EVP_MD_block_size(md);
40         OPENSSL_assert(j <= (int)sizeof(ctx->key));
41         if (j < len) {
42             if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl))
43                 goto err;
44             if (!EVP_DigestUpdate(ctx->md_ctx, key, len))
45                 goto err;
46             if (!EVP_DigestFinal_ex(ctx->md_ctx, ctx->key,
47                                     &ctx->key_length))
48                 goto err;
49         } else {
50             if (len < 0 || len > (int)sizeof(ctx->key))
51                 return 0;
52             memcpy(ctx->key, key, len);
53             ctx->key_length = len;
54         }
55         if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
56             memset(&ctx->key[ctx->key_length], 0,
57                    HMAC_MAX_MD_CBLOCK - ctx->key_length);
58     }
59
60     if (reset) {
61         for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
62             pad[i] = 0x36 ^ ctx->key[i];
63         if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl))
64             goto err;
65         if (!EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
66             goto err;
67
68         for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
69             pad[i] = 0x5c ^ ctx->key[i];
70         if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl))
71             goto err;
72         if (!EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
73             goto err;
74     }
75     if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
76         goto err;
77     return 1;
78  err:
79     return 0;
80 }
81
82 #if OPENSSL_API_COMPAT < 0x10100000L
83 int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
84 {
85     if (key && md)
86         HMAC_CTX_reset(ctx);
87     return HMAC_Init_ex(ctx, key, len, md, NULL);
88 }
89 #endif
90
91 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
92 {
93     if (!ctx->md)
94         return 0;
95     return EVP_DigestUpdate(ctx->md_ctx, data, len);
96 }
97
98 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
99 {
100     unsigned int i;
101     unsigned char buf[EVP_MAX_MD_SIZE];
102
103     if (!ctx->md)
104         goto err;
105
106     if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
107         goto err;
108     if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
109         goto err;
110     if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
111         goto err;
112     if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
113         goto err;
114     return 1;
115  err:
116     return 0;
117 }
118
119 size_t HMAC_size(const HMAC_CTX *ctx)
120 {
121     int size = EVP_MD_size((ctx)->md);
122     if (size < 0)
123         return 0;
124     return size;
125 }
126
127 HMAC_CTX *HMAC_CTX_new(void)
128 {
129     HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
130
131     if (ctx != NULL) {
132         if (!HMAC_CTX_reset(ctx)) {
133             HMAC_CTX_free(ctx);
134             return NULL;
135         }
136     }
137     return ctx;
138 }
139
140 static void hmac_ctx_cleanup(HMAC_CTX *ctx)
141 {
142     EVP_MD_CTX_reset(ctx->i_ctx);
143     EVP_MD_CTX_reset(ctx->o_ctx);
144     EVP_MD_CTX_reset(ctx->md_ctx);
145     ctx->md = NULL;
146     ctx->key_length = 0;
147     OPENSSL_cleanse(ctx->key, sizeof(ctx->key));
148 }
149
150 void HMAC_CTX_free(HMAC_CTX *ctx)
151 {
152     if (ctx != NULL) {
153         hmac_ctx_cleanup(ctx);
154         EVP_MD_CTX_free(ctx->i_ctx);
155         EVP_MD_CTX_free(ctx->o_ctx);
156         EVP_MD_CTX_free(ctx->md_ctx);
157         OPENSSL_free(ctx);
158     }
159 }
160
161 int HMAC_CTX_reset(HMAC_CTX *ctx)
162 {
163     hmac_ctx_cleanup(ctx);
164     if (ctx->i_ctx == NULL)
165         ctx->i_ctx = EVP_MD_CTX_new();
166     if (ctx->i_ctx == NULL)
167         goto err;
168     if (ctx->o_ctx == NULL)
169         ctx->o_ctx = EVP_MD_CTX_new();
170     if (ctx->o_ctx == NULL)
171         goto err;
172     if (ctx->md_ctx == NULL)
173         ctx->md_ctx = EVP_MD_CTX_new();
174     if (ctx->md_ctx == NULL)
175         goto err;
176     ctx->md = NULL;
177     return 1;
178  err:
179     hmac_ctx_cleanup(ctx);
180     return 0;
181 }
182
183 int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
184 {
185     if (!HMAC_CTX_reset(dctx))
186         goto err;
187     if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
188         goto err;
189     if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
190         goto err;
191     if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
192         goto err;
193     memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
194     dctx->key_length = sctx->key_length;
195     dctx->md = sctx->md;
196     return 1;
197  err:
198     hmac_ctx_cleanup(dctx);
199     return 0;
200 }
201
202 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
203                     const unsigned char *d, size_t n, unsigned char *md,
204                     unsigned int *md_len)
205 {
206     HMAC_CTX *c = NULL;
207     static unsigned char m[EVP_MAX_MD_SIZE];
208     static const unsigned char dummy_key[1] = {'\0'};
209
210     if (md == NULL)
211         md = m;
212     if ((c = HMAC_CTX_new()) == NULL)
213         goto err;
214
215     /* For HMAC_Init_ex, NULL key signals reuse. */
216     if (key == NULL && key_len == 0) {
217         key = dummy_key;
218     }
219
220     if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
221         goto err;
222     if (!HMAC_Update(c, d, n))
223         goto err;
224     if (!HMAC_Final(c, md, md_len))
225         goto err;
226     HMAC_CTX_free(c);
227     return md;
228  err:
229     HMAC_CTX_free(c);
230     return NULL;
231 }
232
233 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
234 {
235     EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
236     EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
237     EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
238 }
239
240 const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)
241 {
242     return ctx->md;
243 }