Is there a way to check, (with openssl command for example), if a given public key match with a private key ?
Thanks
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It only takes a minute to sign up.
Sign up to join this communityIs there a way to check, (with openssl command for example), if a given public key match with a private key ?
Thanks
One method works with any signature scheme and any program including OpenSSL: make a signature of a file with the private key, and check signature and file against the public key.
If the check is OK, then private and public key match (or the signature scheme is broken).
If there is some failure, then private and public key do not match (or the program is misused, incomptible with the key formats, or broken).
Example with openssl, showing Verified OK
for matched private/public keys tstpri.pem
/tstpub.pem
; Verification Failure
for unmatched keys altpri.pem
/tstpub.pem
.
# generate an secp256k1 private key
openssl ecparam -genkey -noout -name secp256k1 -out tstpri.pem
# extract the public key
openssl ec -in tstpri.pem -pubout -out tstpub.pem >/dev/null
# generate an alternate secp256k1 private key
openssl ecparam -genkey -noout -name secp256k1 -out altpri.pem
# create a file fil (any content will do)
echo foo >fil
# sign fil with private key
openssl dgst -sha256 -sign tstpri.pem -out tst.sig fil
# verify the signature with matching public key
openssl dgst -sha256 -verify tstpub.pem -signature tst.sig fil
Verified OK
# sign fil with alternate private key
openssl dgst -sha256 -sign altpri.pem -out tst.sig fil
# verify the signature with non-matching public key
openssl dgst -sha256 -verify tstpub.pem -signature tst.sig fil
Verification Failure
Assuming you have the EC private key file in pem format (private.key), and the EC public key file in pem format (publick.key), you can verify that the two files match by deriving the public key from the private key file, like so:
openssl ec -pubout -in private.key
The output of this command should match the contents of public.key.
It appears we're talking about digital signature key pairs. If that's the case it's very easy to do. Say we have a public verification key $\mathsf{vk}$ and a candidate private signing key $\mathsf{sk}$
As long as the signature scheme is unforgeable, a signature produced using an unrelated signing key will not verify with overwhelming probability.
openssl dgst -sign
will create a ASN.1 metadata around the actual signature, so it may not be immediately usable for Bitcoin-related tasks specifically. $\endgroup$ – xorhash May 3 '20 at 12:44