summaryrefslogtreecommitdiff
path: root/keyexchange/isakmpd-20041012/hash.c
blob: 84773f875b03c97ef2c555233a8932e4329026bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* $OpenBSD: hash.c,v 1.17 2004/06/14 09:55:41 ho Exp $	 */
/* $EOM: hash.c,v 1.10 1999/04/17 23:20:34 niklas Exp $	 */

/*
 * Copyright (c) 1998 Niels Provos.  All rights reserved.
 * Copyright (c) 1999 Niklas Hallqvist.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This code was written under funding by Ericsson Radio Systems.
 */

#include <sys/param.h>
#include <string.h>
#if defined (__APPLE__)
#include <openssl/md5.h>
#include <openssl/sha.h>
#else
#include <md5.h>
#include <sha1.h>
#endif				/* __APPLE__ */

#include "sysdep.h"

#include "hash.h"
#include "log.h"

void	hmac_init(struct hash *, unsigned char *, unsigned int);
void	hmac_final(unsigned char *, struct hash *);

/* Temporary hash contexts.  */
static union {
	MD5_CTX		md5ctx;
	SHA1_CTX        sha1ctx;
} Ctx, Ctx2;

/* Temporary hash digest.  */
static unsigned char digest[HASH_MAX];

/* Encapsulation of hash functions.  */

static struct hash hashes[] = {
    {
	HASH_MD5, 5, MD5_SIZE, (void *)&Ctx.md5ctx, digest,
	sizeof(MD5_CTX), (void *)&Ctx2.md5ctx,
	(void (*)(void *))MD5Init,
	(void (*)(void *, unsigned char *, unsigned int))MD5Update,
	(void (*)(unsigned char *, void *))MD5Final,
	hmac_init,
	hmac_final
    }, {
	HASH_SHA1, 6, SHA1_SIZE, (void *)&Ctx.sha1ctx, digest,
	sizeof(SHA1_CTX), (void *)&Ctx2.sha1ctx,
	(void (*)(void *))SHA1Init,
	(void (*)(void *, unsigned char *, unsigned int))SHA1Update,
	(void (*)(unsigned char *, void *))SHA1Final,
	hmac_init,
	hmac_final
    },
};

struct hash *
hash_get(enum hashes hashtype)
{
	size_t	i;

	LOG_DBG((LOG_CRYPTO, 60, "hash_get: requested algorithm %d",
	    hashtype));

	for (i = 0; i < sizeof hashes / sizeof hashes[0]; i++)
		if (hashtype == hashes[i].type)
			return &hashes[i];

	return 0;
}

/*
 * Initial a hash for HMAC usage this requires a special init function.
 * ctx, ctx2 hold the contexts, if you want to use the hash object for
 * something else in the meantime, be sure to store the contexts somewhere.
 */

void
hmac_init(struct hash *hash, unsigned char *okey, unsigned int len)
{
	unsigned int    i, blocklen = HMAC_BLOCKLEN;
	unsigned char   key[HMAC_BLOCKLEN];

	memset(key, 0, blocklen);
	if (len > blocklen) {
		/* Truncate key down to blocklen */
		hash->Init(hash->ctx);
		hash->Update(hash->ctx, okey, len);
		hash->Final(key, hash->ctx);
	} else {
		memcpy(key, okey, len);
	}

	/* HMAC I and O pad computation */
	for (i = 0; i < blocklen; i++)
		key[i] ^= HMAC_IPAD_VAL;

	hash->Init(hash->ctx);
	hash->Update(hash->ctx, key, blocklen);

	for (i = 0; i < blocklen; i++)
		key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);

	hash->Init(hash->ctx2);
	hash->Update(hash->ctx2, key, blocklen);

	memset(key, 0, blocklen);
}

/*
 * HMAC Final function
 */

void
hmac_final(unsigned char *dgst, struct hash *hash)
{
	hash->Final(dgst, hash->ctx);
	hash->Update(hash->ctx2, dgst, hash->hashsize);
	hash->Final(dgst, hash->ctx2);
}