From 493d55252f7ff8504d82c15bd120c1a9c99ad2ec Mon Sep 17 00:00:00 2001 From: Nguyen Van Nguyen Date: Fri, 15 Nov 2024 12:16:18 +0700 Subject: [PATCH] Fix encode SKESK packet to bytes Signed-off-by: Nguyen Van Nguyen --- lib/src/packet/sym_encrypted_session_key.dart | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/src/packet/sym_encrypted_session_key.dart b/lib/src/packet/sym_encrypted_session_key.dart index e240b8f9..94a81626 100644 --- a/lib/src/packet/sym_encrypted_session_key.dart +++ b/lib/src/packet/sym_encrypted_session_key.dart @@ -34,7 +34,7 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { final SymmetricAlgorithm symmetric; - final AeadAlgorithm aead; + final AeadAlgorithm? aead; final S2K s2k; @@ -53,7 +53,7 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { this.iv, this.encrypted, { this.symmetric = SymmetricAlgorithm.aes128, - this.aead = AeadAlgorithm.ocb, + this.aead, this.sessionKey, }) : super(PacketTag.symEncryptedSessionKey); @@ -74,29 +74,27 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { ); pos++; - final AeadAlgorithm aead; + final int ivLength; + final AeadAlgorithm? aead; if (version == 5) { /// A one-octet number describing the aead algorithm used. aead = AeadAlgorithm.values.firstWhere( (algo) => algo.value == bytes[pos], ); + ivLength = aead.ivLength; pos++; } else { - aead = AeadAlgorithm.ocb; + ivLength = 0; + aead = null; } /// A string-to-key (S2K) specifier, length as defined above. final s2k = S2K.fromByteData(bytes.sublist(pos)); pos += s2k.length; - final Uint8List iv; - if (version == 5) { - /// A starting initialization vector of size specified by the AEAD algorithm. - iv = bytes.sublist(pos, pos + aead.ivLength); - pos += aead.ivLength; - } else { - iv = Uint8List(0); - } + /// A starting initialization vector of size specified by the AEAD algorithm. + final iv = bytes.sublist(pos, pos + ivLength); + pos += ivLength; final encrypted = bytes.sublist(pos); return SymEncryptedSessionKeyPacket( @@ -186,9 +184,9 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { 0xC0 | tag.value, version, symmetric.value, - aead.value, + aead!.value, ]); - final cipher = aead.cipherEngine(key, symmetric); + final cipher = aead!.cipherEngine(key, symmetric); final decrypted = cipher.decrypt(encrypted, iv, adata); sessionKey = SessionKey(decrypted, symmetric); } else { @@ -227,7 +225,9 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket { return Uint8List.fromList([ version, symmetric.value, + ...aead != null ? [aead!.value] : [], ...s2k.encode(), + ...iv, ...encrypted, ]); }