aboutsummaryrefslogtreecommitdiff
path: root/cryptfs/nonce.go
blob: 3e464a3ec1a63b6e108456fb0ff97e1bba38e8b5 (plain)
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 cryptfs

import (
	"encoding/binary"
	"encoding/hex"
	"sync"
	"crypto/rand"
)

type nonce96 struct {
	lock sync.Mutex
	high32 uint32
	low64 uint64
	ready int
}

var gcmNonce nonce96

// Get "n" random bytes from /dev/urandom or panic
func RandBytes(n int) []byte {
	b := make([]byte, n)
	_, err := rand.Read(b)
	if err != nil {
		panic("Could not get random bytes for nonce")
	}
	return b
}

func (n *nonce96) init() {
	b := RandBytes(8)
	n.low64 = binary.BigEndian.Uint64(b)
	b = RandBytes(4)
	n.high32 = binary.BigEndian.Uint32(b)
	n.ready = 1
	return
}

func (n *nonce96) Get() []byte {
	n.lock.Lock()
	if n.ready == 0 {
		n.init()
	}
	n.low64++
	if n.low64 == 0 {
		// Counter has wrapped
		n.high32++
	}
	r := make([]byte, 12)
	binary.BigEndian.PutUint32(r[0:4], n.high32)
	binary.BigEndian.PutUint64(r[4:12], n.low64)
	n.lock.Unlock()

	Debug.Printf("nonce96.Get(): %s\n", hex.EncodeToString(r))

	return r
}