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 10, 2024
1 parent 49e8fad commit 0f6c902
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
14 changes: 14 additions & 0 deletions lib/src/key/base_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ abstract class BaseKey implements KeyInterface {
return [];
}

@override
preferredAeads([
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
]) {
for (final signature in directSignatures) {
final subpacket = signature.getSubpacket<PreferredAeadCiphers>();
final aeads = subpacket?.preferredAeads(symmetric) ?? [];
if (aeads.isNotEmpty) {
return aeads;
}
}
return [];
}

@override
isPreferredAeadCiphers([
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
Expand Down
19 changes: 10 additions & 9 deletions lib/src/message/literal_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
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) {
if (!key.isPreferredAeadCiphers(symmetric, aead)) {
throw AssertionError(
'Aead ciphers not compatible with the given `encryptionKeys`',
);
final aeads = key.preferredAeads(symmetric);
if (!aeads.contains(aead)) {
aeadProtect = false;
}
} else {
if (key.preferredSymmetrics.isNotEmpty && !key.preferredSymmetrics.contains(symmetric)) {
throw AssertionError(
'Symmetric not compatible with the given `encryptionKeys`',
);
}
aeadProtect = false;
}
}
Expand Down Expand Up @@ -203,6 +203,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
).packets,
];
var index = 0;

/// innermost OPS refers to the first signature packet
final opsPackets = signaturePackets
.map((packet) {
Expand Down
26 changes: 26 additions & 0 deletions lib/src/packet/signature/preferred_aead_ciphers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ class PreferredAeadCiphers extends SignatureSubpacket {
data,
);

List<AeadAlgorithm> preferredAeads(
final SymmetricAlgorithm symmetric,
) {
final aeads = <AeadAlgorithm>[];
if (data.isNotEmpty) {
const chunkSize = 2;
var data = this.data;
while (data.isNotEmpty) {
final size = chunkSize < data.length ? chunkSize : data.length;
final ciphers = data.sublist(0, size);
if (ciphers.elementAtOrNull(1) != null) {
final preferredSymmetric = SymmetricAlgorithm.values.firstWhere(
(alg) => alg.value == ciphers.elementAt(0),
);
if (symmetric == preferredSymmetric) {
aeads.add(AeadAlgorithm.values.firstWhere(
(alg) => alg.value == ciphers.elementAt(1),
));
}
}
data = data.sublist(size);
}
}
return aeads;
}

bool isPreferred(
final SymmetricAlgorithm symmetric,
final AeadAlgorithm aead,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/type/key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ abstract interface class KeyInterface implements ArmorableInterface, PacketConta
/// Get subkeys
List<SubkeyInterface> get subkeys;

/// Return preferred aead algorithms
List<AeadAlgorithm> preferredAeads([
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
]);

/// Return aead ciphers is preferred
bool isPreferredAeadCiphers([
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
Expand Down

0 comments on commit 0f6c902

Please sign in to comment.