Re: Hash of Public key

From: Valery Pryamikov (valery_at_harper.no)
Date: 05/23/05


Date: Mon, 23 May 2005 12:04:18 +0200


?????????
Why do you show me content of SNK file? Snk file is completely irrelevant.
myAssembly.GetName().GetPublicKey() doesn't use snk file, but retrieves
public key from the assembly.
Read my comment about content of snk file in my prev. post.

-Valery.
http://www.harper.no/valery

"Arkady Frenkel" <arkadyf@hotmailxdotx.com> wrote in message
news:%23nWEc72XFHA.2128@TK2MSFTNGP15.phx.gbl...
> Hi , Valery !
> What I received is next array in debugger
> ( exact copy of byte array of PublicKey.snk opened in VS and attached )
> that
> include unencoded 3 DWORDs ( I ask before about them ) and afterwards
> BLOBHEADER,RSAPUBKEY( which include exponent obviously ) and BLOB of
> modulus
> ( all in little-endian as shown ) .
>
> Wait for your comments
> Arkady
>
> P.S. I read, I'm reading and will (I'll promise ):) about ASN DER BER and
> all the siblings but up now I didn't decide what solution to recommend to
> UPnP committee.
>
> "Valery Pryamikov" <valery@harper.no> wrote in message
> news:eLhL0qwXFHA.228@TK2MSFTNGP12.phx.gbl...
>> myAssembly.GetName().GetPublicKey() returns you ASN1 DER encoded public
>> key
>> as
>> RSAPublicKey ::= SEQUENCE {
>> modulus INTEGER,
>> publicExponent INTEGER
>> }
>> it means - it contain sequence's tag, size of the content, and big-endian
>> integers for modulus and publicExponent. Modulus is prepended 0x0 byte
>> for
>> ensuring that leading bit will always be 0. Check your pk array to see
>> binary presentation. snk file contains information recorded in different
>> way
>> that is proprietary and undocumented (except for code that comes with
>> Rotor). Strongname.h only contains definition of PublicKeyBlob that
>> consist
>> of some extra information, but PublicKey inside of this structure is
>> ASN1.DER encoded structure. For playing with public keys (and other ASN
>> structures) you can use ASNDump utility (just Google for it).
>> Arkady, I've told you about ASN1. from my first post to that thread. Can
>> you, please, check the information I already gave you before asking any
>> further questions...
>>
>> -Valery.
>> http://www.harper.no/valery
>>
>>
>> "Arkady Frenkel" <arkadyf@hotmailxdotx.com> wrote in message
>> news:%23ejRPZiXFHA.3140@TK2MSFTNGP14.phx.gbl...
>>> Let's continue:)..
>>> I'll return to ASN.DER afterwards but now I want to dig into .NET
>>> format.
>>> I did keypair file with sn.exe end extracted public key from it to file.
>>> Now I can read it and make the hash of it with next :
>>>
>>> myAssemblyName.KeyPair = new StrongNameKeyPair(File.Open("KeyPair.snk",
>>> FileMode.Open, FileAccess.Read));
>>>
>>> ...
>>>
>>> byte []pk;
>>>
>>> pk = myAssembly.GetName().GetPublicKey();
>>>
>>> SHA1Managed sha = new SHA1Managed() ;
>>>
>>> byte[] p = sha.ComputeHash(pk,0,pk.Length ) ;.
>>>
>>> byte []pt;
>>>
>>> pt = myAssembly.GetName().GetPublicKeyToken();
>>>
>>> And I see that reversed 8 last bytes of p[] are the same as
>>> GetPublicKeyToken() return , so I see that all 160 bytes of Public key
>>> .Net struct used for hash .
>>>
>>> I want to ask what is 3 dwords before PUBLICKEYSTRUCT ? The last from 3
>>> is
>>> length ( 148 ) of BLOBHEADER(PUBLICKEYSTRUC)+RSAPUBKEY+
>>> PUBLICKEYBLOB.
>>> Are first and second are : ALG_ID & CALG_SHA1 ?They are 0x2400 &
>>> 0x8004
>>> in my case.
>>>
>>> TIA
>>> Arkady
>>>
>>> "Arkady Frenkel" <arkadyf@hotmailxdotx.com> wrote in message
>>> news:%23NVeFhHXFHA.3348@TK2MSFTNGP14.phx.gbl...
>>>> Tnx Valery , I'm out of office and have take some to learn the stuff
>>>> Arkady
>>>>
>>>> "Valery Pryamikov" <valery@harper.no> wrote in message
>>>> news:OHiMnPGXFHA.3716@TK2MSFTNGP12.phx.gbl...
>>>>> Well, that was the first time you actually revealed some of your
>>>>> requirements :-).
>>>>> in that case - you need to hash ASN1.DER encodded public key structure
>>>>> as it is specified in PKCS#1. This is the same form of public key as
>>>>> publickey blob stored inside X509 certificates and you can do it with
>>>>> managed code only. In case if you can use interop to CAPI, or use .Net
>>>>> 2.0. - it would spare you some low-level conversion work.
>>>>> Check Mitch Gallant's web site for good description of ASN1 encoding
>>>>> from .Net framework:
>>>>> http://www.jensign.com/JavaScience/dotnet/JKeyNet/index.html
>>>>>
>>>>> For a sample aplication that uses a lot of C# to CAPI interop and does
>>>>> lots of public key related processing you can check my old PPC
>>>>> Certificate Manager sample:
>>>>> http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=68b9e2f2-7c36-417f-a218-e80557ae2957
>>>>>
>>>>> -Valery.
>>>>> http://www.harper.no/valery
>>>>>
>>>>>
>>>>> "Arkady Frenkel" <arkadyf@hotmailxdotx.com> wrote in message
>>>>> news:e6l23zFXFHA.584@TK2MSFTNGP15.phx.gbl...
>>>>>> No , I can't do it on my own .
>>>>>> I need the way MSFT use it ( at least ) because I do it for UPnP
>>>>>> device
>>>>>> and other devices ( not mine obviously like PC can be UPnP device too
>>>>>> ,
>>>>>> WiFi , cameras and so on ) receive that hashed value as
>>>>>> identification
>>>>>> ID due to UPnP security spec, so I can't do on my own but at least as
>>>>>> MSFT do that and in managed code ( so can't use (C)API ).
>>>>>>
>>>>>> Arkady
>>>>>>
>>>>>> "Valery Pryamikov" <valery@harper.no> wrote in message
>>>>>> news:ulQr0ZFXFHA.3712@TK2MSFTNGP09.phx.gbl...
>>>>>>>I answered you several times you can hash your public key in many
>>>>>>>different ways - all depends on your needs. Ex:
>>>>>>> 1. Calculate hash on concatenation of little endian modulus with
>>>>>>> little endian exponent without conversions (little endian is
>>>>>>> presentation used by CAPI);
>>>>>>> 2. Calculate hash on concatenation of little endian exponent with
>>>>>>> little endian modulus without conversions;
>>>>>>> 3. Calculate hash of little endian modulus and state that you are
>>>>>>> expecting standard recommended public exponent only 0x1001 and fail
>>>>>>> processing if exponent is different.
>>>>>>> 4. Do as in 1, but convert both values to big endian;
>>>>>>> 5. Do as in 2, but convert both values to big endian;
>>>>>>> 6. Do as in 3, but convert modulus to big endian;
>>>>>>> 7. Do as in 1, but convert both values to ASN1;
>>>>>>> 8. Do as in 2, but convert both values to ASN1;
>>>>>>> 9. Do as in 3, but convert modulus to ASN1;
>>>>>>> 7. Do as in 1, but pack both values into xml format;
>>>>>>> 8. Do as in 2, but pack both values into xml format;
>>>>>>> 9. Do as in 3, but pack modulus into xml format;
>>>>>>> 10. any variation of all above;
>>>>>>> 11. Use CAPI's CryptExportKey and calculate hash on PLAINTEXTBLOB;
>>>>>>> 12. Use CAPI's CryptExportKey and calculate hash on PUBLICKEYBLOB;
>>>>>>> 13. Use CAPI's CryptEncodeObjectEx to pack public key into ASN1.DER
>>>>>>> encoded structure of public key as specified in PKCS#1 and calculate
>>>>>>> hash on that blob;
>>>>>>> 14. In case if your public key is stored in certificate it is
>>>>>>> already
>>>>>>> in the form as in 13 - retrieve public key blob and calculate hash
>>>>>>> on
>>>>>>> that blob;
>>>>>>>
>>>>>>> I can add a hundred more practical suggestions on how to hash public
>>>>>>> key. You didn't provide us with concrete requirements to make a
>>>>>>> single
>>>>>>> practical suggestion. I have tried to explain that to you several
>>>>>>> times. I did write that public key could be hashed in many different
>>>>>>> ways depending on your requirements in several of my posts to that
>>>>>>> thread.
>>>>>>>
>>>>>>> -Valery.
>>>>>>> http://www.harper.no/valery
>>>>>>>
>>>>>>> "Arkady Frenkel" <arkadyf@hotmailxdotx.com> wrote in message
>>>>>>> news:OZbLClEXFHA.1508@tk2msftngp13.phx.gbl...
>>>>>>>>I ask very simple question and really want to read practical advice
>>>>>>>>:
>>>>>>>>how to calculate hash from public key , if you ( Valery ) want to
>>>>>>>>think as I have to calculate public key token ( really I don't need
>>>>>>>>that ) , but before to receive last 8 bytes ( token ) I have to
>>>>>>>>receive all hash and that is my simple question. Do I need to send
>>>>>>>>array of bytes of Modulus only or both ( Modulus and Exponent which
>>>>>>>>is
>>>>>>>>3 ( that I wrote about ) in the case of RSA? I don't need theories
>>>>>>>>of
>>>>>>>>security algorithms and their output , just practical answer on my
>>>>>>>>simple question.
>>>>>>>> I have ( C# code ):
>>>>>>>> RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
>>>>>>>>
>>>>>>>> RSAParameters rsaresult = new RSAParameters();
>>>>>>>>
>>>>>>>> rsaresult = rsa.ExportParameters(false);
>>>>>>>>
>>>>>>>> Now I have public key in the rsaresult and want to know if modulus
>>>>>>>> will be enough for hash of public key , that's all I need . Like to
>>>>>>>> extract token ( last reverced 8 bytes ) afterwards e.g.
>>>>>>>>
>>>>>>>> byte[] PKbytes = new byte [ rsaresult.Modulus.GetLength(0)/*
>>>>>>>> probably
>>>>>>>> or something else + rsaresult.Exponent.GetLength(0) */];
>>>>>>>>
>>>>>>>> rsaresult.Modulus.CopyTo(PKbytes,0);
>>>>>>>>
>>>>>>>> // probably or something else
>>>>>>>>
>>>>>>>> //
>>>>>>>> rsaresult.Exponent.CopyTo(PKbytes,rsaresult.Modulus.GetLength(0));
>>>>>>>>
>>>>>>>> SHA1Managed sha = new SHA1Managed() ;
>>>>>>>>
>>>>>>>> byte[] b = sha.ComputeHash(PKbytes,0,PKbytes.Length ) ;
>>>>>>>>
>>>>>>>> All I want to know what have to be in PKbytes array to receive hash
>>>>>>>> of
>>>>>>>>
>>>>>>>> public key.
>>>>>>>>
>>>>>>>> All I want practical answer on practical question ( please no
>>>>>>>> theory
>>>>>>>> , I have enough books around me )
>>>>>>>>
>>>>>>>> Arkady
>>>>>>>>
>>>>>>>> "Valery Pryamikov" <valery@harper.no> wrote in message
>>>>>>>> news:eH09eNDXFHA.1044@TK2MSFTNGP10.phx.gbl...
>>>>>>>>> "Arkady Frenkel" <arkadyf@hotmailxdotx.com> wrote in message
>>>>>>>>> news:u9T$di%23WFHA.1796@TK2MSFTNGP15.phx.gbl...
>>>>>>>>>> That not correct , exponent is 3 not 4 bytes. OTOH only 128 bytes
>>>>>>>>>> used to calculate hash of public key not 131 and not 132.But both
>>>>>>>>>> of you didn't answer my simple question what is algorithm of
>>>>>>>>>> callculation hash from public key so I see I have to try other
>>>>>>>>>> group, indirectly I see both in Windows and open source ( free
>>>>>>>>>> ssl ) modulus used only but I'm not sure 100%
>>>>>>>>>> Arkady
>>>>>>>>>>
>>>>>>>>> Hmm,
>>>>>>>>> at first you are asking a completely meaningless question. We've
>>>>>>>>> tried to clarify your requirements hoping that would show you the
>>>>>>>>> answers that you were looking for. Instead you simply repeat your
>>>>>>>>> meaningless question with extra mumbling about some number that
>>>>>>>>> you
>>>>>>>>> taken from somewhere...
>>>>>>>>> How to hash a public key? You take a hash function, send public
>>>>>>>>> key
>>>>>>>>> as a parameter to a hash function and get the result. That's at
>>>>>>>>> least how it is usually done.
>>>>>>>>> And your "That not correct..." is in fact completely wrong.
>>>>>>>>> Modulus and exponent are not measured in bytes, but in bits. You
>>>>>>>>> are
>>>>>>>>> referring to 1024 bit modulus that becomes less and less used due
>>>>>>>>> to
>>>>>>>>> simple fact that is providing less than 80 bits of security (and
>>>>>>>>> just a couple of weeks ago 740 bits modulus 200 decimal digits was
>>>>>>>>> successfully factored).
>>>>>>>>> Public exponent could be any number from 3 up to large integer
>>>>>>>>> that
>>>>>>>>> takes a half of bits of modulus (not bigger than that, since small
>>>>>>>>> private exponents are vulnerable to Weiner's attack). The only
>>>>>>>>> requirement for public and private exponents in rsa is that e*d=1
>>>>>>>>> mod phi(m), where phi(m) is Euler's phi function and is
>>>>>>>>> (p-1)*(q-1).
>>>>>>>>> In fact, on other platforms than Windows it's quite usual to have
>>>>>>>>> public exponent of the same size as private exponent and about
>>>>>>>>> half-size of modulus, because in this case you actually can use
>>>>>>>>> public key and private key interchangeably...
>>>>>>>>>
>>>>>>>>> But you know, what? I think that you were not really asking for
>>>>>>>>> help....
>>>>>>>>>
>>>>>>>>> -Valery.
>>>>>>>>> http://www.harper.no/valery
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>
>
>



Relevant Pages

  • Re: Hash of Public key
    ... Why do you show me content of SNK file? ... public key from the assembly. ... >> modulus INTEGER, ... >> integers for modulus and publicExponent. ...
    (microsoft.public.security)
  • Re: Hash of Public key
    ... integers for modulus and publicExponent. ... Arkady, ... > I did keypair file with sn.exe end extracted public key from it to file. ...
    (microsoft.public.security)
  • Re: Hash of Public key
    ... integers for modulus and publicExponent. ... Arkady, ... > I did keypair file with sn.exe end extracted public key from it to file. ...
    (microsoft.public.platformsdk.security)
  • Re: Hash of Public key
    ... Just a minor correction - it's DumpASN utility (not ASNDump as I wrote in my ... > integers for modulus and publicExponent. ...
    (microsoft.public.security)
  • Re: Hash of Public key
    ... Just a minor correction - it's DumpASN utility (not ASNDump as I wrote in my ... > integers for modulus and publicExponent. ...
    (microsoft.public.platformsdk.security)

Quantcast