generate ECDSA P384 digest in Go fully compatible with openssl

125 Views Asked by At

I need to sign data using ECDSA P384 algorithm in Go. When I use native Go crypto/ecdsa to sign and verify, it works, but openssl cannot verify created signature. If I run openssl.exe as os.exec inside Go program, it obviously works. Maybe there is some package which could help me?

Here is my code:

func signECDSA(privateKey *ecdsa.PrivateKey, data []byte) ([]byte, error) {
    hash := sha512.Sum512(data)

    r, s, err := ecdsa.Sign(rand.Reader, privateKey, hash[:])
    if err != nil {
        return nil, err
    }

    // Prepare the signature struct with R and S components
    sig := struct {
        R, S *big.Int
    }{
        R: r,
        S: s,
    }

    // Marshal the signature struct to ASN.1 DER format
    signatureBytes, err := asn1.Marshal(sig)
    if err != nil {
        return nil, err
    }

    return signatureBytes, nil
}

openssl command I use to create the signature for the same input data is:

> openssl dgst -sha384 -sign private_key.pem -out signed_data input_data

To validate, I use the following command:

> openssl dgst -sha384 -verify public_key.pem -signature signed_data input_data
0

There are 0 best solutions below