Skip to content

Commit 9c54a6f

Browse files
committed
Refactor bitwise operations for clarity in cipher algorithms and tests
- Updated bitwise checks in AEADCipher, AES decryption modes, and AES cache generation to use parentheses for improved readability. - Enhanced tests for ChaCha20, Salsa20, XChaCha20, and XSalsa20 to validate output consistency with random nonces. - Added handling for non-list iterables in the toUint8List function, including support for Set and Queue types.
1 parent 4630122 commit 9c54a6f

14 files changed

Lines changed: 55 additions & 21 deletions

lib/src/algorithms/aead_cipher.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AEADCipher<C extends Cipher, M extends MACHashBase> implements Cipher {
6767

6868
if (aad != null) {
6969
sink.add(aad);
70-
if (aad.length & 15 != 0) {
70+
if ((aad.length & 15) != 0) {
7171
sink.add(Uint8List(16 - (aad.length & 15))); // pad with zero
7272
}
7373
}
@@ -82,7 +82,7 @@ class AEADCipher<C extends Cipher, M extends MACHashBase> implements Cipher {
8282
final sink = _createSink(aad);
8383

8484
sink.add(data);
85-
if (dataLength & 15 != 0) {
85+
if ((dataLength & 15) != 0) {
8686
sink.add(Uint8List(16 - (dataLength & 15))); // pad with zero
8787
}
8888

@@ -155,7 +155,7 @@ class AEADStreamCipher<C extends StreamCipher, M extends MACHashBase>
155155

156156
@override
157157
StreamTransformer<RS, RT> cast<RS, RT>() {
158-
throw UnsupportedError('AEADCipher does not allow casting');
158+
throw UnsupportedError('AEADStreamCipher does not allow casting');
159159
}
160160

161161
/// Generates a message authentication tag for a [stream] of data.
@@ -182,7 +182,7 @@ class AEADStreamCipher<C extends StreamCipher, M extends MACHashBase>
182182
output.add(data);
183183
},
184184
onDone: () {
185-
if (dataLength & 15 != 0) {
185+
if ((dataLength & 15) != 0) {
186186
sink.add(Uint8List(16 - (dataLength & 15))); // pad with zero
187187
}
188188
sink.add(AEADCipher._build128(dataLength, aadLength));
@@ -222,7 +222,7 @@ class AEADStreamCipher<C extends StreamCipher, M extends MACHashBase>
222222
dataLength += data.length;
223223
sink.add(data);
224224
}
225-
if (dataLength & 15 != 0) {
225+
if ((dataLength & 15) != 0) {
226226
sink.add(Uint8List(16 - (dataLength & 15))); // pad with zero
227227
}
228228

lib/src/algorithms/aes_modes/cbc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class AESInCBCModeDecrypt extends Cipher with SaltedCipher {
145145
final output32 = Uint32List.view(output.buffer);
146146
final xkey32 = AESCore.$expandDecryptionKey(key32);
147147

148-
if (n & 15 != 0) {
148+
if ((n & 15) != 0) {
149149
throw StateError('Invalid input size');
150150
}
151151

lib/src/algorithms/aes_modes/ecb.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class AESInECBModeDecrypt extends Cipher {
121121
final output32 = Uint32List.view(output.buffer);
122122
final xkey32 = AESCore.$expandDecryptionKey(key32);
123123

124-
if (n & 15 != 0) {
124+
if ((n & 15) != 0) {
125125
throw StateError('Invalid input size');
126126
}
127127

lib/src/algorithms/aes_modes/ige.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class AESInIGEModeDecrypt extends Cipher with SaltedCipher {
166166
final output32 = Uint32List.view(output.buffer);
167167
final xkey32 = AESCore.$expandDecryptionKey(key32);
168168

169-
if (n & 15 != 0) {
169+
if ((n & 15) != 0) {
170170
throw StateError('Invalid input size');
171171
}
172172

lib/src/algorithms/aes_modes/pcbc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class AESInPCBCModeDecrypt extends Cipher with SaltedCipher {
157157
final output32 = Uint32List.view(output.buffer);
158158
final xkey32 = AESCore.$expandDecryptionKey(key32);
159159

160-
if (n & 15 != 0) {
160+
if ((n & 15) != 0) {
161161
throw StateError('Invalid input size');
162162
}
163163

test/chacha20_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ void main() {
7373
test('random nonce is used if nonce is null, ', () {
7474
var key = randomNumbers(32);
7575
var text = randomBytes(100);
76-
chacha20(text, key);
76+
var out = chacha20(text, key);
77+
expect(out, isNotEmpty);
78+
expect(out, isNot(equals(text)));
79+
var out2 = chacha20(out, key);
80+
expect(out2, isNot(equals(out)));
7781
});
7882
});
7983

test/salsa20_poly1305_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ void main() {
5757
test('random nonce is used if nonce is null, ', () {
5858
var key = randomNumbers(32);
5959
var text = randomBytes(100);
60-
salsa20poly1305(text, key);
60+
var out = salsa20poly1305(text, key);
61+
expect(out.data, isNotEmpty);
62+
expect(out.data, isNot(equals(text)));
63+
var out2 = salsa20poly1305(text, key);
64+
expect(out2, isNot(equals(out)));
6165
});
6266
test('reset iv', () {
6367
var x = Salsa20(Uint8List(32)).poly1305();

test/salsa20_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ void main() {
6060
test('random nonce is used if nonce is null, ', () {
6161
var key = randomNumbers(32);
6262
var text = randomBytes(100);
63-
salsa20(text, key);
63+
var out = salsa20(text, key);
64+
expect(out, isNotEmpty);
65+
expect(out, isNot(equals(text)));
66+
var out2 = salsa20(text, key);
67+
expect(out2, isNot(equals(out)));
6468
});
6569
});
6670

test/typed_data_test.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2024, Sudipto Chandra
22
// All rights reserved. Check LICENSE file for details.
33

4+
import 'dart:collection';
45
import 'dart:typed_data';
56

67
import 'package:cipherlib/cipherlib.dart';
@@ -46,11 +47,16 @@ void main() {
4647
expect(out[0], equals(2));
4748
});
4849

49-
test('toUint8List handles non-list iterables', () {
50+
test('toUint8List handles non-list iterables of type Set', () {
5051
final out = toUint8List({1, 2, 3, 4});
5152
expect(out, equals(Uint8List.fromList([1, 2, 3, 4])));
5253
});
5354

55+
test('toUint8List handles non-list iterables of type Queue', () {
56+
final out = toUint8List(Queue<int>.from([1, 2, 3, 4]));
57+
expect(out, equals(Uint8List.fromList([1, 2, 3, 4])));
58+
});
59+
5460
test('toUint8List returns same instance for full Uint8List buffer', () {
5561
final full = Uint8List.fromList(List.generate(8, (i) => i));
5662
final out = toUint8List(full);

test/xchacha20_poly1305_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ void main() {
6767
test('random nonce is used if nonce is null', () {
6868
var key = randomNumbers(32);
6969
var text = randomBytes(100);
70-
xchacha20poly1305(text, key);
70+
var out = xchacha20poly1305(text, key);
71+
expect(out.data, isNotEmpty);
72+
expect(out.data, isNot(equals(text)));
73+
var out2 = xchacha20poly1305(text, key);
74+
expect(out2, isNot(equals(out)));
7175
});
7276
test('subkey is same as internal key', () {
7377
var x = XChaCha20(Uint8List(32)).poly1305();

0 commit comments

Comments
 (0)