From 51381430456f790b9352d6d4a153b3a98bfa3635 Mon Sep 17 00:00:00 2001 From: Ethan Hsieh Date: Sat, 14 Aug 2021 16:15:06 +0800 Subject: [PATCH] Add support for hash algorithm SM3 256 --- types.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/types.go b/types.go index 0349a6c..30070e2 100644 --- a/types.go +++ b/types.go @@ -155,7 +155,20 @@ func (a HashAlgorithmId) GetHash() crypto.Hash { // Supported determines if the TPM digest algorithm has an equivalent go crypto.Hash. func (a HashAlgorithmId) Supported() bool { - return a.GetHash() != crypto.Hash(0) + switch a { + case HashAlgorithmSHA1: + case HashAlgorithmSHA256: + case HashAlgorithmSHA384: + case HashAlgorithmSHA512: + case HashAlgorithmSM3_256: + case HashAlgorithmSHA3_256: + case HashAlgorithmSHA3_384: + case HashAlgorithmSHA3_512: + default: + return false + } + + return true } // Available determines if the TPM digest algorithm has an equivalent go crypto.Hash @@ -172,7 +185,26 @@ func (a HashAlgorithmId) NewHash() hash.Hash { // Size returns the size of the algorithm. It will panic if HashAlgorithmId.Supported returns false. func (a HashAlgorithmId) Size() int { - return a.GetHash().Size() + switch a { + case HashAlgorithmSHA1: + return 20 + case HashAlgorithmSHA256: + return 32 + case HashAlgorithmSHA384: + return 48 + case HashAlgorithmSHA512: + return 64 + case HashAlgorithmSM3_256: + return 32 + case HashAlgorithmSHA3_256: + return 32 + case HashAlgorithmSHA3_384: + return 48 + case HashAlgorithmSHA3_512: + return 64 + default: + panic("unknown hash algorithm") + } } // SymAlgorithmId corresponds to the TPMI_ALG_SYM type -- 2.25.1