prng.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /*
  20. Package prng implements a seeded, unbiased PRNG that is suitable for use
  21. cases including obfuscation, network jitter, load balancing.
  22. Seeding is based on crypto/rand.Read and the PRNG stream is provided by
  23. chacha20. As such, this PRNG is suitable for high volume cases such as
  24. generating random bytes per IP packet as it avoids the syscall overhead
  25. (context switch/spinlock) of crypto/rand.Read.
  26. This PRNG also supports replay use cases, where its intended to reproduce the
  27. same traffic shape or protocol attributes there were previously produced.
  28. This PRNG is _not_ for security use cases including production cryptographic
  29. key generation.
  30. Limitations: there is a cycle in the PRNG stream, after roughly 2^64 * 2^38-64
  31. bytes; and the global instance initialized in init() ignores seeding errors.
  32. It is safe to make concurrent calls to a PRNG instance, including the global
  33. instance, but the caller is responsible for serializing multiple calls as
  34. required for replay.
  35. PRNG conforms to io.Reader and math/rand.Source, with additional helper
  36. functions.
  37. */
  38. package prng
  39. import (
  40. crypto_rand "crypto/rand"
  41. "crypto/sha256"
  42. "encoding/base64"
  43. "encoding/binary"
  44. "encoding/hex"
  45. "io"
  46. "math"
  47. "math/rand"
  48. "sync"
  49. "time"
  50. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/Yawning/chacha20"
  51. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  52. "golang.org/x/crypto/hkdf"
  53. )
  54. const (
  55. SEED_LENGTH = 32
  56. )
  57. // Seed is a PRNG seed.
  58. type Seed [SEED_LENGTH]byte
  59. // NewSeed creates a new PRNG seed using crypto/rand.Read.
  60. func NewSeed() (*Seed, error) {
  61. seed := new(Seed)
  62. _, err := crypto_rand.Read(seed[:])
  63. if err != nil {
  64. return nil, errors.Trace(err)
  65. }
  66. return seed, nil
  67. }
  68. // NewSaltedSeed creates a new seed derived from an existing seed and a salt.
  69. // A HKDF is applied to the seed and salt.
  70. //
  71. // NewSaltedSeed is intended for use cases where a single seed needs to be
  72. // used in distinct contexts to produce independent random streams.
  73. func NewSaltedSeed(seed *Seed, salt string) (*Seed, error) {
  74. saltedSeed := new(Seed)
  75. _, err := io.ReadFull(
  76. hkdf.New(sha256.New, seed[:], []byte(salt), nil), saltedSeed[:])
  77. if err != nil {
  78. return nil, errors.Trace(err)
  79. }
  80. return saltedSeed, nil
  81. }
  82. // PRNG is a seeded, unbiased PRNG based on chacha20.
  83. type PRNG struct {
  84. rand *rand.Rand
  85. randomStreamMutex sync.Mutex
  86. randomStreamSeed *Seed
  87. randomStream *chacha20.Cipher
  88. randomStreamUsed uint64
  89. randomStreamRekeyCount uint64
  90. }
  91. // NewPRNG generates a seed and creates a PRNG with that seed.
  92. func NewPRNG() (*PRNG, error) {
  93. seed, err := NewSeed()
  94. if err != nil {
  95. return nil, errors.Trace(err)
  96. }
  97. return NewPRNGWithSeed(seed), nil
  98. }
  99. // NewPRNGWithSeed initializes a new PRNG using an existing seed.
  100. func NewPRNGWithSeed(seed *Seed) *PRNG {
  101. p := &PRNG{
  102. randomStreamSeed: seed,
  103. }
  104. p.rekey()
  105. p.rand = rand.New(p)
  106. return p
  107. }
  108. // NewPRNGWithSaltedSeed initializes a new PRNG using a seed derived from an
  109. // existing seed and a salt with NewSaltedSeed.
  110. func NewPRNGWithSaltedSeed(seed *Seed, salt string) (*PRNG, error) {
  111. saltedSeed, err := NewSaltedSeed(seed, salt)
  112. if err != nil {
  113. return nil, errors.Trace(err)
  114. }
  115. return NewPRNGWithSeed(saltedSeed), nil
  116. }
  117. // GetSeed returns the seed for the PRNG. The returned value must not be mutated.
  118. func (p *PRNG) GetSeed() *Seed {
  119. // Concurrency note: p.randomStreamSeed is not mutated after creationg, and
  120. // is safe for concurrent reads. p.randomStreamSeed is reread internally, and
  121. // so must not be mutated.
  122. return p.randomStreamSeed
  123. }
  124. // Read reads random bytes from the PRNG stream into b. Read conforms to
  125. // io.Reader and always returns len(p), nil.
  126. func (p *PRNG) Read(b []byte) (int, error) {
  127. p.randomStreamMutex.Lock()
  128. defer p.randomStreamMutex.Unlock()
  129. // Re-key before reaching the 2^38-64 chacha20 key stream limit.
  130. if p.randomStreamUsed+uint64(len(b)) >= uint64(1<<38-64) {
  131. p.rekey()
  132. }
  133. p.randomStream.KeyStream(b)
  134. p.randomStreamUsed += uint64(len(b))
  135. return len(b), nil
  136. }
  137. func (p *PRNG) rekey() {
  138. // chacha20 has a stream limit of 2^38-64. Before that limit is reached,
  139. // the cipher must be rekeyed. To rekey without changing the seed, we use
  140. // a counter for the nonce.
  141. //
  142. // Limitation: the counter wraps at 2^64, which produces a cycle in the
  143. // PRNG after 2^64 * 2^38-64 bytes.
  144. //
  145. // TODO: this could be extended by using all 2^96 bits of the nonce for
  146. // the counter; and even further by using the 24 byte XChaCha20 nonce.
  147. var randomKeyNonce [12]byte
  148. binary.BigEndian.PutUint64(randomKeyNonce[0:8], p.randomStreamRekeyCount)
  149. var err error
  150. p.randomStream, err = chacha20.NewCipher(
  151. p.randomStreamSeed[:], randomKeyNonce[:])
  152. if err != nil {
  153. // Functions returning random values, which may call rekey, don't
  154. // return an error. As of github.com/Yawning/chacha20 rev. e3b1f968,
  155. // the only possible errors from chacha20.NewCipher invalid key or
  156. // nonce size, and since we use the correct sizes, there should never
  157. // be an error here. So panic in this unexpected case.
  158. panic(errors.Trace(err))
  159. }
  160. p.randomStreamRekeyCount += 1
  161. p.randomStreamUsed = 0
  162. }
  163. // Int63 is equivilent to math/rand.Int63.
  164. func (p *PRNG) Int63() int64 {
  165. i := p.Uint64()
  166. return int64(i & (1<<63 - 1))
  167. }
  168. // Int63 is equivilent to math/rand.Uint64.
  169. func (p *PRNG) Uint64() uint64 {
  170. var b [8]byte
  171. p.Read(b[:])
  172. return binary.BigEndian.Uint64(b[:])
  173. }
  174. // Seed must exist in order to use a PRNG as a math/rand.Source. This call is
  175. // not supported and ignored.
  176. func (p *PRNG) Seed(_ int64) {
  177. }
  178. // FlipCoin randomly returns true or false.
  179. func (p *PRNG) FlipCoin() bool {
  180. return p.rand.Int31n(2) == 1
  181. }
  182. // FlipWeightedCoin returns the result of a weighted
  183. // random coin flip. If the weight is 0.5, the outcome
  184. // is equally likely to be true or false. If the weight
  185. // is 1.0, the outcome is always true, and if the
  186. // weight is 0.0, the outcome is always false.
  187. //
  188. // Input weights > 1.0 are treated as 1.0.
  189. func (p *PRNG) FlipWeightedCoin(weight float64) bool {
  190. if weight > 1.0 {
  191. weight = 1.0
  192. }
  193. f := float64(p.Int63()) / float64(math.MaxInt64)
  194. return f > 1.0-weight
  195. }
  196. // Intn is equivilent to math/rand.Intn, except it returns 0 if n <= 0
  197. // instead of panicking.
  198. func (p *PRNG) Intn(n int) int {
  199. if n <= 0 {
  200. return 0
  201. }
  202. return p.rand.Intn(n)
  203. }
  204. // Int63n is equivilent to math/rand.Int63n, except it returns 0 if n <= 0
  205. // instead of panicking.
  206. func (p *PRNG) Int63n(n int64) int64 {
  207. if n <= 0 {
  208. return 0
  209. }
  210. return p.rand.Int63n(n)
  211. }
  212. // ExpFloat64Range returns a pseudo-exponentially distributed float64 in the
  213. // range [min, max] with the specified lambda. Numbers are selected using
  214. // math/rand.ExpFloat64 and discarding values that exceed max.
  215. //
  216. // If max < min or lambda is <= 0, min is returned.
  217. func (p *PRNG) ExpFloat64Range(min, max, lambda float64) float64 {
  218. if max <= min || lambda <= 0.0 {
  219. return min
  220. }
  221. var value float64
  222. for {
  223. value = min + (rand.ExpFloat64()/lambda)*(max-min)
  224. if value <= max {
  225. break
  226. }
  227. }
  228. return value
  229. }
  230. // Perm is equivilent to math/rand.Perm.
  231. func (p *PRNG) Perm(n int) []int {
  232. return p.rand.Perm(n)
  233. }
  234. // Range selects a random integer in [min, max].
  235. // If min < 0, min is set to 0. If max < min, min is returned.
  236. func (p *PRNG) Range(min, max int) int {
  237. if min < 0 {
  238. min = 0
  239. }
  240. if max < min {
  241. return min
  242. }
  243. n := p.Intn(max - min + 1)
  244. n += min
  245. return n
  246. }
  247. // Bytes returns a new slice containing length random bytes.
  248. func (p *PRNG) Bytes(length int) []byte {
  249. b := make([]byte, length)
  250. p.Read(b)
  251. return b
  252. }
  253. // Padding selects a random padding length in the indicated
  254. // range and returns a random byte slice of the selected length.
  255. // If maxLength <= minLength, the padding is minLength.
  256. func (p *PRNG) Padding(minLength, maxLength int) []byte {
  257. return p.Bytes(p.Range(minLength, maxLength))
  258. }
  259. // Period returns a random duration, within a given range.
  260. // If max <= min, the duration is min.
  261. func (p *PRNG) Period(min, max time.Duration) time.Duration {
  262. duration := p.Int63n(max.Nanoseconds() - min.Nanoseconds())
  263. return min + time.Duration(duration)
  264. }
  265. // Jitter returns n +/- the given factor.
  266. // For example, for n = 100 and factor = 0.1, the
  267. // return value will be in the range [90, 110].
  268. func (p *PRNG) Jitter(n int64, factor float64) int64 {
  269. a := int64(math.Ceil(float64(n) * factor))
  270. r := p.Int63n(2*a + 1)
  271. return n + r - a
  272. }
  273. // JitterDuration invokes Jitter for time.Duration.
  274. func (p *PRNG) JitterDuration(d time.Duration, factor float64) time.Duration {
  275. return time.Duration(p.Jitter(int64(d), factor))
  276. }
  277. // HexString returns a hex encoded random string.
  278. // byteLength specifies the pre-encoded data length.
  279. func (p *PRNG) HexString(byteLength int) string {
  280. return hex.EncodeToString(p.Bytes(byteLength))
  281. }
  282. // Base64String returns a base64 encoded random string.
  283. // byteLength specifies the pre-encoded data length.
  284. func (p *PRNG) Base64String(byteLength int) string {
  285. return base64.StdEncoding.EncodeToString(p.Bytes(byteLength))
  286. }
  287. var p *PRNG
  288. func DefaultPRNG() *PRNG {
  289. return p
  290. }
  291. func Read(b []byte) (int, error) {
  292. return p.Read(b)
  293. }
  294. func Int63() int64 {
  295. return p.Int63()
  296. }
  297. func Uint64() uint64 {
  298. return p.Uint64()
  299. }
  300. func FlipCoin() bool {
  301. return p.FlipCoin()
  302. }
  303. func FlipWeightedCoin(weight float64) bool {
  304. return p.FlipWeightedCoin(weight)
  305. }
  306. func Intn(n int) int {
  307. return p.Intn(n)
  308. }
  309. func Int63n(n int64) int64 {
  310. return p.Int63n(n)
  311. }
  312. func ExpFloat64Range(min, max, lambda float64) float64 {
  313. return p.ExpFloat64Range(min, max, lambda)
  314. }
  315. func Perm(n int) []int {
  316. return p.Perm(n)
  317. }
  318. func Range(min, max int) int {
  319. return p.Range(min, max)
  320. }
  321. func Bytes(length int) []byte {
  322. return p.Bytes(length)
  323. }
  324. func Padding(minLength, maxLength int) []byte {
  325. return p.Padding(minLength, maxLength)
  326. }
  327. func Period(min, max time.Duration) time.Duration {
  328. return p.Period(min, max)
  329. }
  330. func Jitter(n int64, factor float64) int64 {
  331. return p.Jitter(n, factor)
  332. }
  333. func JitterDuration(d time.Duration, factor float64) time.Duration {
  334. return p.JitterDuration(d, factor)
  335. }
  336. func HexString(byteLength int) string {
  337. return p.HexString(byteLength)
  338. }
  339. func Base64String(byteLength int) string {
  340. return p.Base64String(byteLength)
  341. }
  342. func init() {
  343. // Limitation: if crypto/rand.Read fails, the global PRNG will be
  344. // initialized with a zero-byte seed. This ensures that non-security-
  345. // critical use of the global PRNG can proceed.
  346. //
  347. // As of Go 1.9, with https://github.com/golang/go/issues/19274, on Linux
  348. // kernels v3.17+, cryto/rand.Read should now block instead of failing or
  349. // returning predictable bytes.
  350. var err error
  351. p, err = NewPRNG()
  352. if err != nil {
  353. p = NewPRNGWithSeed(new(Seed))
  354. }
  355. }