diff --git a/lib/src/enum/symmetric_algorithm.dart b/lib/src/enum/symmetric_algorithm.dart index 15d0c717..f19388ca 100644 --- a/lib/src/enum/symmetric_algorithm.dart +++ b/lib/src/enum/symmetric_algorithm.dart @@ -30,6 +30,17 @@ enum SymmetricAlgorithm { const SymmetricAlgorithm(this.value); + static List get preferredSymmetrics => [ + aes128, + aes192, + aes256, + camellia128, + camellia192, + camellia256, + blowfish, + twofish, + ]; + int get keySize => switch (this) { plaintext => 0, idea || cast5 || blowfish || aes128 || camellia128 => 128, diff --git a/lib/src/message/literal_message.dart b/lib/src/message/literal_message.dart index 2d738692..0c9a0994 100644 --- a/lib/src/message/literal_message.dart +++ b/lib/src/message/literal_message.dart @@ -60,21 +60,17 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac final Iterable encryptionKeys, [ final SymmetricAlgorithm defaultSymmetric = SymmetricAlgorithm.aes128, ]) { - var desiredSymmetrics = [ - SymmetricAlgorithm.aes128, - SymmetricAlgorithm.aes192, - SymmetricAlgorithm.aes256, - ]; + var preferredSymmetrics = SymmetricAlgorithm.preferredSymmetrics; for (final key in encryptionKeys) { - desiredSymmetrics = desiredSymmetrics + preferredSymmetrics = preferredSymmetrics .where( (symmetric) => key.preferredSymmetrics.contains(symmetric), ) .toList(); } - final symmetric = desiredSymmetrics.firstOrNull ?? defaultSymmetric; + final symmetric = preferredSymmetrics.firstOrNull ?? defaultSymmetric; - var desiredAeads = [ + var preferredAeads = [ AeadAlgorithm.ocb, AeadAlgorithm.gcm, AeadAlgorithm.eax, @@ -82,7 +78,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac var aeadProtect = Config.aeadProtect; for (final key in encryptionKeys) { if (key.aeadSupported) { - desiredAeads = desiredAeads + preferredAeads = preferredAeads .where( (aead) => key.preferredAeads(symmetric).contains(aead), ) @@ -92,7 +88,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac break; } } - final aead = desiredAeads.firstOrNull ?? Config.preferredAead; + final aead = preferredAeads.firstOrNull ?? Config.preferredAead; return SessionKey.produceKey( symmetric,