-
Notifications
You must be signed in to change notification settings - Fork 4
/
grc_tree.go
42 lines (37 loc) · 1.16 KB
/
grc_tree.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
package ssp
import (
"encoding/binary"
"fmt"
"sync/atomic"
"golang.org/x/crypto/blowfish"
)
// GrcTree Creates a 64-bit nut based on the GRC spec using a monotonic counter
// and blowfish cipher
type GrcTree struct {
monotonicCounter uint64
cipher *blowfish.Cipher
key []byte
}
// NewGrcTree takes an initial counter value (in the case of reboot) and
// a blowfish key (use a max key of random 56 bytes)
// https://godoc.org/golang.org/x/crypto/blowfish
func NewGrcTree(counterInit uint64, blowfishKey []byte) (*GrcTree, error) {
cipher, err := blowfish.NewCipher(blowfishKey)
if err != nil {
return nil, fmt.Errorf("couldn't initialize blowfish cipher: %v", err)
}
return &GrcTree{
monotonicCounter: counterInit,
cipher: cipher,
key: blowfishKey,
}, nil
}
// Nut Create a nut based on the GRC spec.
func (gt *GrcTree) Nut() (Nut, error) {
nextValue := atomic.AddUint64(>.monotonicCounter, 1)
nextValueBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(nextValueBytes, nextValue)
encrypted := make([]byte, 8)
gt.cipher.Encrypt(encrypted, nextValueBytes)
return Nut(Sqrl64.EncodeToString(encrypted)), nil
}