Skip to content

Commit

Permalink
squash! keydata, crypt: fix and refactor tests for legacy keys
Browse files Browse the repository at this point in the history
copy MarshalKeys to MarshalV1Keys which is now used only for legacy
tests. MarshalKeys will be completely removed after the changes
to the tpm platform which still use it.
  • Loading branch information
sespiros committed Nov 23, 2023
1 parent 6d8a861 commit 9de36fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package secboot
import (
"bytes"
"crypto"
"encoding/binary"
"encoding/json"
"io"
"math/rand"
Expand Down Expand Up @@ -151,6 +152,17 @@ func MockReadKeyData(version int) (restore func()) {
}
}

// MarshalV1Keys serializes the supplied disk unlock key and auxiliary key in
// the v1 format that is ready to be encrypted by a platform's secure device.
func MarshalV1Keys(key DiskUnlockKey, auxKey PrimaryKey) []byte {
w := new(bytes.Buffer)
binary.Write(w, binary.BigEndian, uint16(len(key)))
w.Write(key)
binary.Write(w, binary.BigEndian, uint16(len(auxKey)))
w.Write(auxKey)
return w.Bytes()
}

// MockMakeDiskUnlockKey uses the new keydata API but creates v1 keydata payloads.
func MockMakeDiskUnlockKey(primaryKey PrimaryKey) (func(), error) {
origMakeDiskUnlockKey := MakeDiskUnlockKey
Expand All @@ -170,7 +182,7 @@ func MockMakeDiskUnlockKey(primaryKey PrimaryKey) (func(), error) {
return nil, nil, err
}

clearTextPayload = MarshalKeys(unlockKey, primaryKey)
clearTextPayload = MarshalV1Keys(unlockKey, primaryKey)
return unlockKey, clearTextPayload, err
}
return func() {
Expand Down
4 changes: 2 additions & 2 deletions keydata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func (s *keyDataSuite) TestKeyPayloadUnmarshalInvalid1(c *C) {
func (s *keyDataSuite) TestKeyPayloadUnmarshalInvalid2(c *C) {

if s.Version == 1 {
payload := MarshalKeys(make(DiskUnlockKey, 32), make(PrimaryKey, 32))
payload := MarshalV1Keys(make(DiskUnlockKey, 32), make(PrimaryKey, 32))
payload = append(payload, 0xff)

key, auxKey, err := UnmarshalV1KeyPayload(payload)
Expand Down Expand Up @@ -1724,7 +1724,7 @@ func (s *keyDataLegacySuite) testKeyPayload(c *C, data *testKeyPayloadData) {
unlockKey := data.unique
primaryKey := data.primary

payload := MarshalKeys(unlockKey, primaryKey)
payload := MarshalV1Keys(unlockKey, primaryKey)

key, auxKey, err := UnmarshalV1KeyPayload(payload)
c.Check(err, IsNil)
Expand Down

0 comments on commit 9de36fb

Please sign in to comment.