From d93bca358edd21634a68d454dad90302f4182dfc Mon Sep 17 00:00:00 2001 From: Nguyen Van Nguyen Date: Fri, 13 Sep 2024 14:43:38 +0700 Subject: [PATCH] WIP Signed-off-by: Nguyen Van Nguyen --- lib/src/packet/aead_encrypted_data.dart | 26 ++++----- lib/src/packet/packet_reader.dart | 75 +++++++++++++++---------- 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/lib/src/packet/aead_encrypted_data.dart b/lib/src/packet/aead_encrypted_data.dart index 577e9478..9614f7e0 100644 --- a/lib/src/packet/aead_encrypted_data.dart +++ b/lib/src/packet/aead_encrypted_data.dart @@ -166,21 +166,13 @@ class AeadEncryptedData extends ContainedPacket { final tagLength = forEncryption ? 0 : aead.tagLength; final chunkSize = (1 << (this.chunkSize + 6)) + tagLength; - final zeroBuffer = Uint8List(21); - final adataBuffer = zeroBuffer.sublist(0, 13); - final adataTagBuffer = Uint8List(21); + final adataBuffer = Uint8List(13); - final aaData = _getAAData(); - adataBuffer.setAll(0, aaData); - adataTagBuffer.setAll(0, aaData); - adataTagBuffer.setAll( - 13 + 4, - (dataLength - tagLength * (dataLength / chunkSize).ceil()).pack32(), - ); + adataBuffer.setAll(0, _getAAData()); final List crypted = List.empty(growable: true); for (var chunkIndex = 0; chunkIndex == 0 || data.isNotEmpty;) { - final chunkIndexData = adataTagBuffer.sublist(5, 13); + final chunkIndexData = adataBuffer.sublist(5, 13); final size = chunkSize < data.length ? chunkSize : data.length; crypted.add( forEncryption @@ -198,13 +190,19 @@ class AeadEncryptedData extends ContainedPacket { /// We take a chunk of data, en/decrypt it, and shift `data` to the next chunk. data = data.sublist(size); - adataTagBuffer.setAll(5 + 4, (++chunkIndex).pack32()); + adataBuffer.setAll(9, (++chunkIndex).pack32()); } /// After the final chunk, we either encrypt a final, empty data /// chunk to get the final authentication tag or validate that final /// authentication tag. - final chunkIndexData = adataTagBuffer.sublist(5, 13); + final chunkIndexData = adataBuffer.sublist(5, 13); + final adataTagBuffer = Uint8List(21); + adataTagBuffer.setAll(0, adataBuffer); + adataTagBuffer.setAll( + 17, + (dataLength - tagLength * (dataLength / chunkSize).ceil()).pack32(), + ); crypted.add( forEncryption ? cipher.encrypt( @@ -226,7 +224,7 @@ class AeadEncryptedData extends ContainedPacket { Uint8List _getAAData() { return Uint8List.fromList([ - 0xC0 | tag.value, + 0xc0 | tag.value, version, symmetric.value, aead.value, diff --git a/lib/src/packet/packet_reader.dart b/lib/src/packet/packet_reader.dart index 79d4fa39..0fb0ba5d 100644 --- a/lib/src/packet/packet_reader.dart +++ b/lib/src/packet/packet_reader.dart @@ -2,7 +2,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -import 'dart:typed_data'; +import 'package:pinenacl/api.dart'; import '../crypto/math/byte_ext.dart'; import '../enum/packet_tag.dart'; @@ -19,9 +19,7 @@ class PacketReader { PacketReader(this.tag, this.data, this.offset); factory PacketReader.read(final Uint8List bytes, [final int offset = 0]) { - if (bytes.length <= offset || - bytes.sublist(offset).length < 2 || - (bytes[offset] & 0x80) == 0) { + if (bytes.length <= offset || bytes.sublist(offset).length < 2 || (bytes[offset] & 0x80) == 0) { throw StateError( 'Error during parsing. This data probably does not conform to a valid OpenPGP format.', ); @@ -34,6 +32,7 @@ class PacketReader { final tagByte = oldFormat ? (headerByte & 0x3f) >> 2 : headerByte & 0x3f; final tag = PacketTag.values.firstWhere((tag) => tag.value == tagByte); + final Uint8List packetData; var packetLength = bytes.length - pos; if (oldFormat) { final lengthType = headerByte & 0x03; @@ -42,54 +41,70 @@ class PacketReader { packetLength = bytes[pos++]; break; case 1: - packetLength = (bytes[pos++] << 8) | bytes[pos++]; + packetLength = bytes.sublist(pos, pos + 2).toIn16(); + pos += 2; break; case 2: packetLength = bytes.sublist(pos, pos + 4).toInt32(); pos += 4; break; } + packetData = bytes.sublist(pos, pos + packetLength); } else { - if (bytes[pos] < 192) { - packetLength = bytes[pos++]; - } else if (bytes[pos] > 191 && bytes[pos] < 224) { - packetLength = ((bytes[pos++] - 192) << 8) + (bytes[pos++]) + 192; - } else if (bytes[pos] > 223 && bytes[pos] < 255) { - var partialPos = pos + 1 << (bytes[pos++] & 0x1f); + final length = bytes[pos++]; + if (length < 192) { + packetLength = length; + packetData = bytes.sublist(pos, pos + packetLength); + } else if (length < 224) { + packetLength = ((length - 192) << 8) + (bytes[pos++]) + 192; + packetData = bytes.sublist(pos, pos + packetLength); + } else if (length < 255) { + var partialLength = 1 << (length & 0x1f); + final List partialData = List.empty(growable: true); + partialData.add(bytes.sublist(pos, pos + partialLength)); + var partialPos = pos + partialLength; while (true) { - if (bytes[pos] < 192) { - partialPos += bytes[partialPos++]; + partialLength = bytes[partialPos++]; + if (partialLength < 192) { + partialData.add(bytes.sublist(partialPos, partialPos + partialLength)); + partialPos += partialLength; break; - } else if (bytes[partialPos] > 191 && bytes[partialPos] < 224) { - partialPos += ((bytes[partialPos++] - 192) << 8) + - (bytes[partialPos++]) + - 192; - break; - } else if (bytes[partialPos] > 223 && bytes[partialPos] < 255) { - partialPos += 1 << (bytes[partialPos++] & 0x1f); + } else if (partialLength < 224) { + partialLength = ((partialLength - 192) << 8) + (bytes[partialPos++]) + 192; + partialData.add(bytes.sublist(partialPos, partialPos + partialLength)); + partialPos += partialLength; break; + } else if (partialLength < 255) { + partialLength = 1 << (partialLength & 0x1f); + partialData.add(bytes.sublist(partialPos, partialPos + partialLength)); + partialPos += partialLength; } else { - partialPos++; - partialPos += bytes - .sublist( - partialPos, - partialPos + 4, - ) - .toInt32() + - 4; + partialLength = bytes + .sublist( + partialPos, + partialPos + 4, + ) + .toInt32(); + partialPos += 4; + partialData.add(bytes.sublist(partialPos, partialPos + partialLength)); + partialPos += partialLength; + break; } } + packetData = Uint8List.fromList([ + ...partialData.expand((element) => element), + ]); packetLength = partialPos - pos; } else { - pos++; packetLength = bytes.sublist(pos, pos + 4).toInt32(); pos += 4; + packetData = bytes.sublist(pos, pos + packetLength); } } return PacketReader( tag, - bytes.sublist(pos, pos + packetLength), + packetData, pos + packetLength, ); }