diff --git a/lib/src/helpers.dart b/lib/src/helpers.dart index e720cbe6..bc6af757 100644 --- a/lib/src/helpers.dart +++ b/lib/src/helpers.dart @@ -7,6 +7,7 @@ import 'dart:math'; import 'dart:typed_data'; import 'package:pointycastle/api.dart' as pc; +import 'package:pointycastle/api.dart'; import 'crypto/math/byte_ext.dart'; import 'crypto/math/int_ext.dart'; @@ -104,4 +105,13 @@ class Helper { } while (k.compareTo(min) <= 0 || k.compareTo(max) >= 0); return k; } + + static Uint8List pad(Uint8List bytes, int blockSize) { + final padLength = blockSize - (bytes.length % blockSize); + + final padded = Uint8List(bytes.length + padLength)..setAll(0, bytes); + Padding('PKCS7').addPadding(padded, bytes.length); + + return padded; + } } diff --git a/lib/src/packet/secret_key.dart b/lib/src/packet/secret_key.dart index 0fe248e6..7d9ef173 100644 --- a/lib/src/packet/secret_key.dart +++ b/lib/src/packet/secret_key.dart @@ -227,10 +227,15 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket { ); final clearText = secretParams!.encode(); - final cipherText = cipher.process(Uint8List.fromList([ - ...clearText, - ...Helper.hashDigest(clearText, HashAlgorithm.sha1), - ])); + final cipherText = cipher.process( + Helper.pad( + Uint8List.fromList([ + ...clearText, + ...Helper.hashDigest(clearText, HashAlgorithm.sha1), + ]), + symmetric.blockSize, + ), + ); return SecretKeyPacket( publicKey, @@ -255,10 +260,8 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket { symmetric.keySizeInByte, ) ?? Uint8List(symmetric.keySizeInByte); - final blockSize = symmetric.blockSize; - final padding = Padding('PKCS7'); final cipher = PaddedBlockCipherImpl( - padding, + Padding('PKCS7'), symmetric.cfbCipherEngine, ); cipher.init( @@ -266,17 +269,15 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket { PaddedBlockCipherParameters( ParametersWithIV( KeyParameter(key), - iv ?? Uint8List(blockSize), + iv ?? Uint8List(symmetric.blockSize), ), null, ), ); - final padLength = blockSize - (keyData.length % blockSize); - final padded = Uint8List(keyData.length + padLength)..setAll(0, keyData); - padding.addPadding(padded, keyData.length); - - final clearTextWithHash = cipher.process(padded); + final clearTextWithHash = cipher.process( + Helper.pad(keyData, symmetric.blockSize), + ); clearText = clearTextWithHash.sublist( 0, clearTextWithHash.length - HashAlgorithm.sha1.digestSize, diff --git a/lib/src/packet/sym_encrypted_data.dart b/lib/src/packet/sym_encrypted_data.dart index ed6c3b18..7f6984a1 100644 --- a/lib/src/packet/sym_encrypted_data.dart +++ b/lib/src/packet/sym_encrypted_data.dart @@ -65,7 +65,12 @@ class SymEncryptedDataPacket extends ContainedPacket { return SymEncryptedDataPacket( Uint8List.fromList([ ...prefix, - ...cipher.process(packets.encode()), + ...cipher.process( + Helper.pad( + packets.encode(), + symmetric.blockSize, + ), + ), ]), packets: packets, ); @@ -101,9 +106,8 @@ class SymEncryptedDataPacket extends ContainedPacket { throw StateError('Message is not authenticated.'); } final blockSize = symmetric.blockSize; - final padding = Padding('PKCS7'); final cipher = PaddedBlockCipherImpl( - padding, + Padding('PKCS7'), symmetric.cfbCipherEngine, ); cipher.init( @@ -117,15 +121,15 @@ class SymEncryptedDataPacket extends ContainedPacket { ), ); - final data = encrypted.sublist(blockSize + 2); - final padLength = blockSize - (data.length % blockSize); - final padded = Uint8List(data.length + padLength)..setAll(0, data); - padding.addPadding(padded, data.length); - return SymEncryptedDataPacket( encrypted, packets: PacketList.packetDecode( - cipher.process(padded), + cipher.process( + Helper.pad( + encrypted.sublist(blockSize + 2), + blockSize, + ), + ), ), ); } diff --git a/lib/src/packet/sym_encrypted_integrity_protected_data.dart b/lib/src/packet/sym_encrypted_integrity_protected_data.dart index acb80fe6..251561bd 100644 --- a/lib/src/packet/sym_encrypted_integrity_protected_data.dart +++ b/lib/src/packet/sym_encrypted_integrity_protected_data.dart @@ -80,7 +80,7 @@ class SymEncryptedIntegrityProtectedDataPacket extends ContainedPacket { ); return SymEncryptedIntegrityProtectedDataPacket( - cipher.process(plainText), + cipher.process(Helper.pad(plainText, symmetric.blockSize)), packets: packets, ); } @@ -110,10 +110,8 @@ class SymEncryptedIntegrityProtectedDataPacket extends ContainedPacket { final Uint8List key, { final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128, }) async { - final blockSize = symmetric.blockSize; - final padding = Padding('PKCS7'); final cipher = PaddedBlockCipherImpl( - padding, + Padding('PKCS7'), symmetric.cfbCipherEngine, ); cipher.init( @@ -121,17 +119,15 @@ class SymEncryptedIntegrityProtectedDataPacket extends ContainedPacket { PaddedBlockCipherParameters( ParametersWithIV( KeyParameter(key), - Uint8List(blockSize), + Uint8List(symmetric.blockSize), ), null, ), ); - final padLength = blockSize - (encrypted.length % blockSize); - final padded = Uint8List(encrypted.length + padLength)..setAll(0, encrypted); - padding.addPadding(padded, encrypted.length); - - final decrypted = cipher.process(padded); + final decrypted = cipher.process( + Helper.pad(encrypted, symmetric.blockSize), + ); final realHash = decrypted.sublist( decrypted.length - HashAlgorithm.sha1.digestSize, ); diff --git a/lib/src/packet/sym_encrypted_session_key.dart b/lib/src/packet/sym_encrypted_session_key.dart index 43d2af2b..a8e814e6 100644 --- a/lib/src/packet/sym_encrypted_session_key.dart +++ b/lib/src/packet/sym_encrypted_session_key.dart @@ -156,7 +156,9 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { ), ); iv = Uint8List(0); - encrypted = cipher.process(sessionKey.encode()); + encrypted = cipher.process( + Helper.pad(sessionKey.encode(), symmetric.blockSize), + ); } } else { iv = Uint8List(0); @@ -196,10 +198,8 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { final decrypted = cipher.decrypt(encrypted, iv, adata); sessionKey = SessionKey(decrypted, symmetric); } else { - final blockSize = symmetric.blockSize; - final padding = Padding('PKCS7'); final cipher = PaddedBlockCipherImpl( - padding, + Padding('PKCS7'), symmetric.cfbCipherEngine, ); cipher.init( @@ -207,17 +207,15 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { PaddedBlockCipherParameters( ParametersWithIV( KeyParameter(key), - Uint8List(blockSize), + Uint8List(symmetric.blockSize), ), null, ), ); - final padLength = blockSize - (encrypted.length % blockSize); - final padded = Uint8List(encrypted.length + padLength)..setAll(0, encrypted); - padding.addPadding(padded, encrypted.length); - - final decrypted = cipher.process(padded); + final decrypted = cipher.process( + Helper.pad(encrypted, symmetric.blockSize), + ); final sessionKeySymmetric = SymmetricAlgorithm.values.firstWhere( (algo) => algo.value == decrypted[0], );