aboutsummaryrefslogtreecommitdiff
path: root/sys/dev
diff options
context:
space:
mode:
authorConrad Meyer <cem@FreeBSD.org>2019-03-01 19:21:45 +0000
committerConrad Meyer <cem@FreeBSD.org>2019-03-01 19:21:45 +0000
commit51c68d18e2580e5a3949b8c1b331125b71325e0b (patch)
tree8866462e4c6c41057aba675cf35bf0edcc6769bb /sys/dev
parent65847dc90afd2bc038b477742dfaf4a48c331a8e (diff)
downloadsrc-51c68d18e2580e5a3949b8c1b331125b71325e0b.tar.gz
src-51c68d18e2580e5a3949b8c1b331125b71325e0b.zip
Fortuna: push CTR-mode loop down into randomdev hash.h interface
As a step towards adding other potential streaming ciphers. As well as just pushing the loop down into the rijndael APIs (basically 128-bit wide AES-ICM mode) to eliminate some excess explicit_bzero(). No functional change intended. Reviewed by: delphij, markm Approved by: secteam (delphij) Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D19411
Notes
Notes: svn path=/head/; revision=344710
Diffstat (limited to 'sys/dev')
-rw-r--r--sys/dev/random/fortuna.c16
-rw-r--r--sys/dev/random/hash.c23
-rw-r--r--sys/dev/random/hash.h4
3 files changed, 27 insertions, 16 deletions
diff --git a/sys/dev/random/fortuna.c b/sys/dev/random/fortuna.c
index 3a46d527fa9a..4c7ccc13309d 100644
--- a/sys/dev/random/fortuna.c
+++ b/sys/dev/random/fortuna.c
@@ -308,20 +308,16 @@ random_fortuna_reseed_internal(uint32_t *entropy_data, u_int blockcount)
static __inline void
random_fortuna_genblocks(uint8_t *buf, u_int blockcount)
{
- u_int i;
RANDOM_RESEED_ASSERT_LOCK_OWNED();
KASSERT(!uint128_is_zero(fortuna_state.fs_counter), ("FS&K: C != 0"));
- for (i = 0; i < blockcount; i++) {
- /*-
- * FS&K - r = r|E(K,C)
- * - C = C + 1
- */
- randomdev_encrypt(&fortuna_state.fs_key, &fortuna_state.fs_counter, buf, RANDOM_BLOCKSIZE);
- buf += RANDOM_BLOCKSIZE;
- uint128_increment(&fortuna_state.fs_counter);
- }
+ /*
+ * Fills buf with RANDOM_BLOCKSIZE * blockcount bytes of keystream.
+ * Increments fs_counter as it goes.
+ */
+ randomdev_keystream(&fortuna_state.fs_key, &fortuna_state.fs_counter,
+ buf, blockcount);
}
/*-
diff --git a/sys/dev/random/hash.c b/sys/dev/random/hash.c
index fe52cd68b848..0bad46519f50 100644
--- a/sys/dev/random/hash.c
+++ b/sys/dev/random/hash.c
@@ -88,13 +88,26 @@ randomdev_encrypt_init(struct randomdev_key *context, const void *data)
rijndael_makeKey(&context->key, DIR_ENCRYPT, RANDOM_KEYSIZE*8, data);
}
-/* Encrypt the supplied data using the key schedule preset in the context.
- * <length> bytes are encrypted from <*d_in> to <*d_out>. <length> must be
- * a multiple of RANDOM_BLOCKSIZE.
+/*
+ * Create a psuedorandom output stream of 'blockcount' blocks using a CTR-mode
+ * cipher or similar. The 128-bit counter is supplied in the in-out parmeter
+ * 'ctr.' The output stream goes to 'd_out.' 'blockcount' RANDOM_BLOCKSIZE
+ * bytes are generated.
*/
void
-randomdev_encrypt(struct randomdev_key *context, const void *d_in, void *d_out, u_int length)
+randomdev_keystream(struct randomdev_key *context, uint128_t *ctr,
+ void *d_out, u_int blockcount)
{
+ u_int i;
- rijndael_blockEncrypt(&context->cipher, &context->key, d_in, length*8, d_out);
+ for (i = 0; i < blockcount; i++) {
+ /*-
+ * FS&K - r = r|E(K,C)
+ * - C = C + 1
+ */
+ rijndael_blockEncrypt(&context->cipher, &context->key,
+ (void *)ctr, RANDOM_BLOCKSIZE * 8, d_out);
+ d_out = (char *)d_out + RANDOM_BLOCKSIZE;
+ uint128_increment(ctr);
+ }
}
diff --git a/sys/dev/random/hash.h b/sys/dev/random/hash.h
index 41dcf9089f0e..ff24d3fb802d 100644
--- a/sys/dev/random/hash.h
+++ b/sys/dev/random/hash.h
@@ -29,6 +29,8 @@
#ifndef SYS_DEV_RANDOM_HASH_H_INCLUDED
#define SYS_DEV_RANDOM_HASH_H_INCLUDED
+#include <dev/random/uint128.h>
+
/* Keys are formed from cipher blocks */
#define RANDOM_KEYSIZE 32 /* (in bytes) == 256 bits */
#define RANDOM_KEYSIZE_WORDS (RANDOM_KEYSIZE/sizeof(uint32_t))
@@ -52,6 +54,6 @@ void randomdev_hash_init(struct randomdev_hash *);
void randomdev_hash_iterate(struct randomdev_hash *, const void *, size_t);
void randomdev_hash_finish(struct randomdev_hash *, void *);
void randomdev_encrypt_init(struct randomdev_key *, const void *);
-void randomdev_encrypt(struct randomdev_key *context, const void *, void *, u_int);
+void randomdev_keystream(struct randomdev_key *context, uint128_t *, void *, u_int);
#endif /* SYS_DEV_RANDOM_HASH_H_INCLUDED */