Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return value issues #14

Open
JohnLegrandRichards opened this issue Jun 17, 2022 · 1 comment
Open

return value issues #14

JohnLegrandRichards opened this issue Jun 17, 2022 · 1 comment

Comments

@JohnLegrandRichards
Copy link

PKCS11 RSA signature is returning a bit string or something. Is there a way to specify the format of the signature that gets returned?

@larskanis
Copy link
Owner

A RSA signature is just a big number represented as a byte string. It should have the size of the RSA modulus, which is 256 bytes for a 2048 bit RSA key for instance.

There is CKM_RSA_PKCS which is a raw RSA signature without any digest computation. In contrast CKM_SHA256_RSA_PKCS does some precalculation like so before building the RSA signature:

          # Build the digest info TLV as PKCS#1
          ary1 = [
            OpenSSL::ASN1::ObjectId.new('SHA256'),
            OpenSSL::ASN1::Null.new(nil),
          ]
          ary2 = [
            OpenSSL::ASN1::Sequence.new(ary1),
            OpenSSL::ASN1::OctetString.new(OpenSSL::Digest::SHA256.new(document_to_be_signed).digest),
          ]
          raw_value_to_be_signed = OpenSSL::ASN1::Sequence.new(ary2).to_der

Similarly for CKM_SHA256_RSA_PKCS_PSS, which has a bit more complicated precalculation and padding.

Simple raw RSA signature is also in our tests:

def test_sign_verify
plaintext = "important text"
signature = session.sign( :SHA1_RSA_PKCS, rsa_priv_key, plaintext)
assert signature.length>10, 'The signature should contain some data'
signature2 = session.sign( :SHA1_RSA_PKCS, rsa_priv_key){|c|
c.update(plaintext[0..3])
c.update(plaintext[4..-1])
}
assert_equal signature, signature2, 'results of one-step and two-step signatures should be equal'
valid = session.verify( :SHA1_RSA_PKCS, rsa_pub_key, signature, plaintext)
assert valid, 'The signature should be correct'
assert_raises(CKR_SIGNATURE_INVALID, 'The signature should be invalid on different text') do
session.verify( :SHA1_RSA_PKCS, rsa_pub_key, signature, "modified text")
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants