views:

438

answers:

1

I need to digitally sign some text in python using a private key stored in a .pem file. It seems like M2Crypto is the preferred way to do that these days, so that's what I'm using. I think I get most of it, but I'm confused about how to configure padding. To be specific, I need to verify the signature in an iPhone app, using a padding scheme called kSecPaddingPKCS1SHA1 and described like this:

Data to be signed is a SHA1 hash. Standard ASN.1 padding will be done, as well as PKCS1 padding of the underlying RSA operation.

Not being a crypto expert, I have only a fuzzy idea what this means. I've tried to look at some of the RFCs but found them impenetrable. I see that the encryption/decryption methods of RSA objects take padding types, but I don't see anything similar related to signature verification.

Any help, especially with code, will be appreciated.

(In some sense this is the converse of this question.)


Ok, the answer given below is correct AFAICT. The following code generates a signature for text that validates on the iPhone using the kSecPaddingPKCS1SHA1 padding scheme.

from M2Crypto import EVP
privkey = EVP.load_key("privkey.pem")
privkey.sign_init()
privkey.sign_update(text)
signature = privkey.sign_final()

(Sorry to editorialize, but can I just say that crypto hackers are some of the lousiest documentation writers in the universe?)

+1  A: 

AFAIK M2Crypto adds padding where it's required.

PKCS1 padding is the default.

But, (again only AFAIK), signatures don't have padding, padding is only added to encrypted data to prevent a possible attack.
EDIT: user caf, in a comment says that a padding is essnetial to a good signature. I'm still recommending you try it with the default M2Crypto behavior, it might add it.

On M2Crypto's generated docs you can see that the {public,private}_{encrypt,decrypt} methods have a padding option, which is PKCS1 by default, while the sign menthod has none.

IMO just give it a shot with the default M2Crypto params, it will probably work.

Prody
On the contrary, correct padding is absolutely *essential* for a secure signature system with RSA.
caf