Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions kek/kek_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func (b *Bundle) Merge() (des.Cipher, error) {

// MergeTripleDESKey tries to build the result TripleDES key from all the imported components
func (b *Bundle) MergeTripleDESKey() (des.Cipher, error) {
if !b.IsComplete() {
return des.Cipher{}, fmt.Errorf("incomplete KEK bundle: expected %d components but got %d", b.Size, len(b.Components))
}

kekBytes := make([]byte, 24)
for _, component := range b.Components {
kekBytes, _ = xor.XORBytes(kekBytes, component)
Expand All @@ -121,6 +125,10 @@ func (b *Bundle) MergeTripleDESKey() (des.Cipher, error) {

// MergeAESKey tries to build the result AES key from all the imported components
func (b *Bundle) MergeAESKey() (aes.Cipher, error) {
if !b.IsComplete() {
return aes.Cipher{}, fmt.Errorf("incomplete KEK bundle: expected %d components but got %d", b.Size, len(b.Components))
}

var keyLen int
for _, component := range b.Components {
if keyLen == 0 {
Expand Down
34 changes: 34 additions & 0 deletions kek/kek_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ func TestMergeResultKeySuccess(t *testing.T) {
}
}

func TestMergeTripleDESKeyRejectsIncompleteBundleWithMatchingCheckValue(t *testing.T) {
kek := New("visa", 1, 3, "DD1375")

err := kek.AddComponent(1, "E38FD6D9EF85A892F2FBFDD083A407AE", "DD1375")
if err != nil {
t.Fatalf("adding component failed with %v", err)
}

_, err = kek.MergeTripleDESKey()
if err == nil {
t.Fatal("should have failed if the bundle is incomplete")
}
if err.Error() != "incomplete KEK bundle: expected 3 components but got 1" {
t.Fatalf("unexpected error: %v", err)
}
}

// AES KEK tests

func TestAESAddComponentInvalidValue(t *testing.T) {
Expand Down Expand Up @@ -174,6 +191,23 @@ func TestAESMergeAESKeySuccess(t *testing.T) {
}
}

func TestMergeAESKeyRejectsIncompleteBundleWithMatchingCheckValue(t *testing.T) {
kek := NewWithKeyType("aes-kek", 1, 3, "B1DD24", KeyTypeAES)

err := kek.AddComponent(1, "574953452d504f430000000000546263", "B1DD24")
if err != nil {
t.Fatalf("adding component failed with %v", err)
}

_, err = kek.MergeAESKey()
if err == nil {
t.Fatal("should have failed if the bundle is incomplete")
}
if err.Error() != "incomplete KEK bundle: expected 3 components but got 1" {
t.Fatalf("unexpected error: %v", err)
}
}

func TestAESMergeAESKeyCheckValueNotTally(t *testing.T) {
kek := NewWithKeyType("aes-kek", 1, 3, "FFFFFF", KeyTypeAES)

Expand Down