hash.c revision 65686
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 65686 2000-09-10 13:52:19Z 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/mbuf.h> 34#include <sys/random.h> 35#include <sys/time.h> 36#include <sys/types.h> 37#include <crypto/blowfish/blowfish.h> 38 39#include <dev/randomdev/hash.h> 40#include <dev/randomdev/yarrow.h> 41 42/* initialise the hash by copying in some supplied data */ 43void 44yarrow_hash_init(struct yarrowhash *context, void *data, size_t size) 45{ 46 size_t count; 47 48 count = size > KEYSIZE ? KEYSIZE : size; 49 memset(context->hash, 0xff, KEYSIZE); 50 memcpy(context->hash, data, count); 51} 52 53/* Do a Davies-Meyer hash using a block cipher. 54 * H_0 = I 55 * H_i = E_M_i(H_i-1) ^ H_i-1 56 */ 57void 58yarrow_hash_iterate(struct yarrowhash *context, void *data, size_t size) 59{ 60 u_char keybuffer[KEYSIZE], temp[KEYSIZE]; 61 size_t count; 62 int iteration, last, i; 63 64 iteration = 0; 65 last = 0; 66 for (;;) { 67 if (size <= KEYSIZE) 68 last = 1; 69 count = size > KEYSIZE ? KEYSIZE : size; 70 memcpy(keybuffer, &((u_char *)data)[iteration], count); 71 memset(&keybuffer[KEYSIZE - count], 0xff, count); 72 BF_set_key(&context->hashkey, count, 73 &((u_char *)data)[iteration]); 74 BF_cbc_encrypt(context->hash, temp, KEYSIZE, &context->hashkey, 75 context->ivec, BF_ENCRYPT); 76 for (i = 0; i < KEYSIZE; i++) 77 context->hash[i] ^= temp[i]; 78 if (last) 79 break; 80 iteration += KEYSIZE; 81 size -= KEYSIZE; 82 } 83} 84 85/* Conclude by returning a pointer to the data */ 86void 87yarrow_hash_finish(struct yarrowhash *context, void *buf) 88{ 89 memcpy(buf, context->hash, sizeof(context->hash)); 90} 91 92/* Initialise the encryption routine by setting up the key schedule */ 93void 94yarrow_encrypt_init(struct yarrowkey *context, void *data, size_t size) 95{ 96 size_t count; 97 98 count = size > KEYSIZE ? KEYSIZE : size; 99 BF_set_key(&context->key, size, data); 100} 101 102/* Encrypt the supplied data using the key schedule preset in the context */ 103void 104yarrow_encrypt(struct yarrowkey *context, void *d_in, void *d_out, size_t size) 105{ 106 size_t count; 107 int iteration, last; 108 109 last = 0; 110 for (iteration = 0;; iteration += KEYSIZE) { 111 if (size <= KEYSIZE) 112 last = 1; 113 count = size > KEYSIZE ? KEYSIZE : size; 114 BF_cbc_encrypt(&((u_char *)d_in)[iteration], 115 &((u_char *)d_out)[iteration], count, &context->key, 116 context->ivec, BF_ENCRYPT); 117 if (last) 118 break; 119 size -= KEYSIZE; 120 } 121} 122