165686Smarkm/*- 2128059Smarkm * Copyright (c) 2000-2004 Mark R V Murray 365686Smarkm * All rights reserved. 465686Smarkm * 565686Smarkm * Redistribution and use in source and binary forms, with or without 665686Smarkm * modification, are permitted provided that the following conditions 765686Smarkm * are met: 865686Smarkm * 1. Redistributions of source code must retain the above copyright 965686Smarkm * notice, this list of conditions and the following disclaimer 1065686Smarkm * in this position and unchanged. 1165686Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1265686Smarkm * notice, this list of conditions and the following disclaimer in the 1365686Smarkm * documentation and/or other materials provided with the distribution. 1465686Smarkm * 1565686Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1665686Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1765686Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1865686Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1965686Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2065686Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2165686Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2265686Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2365686Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2465686Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2565686Smarkm * 2665686Smarkm */ 2765686Smarkm 28119418Sobrien#include <sys/cdefs.h> 29119418Sobrien__FBSDID("$FreeBSD$"); 30119418Sobrien 3165686Smarkm#include <sys/param.h> 3265686Smarkm#include <sys/systm.h> 3369168Smarkm 34143418Sume#include <crypto/rijndael/rijndael-api-fst.h> 35100082Smarkm#include <crypto/sha2/sha2.h> 3665686Smarkm 3767112Smarkm#include <dev/random/hash.h> 3865686Smarkm 39100082Smarkm/* initialise the hash */ 4065686Smarkmvoid 4174072Smarkmyarrow_hash_init(struct yarrowhash *context) 4265686Smarkm{ 43100082Smarkm SHA256_Init(&context->sha); 4465686Smarkm} 4565686Smarkm 46100082Smarkm/* iterate the hash */ 4765686Smarkmvoid 4865686Smarkmyarrow_hash_iterate(struct yarrowhash *context, void *data, size_t size) 4965686Smarkm{ 50100082Smarkm SHA256_Update(&context->sha, data, size); 5165686Smarkm} 5265686Smarkm 5374072Smarkm/* Conclude by returning the hash in the supplied /buf/ which must be 54103763Smarkm * KEYSIZE bytes long. 5574072Smarkm */ 5665686Smarkmvoid 5765686Smarkmyarrow_hash_finish(struct yarrowhash *context, void *buf) 5865686Smarkm{ 59100082Smarkm SHA256_Final(buf, &context->sha); 6065686Smarkm} 6165686Smarkm 6274072Smarkm/* Initialise the encryption routine by setting up the key schedule 63103763Smarkm * from the supplied /data/ which must be KEYSIZE bytes of binary 6474072Smarkm * data. 6574072Smarkm */ 6665686Smarkmvoid 6774072Smarkmyarrow_encrypt_init(struct yarrowkey *context, void *data) 6865686Smarkm{ 6974072Smarkm rijndael_cipherInit(&context->cipher, MODE_CBC, NULL); 7074072Smarkm rijndael_makeKey(&context->key, DIR_ENCRYPT, KEYSIZE*8, data); 7165686Smarkm} 7265686Smarkm 7374072Smarkm/* Encrypt the supplied data using the key schedule preset in the context. 7474072Smarkm * KEYSIZE bytes are encrypted from /d_in/ to /d_out/. 7574072Smarkm */ 7665686Smarkmvoid 7774072Smarkmyarrow_encrypt(struct yarrowkey *context, void *d_in, void *d_out) 7865686Smarkm{ 7974072Smarkm rijndael_blockEncrypt(&context->cipher, &context->key, d_in, 80103763Smarkm KEYSIZE*8, d_out); 8165686Smarkm} 82