| ... | @@ -122,6 +122,9 @@ fn AesCcm(comptime BlockCipher: type, comptime tag_len: usize, comptime nonce_le | ... | @@ -122,6 +122,9 @@ fn AesCcm(comptime BlockCipher: type, comptime tag_len: usize, comptime nonce_le |
| 122 | ) AuthenticationError!void { | 122 | ) AuthenticationError!void { |
| 123 | assert(m.len == c.len); | 123 | assert(m.len == c.len); |
| 124 | | 124 | |
| | 125 | const max_msg_len: u64 = if (L >= 8) std.math.maxInt(u64) else (@as(u64, 1) << @as(u6, @intCast(L * 8))) - 1; |
| | 126 | if (c.len > max_msg_len) return error.AuthenticationFailed; |
| | 127 | |
| 125 | const cipher_ctx = BlockCipher.initEnc(key); | 128 | const cipher_ctx = BlockCipher.initEnc(key); |
| 126 | | 129 | |
| 127 | // Decrypt the ciphertext using CTR mode (starting from counter = 1) | 130 | // Decrypt the ciphertext using CTR mode (starting from counter = 1) |
| ... | @@ -874,3 +877,11 @@ test "Aes256Ccm0 - Basic encryption-only round-trip" { | ... | @@ -874,3 +877,11 @@ test "Aes256Ccm0 - Basic encryption-only round-trip" { |
| 874 | | 877 | |
| 875 | try testing.expectEqualSlices(u8, m[0..], m2[0..]); | 878 | try testing.expectEqualSlices(u8, m[0..], m2[0..]); |
| 876 | } | 879 | } |
| | 880 | |
| | 881 | test "Aes256Ccm decryption of oversized ciphertext" { |
| | 882 | const key: [32]u8 = @splat(0); |
| | 883 | const nonce: [13]u8 = @splat(0); |
| | 884 | const tag: [Aes256Ccm16.tag_length]u8 = @splat(0); |
| | 885 | var buf: [65536]u8 = @splat(0); |
| | 886 | try testing.expectError(error.AuthenticationFailed, Aes256Ccm16.decrypt(&buf, &buf, tag, "", nonce, key)); |
| | 887 | } |