165686Smarkm/*-
2255362Smarkm * Copyright (c) 2000-2013 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
39255362Smarkm/* Initialise the hash */
4065686Smarkmvoid
41255362Smarkmrandomdev_hash_init(struct randomdev_hash *context)
4265686Smarkm{
43100082Smarkm	SHA256_Init(&context->sha);
4465686Smarkm}
4565686Smarkm
46255362Smarkm/* Iterate the hash */
4765686Smarkmvoid
48255362Smarkmrandomdev_hash_iterate(struct randomdev_hash *context, void *data, size_t size)
4965686Smarkm{
50100082Smarkm	SHA256_Update(&context->sha, data, size);
5165686Smarkm}
5265686Smarkm
53255362Smarkm/* Conclude by returning the hash in the supplied <*buf> which must be
54103763Smarkm * KEYSIZE bytes long.
5574072Smarkm */
5665686Smarkmvoid
57255362Smarkmrandomdev_hash_finish(struct randomdev_hash *context, void *buf)
5865686Smarkm{
59100082Smarkm	SHA256_Final(buf, &context->sha);
6065686Smarkm}
6165686Smarkm
6274072Smarkm/* Initialise the encryption routine by setting up the key schedule
63255362Smarkm * from the supplied <*data> which must be KEYSIZE bytes of binary
64255362Smarkm * data. Use CBC mode for better avalanche.
6574072Smarkm */
6665686Smarkmvoid
67255362Smarkmrandomdev_encrypt_init(struct randomdev_key *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.
74255362Smarkm * <length> bytes are encrypted from <*d_in> to <*d_out>. <length> must be
75255362Smarkm * a multiple of BLOCKSIZE.
7674072Smarkm */
7765686Smarkmvoid
78255362Smarkmrandomdev_encrypt(struct randomdev_key *context, void *d_in, void *d_out, unsigned length)
7965686Smarkm{
80255362Smarkm	rijndael_blockEncrypt(&context->cipher, &context->key, d_in, length*8, d_out);
8165686Smarkm}
82