Speed

Sign the Digest

A PKCS#1 v1.5 signature signs the SHA-256 digest of the message inside a padded block, not the message itself; the 64-byte signature of a test key is the same every time, and a single changed byte or a missing line feed makes it fail.

The question

An RSA signature is made with the private key and checked with the public key. What is signed: the message, or something derived from it? A message can be any length, but RSA works on numbers the size of the modulus, 64 bytes for a 512-bit test key. So the message is hashed first, the digest is padded to 64 bytes, and that block is raised to the power d.

The key alice in this lesson is test material, a 512-bit key made for these lessons. The signature below was made once with that key and is fixed.

Work it by hand

RFC 8017 section 8.2 defines the signature, and section 9.2 the padded block for PKCS#1 v1.5, the method OpenSSL uses by default for RSA. For SHA-256 the block is built from five parts.

  • 00 01, two bytes that start the block.
  • A run of ff bytes. Its length is 64 minus 3 minus the length of the next two parts: 64 minus 3 minus 51 is 10 bytes.
  • 00, one byte that ends the padding.
  • A 19-byte prefix that names the hash, 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 for SHA-256.
  • The 32-byte digest of the message.

The signature is that block to the power d, modulo n. To check it, raise the signature to the power e, rebuild the block from the message, and compare. The signed message pay 10 to bob is 13 characters and a line feed, 14 bytes; the line feed is part of what was signed. For SHA-512 the prefix and digest would take 83 bytes and the block 94 bytes, more than the 64 of this key: this key cannot sign with SHA-512 in this method.

Run it

The command checks the signature in in/sig.bin against the file in/message.txt with the public key in in/pub.txt. -sha256 chooses the hash, -verify the public key and -signature the file with the 64 signature bytes.

The picture draws the final line feed as a question mark. For the test message pay 10 to bob followed by a line feed, the command prints Verified OK. Signing the same file with the private key gives these 64 bytes again, run after run, because this padding has no random part.

The message pay 10 to bob followed by a line feed, which the picture draws as a question mark, the first 16 of the 64 signature bytes, and the result of the check: Verified OK.

Public test material, generated for this lesson. Never use it for anything real.

# Reads the public key in in/pub.txt, the signature in in/sig.bin
# and the message in in/message.txt.
dgst -sha256 -verify in/pub.txt -signature in/sig.bin in/message.txt
{
  "answer": "Verified OK"
}

You can change the message or the signature. A single changed byte, as in pay 11 to bob, or a missing line feed, stops the command with the message "OpenSSL: RSA lib" (the command prints Verification failure and exits with an error; its error queue ends in "RSA lib"). A different hash name, such as -sha512, stops it with the same message.

An empty signature stops it with "Error reading signature file in/sig.bin: unsupported", and a text that is not a public key with "Could not find or decode private key of public key from in/pub.txt: unsupported".

Your turn

The program below should say whether a message was signed by the owner of the private key in key: a PKCS#1 v1.5 signature with SHA-256, in hexadecimal in sig. The program may read the private key, because the page's programs cannot parse a public key. Decide whether the program does, and find the candidates that do. Check runs each candidate on the data shown and on eight others.

The message Pay 20 to carol and the first 16 of the 64 signature bytes.

Public test material, generated for this lesson. Never use it for anything real.

let k = ssl.parse_key(key);
let text = str_lower(message);
let answer = k.verify("SHA256", text, sig);
{
  "answer": true
}

key is the text of an RSA private key, message a text and sig a PKCS#1 v1.5 signature with SHA-256 in hexadecimal. Return true if sig is a valid signature of exactly the bytes of message under that key, and false if not.

Read the result carefully

A valid signature shows that the holder of the private key signed exactly these bytes. It does not hide the message, it does not say when or where the message was signed, and the same signature stays valid for a copy of the message sent again. Those need other tools.

The page shows that a line feed, a changed letter or another hash name breaks verification, because the digest changes or the block does not match. It does not show how keys are protected or distributed, how a verifier knows whose key it holds (certificates do that; see a-certificate-chain-trusts-a-root), or any timing. The key is test material, and 512 bits is far too small to use.

Recap

  • A PKCS#1 v1.5 signature is the padded block of the message's digest, raised to the power d modulo n.
  • The block is 00 01, ff bytes, 00, a hash prefix and the digest, and fills the whole modulus.
  • The same key and message give the same signature every time.
  • Every byte counts, a final line feed included; a hash that does not fit the key cannot sign.