-
Notifications
You must be signed in to change notification settings - Fork 3
/
random.go
56 lines (42 loc) · 968 Bytes
/
random.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
package otr4
import (
"crypto/rand"
"io"
"golang.org/x/crypto/sha3"
"github.com/otrv4/ed448"
)
func (c *conversation) rand() io.Reader {
if c.random != nil {
return c.random
}
return rand.Reader
}
func randSymKey(rand io.Reader) ([]byte, error) {
var b [symKeyBytes]byte
_, err := io.ReadFull(rand, b[:])
if err != nil {
return nil, notEnoughEntropy
}
return b[:], nil
}
func randScalar(rand io.Reader) (ed448.Scalar, error) {
var b [fieldBytes]byte
_, err := io.ReadFull(rand, b[:])
if err != nil {
return nil, notEnoughEntropy
}
return ed448.NewScalar(b[:]), nil
}
func randLongTermScalar(rand io.Reader) (ed448.Scalar, error) {
var b [fieldBytes]byte
var out [fieldBytes]byte
_, err := io.ReadFull(rand, b[:])
if err != nil {
return nil, notEnoughEntropy
}
hash := sha3.NewShake256()
hash.Write(b[:])
hash.Write([]byte("cramershoup_secret")) //XXX: change me!
hash.Read(out[:])
return ed448.NewScalar(out[:]), nil
}