A digital signature is a mathematical scheme for verifying the authenticity of digital messages or documents.
A valid digital signature gives a recipient a very strong reason to believe that the message was created
by the known sender,
this is what authentication means, and that the message was not altered in transit. Or, in simple words
if someone who receives a message or downloads a file that is digitally signed he knows that the message
was really created by the sender and not spoofed by a hacker and that the message was not tampered with
or corrupted.
But what is a digital signature? A digital signature of a file is the hash of that file, encrypted with
the private key of the user that signs! That’s all! You can encrypt and sign a message or just sign a message
without encrypting it.
You do that when you want to share something in plain text or make a public post that everyone can read,
but you want to include a signature to prove you were the original author and that your message is not
altered in transit.
If you don't encrypt the message there is no confidentiality and it only proves integrity and authenticity
of the message. In the real world
there are a lot of messages that are not confidential
like for example applications or software updates they only need to be signed not encrypted.
Let's see the first type of a signature which is called clear text signature.
The document is not encrypted, only signed, and the signature is appended to the end of the document.
This is very useful for text files or emails.
This is the file I wanted to sign and I'll rename it to a secret.txt.
Let's take this file and sign it.
gpg --clearsign and the name of the file.
I'm signing with my private key so I must unlock it
the command generated a clear-text file that contains the signature at the end. Let's see its contents:
secret.txt.asc - it's an ASCII file!
This is the contents of the file and the signature.
This is useful for pasting it into an email or posting it online.
If you are not interested in generating a text file you can use the --sign option
the same way, like this
and the resulting file is a binary one
this binary file is not encrypted, only signed. This comment gpg --sign and the name of
the file compressed the file and then generates a new binary file called secret.txt
.gpg -it contains both the file contents and the signature; so until at this point we have signed
the file in two ways.
We've generated an ASCII file and a binary file; and the last type of signature is called detached signature,
the signature of the file will be in a separate file.
Let's see the command gpg --detach-sig--output and the name of the file
that contains the signature, let's say secret.txt.sig and the file I am signing secret.txt
This is the signature, it's a binary file!
Or if you want to have the signature in ASCII format you add the -a or --amore
option like this.
This was the command and I'm adding--amore or -a. And the resulting file will be
secret1.txt.asc There is already a file called secret.txt.asc and I
do not want to overwrite it, that's why
I'm using the name secret1
and this is the signature in ASCII format.
Let's
copy the files to the other Linux machine using scp.
The other user will verify the signatures so scp the files and I'll copy all signatures
the user, the IP address and the path where the files will be copied, in the home directory.
And they were copied, these three files; each file contains the content and the signature.
Remember that the files were not encrypted, only signed! To verify a digital signature
you always need three things: the file to very, it's here,
the digital signature of the file,it is in the same file and the public key of the one that signed.
Let's see if we have the public key of the user that signed in the gpg keyring.
It's not there, I must import the public key of this the user.
This is the keyid and I'll import the key from the key server on this machine.
In the last lecture I've published both public keys on the key server, so this key is there.
Copy and the gpg--keyserver pgp.mit.edu-- receive and the keyid
The key was imported.
Now, the user can verify the signature. Given a signed a document
you can either check the signature or check the signature and recover the original document. To check
the signature
use the -- verify option, like this gpg--verify and the name of the file that contains
both the files contents and the signature: secret.txt.asc
and this is a good signature. We can assume that the file comes from the
real user and was not changed in transit.
To verify the signature of the other file,
the binary one, I'll use the same command
and it's a good signature. The signature is good
but I've got this message, this warning.
This key is not certified with a trusted signature. Is that okay?
And the answer is that in these conditions the worning is okay.
A trusted signature is a signature from a key that you trust, either because you have personally verified
that it belongs to the person to whom it claims to belong or because it has been signed by a key that
you trust, maybe through a series of intermediate keys.
In our case we've just imported the key from the key server without any other verification This level
of security is enough in most cases. Another option is to verify the signature and extract the document.
We'll use the --decrypt option like this: gpg--decrypt or -d, it's the same,
and the name of the file that contains the clear text contents and the signature secret.txt.asc
and we see the contents of the file and that the signature was good.
The command printed out the content of the file it the terminal.
If you want to create another file, with that clear text content, you can use an output redirection like
this or the --output option, let's say secret.txt
This is the signature and this is secret .txt, the initial file the file I have signed.
Let's see how to verify the detached signature!
Remember that secret1.txt.asc is a detached signature! There is only the signature!
To verify a detached signature you execute gpg--
verify, the name of the signature file and the name of the file, in clear text, this one; and we've got a
good signature.