[Crypto] Use enc/srng

This commit is contained in:
Peter Taoussanis 2020-06-24 12:28:20 +02:00
parent 649e140889
commit f1c71b58d8
2 changed files with 10 additions and 24 deletions

View file

@ -24,8 +24,8 @@
LazySeq IRecord ISeq IType]))
(if (vector? enc/encore-version)
(enc/assert-min-encore-version [2 67 1])
(enc/assert-min-encore-version 2.67))
(enc/assert-min-encore-version [2 121 0])
(enc/assert-min-encore-version 2.121))
(comment
(set! *unchecked-math* :warn-on-boxed)

View file

@ -11,28 +11,14 @@
;;;; Randomness
(do
(enc/compile-if (fn [] (java.security.SecureRandom/getInstanceStrong)) ; Java 8+, blocking
(def ^:private srng* (enc/thread-local-proxy (java.security.SecureRandom/getInstanceStrong)))
(def ^:private srng* (enc/thread-local-proxy (java.security.SecureRandom/getInstance "SHA1SRNG"))))
(defn srng
"Favours security over performance. May block while waiting on system entropy!"
^java.security.SecureRandom []
(let [rng ^java.security.SecureRandom (.get ^ThreadLocal srng*)]
;; Occasionally supplement current seed for extra security.
;; Otherwise an attacker could *theoretically* observe large amounts of
;; srng output to determine initial seed, Ref. https://goo.gl/MPM91w
(when (< (.nextDouble rng) 2.44140625E-4) (.setSeed rng (.generateSeed rng 8)))
rng))
(defn rand-nth "Uses `srng`" [coll] (nth coll (int (* (.nextDouble (srng)) (count coll)))))
(defn rand-bytes "Uses `srng`" ^bytes [size] (let [ba (byte-array size)] (.nextBytes (srng) ba) ba))
(defn rand-double "Uses `srng`" ^double [] (.nextDouble (srng)))
(defn rand-gauss "Uses `srng`" ^double [] (.nextGaussian (srng)))
(defn rand-bool "Uses `srng`" [] (.nextBoolean (srng)))
(defn rand-long "Uses `srng`"
(^long [ ] (.nextLong (srng)))
(^long [n] (long (* (long n) (.nextDouble (srng)))))))
(defn rand-nth [coll] (nth coll (int (* (.nextDouble (enc/srng)) (count coll)))))
(defn rand-bytes ^bytes [size] (let [ba (byte-array size)] (.nextBytes (enc/srng) ba) ba))
(defn rand-double ^double [] (.nextDouble (enc/srng)))
(defn rand-gauss ^double [] (.nextGaussian (enc/srng)))
(defn rand-bool [] (.nextBoolean (enc/srng)))
(defn rand-long
(^long [ ] (.nextLong (enc/srng)))
(^long [n] (long (* (long n) (.nextDouble (enc/srng)))))))
(comment
(seq (rand-bytes 16))