/
securesource.go
64 lines (54 loc) · 1.56 KB
/
securesource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2016 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package rand
import (
"bufio"
"crypto/rand"
"encoding/binary"
"io"
"sync"
)
// The secureSource is a math/rand.Source + io.Reader that reads bytes from
// crypto/rand.Reader. It means we can use the convenience functions
// provided by math/rand.Rand on top of a secure source of numbers. It is
// concurrency safe for ease of use.
type secureSource struct {
rd io.Reader
mut sync.Mutex
}
func newSecureSource() *secureSource {
return &secureSource{
// Using buffering on top of the rand.Reader increases our
// performance by about 20%, even though it means we must use
// locking.
rd: bufio.NewReader(rand.Reader),
}
}
func (s *secureSource) Seed(int64) {
panic("SecureSource is not seedable")
}
func (s *secureSource) Int63() int64 {
return int64(s.Uint64() & (1<<63 - 1))
}
func (s *secureSource) Read(p []byte) (int, error) {
s.mut.Lock()
defer s.mut.Unlock()
return s.rd.Read(p)
}
func (s *secureSource) Uint64() uint64 {
var buf [8]byte
// Read eight bytes of entropy from the buffered, secure random number
// generator. The buffered reader isn't concurrency safe, so we lock
// around that.
s.mut.Lock()
_, err := io.ReadFull(s.rd, buf[:])
s.mut.Unlock()
if err != nil {
panic("randomness failure: " + err.Error())
}
// Grab those bytes as an uint64
return binary.BigEndian.Uint64(buf[:])
}