-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods to directly add Reference Values and Endorsed Values
Signed-off-by: Priyanshu Thapliyal <[email protected]>
- Loading branch information
1 parent
b690f86
commit 00c59d3
Showing
6 changed files
with
398 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,52 @@ | ||
// Copyright 2021 Contributors to the Veraison project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package comid | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/veraison/swid" | ||
"fmt" | ||
"github.com/veraison/swid" | ||
) | ||
|
||
// Digests is an alias for an array of SWID HashEntry | ||
// Digests is an array of SWID HashEntry | ||
type Digests []swid.HashEntry | ||
|
||
// NewDigests instantiates an empty array of Digests | ||
func NewDigests() *Digests { | ||
return new(Digests) | ||
return new(Digests) | ||
} | ||
|
||
// AddDigest create a new digest from the supplied arguments and appends it to | ||
// the (already instantiated) Digests target. The method is a no-op if it is | ||
// invoked on a nil target and will refuse to add inconsistent algo/value | ||
// combinations. | ||
// AddDigest create a new digest from the supplied arguments and appends it to the (already instantiated) Digests target. | ||
// The method is a no-op if it is invoked on a nil target and will refuse to add inconsistent algo/value combinations. | ||
func (o *Digests) AddDigest(algID uint64, value []byte) *Digests { | ||
if o != nil { | ||
he := NewHashEntry(algID, value) | ||
if he == nil { | ||
return nil | ||
} | ||
*o = append(*o, *he) | ||
} | ||
return o | ||
if o != nil { | ||
he := NewHashEntry(algID, value) | ||
if he == nil { | ||
return nil | ||
} | ||
*o = append(*o, *he) | ||
} | ||
return o | ||
} | ||
|
||
func (o Digests) Valid() error { | ||
for i, m := range o { | ||
if err := swid.ValidHashEntry(m.HashAlgID, m.HashValue); err != nil { | ||
return fmt.Errorf("digest at index %d: %w", i, err) | ||
} | ||
} | ||
return nil | ||
if len(o) == 0 { | ||
return fmt.Errorf("digests must not be empty") | ||
} | ||
|
||
for i, m := range o { | ||
if err := swid.ValidHashEntry(m.HashAlgID, m.HashValue); err != nil { | ||
return fmt.Errorf("digest at index %d: %w", i, err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
|
||
func NewHashEntry(algID uint64, value []byte) *swid.HashEntry { | ||
var he swid.HashEntry | ||
var he swid.HashEntry | ||
|
||
err := he.Set(algID, value) | ||
if err != nil { | ||
return nil | ||
} | ||
err := he.Set(algID, value) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return &he | ||
return &he | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package comid | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"encoding/json" | ||
) | ||
|
||
type HashAlg uint64 | ||
|
||
const ( | ||
HashAlgSHA256 HashAlg = 1 | ||
HashAlgSHA384 HashAlg = 2 | ||
HashAlgSHA512 HashAlg = 3 | ||
) | ||
|
||
func (h HashAlg) Valid() bool { | ||
return h >= HashAlgSHA256 && h <= HashAlgSHA512 | ||
} | ||
|
||
func HashAlgFromString(s string) HashAlg { | ||
switch strings.ToLower(s) { | ||
case "sha-256": | ||
return HashAlgSHA256 | ||
case "sha-384": | ||
return HashAlgSHA384 | ||
case "sha-512": | ||
return HashAlgSHA512 | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
func (h HashAlg) String() string { | ||
switch h { | ||
case HashAlgSHA256: | ||
return "sha-256" | ||
case HashAlgSHA384: | ||
return "sha-384" | ||
case HashAlgSHA512: | ||
return "sha-512" | ||
default: | ||
return fmt.Sprintf("unknown(%d)", h) | ||
} | ||
} | ||
|
||
func (h HashAlg) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(h.String()) | ||
} | ||
func (h *HashAlg) UnmarshalJSON(data []byte) error { | ||
var s string | ||
if err := json.Unmarshal(data, &s); err != nil { | ||
return err | ||
} | ||
*h = HashAlgFromString(s) | ||
if !h.Valid() { | ||
return fmt.Errorf("invalid hash algorithm: %s", s) | ||
} | ||
return nil | ||
} | ||
|
||
// ToUint64 returns 0 if invalid, otherwise the numeric value. | ||
func (h HashAlg) ToUint64() uint64 { | ||
if !h.Valid() { | ||
return 0 | ||
} | ||
return uint64(h) | ||
} | ||
|
||
// HashAlgFromUint64 returns 0 if v is invalid, otherwise the matching HashAlg. | ||
func HashAlgFromUint64(v uint64) HashAlg { | ||
h := HashAlg(v) | ||
if !h.Valid() { | ||
return 0 | ||
} | ||
return h | ||
} |
Oops, something went wrong.