-
Notifications
You must be signed in to change notification settings - Fork 1
fix(security): AES key/IV validation, SecureRandom.base64Url, GCM docs + tests #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,12 @@ class AES implements Algorithm { | |
|
|
||
| AES(this.key, {this.mode = AESMode.sic, this.padding = 'PKCS7'}) | ||
| : _streamCipher = padding == null && _streamable.contains(mode) ? StreamCipher('AES/${_modes[mode]}') : null { | ||
| if (key.bytes.length != 16 && key.bytes.length != 24 && key.bytes.length != 32) { | ||
| throw ArgumentError( | ||
| 'AES key must be 16, 24, or 32 bytes (128, 192, or 256 bits). ' | ||
| 'Got ${key.bytes.length} bytes.', | ||
| ); | ||
| } | ||
| if (mode == AESMode.gcm) { | ||
| _cipher = GCMBlockCipher(AESEngine()); | ||
| } else { | ||
|
|
@@ -24,6 +30,13 @@ class AES implements Algorithm { | |
| if (mode != AESMode.ecb && iv == null) { | ||
| throw StateError('IV is required.'); | ||
| } | ||
| if (iv != null) { | ||
| if (mode == AESMode.gcm && iv.bytes.length != 12) { | ||
| throw ArgumentError('GCM mode requires a 12-byte IV (96 bits). Got ${iv.bytes.length} bytes.'); | ||
| } else if (mode != AESMode.ecb && mode != AESMode.gcm && iv.bytes.length != 16) { | ||
| throw ArgumentError('AES ${_modes[mode]} mode requires a 16-byte IV (128 bits). Got ${iv.bytes.length} bytes.'); | ||
| } | ||
| } | ||
|
Comment on lines
+33
to
+39
|
||
|
|
||
| if (_streamCipher != null) { | ||
| _streamCipher | ||
|
|
@@ -49,6 +62,13 @@ class AES implements Algorithm { | |
| if (mode != AESMode.ecb && iv == null) { | ||
| throw StateError('IV is required.'); | ||
| } | ||
| if (iv != null) { | ||
| if (mode == AESMode.gcm && iv.bytes.length != 12) { | ||
| throw ArgumentError('GCM mode requires a 12-byte IV (96 bits). Got ${iv.bytes.length} bytes.'); | ||
| } else if (mode != AESMode.ecb && mode != AESMode.gcm && iv.bytes.length != 16) { | ||
| throw ArgumentError('AES ${_modes[mode]} mode requires a 16-byte IV (128 bits). Got ${iv.bytes.length} bytes.'); | ||
| } | ||
| } | ||
|
|
||
| if (_streamCipher != null) { | ||
| _streamCipher | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ class SecureRandom { | |
|
|
||
| String get base64 => convert.base64.encode(_bytes); | ||
|
|
||
| String get base64Url => convert.base64Url.encode(_bytes); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| String get utf8 => convert.utf8.decode(_bytes); | ||
|
|
||
| int get length => _bytes.length; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -126,6 +126,67 @@ void main() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group('AES GCM', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('encrypt/decrypt round-trip', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypter = Encrypter(AES(key, mode: AESMode.gcm)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final iv = IV.fromLength(12); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypted = encrypter.encrypt(text, iv: iv); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final decrypted = encrypter.decrypt(encrypted, iv: iv); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(decrypted, equals(text)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('encrypt/decrypt with associated data', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypter = Encrypter(AES(key, mode: AESMode.gcm)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final iv = IV.fromLength(12); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final aad = utf8.encode('authenticated-context'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypted = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encrypter.encrypt(text, iv: iv, associatedData: aad); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final decrypted = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encrypter.decrypt(encrypted, iv: iv, associatedData: aad); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(decrypted, equals(text)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('decrypt fails with wrong associated data', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypter = Encrypter(AES(key, mode: AESMode.gcm)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final iv = IV.fromLength(12); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final aad = utf8.encode('correct-context'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final wrongAad = utf8.encode('wrong-context'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypted = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encrypter.encrypt(text, iv: iv, associatedData: aad); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| () => encrypter.decrypt(encrypted, iv: iv, associatedData: wrongAad), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throwsA(anything), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('supports 128, 192, and 256-bit keys', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (final keyLen in [16, 24, 32]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final k = Key.fromLength(keyLen); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypter = Encrypter(AES(k, mode: AESMode.gcm)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final iv = IV.fromLength(12); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypted = encrypter.encrypt(text, iv: iv); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(encrypter.decrypt(encrypted, iv: iv), equals(text)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('different IVs produce different ciphertexts', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final encrypter = Encrypter(AES(key, mode: AESMode.gcm)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final iv1 = IV.fromLength(12); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final iv2 = IV.fromLength(12); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final e1 = encrypter.encrypt(text, iv: iv1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final e2 = encrypter.encrypt(text, iv: iv2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(e1.base64, isNot(equals(e2.base64))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group('AES validation errors', () { | |
| test('throws ArgumentError for invalid GCM key lengths', () { | |
| for (final keyLen in [15, 17, 20]) { | |
| expect( | |
| () => Encrypter(AES(Key.fromLength(keyLen), mode: AESMode.gcm)), | |
| throwsA(isA<ArgumentError>()), | |
| ); | |
| } | |
| }); | |
| test('throws ArgumentError for invalid GCM IV lengths', () { | |
| final encrypter = Encrypter(AES(key, mode: AESMode.gcm)); | |
| for (final ivLen in [11, 13, 16]) { | |
| final iv = IV.fromLength(ivLen); | |
| expect( | |
| () => encrypter.encrypt(text, iv: iv), | |
| throwsA(isA<ArgumentError>()), | |
| ); | |
| } | |
| }); | |
| test('throws ArgumentError for invalid IV lengths in non-GCM mode', () { | |
| final encrypter = Encrypter(AES(key, mode: AESMode.cbc)); | |
| for (final ivLen in [15, 17]) { | |
| final iv = IV.fromLength(ivLen); | |
| expect( | |
| () => encrypter.encrypt(text, iv: iv), | |
| throwsA(isA<ArgumentError>()), | |
| ); | |
| } | |
| }); | |
| }); |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The new validation in
aes.dartrequires a 12-byte IV for GCM mode, but the example code inexample/aes_gcm.dartuses a 16-byte IV, causing a runtimeArgumentError.Severity: HIGH
Suggested Fix
Update the example code in
example/aes_gcm.dartto use a 12-byte IV, for instance, by changingIV.fromSecureRandom(16)toIV.fromSecureRandom(12). This will align the example with the new validation rules. Alternatively, if supporting variable-length IVs for GCM is desired, the validation logic should be relaxed or removed.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.