Update copyright year
[openssl.git] / crypto / hmac / hmac.c
1 /*
2  * Copyright 1995-2021 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 /*
11  * HMAC low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "internal/cryptlib.h"
20 #include <openssl/opensslconf.h>
21 #include <openssl/hmac.h>
22 #include <openssl/core_names.h>
23 #include "hmac_local.h"
24
25 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
26                  const EVP_MD *md, ENGINE *impl)
27 {
28     int rv = 0, reset = 0;
29     int i, j;
30     unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE];
31     unsigned int keytmp_length;
32     unsigned char keytmp[HMAC_MAX_MD_CBLOCK_SIZE];
33
34     /* If we are changing MD then we must have a key */
35     if (md != NULL && md != ctx->md && (key == NULL || len < 0))
36         return 0;
37
38     if (md != NULL)
39         ctx->md = md;
40     else if (ctx->md != NULL)
41         md = ctx->md;
42     else
43         return 0;
44
45     /*
46      * The HMAC construction is not allowed to be used with the
47      * extendable-output functions (XOF) shake128 and shake256.
48      */
49     if ((EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0)
50         return 0;
51
52     if (key != NULL) {
53         reset = 1;
54
55         j = EVP_MD_block_size(md);
56         if (!ossl_assert(j <= (int)sizeof(keytmp)))
57             return 0;
58         if (j < 0)
59             return 0;
60         if (j < len) {
61             if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
62                     || !EVP_DigestUpdate(ctx->md_ctx, key, len)
63                     || !EVP_DigestFinal_ex(ctx->md_ctx, keytmp,
64                                            &keytmp_length))
65                 return 0;
66         } else {
67             if (len < 0 || len > (int)sizeof(keytmp))
68                 return 0;
69             memcpy(keytmp, key, len);
70             keytmp_length = len;
71         }
72         if (keytmp_length != HMAC_MAX_MD_CBLOCK_SIZE)
73             memset(&keytmp[keytmp_length], 0,
74                    HMAC_MAX_MD_CBLOCK_SIZE - keytmp_length);
75
76         for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
77             pad[i] = 0x36 ^ keytmp[i];
78         if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
79                 || !EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
80             goto err;
81
82         for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
83             pad[i] = 0x5c ^ keytmp[i];
84         if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
85                 || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
86             goto err;
87     }
88     if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
89         goto err;
90     rv = 1;
91  err:
92     if (reset) {
93         OPENSSL_cleanse(keytmp, sizeof(keytmp));
94         OPENSSL_cleanse(pad, sizeof(pad));
95     }
96     return rv;
97 }
98
99 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
100 int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
101 {
102     if (key && md)
103         HMAC_CTX_reset(ctx);
104     return HMAC_Init_ex(ctx, key, len, md, NULL);
105 }
106 #endif
107
108 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
109 {
110     if (!ctx->md)
111         return 0;
112     return EVP_DigestUpdate(ctx->md_ctx, data, len);
113 }
114
115 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
116 {
117     unsigned int i;
118     unsigned char buf[EVP_MAX_MD_SIZE];
119
120     if (!ctx->md)
121         goto err;
122
123     if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
124         goto err;
125     if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
126         goto err;
127     if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
128         goto err;
129     if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
130         goto err;
131     return 1;
132  err:
133     return 0;
134 }
135
136 size_t HMAC_size(const HMAC_CTX *ctx)
137 {
138     int size = EVP_MD_size((ctx)->md);
139
140     return (size < 0) ? 0 : size;
141 }
142
143 HMAC_CTX *HMAC_CTX_new(void)
144 {
145     HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
146
147     if (ctx != NULL) {
148         if (!HMAC_CTX_reset(ctx)) {
149             HMAC_CTX_free(ctx);
150             return NULL;
151         }
152     }
153     return ctx;
154 }
155
156 static void hmac_ctx_cleanup(HMAC_CTX *ctx)
157 {
158     EVP_MD_CTX_reset(ctx->i_ctx);
159     EVP_MD_CTX_reset(ctx->o_ctx);
160     EVP_MD_CTX_reset(ctx->md_ctx);
161     ctx->md = NULL;
162 }
163
164 void HMAC_CTX_free(HMAC_CTX *ctx)
165 {
166     if (ctx != NULL) {
167         hmac_ctx_cleanup(ctx);
168         EVP_MD_CTX_free(ctx->i_ctx);
169         EVP_MD_CTX_free(ctx->o_ctx);
170         EVP_MD_CTX_free(ctx->md_ctx);
171         OPENSSL_free(ctx);
172     }
173 }
174
175 static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
176 {
177     if (ctx->i_ctx == NULL)
178         ctx->i_ctx = EVP_MD_CTX_new();
179     if (ctx->i_ctx == NULL)
180         return 0;
181     if (ctx->o_ctx == NULL)
182         ctx->o_ctx = EVP_MD_CTX_new();
183     if (ctx->o_ctx == NULL)
184         return 0;
185     if (ctx->md_ctx == NULL)
186         ctx->md_ctx = EVP_MD_CTX_new();
187     if (ctx->md_ctx == NULL)
188         return 0;
189     return 1;
190 }
191
192 int HMAC_CTX_reset(HMAC_CTX *ctx)
193 {
194     hmac_ctx_cleanup(ctx);
195     if (!hmac_ctx_alloc_mds(ctx)) {
196         hmac_ctx_cleanup(ctx);
197         return 0;
198     }
199     return 1;
200 }
201
202 int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
203 {
204     if (!hmac_ctx_alloc_mds(dctx))
205         goto err;
206     if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
207         goto err;
208     if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
209         goto err;
210     if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
211         goto err;
212     dctx->md = sctx->md;
213     return 1;
214  err:
215     hmac_ctx_cleanup(dctx);
216     return 0;
217 }
218
219 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
220                     const unsigned char *data, size_t data_len,
221                     unsigned char *md, unsigned int *md_len)
222 {
223     static unsigned char static_md[EVP_MAX_MD_SIZE];
224     int size = EVP_MD_size(evp_md);
225
226     if (size < 0)
227         return NULL;
228     return EVP_Q_mac(NULL, "HMAC", NULL, EVP_MD_name(evp_md), NULL,
229                      key, key_len, data, data_len,
230                      md == NULL ? static_md : md, size, md_len);
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 }