Digital Signature
A complete guide to digital signatures, covering authenticity, integrity, non-repudiation, PKI, Certificate Authorities, and asymmetric signing.
In medieval times, monarchs sealed royal decrees using melted wax and a unique signet ring worn on the ruler's finger. Once the wax hardened, anyone in the kingdom could inspect the royal crest. If an impostor tried to forge a decree, the seal did not match; if an adversary tried to alter the parchment, the brittle wax cracked. And because only the monarch possessed that personal ring, the ruler could not easily deny issuing the command. In our modern digital world, how does your phone know that a system update genuinely came from Apple or Google rather than an attacker? How does a bank verify that a large electronic wire transfer was actually authorized by the account holder? A digital signature is the modern mathematical equivalent of that royal wax seal. By combining asymmetric cryptography with cryptographic hashing, digital signatures provide three essential guarantees: Authenticity (proof of who sent it), Integrity (proof that the content was not altered), and Non-Repudiation (proof that the signer cannot deny sending it).
Definition
A digital signature is a mathematical scheme used to verify the authenticity, integrity, and non-repudiation of a digital message, document, or software package. It is created using the sender's private key and verified by anyone using the sender's public key.
Why It Matters
Without digital signatures, safe software distribution would be impossible. Whenever your phone or computer updates its operating system, the bootloader checks the digital signature on the downloaded package against the hardware manufacturer's built-in public root certificate. If an attacker modifies even a single byte of code, signature verification fails and the device refuses to install the file. Digital signatures also power online legal agreements (such as DocuSign), SSL/TLS website certificates, secure email (S/MIME), and blockchain transactions.
How It Works
A digital signature workflow operates in two main phases: Signing and Verification: 1. The Signing Phase (Performed by the Sender using their PRIVATE Key): • Step A (Hash the Payload): The sender runs the document or code file through a cryptographic hash function like SHA-256 to create a compact digest: H = Hash(M). (Signing the short hash rather than a large multi-gigabyte file is fast and computationally efficient). • Step B (Sign the Hash): The sender computes a mathematical signature over that digest using their secret private key: Signature = Sign(PrivateKey, H). • Step C (Attach and Send): The original document and the signature are sent together to the recipient. 2. The Verification Phase (Performed by Anyone using the PUBLIC Key): • Step A (Hash the Received File): The recipient independently calculates the SHA-256 hash of the received document: H1 = Hash(M). • Step B (Verify the Signature): The recipient uses the sender's public key to check the signature and retrieve the expected hash value: H2 = Verify(PublicKey, Signature). • Step C (Compare): If H1 matches H2, the signature is valid! This proves the document came from the owner of the private key and was not modified in transit. 3. The Three Key Guarantees: • Authenticity: Only the person with the private key could have produced a signature that matches their public key. • Integrity: If anyone changed even a single comma in the document, H1 will not match H2, exposing the tampering immediately. • Non-Repudiation: The signer cannot claim 'I did not send this' because no one else holds their secret private key. 4. Public Key Infrastructure (PKI) and Certificate Authorities (CAs): How do you know that a public key really belongs to Bob rather than an attacker pretending to be Bob? A trusted Certificate Authority (CA), such as Let's Encrypt or DigiCert, validates Bob's identity and digitally signs an X.509 Certificate that binds Bob's identity to his public key.
Digital Signature Creation and Verification Workflow
Key Points
- Digital signatures provide Authenticity, Integrity, and Non-Repudiation.
- You sign data with your PRIVATE key, and anyone can verify it with your PUBLIC key.
- Sign the SHA-256 hash of a file rather than the raw payload for better performance and security.
- Non-repudiation prevents the signer from denying they authorized the document.
- Public Key Infrastructure (PKI) and Certificate Authorities verify who actually owns a public key.
Steps
Choose a Modern Signing Algorithm
Use fast and secure algorithms like Ed25519 (EdDSA) or ECDSA with P-256. For legacy environments requiring RSA, use at least 3,072-bit keys with PSS padding.
Protect Signing Keys in Hardware
Store code-signing and corporate signing keys inside Hardware Security Modules (HSMs) or hardware tokens (such as YubiKeys) so private keys cannot be extracted.
Always Sign Hashes Rather than Raw Files
Hash files with SHA-256 or SHA-512 before signing to keep processing fast and protect against cryptographic malleability.
Include Cryptographic Timestamps
Add RFC 3161 timestamps from a trusted Timestamp Authority (TSA). This ensures software signatures remain valid even after the signing certificate expires.
Examples
The Signet Ring and Wax Seal Analogy
The monarch's signet ring is the Private Key (worn only by the ruler). The stamped impression in the red wax is the Digital Signature. An official portrait or royal registry showing the crest is the Public Key. Anyone in the kingdom can compare the wax seal against the official crest to verify that the message is authentic.
Real-World Examples
The SolarWinds Supply Chain Incident (2020)
In 2020, attackers compromised the internal build system of software vendor SolarWinds. The attackers inserted a backdoor into the Orion software before the build server compiled and digitally signed the executable using SolarWinds' valid corporate certificate. Because the resulting update carried an authentic, valid signature, thousands of corporate and government customers installed the compromised update without triggering security warnings.
Technical Examples
import crypto from "crypto";
// 1. GENERATE AN ED25519 KEYPAIR
// Ed25519 is fast, compact, and resistant to side-channel timing attacks
const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
const documentData = "LEGAL AGREEMENT: Authorize release of funds to Contractor A.";
// 2. SIGN THE DOCUMENT WITH THE PRIVATE KEY
// crypto.sign handles internal hashing and mathematical signing
const signature = crypto.sign(null, Buffer.from(documentData, "utf8"), privateKey);
console.log(`Signature (Hex): ${signature.toString("hex").substring(0, 48)}...`);
// 3. VERIFY THE SIGNATURE WITH THE PUBLIC KEY
const isValid = crypto.verify(
null,
Buffer.from(documentData, "utf8"),
publicKey,
signature
);
console.log(`Is original document authentic? ${isValid}`); // true
// 4. DEMONSTRATE TAMPER DETECTION
const tamperedDocument = "LEGAL AGREEMENT: Authorize release of funds to Contractor B.";
const isTamperedValid = crypto.verify(
null,
Buffer.from(tamperedDocument, "utf8"),
publicKey,
signature
);
console.log(`Is tampered document valid? ${isTamperedValid}`); // falseDigital Signature vs HMAC (Message Authentication Code)
| Aspect | Digital Signature | HMAC (Message Authentication Code) |
|---|---|---|
| Cryptography Type | Asymmetric (uses public and private keys) | Symmetric (uses a single pre-shared secret key) |
| Non-Repudiation | Yes: only the sender possesses the private signing key | No: both sender and receiver hold the exact same secret key |
| Verification Access | Anyone holding the public key can verify the signature | Only parties that know the shared secret can verify the code |
| Performance | Slower, requiring asymmetric curve or modular calculations | Very fast, relying on efficient hash functions and XOR operations |
| Common Uses | Code signing, legal contracts, TLS website certificates | API webhooks (Stripe, GitHub), internal service tokens |
Advantages
- Unforgeable Proof of Origin: Mathematically demonstrates which entity produced a file or message.
- Immediate Tamper Detection: Detects even single-bit modifications across files of any size.
- Legal Enforceability: Recognized by major digital signature laws worldwide (such as the US ESIGN Act and EU eIDAS regulation).
Limitations
- Private Key Theft: If an organization's private code-signing key is stolen, attackers can sign malware under that organization's trusted name.
- Reliance on Certificate Authorities: If a Certificate Authority issues a fraudulent certificate or is compromised, an attacker could sign forged assets.
Interview Questions
Walk through how an RSA digital signature is created and verified.
1. Signing: The sender hashes the message m to produce digest h = Hash(m) and applies a padding scheme (such as PSS) to match the RSA key size, producing block M. The sender computes signature s = M^d mod n using their private exponent d. 2. Verification: The recipient receives m and s, and uses public exponent e and modulus n to compute M' = s^e mod n. The recipient unpads M' to recover digest h'. The recipient also independently calculates h = Hash(m). If h matches h', the signature is valid, proving both authenticity and integrity.
Why is Ed25519 (EdDSA) preferred over ECDSA (P-256) in modern applications?
ECDSA requires generating a fresh, unique random number (nonce k) for every signature. If the random number generator is flawed or repeats a nonce even once, an attacker can mathematically extract the private key from just two signatures (as happened in the Sony PlayStation 3 security incident). Ed25519 is deterministic: it derives its nonce directly from a hash of the private key and the message, eliminating nonce-reuse risks. It is also faster and naturally resistant to side-channel timing attacks.
Practice Questions
- Generate a GPG keypair using gpg --full-generate-key and practice signing a text file to see how detached signatures work.
- Inspect a signed software executable on your computer (for example, right-click an installer on Windows, view Properties, and examine the Digital Signatures tab).
