0
$\begingroup$

:-) Hello

  1. I was wondering if the signature hash algorithm was a part of the private key or just the signing process?

  2. Should a signature instance depend on the private key? (Of course, still talking about ECDSA)

  3. If I don't sha256 my data before calling a signature instance SHA256withECDSA. Does this have the same effect as sha256 my data before calling a signature instance NONEwithECDSA?

  4. In other way, are these two pieces of Java code are equivalent:

PrivateKey privateKey = pk;
byte[] dataToSign = data;
Signature signature = Signature.getInstance("SHA256withECDSA");
signature.initSign(privateKey);
byte[] signedData = signature.update(dataToSign);
PrivateKey privateKey = pk;
byte[] dataToSign = SHA256(data);
Signature signature = Signature.getInstance("NONEwithECDSA");
signature.initSign(privateKey);
byte[] signedData = signature.update(dataToSign);

Thank you

Edit

From a keypair, I can't find anything about the hash algorithm. But there are two mentions of it in the certificate.

So the way I understand, it is just a flag to say "Hey look at my certificate, I SHA256'd my data before signing it so if you want to check the signature, make sure to SHA256'd yours"

Am I close of how it works? (or just a bit fool)

Keypair details

Edit: Add ASN.1 data

The private Key (No mention EcdsaWithSHA256)

SEQUENCE
{
    INTEGER=0
    SEQUENCE
    {
        OBJECT IDENTIFIER=EcPublicKey (1.2.840.10045.2.1)
        OBJECT IDENTIFIER=Prime256v1 (1.2.840.10045.3.1.7)
    }
    OCTET STRING, encapsulates:
        SEQUENCE
        {
            INTEGER=1
            OCTET STRING=
                AD 73 F5 EA 9D 51 3E 91   ­sõê.Q>.
                09 54 9D B6 77 D9 AB 11   .T.¶wÙ«.
                88 EA 2B 86 D9 0F B9 65   .ê+.Ù.¹e
                9E CF E6 77 FF E7 8F 73   .Ïæwÿç.s
        }

}

The public key (No mention EcdsaWithSHA256)

SEQUENCE
{
    SEQUENCE
    {
        OBJECT IDENTIFIER=EcPublicKey (1.2.840.10045.2.1)
        OBJECT IDENTIFIER=Prime256v1 (1.2.840.10045.3.1.7)
    }
    BIT STRING=
        04 F1 73 E0 93 95 A7 FF   .ñsà..§ÿ
        D1 F9 CB 4A E7 A5 7C 97   ÑùËJç¥|.
        BC 85 FF 05 21 90 D0 9E   ¼.ÿ.!.Ð.
        BF FB DC F4 66 FD A2 D2   ¿ûÜôfý¢Ò
        2C 38 37 E6 9D 9C 5A 78   ,87æ..Zx
        10 AA 6D F1 36 43 DA 69   .ªmñ6CÚi
        15 ED 12 59 D8 21 78 20   .í.YØ!x 
        11 89 E7 B8 34 B5 28 FF   ..ç¸4µ(ÿ
        B9                        ¹
}

The Certificate (Mention EcdsaWithSHA256)

SEQUENCE
{
    SEQUENCE
    {
        TAGGED [0]:
            INTEGER=2
        INTEGER=139084911 (0x84a446f)
        SEQUENCE
        {
            OBJECT IDENTIFIER=EcdsaWithSHA256 (1.2.840.10045.4.3.2)
            NULL
        }
        SEQUENCE
        {
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=CountryName (2.5.4.6)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=StateOrProvinceName (2.5.4.8)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=LocalityName (2.5.4.7)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=OrganizationName (2.5.4.10)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=OrganizationalUnitName (2.5.4.11)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=CommonName (2.5.4.3)
                    PRINTABLE STRING='Unknown'
                }
            }
        }
        SEQUENCE
        {
            UTC TIME=22/sept./2022 08:28:22 CEST (220922062822GMT+00:00)
            UTC TIME=21/déc./2022 07:28:22 CET (221221062822GMT+00:00)
        }
        SEQUENCE
        {
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=CountryName (2.5.4.6)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=StateOrProvinceName (2.5.4.8)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=LocalityName (2.5.4.7)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=OrganizationName (2.5.4.10)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=OrganizationalUnitName (2.5.4.11)
                    PRINTABLE STRING='Unknown'
                }
            }
            SET
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=CommonName (2.5.4.3)
                    PRINTABLE STRING='Unknown'
                }
            }
        }
        SEQUENCE
        {
            SEQUENCE
            {
                OBJECT IDENTIFIER=EcPublicKey (1.2.840.10045.2.1)
                OBJECT IDENTIFIER=Prime256v1 (1.2.840.10045.3.1.7)
            }
            BIT STRING=
                04 F1 73 E0 93 95 A7 FF   .ñsà..§ÿ
                D1 F9 CB 4A E7 A5 7C 97   ÑùËJç¥|.
                BC 85 FF 05 21 90 D0 9E   ¼.ÿ.!.Ð.
                BF FB DC F4 66 FD A2 D2   ¿ûÜôfý¢Ò
                2C 38 37 E6 9D 9C 5A 78   ,87æ..Zx
                10 AA 6D F1 36 43 DA 69   .ªmñ6CÚi
                15 ED 12 59 D8 21 78 20   .í.YØ!x 
                11 89 E7 B8 34 B5 28 FF   ..ç¸4µ(ÿ
                B9                        ¹
        }
        TAGGED [3]:
            SEQUENCE
            {
                SEQUENCE
                {
                    OBJECT IDENTIFIER=SubjectKeyIdentifier (2.5.29.14)
                    OCTET STRING, encapsulates:
                        OCTET STRING=
                            2C 8C 93 ED 48 FD 94 A4   ,..íHý.¤
                            08 CD 0F 33 8A A1 0C F5   .Í.3.¡.õ
                            F7 CD B3 CD               ÷ͳÍ

                }
            }
    }
    SEQUENCE
    {
        OBJECT IDENTIFIER=EcdsaWithSHA256 (1.2.840.10045.4.3.2)
        NULL
    }
    BIT STRING, encapsulates:
        SEQUENCE
        {
            INTEGER=
                0A 6D 35 87 32 88 62 12   .m5.2.b.
                FD 5F AE 23 AC 58 E7 9D   ý_®#¬Xç.
                DC 92 64 50 8C 6C 0B 1B   Ü.dP.l..
                FF 2A 35 02 11 10 8E 5D   ÿ*5....]
            INTEGER=
                32 9B 25 4D 2B DD 12 97   2.%M+Ý..
                01 1E F6 DA 02 86 4D ED   ..öÚ..Mí
                C2 B2 2F 79 5C 57 24 2F   ²/y\W$/
                4E F6 8E 72 88 B7 09 A6   Nö.r.·.¦
        }

}
New contributor
Pontotos is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
0

1 Answer 1

1
$\begingroup$

I'm not sure about the Java code, but whether or not a keypair has a hash function associated with it depends on its AlgorithmIdentifier, which usually contains ASN.1 OID for the domain parameters as well as the said hash function.

You see, the standards for RSA (PKCS #1) and ECC (SEC #1, #2) define data structures for keys using the ASN.1 syntax.

Update for Comments

I can't find the hash algorithm in the private key, so does this mean the hash doesn't apply directly in during a signing process?

No. A hash function is still needed during signing (and verification), except that it can be any hash function chosen by your program, at the time the signature is computed.

Here's an excerpt of SEC #1 ver.2 Appendix C.5, with OIDs for ECDSA instantiated with various hash functions:

ecdsa-with-SHA1 OBJECT IDENTIFIER ::= { id-ecSigType sha1(1)} 
ecdsa-with-Recommended OBJECT IDENTIFIER ::= { id-ecSigType recommended(2) } 
ecdsa-with-Specified OBJECT IDENTIFIER ::= { id-ecSigType specified(3)} 
ecdsa-with-Sha224 OBJECT IDENTIFIER ::= { id-ecSigType specified(3) 1 } 
ecdsa-with-Sha256 OBJECT IDENTIFIER ::= { id-ecSigType specified(3) 2 } 
ecdsa-with-Sha384 OBJECT IDENTIFIER ::= { id-ecSigType specified(3) 3 } 
ecdsa-with-Sha512 OBJECT IDENTIFIER ::= { id-ecSigType specified(3) 4 } 
id-ecSigType OBJECT IDENTIFIER ::= { ansi-X9-62 signatures(4) }
$\endgroup$
4
  • $\begingroup$ I edited and added a picture of ASN.1 data from a keypair. I can't find the hash algorithm in the private key, so does this mean the hash doesn't apply directly in during a signing process? (I do my best to be clear, I'm not vocabulary friendly with crypto) $\endgroup$
    – Pontotos
    Sep 22 at 13:55
  • $\begingroup$ @Pontotos Sorry but, my national firewall blocks Imgur, which provides image hosting service for the SE network. Can you convert it into a textual hex dump (od on POSIX, and there's UltraEdit for Windows). Also, if you can operate OpenSSL command-line utility, a data structure dump can be made. $\endgroup$
    – DannyNiu
    Sep 23 at 4:05
  • $\begingroup$ I added ASN.1 data to the post. $\endgroup$
    – Pontotos
    Sep 23 at 7:14
  • $\begingroup$ Ok, now I get it! The document really accurate and I understand much better the ASN.1 representation. Thank you so much @DannyNiu! $\endgroup$
    – Pontotos
    Sep 23 at 10:34

Your Answer

Pontotos is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.