Author: cem
Date: Fri Mar  1 19:21:45 2019
New Revision: 344710
URL: https://svnweb.freebsd.org/changeset/base/344710

Log:
  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

Modified:
  head/sys/dev/random/fortuna.c
  head/sys/dev/random/hash.c
  head/sys/dev/random/hash.h

Modified: head/sys/dev/random/fortuna.c
==============================================================================
--- head/sys/dev/random/fortuna.c       Fri Mar  1 19:06:13 2019        
(r344709)
+++ head/sys/dev/random/fortuna.c       Fri Mar  1 19:21:45 2019        
(r344710)
@@ -308,20 +308,16 @@ random_fortuna_reseed_internal(uint32_t *entropy_data,
 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);
 }
 
 /*-

Modified: head/sys/dev/random/hash.c
==============================================================================
--- head/sys/dev/random/hash.c  Fri Mar  1 19:06:13 2019        (r344709)
+++ head/sys/dev/random/hash.c  Fri Mar  1 19:21:45 2019        (r344710)
@@ -88,13 +88,26 @@ randomdev_encrypt_init(struct randomdev_key *context, 
        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);
+       }
 }

Modified: head/sys/dev/random/hash.h
==============================================================================
--- head/sys/dev/random/hash.h  Fri Mar  1 19:06:13 2019        (r344709)
+++ head/sys/dev/random/hash.h  Fri Mar  1 19:21:45 2019        (r344710)
@@ -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 */
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "[email protected]"

Reply via email to