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 Nov 21, 2024
1 parent 6bd8d0f commit e207943
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/src/crypto/math/byte_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension Uint8ListExt on Uint8List {

String toHexadecimal() {
final result = StringBuffer();
for (var i = 0; i < lengthInBytes; i++) {
for (var i = 0; i < length; i++) {
final part = this[i];
result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/crypto/symmetric/blowfish.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ class BlowfishEngine extends BaseCipher {
if (_workingKey.isEmpty) {
throw StateError('$algorithmName not initialised');
}
if ((inOff + _blockSize) > input.lengthInBytes) {
if ((inOff + _blockSize) > input.length) {
throw ArgumentError('input buffer too short for $algorithmName engine');
}
if ((outOff + _blockSize) > output.lengthInBytes) {
if ((outOff + _blockSize) > output.length) {
throw ArgumentError('output buffer too short for $algorithmName engine');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/crypto/symmetric/cast5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,10 @@ class CAST5Engine extends BaseCipher {
if (_workingKey.isEmpty) {
throw StateError('$algorithmName not initialised');
}
if ((inOff + _blockSize) > input.lengthInBytes) {
if ((inOff + _blockSize) > input.length) {
throw ArgumentError('input buffer too short for $algorithmName engine');
}
if ((outOff + _blockSize) > output.lengthInBytes) {
if ((outOff + _blockSize) > output.length) {
throw ArgumentError('output buffer too short for $algorithmName engine');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/crypto/symmetric/idea.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class IDEAEngine extends BaseCipher {
if (_workingKey.isEmpty) {
throw StateError('$algorithmName not initialised');
}
if ((inOff + _blockSize) > input.lengthInBytes) {
if ((inOff + _blockSize) > input.length) {
throw ArgumentError('input buffer too short for $algorithmName engine');
}
if ((outOff + _blockSize) > output.lengthInBytes) {
if ((outOff + _blockSize) > output.length) {
throw ArgumentError('output buffer too short for $algorithmName engine');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/crypto/symmetric/twofish.dart
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,10 @@ class TwofishEngine extends BaseCipher {
if (_workingKey.isEmpty) {
throw StateError('algorithmName not initialised');
}
if ((inOff + _blockSize) > input.lengthInBytes) {
if ((inOff + _blockSize) > input.length) {
throw ArgumentError('input buffer too short for algorithmName engine');
}
if ((outOff + _blockSize) > output.lengthInBytes) {
if ((outOff + _blockSize) > output.length) {
throw ArgumentError('output buffer too short for algorithmName engine');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/packet/key/ecdh_session_key_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ECDHSessionKeyParams extends SessionKeyParams {
Uint8List encode() => Uint8List.fromList([
...ephemeralKey.bitLength.pack16(),
...ephemeralKey.toUnsignedBytes(),
wrappedKey.lengthInBytes,
wrappedKey.length,
...wrappedKey,
]);

Expand Down Expand Up @@ -220,7 +220,7 @@ class ECDHSessionKeyParams extends SessionKeyParams {

/// Add pkcs5 padding to a message
static Uint8List _pkcs5Encode(final Uint8List message) {
final c = 8 - (message.lengthInBytes % 8);
final c = 8 - (message.length % 8);
return Uint8List.fromList(
List.filled(message.length + c, c),
)..setAll(0, message);
Expand Down
16 changes: 8 additions & 8 deletions lib/src/packet/key/key_wrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ abstract class KeyWrap {
final Uint8List kek,
final Uint8List key,
) async {
if (kek.lengthInBytes != keySize) {
if (kek.length != keySize) {
throw ArgumentError('Key encryption key size must be $keySize bytes.');
}
if (key.lengthInBytes < 16) {
if (key.length < 16) {
throw ArgumentError('Key length must be at least 16 octets.');
}
if (key.lengthInBytes % 8 != 0) {
if (key.length % 8 != 0) {
throw ArgumentError('Key length must be a multiple of 64 bits.');
}

cipher.init(true, KeyParameter(kek));
final a = Uint8List.fromList(_iv);
final r = Uint8List.fromList(key);
final n = key.lengthInBytes ~/ 8;
final n = key.length ~/ 8;
for (var j = 0; j <= 5; j++) {
for (var i = 1; i <= n; i++) {
final buffer = Uint8List.fromList([
Expand All @@ -63,20 +63,20 @@ abstract class KeyWrap {
final Uint8List kek,
final Uint8List wrappedKey,
) async {
if (kek.lengthInBytes != keySize) {
if (kek.length != keySize) {
throw ArgumentError('Key encryption key size must be $keySize bytes.');
}
if (wrappedKey.lengthInBytes < 16) {
if (wrappedKey.length < 16) {
throw ArgumentError('Wrapped key length must be at least 16 octets.');
}
if (wrappedKey.lengthInBytes % 8 != 0) {
if (wrappedKey.length % 8 != 0) {
throw ArgumentError('Wrapped key length must be a multiple of 64 bits.');
}

cipher.init(false, KeyParameter(kek));
final a = wrappedKey.sublist(0, 8);
final r = wrappedKey.sublist(8);
final n = (wrappedKey.lengthInBytes ~/ 8) - 1;
final n = (wrappedKey.length ~/ 8) - 1;
for (var j = 5; j >= 0; j--) {
for (var i = n; i >= 1; i--) {
a[7] ^= (n * j + i) & 0xff;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/key/rsa_secret_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class RSASecretParams extends KeyParams {
);
final signature = signer.generateSignature(message) as RSASignature;
return Uint8List.fromList([
...(signature.bytes.lengthInBytes * 8).pack16(),
...(signature.bytes.length * 8).pack16(),
...signature.bytes,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/key/session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SessionKey {
/// Compute checksum
Uint8List computeChecksum() {
var sum = 0;
for (var i = 0; i < key.lengthInBytes; i++) {
for (var i = 0; i < key.length; i++) {
sum = (sum + key[i]) & 0xffff;
}
return sum.pack16();
Expand Down
7 changes: 4 additions & 3 deletions lib/src/packet/key/session_key_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ abstract class SessionKeyParams {
final Uint8List input,
) {
final numBlocks = input.length ~/ engine.inputBlockSize +
((input.lengthInBytes % engine.inputBlockSize != 0) ? 1 : 0);
((input.length % engine.inputBlockSize != 0) ? 1 : 0);

final output = Uint8List(numBlocks * engine.outputBlockSize);

var inpOff = 0;
var outOff = 0;
while (inpOff < input.length) {
final chunkSize = (inpOff + engine.inputBlockSize <= input.lengthInBytes)
final chunkSize =
(inpOff + engine.inputBlockSize <= input.length)
? engine.inputBlockSize
: input.lengthInBytes - inpOff;
: input.length - inpOff;

outOff += engine.processBlock(
input,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/public_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class PublicKeyPacket extends ContainedPacket implements KeyPacket {
final bytes = toByteData();
return Uint8List.fromList([
0x99,
...bytes.lengthInBytes.pack16(),
...bytes.length.pack16(),
...bytes,
]);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/packet/signature_packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class SignaturePacket extends ContainedPacket {
...signatureData,
..._calculateTrailer(
version,
signatureData.lengthInBytes,
signatureData.length,
)
]);
return SignaturePacket(
Expand Down Expand Up @@ -504,7 +504,7 @@ class SignaturePacket extends ContainedPacket {
...signatureData,
..._calculateTrailer(
version,
signatureData.lengthInBytes,
signatureData.length,
)
]);
final hash = Helper.hashDigest(message, hashAlgorithm);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/user_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class UserAttributePacket extends ContainedPacket {
final bytes = toByteData();
return Uint8List.fromList([
0xd1,
...bytes.lengthInBytes.pack32(),
...bytes.length.pack32(),
...bytes,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/user_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class UserIDPacket extends ContainedPacket {
final bytes = toByteData();
return Uint8List.fromList([
0xb4,
...bytes.lengthInBytes.pack32(),
...bytes.length.pack32(),
...bytes,
]);
}
Expand Down

0 comments on commit e207943

Please sign in to comment.