RSA sign and verify using Openssl : Behind the scene - JohnHau/mis GitHub Wiki
Jupyter (IPython) notebook version of this page: openssl_sign_verify Digital signature and verification A digital signature is a mathematical scheme for presenting the authenticity of digital messages or documents. Message / file to be sent is signed with private key. Message received by the recipient is authenticated using public key. RSA sign and verify using OpenSSL Create sample data file, private key and public key
Create a file containing all lower case alphabets
$ echo abcdefghijklmnopqrstuvwxyz > myfile.txt
Generate 512 bit Private key
$ openssl genrsa -out myprivate.pem 512
Separate the public part from the Private key file.
$ openssl rsa -in myprivate.pem -pubout > mypublic.pem
Cat the contents of private key
$ cat myprivate.pem -----BEGIN RSA PRIVATE KEY----- MIIBOwIBAAJBAMv7Reawnxr0DfYN3IZbb5ih/XJGeLWDv7WuhTlie//c2TDXw/mW 914VFyoBfxQxAezSj8YpuADiTwqDZl13wKMCAwEAAQJAYaTrFT8/KpvhgwOnqPlk NmB0/psVdW6X+tSMGag3S4cFid3nLkN384N6tZ+na1VWNkLy32Ndpxo6pQq4NSAb YQIhAPNlJsV+Snpg+JftgviV5+jOKY03bx29GsZF+umN6hD/AiEA1ouXAO2mVGRk BuoGXe3o/d5AOXj41vTB8D6IUGu8bF0CIQC6zah7LRmGYYSKPk0l8w+hmxFDBAex IGE7SZxwwm2iCwIhAInnDbe2CbyjDrx2/oKvopxTmDqY7HHWvzX6K8pthZ6tAiAw w+DJoSx81QQpD8gY/BXjovadVtVROALaFFvdmN64sw== -----END RSA PRIVATE KEY----- Sign using Openssl Message digest algorithm : SHA1 Padding scheme : PCKS#1 v1.5
Sign the file using sha1 digest and PKCS1 padding scheme
$ openssl dgst -sha1 -sign myprivate.pem -out sha1.sign myfile.txt
Dump the signature file
$ hexdump sha1.sign 0000000 91 39 be 98 f1 6c f5 3d 22 da 63 cb 55 9b b0 6a 0000010 93 33 8d a6 a3 44 e2 8a 42 85 c2 da 33 fa cb 70 0000020 80 d2 6e 7a 09 48 37 79 a0 16 ee bc 20 76 02 fc 0000030 3f 90 49 2c 2f 2f b8 14 3f 0f e3 0f d8 55 59 3d 0000040 Verify sign using Openssl Openssl decrypts the signature to generate hash and compares it to the hash of the input file.
Verify the signature of file
$ openssl dgst -sha1 -verify mypublic.pem -signature sha1.sign myfile.txt Verified OK
RSA signature generation : Behind the scene
Step 1: Get modulus and public exponent from public key View the contents of public key: Pubic key contains Modulus, public exponent and key size. 65537 (0x10001) is widely accepted default public exponent.
Get modulus and public exponent from public key
$ openssl rsa -pubin -inform PEM -text -noout < mypublic.pem Public-Key: (512 bit) Modulus: 00:cb:fb:45:e6:b0:9f:1a:f4:0d:f6:0d:dc:86:5b: 6f:98:a1:fd:72:46:78:b5:83:bf:b5:ae:85:39:62: 7b:ff:dc:d9:30:d7:c3:f9:96:f7:5e:15:17:2a:01: 7f:14:31:01:ec:d2:8f:c6:29:b8:00:e2:4f:0a:83: 66:5d:77:c0:a3 Exponent: 65537 (0x10001) Format the output to print modulus and public exponent:
Store the output of public key info in a variable
$ PBKEY_INFO=openssl rsa -pubin -inform PEM -text -noout < mypublic.pem
Grep and format string to output Modulus
$ MODULUS=echo "$PBKEY_INFO" | grep Modulus: -A 5 | tail -5
$ echo echo $MODULUS | tr -cd [:alnum:]
00cbfb45e6b09f1af40df60ddc865b6f98a1fd724678b583bfb5ae8539627bffdcd930d7c3f996f75e15172a017f143101ecd28fc629b800e24f0a83665d77c0a3
Grep to print public exponent
$ echo "$PBKEY_INFO" | grep Exponent: Exponent: 65537 (0x10001) Step 2: Format and print signature file Signature is a binary file which is converted to a big integer and used in authentication.
sha1.sign is the signature file sent along with data file.
$ echo hexdump sha1.sign | cut -c 9- | tr -cd [:alnum:]
9139be98f16cf53d22da63cb559bb06a93338da6a344e28a4285c2da33facb7080d26e7a09483779a016eebc207602fc3f90492c2f2fb8143f0fe30fd855593d