Skip to content

Commit 8c59288

Browse files
authored
Fix P-521 ECDSA verification and add curve coverage tests (#1956)
* Fix P-521 curve name typo and signature length check in ECDSA verifier hashForCurve matched "P-512" instead of "P-521", making the P-521 path unreachable. verifyECDSASignature used hash length (64 for SHA-512) to determine signature component size, but P-521 COSE signatures use 66-byte components (ceil(521/8)). Derive component size from the curve order instead. Add verify_test.go with sign/verify roundtrips for all four NIST curves, rejection tests (wrong key, wrong payload, tampered sig, wrong length, DER format), and an explicit test proving P-521 key size != hash size. * Add COSE Sign1 verification tests against RFC 8152 / cose-wg vectors Verify the ECDSA signature pipeline (CBOR parse, Sig_structure build, verifyECDSASignature) against official test vectors from the COSE Working Group Examples repository (normative suite for RFC 9052/9053): - ES256: RFC 8152 Appendix C.2.1 (sign-pass-03) - ES384: ecdsa-sig-02 - ES512: ecdsa-sig-03 (exercises the P-521 fix from previous commit) - Tampered payload: sign-fail-02 - Modified protected header: sign-fail-06 - Wrong key: valid ES256 vector verified with P-384 key * Address review: reuse curveKeySize in test, use ecdsa.SignASN1 for DER test
1 parent 8add112 commit 8c59288

2 files changed

Lines changed: 377 additions & 4 deletions

File tree

pkg/teeattestation/nitro/verify.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,27 @@ func parseCertificateChain(doc *attestationDocument) (*x509.Certificate, *x509.C
220220
return leafCert, intermediates, nil
221221
}
222222

223+
// curveKeySize returns the byte length of one ECDSA signature component
224+
// (r or s) for the given curve: ceil(bitSize / 8). This is the correct
225+
// size per RFC 9053 section 2.1, and differs from the hash length for P-521
226+
// (key component = 66 bytes, hash = 64 bytes).
227+
func curveKeySize(publicKey *ecdsa.PublicKey) int {
228+
return (publicKey.Curve.Params().BitSize + 7) / 8
229+
}
230+
223231
func verifyECDSASignature(publicKey *ecdsa.PublicKey, sigStructure, signature []byte) bool {
232+
keySize := curveKeySize(publicKey)
233+
if len(signature) != 2*keySize {
234+
return false
235+
}
236+
224237
hash, ok := hashForCurve(publicKey, sigStructure)
225-
if !ok || len(signature) != 2*len(hash) {
238+
if !ok {
226239
return false
227240
}
228241

229-
r := new(big.Int).SetBytes(signature[:len(hash)])
230-
s := new(big.Int).SetBytes(signature[len(hash):])
242+
r := new(big.Int).SetBytes(signature[:keySize])
243+
s := new(big.Int).SetBytes(signature[keySize:])
231244
return ecdsa.Verify(publicKey, hash, r, s)
232245
}
233246

@@ -242,7 +255,7 @@ func hashForCurve(publicKey *ecdsa.PublicKey, sigStructure []byte) ([]byte, bool
242255
case "P-384":
243256
sum := sha512.Sum384(sigStructure)
244257
return sum[:], true
245-
case "P-512":
258+
case "P-521":
246259
sum := sha512.Sum512(sigStructure)
247260
return sum[:], true
248261
default:

0 commit comments

Comments
 (0)