forked from canonical/secboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeydata_legacy_test.go
198 lines (157 loc) · 5.42 KB
/
keydata_legacy_test.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package secboot_test
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"hash"
"math/rand"
. "github.com/snapcore/secboot"
. "gopkg.in/check.v1"
)
type keyDataLegacyTestBase struct {
keyDataTestBase
}
func (s *keyDataLegacyTestBase) SetUpSuite(c *C) {
s.handler = &mockPlatformKeyDataHandler{}
s.mockPlatformName = "mock-legacy"
RegisterPlatformKeyDataHandler(s.mockPlatformName, s.handler)
}
func (s *keyDataLegacyTestBase) SetUpTest(c *C) {
s.handler.state = mockPlatformDeviceStateOK
s.handler.passphraseSupport = false
}
func (s *keyDataLegacyTestBase) TearDownSuite(c *C) {
RegisterPlatformKeyDataHandler(s.mockPlatformName, nil)
}
func (s *keyDataLegacyTestBase) newKeyDataKeys(c *C, sz1, sz2 int) (DiskUnlockKey, PrimaryKey) {
key := make([]byte, sz1)
auxKey := make([]byte, sz2)
_, err := rand.Read(key)
c.Assert(err, IsNil)
_, err = rand.Read(auxKey)
c.Assert(err, IsNil)
return key, auxKey
}
func (s *keyDataLegacyTestBase) mockProtectKeys(c *C, key DiskUnlockKey, auxKey PrimaryKey, kdfAlg crypto.Hash, modelAuthHash crypto.Hash) (out *KeyParams) {
payload := MarshalKeys(key, auxKey)
k := make([]byte, 48)
_, err := rand.Read(k)
c.Assert(err, IsNil)
handle := mockPlatformKeyDataHandle{
Key: k[:32],
IV: k[32:],
ExpectedGeneration: 1,
ExpectedKDFAlg: kdfAlg,
ExpectedAuthMode: AuthModeNone,
}
h := hmac.New(func() hash.Hash { return kdfAlg.New() }, handle.Key)
handle.AuthKeyHMAC = h.Sum(nil)
b, err := aes.NewCipher(handle.Key)
c.Assert(err, IsNil)
stream := cipher.NewCFBEncrypter(b, handle.IV)
out = &KeyParams{
PlatformName: s.mockPlatformName,
Handle: &handle,
EncryptedPayload: make([]byte, len(payload))}
stream.XORKeyStream(out.EncryptedPayload, payload)
return
}
type keyDataLegacySuite struct {
keyDataLegacyTestBase
}
var _ = Suite(&keyDataLegacySuite{})
type testLegacyKeyPayloadData struct {
key DiskUnlockKey
auxKey PrimaryKey
}
func (s *keyDataLegacySuite) testKeyPayload(c *C, data *testLegacyKeyPayloadData) {
payload := MarshalKeys(data.key, data.auxKey)
key, auxKey, err := UnmarshalV1KeyPayload(payload)
c.Check(err, IsNil)
c.Check(key, DeepEquals, data.key)
c.Check(auxKey, DeepEquals, data.auxKey)
}
func (s *keyDataLegacySuite) TestLegacyKeyPayload1(c *C) {
key, auxKey := s.newKeyDataKeys(c, 32, 32)
s.testKeyPayload(c, &testLegacyKeyPayloadData{
key: key,
auxKey: auxKey})
}
func (s *keyDataLegacySuite) TestLegacyKeyPayload2(c *C) {
key, auxKey := s.newKeyDataKeys(c, 64, 32)
s.testKeyPayload(c, &testLegacyKeyPayloadData{
key: key,
auxKey: auxKey})
}
func (s *keyDataLegacySuite) TestLegacyKeyPayload3(c *C) {
key, _ := s.newKeyDataKeys(c, 32, 0)
s.testKeyPayload(c, &testLegacyKeyPayloadData{
key: key})
}
func (s *keyDataLegacySuite) TestLegacyKeyPayloadUnmarshalInvalid1(c *C) {
payload := make([]byte, 66)
for i := range payload {
payload[i] = 0xff
}
key, auxKey, err := UnmarshalV1KeyPayload(payload)
c.Check(err, ErrorMatches, "EOF")
c.Check(key, IsNil)
c.Check(auxKey, IsNil)
}
func (s *keyDataLegacySuite) TestLegacyKeyPayloadUnmarshalInvalid2(c *C) {
payload := MarshalKeys(make(DiskUnlockKey, 32), make(PrimaryKey, 32))
payload = append(payload, 0xff)
key, auxKey, err := UnmarshalV1KeyPayload(payload)
c.Check(err, ErrorMatches, "1 excess byte\\(s\\)")
c.Check(key, IsNil)
c.Check(auxKey, IsNil)
}
func (s *keyDataLegacySuite) TestRecoverKeys(c *C) {
key, auxKey := s.newKeyDataKeys(c, 32, 32)
protected := s.mockProtectKeys(c, key, auxKey, crypto.SHA256, crypto.SHA256)
restore := MockKeyDataGeneration(0)
defer restore()
keyData, err := NewKeyData(protected)
c.Assert(err, IsNil)
recoveredKey, recoveredAuxKey, err := keyData.RecoverKeys()
c.Check(err, IsNil)
c.Check(recoveredKey, DeepEquals, key)
c.Check(recoveredAuxKey, DeepEquals, auxKey)
}
func (s *keyDataLegacySuite) TestRecoverKeysUnrecognizedPlatform(c *C) {
key, auxKey := s.newKeyDataKeys(c, 32, 32)
protected := s.mockProtectKeys(c, key, auxKey, crypto.SHA256, crypto.SHA256)
protected.PlatformName = "foo"
restore := MockKeyDataGeneration(0)
defer restore()
keyData, err := NewKeyData(protected)
c.Assert(err, IsNil)
recoveredKey, recoveredAuxKey, err := keyData.RecoverKeys()
c.Check(err, ErrorMatches, "no appropriate platform handler is registered")
c.Check(recoveredKey, IsNil)
c.Check(recoveredAuxKey, IsNil)
}
func (s *keyDataLegacySuite) TestRecoverKeysInvalidData(c *C) {
key, auxKey := s.newKeyDataKeys(c, 32, 32)
protected := s.mockProtectKeys(c, key, auxKey, crypto.SHA256, crypto.SHA256)
protected.Handle = []byte("\"\"")
restore := MockKeyDataGeneration(0)
defer restore()
keyData, err := NewKeyData(protected)
c.Assert(err, IsNil)
recoveredKey, recoveredAuxKey, err := keyData.RecoverKeys()
c.Check(err, ErrorMatches, "invalid key data: JSON decode error: json: cannot unmarshal string into Go value of type secboot_test.mockPlatformKeyDataHandle")
c.Check(recoveredKey, IsNil)
c.Check(recoveredAuxKey, IsNil)
}
func (s *keyDataLegacySuite) TestRecoverKeysWithPassphraseAuthModeNone(c *C) {
key, auxKey := s.newKeyDataKeys(c, 32, 32)
protected := s.mockProtectKeys(c, key, auxKey, crypto.SHA256, crypto.SHA256)
keyData, err := NewKeyData(protected)
c.Assert(err, IsNil)
recoveredKey, recoveredAuxKey, err := keyData.RecoverKeysWithPassphrase("", nil)
c.Check(err, ErrorMatches, "cannot recover key with passphrase")
c.Check(recoveredKey, IsNil)
c.Check(recoveredAuxKey, IsNil)
}