Skip to content

Commit

Permalink
Fix encode SKESK packet to bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Nov 15, 2024
1 parent 432a1be commit 493d552
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/src/packet/sym_encrypted_session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {

final SymmetricAlgorithm symmetric;

final AeadAlgorithm aead;
final AeadAlgorithm? aead;

final S2K s2k;

Expand All @@ -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);

Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -227,7 +225,9 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {
return Uint8List.fromList([
version,
symmetric.value,
...aead != null ? [aead!.value] : [],
...s2k.encode(),
...iv,
...encrypted,
]);
}
Expand Down

0 comments on commit 493d552

Please sign in to comment.