From 3df81bdd76c85c2c81b45a4ec3318f40a5df5467 Mon Sep 17 00:00:00 2001 From: HamdaanAliQuatil Date: Tue, 10 Nov 2026 14:02:37 +0530 Subject: [PATCH] fix: remove unnecessary input snapshots --- lib/src/impl_ffi/impl_ffi.aescbc.dart | 10 +- lib/src/impl_ffi/impl_ffi.aesctr.dart | 4 +- lib/src/impl_ffi/impl_ffi.digest.dart | 2 +- lib/src/impl_ffi/impl_ffi.ecdsa.dart | 2 +- lib/src/impl_ffi/impl_ffi.hmac.dart | 16 +-- lib/src/impl_ffi/impl_ffi.rsapss.dart | 2 +- lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart | 3 +- lib/src/impl_ffi/impl_ffi.utils.dart | 5 +- lib/src/impl_js/impl_js.hmac.dart | 10 +- lib/src/impl_js/impl_js.rsapss.dart | 7 +- lib/src/impl_js/impl_js.rsassapkcs1v15.dart | 3 +- .../regression/aes_cbc_data_snapshot.dart | 49 ------- .../regression/aes_cbc_iv_snapshot.dart | 52 -------- .../regression/digest_input_snapshot.dart | 37 ------ .../regression/hmac_signature_snapshot.dart | 92 ------------- .../regression/input_buffers_unchanged.dart | 125 ++++++++++++++++++ .../regression/rsa_signature_snapshot.dart | 88 ------------ .../sign_bytes_message_snapshot.dart | 89 ------------- lib/src/testing/testing.dart | 15 +-- lib/src/webcrypto/webcrypto.aescbc.dart | 2 + lib/src/webcrypto/webcrypto.aesctr.dart | 2 + lib/src/webcrypto/webcrypto.aesgcm.dart | 2 + lib/src/webcrypto/webcrypto.digest.dart | 2 + lib/src/webcrypto/webcrypto.ecdh.dart | 4 + lib/src/webcrypto/webcrypto.ecdsa.dart | 6 + lib/src/webcrypto/webcrypto.hkdf.dart | 2 + lib/src/webcrypto/webcrypto.hmac.dart | 2 + lib/src/webcrypto/webcrypto.pbkdf2.dart | 2 + lib/src/webcrypto/webcrypto.rsaoaep.dart | 4 + lib/src/webcrypto/webcrypto.rsapss.dart | 4 + .../webcrypto/webcrypto.rsassapkcs1v15.dart | 4 + lib/webcrypto.dart | 12 ++ test/aes_ctr_counter_wrap_test.dart | 79 ++++------- 33 files changed, 219 insertions(+), 519 deletions(-) delete mode 100644 lib/src/testing/regression/aes_cbc_data_snapshot.dart delete mode 100644 lib/src/testing/regression/aes_cbc_iv_snapshot.dart delete mode 100644 lib/src/testing/regression/digest_input_snapshot.dart delete mode 100644 lib/src/testing/regression/hmac_signature_snapshot.dart create mode 100644 lib/src/testing/regression/input_buffers_unchanged.dart delete mode 100644 lib/src/testing/regression/rsa_signature_snapshot.dart delete mode 100644 lib/src/testing/regression/sign_bytes_message_snapshot.dart diff --git a/lib/src/impl_ffi/impl_ffi.aescbc.dart b/lib/src/impl_ffi/impl_ffi.aescbc.dart index 873d3f1e4..c65becd72 100644 --- a/lib/src/impl_ffi/impl_ffi.aescbc.dart +++ b/lib/src/impl_ffi/impl_ffi.aescbc.dart @@ -126,10 +126,7 @@ final class _AesCbcSecretKeyImpl implements AesCbcSecretKeyImpl { @override Future decryptBytes(List data, List iv) async => await _bufferStream( - decryptStream( - Stream.value(Uint8List.fromList(data)), - Uint8List.fromList(iv), - ), + decryptStream(Stream.value(data), Uint8List.fromList(iv)), ); @override @@ -139,10 +136,7 @@ final class _AesCbcSecretKeyImpl implements AesCbcSecretKeyImpl { @override Future encryptBytes(List data, List iv) async => await _bufferStream( - encryptStream( - Stream.value(Uint8List.fromList(data)), - Uint8List.fromList(iv), - ), + encryptStream(Stream.value(data), Uint8List.fromList(iv)), ); @override diff --git a/lib/src/impl_ffi/impl_ffi.aesctr.dart b/lib/src/impl_ffi/impl_ffi.aesctr.dart index 785724140..8e5d6f9f7 100644 --- a/lib/src/impl_ffi/impl_ffi.aesctr.dart +++ b/lib/src/impl_ffi/impl_ffi.aesctr.dart @@ -241,7 +241,7 @@ final class _AesCtrSecretKeyImpl implements AesCtrSecretKeyImpl { ) async { _checkArguments(counter, length); return await _bufferStream( - decryptStream(Stream.value(Uint8List.fromList(data)), counter, length), + decryptStream(Stream.value(data), counter, length), ); } @@ -269,7 +269,7 @@ final class _AesCtrSecretKeyImpl implements AesCtrSecretKeyImpl { ) async { _checkArguments(counter, length); return await _bufferStream( - encryptStream(Stream.value(Uint8List.fromList(data)), counter, length), + encryptStream(Stream.value(data), counter, length), ); } diff --git a/lib/src/impl_ffi/impl_ffi.digest.dart b/lib/src/impl_ffi/impl_ffi.digest.dart index ff6abc61e..9d9cd2882 100644 --- a/lib/src/impl_ffi/impl_ffi.digest.dart +++ b/lib/src/impl_ffi/impl_ffi.digest.dart @@ -38,7 +38,7 @@ abstract class _HashImpl implements HashImpl { @override Future digestBytes(List data) => - digestStream(Stream.value(Uint8List.fromList(data))); + digestStream(Stream.value(data)); @override Future digestStream(Stream> data) { diff --git a/lib/src/impl_ffi/impl_ffi.ecdsa.dart b/lib/src/impl_ffi/impl_ffi.ecdsa.dart index eeb6e5325..f3c560239 100644 --- a/lib/src/impl_ffi/impl_ffi.ecdsa.dart +++ b/lib/src/impl_ffi/impl_ffi.ecdsa.dart @@ -220,7 +220,7 @@ final class _EcdsaPrivateKeyImpl implements EcdsaPrivateKeyImpl { @override Future signBytes(List data, HashImpl hash) => - signStream(Stream.value(Uint8List.fromList(data)), hash); + signStream(Stream.value(data), hash); @override Future signStream(Stream> data, HashImpl hash) async { diff --git a/lib/src/impl_ffi/impl_ffi.hmac.dart b/lib/src/impl_ffi/impl_ffi.hmac.dart index cba818fed..fdf15cfa5 100644 --- a/lib/src/impl_ffi/impl_ffi.hmac.dart +++ b/lib/src/impl_ffi/impl_ffi.hmac.dart @@ -142,8 +142,7 @@ final class _HmacSecretKeyImpl implements HmacSecretKeyImpl { } @override - Future signBytes(List data) => - signStream(Stream.value(Uint8List.fromList(data))); + Future signBytes(List data) => signStream(Stream.value(data)); @override Future signStream(Stream> data) { @@ -176,18 +175,7 @@ final class _HmacSecretKeyImpl implements HmacSecretKeyImpl { verifyStream(signature, Stream.value(data)); @override - Future verifyStream(List signature, Stream> data) { - // Snapshot caller-owned input before any asynchronous work. This matches - // Web Crypto semantics and keeps verification independent of mutations to - // the original list while the data stream is being consumed. - final signatureSnapshot = List.of(signature, growable: false); - return _verifySignatureStream(signatureSnapshot, data); - } - - Future _verifySignatureStream( - List signature, - Stream> data, - ) async { + Future verifyStream(List signature, Stream> data) async { final other = await signStream(data); if (signature.length != other.length) { return false; diff --git a/lib/src/impl_ffi/impl_ffi.rsapss.dart b/lib/src/impl_ffi/impl_ffi.rsapss.dart index ede6e33f4..381c808b6 100644 --- a/lib/src/impl_ffi/impl_ffi.rsapss.dart +++ b/lib/src/impl_ffi/impl_ffi.rsapss.dart @@ -128,7 +128,7 @@ final class _RsaPssPrivateKeyImpl implements RsaPssPrivateKeyImpl { @override Future signBytes(List data, int saltLength) { - return signStream(Stream.value(Uint8List.fromList(data)), saltLength); + return signStream(Stream.value(data), saltLength); } @override diff --git a/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart b/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart index 874156e97..61ae9e4cf 100644 --- a/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart +++ b/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart @@ -126,8 +126,7 @@ final class _RsaSsaPkcs1V15PrivateKeyImpl } @override - Future signBytes(List data) => - signStream(Stream.value(Uint8List.fromList(data))); + Future signBytes(List data) => signStream(Stream.value(data)); @override Future signStream(Stream> data) => _signStream( diff --git a/lib/src/impl_ffi/impl_ffi.utils.dart b/lib/src/impl_ffi/impl_ffi.utils.dart index d467df855..be42d8905 100644 --- a/lib/src/impl_ffi/impl_ffi.utils.dart +++ b/lib/src/impl_ffi/impl_ffi.utils.dart @@ -424,7 +424,6 @@ Future _verifyStream( Stream> data, { void Function(ffi.Pointer ctx)? config, }) { - final signatureSnapshot = Uint8List.fromList(signature); return _Scope.async((scope) async { // Create and initialize verification context final ctx = scope.create(ssl.EVP_MD_CTX_new, ssl.EVP_MD_CTX_free); @@ -444,8 +443,8 @@ Future _verifyStream( // Verify signature final result = ssl.EVP_DigestVerifyFinal( ctx, - scope.dataAsPointer(signatureSnapshot), - signatureSnapshot.length, + scope.dataAsPointer(signature), + signature.length, ); if (result != 1) { // TODO: We should always clear errors, when returning from any diff --git a/lib/src/impl_js/impl_js.hmac.dart b/lib/src/impl_js/impl_js.hmac.dart index c7975c17a..262eb4047 100644 --- a/lib/src/impl_js/impl_js.hmac.dart +++ b/lib/src/impl_js/impl_js.hmac.dart @@ -164,15 +164,7 @@ final class _HmacSecretKeyImpl implements HmacSecretKeyImpl { } @override - Future verifyStream(List signature, Stream> data) { - final signatureSnapshot = List.of(signature, growable: false); - return _verifySignatureStream(signatureSnapshot, data); - } - - Future _verifySignatureStream( - List signature, - Stream> data, - ) async { + Future verifyStream(List signature, Stream> data) async { return await verifyBytes(signature, await _bufferStream(data)); } diff --git a/lib/src/impl_js/impl_js.rsapss.dart b/lib/src/impl_js/impl_js.rsapss.dart index a00f6b92b..670dacd0e 100644 --- a/lib/src/impl_js/impl_js.rsapss.dart +++ b/lib/src/impl_js/impl_js.rsapss.dart @@ -240,12 +240,7 @@ final class _RsaPssPublicKeyImpl implements RsaPssPublicKeyImpl { Stream> data, int saltLength, ) async { - final signatureSnapshot = Uint8List.fromList(signature); - return await verifyBytes( - signatureSnapshot, - await _bufferStream(data), - saltLength, - ); + return await verifyBytes(signature, await _bufferStream(data), saltLength); } @override diff --git a/lib/src/impl_js/impl_js.rsassapkcs1v15.dart b/lib/src/impl_js/impl_js.rsassapkcs1v15.dart index 5a33bd155..0d54860c7 100644 --- a/lib/src/impl_js/impl_js.rsassapkcs1v15.dart +++ b/lib/src/impl_js/impl_js.rsassapkcs1v15.dart @@ -194,8 +194,7 @@ final class _RsaSsaPkcs1V15PublicKeyImpl @override Future verifyStream(List signature, Stream> data) async { - final signatureSnapshot = Uint8List.fromList(signature); - return await verifyBytes(signatureSnapshot, await _bufferStream(data)); + return await verifyBytes(signature, await _bufferStream(data)); } @override diff --git a/lib/src/testing/regression/aes_cbc_data_snapshot.dart b/lib/src/testing/regression/aes_cbc_data_snapshot.dart deleted file mode 100644 index 84b49219a..000000000 --- a/lib/src/testing/regression/aes_cbc_data_snapshot.dart +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2026 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'package:webcrypto/webcrypto.dart'; -import '../utils/utils.dart'; - -void main() => tests().runTests(); - -List<({String name, Future Function() test})> tests() => [ - ( - name: 'AES-CBC encryptBytes snapshots plaintext', - test: () async { - final key = await AesCbcSecretKey.importRawKey(List.filled(16, 0)); - final iv = List.filled(16, 1); - final plaintext = List.filled(32, 2); - final expected = await key.encryptBytes(List.of(plaintext), iv); - - final pending = key.encryptBytes(plaintext, iv); - plaintext.fillRange(0, plaintext.length, 3); - - check(equalBytes(await pending, expected)); - }, - ), - ( - name: 'AES-CBC decryptBytes snapshots ciphertext', - test: () async { - final key = await AesCbcSecretKey.importRawKey(List.filled(16, 0)); - final iv = List.filled(16, 1); - final plaintext = List.filled(32, 2); - final ciphertext = await key.encryptBytes(plaintext, iv); - - final pending = key.decryptBytes(ciphertext, iv); - ciphertext[0] ^= 1; - - check(equalBytes(await pending, plaintext)); - }, - ), -]; diff --git a/lib/src/testing/regression/aes_cbc_iv_snapshot.dart b/lib/src/testing/regression/aes_cbc_iv_snapshot.dart deleted file mode 100644 index 8966d7829..000000000 --- a/lib/src/testing/regression/aes_cbc_iv_snapshot.dart +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2026 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'package:webcrypto/webcrypto.dart'; -import '../utils/utils.dart'; - -void main() => tests().runTests(); - -List<({String name, Future Function() test})> tests() => [ - ( - name: 'AES-CBC encryptBytes snapshots the IV', - test: () async { - final key = await AesCbcSecretKey.importRawKey(List.filled(16, 0)); - const plaintext = [1, 2, 3]; - final iv = List.filled(16, 0); - final pending = key.encryptBytes(plaintext, iv); - iv[15] = 1; - - final actual = await pending; - final expected = await key.encryptBytes( - plaintext, - List.filled(16, 0), - ); - check(equalBytes(actual, expected)); - }, - ), - ( - name: 'AES-CBC decryptBytes snapshots the IV', - test: () async { - final key = await AesCbcSecretKey.importRawKey(List.filled(16, 0)); - const plaintext = [1, 2, 3]; - final originalIv = List.filled(16, 0); - final ciphertext = await key.encryptBytes(plaintext, originalIv); - final iv = List.filled(16, 0); - final pending = key.decryptBytes(ciphertext, iv); - iv[0] = 1; - - check(equalBytes(await pending, plaintext)); - }, - ), -]; diff --git a/lib/src/testing/regression/digest_input_snapshot.dart b/lib/src/testing/regression/digest_input_snapshot.dart deleted file mode 100644 index e999b6482..000000000 --- a/lib/src/testing/regression/digest_input_snapshot.dart +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2026 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'package:webcrypto/webcrypto.dart'; - -import '../utils/utils.dart'; - -void main() => tests().runTests(); - -List<({String name, Future Function() test})> tests() => [ - ( - name: 'Hash.digestBytes snapshots input at invocation', - test: () async { - final data = [1, 2, 3]; - final expected = await Hash.sha256.digestBytes(data); - - final pending = Hash.sha256.digestBytes(data); - data[0] = 9; - - check( - equalBytes(await pending, expected), - 'digestBytes must not observe a later input mutation', - ); - }, - ), -]; diff --git a/lib/src/testing/regression/hmac_signature_snapshot.dart b/lib/src/testing/regression/hmac_signature_snapshot.dart deleted file mode 100644 index 3f499eb3a..000000000 --- a/lib/src/testing/regression/hmac_signature_snapshot.dart +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright 2026 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'dart:async'; - -import 'package:webcrypto/webcrypto.dart'; - -import '../utils/utils.dart'; - -void main() => tests().runTests(); - -List<({String name, Future Function() test})> tests() { - final tests = <({String name, Future Function() test})>[]; - void test(String name, Future Function() fn) => - tests.add((name: name, test: fn)); - - test('Hmac: verifyStream snapshots an invalid signature', () async { - final key = await HmacSecretKey.importRawKey( - List.filled(32, 7), - Hash.sha256, - ); - final data = List.filled(128, 3); - final signature = await key.signBytes(data); - signature[0] ^= 1; - - final controller = StreamController>(); - final pending = key.verifyStream(signature, controller.stream); - - // Restore the valid bytes while verification is waiting for stream data. - signature[0] ^= 1; - controller.add(data); - await controller.close(); - - check( - !await pending, - 'verification must use the invalid signature supplied at invocation', - ); - }); - - test('Hmac: verifyStream snapshots a valid signature', () async { - final key = await HmacSecretKey.importRawKey( - List.filled(32, 7), - Hash.sha256, - ); - final data = List.filled(128, 3); - final signature = await key.signBytes(data); - - final controller = StreamController>(); - final pending = key.verifyStream(signature, controller.stream); - - // Corrupt the caller-owned list while verification is waiting. - signature[0] ^= 1; - controller.add(data); - await controller.close(); - - check( - await pending, - 'verification must use the valid signature supplied at invocation', - ); - }); - - test('Hmac: verifyBytes snapshots the signature at invocation', () async { - final key = await HmacSecretKey.importRawKey( - List.filled(32, 7), - Hash.sha256, - ); - final data = List.filled(50000, 3); - final signature = await key.signBytes(data); - signature[0] ^= 1; - - final pending = key.verifyBytes(signature, data); - signature[0] ^= 1; - - check( - !await pending, - 'verifyBytes must not observe a later signature mutation', - ); - }); - - return tests; -} diff --git a/lib/src/testing/regression/input_buffers_unchanged.dart b/lib/src/testing/regression/input_buffers_unchanged.dart new file mode 100644 index 000000000..acd7abb0e --- /dev/null +++ b/lib/src/testing/regression/input_buffers_unchanged.dart @@ -0,0 +1,125 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import 'package:webcrypto/webcrypto.dart'; + +import '../utils/utils.dart'; + +List<({String name, Future Function() test})> tests() => [ + ( + name: 'Hash byte operations leave caller input unchanged', + test: () async { + final data = List.generate(8193, (i) => i & 0xff); + final original = List.of(data); + for (final hash in [Hash.sha1, Hash.sha256, Hash.sha384, Hash.sha512]) { + final digest = await hash.digestBytes(data); + check(equalBytes(data, original)); + check(equalBytes(digest, await hash.digestBytes(data))); + } + }, + ), + ( + name: 'AES-CBC byte operations leave data and IV unchanged', + test: () async { + final key = await AesCbcSecretKey.importRawKey(List.filled(16, 0)); + final data = List.generate(8193, (i) => i & 0xff); + final original = List.of(data); + final iv = List.generate(16, (i) => i); + final originalIv = List.of(iv); + final ciphertext = await key.encryptBytes(data, iv); + check(equalBytes(data, original)); + check(equalBytes(iv, originalIv)); + final savedCiphertext = List.of(ciphertext); + check(equalBytes(await key.decryptBytes(ciphertext, iv), original)); + check(equalBytes(ciphertext, savedCiphertext)); + check(equalBytes(iv, originalIv)); + }, + ), + ( + name: 'HMAC byte and stream verification leave inputs unchanged', + test: () async { + final key = await HmacSecretKey.importRawKey([1, 2, 3], Hash.sha256); + await _checkSigningInputs( + key.signBytes, + key.verifyBytes, + key.verifyStream, + ); + }, + ), + ( + name: 'RSA-PSS byte and stream verification leave inputs unchanged', + test: () async { + final pair = await RsaPssPrivateKey.generateKey( + 2048, + BigInt.from(65537), + Hash.sha256, + ); + await _checkSigningInputs( + (data) => pair.privateKey.signBytes(data, 32), + (signature, data) => pair.publicKey.verifyBytes(signature, data, 32), + (signature, data) => pair.publicKey.verifyStream(signature, data, 32), + ); + }, + ), + ( + name: 'RSASSA-PKCS1-v1_5 verification leaves inputs unchanged', + test: () async { + final pair = await RsassaPkcs1V15PrivateKey.generateKey( + 2048, + BigInt.from(65537), + Hash.sha256, + ); + await _checkSigningInputs( + pair.privateKey.signBytes, + pair.publicKey.verifyBytes, + pair.publicKey.verifyStream, + ); + }, + ), + ( + name: 'ECDSA byte and stream verification leave inputs unchanged', + test: () async { + final pair = await EcdsaPrivateKey.generateKey(EllipticCurve.p256); + await _checkSigningInputs( + (data) => pair.privateKey.signBytes(data, Hash.sha256), + (signature, data) => + pair.publicKey.verifyBytes(signature, data, Hash.sha256), + (signature, data) => + pair.publicKey.verifyStream(signature, data, Hash.sha256), + ); + }, + ), +]; + +Future _checkSigningInputs( + Future> Function(List) sign, + Future Function(List, List) verifyBytes, + Future Function(List, Stream>) verifyStream, +) async { + final data = List.generate(8193, (i) => i & 0xff); + final original = List.of(data); + final signature = await sign(data); + final originalSignature = List.of(signature); + check(equalBytes(data, original)); + check(await verifyBytes(signature, data)); + check(await verifyStream(signature, Stream.value(data))); + check(equalBytes(data, original)); + check(equalBytes(signature, originalSignature)); + signature[0] ^= 1; + final invalidSignature = List.of(signature); + check(!await verifyBytes(signature, data)); + check(!await verifyStream(signature, Stream.value(data))); + check(equalBytes(signature, invalidSignature)); + check(equalBytes(data, original)); +} diff --git a/lib/src/testing/regression/rsa_signature_snapshot.dart b/lib/src/testing/regression/rsa_signature_snapshot.dart deleted file mode 100644 index 856ffc814..000000000 --- a/lib/src/testing/regression/rsa_signature_snapshot.dart +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2026 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'dart:async'; - -import 'package:webcrypto/webcrypto.dart'; - -import '../utils/utils.dart'; - -void main() => tests().runTests(); - -List<({String name, Future Function() test})> tests() => [ - ( - name: 'RSASSA-PKCS1-v1_5 verification snapshots the signature', - test: () async { - final pair = await RsassaPkcs1V15PrivateKey.generateKey( - 1024, - BigInt.from(65537), - Hash.sha256, - ); - await _checkSnapshot( - pair.privateKey.signBytes, - pair.publicKey.verifyStream, - pair.publicKey.verifyBytes, - ); - }, - ), - ( - name: 'RSA-PSS verification snapshots the signature', - test: () async { - final pair = await RsaPssPrivateKey.generateKey( - 1024, - BigInt.from(65537), - Hash.sha256, - ); - await _checkSnapshot( - (data) => pair.privateKey.signBytes(data, 32), - (signature, data) => pair.publicKey.verifyStream(signature, data, 32), - (signature, data) => pair.publicKey.verifyBytes(signature, data, 32), - ); - }, - ), -]; - -Future _checkSnapshot( - Future> Function(List) sign, - Future Function(List, Stream>) verifyStream, - Future Function(List, List) verifyBytes, -) async { - const data = [1, 2, 3]; - final signature = await sign(data); - - for (final validAtInvocation in [true, false]) { - final input = List.of(signature); - if (!validAtInvocation) input[0] ^= 1; - - final controller = StreamController>(); - final pending = verifyStream(input, controller.stream); - input[0] ^= 1; - controller.add(data); - await controller.close(); - - check( - await pending == validAtInvocation, - 'verification must use the signature supplied at invocation', - ); - - final bytesInput = List.of(signature); - if (!validAtInvocation) bytesInput[0] ^= 1; - final bytesPending = verifyBytes(bytesInput, data); - bytesInput[0] ^= 1; - check( - await bytesPending == validAtInvocation, - 'byte verification must use the signature supplied at invocation', - ); - } -} diff --git a/lib/src/testing/regression/sign_bytes_message_snapshot.dart b/lib/src/testing/regression/sign_bytes_message_snapshot.dart deleted file mode 100644 index 233c9f4ff..000000000 --- a/lib/src/testing/regression/sign_bytes_message_snapshot.dart +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2026 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'dart:typed_data'; - -import 'package:webcrypto/webcrypto.dart'; - -import '../utils/utils.dart'; - -void main() => tests().runTests(); - -Future _checkMessageSnapshot( - Future Function(List) sign, - Future Function(Uint8List, List) verify, -) async { - final original = [1, 2, 3]; - final data = List.of(original); - final pending = sign(data); - data[0] ^= 1; - - final signature = await pending; - check( - await verify(signature, original), - 'signBytes must use the message supplied at invocation', - ); -} - -List<({String name, Future Function() test})> tests() => [ - ( - name: 'HMAC signBytes snapshots message', - test: () async { - final key = await HmacSecretKey.importRawKey( - List.filled(32, 7), - Hash.sha256, - ); - await _checkMessageSnapshot(key.signBytes, key.verifyBytes); - }, - ), - ( - name: 'RSA-PSS signBytes snapshots message', - test: () async { - final keys = await RsaPssPrivateKey.generateKey( - 2048, - BigInt.from(65537), - Hash.sha256, - ); - await _checkMessageSnapshot( - (data) => keys.privateKey.signBytes(data, 32), - (signature, data) => keys.publicKey.verifyBytes(signature, data, 32), - ); - }, - ), - ( - name: 'RSASSA-PKCS1-v1_5 signBytes snapshots message', - test: () async { - final keys = await RsassaPkcs1V15PrivateKey.generateKey( - 2048, - BigInt.from(65537), - Hash.sha256, - ); - await _checkMessageSnapshot( - keys.privateKey.signBytes, - keys.publicKey.verifyBytes, - ); - }, - ), - ( - name: 'ECDSA signBytes snapshots message', - test: () async { - final keys = await EcdsaPrivateKey.generateKey(EllipticCurve.p256); - await _checkMessageSnapshot( - (data) => keys.privateKey.signBytes(data, Hash.sha256), - (signature, data) => - keys.publicKey.verifyBytes(signature, data, Hash.sha256), - ); - }, - ), -]; diff --git a/lib/src/testing/testing.dart b/lib/src/testing/testing.dart index 553b4f646..d83eefd60 100644 --- a/lib/src/testing/testing.dart +++ b/lib/src/testing/testing.dart @@ -35,15 +35,10 @@ import 'regression/aes_cbc_invalid_iv.dart' as aes_cbc_invalid_iv; import 'regression/aes_ctr_invalid_length.dart' as aes_ctr_invalid_length; import 'regression/aes_key_length.dart' as aes_key_length; import 'regression/issue_302_hmac_jwk_length.dart' as issue_302_hmac_jwk_length; -import 'regression/hmac_signature_snapshot.dart' as hmac_signature_snapshot; -import 'regression/sign_bytes_message_snapshot.dart' - as sign_bytes_message_snapshot; +import 'regression/input_buffers_unchanged.dart' as input_buffers_unchanged; import 'regression/aes_gcm_invalid_tag_length.dart' as aes_gcm_invalid_tag_length; -import 'regression/aes_cbc_iv_snapshot.dart' as aes_cbc_iv_snapshot; -import 'regression/aes_cbc_data_snapshot.dart' as aes_cbc_data_snapshot; import 'regression/derive_bits_zero_length.dart' as derive_bits_zero_length; -import 'regression/digest_input_snapshot.dart' as digest_input_snapshot; import 'regression/ecdh_invalid_length.dart' as ecdh_invalid_length; import 'ecdh/derive_bits.dart' as ecdh_derive_bits; import 'regression/hmac_empty_key.dart' as hmac_empty_key; @@ -55,7 +50,6 @@ import 'regression/rsa_oaep_sha1_jwk_alg.dart' as rsa_oaep_sha1_jwk_alg; import 'regression/rsa_modulus_length.dart' as rsa_modulus_length; import 'regression/rsa_private_jwk_oth.dart' as rsa_private_jwk_oth; import 'regression/rsa_pss_async_errors.dart' as rsa_pss_async_errors; -import 'regression/rsa_signature_snapshot.dart' as rsa_signature_snapshot; /// Test runners from all test files except `digest.dart` and /// `random.dart`, which do not use [TestRunner]. @@ -88,17 +82,13 @@ void runAllTests( ...aes_ctr_invalid_length.tests(), ...aes_key_length.tests(), ...issue_302_hmac_jwk_length.tests(), - ...hmac_signature_snapshot.tests(), - ...sign_bytes_message_snapshot.tests(), + ...input_buffers_unchanged.tests(), ...aes_gcm_invalid_tag_length.tests(), - ...aes_cbc_iv_snapshot.tests(), - ...aes_cbc_data_snapshot.tests(), ...ecdh_derive_bits.tests(), ...issue_60_trailing_bytes.tests(), ...jwk_base64url.tests(), ...jwk_export_use.tests(), ...derive_bits_zero_length.tests(), - ...digest_input_snapshot.tests(), ...ecdh_invalid_length.tests(), ...hmac_empty_key.tests(), ...rsa_oaep_sha1_jwk_alg.tests(), @@ -107,7 +97,6 @@ void runAllTests( if (detectedRuntime != 'firefox') ...hmac_large_generated_key.tests(), ...rsa_private_jwk_oth.tests(), ...rsa_pss_async_errors.tests(), - ...rsa_signature_snapshot.tests(), ]; for (final (:name, :test) in allTests) { diff --git a/lib/src/webcrypto/webcrypto.aescbc.dart b/lib/src/webcrypto/webcrypto.aescbc.dart index 2d467ddad..ce0066a19 100644 --- a/lib/src/webcrypto/webcrypto.aescbc.dart +++ b/lib/src/webcrypto/webcrypto.aescbc.dart @@ -34,6 +34,8 @@ part of 'webcrypto.dart'; /// [1]: https://csrc.nist.gov/publications/detail/sp/800-38a/final /// [2]: https://www.rfc-editor.org/rfc/rfc2315#section-10.3 /// [3]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class AesCbcSecretKey { final AesCbcSecretKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.aesctr.dart b/lib/src/webcrypto/webcrypto.aesctr.dart index 0fa5f504e..4544fd484 100644 --- a/lib/src/webcrypto/webcrypto.aesctr.dart +++ b/lib/src/webcrypto/webcrypto.aesctr.dart @@ -28,6 +28,8 @@ part of 'webcrypto.dart'; /// /// [1]: https://csrc.nist.gov/publications/detail/sp/800-38a/final /// [2]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class AesCtrSecretKey { final AesCtrSecretKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.aesgcm.dart b/lib/src/webcrypto/webcrypto.aesgcm.dart index 95da9906e..1c005235e 100644 --- a/lib/src/webcrypto/webcrypto.aesgcm.dart +++ b/lib/src/webcrypto/webcrypto.aesgcm.dart @@ -32,6 +32,8 @@ part of 'webcrypto.dart'; /// [1]: https://csrc.nist.gov/pubs/sp/800/38/d/final /// [2]: https://www.rfc-editor.org/rfc/rfc7517 /// [3]: https://en.wikipedia.org/wiki/Authenticated_encryption +/// +/// {@macro webcrypto.input-lifetimes} final class AesGcmSecretKey { final AesGcmSecretKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.digest.dart b/lib/src/webcrypto/webcrypto.digest.dart index 92d2128f9..00fef818d 100644 --- a/lib/src/webcrypto/webcrypto.digest.dart +++ b/lib/src/webcrypto/webcrypto.digest.dart @@ -30,6 +30,8 @@ part of 'webcrypto.dart'; /// to other methods in this library. /// /// [1]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf +/// +/// {@macro webcrypto.input-lifetimes} abstract final class Hash { HashImpl get _impl; diff --git a/lib/src/webcrypto/webcrypto.ecdh.dart b/lib/src/webcrypto/webcrypto.ecdh.dart index 532dfb416..fb5beb649 100644 --- a/lib/src/webcrypto/webcrypto.ecdh.dart +++ b/lib/src/webcrypto/webcrypto.ecdh.dart @@ -51,6 +51,8 @@ part of 'webcrypto.dart'; /// } /// ``` /// {@endtemplate} +/// +/// {@macro webcrypto.input-lifetimes} final class EcdhPrivateKey { final EcdhPrivateKeyImpl _impl; @@ -275,6 +277,8 @@ final class EcdhPrivateKey { Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } +/// +/// {@macro webcrypto.input-lifetimes} final class EcdhPublicKey { final EcdhPublicKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.ecdsa.dart b/lib/src/webcrypto/webcrypto.ecdsa.dart index 98c03b093..9e83fbe77 100644 --- a/lib/src/webcrypto/webcrypto.ecdsa.dart +++ b/lib/src/webcrypto/webcrypto.ecdsa.dart @@ -14,6 +14,9 @@ part of 'webcrypto.dart'; +/// An ECDSA private key for signing messages. +/// +/// {@macro webcrypto.input-lifetimes} final class EcdsaPrivateKey { final EcdsaPrivateKeyImpl _impl; @@ -72,6 +75,9 @@ final class EcdsaPrivateKey { Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } +/// An ECDSA public key for verifying signatures. +/// +/// {@macro webcrypto.input-lifetimes} final class EcdsaPublicKey { final EcdsaPublicKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.hkdf.dart b/lib/src/webcrypto/webcrypto.hkdf.dart index 288a04432..d647fd0b5 100644 --- a/lib/src/webcrypto/webcrypto.hkdf.dart +++ b/lib/src/webcrypto/webcrypto.hkdf.dart @@ -50,6 +50,8 @@ part of 'webcrypto.dart'; /// [1]: https://www.rfc-editor.org/rfc/rfc5869 // TODO: It might be wise to use a random salt, then suggest that the non-secret // salt is stored or exchanged... +/// +/// {@macro webcrypto.input-lifetimes} final class HkdfSecretKey { final HkdfSecretKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.hmac.dart b/lib/src/webcrypto/webcrypto.hmac.dart index 21f34594f..40d4f4f14 100644 --- a/lib/src/webcrypto/webcrypto.hmac.dart +++ b/lib/src/webcrypto/webcrypto.hmac.dart @@ -48,6 +48,8 @@ part of 'webcrypto.dart'; /// ``` /// /// [1]: https://doi.org/10.6028/NIST.FIPS.180-4 +/// +/// {@macro webcrypto.input-lifetimes} final class HmacSecretKey { final HmacSecretKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.pbkdf2.dart b/lib/src/webcrypto/webcrypto.pbkdf2.dart index 37fcc46e3..28a43ffd4 100644 --- a/lib/src/webcrypto/webcrypto.pbkdf2.dart +++ b/lib/src/webcrypto/webcrypto.pbkdf2.dart @@ -49,6 +49,8 @@ part of 'webcrypto.dart'; /// {@endtemplate} /// /// [1]: https://www.rfc-editor.org/rfc/rfc8018 +/// +/// {@macro webcrypto.input-lifetimes} final class Pbkdf2SecretKey { final Pbkdf2SecretKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.rsaoaep.dart b/lib/src/webcrypto/webcrypto.rsaoaep.dart index dc665c956..e787c2fa7 100644 --- a/lib/src/webcrypto/webcrypto.rsaoaep.dart +++ b/lib/src/webcrypto/webcrypto.rsaoaep.dart @@ -86,6 +86,8 @@ Map _normalizeLegacyRsaOaepJwkAlg( /// [1]: https://www.rfc-editor.org/rfc/rfc3447 /// [2]: https://www.rfc-editor.org/rfc/rfc5208 /// [3]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class RsaOaepPrivateKey { final RsaOaepPrivateKeyImpl _impl; @@ -363,6 +365,8 @@ final class RsaOaepPrivateKey { /// [1]: https://www.rfc-editor.org/rfc/rfc3447 /// [2]: https://www.rfc-editor.org/rfc/rfc5280 /// [3]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class RsaOaepPublicKey { final RsaOaepPublicKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.rsapss.dart b/lib/src/webcrypto/webcrypto.rsapss.dart index aa902d816..4a4e201bb 100644 --- a/lib/src/webcrypto/webcrypto.rsapss.dart +++ b/lib/src/webcrypto/webcrypto.rsapss.dart @@ -66,6 +66,8 @@ part of 'webcrypto.dart'; /// [1]: https://www.rfc-editor.org/rfc/rfc3447 /// [2]: https://www.rfc-editor.org/rfc/rfc5208 /// [3]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class RsaPssPrivateKey { final RsaPssPrivateKeyImpl _impl; @@ -452,6 +454,8 @@ final class RsaPssPrivateKey { /// [1]: https://www.rfc-editor.org/rfc/rfc3447 /// [2]: https://www.rfc-editor.org/rfc/rfc5280 /// [3]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class RsaPssPublicKey { final RsaPssPublicKeyImpl _impl; diff --git a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart index d68c79ee5..8fdf8d4be 100644 --- a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart +++ b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart @@ -60,6 +60,8 @@ part of 'webcrypto.dart'; /// [1]: https://www.rfc-editor.org/rfc/rfc3447 /// [2]: https://www.rfc-editor.org/rfc/rfc5208 /// [3]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class RsassaPkcs1V15PrivateKey { final RsaSsaPkcs1V15PrivateKeyImpl _impl; @@ -426,6 +428,8 @@ final class RsassaPkcs1V15PrivateKey { /// [1]: https://www.rfc-editor.org/rfc/rfc3447 /// [2]: https://www.rfc-editor.org/rfc/rfc5280 /// [3]: https://www.rfc-editor.org/rfc/rfc7517 +/// +/// {@macro webcrypto.input-lifetimes} final class RsassaPkcs1V15PublicKey { final RsaSsaPkcs1V15PublicKeyImpl _impl; diff --git a/lib/webcrypto.dart b/lib/webcrypto.dart index ace27b93e..b8ae13077 100644 --- a/lib/webcrypto.dart +++ b/lib/webcrypto.dart @@ -14,6 +14,18 @@ /// Cryptographic primitives for use on Dart VM and Dart in the browser. /// +/// ## Input lifetimes +/// +/// {@template webcrypto.input-lifetimes} +/// Keep input lists and maps unchanged until the returned `Future` completes. +/// Operations may read these inputs asynchronously; a call-time snapshot is +/// not guaranteed, even if a particular backend happens to make a copy. +/// +/// For methods returning a `Stream`, keep non-stream inputs (such as an IV or +/// counter) unchanged until the output stream completes or cancellation of its +/// subscription has completed. +/// {@endtemplate} +/// /// TODO: Finish documentation of a public identifiers, so far the folliwng /// items have been documented: /// * [fillRandomBytes] diff --git a/test/aes_ctr_counter_wrap_test.dart b/test/aes_ctr_counter_wrap_test.dart index 3ab785876..22dc500f2 100644 --- a/test/aes_ctr_counter_wrap_test.dart +++ b/test/aes_ctr_counter_wrap_test.dart @@ -64,62 +64,41 @@ void main() { expect(streamedCiphertext, equals(ciphertext)); }); - test('AES-CTR byte operations snapshot the counter', () async { + test('AES-CTR byte operations leave inputs unchanged across wrap', () async { final key = await AesCtrSecretKey.importRawKey(Uint8List(16)); - final plaintext = Uint8List.fromList([1, 2, 3]); - final original = Uint8List(16); - final expected = await key.encryptBytes(plaintext, original, 128); - - final encryptCounter = Uint8List(16); - final encrypting = key.encryptBytes(plaintext, encryptCounter, 128); - encryptCounter[15] = 1; - expect(await encrypting, equals(expected)); - - final decryptCounter = Uint8List(16); - final decrypting = key.decryptBytes(expected, decryptCounter, 128); - decryptCounter[15] = 1; - expect(await decrypting, equals(plaintext)); + final plaintext = Uint8List(8193); + final original = Uint8List.fromList(plaintext); + final counter = Uint8List(16) + ..[14] = 0xff + ..[15] = 0xff; + final originalCounter = Uint8List.fromList(counter); + final ciphertext = await key.encryptBytes(plaintext, counter, 16); + expect(plaintext, equals(original)); + expect(counter, equals(originalCounter)); + final savedCiphertext = Uint8List.fromList(ciphertext); + expect(await key.decryptBytes(ciphertext, counter, 16), equals(original)); + expect(ciphertext, equals(savedCiphertext)); + expect(counter, equals(originalCounter)); }); - test('AES-CTR stream operations snapshot the counter', () async { + test('AES-CTR stream operations leave the counter unchanged', () async { final key = await AesCtrSecretKey.importRawKey(Uint8List(16)); - final plaintext = Uint8List.fromList([1, 2, 3]); - final expected = await key.encryptBytes(plaintext, Uint8List(16), 128); - - final encryptCounter = Uint8List(16); - final encrypting = key.encryptStream( - Stream.value(plaintext), - encryptCounter, - 128, + final plaintext = Uint8List(8193); + final counter = Uint8List(16) + ..[14] = 0xff + ..[15] = 0xff; + final originalCounter = Uint8List.fromList(counter); + final expected = await key.encryptBytes(plaintext, counter, 16); + final encrypted = await _collectBytes( + key.encryptStream(Stream.value(plaintext), counter, 16), ); - encryptCounter[15] = 1; - expect(await _collectBytes(encrypting), equals(expected)); - - final decryptCounter = Uint8List(16); - final decrypting = key.decryptStream( - Stream.value(expected), - decryptCounter, - 128, + expect(encrypted, equals(expected)); + expect(counter, equals(originalCounter)); + final decrypted = await _collectBytes( + key.decryptStream(Stream.value(encrypted), counter, 16), ); - decryptCounter[15] = 1; - expect(await _collectBytes(decrypting), equals(plaintext)); - }); - - test('AES-CTR byte operations snapshot the input', () async { - final key = await AesCtrSecretKey.importRawKey(Uint8List(16)); - final plaintext = Uint8List.fromList([1, 2, 3]); - final counter = Uint8List(16); - final ciphertext = await key.encryptBytes(plaintext, counter, 128); - - final encryptInput = Uint8List.fromList(plaintext); - final encrypting = key.encryptBytes(encryptInput, counter, 128); - encryptInput[0] ^= 1; - expect(await encrypting, equals(ciphertext)); - - final decryptInput = Uint8List.fromList(ciphertext); - final decrypting = key.decryptBytes(decryptInput, counter, 128); - decryptInput[0] ^= 1; - expect(await decrypting, equals(plaintext)); + expect(decrypted, equals(plaintext)); + expect(counter, equals(originalCounter)); }); }