1 /* $OpenBSD: hash.c,v 1.20 2005/04/08 22:32:10 cloder Exp $	 */
2 /* $EOM: hash.c,v 1.10 1999/04/17 23:20:34 niklas Exp $	 */
3 
4 /*
5  * Copyright (c) 1998 Niels Provos.  All rights reserved.
6  * Copyright (c) 1999 Niklas Hallqvist.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * This code was written under funding by Ericsson Radio Systems.
31  */
32 
33 #include <sys/param.h>
34 #include <string.h>
35 #include <md5.h>
36 #include <sha1.h>
37 
38 #include "hash.h"
39 #include "log.h"
40 
41 void	hmac_init(struct hash *, unsigned char *, unsigned int);
42 void	hmac_final(unsigned char *, struct hash *);
43 
44 /* Temporary hash contexts.  */
45 static union {
46 	MD5_CTX		md5ctx;
47 	SHA1_CTX        sha1ctx;
48 } Ctx, Ctx2;
49 
50 /* Temporary hash digest.  */
51 static unsigned char digest[HASH_MAX];
52 
53 /* Encapsulation of hash functions.  */
54 
55 static struct hash hashes[] = {
56     {
57 	HASH_MD5, 5, MD5_SIZE, (void *)&Ctx.md5ctx, digest,
58 	sizeof(MD5_CTX), (void *)&Ctx2.md5ctx,
59 	(void (*)(void *))MD5Init,
60 	(void (*)(void *, unsigned char *, unsigned int))MD5Update,
61 	(void (*)(unsigned char *, void *))MD5Final,
62 	hmac_init,
63 	hmac_final
64     }, {
65 	HASH_SHA1, 6, SHA1_SIZE, (void *)&Ctx.sha1ctx, digest,
66 	sizeof(SHA1_CTX), (void *)&Ctx2.sha1ctx,
67 	(void (*)(void *))SHA1Init,
68 	(void (*)(void *, unsigned char *, unsigned int))SHA1Update,
69 	(void (*)(unsigned char *, void *))SHA1Final,
70 	hmac_init,
71 	hmac_final
72     },
73 };
74 
75 struct hash *
hash_get(enum hashes hashtype)76 hash_get(enum hashes hashtype)
77 {
78 	size_t	i;
79 
80 	LOG_DBG((LOG_CRYPTO, 60, "hash_get: requested algorithm %d",
81 	    hashtype));
82 
83 	for (i = 0; i < sizeof hashes / sizeof hashes[0]; i++)
84 		if (hashtype == hashes[i].type)
85 			return &hashes[i];
86 
87 	return 0;
88 }
89 
90 /*
91  * Initial a hash for HMAC usage this requires a special init function.
92  * ctx, ctx2 hold the contexts, if you want to use the hash object for
93  * something else in the meantime, be sure to store the contexts somewhere.
94  */
95 
96 void
hmac_init(struct hash * hash,unsigned char * okey,unsigned int len)97 hmac_init(struct hash *hash, unsigned char *okey, unsigned int len)
98 {
99 	unsigned int    i, blocklen = HMAC_BLOCKLEN;
100 	unsigned char   key[HMAC_BLOCKLEN];
101 
102 	bzero(key, blocklen);
103 	if (len > blocklen) {
104 		/* Truncate key down to blocklen */
105 		hash->Init(hash->ctx);
106 		hash->Update(hash->ctx, okey, len);
107 		hash->Final(key, hash->ctx);
108 	} else {
109 		memcpy(key, okey, len);
110 	}
111 
112 	/* HMAC I and O pad computation */
113 	for (i = 0; i < blocklen; i++)
114 		key[i] ^= HMAC_IPAD_VAL;
115 
116 	hash->Init(hash->ctx);
117 	hash->Update(hash->ctx, key, blocklen);
118 
119 	for (i = 0; i < blocklen; i++)
120 		key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
121 
122 	hash->Init(hash->ctx2);
123 	hash->Update(hash->ctx2, key, blocklen);
124 
125 	bzero(key, blocklen);
126 }
127 
128 /*
129  * HMAC Final function
130  */
131 
132 void
hmac_final(unsigned char * dgst,struct hash * hash)133 hmac_final(unsigned char *dgst, struct hash *hash)
134 {
135 	hash->Final(dgst, hash->ctx);
136 	hash->Update(hash->ctx2, dgst, hash->hashsize);
137 	hash->Final(dgst, hash->ctx2);
138 }
139