diff --git a/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts b/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts index a859370cf..e3e966244 100644 --- a/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts +++ b/example/webcrypto_demo_flutter_app/android/app/build.gradle.kts @@ -22,6 +22,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.example.webcrypto_example" + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdk = flutter.minSdkVersion @@ -42,3 +43,8 @@ android { flutter { source = "../.." } + +dependencies { + androidTestImplementation("androidx.test:runner:1.2.0") + androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0") +} diff --git a/example/webcrypto_demo_flutter_app/android/app/src/androidTest/kotlin/com/example/webcrypto_example/MainActivityTest.kt b/example/webcrypto_demo_flutter_app/android/app/src/androidTest/kotlin/com/example/webcrypto_example/MainActivityTest.kt new file mode 100644 index 000000000..8eabb7812 --- /dev/null +++ b/example/webcrypto_demo_flutter_app/android/app/src/androidTest/kotlin/com/example/webcrypto_example/MainActivityTest.kt @@ -0,0 +1,14 @@ +package com.example.webcrypto_example + +import androidx.test.rule.ActivityTestRule +import dev.flutter.plugins.integration_test.FlutterTestRunner +import org.junit.Rule +import org.junit.runner.RunWith + +@RunWith(FlutterTestRunner::class) +class MainActivityTest { + @Rule + @JvmField + val rule: ActivityTestRule = + ActivityTestRule(MainActivity::class.java, true, false) +} diff --git a/example/webcrypto_demo_flutter_app/integration_test/bn_word_abi_32_test.dart b/example/webcrypto_demo_flutter_app/integration_test/bn_word_abi_32_test.dart new file mode 100644 index 000000000..a028742b7 --- /dev/null +++ b/example/webcrypto_demo_flutter_app/integration_test/bn_word_abi_32_test.dart @@ -0,0 +1,59 @@ +// 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:ffi' as ffi; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:webcrypto/webcrypto.dart'; +import 'package:webcrypto/src/third_party/boringssl/generated_bindings.dart'; + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + test('BN_set_word uses the 32-bit native argument width', () { + expect(ffi.Abi.current(), ffi.Abi.androidArm); + expect(ffi.sizeOf(), 4); + + expect(_readWord(), [0, 0, 0, 0, 0, 1, 0, 1]); + }); + + test('RSA-OAEP generates a key with exponent 65537', () async { + final pair = await RsaOaepPrivateKey.generateKey( + 2048, + BigInt.from(65537), + Hash.sha256, + ); + final publicJwk = await pair.publicKey.exportJsonWebKey(); + expect(publicJwk['e'], 'AQAB'); + }); +} + +List _readWord() { + final word = BN_new(); + expect(word, isNot(ffi.nullptr)); + try { + final bytes = OPENSSL_malloc(8).cast(); + expect(bytes, isNot(ffi.nullptr)); + try { + expect(BN_set_word(word, 65537), 1); + expect(BN_bn2bin_padded(bytes, 8, word), 1); + return List.from(bytes.asTypedList(8)); + } finally { + OPENSSL_free(bytes.cast()); + } + } finally { + BN_free(word); + } +} diff --git a/lib/src/third_party/boringssl/generated_bindings.dart b/lib/src/third_party/boringssl/generated_bindings.dart index b1fdb5da8..f8d3f5ddb 100644 --- a/lib/src/third_party/boringssl/generated_bindings.dart +++ b/lib/src/third_party/boringssl/generated_bindings.dart @@ -145,7 +145,7 @@ external int BN_num_bytes(ffi.Pointer bn); /// BN_set_word sets |bn| to |value|. It returns one on success or zero on /// allocation failure. -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'webcrypto_BN_set_word', ) external int BN_set_word(ffi.Pointer bn, int value); @@ -1758,8 +1758,6 @@ const int AES_BLOCK_SIZE = 16; typedef BIGNUM = bignum_st; typedef BN_CTX = bignum_ctx; typedef BN_GENCB = bn_gencb_st; -typedef BN_ULONG = ffi.Uint64; -typedef DartBN_ULONG = int; typedef CBB = cbb_st; typedef CBS = cbs_st; typedef ECDSA_SIG = ecdsa_sig_st; diff --git a/test/ffi/bn_word_abi_test.dart b/test/ffi/bn_word_abi_test.dart new file mode 100644 index 000000000..45022a810 --- /dev/null +++ b/test/ffi/bn_word_abi_test.dart @@ -0,0 +1,38 @@ +// 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. + +@TestOn('vm') +library; + +import 'dart:ffi' as ffi; + +import 'package:ffi/ffi.dart'; +import 'package:test/test.dart'; +import 'package:webcrypto/src/third_party/boringssl/generated_bindings.dart'; + +void main() { + test('BN_set_word passes a native-width value to BoringSSL', () { + final word = BN_new(); + expect(word, isNot(ffi.nullptr)); + final bytes = calloc(3); + try { + expect(BN_set_word(word, 65537), 1); + expect(BN_bn2bin_padded(bytes, 3, word), 1); + expect(bytes.asTypedList(3), [1, 0, 1]); + } finally { + calloc.free(bytes); + BN_free(word); + } + }); +} diff --git a/tool/ffigen.dart b/tool/ffigen.dart index 9408a6764..4c5bc143b 100644 --- a/tool/ffigen.dart +++ b/tool/ffigen.dart @@ -196,29 +196,42 @@ void generateBoringSslBindings(Uri packageRoot) { include: Declarations.includeSet({'cbs_st', 'cbb_st'}), dependencies: CompoundDependencies.opaque, ), - typedefs: Typedefs.includeSet({ - 'BIGNUM', - 'BN_CTX', - 'BN_GENCB', - 'BN_ULONG', - 'CBB', - 'CBS', - 'ECDSA_SIG', - 'EC_GROUP', - 'EC_KEY', - 'EC_POINT', - 'ENGINE', - 'EVP_AEAD', - 'EVP_AEAD_CTX', - 'EVP_CIPHER', - 'EVP_CIPHER_CTX', - 'EVP_MD', - 'EVP_MD_CTX', - 'EVP_PKEY', - 'EVP_PKEY_CTX', - 'HMAC_CTX', - 'RSA', - }), + // BoringSSL uses a pointer-sized unsigned word on both 32-bit and 64-bit + // targets. Generation runs on a 64-bit host, so map this typedef explicitly. + typedefs: Typedefs( + // ignore: deprecated_member_use + imported: [ + ImportedType( + const LibraryImport('ffi', 'dart:ffi'), + 'UintPtr', + 'int', + 'BN_ULONG', + ), + ], + include: Declarations.includeSet({ + 'BIGNUM', + 'BN_CTX', + 'BN_GENCB', + 'BN_ULONG', + 'CBB', + 'CBS', + 'ECDSA_SIG', + 'EC_GROUP', + 'EC_KEY', + 'EC_POINT', + 'ENGINE', + 'EVP_AEAD', + 'EVP_AEAD_CTX', + 'EVP_CIPHER', + 'EVP_CIPHER_CTX', + 'EVP_MD', + 'EVP_MD_CTX', + 'EVP_PKEY', + 'EVP_PKEY_CTX', + 'HMAC_CTX', + 'RSA', + }), + ), functions: Functions( include: Declarations.includeSet({ // Source of truth for BoringSSL entry points bound from Dart.