1/*
2 * Copyright (c) 2014, Yawning Angel <yawning at torproject dot org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  * Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 *  * Redistributions in binary form must reproduce the above copyright notice,
12 *    this list of conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// Package csrand implements the math/rand interface over crypto/rand, along
29// with some utility functions for common random number/byte related tasks.
30//
31// Not all of the convinience routines are replicated, only those that are
32// immediately useful.  The Rand variable provides access to the full math/rand
33// API.
34package csrand // import "gitlab.com/yawning/obfs4.git/common/csrand"
35
36import (
37	cryptRand "crypto/rand"
38	"encoding/binary"
39	"fmt"
40	"io"
41	"math/rand"
42)
43
44var (
45	csRandSourceInstance csRandSource
46
47	// Rand is a math/rand instance backed by crypto/rand CSPRNG.
48	Rand = rand.New(csRandSourceInstance)
49)
50
51type csRandSource struct {
52	// This does not keep any state as it is backed by crypto/rand.
53}
54
55func (r csRandSource) Int63() int64 {
56	var src [8]byte
57	if err := Bytes(src[:]); err != nil {
58		panic(err)
59	}
60	val := binary.BigEndian.Uint64(src[:])
61	val &= (1<<63 - 1)
62
63	return int64(val)
64}
65
66func (r csRandSource) Seed(seed int64) {
67	// No-op.
68}
69
70// Intn returns, as a int, a pseudo random number in [0, n).
71func Intn(n int) int {
72	return Rand.Intn(n)
73}
74
75// Float64 returns, as a float64, a pesudo random number in [0.0,1.0).
76func Float64() float64 {
77	return Rand.Float64()
78}
79
80// IntRange returns a uniformly distributed int [min, max].
81func IntRange(min, max int) int {
82	if max < min {
83		panic(fmt.Sprintf("IntRange: min > max (%d, %d)", min, max))
84	}
85
86	r := (max + 1) - min
87	ret := Rand.Intn(r)
88	return ret + min
89}
90
91// Bytes fills the slice with random data.
92func Bytes(buf []byte) error {
93	if _, err := io.ReadFull(cryptRand.Reader, buf); err != nil {
94		return err
95	}
96
97	return nil
98}
99
100// Reader is a alias of rand.Reader.
101var Reader = cryptRand.Reader
102