Skip to content

Commit

Permalink
integrityregisters: add Compare routine
Browse files Browse the repository at this point in the history
Add a routine to compare IntegrityRegisters against a reference

Signed-off-by: Jagannathan Raman <[email protected]>
  • Loading branch information
jraman567 committed Feb 11, 2025
1 parent 8c9d941 commit 81e77c4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
21 changes: 20 additions & 1 deletion comid/integrityregisters.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2024 Contributors to the Veraison project.
// Copyright 2024-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid

import (
"bytes"
"encoding/json"
"fmt"
"strconv"
Expand Down Expand Up @@ -136,3 +137,21 @@ func (i *IntegrityRegisters) UnmarshalJSON(data []byte) error {
}
return nil
}

func (i *IntegrityRegisters) Compare(r *IntegrityRegisters) error {
claim, err := i.MarshalJSON()
if err != nil {
return err
}

ref, err := r.MarshalJSON()
if err != nil {
return err
}

if bytes.Equal(claim, ref) {
return nil
}

return fmt.Errorf("mismatched integrity registers")
}
41 changes: 40 additions & 1 deletion comid/integrityregisters_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Contributors to the Veraison project.
// Copyright 2024-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package comid
Expand Down Expand Up @@ -300,3 +300,42 @@ func TestIntegrityRegisters_UnmarshalJSON_NOK(t *testing.T) {
})
}
}

func TestIntegrityRegisters_Compare_OK(t *testing.T) {
claim := IntegrityRegisters{map[IRegisterIndex]Digests{
uint64(0): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(1): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(2): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(3): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(4): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
}}

ref := IntegrityRegisters{map[IRegisterIndex]Digests{
uint64(0): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(1): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(2): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(3): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(4): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
}}

assert.Nil(t, claim.Compare(&ref))
}

func TestIntegrityRegisters_Compare_NOK(t *testing.T) {
claim := IntegrityRegisters{map[IRegisterIndex]Digests{
uint64(0): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(1): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(2): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(3): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(4): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
}}

ref := IntegrityRegisters{map[IRegisterIndex]Digests{
uint64(0): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(1): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(2): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint64(3): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
}}

assert.Errorf(t, claim.Compare(&ref), "mismatched integrity registers")
}

0 comments on commit 81e77c4

Please sign in to comment.