-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplan.go
More file actions
160 lines (131 loc) · 2.94 KB
/
plan.go
File metadata and controls
160 lines (131 loc) · 2.94 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package vcrypt
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"io"
"github.com/vcrypt/vcrypt/config"
"github.com/vcrypt/vcrypt/graph"
"github.com/vcrypt/vcrypt/seal"
)
// NewPlan constructs a Plan from an pre-built Graph.
func NewPlan(g *Graph, comment string) (*Plan, error) {
nonce := make([]byte, 24)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
nodes, err := g.Nodes()
if err != nil {
return nil, err
}
return &Plan{
comment: comment,
Nonce: nonce,
Nodes: nodes,
}, nil
}
// BuildPlan constructs a Plan from the config data in r.
func BuildPlan(r io.Reader) (*Plan, error) {
cp := config.Plan{}
if err := config.NewDecoder(r).Decode(&cp); err != nil {
return nil, err
}
g, err := build(cp)
if err != nil {
return nil, err
}
return NewPlan(g, cp.Comment)
}
// Comment string
func (p *Plan) Comment() string {
return p.comment
}
// BFS walks the nodes in breadth-first order.
func (p *Plan) BFS(fn func(*Node) error) error {
g, err := p.Graph()
if err != nil {
return err
}
return g.BFS(func(vrt *graph.Vertex) error {
node, err := g.node(vrt)
if err != nil {
return err
}
return fn(node)
})
}
// AddSeal adds a Seal for the Plan from the nonce, root node, and comment
// data.
func (p *Plan) AddSeal(slr Sealer) (seal.Seal, error) {
data, err := p.sealData()
if err != nil {
return nil, err
}
s, err := slr.Seal(data)
if err != nil {
return nil, err
}
env, err := seal.Wrap(s)
if err != nil {
return nil, err
}
p.seals = append(p.seals, env)
return s, nil
}
// Digest is a unique series of bytes that identify the Plan.
func (p *Plan) Digest() ([]byte, error) {
// HMAC(Nonce,Nodes[0].Digest|Comment|Seal[*].Digest)
hash := hmac.New(sha256.New, p.Nonce)
fp, err := p.Nodes[0].Digest()
if err != nil {
return nil, err
}
if _, err := hash.Write(fp); err != nil {
return nil, err
}
if _, err := hash.Write([]byte(p.comment)); err != nil {
return nil, err
}
seals, err := p.Seals()
if err != nil {
return nil, err
}
for _, s := range seals {
fp, err = s.Digest()
if err != nil {
return nil, err
}
if _, err := hash.Write(fp); err != nil {
return nil, err
}
}
return hash.Sum(nil), nil
}
// Graph returns a new Graph built from the plan nodes.
func (p *Plan) Graph() (*Graph, error) {
return BuildGraph(p.Nodes)
}
// Seals return a Seal slice for the Plan.
func (p *Plan) Seals() ([]seal.Seal, error) {
seals := make([]seal.Seal, 0, len(p.seals))
for _, env := range p.seals {
seal, err := env.Seal()
if err != nil {
return nil, err
}
seals = append(seals, seal)
}
return seals, nil
}
func (p *Plan) sealData() ([]byte, error) {
// Nonce|Nodes[0].Digest|Comment
nfp, err := p.Nodes[0].Digest()
if err != nil {
return nil, err
}
data := make([]byte, 0, len(p.Nonce)+len(nfp)+len(p.comment))
copy(data, p.Nonce)
data = append(data, nfp...)
data = append(data, []byte(p.comment)...)
return data, nil
}