Email Authentication: DKIM, SPF and DMARC
How receiving mail servers decide whether a message really comes from the domain it claims: DKIM signatures, SPF sender policies, DMARC alignment and reporting, and their limits with forwarding, run in the lab with real keys and DNS records, plus a look back at attacks on security protocols.
kvi5hnuCode Sprout team member15 min readOn this page
SMTP, the protocol that moves email between servers, has no authentication of its own. Anyone can connect to a mail server and send a message with From: ceo@example.com. That's how most phishing works. Three mechanisms, each published in DNS by the sending domain as a TXT (free-text) record (article 52), now let receivers check:
- SPF says which servers may send mail for a domain.
- DKIM signs each message, so the receiver can check that it came from the domain and wasn't changed.
- DMARC ties both to the From address the user actually sees, and says what to do with mail that fails.
Since 2024 the largest mailbox providers require all three from bulk senders, and mail without them increasingly goes to spam or is rejected.
The lab runs a BIND server for the domains example.com, example.net and example.org, and a mail host with the Python dkim and spf libraries (the dkimsign and dkimverify tools), which perform the same checks as a receiving mail server. Here's what example.com publishes:
example.com TXT: "v=spf1 mx include:_spf.example.net -all"
_dmarc.example.com TXT: "v=DMARC1; p=reject; rua=mailto:dmarc-reports@example.com; adkim=r; aspf=r"
_spf.example.net TXT: "v=spf1 ip4:192.0.2.128/25 -all"
s2026._domainkey.example.com TXT: v=DKIM1;h=sha256;k=rsa;p=MIIBIjANBgkqhkiG9w0BAQE… (417 characters)

Two different "from" addresses
Email has two sender addresses, and the difference is the key to all three mechanisms:
- The envelope sender, given in the SMTP
MAIL FROMcommand, is where bounces go. Users never see it. Mailing lists and bulk senders set it to their own domain. - The header From, the
From:line in the message itself, is what mail programs display.
SPF checks the envelope; DKIM signs with whatever domain the signer chooses; only DMARC is about the address the user sees.
SPF
Sender Policy Framework (SPF, RFC 7208) lets a domain list the IP addresses allowed to send mail with its name in MAIL FROM (and in the HELO greeting). The record is a TXT record at the domain: v=spf1 mx include:_spf.example.net -all means "the hosts in my MX (mail exchanger) records may send, so may whatever _spf.example.net allows, and nothing else may." Mechanisms include ip4:, ip6:, a, mx and include:, and the final all sets the default: -all (fail), ~all (softfail), ?all (neutral). The lab evaluated three connecting servers:
203.0.113.25 example.com MX host -> pass
192.0.2.140 example.net relay (include) -> pass
192.0.2.66 some other server -> fail
The first matched mx (the MX host mail.example.com is 203.0.113.25), the second matched the included 192.0.2.128/25, and the third matched nothing, so -all failed it.
To protect receivers, SPF evaluation may cause at most 10 DNS lookups (each include, a, mx and similar counts). Domains that include many cloud services hit the limit, and evaluation then gives permerror. SPF's big weakness is forwarding: when a mailing list or a forwarding address resends a message, it comes from the forwarder's IP, which the original domain's SPF doesn't list. And SPF says nothing about the header From.
DKIM
DomainKeys Identified Mail (DKIM, RFC 6376) has the sending server sign each message with a private key and publish the public key in DNS, at selector._domainkey.domain. The selector lets a domain have several keys, such as one per mail service or per year, and rotate them. The lab generated a 2,048-bit RSA key with selector s2026 (the TXT record above, 417 characters, splits into several strings because one DNS string holds at most 255 bytes) and signed a message. The DKIM-Signature header it added:
v=1
a=rsa-sha256
c=relaxed/relaxed
d=example.com
i=@example.com
q=dns/txt
s=s2026
t=1795219749
h=from:to:subject:date:message-id:mime-version:content-type:from
bh=eoADmoWctshp9/uyC+CRigo7JmYdIXSx… (44 chars)
b=K701sQ/rP+N2aaBfT3MUvYABlIrEpQtr… (344 chars)
v=1,a=rsa-sha256: the version and the algorithm. Ed25519 (RFC 8463) is also defined, but receivers' support is patchy, so domains that use it add an RSA signature too.c=relaxed/relaxed: the canonicalization for headers and body.simpletolerates almost no change;relaxedignores changes in whitespace and header case, which mail servers often introduce.d=example.com: the signing domain, the identity DKIM vouches for;s=s2026, the selector;t=, the signing time (in seconds since 1970).i=is the identity of the signer within that domain (here the whole domain), andq=dns/txtsays the key is fetched from a DNS TXT record.h=: the header fields covered by the signature.fromappears twice: listing a header once more than it occurs (oversigning) stops anyone from adding a second From header later.bh=: the hash of the canonicalized body.b=: the signature over the listed headers and the DKIM-Signature header itself (withb=empty).
The receiver fetches the key from DNS and checks. The lab verified the original and four changed copies:
signed message: signature ok
body changed (450 -> 4500): dkim.ValidationError: body hash mismatch
Subject header changed: signature verification failed
unsigned header added: signature ok
whitespace added (relaxed canon): signature ok
Changing the amount in the body broke the body hash. Changing the signed Subject broke the signature. But adding a header that wasn't signed, or extra whitespace (with relaxed canonicalization), didn't matter. DKIM survives forwarding as long as the forwarder doesn't modify the message, which mailing lists that add footers or subject tags do.
A DKIM pass proves only that the domain in d= took responsibility for the message. Any domain can sign any mail, so an attacker's message can pass DKIM with d=attacker.example. That's the gap DMARC closes. An older mechanism, ADSP (author domain signing practices), tried to let domains say "all my mail is signed," but it was little used and declared historic in 2013.
DMARC
Domain-based Message Authentication, Reporting and Conformance (DMARC) is published at _dmarc.domain. Originally an informational RFC (7489, 2015), it was revised and published on the Standards Track in 2026 as RFC 9989, with aggregate and failure reporting in RFCs 9990 and 9991. A message passes DMARC if either:
- DKIM passed with a
d=domain aligned with the header From domain, or - SPF passed for an envelope domain aligned with the header From domain.
Relaxed alignment (adkim=r, aspf=r, the default) needs the same organizational domain, so mail.example.com aligns with example.com. Strict (s) needs an exact match. The p= tag says what the domain wants done with failures: none (just monitor), quarantine (treat as suspicious, usually the spam folder) or reject. rua= asks receivers to send daily aggregate reports: XML summaries of which IP addresses sent mail claiming the domain, and how they fared. They're how a domain owner finds forgotten legitimate senders before tightening the policy. The revised DMARC also adds np= for non-existent subdomains, removes the pct= sampling tag in favor of a t= testing flag, and finds the organizational domain by walking the DNS tree rather than using a public suffix list.
The lab's small evaluator (a few lines of Python around the same libraries) applied example.com's p=reject policy to four deliveries:
case: direct from example.com's server
From: example.com DKIM: pass d=example.com (aligned: True) SPF: pass for example.com (aligned: True)
DMARC: pass
case: forwarded by a mailing list, body intact
From: example.com DKIM: pass d=example.com (aligned: True) SPF: pass for example.org (aligned: False)
DMARC: pass
case: list added a footer (body changed)
From: example.com DKIM: fail (aligned: False) SPF: pass for example.org (aligned: False)
DMARC: fail -> policy p=reject: the receiver should reject it
case: spoofed From from another server, no signature
From: example.com DKIM: fail (aligned: False) SPF: pass for example.org (aligned: False)
DMARC: fail -> policy p=reject: the receiver should reject it
- Sent directly by
example.com's server: both SPF and DKIM pass and align. - Relayed by a mailing list at
example.orgthat left the message unchanged: SPF passed only for the list's own envelope domain (not aligned), but DKIM still passed and aligned, so DMARC passed. DKIM is what makes DMARC survive forwarding. - Relayed by a list that added a footer: DKIM broke, SPF didn't align, so DMARC failed, and a receiver would reject a legitimate message. Mailing lists work around this by rewriting the From address to their own domain ("Alice via the list"), and the Authenticated Received Chain (ARC, RFC 8617) lets each intermediary record and sign the authentication results it saw, so a later receiver can choose to trust them.
- A forged message from another server: SPF passed, but for the forger's own domain, which doesn't align with
From: example.com, and there's no valid signature. This is exactly the spoof DMARC exists to stop.
Beyond authentication: MTA-STS, DANE and BIMI
DKIM, SPF and DMARC authenticate the sender. They don't encrypt anything. Mail between servers is usually encrypted with STARTTLS, but opportunistically: an attacker who strips the STARTTLS offer can force plaintext. Two mechanisms make it mandatory. MTA-STS (Mail Transfer Agent Strict Transport Security, RFC 8461; a mail transfer agent is a mail server) publishes a policy over HTTPS that says "my MX hosts always support TLS with valid certificates." DANE for SMTP (RFC 7672) publishes the servers' certificate information in DNSSEC-signed TLSA records (article 84). BIMI (Brand Indicators for Message Identification) lets a domain with an enforced DMARC policy show its logo in supporting mail programs.
Attacks on security protocols
Part 13 has shown the mechanisms working. It's worth ending with how they have failed, because each failure shaped the current designs. Attacks on security protocols include all the protocol and implementation flaws seen elsewhere in this course, plus attacks on the cryptography and on how it's combined.
- Link layer (Wi-Fi security names are explained in article 18). WEP's RC4 usage was broken within a few years of its release; WPA-TKIP followed. WPA2 with a weak passphrase falls to offline dictionary attacks on a captured handshake, which WPA3's SAE fixes. EAP inherits the weaknesses of its methods (LEAP, MSCHAPv2 dictionary attacks), and tunneled methods were open to a man in the middle when the inner and outer authentications weren't cryptographically bound together.
- IPsec. Configurations using encryption without integrity let attackers flip bits in tunnel-mode packets (the article 79 demo, but against IP headers) so that receivers decrypted them into corrupted packets and leaked plaintext through ICMP errors. This is why ESP without integrity is no longer allowed by current algorithm requirements, and AEAD is the default.
- TLS. SSL 2.0 allowed cipher suite rollback, tricking both ends into weak encryption. Padding oracles in CBC mode (and later BEAST, Lucky Thirteen and POODLE) recovered plaintext by watching how receivers reacted to bad padding. The renegotiation attack let a man in the middle prepend data to a victim's session. Export ciphers returned as FREAK and Logjam, compression as CRIME, a heartbeat bug as Heartbleed. TLS 1.3 removed the features most of these depended on, and downgrade protection in its ServerHello random value blocks rollback.
- DNS. The Kaminsky cache-poisoning attack (article 54) made DNSSEC urgent. DNSSEC brought its own concerns: zone walking with NSEC, large signed answers usable for DNS amplification, operational fragility, and no way to revoke a signature before it expires.
- Implementation and people. Many real breaches came not from broken algorithms but from implementations (bad random numbers, unchecked certificates, timing leaks) and from configuration: accepting any server certificate, weak pre-shared keys, unrotated keys.
The lesson repeated across these is to use AEAD instead of separate encryption and MACs, to authenticate the whole handshake transcript, to remove options rather than add them, and to make the secure behavior the default.

Common questions
Do I need all three of SPF, DKIM and DMARC?
Yes, in practice. DMARC needs at least one aligned pass, and having both makes it robust: SPF covers mail where signing fails, DKIM covers forwarded mail. Large providers require all three for bulk senders.
Why did my legitimate mail fail DMARC?
Usually a third-party sender (a newsletter or ticketing service) sends as your domain without signing with your domain's DKIM key or using an aligned envelope domain. Aggregate reports show such senders; the fix is to set up DKIM for them with your domain.
Is ~all or -all better in SPF?
With DMARC in place, receivers act on the DMARC policy, so the difference matters little, and many domains use ~all to avoid hard rejections of forwarded mail by receivers that act on SPF alone. Without DMARC, -all is the stronger statement.
Does DKIM prove who wrote the message?
No. It proves that the signing domain's mail system handled it and that the signed parts are unchanged. Individuals sign personally with S/MIME or OpenPGP, which are end-to-end.
How long should a DKIM key be, and how often should it change?
RSA keys should be at least 1,024 bits, and 2,048 is the common choice now. Rotating keys every year or so, using a new selector and leaving the old key published for a while, limits the damage if a key leaks.
Why can't a phisher just use a look-alike domain?
They can, and do: DMARC protects only the exact domain (and its subdomains). Look-alikes such as examp1e.com are fought with other tools: registrars, brand monitoring, user interface warnings and spam filtering.
Summary
- SMTP has no sender authentication. The envelope sender (MAIL FROM) and the visible header From are different addresses.
- SPF publishes the IPs allowed to use a domain in MAIL FROM; it has a 10-lookup limit and breaks on forwarding.
- DKIM signs headers (listed in
h=) and a body hash with a key published atselector._domainkey.domain. In the lab, body and signed-header changes broke it; unsigned headers and whitespace (relaxed) didn't. - DMARC requires an SPF or DKIM pass aligned with the header From, publishes
p=none|quarantine|reject, and collects aggregate reports. It's now on the Standards Track (RFC 9989, 2026). - Forwarding that modifies messages breaks DMARC; From rewriting and ARC work around it. MTA-STS and DANE secure the transport between mail servers.
- Attacks on security protocols have come from weak algorithms, combining them badly (MAC-then-encrypt, encryption without integrity), downgrades and optional features, and implementations. Modern protocols answer with AEAD, transcript authentication and fewer options.
Practice questions
1. A domain's SPF record is v=spf1 include:a.example.net include:b.example.net include:c.example.net mx a -all, and each included record contains two more includes. Does it work?
Answer
Count lookups: 3 includes + their 6 nested includes + mx + a = 11, over the limit of 10, so evaluation returns permerror, which DMARC treats as an SPF failure. The domain needs to flatten some includes into ip4:/ip6: ranges or remove unused services.
2. A message has From: news@example.com, a valid DKIM signature with d=mailer.example.net, and passes SPF for bounces.example.net. Does it pass DMARC for example.com?
Answer
No. Both passes are for example.net, which doesn't align with example.com, even under relaxed alignment. The fix is to sign with d=example.com (the service publishing a key under the customer's domain) or use an envelope domain under example.com.
3. Why does DKIM list from twice in h= in the lab's signature?
Answer
Oversigning: each listed name covers one instance of that header, and an extra listing covers a non-existent one. If an attacker added a second From: header, the verifier would include it, and the signature would fail. Without oversigning, some mail programs might display the added From.
4. A domain publishes p=reject and its CEO's message to a mailing list, which adds a footer, is rejected by many list members' providers. Why, and what could the list do?
Answer
The footer changes the body, so DKIM fails, and SPF passes only for the list's own domain, which isn't aligned. DMARC fails and the receivers apply reject. The list can rewrite the From to its own domain (keeping the sender's display name and putting the original in Reply-To), stop modifying messages, or add ARC headers so receivers that trust it can override the failure.
5. Which of SPF, DKIM and DMARC would stop each of these: (a) a spammer sending From: billing@example.com from their own server; (b) a forwarder relaying an unmodified signed message; (c) an attacker altering an invoice's bank details in transit between servers?
Answer
(a) DMARC (with SPF and DKIM failing to align) lets receivers reject it. (b) Nothing should stop it: DKIM still verifies and keeps DMARC passing even though SPF fails. (c) DKIM: the body hash no longer matches, so the signature fails (and DMARC then fails too). TLS between servers, made mandatory by MTA-STS or DANE, would also stop the alteration.
6. Why does the TLS 1.3 ServerHello random value help against downgrade attacks?
Answer
A TLS 1.3 server that's forced to negotiate TLS 1.2 or lower puts a fixed marker in the last 8 bytes of its random value. That random is covered by the signature, so a man in the middle can't remove it, and a TLS 1.3 client that sees the marker knows someone forced the downgrade and aborts.
Review quiz
You've finished Part 13. Test yourself on threats and security properties, cryptography and post-quantum keys, certificates and revocation, 802.1X and EAP, IPsec and WireGuard, TLS 1.3, DNSSEC and email authentication:

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.