Digital signatures
A digital signature shows who wrote a message and that nobody changed it. This lesson makes an ECDsa key pair, signs a message, verifies it, shows that a changed message or another key fails, and shares only the public key.
What you will learn#
- what a signature proves, and what it does not,
- private and public keys,
ECDsa: making a key,SignData,VerifyData,- why a changed message or a different key fails,
- sharing the public key and nothing else.
The tutorial Hashing and encryption used hashes and encryption and the tutorial Protecting and signing messages used RSA and more. A signature adds the answer to "who?".
The program#
# digital signatures - ECDsa keys, signing a message, verifying it, and what happens when the message or the key changes
load "dotnet.ring"
using {
"System"
"System.Security.Cryptography"
"System.Text"
}
func main
oUtf8 = new UTF8Encoding()
# A digital signature proves two things about a message: who sent it (only the owner of the PRIVATE key can make the signature) and
# that it was not changed (the signature fits only this exact message). Everybody with the PUBLIC key can check it.
# ECDsa uses elliptic curves: small keys and fast signatures. (the tutorial "Protecting and signing messages" used RSA and hashes)
oCurve = new ECCurve().CreateFromFriendlyName("nistP256") # a named curve
oKey = new ECDsa().Create(oCurve)
? "1. a key pair was made on the curve nistP256, key size " + oKey.KeySize + " bits"
# 2. Sign a message: the data is hashed (SHA-256) and the hash is signed. The signature of ECDsa has 64 bytes for this curve.
cMessage = oUtf8.GetBytes("Pay 100 to Bob")
oSha = new HashAlgorithmName().SHA256
cSignature = oKey.SignData([ :data = cMessage, :hashAlgorithm = oSha ])
? nl + "2. the signature has " + len(cSignature) + " bytes"
? " it is different every time you sign (a random number is part of it), so the program does not print it"
# 3. Verify: true only for the same message and the same signature
? nl + "3. the original message: " + oKey.VerifyData([ :data = cMessage, :signature = cSignature, :hashAlgorithm = oSha ])
cChanged = oUtf8.GetBytes("Pay 900 to Bob")
? " the message changed (100 to 900): " + oKey.VerifyData([ :data = cChanged, :signature = cSignature, :hashAlgorithm = oSha ])
cOther = oKey.SignData([ :data = cMessage, :hashAlgorithm = oSha ])
? " a second signature of the same message is also valid: " + oKey.VerifyData([ :data = cMessage, :signature = cOther, :hashAlgorithm = oSha ]) + ", but different from the first: " + (cOther != cSignature)
# 4. A signature of one key is not valid for another key: somebody else can not sign in your name
oOtherKey = new ECDsa().Create(oCurve)
? nl + "4. verified with another key: " + oOtherKey.VerifyData([ :data = cMessage, :signature = cSignature, :hashAlgorithm = oSha ])
# 5. Share only the PUBLIC part of the key: ExportSubjectPublicKeyInfo() gives its bytes (a Ring text, in a standard format that every system reads),
# without the private number. The receiver imports the bytes into a new key object and can only VERIFY, never sign in your name.
cPublic = oKey.ExportSubjectPublicKeyInfo()
oReceiver = new ECDsa().Create(oCurve)
oReceiver.ImportSubjectPublicKeyInfo(cPublic)
? nl + "5. the public key is " + len(cPublic) + " bytes; the receiver has only that and can verify the message: " + oReceiver.VerifyData([ :data = cMessage, :signature = cSignature, :hashAlgorithm = oSha ])
lCanSign = True
try
oReceiver.SignData([ :data = cMessage, :hashAlgorithm = oSha ])
catch
lCanSign = False
done
? " the receiver can sign with it: " + lCanSign + " (there is no private key)"
# 6. A signature is not encryption: the message itself is not hidden. If the message must be secret too, encrypt it (the tutorial "Protecting and signing messages").
oKey.Dispose()
oOtherKey.Dispose()
oReceiver.Dispose()1. a key pair was made on the curve nistP256, key size 256 bits
2. the signature has 64 bytes
it is different every time you sign (a random number is part of it), so the program does not print it
3. the original message: 1
the message changed (100 to 900): 0
a second signature of the same message is also valid: 1, but different from the first: 1
4. verified with another key: 0
5. the public key is 91 bytes; the receiver has only that and can verify the message: 1
the receiver can sign with it: 0 (there is no private key)How it works#
What a signature proves#
A key pair has two parts that fit each other. The private key stays with its owner and makes signatures. The public key can be given to everybody and only checks signatures. A valid signature proves two things: the message was signed by the owner of the private key, and the message is exactly the signed one — changing a single character makes the check fail. It does not hide the message (that is encryption) and it does not prove that the owner is the person you think; for that you need a certificate from somebody you trust.
ECDsa#
ECDsa uses elliptic curves: a 256-bit key gives the security of a much longer RSA key, and signing is fast. The key is made on a named curve: ECCurve.CreateFromFriendlyName("nistP256") and then ECDsa.Create(curve). Both are static methods, called through new ECCurve() and new ECDsa(). The key size is 256 bits and a signature on this curve has 64 bytes.
Sign and verify#
SignData([ :data = ..., :hashAlgorithm = ... ]) hashes the data (SHA-256) and signs the hash; VerifyData([ :data = ..., :signature = ..., :hashAlgorithm = ... ]) answers true or false. The data is a byte array: a Ring text of bytes (UTF8Encoding.GetBytes). Step 3 shows the three results that matter: the original message is valid, the changed message ("100" to "900") is not, and a second signature of the same message is also valid but different from the first, because a random number is part of every ECDsa signature. So never compare signatures to see whether a message is the same: always verify.
Another key#
A signature made with one key does not verify with another key (step 4). Nobody can sign in your name without your private key, which is why the private key must be kept safe: in a file with strict rights, in a key store or in a hardware token, and never in the source code or in a mail.
Sharing the public key#
ExportSubjectPublicKeyInfo() returns the public part of the key as bytes (a Ring text of 91 bytes for this curve) in a standard format that every system and language reads, without the private number. The receiver makes a new key object and imports the bytes (ImportSubjectPublicKeyInfo, which takes the bytes as a ReadOnlySpan<byte> and answers [ bytes read ]); now it can verify, and it cannot sign: SignData raises an error because there is no private key. In a real program you send the bytes as a file or as Base64 text (the tutorial Bytes as text: Base64 and hex), or fetch the key from a certificate. ExportParameters(False) and ImportParameters do the same with the parts of the key as an object.
Sign bytes, not "the same text": agree exactly on the bytes (the encoding, the line endings, the order of the fields). A message that is written as JSON by one program and re-written by another may be the same data but not the same bytes, and then the signature fails. Sign the exact bytes that you send.
What you learned#
- a signature proves the owner and the integrity, not secrecy,
- the private key signs and the public key verifies,
ECCurve.CreateFromFriendlyName,ECDsa.Create,SignData,VerifyData,- a changed message or another key fails,
- export the public parameters and keep the private key.
Exercises#
- Sign a file (the tutorial Files and folders) and write the signature to another file; verify both later.
- Write
signedMessage(text)that returns the text and its signature as Base64 in a JSON object (the tutorial Writing JSON as a stream). - Change one byte of the signature and check that verification fails.
- Try the curve
nistP384and look at the size of the signature.