-
Notifications
You must be signed in to change notification settings - Fork 0
/
slowrand.go
79 lines (64 loc) · 1.74 KB
/
slowrand.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package slowrand
import (
"crypto/sha512"
"errors"
"sync"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/ripemd160"
)
// Reader represents a deterministic rand source from seed.
// Slowness is a feature to prevent brute-force attack.
type Reader struct {
rounds int
time uint32
memory uint32
threads uint8
mu *sync.RWMutex
seed []byte
salt []byte
key []byte
reads int
}
// New instance of slow reader.
// Seed as a start of the pseudorandom sequence.
// Rounds, time, memory and threads are params to the underlying PBKDF2 & Argon2 algorithms.
func New(seed []byte, rounds int, time, memory uint32, threads uint8) (*Reader, error) {
switch {
case len(seed) == 0:
return nil, errors.New("slowrand: seed cannoe be empty")
case rounds < 1:
return nil, errors.New("slowrand: rounds must be greater than 0")
case time < 1:
return nil, errors.New("slowrand: time must be greater than 0")
case memory < 1:
return nil, errors.New("slowrand: memory must be greater than 0")
case threads < 1:
return nil, errors.New("slowrand: threads must be greater than 0")
}
r := &Reader{
seed: seed,
rounds: rounds,
time: time,
memory: memory,
threads: threads,
mu: &sync.RWMutex{},
}
return r, nil
}
func (r *Reader) Read(b []byte) (int, error) {
r.mu.Lock()
defer r.mu.Unlock()
r.seed = pbkdf2.Key(r.seed, r.key, r.rounds, sha512.Size, sha512.New)
r.salt = pbkdf2.Key(r.salt, r.key, r.reads, ripemd160.Size, ripemd160.New)
r.key = argon2.Key(r.seed, r.salt, r.time, r.memory, r.threads, uint32(len(b)))
n := copy(b, r.key)
r.reads += n
return n, nil
}
// Len returns number of read bytes.
func (r *Reader) Len() int {
r.mu.Lock()
defer r.mu.Unlock()
return r.reads
}