I'm working on a PKCS#11 (
github.com/miekg/pkcs11) crypto.Signer and currently struggling to obtain 'r' and 's' in the right format as used by 'ecdsaSignature'. The same crypto.Signer is dealing with RSA signatures and is working fine.
When verifying the certificate that contains the embedded signature I get a "x509: ECDSA verification failure".
http://play.golang.org/p/KEDhy6akWQ
The issue is potentially caused by the way I'm splitting the signature as returned by the PKCS#11 interface (one array of 64 bytes for a P256 curve).
r := new(big.Int).SetBytes(sig[:len(sig)/2])
s := new(big.Int).SetBytes(sig[len(sig)/2:])
sig, err = asn1.Marshal(ecdsaSignature{r, s})
Which results in:
SEQUENCE(2 elem)
INTEGER(255 bit) 4926646066816546635773105742728194416509116305881183826812797818311257…
INTEGER(256 bit) 8001629261687403358200509917628748063517175240869055934673736255153843…
PKCS #11 Mechanisms v2.30 states:
"The signature octets correspond to the concatenation of the ECDSA values r and s, both represented as an octet string of equal length of at most nLen with the most significant byte first. If r and s have different octet length, the shorter of both must be padded with leading zero octets such that both have the same octet length. Loosely spoken, the first half of the signature is r and the second half is s."
I'm not padding the shorter of both (this could be the cause of the invalid siganture), but r and s they have the same length in bytes so there is nothing to add... is there?
Thanks,
Paul