Repository: go-webauthn/webauthn
Pull request #323 (2024-11-06 00:56:53)

refactor(webauthncose): to make it more readable and usable.
The cause was a concern found in `func (k *RSAPublicKeyData) Verify`.

The [`hasher hash.Hash`](https://github.com/go-webauthn/webauthn/blob/master/protocol/webauthncose/webauthncose.go#L126) and [`hash crypto.Hash`](https://github.com/go-webauthn/webauthn/blob/master/protocol/webauthncose/webauthncose.go#L130) come from different source, whose type can be unmatched, if the list of struct `SignatureAlgorithmDetails` is misconfigured.

So I did a little bit refactor to
1. Use `crypto.Hash` as the representation for a hash algorithm in this package, and call `New()` to generate new `hash.Hash`er once needed. To avoid the mismatch issue mentioned above.
2. Use `big.Int.FillBytes()` to ensure the parsed ec Coord is 32-byte.
3. Make `SignatureAlgorithmDetails` into a map, to provide easier way to query COSE algorithm detail.
4. Remove the redundant `SignatureAlgorithm` definition, which is a duplication of `x509.SignatureAlgorithm`. I don't think anyone is using this, since `webauthn` package itself is using `x509` instead in [other places](https://github.com/go-webauthn/webauthn/blob/90441ec9ce635a1da703c7b0763d4b2b4560cb7e/protocol/attestation_packed.go#L109), which should be safe to remove.
5. A breaking change to the API `webauthncose.HasherFromCOSEAlg`, from returning `func() hash.Hash` to `hash.Hash`. There is few repo using this API directly, instead they relying on the `Parsing` and `Verify` extensively. It may break someone, but is easy to fix, given this package is still `v0`. But I won't insist on this. If you think this is unsafe, I can revert this piece.

coderabbitai[bot]: <!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- This is an auto-generated comment: review in progress by coderabbit.ai -->

> [!NOTE]
> Currently processing new changes in this PR. This may take a few minutes, please wait...
> 
> <details>
> <summary>📥 Commits</summary>
> 
> Reviewing files that changed from the base of the PR and between 90441ec9ce635a1da703c7b0763d4b2b4560cb7e and 70cac12710671fdeaa80157099032a8e9d904a2e.
> 
> </details>
> 
> <details>
> <summary>📒 Files selected for processing (5)</summary>
> 
> * `protocol/attestation_androidkey.go` (1 hunks)
> * `protocol/attestation_packed.go` (1 hunks)
> * `protocol/attestation_tpm.go` (2 hunks)
> * `protocol/attestation_tpm_test.go` (2 hunks)
> * `protocol/webauthncose/webauthncose.go` (7 hunks)
> 
> </details>
> 
> ```ascii
>  _________________________________
> < Winner winner, bug-free dinner. >
>  ---------------------------------
>   \
>    \   (\__/)
>        (•ㅅ•)
>        / 　 づ
> ```

<!-- end of auto-generated comment: review in progress by coderabbit.ai -->

<!-- usage_tips_start -->

> [!TIP]
> <details>
> <summary>You can disable poems in the walkthrough.</summary>
> 
> Disable the `reviews.poem` setting in your project's settings in CodeRabbit to disable the poems in the walkthrough.
> 
> </details>

<!-- usage_tips_end -->
<!-- tips_start -->

---

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Generate unit testing code for this file.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai generate unit testing code for this file.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and generate unit testing code.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### CodeRabbit Configuration File (`.coderabbit.yaml`)

- You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository.
- Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json`

### Documentation and Community

- Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->
james-d-elliott: Thanks for this, looks fine on the surface but I'm going to do some manual testing to ensure there's not something I'm overlooking.
ikimonogakari: Thank you @james-d-elliott for your review!

Patch:
diff --git a/protocol/assertion.go b/protocol/assertion.go
index 331d9fe..50cb500 100644
--- a/protocol/assertion.go
+++ b/protocol/assertion.go
@@ -141,7 +141,7 @@ func (car CredentialAssertionResponse) Parse() (par *ParsedCredentialAssertionDa
 // Specification: §7.2 Verifying an Authentication Assertion (https://www.w3.org/TR/webauthn/#sctn-verifying-assertion)
 func (p *ParsedCredentialAssertionData) Verify(storedChallenge string, relyingPartyID string, rpOrigins, rpTopOrigins []string, rpTopOriginsVerify TopOriginVerificationMode, appID string, verifyUser bool, credentialBytes []byte) error {
 	// Steps 4 through 6 in verifying the assertion data (https://www.w3.org/TR/webauthn/#verifying-assertion) are
-	// "assertive" steps, i.e "Let JSONtext be the result of running UTF-8 decode on the value of cData."
+	// "assertive" steps, i.e. "Let JSONtext be the result of running UTF-8 decode on the value of cData."
 	// We handle these steps in part as we verify but also beforehand
 
 	// Handle steps 7 through 10 of assertion by verifying stored data against the Collected Client Data
diff --git a/protocol/webauthncose/webauthncose.go b/protocol/webauthncose/webauthncose.go
index eb1f0d7..02d0bda 100644
--- a/protocol/webauthncose/webauthncose.go
+++ b/protocol/webauthncose/webauthncose.go
@@ -36,6 +36,8 @@ type PublicKeyData struct {
 	Algorithm int64 `cbor:"3,keyasint" json:"alg"`
 }
 
+const ecCoordSize = 32
+
 type EC2PublicKeyData struct {
 	PublicKeyData
 
@@ -79,16 +81,8 @@ func (k *OKPPublicKeyData) Verify(data []byte, sig []byte) (bool, error) {
 
 // Verify Elliptic Curve Public Key Signature.
 func (k *EC2PublicKeyData) Verify(data []byte, sig []byte) (bool, error) {
-	var curve elliptic.Curve
-
-	switch COSEAlgorithmIdentifier(k.Algorithm) {
-	case AlgES512: // IANA COSE code for ECDSA w/ SHA-512.
-		curve = elliptic.P521()
-	case AlgES384: // IANA COSE code for ECDSA w/ SHA-384.
-		curve = elliptic.P384()
-	case AlgES256: // IANA COSE code for ECDSA w/ SHA-256.
-		curve = elliptic.P256()
-	default:
+	curve := ec2AlgCurve(k.Algorithm)
+	if curve == nil {
 		return false, ErrUnsupportedAlgorithm
 	}
 
@@ -98,16 +92,13 @@ func (k *EC2PublicKeyData) Verify(data []byte, sig []byte) (bool, error) {
 		Y:     big.NewInt(0).SetBytes(k.YCoord),
 	}
 
+	h := HasherFromCOSEAlg(COSEAlgorithmIdentifier(k.PublicKeyData.Algorithm))
+	h.Write(data)
+
 	type ECDSASignature struct {
 		R, S *big.Int
 	}
-
 	e := &ECDSASignature{}
-	f := HasherFromCOSEAlg(COSEAlgorithmIdentifier(k.PublicKeyData.Algorithm))
-	h := f()
-
-	h.Write(data)
-
 	_, err := asn1.Unmarshal(sig, e)
 	if err != nil {
 		return false, ErrSigNotProvidedOrInvalid
@@ -123,26 +114,16 @@ func (k *RSAPublicKeyData) Verify(data []byte, sig []byte) (bool, error) {
 		E: int(uint(k.Exponent[2]) | uint(k.Exponent[1])<<8 | uint(k.Exponent[0])<<16),
 	}
 
-	f := HasherFromCOSEAlg(COSEAlgorithmIdentifier(k.PublicKeyData.Algorithm))
-	h := f()
-	h.Write(data)
-
-	var hash crypto.Hash
-
-	switch COSEAlgorithmIdentifier(k.PublicKeyData.Algorithm) {
-	case AlgRS1:
-		hash = crypto.SHA1
-	case AlgPS256, AlgRS256:
-		hash = crypto.SHA256
-	case AlgPS384, AlgRS384:
-		hash = crypto.SHA384
-	case AlgPS512, AlgRS512:
-		hash = crypto.SHA512
-	default:
+	coseAlg := COSEAlgorithmIdentifier(k.PublicKeyData.Algorithm)
+	algDetail, ok := COSESignatureAlgorithmDetails[coseAlg]
+	if !ok {
 		return false, ErrUnsupportedAlgorithm
 	}
+	hash := algDetail.hash
+	h := hash.New()
+	h.Write(data)
 
-	switch COSEAlgorithmIdentifier(k.PublicKeyData.Algorithm) {
+	switch coseAlg {
 	case AlgPS256, AlgPS384, AlgPS512:
 		err := rsa.VerifyPSS(pubkey, hash, h.Sum(nil), sig, nil)
 
@@ -156,28 +137,6 @@ func (k *RSAPublicKeyData) Verify(data []byte, sig []byte) (bool, error) {
 	}
 }
 
-// SigAlgFromCOSEAlg return which signature algorithm is being used from the COSE Key.
-func SigAlgFromCOSEAlg(coseAlg COSEAlgorithmIdentifier) SignatureAlgorithm {
-	for _, details := range SignatureAlgorithmDetails {
-		if details.coseAlg == coseAlg {
-			return details.algo
-		}
-	}
-
-	return UnknownSignatureAlgorithm
-}
-
-// HasherFromCOSEAlg returns the Hashing interface to be used for a given COSE Algorithm.
-func HasherFromCOSEAlg(coseAlg COSEAlgorithmIdentifier) func() hash.Hash {
-	for _, details := range SignatureAlgorithmDetails {
-		if details.coseAlg == coseAlg {
-			return details.hasher
-		}
-	}
-	// default to SHA256?  Why not.
-	return crypto.SHA256.New
-}
-
 // ParsePublicKey figures out what kind of COSE material was provided and create the data for the new key.
 func ParsePublicKey(keyBytes []byte) (any, error) {
 	pk := PublicKeyData{}
@@ -223,11 +182,97 @@ func ParseFIDOPublicKey(keyBytes []byte) (data EC2PublicKeyData, err error) {
 		PublicKeyData: PublicKeyData{
 			Algorithm: int64(AlgES256),
 		},
-		XCoord: x.Bytes(),
-		YCoord: y.Bytes(),
+		XCoord: x.FillBytes(make([]byte, ecCoordSize)),
+		YCoord: y.FillBytes(make([]byte, ecCoordSize)),
 	}, nil
 }
 
+func VerifySignature(key any, data []byte, sig []byte) (bool, error) {
+	switch k := key.(type) {
+	case OKPPublicKeyData:
+		return k.Verify(data, sig)
+	case EC2PublicKeyData:
+		return k.Verify(data, sig)
+	case RSAPublicKeyData:
+		return k.Verify(data, sig)
+	default:
+		return false, ErrUnsupportedKey
+	}
+}
+
+func DisplayPublicKey(cpk []byte) string {
+	parsedKey, err := ParsePublicKey(cpk)
+	if err != nil {
+		return keyCannotDisplay
+	}
+
+	switch k := parsedKey.(type) {
+	case RSAPublicKeyData:
+		rKey := &rsa.PublicKey{
+			N: big.NewInt(0).SetBytes(k.Modulus),
+			E: int(uint(k.Exponent[2]) | uint(k.Exponent[1])<<8 | uint(k.Exponent[0])<<16),
+		}
+
+		data, err := x509.MarshalPKIXPublicKey(rKey)
+		if err != nil {
+			return keyCannotDisplay
+		}
+
+		pemBytes := pem.EncodeToMemory(&pem.Block{
+			Type:  "RSA PUBLIC KEY",
+			Bytes: data,
+		})
+
+		return string(pemBytes)
+	case EC2PublicKeyData:
+		curve := ec2AlgCurve(k.Algorithm)
+		if curve == nil {
+			return keyCannotDisplay
+		}
+
+		eKey := &ecdsa.PublicKey{
+			Curve: curve,
+			X:     big.NewInt(0).SetBytes(k.XCoord),
+			Y:     big.NewInt(0).SetBytes(k.YCoord),
+		}
+
+		data, err := x509.MarshalPKIXPublicKey(eKey)
+		if err != nil {
+			return keyCannotDisplay
+		}
+
+		pemBytes := pem.EncodeToMemory(&pem.Block{
+			Type:  "PUBLIC KEY",
+			Bytes: data,
+		})
+
+		return string(pemBytes)
+	case OKPPublicKeyData:
+		if len(k.XCoord) != ed25519.PublicKeySize {
+			return keyCannotDisplay
+		}
+
+		var oKey ed25519.PublicKey = make([]byte, ed25519.PublicKeySize)
+
+		copy(oKey, k.XCoord)
+
+		data, err := marshalEd25519PublicKey(oKey)
+		if err != nil {
+			return keyCannotDisplay
+		}
+
+		pemBytes := pem.EncodeToMemory(&pem.Block{
+			Type:  "PUBLIC KEY",
+			Bytes: data,
+		})
+
+		return string(pemBytes)
+
+	default:
+		return "Cannot display key of this type"
+	}
+}
+
 // COSEAlgorithmIdentifier is a number identifying a cryptographic algorithm. The algorithm identifiers SHOULD be values
 // registered in the IANA COSE Algorithms registry [https://www.w3.org/TR/webauthn/#biblio-iana-cose-algs-reg], for
 // instance, -7 for "ES256" and -257 for "RS256".
@@ -239,24 +284,15 @@ const (
 	// AlgES256 ECDSA with SHA-256.
 	AlgES256 COSEAlgorithmIdentifier = -7
 
+	// AlgEdDSA EdDSA.
+	AlgEdDSA COSEAlgorithmIdentifier = -8
+
 	// AlgES384 ECDSA with SHA-384.
 	AlgES384 COSEAlgorithmIdentifier = -35
 
 	// AlgES512 ECDSA with SHA-512.
 	AlgES512 COSEAlgorithmIdentifier = -36
 
-	// AlgRS1 RSASSA-PKCS1-v1_5 with SHA-1.
-	AlgRS1 COSEAlgorithmIdentifier = -65535
-
-	// AlgRS256 RSASSA-PKCS1-v1_5 with SHA-256.
-	AlgRS256 COSEAlgorithmIdentifier = -257
-
-	// AlgRS384 RSASSA-PKCS1-v1_5 with SHA-384.
-	AlgRS384 COSEAlgorithmIdentifier = -258
-
-	// AlgRS512 RSASSA-PKCS1-v1_5 with SHA-512.
-	AlgRS512 COSEAlgorithmIdentifier = -259
-
 	// AlgPS256 RSASSA-PSS with SHA-256.
 	AlgPS256 COSEAlgorithmIdentifier = -37
 
@@ -266,11 +302,20 @@ const (
 	// AlgPS512 RSASSA-PSS with SHA-512.
 	AlgPS512 COSEAlgorithmIdentifier = -39
 
-	// AlgEdDSA EdDSA.
-	AlgEdDSA COSEAlgorithmIdentifier = -8
-
 	// AlgES256K is ECDSA using secp256k1 curve and SHA-256.
 	AlgES256K COSEAlgorithmIdentifier = -47
+
+	// AlgRS256 RSASSA-PKCS1-v1_5 with SHA-256.
+	AlgRS256 COSEAlgorithmIdentifier = -257
+
+	// AlgRS384 RSASSA-PKCS1-v1_5 with SHA-384.
+	AlgRS384 COSEAlgorithmIdentifier = -258
+
+	// AlgRS512 RSASSA-PKCS1-v1_5 with SHA-512.
+	AlgRS512 COSEAlgorithmIdentifier = -259
+
+	// AlgRS1 RSASSA-PKCS1-v1_5 with SHA-1.
+	AlgRS1 COSEAlgorithmIdentifier = -65535
 )
 
 // COSEKeyType is The Key type derived from the IANA COSE AuthData.
@@ -343,139 +388,54 @@ func (k *EC2PublicKeyData) TPMCurveID() tpm2.EllipticCurve {
 	}
 }
 
-func VerifySignature(key any, data []byte, sig []byte) (bool, error) {
-	switch k := key.(type) {
-	case OKPPublicKeyData:
-		return k.Verify(data, sig)
-	case EC2PublicKeyData:
-		return k.Verify(data, sig)
-	case RSAPublicKeyData:
-		return k.Verify(data, sig)
+func ec2AlgCurve(coseAlg int64) elliptic.Curve {
+	switch COSEAlgorithmIdentifier(coseAlg) {
+	case AlgES512: // IANA COSE code for ECDSA w/ SHA-512.
+		return elliptic.P521()
+	case AlgES384: // IANA COSE code for ECDSA w/ SHA-384.
+		return elliptic.P384()
+	case AlgES256: // IANA COSE code for ECDSA w/ SHA-256.
+		return elliptic.P256()
 	default:
-		return false, ErrUnsupportedKey
+		return nil
 	}
 }
 
-func DisplayPublicKey(cpk []byte) string {
-	parsedKey, err := ParsePublicKey(cpk)
-	if err != nil {
-		return keyCannotDisplay
+// SigAlgFromCOSEAlg return which signature algorithm is being used from the COSE Key.
+func SigAlgFromCOSEAlg(coseAlg COSEAlgorithmIdentifier) x509.SignatureAlgorithm {
+	d, ok := COSESignatureAlgorithmDetails[coseAlg]
+	if !ok {
+		return x509.UnknownSignatureAlgorithm
 	}
+	return d.sigAlg
+}
 
-	switch k := parsedKey.(type) {
-	case RSAPublicKeyData:
-		rKey := &rsa.PublicKey{
-			N: big.NewInt(0).SetBytes(k.Modulus),
-			E: int(uint(k.Exponent[2]) | uint(k.Exponent[1])<<8 | uint(k.Exponent[0])<<16),
-		}
-
-		data, err := x509.MarshalPKIXPublicKey(rKey)
-		if err != nil {
-			return keyCannotDisplay
-		}
-
-		pemBytes := pem.EncodeToMemory(&pem.Block{
-			Type:  "RSA PUBLIC KEY",
-			Bytes: data,
-		})
-
-		return string(pemBytes)
-	case EC2PublicKeyData:
-		var curve elliptic.Curve
-
-		switch COSEAlgorithmIdentifier(k.Algorithm) {
-		case AlgES256:
-			curve = elliptic.P256()
-		case AlgES384:
-			curve = elliptic.P384()
-		case AlgES512:
-			curve = elliptic.P521()
-		default:
-			return keyCannotDisplay
-		}
-
-		eKey := &ecdsa.PublicKey{
-			Curve: curve,
-			X:     big.NewInt(0).SetBytes(k.XCoord),
-			Y:     big.NewInt(0).SetBytes(k.YCoord),
-		}
-
-		data, err := x509.MarshalPKIXPublicKey(eKey)
-		if err != nil {
-			return keyCannotDisplay
-		}
-
-		pemBytes := pem.EncodeToMemory(&pem.Block{
-			Type:  "PUBLIC KEY",
-			Bytes: data,
-		})
-
-		return string(pemBytes)
-	case OKPPublicKeyData:
-		if len(k.XCoord) != ed25519.PublicKeySize {
-			return keyCannotDisplay
-		}
-
-		var oKey ed25519.PublicKey = make([]byte, ed25519.PublicKeySize)
-
-		copy(oKey, k.XCoord)
-
-		data, err := marshalEd25519PublicKey(oKey)
-		if err != nil {
-			return keyCannotDisplay
-		}
-
-		pemBytes := pem.EncodeToMemory(&pem.Block{
-			Type:  "PUBLIC KEY",
-			Bytes: data,
-		})
-
-		return string(pemBytes)
-
-	default:
-		return "Cannot display key of this type"
+// HasherFromCOSEAlg returns the Hashing interface to be used for a given COSE Algorithm.
+func HasherFromCOSEAlg(coseAlg COSEAlgorithmIdentifier) hash.Hash {
+	d, ok := COSESignatureAlgorithmDetails[coseAlg]
+	if !ok {
+		// default to SHA256?  Why not.
+		return crypto.SHA256.New()
 	}
+	return d.hash.New()
 }
 
-// SignatureAlgorithm represents algorithm enumerations used for COSE signatures.
-type SignatureAlgorithm int
-
-const (
-	UnknownSignatureAlgorithm SignatureAlgorithm = iota
-	MD2WithRSA
-	MD5WithRSA
-	SHA1WithRSA
-	SHA256WithRSA
-	SHA384WithRSA
-	SHA512WithRSA
-	DSAWithSHA1
-	DSAWithSHA256
-	ECDSAWithSHA1
-	ECDSAWithSHA256
-	ECDSAWithSHA384
-	ECDSAWithSHA512
-	SHA256WithRSAPSS
-	SHA384WithRSAPSS
-	SHA512WithRSAPSS
-)
-
-var SignatureAlgorithmDetails = []struct {
-	algo    SignatureAlgorithm
-	coseAlg COSEAlgorithmIdentifier
-	name    string
-	hasher  func() hash.Hash
+var COSESignatureAlgorithmDetails = map[COSEAlgorithmIdentifier]struct {
+	name   string
+	hash   crypto.Hash
+	sigAlg x509.SignatureAlgorithm
 }{
-	{SHA1WithRSA, AlgRS1, "SHA1-RSA", crypto.SHA1.New},
-	{SHA256WithRSA, AlgRS256, "SHA256-RSA", crypto.SHA256.New},
-	{SHA384WithRSA, AlgRS384, "SHA384-RSA", crypto.SHA384.New},
-	{SHA512WithRSA, AlgRS512, "SHA512-RSA", crypto.SHA512.New},
-	{SHA256WithRSAPSS, AlgPS256, "SHA256-RSAPSS", crypto.SHA256.New},
-	{SHA384WithRSAPSS, AlgPS384, "SHA384-RSAPSS", crypto.SHA384.New},
-	{SHA512WithRSAPSS, AlgPS512, "SHA512-RSAPSS", crypto.SHA512.New},
-	{ECDSAWithSHA256, AlgES256, "ECDSA-SHA256", crypto.SHA256.New},
-	{ECDSAWithSHA384, AlgES384, "ECDSA-SHA384", crypto.SHA384.New},
-	{ECDSAWithSHA512, AlgES512, "ECDSA-SHA512", crypto.SHA512.New},
-	{UnknownSignatureAlgorithm, AlgEdDSA, "EdDSA", crypto.SHA512.New},
+	AlgRS1:   {"SHA1-RSA", crypto.SHA1, x509.SHA1WithRSA},
+	AlgRS256: {"SHA256-RSA", crypto.SHA256, x509.SHA256WithRSA},
+	AlgRS384: {"SHA384-RSA", crypto.SHA384, x509.SHA384WithRSA},
+	AlgRS512: {"SHA512-RSA", crypto.SHA512, x509.SHA512WithRSA},
+	AlgPS256: {"SHA256-RSAPSS", crypto.SHA256, x509.SHA256WithRSAPSS},
+	AlgPS384: {"SHA384-RSAPSS", crypto.SHA384, x509.SHA384WithRSAPSS},
+	AlgPS512: {"SHA512-RSAPSS", crypto.SHA512, x509.SHA512WithRSAPSS},
+	AlgES256: {"ECDSA-SHA256", crypto.SHA256, x509.ECDSAWithSHA256},
+	AlgES384: {"ECDSA-SHA384", crypto.SHA384, x509.ECDSAWithSHA384},
+	AlgES512: {"ECDSA-SHA512", crypto.SHA512, x509.ECDSAWithSHA512},
+	AlgEdDSA: {"EdDSA", crypto.SHA512, x509.PureEd25519},
 }
 
 type Error struct {


Test patch:
diff --git a/protocol/attestation_androidkey.go b/protocol/attestation_androidkey.go
index 2201303..6e387b3 100644
--- a/protocol/attestation_androidkey.go
+++ b/protocol/attestation_androidkey.go
@@ -69,9 +69,7 @@ func verifyAndroidKeyFormat(att AttestationObject, clientDataHash []byte, _ meta
 	}
 
 	coseAlg := webauthncose.COSEAlgorithmIdentifier(alg)
-	sigAlg := webauthncose.SigAlgFromCOSEAlg(coseAlg)
-
-	if err = attCert.CheckSignature(x509.SignatureAlgorithm(sigAlg), signatureData, sig); err != nil {
+	if err = attCert.CheckSignature(webauthncose.SigAlgFromCOSEAlg(coseAlg), signatureData, sig); err != nil {
 		return "", nil, ErrInvalidAttestation.WithDetails(fmt.Sprintf("Signature validation error: %+v\n", err))
 	}
 
diff --git a/protocol/attestation_packed.go b/protocol/attestation_packed.go
index 7176947..a46a47f 100644
--- a/protocol/attestation_packed.go
+++ b/protocol/attestation_packed.go
@@ -104,9 +104,7 @@ func handleBasicAttestation(signature, clientDataHash, authData, aaguid []byte,
 	}
 
 	coseAlg := webauthncose.COSEAlgorithmIdentifier(alg)
-	sigAlg := webauthncose.SigAlgFromCOSEAlg(coseAlg)
-
-	if err = attCert.CheckSignature(x509.SignatureAlgorithm(sigAlg), signatureData, signature); err != nil {
+	if err = attCert.CheckSignature(webauthncose.SigAlgFromCOSEAlg(coseAlg), signatureData, signature); err != nil {
 		return "", x5c, ErrInvalidAttestation.WithDetails(fmt.Sprintf("Signature validation error: %+v\n", err))
 	}
 
diff --git a/protocol/attestation_tpm.go b/protocol/attestation_tpm.go
index 8553ef8..562e176 100644
--- a/protocol/attestation_tpm.go
+++ b/protocol/attestation_tpm.go
@@ -113,9 +113,7 @@ func verifyTPMFormat(att AttestationObject, clientDataHash []byte, _ metadata.Pr
 	}
 
 	// 3/4 Verify that extraData is set to the hash of attToBeSigned using the hash algorithm employed in "alg".
-	f := webauthncose.HasherFromCOSEAlg(coseAlg)
-	h := f()
-
+	h := webauthncose.HasherFromCOSEAlg(coseAlg)
 	h.Write(attToBeSigned)
 	if !bytes.Equal(certInfo.ExtraData, h.Sum(nil)) {
 		return "", nil, ErrAttestationFormat.WithDetails("ExtraData is not set to hash of attToBeSigned")
@@ -152,9 +150,7 @@ func verifyTPMFormat(att AttestationObject, clientDataHash []byte, _ metadata.Pr
 			return "", nil, ErrAttestationFormat.WithDetails("Error parsing certificate from ASN.1")
 		}
 
-		sigAlg := webauthncose.SigAlgFromCOSEAlg(coseAlg)
-
-		err = aikCert.CheckSignature(x509.SignatureAlgorithm(sigAlg), certInfoBytes, sigBytes)
+		err = aikCert.CheckSignature(webauthncose.SigAlgFromCOSEAlg(coseAlg), certInfoBytes, sigBytes)
 		if err != nil {
 			return "", nil, ErrAttestationFormat.WithDetails(fmt.Sprintf("Signature validation error: %+v\n", err))
 		}
diff --git a/protocol/attestation_tpm_test.go b/protocol/attestation_tpm_test.go
index d8f6a4e..b784882 100644
--- a/protocol/attestation_tpm_test.go
+++ b/protocol/attestation_tpm_test.go
@@ -407,8 +407,7 @@ func TestTPMAttestationVerificationFailCertInfo(t *testing.T) {
 		},
 	}
 
-	f := webauthncose.HasherFromCOSEAlg(webauthncose.AlgRS256)
-	h := f()
+	h := webauthncose.HasherFromCOSEAlg(webauthncose.AlgRS256)
 	h.Write(att.RawAuthData)
 	extraData := h.Sum(nil)
 
@@ -522,8 +521,7 @@ func TestTPMAttestationVerificationFailX5c(t *testing.T) {
 		},
 	}
 
-	f := webauthncose.HasherFromCOSEAlg(webauthncose.AlgRS256)
-	h := f()
+	h := webauthncose.HasherFromCOSEAlg(webauthncose.AlgRS256)
 	h.Write(att.RawAuthData)
 	extraData := h.Sum(nil)