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 May 11, 2023
1 parent 5c331eb commit f78eb27
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 49 deletions.
5 changes: 5 additions & 0 deletions lib/src/packet/key/session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:dart_pg/src/crypto/math/int_ext.dart';

import '../../crypto/math/byte_ext.dart';
import '../../enum/symmetric_algorithm.dart';
import '../../helpers.dart';

class SessionKey {
/// Algorithm to encrypt the message with
Expand All @@ -18,6 +19,10 @@ class SessionKey {

SessionKey(this.key, [this.symmetric = SymmetricAlgorithm.aes256]);

factory SessionKey.produceKey([SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes256]) {
return SessionKey(Helper.generateEncryptionKey(symmetric), symmetric);
}

/// Serializes session key to bytes
Uint8List encode() => Uint8List.fromList([symmetric.value, ...key]);

Expand Down
12 changes: 3 additions & 9 deletions lib/src/packet/public_key_encrypted_session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:typed_data';
import '../crypto/asymmetric/elgamal.dart';
import '../enum/key_algorithm.dart';
import '../enum/packet_tag.dart';
import '../enum/symmetric_algorithm.dart';
import '../helpers.dart';
import 'contained_packet.dart';
import 'key/key_id.dart';
Expand Down Expand Up @@ -93,14 +92,9 @@ class PublicKeyEncryptedSessionKeyPacket extends ContainedPacket {
}

static Future<PublicKeyEncryptedSessionKeyPacket> encryptSessionKey(
final PublicKeyPacket publicKey, {
final Uint8List? sessionKeyData,
final SymmetricAlgorithm sessionKeySymmetric = SymmetricAlgorithm.aes256,
}) async {
final sessionKey = SessionKey(
sessionKeyData ?? Helper.generateEncryptionKey(sessionKeySymmetric),
sessionKeySymmetric,
);
final PublicKeyPacket publicKey,
final SessionKey sessionKey,
) async {
final SessionKeyParams params;
final keyParams = publicKey.publicParams;
if (keyParams is RSAPublicParams) {
Expand Down
19 changes: 7 additions & 12 deletions lib/src/packet/sym_encrypted_session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {
}

static Future<SymEncryptedSessionKeyPacket> encryptSessionKey(
final String password, {
final Uint8List? sessionKeyData,
final SymmetricAlgorithm sessionKeySymmetric = SymmetricAlgorithm.aes256,
final SymmetricAlgorithm encryptionKeySymmetric = SymmetricAlgorithm.aes256,
final String password,
final SessionKey sessionKey, {
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes256,
final HashAlgorithm hash = HashAlgorithm.sha256,
final S2kType type = S2kType.iterated,
}) async {
Expand All @@ -91,26 +90,22 @@ class SymEncryptedSessionKeyPacket extends ContainedPacket {
);
final key = await s2k.produceKey(
password,
encryptionKeySymmetric.keySizeInByte,
symmetric.keySizeInByte,
);
final cipher = BufferedCipher(
encryptionKeySymmetric.cipherEngine,
symmetric.cipherEngine,
)..init(
true,
ParametersWithIV(
KeyParameter(key),
Uint8List(encryptionKeySymmetric.blockSize),
Uint8List(symmetric.blockSize),
),
);
final sessionKey = SessionKey(
sessionKeyData ?? Helper.generateEncryptionKey(sessionKeySymmetric),
sessionKeySymmetric,
);

return SymEncryptedSessionKeyPacket(
s2k,
cipher.process(sessionKey.encode()),
symmetric: encryptionKeySymmetric,
symmetric: symmetric,
sessionKey: sessionKey,
);
}
Expand Down
35 changes: 12 additions & 23 deletions lib/src/type/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import '../enum/literal_format.dart';
import '../enum/packet_tag.dart';
import '../enum/signature_type.dart';
import '../enum/symmetric_algorithm.dart';
import '../helpers.dart';
import '../packet/compressed_data.dart';
import '../packet/key/key_id.dart';
import '../packet/key/session_key.dart';
Expand All @@ -38,8 +37,7 @@ class Message {

final List<Verification> verifications;

Message(this.packetList,
[final Iterable<Verification> verifications = const []])
Message(this.packetList, [final Iterable<Verification> verifications = const []])
: verifications = verifications.toList(growable: false);

factory Message.fromArmored(final String armored) {
Expand Down Expand Up @@ -89,8 +87,7 @@ class Message {
]));

LiteralDataPacket? get literalData {
final packets =
unwrapCompressed().packetList.whereType<LiteralDataPacket>();
final packets = unwrapCompressed().packetList.whereType<LiteralDataPacket>();
return packets.isNotEmpty ? packets.elementAt(0) : null;
}

Expand All @@ -107,13 +104,10 @@ class Message {
}

/// Gets the key IDs of the keys to which the session key is encrypted
Iterable<KeyID> get encryptionKeyIDs => unwrapCompressed()
.packetList
.whereType<PublicKeyEncryptedSessionKeyPacket>()
.map((packet) => packet.publicKeyID);
Iterable<KeyID> get encryptionKeyIDs =>
unwrapCompressed().packetList.whereType<PublicKeyEncryptedSessionKeyPacket>().map((packet) => packet.publicKeyID);

Iterable<SignaturePacket> get signaturePackets =>
unwrapCompressed().packetList.whereType<SignaturePacket>();
Iterable<SignaturePacket> get signaturePackets => unwrapCompressed().packetList.whereType<SignaturePacket>();

/// Returns ASCII armored text of message
String armor() => Armor.encode(ArmorType.message, packetList.encode());
Expand Down Expand Up @@ -226,8 +220,7 @@ class Message {
final List<PublicKey> verificationKeys, {
final DateTime? date,
}) async {
final literalDataPackets =
unwrapCompressed().packetList.whereType<LiteralDataPacket>();
final literalDataPackets = unwrapCompressed().packetList.whereType<LiteralDataPacket>();
if (literalDataPackets.isEmpty) {
throw StateError('No literal data packet to verify.');
}
Expand All @@ -253,29 +246,26 @@ class Message {
if (encryptionKeys.isEmpty && passwords.isEmpty) {
throw ArgumentError('No encryption keys or passwords provided');
}
final sessionKeyData = Helper.generateEncryptionKey(sessionKeySymmetric);
final sessionKey = SessionKey.produceKey(sessionKeySymmetric);

final pkeskPackets = await Future.wait(
encryptionKeys.map(
(key) async => PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(
await key.getEncryptionKeyPacket(),
sessionKeyData: sessionKeyData,
sessionKeySymmetric: sessionKeySymmetric,
await key.getEncryptionKeyPacket(), sessionKey
),
),
);
final skeskPackets = await Future.wait(
passwords.map(
(password) => SymEncryptedSessionKeyPacket.encryptSessionKey(
password,
sessionKeyData: sessionKeyData,
sessionKeySymmetric: sessionKeySymmetric,
encryptionKeySymmetric: encryptionKeySymmetric,
sessionKey,
symmetric: encryptionKeySymmetric,
),
),
);
final seip = await SymEncryptedIntegrityProtectedDataPacket.encryptPackets(
sessionKeyData,
sessionKey.key,
packetList,
symmetric: sessionKeySymmetric,
);
Expand Down Expand Up @@ -376,8 +366,7 @@ class Message {
}) async {
final sessionKeys = <SessionKey>[];
if (decryptionKeys.isNotEmpty) {
final pkeskPackets =
packetList.whereType<PublicKeyEncryptedSessionKeyPacket>();
final pkeskPackets = packetList.whereType<PublicKeyEncryptedSessionKeyPacket>();
for (final pkesk in pkeskPackets) {
for (final key in decryptionKeys) {
final keyPacket = await key.getDecryptionKeyPacket();
Expand Down
13 changes: 8 additions & 5 deletions test/packet/encryption_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:dart_pg/src/helpers.dart';
import 'package:dart_pg/src/packet/key/session_key.dart';
import 'package:dart_pg/src/packet/key_packet.dart';
import 'package:dart_pg/src/packet/literal_data.dart';
import 'package:dart_pg/src/packet/packet_list.dart';
Expand Down Expand Up @@ -42,7 +43,7 @@ void main() {
});

test('password protected session key test', () async {
final skesk = await SymEncryptedSessionKeyPacket.encryptSessionKey(kek);
final skesk = await SymEncryptedSessionKeyPacket.encryptSessionKey(kek, SessionKey.produceKey());
final seip = await SymEncryptedIntegrityProtectedDataPacket.encryptPackets(
skesk.sessionKey!.key,
packets,
Expand Down Expand Up @@ -74,7 +75,8 @@ void main() {
base64.decode(rsaSecretKeyPacket.replaceAll(RegExp(r'\r?\n', multiLine: true), '')),
).decrypt(passphrase);

final pkesk = await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(secretKey.publicKey);
final pkesk =
await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(secretKey.publicKey, SessionKey.produceKey());
final seip = await SymEncryptedIntegrityProtectedDataPacket.encryptPackets(
pkesk.sessionKey!.key,
packets,
Expand Down Expand Up @@ -102,7 +104,8 @@ void main() {
base64.decode(elgamalSecretKeyPacket.replaceAll(RegExp(r'\r?\n', multiLine: true), '')),
).decrypt(passphrase);

final pkesk = await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(secretKey.publicKey);
final pkesk =
await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(secretKey.publicKey, SessionKey.produceKey());
final seip = await SymEncryptedIntegrityProtectedDataPacket.encryptPackets(
pkesk.sessionKey!.key,
packets,
Expand Down Expand Up @@ -133,7 +136,7 @@ void main() {
base64.decode(ecdhPublicSubkeyPacket.replaceAll(RegExp(r'\r?\n', multiLine: true), '')),
);

final pkesk = await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(publicKey);
final pkesk = await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(publicKey, SessionKey.produceKey());
final seip = await SymEncryptedIntegrityProtectedDataPacket.encryptPackets(
pkesk.sessionKey!.key,
packets,
Expand Down Expand Up @@ -164,7 +167,7 @@ void main() {
base64.decode(curve25519PublicSubkeyPacket.replaceAll(RegExp(r'\r?\n', multiLine: true), '')),
);

final pkesk = await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(publicKey);
final pkesk = await PublicKeyEncryptedSessionKeyPacket.encryptSessionKey(publicKey, SessionKey.produceKey());
final seip = await SymEncryptedIntegrityProtectedDataPacket.encryptPackets(
pkesk.sessionKey!.key,
packets,
Expand Down

0 comments on commit f78eb27

Please sign in to comment.