#crypto #aes-kw #kw #aes-kwp #kwp

no-std aes-kw

NIST 800-38F AES Key Wrap (KW) and Key Wrap with Padding (KWP) modes

3 unstable releases

Uses new Rust 2021

0.2.1 Apr 20, 2022
0.2.0 Feb 10, 2022
0.1.0 Jan 6, 2022
0.0.0 Jan 3, 2022

#883 in Cryptography

Download history 1288/week @ 2022-06-11 1538/week @ 2022-06-18 1781/week @ 2022-06-25 1466/week @ 2022-07-02 1816/week @ 2022-07-09 1657/week @ 2022-07-16 1582/week @ 2022-07-23 1618/week @ 2022-07-30 1511/week @ 2022-08-06 1765/week @ 2022-08-13 2034/week @ 2022-08-20 1831/week @ 2022-08-27 2058/week @ 2022-09-03 1855/week @ 2022-09-10 2145/week @ 2022-09-17 1815/week @ 2022-09-24

8,195 downloads per month
Used in 17 crates (via deno_crypto)


315 lines

RustCrypto: AES Key Wrap Algorithm

crate Docs Apache2/MIT licensed Rust Version Build Status

Pure Rust implementation of the NIST AES-KW Key Wrap and NIST AES-KWP Key Wrap with Padding modes also described in RFC3394 and RFC5649.


RFC3394 § 2 describes AES-KW as follows:

The AES key wrap algorithm is designed to wrap or encrypt key data. The key wrap operates on blocks of 64 bits. Before being wrapped, the key data is parsed into n blocks of 64 bits.

The only restriction the key wrap algorithm places on n is that n be at least two. (For key data with length less than or equal to 64 bits, the constant field used in this specification and the key data form a single 128-bit codebook input making this key wrap unnecessary.) The key wrap algorithm accommodates all supported AES key sizes. However, other cryptographic values often need to be wrapped. One such value is the seed of the random number generator for DSS. This seed value requires n to be greater than four. Undoubtedly other values require this type of protection. Therefore, no upper bound is imposed on n.

The AES key wrap can be configured to use any of the three key sizes supported by the AES codebook. The choice of a key size affects the overall security provided by the key wrap, but it does not alter the description of the key wrap algorithm. Therefore, in the description that follows, the key wrap is described generically; no key size is specified for the KEK.

RFC5649 § 1 describes AES-KWP as follows:

This document specifies an extension of the Advanced Encryption Standard (AES) Key Wrap algorithm [AES-KW1, AES-KW2]. Without this extension, the input to the AES Key Wrap algorithm, called the key data, must be a sequence of two or more 64-bit blocks.

The AES Key Wrap with Padding algorithm can be used to wrap a key of any practical size with an AES key. The AES key-encryption key (KEK) must be 128, 192, or 256 bits. The input key data may be as short as one octet, which will result in an output of two 64-bit blocks (or 16 octets). Although the AES Key Wrap algorithm does not place a maximum bound on the size of the key data that can be wrapped, this extension does so. The use of a 32-bit fixed field to carry the octet length of the key data bounds the size of the input at 2^32 octets. Most systems will have other factors that limit the practical size of key data to much less than 2^32 octets.


The most common way to use AES-KW is as follows: you provide the Key Wrapping Key and the key-to-be-wrapped, then wrap it, or provide a wrapped-key and unwrap it.

# fn main() -> Result<(), Box<dyn std::error::Error>> {
# #[cfg(feature = "std")]
# {
use aes_kw::Kek;
use hex_literal::hex;

let kek = Kek::from(hex!("000102030405060708090A0B0C0D0E0F"));
let input_key = hex!("00112233445566778899AABBCCDDEEFF");

let wrapped_key = kek.wrap_vec(&input_key)?;
assert_eq!(wrapped_key, hex!("1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5"));

let unwrapped_key = kek.unwrap_vec(&wrapped_key)?;
assert_eq!(unwrapped_key, input_key);
# }
# Ok(())
# }

Alternatively, AES-KWP can be used to wrap keys which are not a multiple of 8 bytes long.

# fn main() -> Result<(), Box<dyn std::error::Error>> {
# #[cfg(feature = "std")]
# {
use aes_kw::Kek;
use hex_literal::hex;

let kek = Kek::from(hex!("5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8"));
let input_key = hex!("c37b7e6492584340bed12207808941155068f738");

let wrapped_key = kek.wrap_with_padding_vec(&input_key)?;
assert_eq!(wrapped_key, hex!("138bdeaa9b8fa7fc61f97742e72248ee5ae6ae5360d1ae6a5f54f373fa543b6a"));

let unwrapped_key = kek.unwrap_with_padding_vec(&wrapped_key)?;
assert_eq!(unwrapped_key, input_key);
# }
# Ok(())
# }

Implemented for 128/192/256bit keys.

Minimum Supported Rust Version

This crate requires Rust 1.56 at a minimum.

We may change the MSRV in the future, but it will be accompanied by a minor version bump.


Licensed under either of:

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~13K SLoC