xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/hmac.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: hmac.c,v 1.1.1.1 2011/04/13 18:14:50 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #include <sys/types.h>
37*ebfedea0SLionel Sambuc #include <stdio.h>
38*ebfedea0SLionel Sambuc #include <stdlib.h>
39*ebfedea0SLionel Sambuc #include <string.h>
40*ebfedea0SLionel Sambuc #include <hmac.h>
41*ebfedea0SLionel Sambuc 
42*ebfedea0SLionel Sambuc void
HMAC_CTX_init(HMAC_CTX * ctx)43*ebfedea0SLionel Sambuc HMAC_CTX_init(HMAC_CTX *ctx)
44*ebfedea0SLionel Sambuc {
45*ebfedea0SLionel Sambuc     memset(ctx, 0, sizeof(*ctx));
46*ebfedea0SLionel Sambuc }
47*ebfedea0SLionel Sambuc 
48*ebfedea0SLionel Sambuc void
HMAC_CTX_cleanup(HMAC_CTX * ctx)49*ebfedea0SLionel Sambuc HMAC_CTX_cleanup(HMAC_CTX *ctx)
50*ebfedea0SLionel Sambuc {
51*ebfedea0SLionel Sambuc     if (ctx->buf) {
52*ebfedea0SLionel Sambuc 	memset(ctx->buf, 0, ctx->key_length);
53*ebfedea0SLionel Sambuc 	free(ctx->buf);
54*ebfedea0SLionel Sambuc 	ctx->buf = NULL;
55*ebfedea0SLionel Sambuc     }
56*ebfedea0SLionel Sambuc     if (ctx->opad) {
57*ebfedea0SLionel Sambuc 	memset(ctx->opad, 0, EVP_MD_block_size(ctx->md));
58*ebfedea0SLionel Sambuc 	free(ctx->opad);
59*ebfedea0SLionel Sambuc 	ctx->opad = NULL;
60*ebfedea0SLionel Sambuc     }
61*ebfedea0SLionel Sambuc     if (ctx->ipad) {
62*ebfedea0SLionel Sambuc 	memset(ctx->ipad, 0, EVP_MD_block_size(ctx->md));
63*ebfedea0SLionel Sambuc 	free(ctx->ipad);
64*ebfedea0SLionel Sambuc 	ctx->ipad = NULL;
65*ebfedea0SLionel Sambuc     }
66*ebfedea0SLionel Sambuc     if (ctx->ctx) {
67*ebfedea0SLionel Sambuc 	EVP_MD_CTX_destroy(ctx->ctx);
68*ebfedea0SLionel Sambuc 	ctx->ctx = NULL;
69*ebfedea0SLionel Sambuc     }
70*ebfedea0SLionel Sambuc }
71*ebfedea0SLionel Sambuc 
72*ebfedea0SLionel Sambuc size_t
HMAC_size(const HMAC_CTX * ctx)73*ebfedea0SLionel Sambuc HMAC_size(const HMAC_CTX *ctx)
74*ebfedea0SLionel Sambuc {
75*ebfedea0SLionel Sambuc     return EVP_MD_size(ctx->md);
76*ebfedea0SLionel Sambuc }
77*ebfedea0SLionel Sambuc 
78*ebfedea0SLionel Sambuc void
HMAC_Init_ex(HMAC_CTX * ctx,const void * key,size_t keylen,const EVP_MD * md,ENGINE * engine)79*ebfedea0SLionel Sambuc HMAC_Init_ex(HMAC_CTX *ctx,
80*ebfedea0SLionel Sambuc 	     const void *key,
81*ebfedea0SLionel Sambuc 	     size_t keylen,
82*ebfedea0SLionel Sambuc 	     const EVP_MD *md,
83*ebfedea0SLionel Sambuc 	     ENGINE *engine)
84*ebfedea0SLionel Sambuc {
85*ebfedea0SLionel Sambuc     unsigned char *p;
86*ebfedea0SLionel Sambuc     size_t i;
87*ebfedea0SLionel Sambuc 
88*ebfedea0SLionel Sambuc     if (ctx->md != md) {
89*ebfedea0SLionel Sambuc 	ctx->md = md;
90*ebfedea0SLionel Sambuc 	if (ctx->buf) {
91*ebfedea0SLionel Sambuc 	    memset(ctx->buf, 0, ctx->key_length);
92*ebfedea0SLionel Sambuc 	    free (ctx->buf);
93*ebfedea0SLionel Sambuc 	}
94*ebfedea0SLionel Sambuc 	ctx->key_length = EVP_MD_size(ctx->md);
95*ebfedea0SLionel Sambuc 	ctx->buf = malloc(ctx->key_length);
96*ebfedea0SLionel Sambuc     }
97*ebfedea0SLionel Sambuc #if 0
98*ebfedea0SLionel Sambuc     ctx->engine = engine;
99*ebfedea0SLionel Sambuc #endif
100*ebfedea0SLionel Sambuc 
101*ebfedea0SLionel Sambuc     if (keylen > EVP_MD_block_size(ctx->md)) {
102*ebfedea0SLionel Sambuc 	EVP_Digest(key, keylen, ctx->buf, NULL, ctx->md, engine);
103*ebfedea0SLionel Sambuc 	key = ctx->buf;
104*ebfedea0SLionel Sambuc 	keylen = EVP_MD_size(ctx->md);
105*ebfedea0SLionel Sambuc     }
106*ebfedea0SLionel Sambuc 
107*ebfedea0SLionel Sambuc     if (ctx->opad) {
108*ebfedea0SLionel Sambuc 	memset(ctx->opad, 0, ctx->key_length);
109*ebfedea0SLionel Sambuc 	free(ctx->opad);
110*ebfedea0SLionel Sambuc     }
111*ebfedea0SLionel Sambuc     if (ctx->ipad) {
112*ebfedea0SLionel Sambuc 	memset(ctx->ipad, 0, ctx->key_length);
113*ebfedea0SLionel Sambuc 	free(ctx->ipad);
114*ebfedea0SLionel Sambuc     }
115*ebfedea0SLionel Sambuc 
116*ebfedea0SLionel Sambuc     ctx->opad = malloc(EVP_MD_block_size(ctx->md));
117*ebfedea0SLionel Sambuc     ctx->ipad = malloc(EVP_MD_block_size(ctx->md));
118*ebfedea0SLionel Sambuc     memset(ctx->ipad, 0x36, EVP_MD_block_size(ctx->md));
119*ebfedea0SLionel Sambuc     memset(ctx->opad, 0x5c, EVP_MD_block_size(ctx->md));
120*ebfedea0SLionel Sambuc 
121*ebfedea0SLionel Sambuc     for (i = 0, p = ctx->ipad; i < keylen; i++)
122*ebfedea0SLionel Sambuc 	p[i] ^= ((const unsigned char *)key)[i];
123*ebfedea0SLionel Sambuc     for (i = 0, p = ctx->opad; i < keylen; i++)
124*ebfedea0SLionel Sambuc 	p[i] ^= ((const unsigned char *)key)[i];
125*ebfedea0SLionel Sambuc 
126*ebfedea0SLionel Sambuc     if (ctx->ctx == NULL)
127*ebfedea0SLionel Sambuc 	ctx->ctx = EVP_MD_CTX_create();
128*ebfedea0SLionel Sambuc 
129*ebfedea0SLionel Sambuc     EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
130*ebfedea0SLionel Sambuc     EVP_DigestUpdate(ctx->ctx, ctx->ipad, EVP_MD_block_size(ctx->md));
131*ebfedea0SLionel Sambuc }
132*ebfedea0SLionel Sambuc 
133*ebfedea0SLionel Sambuc void
HMAC_Update(HMAC_CTX * ctx,const void * data,size_t len)134*ebfedea0SLionel Sambuc HMAC_Update(HMAC_CTX *ctx, const void *data, size_t len)
135*ebfedea0SLionel Sambuc {
136*ebfedea0SLionel Sambuc     EVP_DigestUpdate(ctx->ctx, data, len);
137*ebfedea0SLionel Sambuc }
138*ebfedea0SLionel Sambuc 
139*ebfedea0SLionel Sambuc void
HMAC_Final(HMAC_CTX * ctx,void * md,unsigned int * len)140*ebfedea0SLionel Sambuc HMAC_Final(HMAC_CTX *ctx, void *md, unsigned int *len)
141*ebfedea0SLionel Sambuc {
142*ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(ctx->ctx, ctx->buf, NULL);
143*ebfedea0SLionel Sambuc 
144*ebfedea0SLionel Sambuc     EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
145*ebfedea0SLionel Sambuc     EVP_DigestUpdate(ctx->ctx, ctx->opad, EVP_MD_block_size(ctx->md));
146*ebfedea0SLionel Sambuc     EVP_DigestUpdate(ctx->ctx, ctx->buf, ctx->key_length);
147*ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(ctx->ctx, md, len);
148*ebfedea0SLionel Sambuc }
149*ebfedea0SLionel Sambuc 
150*ebfedea0SLionel Sambuc void *
HMAC(const EVP_MD * md,const void * key,size_t key_size,const void * data,size_t data_size,void * hash,unsigned int * hash_len)151*ebfedea0SLionel Sambuc HMAC(const EVP_MD *md,
152*ebfedea0SLionel Sambuc      const void *key, size_t key_size,
153*ebfedea0SLionel Sambuc      const void *data, size_t data_size,
154*ebfedea0SLionel Sambuc      void *hash, unsigned int *hash_len)
155*ebfedea0SLionel Sambuc {
156*ebfedea0SLionel Sambuc     HMAC_CTX ctx;
157*ebfedea0SLionel Sambuc 
158*ebfedea0SLionel Sambuc     HMAC_CTX_init(&ctx);
159*ebfedea0SLionel Sambuc     HMAC_Init_ex(&ctx, key, key_size, md, NULL);
160*ebfedea0SLionel Sambuc     HMAC_Update(&ctx, data, data_size);
161*ebfedea0SLionel Sambuc     HMAC_Final(&ctx, hash, hash_len);
162*ebfedea0SLionel Sambuc     HMAC_CTX_cleanup(&ctx);
163*ebfedea0SLionel Sambuc     return hash;
164*ebfedea0SLionel Sambuc }
165