Skip to content

fix(ffi): use native word width for BN_set_word - #432

Merged
HamdaanAliQuatil merged 3 commits into
google:masterfrom
harrshita123:fix/rsa-native-word-abi
Oct 11, 2026
Merged

HamdaanAliQuatil merged 3 commits into
google:masterfrom
harrshita123:fix/rsa-native-word-abi

Conversation

@harrshita123

Copy link
Copy Markdown
Contributor

Summary

BoringSSL defines BN_ULONG as a 32-bit type on 32-bit targets and a 64-bit type on 64-bit targets. The checked-in bindings were generated on a 64-bit host and used ffi.Uint64 for BN_set_word on every target. Map this typedef to ffi.UintPtr in the binding generator and regenerate the binding so the FFI argument follows the target's native word width.

Add a focused native test that sets a BIGNUM to the RSA public exponent 65537 and reads the bytes back.

Validation

  • Formatting and static analysis pass for the changed files.
  • The new FFI test passes on macOS arm64.
  • The existing 2048-bit RSA-OAEP case passes (50 tests).

The reported failure in #136 has not been reproduced on a 32-bit Android target here. This PR is a draft pending that validation. It addresses the ABI mismatch identified in the issue discussion; it does not establish that every reported TOO_MANY_ITERATIONS failure has the same cause.

Related: #136. The pending #417 replaces these bindings, so this correction may need to move to that dependency if #417 lands first.

@harrshita123
harrshita123 marked this pull request as ready for review October 9, 2026 14:27
Comment thread test/ffi/bn_word_abi_test.dart Resolved
Comment thread test/ffi/bn_word_abi_test.dart Resolved
@harrshita123

Copy link
Copy Markdown
Contributor Author

Implemented in b5dce7d.

The permanent Android integration test now contains only the corrected UintPtr binding. It keeps the Android ARM32 and ffi.sizeOf<ffi.UintPtr>() == 4 checks, verifies the BN_set_word(65537) bytes, and generates an RSA-OAEP key with public exponent 65537.

ARM32 device validation

Corrected binding, run through Firebase Test Lab on Galaxy A10, API 29 (armeabi-v7a):

  • Test Lab execution: Passed in 53 seconds.
  • The integration test confirmed Android ARM and ffi.sizeOf<ffi.UintPtr>() == 4.
  • The BIGNUM bytes matched [0, 0, 0, 0, 0, 1, 0, 1] for 65537.
  • RSA-OAEP key generation passed and the exported public JWK exponent was AQAB.
  • The device log contains both the ABI test and All tests passed!.

Corrected run and logs

Separate old-binding comparison

I ran the previous Uint64 binding only in a disposable local copy on the same Galaxy A10 API 29 target. It is not present in this PR's permanent test.

  • Observed BIGNUM bytes: [0, 0, 0, 0, 175, 203, 98, 137]
  • Observed RSA public JWK exponent: r8tiiQ

This comparison is evidence from that run only. The mismatched native call has no guaranteed behavior, which is why the permanent test asserts only the corrected binding.

Separate old-binding run log

Local Android validation also passed:

./gradlew app:assembleAndroidTest
BUILD SUCCESSFUL

@HamdaanAliQuatil HamdaanAliQuatil left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great! thank you @harrshita123 we should take a patch release with this.

cc: @jonasfj

@HamdaanAliQuatil
HamdaanAliQuatil merged commit da6648c into google:master Oct 11, 2026
15 checks passed
mosuem added a commit to mosuem/webcrypto.dart that referenced this pull request Nov 17, 2026
Conflict resolution:
- Keep the deletion of hook/build.dart, src/CMakeLists.txt,
  third_party/boringssl/, the generated bindings, tool/ffigen.dart and
  tool/bump-boringssl-revision.sh. Their master-side fixes are now
  package:boring's concern: it already includes the fiat ADX assembly on
  Apple x86_64 (google#448), and tracks its own build inputs (google#429).
- Keep _checkUnsignedLong and _checkInt32 from master in impl_ffi.utils.dart.
- Drop ffigen from dev_dependencies, keep master's new dev_dependencies.
- Remove test/build_dependencies_test.dart, which tested the removed
  hook/build.dart.

BN_set_word (google#432): package:boring binds BN_ULONG as Uint64, which is
wrong on 32-bit targets, so this would regress RSA key generation on
Android ARM32. Use BN_set_u64, whose argument is 64 bits wide everywhere.
Remove test/ffi/bn_word_abi_test.dart and
integration_test/bn_word_abi_32_test.dart, which tested webcrypto's own
binding. integration_test/rsa_key_generation_test.dart still covers the
exponent end to end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants