1ebfedea0SLionel Sambuc /* crypto/hmac/hmac.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc #include <stdio.h>
59ebfedea0SLionel Sambuc #include <stdlib.h>
60ebfedea0SLionel Sambuc #include <string.h>
61ebfedea0SLionel Sambuc #include "cryptlib.h"
62ebfedea0SLionel Sambuc #include <openssl/hmac.h>
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
65ebfedea0SLionel Sambuc # include <openssl/fips.h>
66ebfedea0SLionel Sambuc #endif
67ebfedea0SLionel Sambuc
HMAC_Init_ex(HMAC_CTX * ctx,const void * key,int len,const EVP_MD * md,ENGINE * impl)68ebfedea0SLionel Sambuc int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
69ebfedea0SLionel Sambuc const EVP_MD *md, ENGINE *impl)
70ebfedea0SLionel Sambuc {
71ebfedea0SLionel Sambuc int i, j, reset = 0;
72ebfedea0SLionel Sambuc unsigned char pad[HMAC_MAX_MD_CBLOCK];
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
75*0a6a1f1dSLionel Sambuc if (FIPS_mode()) {
76ebfedea0SLionel Sambuc /* If we have an ENGINE need to allow non FIPS */
77ebfedea0SLionel Sambuc if ((impl || ctx->i_ctx.engine)
78*0a6a1f1dSLionel Sambuc && !(ctx->i_ctx.flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)) {
79ebfedea0SLionel Sambuc EVPerr(EVP_F_HMAC_INIT_EX, EVP_R_DISABLED_FOR_FIPS);
80ebfedea0SLionel Sambuc return 0;
81ebfedea0SLionel Sambuc }
82*0a6a1f1dSLionel Sambuc /*
83*0a6a1f1dSLionel Sambuc * Other algorithm blocking will be done in FIPS_cmac_init, via
84*0a6a1f1dSLionel Sambuc * FIPS_hmac_init_ex().
85ebfedea0SLionel Sambuc */
86ebfedea0SLionel Sambuc if (!impl && !ctx->i_ctx.engine)
87ebfedea0SLionel Sambuc return FIPS_hmac_init_ex(ctx, key, len, md, NULL);
88ebfedea0SLionel Sambuc }
89ebfedea0SLionel Sambuc #endif
90*0a6a1f1dSLionel Sambuc /* If we are changing MD then we must have a key */
91*0a6a1f1dSLionel Sambuc if (md != NULL && md != ctx->md && (key == NULL || len < 0))
92*0a6a1f1dSLionel Sambuc return 0;
93ebfedea0SLionel Sambuc
94*0a6a1f1dSLionel Sambuc if (md != NULL) {
95ebfedea0SLionel Sambuc reset = 1;
96ebfedea0SLionel Sambuc ctx->md = md;
97*0a6a1f1dSLionel Sambuc } else if (ctx->md) {
98ebfedea0SLionel Sambuc md = ctx->md;
99*0a6a1f1dSLionel Sambuc } else {
100*0a6a1f1dSLionel Sambuc return 0;
101*0a6a1f1dSLionel Sambuc }
102ebfedea0SLionel Sambuc
103*0a6a1f1dSLionel Sambuc if (key != NULL) {
104ebfedea0SLionel Sambuc reset = 1;
105ebfedea0SLionel Sambuc j = EVP_MD_block_size(md);
106ebfedea0SLionel Sambuc OPENSSL_assert(j <= (int)sizeof(ctx->key));
107*0a6a1f1dSLionel Sambuc if (j < len) {
108ebfedea0SLionel Sambuc if (!EVP_DigestInit_ex(&ctx->md_ctx, md, impl))
109ebfedea0SLionel Sambuc goto err;
110ebfedea0SLionel Sambuc if (!EVP_DigestUpdate(&ctx->md_ctx, key, len))
111ebfedea0SLionel Sambuc goto err;
112ebfedea0SLionel Sambuc if (!EVP_DigestFinal_ex(&(ctx->md_ctx), ctx->key,
113ebfedea0SLionel Sambuc &ctx->key_length))
114ebfedea0SLionel Sambuc goto err;
115*0a6a1f1dSLionel Sambuc } else {
116*0a6a1f1dSLionel Sambuc if (len < 0 || len > (int)sizeof(ctx->key))
117*0a6a1f1dSLionel Sambuc return 0;
118ebfedea0SLionel Sambuc memcpy(ctx->key, key, len);
119ebfedea0SLionel Sambuc ctx->key_length = len;
120ebfedea0SLionel Sambuc }
121ebfedea0SLionel Sambuc if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
122ebfedea0SLionel Sambuc memset(&ctx->key[ctx->key_length], 0,
123ebfedea0SLionel Sambuc HMAC_MAX_MD_CBLOCK - ctx->key_length);
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc
126*0a6a1f1dSLionel Sambuc if (reset) {
127ebfedea0SLionel Sambuc for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
128ebfedea0SLionel Sambuc pad[i] = 0x36 ^ ctx->key[i];
129ebfedea0SLionel Sambuc if (!EVP_DigestInit_ex(&ctx->i_ctx, md, impl))
130ebfedea0SLionel Sambuc goto err;
131ebfedea0SLionel Sambuc if (!EVP_DigestUpdate(&ctx->i_ctx, pad, EVP_MD_block_size(md)))
132ebfedea0SLionel Sambuc goto err;
133ebfedea0SLionel Sambuc
134ebfedea0SLionel Sambuc for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
135ebfedea0SLionel Sambuc pad[i] = 0x5c ^ ctx->key[i];
136ebfedea0SLionel Sambuc if (!EVP_DigestInit_ex(&ctx->o_ctx, md, impl))
137ebfedea0SLionel Sambuc goto err;
138ebfedea0SLionel Sambuc if (!EVP_DigestUpdate(&ctx->o_ctx, pad, EVP_MD_block_size(md)))
139ebfedea0SLionel Sambuc goto err;
140ebfedea0SLionel Sambuc }
141ebfedea0SLionel Sambuc if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->i_ctx))
142ebfedea0SLionel Sambuc goto err;
143ebfedea0SLionel Sambuc return 1;
144ebfedea0SLionel Sambuc err:
145ebfedea0SLionel Sambuc return 0;
146ebfedea0SLionel Sambuc }
147ebfedea0SLionel Sambuc
HMAC_Init(HMAC_CTX * ctx,const void * key,int len,const EVP_MD * md)148ebfedea0SLionel Sambuc int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
149ebfedea0SLionel Sambuc {
150ebfedea0SLionel Sambuc if (key && md)
151ebfedea0SLionel Sambuc HMAC_CTX_init(ctx);
152ebfedea0SLionel Sambuc return HMAC_Init_ex(ctx, key, len, md, NULL);
153ebfedea0SLionel Sambuc }
154ebfedea0SLionel Sambuc
HMAC_Update(HMAC_CTX * ctx,const unsigned char * data,size_t len)155ebfedea0SLionel Sambuc int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
156ebfedea0SLionel Sambuc {
157ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
158ebfedea0SLionel Sambuc if (FIPS_mode() && !ctx->i_ctx.engine)
159ebfedea0SLionel Sambuc return FIPS_hmac_update(ctx, data, len);
160ebfedea0SLionel Sambuc #endif
161*0a6a1f1dSLionel Sambuc if (!ctx->md)
162*0a6a1f1dSLionel Sambuc return 0;
163*0a6a1f1dSLionel Sambuc
164ebfedea0SLionel Sambuc return EVP_DigestUpdate(&ctx->md_ctx, data, len);
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc
HMAC_Final(HMAC_CTX * ctx,unsigned char * md,unsigned int * len)167ebfedea0SLionel Sambuc int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
168ebfedea0SLionel Sambuc {
169ebfedea0SLionel Sambuc unsigned int i;
170ebfedea0SLionel Sambuc unsigned char buf[EVP_MAX_MD_SIZE];
171ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
172ebfedea0SLionel Sambuc if (FIPS_mode() && !ctx->i_ctx.engine)
173ebfedea0SLionel Sambuc return FIPS_hmac_final(ctx, md, len);
174ebfedea0SLionel Sambuc #endif
175ebfedea0SLionel Sambuc
176*0a6a1f1dSLionel Sambuc if (!ctx->md)
177*0a6a1f1dSLionel Sambuc goto err;
178*0a6a1f1dSLionel Sambuc
179ebfedea0SLionel Sambuc if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i))
180ebfedea0SLionel Sambuc goto err;
181ebfedea0SLionel Sambuc if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx))
182ebfedea0SLionel Sambuc goto err;
183ebfedea0SLionel Sambuc if (!EVP_DigestUpdate(&ctx->md_ctx, buf, i))
184ebfedea0SLionel Sambuc goto err;
185ebfedea0SLionel Sambuc if (!EVP_DigestFinal_ex(&ctx->md_ctx, md, len))
186ebfedea0SLionel Sambuc goto err;
187ebfedea0SLionel Sambuc return 1;
188ebfedea0SLionel Sambuc err:
189ebfedea0SLionel Sambuc return 0;
190ebfedea0SLionel Sambuc }
191ebfedea0SLionel Sambuc
HMAC_CTX_init(HMAC_CTX * ctx)192ebfedea0SLionel Sambuc void HMAC_CTX_init(HMAC_CTX *ctx)
193ebfedea0SLionel Sambuc {
194ebfedea0SLionel Sambuc EVP_MD_CTX_init(&ctx->i_ctx);
195ebfedea0SLionel Sambuc EVP_MD_CTX_init(&ctx->o_ctx);
196ebfedea0SLionel Sambuc EVP_MD_CTX_init(&ctx->md_ctx);
197*0a6a1f1dSLionel Sambuc ctx->md = NULL;
198ebfedea0SLionel Sambuc }
199ebfedea0SLionel Sambuc
HMAC_CTX_copy(HMAC_CTX * dctx,HMAC_CTX * sctx)200ebfedea0SLionel Sambuc int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
201ebfedea0SLionel Sambuc {
202ebfedea0SLionel Sambuc if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx))
203ebfedea0SLionel Sambuc goto err;
204ebfedea0SLionel Sambuc if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx))
205ebfedea0SLionel Sambuc goto err;
206ebfedea0SLionel Sambuc if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
207ebfedea0SLionel Sambuc goto err;
208ebfedea0SLionel Sambuc memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
209ebfedea0SLionel Sambuc dctx->key_length = sctx->key_length;
210ebfedea0SLionel Sambuc dctx->md = sctx->md;
211ebfedea0SLionel Sambuc return 1;
212ebfedea0SLionel Sambuc err:
213ebfedea0SLionel Sambuc return 0;
214ebfedea0SLionel Sambuc }
215ebfedea0SLionel Sambuc
HMAC_CTX_cleanup(HMAC_CTX * ctx)216ebfedea0SLionel Sambuc void HMAC_CTX_cleanup(HMAC_CTX *ctx)
217ebfedea0SLionel Sambuc {
218ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
219*0a6a1f1dSLionel Sambuc if (FIPS_mode() && !ctx->i_ctx.engine) {
220ebfedea0SLionel Sambuc FIPS_hmac_ctx_cleanup(ctx);
221ebfedea0SLionel Sambuc return;
222ebfedea0SLionel Sambuc }
223ebfedea0SLionel Sambuc #endif
224ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&ctx->i_ctx);
225ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&ctx->o_ctx);
226ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&ctx->md_ctx);
227ebfedea0SLionel Sambuc memset(ctx, 0, sizeof *ctx);
228ebfedea0SLionel Sambuc }
229ebfedea0SLionel Sambuc
HMAC(const EVP_MD * evp_md,const void * key,int key_len,const unsigned char * d,size_t n,unsigned char * md,unsigned int * md_len)230ebfedea0SLionel Sambuc unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
231ebfedea0SLionel Sambuc const unsigned char *d, size_t n, unsigned char *md,
232ebfedea0SLionel Sambuc unsigned int *md_len)
233ebfedea0SLionel Sambuc {
234ebfedea0SLionel Sambuc HMAC_CTX c;
235ebfedea0SLionel Sambuc static unsigned char m[EVP_MAX_MD_SIZE];
236ebfedea0SLionel Sambuc
237*0a6a1f1dSLionel Sambuc if (md == NULL)
238*0a6a1f1dSLionel Sambuc md = m;
239ebfedea0SLionel Sambuc HMAC_CTX_init(&c);
240ebfedea0SLionel Sambuc if (!HMAC_Init(&c, key, key_len, evp_md))
241ebfedea0SLionel Sambuc goto err;
242ebfedea0SLionel Sambuc if (!HMAC_Update(&c, d, n))
243ebfedea0SLionel Sambuc goto err;
244ebfedea0SLionel Sambuc if (!HMAC_Final(&c, md, md_len))
245ebfedea0SLionel Sambuc goto err;
246ebfedea0SLionel Sambuc HMAC_CTX_cleanup(&c);
247ebfedea0SLionel Sambuc return md;
248ebfedea0SLionel Sambuc err:
249*0a6a1f1dSLionel Sambuc HMAC_CTX_cleanup(&c);
250ebfedea0SLionel Sambuc return NULL;
251ebfedea0SLionel Sambuc }
252ebfedea0SLionel Sambuc
HMAC_CTX_set_flags(HMAC_CTX * ctx,unsigned long flags)253ebfedea0SLionel Sambuc void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
254ebfedea0SLionel Sambuc {
255ebfedea0SLionel Sambuc EVP_MD_CTX_set_flags(&ctx->i_ctx, flags);
256ebfedea0SLionel Sambuc EVP_MD_CTX_set_flags(&ctx->o_ctx, flags);
257ebfedea0SLionel Sambuc EVP_MD_CTX_set_flags(&ctx->md_ctx, flags);
258ebfedea0SLionel Sambuc }
259