Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,3 +43,8 @@ android {
flutter {
source = "../.."
}

dependencies {
androidTestImplementation("androidx.test:runner:1.2.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0")
}
Original file line number Diff line number Diff line change
@@ -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<MainActivity> =
ActivityTestRule(MainActivity::class.java, true, false)
}
Original file line number Diff line number Diff line change
@@ -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<ffi.UintPtr>(), 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<int> _readWord() {
final word = BN_new();
expect(word, isNot(ffi.nullptr));
try {
final bytes = OPENSSL_malloc(8).cast<ffi.Uint8>();
expect(bytes, isNot(ffi.nullptr));
try {
expect(BN_set_word(word, 65537), 1);
expect(BN_bn2bin_padded(bytes, 8, word), 1);
return List<int>.from(bytes.asTypedList(8));
} finally {
OPENSSL_free(bytes.cast());
}
} finally {
BN_free(word);
}
}
4 changes: 1 addition & 3 deletions lib/src/third_party/boringssl/generated_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ external int BN_num_bytes(ffi.Pointer<BIGNUM> bn);

/// BN_set_word sets |bn| to |value|. It returns one on success or zero on
/// allocation failure.
@ffi.Native<ffi.Int Function(ffi.Pointer<BIGNUM>, BN_ULONG)>(
@ffi.Native<ffi.Int Function(ffi.Pointer<BIGNUM>, ffi.UintPtr)>(
symbol: 'webcrypto_BN_set_word',
)
external int BN_set_word(ffi.Pointer<BIGNUM> bn, int value);
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions test/ffi/bn_word_abi_test.dart
Original file line number Diff line number Diff line change
@@ -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', () {
Comment thread
harrshita123 marked this conversation as resolved.
Resolved
final word = BN_new();
expect(word, isNot(ffi.nullptr));
final bytes = calloc<ffi.Uint8>(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);
}
Comment thread
harrshita123 marked this conversation as resolved.
Resolved
});
}
59 changes: 36 additions & 23 deletions tool/ffigen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading