Skip to content

Add RFC3394 KeyWrap #130490

Closed
Closed

Description

@kirill-abblix

Background and motivation

.NET 10 added RFC 5649 key wrap ("AES Key Wrap with Padding") via #108332, exposed as Aes.EncryptKeyWrapPadded / Aes.DecryptKeyWrapPadded. As that proposal notes, RFC 3394 ("AES Key Wrap") and RFC 5649 are mutually unintelligible constructions, and only the padded one shipped. This proposal is for the unpadded sibling: RFC 3394, equivalently NIST SP 800-38F KW-AE / KW-AD.

The primary consumer is JOSE/JWT. RFC 7518 (JSON Web Algorithms) defines the JWE key management algorithms A128KW, A192KW and A256KW strictly as RFC 3394, and two more IANA-registered families build on it: ECDH-ES+A128/192/256KW and PBES2-HS256/384/512+A*KW. There is no JOSE algorithm identifier for RFC 5649, so the padded API cannot serve this scenario: 8 of the 17 JWE key management algorithms need plain RFC 3394.

Because the BCL does not expose it, every .NET JOSE implementation carries its own managed copy of the RFC 3394 block shuffle over AES-ECB: Microsoft.IdentityModel has one (SymmetricKeyWrapProvider), and we maintain another in Abblix.JWT. The proper layering would be the reverse: a single implementation in the BCL, with Microsoft.IdentityModel and third-party JOSE libraries consuming it instead of maintaining parallel copies of security-critical code.

The platform plumbing largely exists already: the .NET 10 padded implementation binds OpenSSL's EVP_aes_128/192/256_wrap_pad ciphers, and their RFC 3394 siblings EVP_aes_128/192/256_wrap sit in the same API family (both are thin wrappers over the CRYPTO_128_wrap / CRYPTO_128_wrap_pad pair referenced in #108332).

API Proposal

The proposed surface mirrors, method for method, the RFC 5649 API approved in #108332 and shipped in .NET 10.

The RFC 3394 sibling drops the Padded suffix and the length helper (the output length is deterministic: wrap adds exactly 8 bytes, unwrap removes exactly 8 bytes):

namespace System.Security.Cryptography
{
    public partial class Aes
    {
        // Existing
        //public byte[] EncryptKeyWrapPadded(byte[] plaintext);
        //public byte[] EncryptKeyWrapPadded(ReadOnlySpan<byte> plaintext);
        //public void EncryptKeyWrapPadded(ReadOnlySpan<byte> plaintext, Span<byte> destination);
        //protected virtual void EncryptKeyWrapPaddedCore(ReadOnlySpan<byte> source, Span<byte> destination);

        //public byte[] DecryptKeyWrapPadded(byte[] ciphertext);
        //public byte[] DecryptKeyWrapPadded(ReadOnlySpan<byte> ciphertext);
        //public int DecryptKeyWrapPadded(ReadOnlySpan<byte> ciphertext, Span<byte> destination);
        //protected virtual int DecryptKeyWrapPaddedCore(ReadOnlySpan<byte> source, Span<byte> destination);
        //public bool TryDecryptKeyWrapPadded(ReadOnlySpan<byte> ciphertext, Span<byte> destination, out int bytesWritten);

        //public static int GetKeyWrapPaddedLength(int plaintextLengthInBytes);
    
        public byte[] EncryptKeyWrap(byte[] plaintext);
        public byte[] EncryptKeyWrap(ReadOnlySpan<byte> plaintext);
        public void EncryptKeyWrap(ReadOnlySpan<byte> plaintext, Span<byte> destination);
        protected virtual void EncryptKeyWrapCore(ReadOnlySpan<byte> source, Span<byte> destination);

        public byte[] DecryptKeyWrap(byte[] ciphertext);
        public byte[] DecryptKeyWrap(ReadOnlySpan<byte> ciphertext);
        public int DecryptKeyWrap(ReadOnlySpan<byte> ciphertext, Span<byte> destination);
        protected virtual int DecryptKeyWrapCore(ReadOnlySpan<byte> source, Span<byte> destination);
        public bool TryDecryptKeyWrap(ReadOnlySpan<byte> ciphertext, Span<byte> destination, out int bytesWritten);

        public static int GetKeyWrapLength(int plaintextLengthInBytes);
    }
}

Input domain per RFC 3394 / SP 800-38F: the plaintext must be a multiple of 8 bytes and at least 16 bytes; the ciphertext is plaintext length + 8. DecryptKeyWrap throws CryptographicException when the integrity check register does not match (wrong KEK or tampered input).

API Usage

// JOSE A256KW (RFC 7518 section 4.4): wrap a content encryption key
using Aes kek = Aes.Create();
kek.Key = keyEncryptionKey; // 128-, 192- or 256-bit KEK

byte[] wrappedCek = kek.EncryptKeyWrap(contentEncryptionKey); // CEK length + 8
byte[] cek = kek.DecryptKeyWrap(wrappedCek);                  // throws on integrity mismatch

Alternative Designs

  • Naming: EncryptKeyWrap / DecryptKeyWrap follows directly from the shipped EncryptKeyWrapPadded / DecryptKeyWrapPadded by dropping the suffix; KeyWrapRfc3394 remains the "include the RFC number" fallback discussed in Add RFC5649 KeyWrap #108332.
  • A static int GetKeyWrapLength(int plaintextLengthInBytes) helper could be added for symmetry with GetKeyWrapPaddedLength, though the answer is always plaintextLengthInBytes + 8.
  • Downlevel exposure could follow the same route Add RFC5649 KeyWrap #108332 outlined: a polyfill in Microsoft.Bcl.Cryptography forwarding to the instance methods on .NET 11+.

Risks

No new risk surface: this is the second member of the key wrap family whose infrastructure shipped in .NET 10, and the mode is used for wrapping keys rather than general data. The algorithm is fully pinned by the six known-answer vectors of RFC 3394 section 4, which cover every KEK size and key data size combination the specification defines.

Activity

  1. added
    api-suggestion
    on Jul 10, 2026
  2. added
    untriaged
    on Jul 10, 2026
  3. dotnet-policy-service commented on Jul 10, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
    See info in area-owners.md if you want to be subscribed.

  4. vcsjones commented on Jul 16, 2026

    @vcsjones
    Member

    @bartonjs we should do this soon. We need it for KEMRecipientInfo for ML-KEM + EnvelopedCms. From Section 2.1 of RFC 9936

    Identifies a key-encryption algorithm used to encrypt the content-encryption key. Implementations supporting ML-KEM-512 MUST support the AES-Wrap-128 [RFC3394] key-encryption algorithm using the id-aes128-wrap key-encryption algorithm OID [RFC3565].
    Implementations supporting ML-KEM-768 or ML-KEM-1024 MUST support the AES-Wrap-256 [RFC3394] key-encryption algorithm using the id-aes256-wrap key-encryption algorithm OID [RFC3565]. Implementations MAY support other key-encryption algorithms as well.

  5. bartonjs commented on Jul 17, 2026

    @bartonjs
    Member

    KWP's DecryptCore needs to return the int because the padding is variable. If KW is always +8 on Enc and -8 on Dec then we don't really need the return, so it should probably be virtual void.

    That would then also apply to the Span-writing public method.

    Of course, then we have the good old "so do we just assert that someone who calls it needs to know about the +8/-8 relationship?"; so maybe it makes sense for the public method to return int and the virtual to be void, and then GetKeyWrapLength for the encrypt side? Or... a constant? public const int [Aes::]KeyWrapEncryptionOverhead = 8;?

  6. vcsjones commented on Jul 17, 2026

    @vcsjones
    Member

    GetKeyWrapLength

    I think following existing shape is probably the most reasonable thing to do, even if the information conveyed is superfluous. I'm fine leaving off the length calculation helper since it's "just" count + 8 instead of (count / BlockSize * BlockSize) + BlockSize. If we want the protected one to be void I am fine with that too, but the public ones should express the same semantics, returns values, etc.

  7. kirill-abblix commented on Jul 19, 2026

    @kirill-abblix
    Author

    As the proposer, I want to push back on my own proposal on one point: the length helper. The pitch here is mirroring EncryptKeyWrapPadded / DecryptKeyWrapPadded method for method, and GetKeyWrapPaddedLength is part of that shape, so leaving out GetKeyWrapLength is the one place the mirror breaks. Even though the value is trivially plaintext length + 8, keeping the method gives full structural symmetry: buffer-sizing code that selects padded or unpadded at runtime can call GetKeyWrap*Length uniformly, and it directly answers @bartonjs's question about whether the caller must know the +8/-8 relationship. They would not.

    So I agree with @bartonjs's suggestion and prefer the GetKeyWrapLength method over the KeyWrapEncryptionOverhead constant. The method is both the more symmetric choice and the more expected one for anyone already familiar with the padded API, who will look for GetKeyWrapLength next to GetKeyWrapPaddedLength and be mildly surprised by its absence. The constant would work, but it hands the +8/-8 relationship back to the caller, the very thing a length method removes. I would take it only as a fallback, if a full method is judged too much surface for a fixed offset.

    On everything else I am with @vcsjones: mirror the shipped shape and keep the public return semantics identical.

  8. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Jul 24, 2026
  9. added this to the 11.0.0 milestone on Jul 24, 2026
  10. bartonjs commented on Jul 24, 2026

    @bartonjs
    Member

    Okay, I updated the top post to what I believe is consensus (just replicate all of KWP and remove "Padded"), and marked it ready for review.

  11. self-assigned this
    on Jul 24, 2026
  12. bartonjs commented on Aug 11, 2026

    @bartonjs
    Member

    Video

    • We discussed no-suffix vs "Unpadded" and went with no-suffix as that matches the algorithm name.
    namespace System.Security.Cryptography
    {
        public partial class Aes
        {
            public byte[] EncryptKeyWrap(byte[] plaintext);
            public byte[] EncryptKeyWrap(ReadOnlySpan<byte> plaintext);
            public void EncryptKeyWrap(ReadOnlySpan<byte> plaintext, Span<byte> destination);
            protected virtual void EncryptKeyWrapCore(ReadOnlySpan<byte> source, Span<byte> destination);
    
            public byte[] DecryptKeyWrap(byte[] ciphertext);
            public byte[] DecryptKeyWrap(ReadOnlySpan<byte> ciphertext);
            public int DecryptKeyWrap(ReadOnlySpan<byte> ciphertext, Span<byte> destination);
            protected virtual int DecryptKeyWrapCore(ReadOnlySpan<byte> source, Span<byte> destination);
            public bool TryDecryptKeyWrap(ReadOnlySpan<byte> ciphertext, Span<byte> destination, out int bytesWritten);
    
            public static int GetKeyWrapLength(int plaintextLengthInBytes);
        }
    }
  13. added
    api-approved
    and removed
    api-ready-for-review
    on Aug 11, 2026
  14. locked and limited conversation to collaborators on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Security

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions