Skip to content

Commit 384d71e

Browse files
authored
Add helper method FromCryptoHash() to convert to HashAlg (#463)
1 parent 411b084 commit 384d71e

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

attest/attest.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ func (a HashAlg) cryptoHash() (crypto.Hash, error) {
395395
}
396396
return h, nil
397397
}
398+
398399
func (a HashAlg) goTPMAlg() tpm2.Algorithm {
399400
return tpm2.Algorithm(a)
400401
}
@@ -420,6 +421,22 @@ func FromTPMAlg(h tpm2.Algorithm) (HashAlg, error) {
420421
}
421422
}
422423

424+
// FromCryptoHash returns the HashAlg corresponding to the given crypto.Hash.
425+
func FromCryptoHash(h crypto.Hash) (HashAlg, error) {
426+
switch h {
427+
case crypto.SHA1:
428+
return HashSHA1, nil
429+
case crypto.SHA256:
430+
return HashSHA256, nil
431+
case crypto.SHA384:
432+
return HashSHA384, nil
433+
case crypto.SHA512:
434+
return HashSHA512, nil
435+
default:
436+
return 0, fmt.Errorf("crypto.Hash %v has no corresponding HashAlg", h)
437+
}
438+
}
439+
423440
// PlatformParameters encapsulates the set of information necessary to attest
424441
// the booted state of the machine the TPM is attached to.
425442
//

attest/attest_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package attest
1616

1717
import (
1818
"bytes"
19+
"crypto"
1920
"flag"
2021
"fmt"
2122
"reflect"
@@ -187,3 +188,42 @@ func TestBug142(t *testing.T) {
187188
t.Errorf("ParseEKCertificate() = %v, want %v", err, wantErr)
188189
}
189190
}
191+
192+
func TestFromCryptoHash(t *testing.T) {
193+
tests := []struct {
194+
hash crypto.Hash
195+
want HashAlg
196+
err bool
197+
}{
198+
{
199+
hash: crypto.SHA1,
200+
want: HashSHA1,
201+
},
202+
{
203+
hash: crypto.SHA256,
204+
want: HashSHA256,
205+
},
206+
{
207+
hash: crypto.SHA384,
208+
want: HashSHA384,
209+
},
210+
{
211+
hash: crypto.SHA512,
212+
want: HashSHA512,
213+
},
214+
{
215+
hash: crypto.MD5,
216+
err: true,
217+
},
218+
}
219+
220+
for _, tc := range tests {
221+
got, err := FromCryptoHash(tc.hash)
222+
if tc.err != (err != nil) {
223+
t.Errorf("FromCryptoHash(%v) returned err=%v, want err=%v", tc.hash, err, tc.err)
224+
}
225+
if got != tc.want {
226+
t.Errorf("FromCryptoHash(%v) = %v, want %v", tc.hash, got, tc.want)
227+
}
228+
}
229+
}

attest/eventlog.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,17 +742,12 @@ func AppendEvents(base []byte, additional ...[]byte) ([]byte, error) {
742742

743743
// Serialize digests
744744
for _, d := range e.digests {
745-
var algID uint16
746-
switch d.hash {
747-
case crypto.SHA256:
748-
algID = uint16(HashSHA256)
749-
case crypto.SHA1:
750-
algID = uint16(HashSHA1)
751-
default:
745+
algID, err := FromCryptoHash(d.hash)
746+
if err != nil {
752747
return nil, fmt.Errorf("log %d: event %d: unhandled hash function %v", i, x, d.hash)
753748
}
754749

755-
binary.Write(out, binary.LittleEndian, algID)
750+
binary.Write(out, binary.LittleEndian, uint16(algID))
756751
out.Write(d.data)
757752
}
758753

0 commit comments

Comments
 (0)