Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Dec 11, 2024
1 parent 0f6c902 commit ba8fb76
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
56 changes: 37 additions & 19 deletions lib/src/message/literal_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:typed_data';
import '../common/armor.dart';
import '../common/config.dart';
import '../common/helpers.dart';
import '../enum/aead_algorithm.dart';
import '../enum/armor_type.dart';
import '../enum/compression_algorithm.dart';
import '../enum/preset_rfc.dart';
Expand Down Expand Up @@ -58,26 +59,42 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
/// Checking the algorithm preferences of the passed encryption keys.
static SessionKeyInterface generateSessionKey(
final Iterable<KeyInterface> encryptionKeys, [
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
final SymmetricAlgorithm defaultSymmetric = SymmetricAlgorithm.aes128,
]) {
var desiredSymmetrics = [
SymmetricAlgorithm.aes128,
SymmetricAlgorithm.aes192,
SymmetricAlgorithm.aes256,
];
for (final key in encryptionKeys) {
desiredSymmetrics = desiredSymmetrics
.where(
(symmetric) => key.preferredSymmetrics.contains(symmetric),
)
.toList();
}
final symmetric = desiredSymmetrics.firstOrNull ?? defaultSymmetric;

var desiredAeads = [
AeadAlgorithm.ocb,
AeadAlgorithm.gcm,
AeadAlgorithm.eax,
];
var aeadProtect = Config.aeadProtect;
final aead = Config.preferredAead;
for (final key in encryptionKeys) {
final symmetrics = key.preferredSymmetrics;
if (symmetrics.isNotEmpty && !symmetrics.contains(symmetric)) {
throw AssertionError(
'Symmetric not compatible with the given `encryptionKeys`',
);
}
if (key.aeadSupported) {
final aeads = key.preferredAeads(symmetric);
if (!aeads.contains(aead)) {
aeadProtect = false;
}
desiredAeads = desiredAeads
.where(
(aead) => key.preferredAeads(symmetric).contains(aead),
)
.toList();
} else {
aeadProtect = false;
break;
}
}
final aead = desiredAeads.firstOrNull ?? Config.preferredAead;

return SessionKey.produceKey(
symmetric,
aeadProtect ? aead : null,
Expand Down Expand Up @@ -120,15 +137,16 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
);

@override
compress([final CompressionAlgorithm? algorithm]) {
final algo = algorithm ?? Config.preferredCompression;
if (algo != CompressionAlgorithm.uncompressed) {
compress([
final CompressionAlgorithm algorithm = CompressionAlgorithm.uncompressed,
]) {
if (algorithm != CompressionAlgorithm.uncompressed) {
return LiteralMessage(
PacketList(
[
CompressedDataPacket.fromPacketList(
_unwrapCompressed(),
algorithm: algo,
algorithm: algorithm,
)
],
),
Expand All @@ -141,7 +159,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
encrypt({
final Iterable<KeyInterface> encryptionKeys = const [],
final Iterable<String> passwords = const [],
final SymmetricAlgorithm? symmetric,
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
}) {
if (encryptionKeys.isEmpty && passwords.isEmpty) {
throw ArgumentError(
Expand All @@ -156,7 +174,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
}
final sessionKey = generateSessionKey(
encryptionKeys,
symmetric ?? Config.preferredSymmetric,
symmetric,
);

final packetList = addPadding
Expand All @@ -180,7 +198,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
SymEncryptedIntegrityProtectedDataPacket.encryptPackets(
sessionKey.encryptionKey,
packetList,
symmetric: symmetric ?? Config.preferredSymmetric,
symmetric: symmetric,
aead: sessionKey.aead,
),
]));
Expand Down
16 changes: 12 additions & 4 deletions lib/src/openpgp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,14 @@ final class OpenPGP {
final DateTime? time,
}) {
return signingKeys.isEmpty
? message.compress(compression).encrypt(
? message
.compress(
compression ?? Config.preferredCompression,
)
.encrypt(
encryptionKeys: encryptionKeys,
passwords: passwords,
symmetric: symmetric,
symmetric: symmetric ?? Config.preferredSymmetric,
)
: message
.sign(
Expand All @@ -281,11 +285,13 @@ final class OpenPGP {
notationData: notationData,
time: time,
)
.compress(compression)
.compress(
compression ?? Config.preferredCompression,
)
.encrypt(
encryptionKeys: encryptionKeys,
passwords: passwords,
symmetric: symmetric,
symmetric: symmetric ?? Config.preferredSymmetric,
);
}

Expand All @@ -308,6 +314,7 @@ final class OpenPGP {
passwords: passwords,
signingKeys: signingKeys,
symmetric: symmetric,
compression: compression,
notationData: notationData,
time: time,
);
Expand All @@ -332,6 +339,7 @@ final class OpenPGP {
passwords: passwords,
signingKeys: signingKeys,
symmetric: symmetric,
compression: compression,
notationData: notationData,
time: time,
);
Expand Down
7 changes: 0 additions & 7 deletions lib/src/packet/signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {
PreferredSymmetricAlgorithms(Uint8List.fromList([
SymmetricAlgorithm.aes128.value,
SymmetricAlgorithm.aes256.value,
SymmetricAlgorithm.aes192.value,
])),
PreferredAeadAlgorithms(Uint8List.fromList([
AeadAlgorithm.ocb.value,
Expand Down Expand Up @@ -531,12 +530,6 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {
SymmetricAlgorithm.aes256.value,
AeadAlgorithm.gcm.value,
],
...[
SymmetricAlgorithm.aes192.value,
AeadAlgorithm.ocb.value,
SymmetricAlgorithm.aes192.value,
AeadAlgorithm.gcm.value,
],
])));
}
return subpackets;
Expand Down
6 changes: 4 additions & 2 deletions lib/src/type/literal_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ abstract interface class LiteralMessageInterface implements ArmorableInterface,
EncryptedMessageInterface encrypt({
final Iterable<KeyInterface> encryptionKeys = const [],
final Iterable<String> passwords = const [],
final SymmetricAlgorithm? symmetric,
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
});

/// Compress the message (the literal and signature packets of the message)
/// Return new message with compressed content.
LiteralMessageInterface compress([final CompressionAlgorithm? algorithm]);
LiteralMessageInterface compress([
final CompressionAlgorithm algorithm = CompressionAlgorithm.uncompressed,
]);
}

0 comments on commit ba8fb76

Please sign in to comment.