Repository navigation
Description
Edited by @vcsjones
API Proposal:
As the issue outlines below, this proposes changes to AesGcm because it's current design makes it prone to misuse of allowing truncated authentication tags during decryption.
This proposes new constructors that accept the authentication tag size. During decryption or encryption, the tag parameter must match this specified exactly. This means the caller must decide at instantiation what tag size is correct, not what callers of Encrypt or Decrypt are provided.
The current constructors will continue to have the same existing behavior. However, since this is an issue around misuse, this also proposes obsoleting the existing constructors to steer people toward using constructors which take the authentication tag size.
namespace System.Security.Cryptography;
public partial class AesGcm {
+ public AesGcm(byte[] key, int tagSizeInBytes);
+ public AesGcm(ReadOnlySpan<byte> key, int tagSizeInBytes);
+ [Obsolete("AesGcm should indicate the required tag size for encryption and decryption. Use a constructor that accepts the tag size.", DiagnosticId = "SYSLIB9999")]
public AesGcm(byte[] key);
+ [Obsolete("AesGcm should indicate the required tag size for encryption and decryption. Use a constructor that accepts the tag size.", DiagnosticId = "SYSLIB9999")]
public AesGcm(ReadOnlySpan<byte> key);
+ public int? TagSizeInBytes { get; }
}Original issue:
void Main()
{
Span<byte> plaintext = Encoding.UTF8.GetBytes("[Hello World!]");
Span<byte> key = new byte[32];
Span<byte> nonce = new byte[12];
Span<byte> ciphertext = new byte[plaintext.Length];
Span<byte> tag = new byte[16];
using var gcm = new AesGcm(key);
Encoding.UTF8.GetString(plaintext).Dump();
gcm.Encrypt(nonce, plaintext, ciphertext, tag);
plaintext.Clear();
gcm.Decrypt(nonce, ciphertext, tag.Slice(0, 12), plaintext);
Encoding.UTF8.GetString(plaintext).Dump();
}Scenario:
A naive (documentation-reading) user uses AesGcm to encrypt plaintext and produce ciphertext with the maximum allowed tag of 16 bytes (ie. naively expecting ~128 bits of tag strength). However the Decrypt API is quite happy to accept truncated 12-byte tags, which means that the attacker can truncate all tags to 12 bytes for ~96 bits of tag strength (assuming decryption oracle is readily available).
AesGcmn-bit tags provide onlyn-kbits of authentication security for messages that are2^kblocks long- Large files at ~64GiB size are quite common, and it's plausible to assume that close to 2^32 blocks are routinely encrypted
Would that suggest that AesGcm tags as implemented in .NET realistically provide close to 96-32=64 bits of security? If yes - is anyone concerned about that? (both in terms of implementation/security and in terms of documentation). If no - please explain and help me understand what I'm missing. Thanks.
Activity
vcsjones commentedon Jun 28, 2022
Tag truncation is just how AES GCM supports short tags, as demonstrated by:
This is not a .NET-ism, either. For example, Ruby:
Ruby will even let you truncate it below 12 octets.
Since the AES-GCM implementation supports short tags,
Decryptcan't tell the difference between a tag that was truncated legitimately or illegitimately.Consumers of
AesGcmshould ensure that the tag length that is being supplied is the expected length.sdrapkin commentedon Jun 28, 2022
Thanks for confirming that my understanding is correct.
I see 2 issues:
AesGcmconsumers are expected to ensure for tag lengths. The description for thetagparameter to.Decryptstates:The authentication tag produced for this message during encryption(which is clearly incorrect when truncated tags are accepted, since they were likely never produced by the Encryption).Decryptunsafe for direct exposure (ie. requires a tag-length validator in front of it). N must become part of the caller-state/protocol, since it is not part of theEncryptoutput.AesGcmprimitive API alone. Our key assumption is thatciphertextandtagare malleable (which is precisely what the tag is supposed to guard against). While the tag-length could be made non-malleable (option-1), ciphertext-length cannot be made non-malleable. While Microsoft (rightfully) does not prescribe how the tag and ciphertext are to be stored, in the vast majority of uses they are concatenated into one bytestream (order is not relevant). Thus, it is not possible to calculate the correct value of tag-length N from the (ciphertext+tag) bytestream. One approach is to callDecrypt5 times for each of the 5 possible values of N (not an appealing idea for obvious reasons).Given that it seems option-1 is the only practical/reasonable choice, it would also need to be added to
AesGcmdocumentation (ie. guidance that N must be explicitly fixed for safe operation ofDecrypt, which includes tag validation).Finally, it might be a good idea to also document a caution that
AesGcmtag of length N bytes should not be assumed to provide N*8 bits of authentication strength in all scenarios.Thoughts?
sdrapkin commentedon Jun 28, 2022
What GCM specification does
S.S.C.AesGcmactually implement?AEAD_AES_128_GCMandAEAD_AES_256_GCM. Both fix the tag-length to 16 bytes.[tag length t]from among the supported choices shall be associated with each key." (whichS.S.C.AesGcmdoes not do)Tas the Encryption-step-produced tag,T'as the computed Decryption-step-produced tag, and states that "If the two tags match (in both length and value), then the ciphertext is returned. Otherwise, the special symbol FAIL is returned." (which is obviously not howS.S.C.AesGcmbehaves)sdrapkin commentedon Jun 28, 2022
Proposed improvements to
AesGcmAPIs (non-breaking, backwards-compatible):Change internal implementation for
by adding the following check to it:
sdrapkin commentedon Jun 30, 2022
Example of a likely vulnerability in Microsoft's code due to incorrect assumptions of what
AesGcm.Decryptdoes/doesn't do:MS domain: AzureAD
Repo: azure-activedirectory-identity-model-extensions-for-dotnet
Code in question
If I traced their call-paths right, the
authenticationTagis coming straight from JWT token field, which is treated as any-length base64-encoded string. An attacker can be truncating all JWT tokens to 12-byte Auth-tags which reduces GCM tag strength to 96 bits of security at most. I doubt AzureAD team realizes this...I'm sure there are many more cases like this one, where the
authentication Taglives separately fromciphertext(ie. not a meretake X fixed bytes from the end of ciphertextslicing).AesGcmnot having safe-by-default constructors (that default to fixed tag of 16 bytes) is going to be the cause of numerous security vulnerabilities. In the meantime I've created AesGcmStrict wrapper to help deal with this.bartonjs commentedon Jul 13, 2022
We're exposing GCM in the same way the underlying providers are, which leaves the algorithms loose tag processing as an application problem. (I'd've included the tag length in the tag calculation, but I guess the GCM designers disagree)
Generally the application context should know what the tag size is. In the case of JWA, it's supposed to always be 128 bits (https://datatracker.ietf.org/doc/html/rfc7518#section-4.7), which is up to the implementer to verify.
Adding ctor overloads for the expected tag size is reasonable, and not limiting to anyone following the SP800-38D recommendation, and I guess helps provide a standardized exception message to anyone who chooses to utilize it.
sdrapkin commentedon Jul 14, 2022
Thanks for the response. I would encourage .NET team to consider adding tag-length-fixing constructor overloads to
AesGcm, based on @bartonjs reply.Do note that the
AesCcmbehavior does not have the same problem - ie. truncatedAesCcmtags (that are still length-permitted based onAesCcm.TagByteSizes) will fail to decrypt. This creates 2 different (and undocumented) behaviors forAesGcmandAesCcmAPIs in .NET.Just do a simple sanity test: ask any of your esteemed cryptography-aware colleagues the following simple question:
..and watch how many no's you're going to get.
I believe it would be somewhat irresponsible to take the position that "loose AesGcm tag processing" is application's problem. Arguing that .NET team is merely exposing the deficiencies of underlying GCM providers is a dodge at best (at the very least you cannot vouch for all possible GCM providers on all supported current and future .NET-running platforms). However let me provide a specification-based argument:
NIST SP 800-38D :
5.2.1.2 Output Datatis the bit-length of tagT5.2.2 Authenticated Decryption Functionthe authenticated decryption function are values for IV, A, C, and T, as described in Sec. 5.2.1 above."
What this means is that for a specific instance of AesGcm implemented to the spec:
...
one_gcm.Decrypt(...)MUST NOT accept tag-lengths that differ from those produced byone_gcm.Encrypt(...)Ask any FIPS auditor/validator lab, and they should tell you that the GCM spec wording above is specifically intended to prevent tag truncations under a fixed key. The sister CCM spec (800-38C) which targets the same AEAD behavior has the same constraint:
denoted
Tlen, is a parameter of the mode that shall be fixed for all invocations of CCM with the given key."Thus it is quite likely that the current status of
AesGcmin .NET is that it is not spec-compliant.vcsjones commentedon Jul 14, 2022
I'm not quite sure I follow this logic.
The "selected" tag length just happens to be whatever
tag.Lengthis.CCM does not have this problem because CCM bakes the MAC length in to the control information block:
Where t is "The octet length of the MAC." CCM is an entirely different mode. While CCM and GCM are both AEADs I do not think it is entirely reasonable to consider their behaviors and design as interchangeable. GCM has no control information block.
By your logic then, is OpenSSL not "spec compliant" for GCM?
4 remaining items
vcsjones commentedon May 5, 2023
The top post has been updated with an API proposal.
bartonjs commentedon May 9, 2023
Video
Looks good as proposed.
namespace System.Security.Cryptography; public partial class AesGcm { + public AesGcm(byte[] key, int tagSizeInBytes); + public AesGcm(ReadOnlySpan<byte> key, int tagSizeInBytes); + [Obsolete("AesGcm should indicate the required tag size for encryption and decryption. Use a constructor that accepts the tag size.", DiagnosticId = "SYSLIB9999")] public AesGcm(byte[] key); + [Obsolete("AesGcm should indicate the required tag size for encryption and decryption. Use a constructor that accepts the tag size.", DiagnosticId = "SYSLIB9999")] public AesGcm(ReadOnlySpan<byte> key); + public int? TagSizeInBytes { get; } }vcsjones commentedon May 15, 2023
Breaking change doc: dotnet/docs#35338