Crypto RSAWithSHA1 sign

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

Crypto RSAWithSHA1 sign

Denis Kudriashov
Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test


Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test





Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test





Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test






RSASHA1SignatureFix.1.cs (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Denis Kudriashov
Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test










RSAPrivateKey-v15SignMessageHash.st (910 bytes) Download Attachment
CryptoRSATest-testRSASHASigning.st (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
Thanks for this, Denis.   I will add it to the CryptoCore package tonight, after work.
 
I investigated the int you are producing versus the int I was producing, as I thought #asInteger was dealing with little-endian already.   Sure enough, the message bytes were in the correct locations.  The problem was the length of the byteArray I was converting to a LargePositiveInteger.  I had 256 while you have 128 (255 and 127 since the MSBit is 0).  I looked at the spec again and section 4.2 on page 9 discusses converting byteArray to int using 256 size.  I am confused by this.
 
I attached another version of RSAPrivateKey>>v15SignMessageHash: encodedMsg that works, by changing the byteArray size.
 
RSAPrivateKey>>v15SignMessageHash: encodedMsg
 
    | padded toBeSigned |
    padded := ByteArray new: (128 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.

    ^ (self crypt: toBeSigned asInteger) asByteArray.
I will use your version since it looks like it handles other array sizes (p*q) digitLength - 1 and it also does not create many arrays, just inserts into the right locations of a LargePositiveInteger.
 
Thanks for the test case!
 
Cheers,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test











Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
In reply to this post by Denis Kudriashov
Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test











Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Denis Kudriashov
Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>
Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test















Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Denis Kudriashov
Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>
Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test

















pubkey.pem (372 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Denis Kudriashov
I found when I join private and public keys my code work good and I get RSAKey instance.

I join it by:

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ==
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.


Why I cant read public key separetelly?

Best regards,
Denis

2010/9/23 Denis Kudriashov <[hidden email]>
Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>

Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test

















Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Denis Kudriashov
And verification work good.

I attatch new tests

2010/9/23 Denis Kudriashov <[hidden email]>
I found when I join private and public keys my code work good and I get RSAKey instance.

I join it by:

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ==

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.


Why I cant read public key separetelly?

Best regards,
Denis

2010/9/23 Denis Kudriashov <[hidden email]>

Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>

Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test



















CryptoRSATest.st (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
Denis,
 
I found that when you join the "privateKey" bytes with the publicKey bytes it does nothing.  The "privateKey" bytes alone carry the public key with the private key.  The Der bytes result in a 9 element OrderedCollection and fields 2 and 3 are the exponent and modulus for the public key.
 
So I run the following to get both keys:
 

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ=='.
 

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey.
publicKey := r asPublicKey.
privateKey := r asPrivateKey.
{publicKey. privateKey}
 
 
Now I look at the publicKey you gave me and the 2 elements of a OrderedCollection.  The second element is a BitString and if you reach in a grab the bytes, they are also DER encoded.  So decode those and you get the exponent and modulus of the publicKey.  I wrote a class attached to process an RSA Public Key.  I used the code below to process it:
 
key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.
 
derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPublicKeyFileReader new.
r bytes: derKey .
publicKey := r asPublicKey .
 
Thanks for the test!
 
Cheers,
Rob

Sent: Thursday, September 23, 2010 3:33 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

And verification work good.

I attatch new tests

2010/9/23 Denis Kudriashov <[hidden email]>
I found when I join private and public keys my code work good and I get RSAKey instance.

I join it by:

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ==

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.


Why I cant read public key separetelly?

Best regards,
Denis

2010/9/23 Denis Kudriashov <[hidden email]>

Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>

Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test





















RSAPublicKeyFileReader.st (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Denis Kudriashov
Thank you very much Rob.

All good ok.

2010/9/23 Rob Withers <[hidden email]>
Denis,
 
I found that when you join the "privateKey" bytes with the publicKey bytes it does nothing.  The "privateKey" bytes alone carry the public key with the private key.  The Der bytes result in a 9 element OrderedCollection and fields 2 and 3 are the exponent and modulus for the public key.
 
So I run the following to get both keys:
 

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ=='.
 

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey.
publicKey := r asPublicKey.
privateKey := r asPrivateKey.
{publicKey. privateKey}
 
 
Now I look at the publicKey you gave me and the 2 elements of a OrderedCollection.  The second element is a BitString and if you reach in a grab the bytes, they are also DER encoded.  So decode those and you get the exponent and modulus of the publicKey.  I wrote a class attached to process an RSA Public Key.  I used the code below to process it:
 
key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.
 
derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPublicKeyFileReader new.
r bytes: derKey .
publicKey := r asPublicKey .
 
Thanks for the test!
 
Cheers,
Rob

Sent: Thursday, September 23, 2010 3:33 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

And verification work good.

I attatch new tests

2010/9/23 Denis Kudriashov <[hidden email]>
I found when I join private and public keys my code work good and I get RSAKey instance.

I join it by:

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ==

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.


Why I cant read public key separetelly?

Best regards,
Denis

2010/9/23 Denis Kudriashov <[hidden email]>

Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>

Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test
























Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
I published our code changes into both the Cryptography repository's Cryptography package and in the Inbox's Crypto packages.
 
All tests are green.
 
Thanks, Denis!

Sent: Thursday, September 23, 2010 5:10 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Thank you very much Rob.

All good ok.

2010/9/23 Rob Withers <[hidden email]>
Denis,
 
I found that when you join the "privateKey" bytes with the publicKey bytes it does nothing.  The "privateKey" bytes alone carry the public key with the private key.  The Der bytes result in a 9 element OrderedCollection and fields 2 and 3 are the exponent and modulus for the public key.
 
So I run the following to get both keys:
 

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ=='.
 

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey.
publicKey := r asPublicKey.
privateKey := r asPrivateKey.
{publicKey. privateKey}
 
 
Now I look at the publicKey you gave me and the 2 elements of a OrderedCollection.  The second element is a BitString and if you reach in a grab the bytes, they are also DER encoded.  So decode those and you get the exponent and modulus of the publicKey.  I wrote a class attached to process an RSA Public Key.  I used the code below to process it:
 
key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.
 
derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPublicKeyFileReader new.
r bytes: derKey .
publicKey := r asPublicKey .
 
Thanks for the test!
 
Cheers,
Rob

Sent: Thursday, September 23, 2010 3:33 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

And verification work good.

I attatch new tests

2010/9/23 Denis Kudriashov <[hidden email]>
I found when I join private and public keys my code work good and I get RSAKey instance.

I join it by:

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ==

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.


Why I cant read public key separetelly?

Best regards,
Denis

2010/9/23 Denis Kudriashov <[hidden email]>

Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>

Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test


























Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Denis Kudriashov
Hello Rob.

I found another issue - rsa keys generation.
Method RSAKeyPairGenerator>>privateKey returns RSAKey instance instead of RSAPrivateKey instance. I add test

testSignVerificationByGeneratedKeys

    | signBytes gen |
    gen := RSAKeyPairGenerator bits: 1024.   
    gen computePrimes.
   
    signBytes := gen privateKey v15SignMessage: 'hello'. 
   
    self assert: (gen publicKey v15Verify: signBytes isSignatureOf: 'hello')

And this tests broken with existed functionallity.

Now I fix it with some based on VW classes. I attatch all with test. (its green)


2010/9/23 Rob Withers <[hidden email]>
I published our code changes into both the Cryptography repository's Cryptography package and in the Inbox's Crypto packages.
 
All tests are green.
 
Thanks, Denis!

Sent: Thursday, September 23, 2010 5:10 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Thank you very much Rob.

All good ok.

2010/9/23 Rob Withers <[hidden email]>
Denis,
 
I found that when you join the "privateKey" bytes with the publicKey bytes it does nothing.  The "privateKey" bytes alone carry the public key with the private key.  The Der bytes result in a 9 element OrderedCollection and fields 2 and 3 are the exponent and modulus for the public key.
 
So I run the following to get both keys:
 

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ=='.
 

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey.
publicKey := r asPublicKey.
privateKey := r asPrivateKey.
{publicKey. privateKey}
 
 
Now I look at the publicKey you gave me and the 2 elements of a OrderedCollection.  The second element is a BitString and if you reach in a grab the bytes, they are also DER encoded.  So decode those and you get the exponent and modulus of the publicKey.  I wrote a class attached to process an RSA Public Key.  I used the code below to process it:
 
key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.
 
derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPublicKeyFileReader new.
r bytes: derKey .
publicKey := r asPublicKey .
 
Thanks for the test!
 
Cheers,
Rob

Sent: Thursday, September 23, 2010 3:33 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

And verification work good.

I attatch new tests

2010/9/23 Denis Kudriashov <[hidden email]>
I found when I join private and public keys my code work good and I get RSAKey instance.

I join it by:

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ==

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.


Why I cant read public key separetelly?

Best regards,
Denis

2010/9/23 Denis Kudriashov <[hidden email]>

Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>

Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test































EuclidAlgorithm.st (4K) Download Attachment
RSAKeyPairGenerator.st (3K) Download Attachment
CryptoRSATest-testSignVerificationByGeneratedKeys.st (578 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
Hi Denis,
 
Nice find and thanks for fixing it and testing it.  I have integrated it into the various Cryptography packages (*see below*) and published.  All tests green.
 
I am maintaining 2 parallel Monticello package streams for Cryptography until we can figure out what will go into the Trunk and what will remain separate.  The original Cryptography package in the Cryptography repository has been updated.   The new CryptoCore, CryptoCoreTests, CryptoExtras, CryptoExtrasTests, CryptoCerts nad CryptoCertsTests have all been updated and published to the Inbox.  All tests green in both streams.
 
Rob

Sent: Tuesday, September 28, 2010 6:21 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello Rob.

I found another issue - rsa keys generation.
Method RSAKeyPairGenerator>>privateKey returns RSAKey instance instead of RSAPrivateKey instance. I add test

testSignVerificationByGeneratedKeys

    | signBytes gen |
    gen := RSAKeyPairGenerator bits: 1024.   
    gen computePrimes.
   
    signBytes := gen privateKey v15SignMessage: 'hello'. 
   
    self assert: (gen publicKey v15Verify: signBytes isSignatureOf: 'hello')

And this tests broken with existed functionallity.

Now I fix it with some based on VW classes. I attatch all with test. (its green)


2010/9/23 Rob Withers <[hidden email]>
I published our code changes into both the Cryptography repository's Cryptography package and in the Inbox's Crypto packages.
 
All tests are green.
 
Thanks, Denis!

Sent: Thursday, September 23, 2010 5:10 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Thank you very much Rob.

All good ok.

2010/9/23 Rob Withers <[hidden email]>
Denis,
 
I found that when you join the "privateKey" bytes with the publicKey bytes it does nothing.  The "privateKey" bytes alone carry the public key with the private key.  The Der bytes result in a 9 element OrderedCollection and fields 2 and 3 are the exponent and modulus for the public key.
 
So I run the following to get both keys:
 

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ=='.
 

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey.
publicKey := r asPublicKey.
privateKey := r asPrivateKey.
{publicKey. privateKey}
 
 
Now I look at the publicKey you gave me and the 2 elements of a OrderedCollection.  The second element is a BitString and if you reach in a grab the bytes, they are also DER encoded.  So decode those and you get the exponent and modulus of the publicKey.  I wrote a class attached to process an RSA Public Key.  I used the code below to process it:
 
key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.
 
derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream )
contents.
 
r := RSAPublicKeyFileReader new.
r bytes: derKey .
publicKey := r asPublicKey .
 
Thanks for the test!
 
Cheers,
Rob

Sent: Thursday, September 23, 2010 3:33 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

And verification work good.

I attatch new tests

2010/9/23 Denis Kudriashov <[hidden email]>
I found when I join private and public keys my code work good and I get RSAKey instance.

I join it by:

key64 := 'MIICXgIBAAKBgQDkI45GM6mYLlbxrKozE6bXWMoxvtVuIWOIF1KcGLED+4Gipriw
Nt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYhATtvODR1nD/dl0JpFH7BLcD9
NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZImTdErllmhzrTKT3YQIDAQAB
AoGBALPQjogomii3hZHQ3QmLGLqtYhjZaBH4wSF3+IXONF+GMcRIklNZuuuGPKt/
JjrUOh4fBqFJDuO3u+aXcx45MRMLVHuQIuUbegJXVS+rnxXI3I6I4SLBkoV7Jqn/
J9T9biOXrzq/xN8XVJQm7zq/FXaHR6l+Wo50LaBj7llY+CMBAkEA8bfsbySseTbI
D7tR/bytEz/DhQ1knKS3nFo83NYwDm7YcdGC+f0rQIUuS30lKApeoygBUiLOSs7K
euxEi6wC0QJBAPGePI59Fc5alAivyTkYdV4sbIL+SL5oXEERRyezogEgRqCGJtyd
MbnVviwREF4MiGTYQIIOx4aFrM/U4q9DL5ECQH+/QImMzEpTlXAbA74iFSZzMJYE
+gN/WjqbxkbAPC2kj2e33ozYLB+xQ0JKJXT/5fw8jFYoZvZKS+CjNabLhcECQQDp
bgrrToXGu1PRoKMzeiHKPfkIBUuaSZD3fA3WmYGmvNv/MhxRf70O4SW9xS6e7lTH
uPV5sXWqzXLLx8zJrotBAkEAgAoiR+5uOW9CgfWkblJ/YIgGbLiLQoFNZbwxXDfr
jcosTQvBjrc//rCnb2Pgm8QRGeN/CXKc9kKoWNqUQSYchQ==

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.


Why I cant read public key separetelly?

Best regards,
Denis

2010/9/23 Denis Kudriashov <[hidden email]>

Hello, Rob

I attatch public key in pem format (it corresponds private key in my test).

I try read it by:

key64 := 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkI45GM6mYLlbxrKozE6bXWMox
vtVuIWOIF1KcGLED+4GipriwNt0hkosuil513/CMjn9XBSZtaSsiIOLkXLZtbKYh
ATtvODR1nD/dl0JpFH7BLcD9NGGRKOow0jg8fcPtXhLTy6Dsl7rfmVPJYuc4IlzZ
ImTdErllmhzrTKT3YQIDAQAB'.

derKey := (Base64MimeConverter mimeDecodeToBytes:  key64 readStream ) contents.

r := RSAPrivateKeyFileReader new.
r decryptedBytes: derKey .
publicKey := r asPublicKey .

and last line raise error.

What you think about this?

2010/9/22 Denis Kudriashov <[hidden email]>

Yes, I have public key and start test verification but I faced in some problem and stopped for today.
I can't read public key instance from der bytes (I have pem formated public key file) by same way as I read private key instance (by "aRSAPrivateKeyFileReader asPublicKey").

I think tomorrow I send you my results.

2010/9/22 Rob Withers <[hidden email]>

Denis,
 
One other thing.  Do you have the publicKey for that privateKey you used in the test case?  We should really be checking the verification step as well.
 
Thanks,
Rob

Sent: Wednesday, September 22, 2010 9:51 AM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hello, Rob.

I found solution. VW help me very much.

Your changes almost right.

method SHA1 class>>digestInfoAsn1DerEncodingFromMessage: is good and placed right. But method RSAPrivateKey>>v15SignMessageHash: is wrong:

RSAPrivateKey>>v15SignMessageHash: encodedMsg

    | padded toBeSigned |
    padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
    toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
    ^ (self crypt: toBeSigned asInteger) asByteArray.

I examine what happen in VW code (it is work good like java). And now I have this version:

v15SignMessageHash: encodedMsg

    | int emLen |
   
    emLen := (p * q) digitLength -1.
       
    int := LargePositiveInteger basicNew: emLen.
    " Our LargeIntegers are little endian, so we have to reverse the bytes"
    encodedMsg with: (encodedMsg size to: 1 by: -1) do: [:each :index |
        int basicAt: index put: each].
    int basicAt: encodedMsg size + 1 put: 0.

    encodedMsg size + 2 to: emLen - 1 do: [ :ind | int basicAt: ind put: 255].
    int basicAt: emLen put: 1.
   
    ^ (self crypt: int) asByteArray.


This is give me results same as java and VW.

I attach this method and acceptence test for it.



2010/9/21 Rob Withers <[hidden email]>
Denis,
 
I looks like I missed step 2 on page 38.  I am not preappending the AlgorithmIndentifier and producing the DER encoding of the DigestInfo prior to padding and encrypting.  I implemented it in the attached changeset.  Please load this and test for me. 
 
Note that it requires either all of Cryptography from the Cryptography repository loaded, or all of CryptoBase and CryptoCerts from the inbox.  The digest requires ASN1 encoding framework which is in the certificate package.
 
Rob
 
 

Sent: Tuesday, September 21, 2010 12:31 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Denis,
 
I do not know why I was looking at PKCS#11.  THe RSA spec is PKCS#1.  In that document (ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf) on page 25 it says:
 
"Two signature schemes with appendix are specified in this document: RSASSA-PSS and RSASSA-PKCS1-v1_5."
 
I implemented v1_5.  It may be that Java is using PSS.  I may have implemented v1.5 wrong.  The signature creation and verification algorithms start on page 30.  The encoding is on 35.
 
Rob

Sent: Tuesday, September 21, 2010 12:06 PM
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

Hi Denis,
 
I originally wrote the v15 signature methods in April of 2007.  I am currently trying to download the PKCS#11 V2.30 doc to verify, but IIRC there are more than one signature algorithm defined for RSA.  I don't recall why I chose v15.  Perhaps Java is using another RSA signature function.
 
There are no explicit tests for this signature.  There is a test inside of the CryptoX509Test  (#verifySHA1WithRSAEncryptionFromParentCertificate: ), but it isn't used as the certificate that exposed it has expired and so is failing.  I removed that certificate test.
 
Let's talk bytes...the way this works in Squeak is that the RSA pads the SHA1 hashed message and encrypts it.
 
v15SignMessage: aMessage
 
 ^ self v15SignMessageHash: (SHA1 hashMessage: aMessage).
and
 
v15SignMessageHash: encodedMsg
 
 | padded toBeSigned |
 padded := ByteArray new: (256 - encodedMsg size - 3) withAll: 255.
 toBeSigned := #(0) asByteArray, #(1) asByteArray, padded, #(0) asByteArray, encodedMsg.
 ^ (self crypt: toBeSigned asInteger) asByteArray.
Presumably the #crypt: function will be the same in Java and Squeak given the same key.  So if there are 2 different signature functions in RSA, I would suspect that the padding would be different.
 
Still trying to download the spec....
 
What do you think?
 
Cheers,
Rob

Sent: Tuesday, September 21, 2010 11:21 AM
Subject: [squeak-dev] Crypto RSAWithSHA1 sign

Hello

Is somebody use Cryptography for RSA with SHA1 digital signature?

I try do same result as I hava in java programm
I have rsa private key as smalltalk object. It has same values as java private key object.

But code

privateKey v15SignMessage: message asByteArray  .

returns me wrong result. Its differ from java working test
































Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Chris Muller-3
Hi Rob,

> I am maintaining 2 parallel Monticello package streams for Cryptography
> until we can figure out what will go into the Trunk and what will remain
> separate.

Are you saying the packages are identical except for the package-names
"Crypto" vs. "Cryptography"?

Thanks,
  Chris

Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2


--------------------------------------------------
From: "Chris Muller" <[hidden email]>
Sent: Sunday, October 03, 2010 5:33 PM
To: "The general-purpose Squeak developers list"
<[hidden email]>
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

> Hi Rob,
>
>> I am maintaining 2 parallel Monticello package streams for Cryptography
>> until we can figure out what will go into the Trunk and what will remain
>> separate.
>
> Are you saying the packages are identical except for the package-names
> "Crypto" vs. "Cryptography"?
>

Hi Chris,

Well, it is slightly more complicated than that.

There is the Cryptography package in the Cryptography repository.  It holds
all cryptography code (other than plugin code).

Then there is the "set" of Crypto packages in the Inbox repository.  This
set is the contents of the Cryptography package, broken down into
sub-packages.  There is
    CryptoCore
    CryptoCoreTests
    CryptoExtras
    CryptoExtrasTests
    CryptoCerts
    CryptoCertsTests.

The latest plan I heard was that
CryptoCore/CryptoCoreTests/CryptoExtras/CryptoExtrasTests would all go into
the Trunk, leaving CryptoCerts/CryptoCertsTests to reside in the
Cryptography repository.

HTH,
Rob

> Thanks,
>  Chris
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Chris Muller-3
> The latest plan I heard was that
> CryptoCore/CryptoCoreTests/CryptoExtras/CryptoExtrasTests would all go into
> the Trunk, leaving CryptoCerts/CryptoCertsTests to reside in the
> Cryptography repository.

Ok, but I would just like to rename them back to Cryptography before
we put them into trunk.

Reply | Threaded
Open this post in threaded view
|

Re: Crypto RSAWithSHA1 sign

Rob Withers-2
Hi Andreas,

Is the following alright with you?

1- Combine CryptoCore and CryptoExtras, name it CryptographyCore, publish to
Inbox and brought into Trunk soon (when?).
2- Combine CryptoCoreTests and CryptoExtrasTests, name it
CryptographyCoreTests, publish to Inbox and brought into Trunk soon (when?).
3- Rename CryptoCerts to Certificates, published to Cryptography repository.
4- Rename CryptoCertsTests to CertificatesTests, published to Cryptography
repository.

If you agree, I'll do the work tonight.

Thanks,
Rob

--------------------------------------------------
From: "Chris Muller" <[hidden email]>
Sent: Tuesday, October 05, 2010 3:10 PM
To: "The general-purpose Squeak developers list"
<[hidden email]>
Subject: Re: [squeak-dev] Crypto RSAWithSHA1 sign

>> The latest plan I heard was that
>> CryptoCore/CryptoCoreTests/CryptoExtras/CryptoExtrasTests would all go
>> into
>> the Trunk, leaving CryptoCerts/CryptoCertsTests to reside in the
>> Cryptography repository.
>
> Ok, but I would just like to rename them back to Cryptography before
> we put them into trunk.
>
>