Skip to content

Commit

Permalink
Merge branch '1.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyennv committed Sep 13, 2024
2 parents d7d6b54 + d93bca3 commit ab7a45c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
26 changes: 12 additions & 14 deletions lib/src/packet/aead_encrypted_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8List> 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
Expand All @@ -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(
Expand All @@ -226,7 +224,7 @@ class AeadEncryptedData extends ContainedPacket {

Uint8List _getAAData() {
return Uint8List.fromList([
0xC0 | tag.value,
0xc0 | tag.value,
version,
symmetric.value,
aead.value,
Expand Down
75 changes: 45 additions & 30 deletions lib/src/packet/packet_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.',
);
Expand All @@ -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;
Expand All @@ -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<Uint8List> 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,
);
}
Expand Down

0 comments on commit ab7a45c

Please sign in to comment.