A DNSKEY Has a Key Tag
A DNSKEY's key tag is a 16-bit checksum of its RDATA; the first Ed25519 key of RFC 8080 has tag 3613, and the tag finds candidate keys without proving which one signed.
The question
A resolver that holds many DNSKEY records needs a quick way to say which key a signature names. How does a DNSKEY get its 16-bit key tag, and what does the tag prove?
This book uses published test material: the DNSKEY records of RFC 8080, section 6, and the one in RFC 4034, section 5.4. They are public records made for the standards and protect nothing. The names are wire bytes as taught in Names Are a Hierarchy; no DNS message is parsed here.
Work it by hand
The RDATA of a DNSKEY is the flags (2 bytes: 256 for a zone key, 257 when the SEP bit is also set; RFC 4034, 2.1.1), the protocol (3, RFC 4034, 2.1.2), the algorithm (15 is Ed25519 and 16 is Ed448, RFC 8080, section 5) and the public key.
The key tag is a checksum of that RDATA (RFC 4034, Appendix B). Add the bytes as 16-bit numbers: each byte at an even position (the first is position 0) counts as the high byte of a number and each byte at an odd position as the low byte, so an odd number of bytes ends on a high byte. Then add the carry, the sum divided by 65536, once, and keep the low 16 bits.
Run it
The program takes the RDATA of the first Ed25519 DNSKEY of RFC 8080, section 6.1, as hex in rdata. Lines 2 to 5 give the fields by position. The loop of lines 7 to 9 adds the bytes two at a time as 16-bit numbers; lines 10 to 12 add the last byte as a high byte if the length is odd; line 13 adds the carry; line 14 adds the row with the key tag; line 15 returns the rows.

Watch the steps (recorded video)
Public test material, generated for this lesson. Never use it for anything real.
let rows = str_list();
rows.add("flags|" + str(hex_u16(rdata, 0)));
rows.add("protocol|" + str(hex_u8(rdata, 2)));
rows.add("algorithm|" + str(hex_u8(rdata, 3)));
rows.add("public key bytes|" + str(hex_len(rdata) - 4));
let words = 0;
for (k in range(0, hex_len(rdata) / 2)) {
words = words + hex_u16(rdata, 2 * k);
}
if (hex_len(rdata) % 2 == 1) {
words = words + hex_u8(rdata, hex_len(rdata) - 1) * 256;
}
words = words + words / 65536;
rows.add("key tag|" + str(words % 65536));
let answer = rows;
{
"answer": [
"flags|257",
"protocol|3",
"algorithm|15",
"public key bytes|32",
"key tag|3613"
]
}The key tag it gives, 3613, is the one RFC 8080 publishes in the DS record of the same key. You can change the RDATA freely as long as it has at least 4 bytes: any bytes give a tag, and the rows show the fields at the positions where a real DNSKEY has them. A 2-byte RDATA stops the run with "byte 2 is past the end of the data".
Your turn
A resolver tool needs the key tag of a DNSKEY. The program below is meant to return it, as an integer, for the RDATA in rdata: flags, protocol, algorithm and key bytes. Decide whether it does, and find the candidates that do. Check runs each candidate on the RDATA shown and on five others. The RDATA shown and most of the others are published (RFC 8080, sections 6.1 and 6.2, and RFC 4034, section 5.4); two are made up for this page.

Public test material, generated for this lesson. Never use it for anything real.
let total = 0;
for (i in range(0, hex_len(rdata))) {
let b = hex_u8(rdata, i);
if (i % 2 == 0) { total = total + b; } else { total = total + b * 256; }
}
let answer = (total + total / 65536) % 65536;
{
"answer": 61733
}Return the key tag of a DNSKEY, as an integer, from its RDATA in rdata (RFC 4034, Appendix B).
Read the result carefully
The tag is a checksum and not a signature: two different keys can have the same tag, so a tag only finds candidates for a validator to try. The page computes tags of published keys and proves nothing about any key in use. It does not show how a resolver finds a DNSKEY, and the keys are published test material that protect nothing.
The RSA example of RFC 4034 is shown with algorithm 5 only as published data: the page does not teach RSA keys in DNSSEC or say that an algorithm is secure or insecure.
Recap
- A DNSKEY's RDATA is flags, protocol 3, algorithm and the public key.
- The key tag is a 16-bit checksum of the whole RDATA.
- The carry is added once and the low 16 bits are kept.
- A key tag finds candidates; it does not identify a key.