hash.c revision 69168
1/*-
2 * Copyright (c) 2000 Mark R V Murray
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/random/hash.c 69168 2000-11-25 17:09:01Z markm $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/queue.h>
32#include <sys/libkern.h>
33#include <sys/random.h>
34#include <sys/types.h>
35
36#include <crypto/blowfish/blowfish.h>
37
38#include <dev/random/hash.h>
39
40/* initialise the hash by copying in some supplied data */
41void
42yarrow_hash_init(struct yarrowhash *context, void *data, size_t size)
43{
44	size_t count;
45
46	count = size > KEYSIZE ? KEYSIZE : size;
47	memset(context->hash, 0xff, KEYSIZE);
48	memcpy(context->hash, data, count);
49}
50
51/* Do a Davies-Meyer hash using a block cipher.
52 * H_0 = I
53 * H_i = E_M_i(H_i-1) ^ H_i-1
54 */
55void
56yarrow_hash_iterate(struct yarrowhash *context, void *data, size_t size)
57{
58	u_char keybuffer[KEYSIZE], temp[KEYSIZE];
59	size_t count;
60	int iteration, last, i;
61
62	iteration = 0;
63	last = 0;
64	for (;;) {
65		if (size <= KEYSIZE)
66			last = 1;
67		count = size > KEYSIZE ? KEYSIZE : size;
68		memcpy(keybuffer, &((u_char *)data)[iteration], count);
69		memset(&keybuffer[KEYSIZE - count], 0xff, count);
70		BF_set_key(&context->hashkey, count,
71			&((u_char *)data)[iteration]);
72		BF_cbc_encrypt(context->hash, temp, KEYSIZE, &context->hashkey,
73			context->ivec, BF_ENCRYPT);
74		for (i = 0; i < KEYSIZE; i++)
75			context->hash[i] ^= temp[i];
76		if (last)
77			break;
78		iteration += KEYSIZE;
79		size -= KEYSIZE;
80	}
81}
82
83/* Conclude by returning a pointer to the data */
84void
85yarrow_hash_finish(struct yarrowhash *context, void *buf)
86{
87	memcpy(buf, context->hash, sizeof(context->hash));
88}
89
90/* Initialise the encryption routine by setting up the key schedule */
91void
92yarrow_encrypt_init(struct yarrowkey *context, void *data, size_t size)
93{
94	size_t count;
95
96	count = size > KEYSIZE ? KEYSIZE : size;
97	BF_set_key(&context->key, size, data);
98}
99
100/* Encrypt the supplied data using the key schedule preset in the context */
101void
102yarrow_encrypt(struct yarrowkey *context, void *d_in, void *d_out, size_t size)
103{
104	size_t count;
105	int iteration, last;
106
107	last = 0;
108	for (iteration = 0;; iteration += KEYSIZE) {
109		if (size <= KEYSIZE)
110			last = 1;
111		count = size > KEYSIZE ? KEYSIZE : size;
112		BF_cbc_encrypt(&((u_char *)d_in)[iteration],
113			&((u_char *)d_out)[iteration], count, &context->key,
114			context->ivec, BF_ENCRYPT);
115		if (last)
116			break;
117		size -= KEYSIZE;
118	}
119}
120