155714Skris/* crypto/hmac/hmac.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris#include <stdio.h>
5955714Skris#include <stdlib.h>
6055714Skris#include <string.h>
61160814Ssimon#include "cryptlib.h"
6255714Skris#include <openssl/hmac.h>
6355714Skris
64238405Sjkim#ifdef OPENSSL_FIPS
65280304Sjkim# include <openssl/fips.h>
66238405Sjkim#endif
67194206Ssimon
68238405Sjkimint HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
69280304Sjkim                 const EVP_MD *md, ENGINE *impl)
70280304Sjkim{
71280304Sjkim    int i, j, reset = 0;
72280304Sjkim    unsigned char pad[HMAC_MAX_MD_CBLOCK];
7355714Skris
74238405Sjkim#ifdef OPENSSL_FIPS
75280304Sjkim    if (FIPS_mode()) {
76280304Sjkim        /* If we have an ENGINE need to allow non FIPS */
77280304Sjkim        if ((impl || ctx->i_ctx.engine)
78280304Sjkim            && !(ctx->i_ctx.flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)) {
79280304Sjkim            EVPerr(EVP_F_HMAC_INIT_EX, EVP_R_DISABLED_FOR_FIPS);
80280304Sjkim            return 0;
81280304Sjkim        }
82280304Sjkim        /*
83280304Sjkim         * Other algorithm blocking will be done in FIPS_cmac_init, via
84280304Sjkim         * FIPS_hmac_init_ex().
85280304Sjkim         */
86280304Sjkim        if (!impl && !ctx->i_ctx.engine)
87280304Sjkim            return FIPS_hmac_init_ex(ctx, key, len, md, NULL);
88280304Sjkim    }
89238405Sjkim#endif
90284330Sjkim    /* If we are changing MD then we must have a key */
91284330Sjkim    if (md != NULL && md != ctx->md && (key == NULL || len < 0))
92284330Sjkim        return 0;
93238405Sjkim
94280304Sjkim    if (md != NULL) {
95280304Sjkim        reset = 1;
96280304Sjkim        ctx->md = md;
97284285Sjkim    } else if (ctx->md) {
98280304Sjkim        md = ctx->md;
99284285Sjkim    } else {
100284285Sjkim        return 0;
101284285Sjkim    }
10255714Skris
103280304Sjkim    if (key != NULL) {
104280304Sjkim        reset = 1;
105280304Sjkim        j = EVP_MD_block_size(md);
106280304Sjkim        OPENSSL_assert(j <= (int)sizeof(ctx->key));
107280304Sjkim        if (j < len) {
108280304Sjkim            if (!EVP_DigestInit_ex(&ctx->md_ctx, md, impl))
109280304Sjkim                goto err;
110280304Sjkim            if (!EVP_DigestUpdate(&ctx->md_ctx, key, len))
111280304Sjkim                goto err;
112280304Sjkim            if (!EVP_DigestFinal_ex(&(ctx->md_ctx), ctx->key,
113280304Sjkim                                    &ctx->key_length))
114280304Sjkim                goto err;
115280304Sjkim        } else {
116284285Sjkim            if (len < 0 || len > (int)sizeof(ctx->key))
117284285Sjkim                return 0;
118280304Sjkim            memcpy(ctx->key, key, len);
119280304Sjkim            ctx->key_length = len;
120280304Sjkim        }
121280304Sjkim        if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
122280304Sjkim            memset(&ctx->key[ctx->key_length], 0,
123280304Sjkim                   HMAC_MAX_MD_CBLOCK - ctx->key_length);
124280304Sjkim    }
12555714Skris
126280304Sjkim    if (reset) {
127280304Sjkim        for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
128280304Sjkim            pad[i] = 0x36 ^ ctx->key[i];
129280304Sjkim        if (!EVP_DigestInit_ex(&ctx->i_ctx, md, impl))
130280304Sjkim            goto err;
131280304Sjkim        if (!EVP_DigestUpdate(&ctx->i_ctx, pad, EVP_MD_block_size(md)))
132280304Sjkim            goto err;
13355714Skris
134280304Sjkim        for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
135280304Sjkim            pad[i] = 0x5c ^ ctx->key[i];
136280304Sjkim        if (!EVP_DigestInit_ex(&ctx->o_ctx, md, impl))
137280304Sjkim            goto err;
138280304Sjkim        if (!EVP_DigestUpdate(&ctx->o_ctx, pad, EVP_MD_block_size(md)))
139280304Sjkim            goto err;
140280304Sjkim    }
141280304Sjkim    if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->i_ctx))
142280304Sjkim        goto err;
143280304Sjkim    return 1;
144280304Sjkim err:
145280304Sjkim    return 0;
146280304Sjkim}
14755714Skris
148238405Sjkimint HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
149280304Sjkim{
150280304Sjkim    if (key && md)
151280304Sjkim        HMAC_CTX_init(ctx);
152280304Sjkim    return HMAC_Init_ex(ctx, key, len, md, NULL);
153280304Sjkim}
15455714Skris
155238405Sjkimint HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
156280304Sjkim{
157238405Sjkim#ifdef OPENSSL_FIPS
158280304Sjkim    if (FIPS_mode() && !ctx->i_ctx.engine)
159280304Sjkim        return FIPS_hmac_update(ctx, data, len);
160238405Sjkim#endif
161284330Sjkim    if (!ctx->md)
162284285Sjkim        return 0;
163284285Sjkim
164280304Sjkim    return EVP_DigestUpdate(&ctx->md_ctx, data, len);
165280304Sjkim}
16655714Skris
167238405Sjkimint HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
168280304Sjkim{
169280304Sjkim    unsigned int i;
170280304Sjkim    unsigned char buf[EVP_MAX_MD_SIZE];
171238405Sjkim#ifdef OPENSSL_FIPS
172280304Sjkim    if (FIPS_mode() && !ctx->i_ctx.engine)
173280304Sjkim        return FIPS_hmac_final(ctx, md, len);
174238405Sjkim#endif
17555714Skris
176284330Sjkim    if (!ctx->md)
177284285Sjkim        goto err;
178284285Sjkim
179280304Sjkim    if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i))
180280304Sjkim        goto err;
181280304Sjkim    if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx))
182280304Sjkim        goto err;
183280304Sjkim    if (!EVP_DigestUpdate(&ctx->md_ctx, buf, i))
184280304Sjkim        goto err;
185280304Sjkim    if (!EVP_DigestFinal_ex(&ctx->md_ctx, md, len))
186280304Sjkim        goto err;
187280304Sjkim    return 1;
188280304Sjkim err:
189280304Sjkim    return 0;
190280304Sjkim}
19155714Skris
192109998Smarkmvoid HMAC_CTX_init(HMAC_CTX *ctx)
193280304Sjkim{
194280304Sjkim    EVP_MD_CTX_init(&ctx->i_ctx);
195280304Sjkim    EVP_MD_CTX_init(&ctx->o_ctx);
196280304Sjkim    EVP_MD_CTX_init(&ctx->md_ctx);
197284285Sjkim    ctx->md = NULL;
198280304Sjkim}
19955714Skris
200238405Sjkimint HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
201280304Sjkim{
202280304Sjkim    if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx))
203280304Sjkim        goto err;
204280304Sjkim    if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx))
205280304Sjkim        goto err;
206280304Sjkim    if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
207280304Sjkim        goto err;
208284330Sjkim    memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
209284330Sjkim    dctx->key_length = sctx->key_length;
210280304Sjkim    dctx->md = sctx->md;
211280304Sjkim    return 1;
212280304Sjkim err:
213280304Sjkim    return 0;
214280304Sjkim}
215238405Sjkim
216109998Smarkmvoid HMAC_CTX_cleanup(HMAC_CTX *ctx)
217280304Sjkim{
218238405Sjkim#ifdef OPENSSL_FIPS
219280304Sjkim    if (FIPS_mode() && !ctx->i_ctx.engine) {
220280304Sjkim        FIPS_hmac_ctx_cleanup(ctx);
221280304Sjkim        return;
222280304Sjkim    }
223238405Sjkim#endif
224280304Sjkim    EVP_MD_CTX_cleanup(&ctx->i_ctx);
225280304Sjkim    EVP_MD_CTX_cleanup(&ctx->o_ctx);
226280304Sjkim    EVP_MD_CTX_cleanup(&ctx->md_ctx);
227280304Sjkim    memset(ctx, 0, sizeof *ctx);
228280304Sjkim}
229109998Smarkm
23055714Skrisunsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
231280304Sjkim                    const unsigned char *d, size_t n, unsigned char *md,
232280304Sjkim                    unsigned int *md_len)
233280304Sjkim{
234280304Sjkim    HMAC_CTX c;
235280304Sjkim    static unsigned char m[EVP_MAX_MD_SIZE];
23655714Skris
237280304Sjkim    if (md == NULL)
238280304Sjkim        md = m;
239280304Sjkim    HMAC_CTX_init(&c);
240280304Sjkim    if (!HMAC_Init(&c, key, key_len, evp_md))
241280304Sjkim        goto err;
242280304Sjkim    if (!HMAC_Update(&c, d, n))
243280304Sjkim        goto err;
244280304Sjkim    if (!HMAC_Final(&c, md, md_len))
245280304Sjkim        goto err;
246280304Sjkim    HMAC_CTX_cleanup(&c);
247280304Sjkim    return md;
248280304Sjkim err:
249284285Sjkim    HMAC_CTX_cleanup(&c);
250280304Sjkim    return NULL;
251280304Sjkim}
25255714Skris
253194206Ssimonvoid HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
254280304Sjkim{
255280304Sjkim    EVP_MD_CTX_set_flags(&ctx->i_ctx, flags);
256280304Sjkim    EVP_MD_CTX_set_flags(&ctx->o_ctx, flags);
257280304Sjkim    EVP_MD_CTX_set_flags(&ctx->md_ctx, flags);
258280304Sjkim}
259