Definition
Key wrapping is encryption of a cryptographic key under a second key, the key-encryption key (KEK), in a form that keeps the key secret and reveals any change: unwrapping returns exactly the original key or fails. Ordinary encryption is designed for general data, which an attacker may know or choose. Its basic modes, such as counter mode §15.5, hide the data, detect no change and need a nonce §5.18, a value never used before with that key, for each message. Key wrapping always detects changes, and its dedicated modes need no nonce.
The idea
A device encrypts its stored data with a key, here called the protected key, and must keep that key in external flash memory §6.6, which an attacker with the device in hand can read and rewrite. A second key, the KEK, stays inside the chip, where the attacker cannot read it. The device must therefore store the protected key under the KEK. A server that sends a new key to a device over a network faces the same task. Two properties are needed: the stored bytes reveal nothing about the protected key, and any change to the stored bytes is detected before the key is used.
A tempting design derives a pad, a string of pseudorandom bytes as long as the key, from the KEK with a key derivation function §5.20 (an algorithm that turns one secret into further keys), and stores the key XOR the pad. XOR (exclusive or) compares two bit strings position by position and outputs 1 where the bits differ, so applying XOR with the same pad a second time gives back the original. If the pad is secret and used only once, the stored bytes reveal nothing about the key. Secrecy is the only property this design has. An attacker who flips a stored bit flips the same bit of the recovered key, and unwrapping never fails, so the device uses an altered key without noticing. A scheme in which an attacker can change encrypted data so that it decrypts to a related value is called malleable. And because the pad depends only on the KEK and a fixed label, the next key stored in the same place gets the same pad.
Key wrapping works like a sealed, tamper-evident envelope: the content is hidden, and any tampering breaks the seal, so the envelope is refused. The XOR design is an opaque envelope with no seal: the attacker cannot read the content but can change parts of it blindly, and the receiver cannot tell. The analogy stops at one point. A seal shows that an envelope is unchanged, not that it is the right envelope. An intact wrapped key moved from another slot (a storage place meant for a different key), or saved from before a key change §5.29, still opens, unless the format seals the slot name and the version inside as well.
Ordinary encryption must also hide whether two messages are equal, because messages repeat and an attacker may choose them. It therefore takes a fresh nonce or a random initialization vector (a per-message starting value) for every message. The plaintext of key wrapping, the input it encrypts, is a secret random key. Equal plaintexts occur only when the same key is wrapped twice, and an observer then learns only that fact. Dedicated key wrapping modes are therefore deterministic: the same key wrapped under the same KEK always gives the same output, and no nonce is taken, which suits a device that has no reliable source of unique values.
How it works
Key wrapping involves three values: the KEK, used only to wrap and unwrap other keys; the protected key, which encrypts or authenticates the application’s data; and the wrapped key, the output of wrapping, which can be stored or sent where an attacker can read and change it.
AES (Advanced Encryption Standard) is a block cipher §15.3: a keyed function that maps each 16-byte block to a 16-byte block in a reversible way, where neither direction can be computed without the key. The AES Key Wrap mode (KW), defined in RFC 3394 [307] and in NIST Special Publication (SP) 800-38F, builds on AES. SP 800-38F calls an 8-byte half of a block a semiblock. The KEK is an AES key of 16, 24 or 32 bytes. The protected key is split into semiblocks , with , so it must be at least 16 bytes long and a multiple of 8 bytes. Wrapping works as follows.
Set an 8-byte check register to the constant
a6a6a6a6a6a6a6a6, written in hexadecimal. RFC 3394 calls it the default initial value; SP 800-38F calls it the integrity §1.9 check value. Set for each .Make six passes, numbered to . In each pass, for to : encrypt the 16-byte block made of followed by with AES under the KEK; set to the first 8 bytes of the result, combined by XOR with the step number written as an 8-byte number; set to the last 8 bytes of the result.
Output end to end: semiblocks, 8 bytes more than the protected key.
Wrapping costs AES operations; a 16-byte key, with , costs 12.
Unwrapping runs the same steps backwards with AES decryption: the passes from down to , and within each pass from down to , each time combining with by XOR before decrypting. At the end, if equals a6a6a6a6a6a6a6a6, unwrapping returns . Otherwise it returns failure and releases none of the . SP 800-38F describes KW as robust in the sense that each output bit depends on each input bit. A wrapped key changed anywhere is therefore expected to unwrap to a value of that behaves like a random 8-byte value, and a random 8-byte value equals the constant with probability .
For keys of any other length, the padded variant, AES Key Wrap with Padding (KWP), uses in place of the 8-byte constant the 4-byte constant a65959a6 followed by the key’s length in bytes as a 4-byte number, and appends zero bytes up to a multiple of 8 bytes. Unwrapping fails unless the constant, the length and the zero bytes all check out.
Other designs also give both secrecy and change detection. AES-SIV §19.1.11, defined in RFC 5297 (SIV stands for synthetic initialization vector), is also deterministic and also takes associated data, inputs that are authenticated but not encrypted, which can tie a wrapped key to its purpose. Authenticated encryption §5.16 that takes a nonce, such as AES-GCM (GCM stands for Galois/Counter Mode), also protects keys if each nonce is used only once under the KEK. SP 800-38F approves authenticated-encryption modes for keys, and KW and KWP for general data. The dedicated modes are slower: KW makes 6 AES operations per 8 bytes of input, counter mode 1 per 16 bytes.
KW and KWP authenticate the key bytes and nothing else. To tie a wrapped key to its slot, its type and its version, a design does one of three things: it passes those fields as associated data to a mode that accepts associated data; it places them inside the wrapped plaintext next to the key and checks them after unwrapping; or it gives each purpose its own KEK. In every case the unwrapping code compares the fields with the values it expects and refuses a mismatch.
Worked example
The pad design first. The protected key and an invented 16-byte pad , standing for bytes derived from the KEK, are, in hexadecimal:
=
00112233445566778899aabbccddeeff
=5b0e9a47c21d83f6a9e5107b4c28d93c
The stored value is XOR , byte by byte. The first byte is 00 XOR 5b = 5b. The last byte is ff XOR 3c: in binary, 11111111 XOR 00111100 = 11000011, which is c3. In full:
=
5b1fb8748648e581217cbac080f537c3
An attacker changes the last stored byte from c3 to c2, flipping its lowest bit, without knowing or . The device computes c2 XOR 3c: 11000010 XOR 00111100 = 11111110, which is fe. The device now uses the key 00112233445566778899aabbccddeefe, which differs from in exactly the flipped bit, and nothing reports an error.
Later the device replaces with a new key and stores XOR under the same pad. An attacker who copied both stored values computes XOR :
=
c4a1f0273e9b5d8812f6a0b7e35c9d41
=9faf6a60fc86de7ebb13b0ccaf74447d
XOR =c4b0d2147ace3bff9a6f0a0c2f8173be
The result equals XOR , because XOR is all zeros. If the old key ever leaks, the attacker computes XOR XOR and obtains . For the first byte: 5b XOR 9f = c4, and c4 XOR 00 = c4, the first byte of .
Now KW, with test vector 4.1 of RFC 3394: the 16-byte KEK 000102030405060708090a0b0c0d0e0f and the same protected key . Here , so wrapping makes AES operations and outputs bytes:
1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5
Unwrapping these bytes ends with = a6a6a6a6a6a6a6a6, so the device accepts them and recovers .
The attacker again flips the lowest bit of the last byte, so e5 becomes e4. Unwrapping now ends with = ecc2839d214839b6, which is not the constant, so the device refuses the wrapped key and uses no key from it. The bytes it would have returned, d09fdd8c6d2466a17bbc3eca1ddf9fbf, show no visible relation to : one flipped bit scrambled every byte. These values were computed with an AES Key Wrap implementation, and the unchanged wrapped key equals the value printed in RFC 3394.
Stated precisely
Write for wrapping the key under a KEK, and for unwrapping the byte string . Unwrap returns either a key or the failure symbol . Correctness requires for every KEK and every key of an allowed length.
For the pad design, let be the pad and let denote XOR. For every bit pattern that an attacker combines by XOR with the stored value, so unwrapping accepts and returns the key changed by exactly . If keys and are stored under the same pad, then .
The security goal of key wrapping is deterministic authenticated encryption §19.1.10, defined in 2006 by Phillip Rogaway and Thomas Shrimpton [435], who also designed SIV to meet it. An attacker gets two services under one KEK that it does not know: wrapping of keys it chooses and unwrapping of byte strings it chooses. The scheme meets the goal when no feasible attacker can tell these services apart from an ideal pair that answers every wrapping request with fresh random bytes of the correct length and every unwrapping request with . The attacker may not repeat a wrapping request, which a deterministic scheme answers with the same bytes, nor ask to unwrap a string that wrapping returned to it. Meeting the goal implies two things: a wrapped key reveals nothing about the key except its length and whether the same key was wrapped before under that KEK, and unwrapping rejects every string that wrapping did not produce.
Any claim that a mode meets this goal rests on four assumptions.
The KEK is uniformly random, meaning that every value of its length is equally likely, and it is secret.
The KEK is used for nothing but wrapping with this one mode.
The block cipher behaves as a pseudorandom permutation §15.2: under a secret key, no feasible computation can tell it apart from a reversible mapping of blocks chosen at random.
The data wrapped under one KEK stays within the limits of the mode. For KW, SP 800-38F limits a plaintext to fewer than semiblocks.
Limits and common mistakes
Key wrapping moves the secrecy problem to the KEK; it does not remove it. Whoever holds the KEK can read every key wrapped under it and can create wrapped keys that unwrap correctly. A KEK stored next to the wrapped keys, or built into firmware that attackers can read, protects nothing.
Integrity is not freshness. A wrapped key saved before a key change still unwraps correctly under the same KEK, so wrapping a new key does not revoke the old wrapped key. An attacker who writes the old wrapped key back rolls the device back to the old key. A version number inside the authenticated data, checked against a stored minimum, prevents this, and so does changing the KEK.
KW checks the key bytes only. Under one KEK, a wrapped key moved from one slot to another unwraps without error. Bind the slot and the key’s type as described above, or use a separate KEK for each purpose, which is key separation §5.23.
Deterministic modes show repetition: wrapping the same key twice under one KEK gives the same bytes. This is harmless for fresh random keys, and it leaks information when the wrapped values come from a small set of possibilities.
Wrapping protects a key only while it is stored or sent. After unwrapping, the key is plaintext in memory; erase it when it is no longer needed (secret zeroization §5.37).
On failure, unwrapping must return one error and no part of the candidate key. Separate errors for a bad length, bad padding and a bad check value give an attacker information about the candidate.
The 8-byte check of KW stops blind changes, each of which passes with probability . It is not a signature: anyone who holds the KEK can produce valid wrapped keys, so a valid wrapped key shows only that its producer held the KEK.
Questions answered
Why is XOR with a derived pad insufficient as a complete design?
XOR with a derived pad is insufficient because it provides secrecy and nothing else, while protecting a stored or transmitted key also needs change detection, a defined failure and safe reuse. In this design a pad of pseudorandom bytes is derived from a key-encryption key (KEK) and combined with the protected key by XOR (exclusive or), the bitwise operation §4.50 that outputs 1 where two bits differ; applying XOR with the pad again recovers the key. The design fails in four ways.
No integrity. Flipping a bit of the stored value flips the same bit of the recovered key. An attacker can change the key in chosen ways without knowing it, and the receiver never notices.
No failure. Unwrapping always returns some bytes, so a wrong KEK, corrupted storage or a substituted value silently produces a wrong key.
Pad reuse. A pad derived from the KEK and a fixed label is the same every time. If keys and are stored under it as and , then XOR equals XOR , so learning one key reveals the other.
No context. Nothing ties the stored value to the key’s slot, type or version, so old or misplaced values are accepted.
A complete design uses a key wrapping mode, so that unwrapping returns exactly the wrapped key or fails. Examples are the AES Key Wrap mode (KW), built on the Advanced Encryption Standard (AES) block cipher and defined in RFC 3394 and NIST Special Publication (SP) 800-38F; AES-SIV (synthetic initialization vector); and authenticated encryption with a nonce that is never reused under the KEK. The design also binds the slot, type and version that the receiver checks, uses the KEK only for wrapping, and handles failure with one error that releases nothing.
Related entries: Authenticated encryption (AEAD) (§5.16), Key separation (§5.23), Root key and root seed (§5.25), Secret zeroization (§5.37), Counter mode (CTR) (§15.5), Deterministic authenticated encryption and equality leakage (§19.1.10), Synthetic initialization vector (SIV) and misuse resistance (§19.1.11). Further reading: [149, 153, 307, 435].