Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/impl_ffi/impl_ffi.aesctr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Stream<Uint8List> _aesCtrEncryptOrDecrypt(
// Consume the first M bytes from data.
var i = 0; // Number of bytes consumed, after offset
while (i < M) {
final N = math.min(M, bufSize);
final N = math.min(M - i, bufSize);
inData.setAll(0, data.skip(offset + i).take(N));

_checkOpIsOne(ssl.EVP_CipherUpdate(ctx, outBuf, outLen, inBuf, N));
Expand Down
18 changes: 18 additions & 0 deletions test/aes_ctr_counter_wrap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:async';
import 'dart:typed_data';

import 'package:test/test.dart';
Expand All @@ -37,4 +38,21 @@ void main() {

expect(ciphertextA.sublist(16, 32), isNot(equals(ciphertextB)));
});

test('AES-CTR handles input larger than the internal buffer', () async {
final key = await AesCtrSecretKey.importRawKey(Uint8List(16));
final plaintext = Uint8List(4097);
final counter = Uint8List(16);

final ciphertext = await key.encryptBytes(plaintext, counter, 128);
final decrypted = await key.decryptBytes(ciphertext, counter, 128);
final streamedCiphertext = await key
.encryptStream(Stream.value(plaintext), counter, 128)
.expand((chunk) => chunk)
.toList();

expect(ciphertext, hasLength(plaintext.length));
expect(decrypted, equals(plaintext));
expect(streamedCiphertext, equals(ciphertext));
});
}