Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't generate keys on Android #7

Closed
cremfert opened this issue Nov 9, 2021 · 13 comments
Closed

Can't generate keys on Android #7

cremfert opened this issue Nov 9, 2021 · 13 comments
Assignees
Labels
bug Something isn't working

Comments

@cremfert
Copy link

cremfert commented Nov 9, 2021

I try to generate a secure key by using:
await sodium.crypto.secretBox.keygen(); in Flutter (using sodium_libs: ^1.1.1).
This works fine on iOS, but fails with an error on Android:
RangeError (RangeError (count): Invalid value: Not greater than or equal to 0: -5365866809427754976)

How can I fix this?

Thanks a lot!

@Skycoder42 Skycoder42 self-assigned this Nov 10, 2021
@Skycoder42 Skycoder42 added the bug Something isn't working label Nov 10, 2021
@Skycoder42
Copy link
Owner

Interesting. I have an idea where this could come from, but am not quite sure yet. Could you give me a stack trace? Specifically the parts regarding libsodium are important for me.

@cremfert
Copy link
Author

Yes sure, here it is:

E/flutter (12905): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: RangeError (count): Invalid value: Not greater than or equal to 0: -5381629408123551712
E/flutter (12905): #0      RangeError.checkNotNegative (dart:core/errors.dart:356:7)
E/flutter (12905): #1      new SodiumPointer.alloc
package:sodium/…/bindings/sodium_pointer.dart:58
E/flutter (12905): #2      new SecureKeyFFI.alloc
package:sodium/…/api/secure_key_ffi.dart:30
E/flutter (12905): #3      KeygenMixin.keygenImpl
package:sodium/…/helpers/keygen_mixin.dart:21
E/flutter (12905): #4      SecretBoxFFI.keygen
package:sodium/…/api/secret_box_ffi.dart:32
E/flutter (12905): #5      EncryptionV2Service.generateKey

(Last line is my function that includes the keygen() command.

Thanks for your help!

@Skycoder42
Copy link
Owner

Skycoder42 commented Nov 12, 2021

Could you please add the following to your code, right before the SecretBoxFFI.keygen method and send me the result?

print(mySecretBox.keyBytes)

I cannot test on Android myself for the next few days, so that would help me working on this earlier.

@Skycoder42
Copy link
Owner

Also, which Android version and Architecture are you testing on

@Skycoder42
Copy link
Owner

I cannot reproduce your issue on my arm x64 device. I think I need more details about the exact device you are testing on

@cremfert
Copy link
Author

cremfert commented Nov 18, 2021

Sorry for being late - I'm developing my app on an Android Emulator with API 30.
The specs are:

Name: Pixel_3a_API_30_x86_1
CPU/ABI: Google APIs Intel Atom (x86)
Target: google_apis [Google APIs] (API level 30)
Skin: pixel_3a
SD Card: 800 MB
fastboot.chosenSnapshotFile: 
runtime.network.speed: full
hw.accelerometer: yes
hw.device.name: pixel_3a
hw.lcd.width: 1080
hw.initialOrientation: Portrait
image.androidVersion.api: 30
tag.id: google_apis
hw.mainKeys: no
hw.camera.front: emulated
avd.ini.displayname: Pixel_3a_API_30_x86_1
hw.gpu.mode: auto
hw.ramSize: 1536
PlayStore.enabled: false
fastboot.forceColdBoot: no
hw.cpu.ncore: 4
hw.keyboard: yes
hw.sensors.proximity: yes
hw.dPad: no
hw.lcd.height: 2220
vm.heapSize: 256
skin.dynamic: yes
hw.device.manufacturer: Google
hw.gps: yes
hw.audioInput: yes
image.sysdir.1: system-images/android-30/google_apis/x86/
showDeviceFrame: yes
hw.camera.back: virtualscene
AvdId: Pixel_3a_API_30_x86_1
hw.lcd.density: 440
hw.arc: false
fastboot.forceChosenSnapshotBoot: no
fastboot.forceFastBoot: yes
hw.trackBall: no
hw.battery: yes
hw.sdCard: yes
tag.display: Google APIs
runtime.network.latency: none
disk.dataPartition.size: 6G
hw.sensors.orientation: yes
avd.ini.encoding: UTF-8
hw.gpu.enabled: yes

I will have a chance to give you the keybytes output in the next days (maybe on the weekend).

@Skycoder42
Copy link
Owner

Skycoder42 commented Nov 19, 2021

I can confirm this happening and I do know why. The native C-Bindings for libsodium have been generated on a x64 machine, with size_t beeing resolved to UInt64. This of course does not work on a 32 bit system. For know, the easiest way around this is to create yourself a 64 bit emulator.

Regarding deployment to actuald devices, arm64 and intel x64 devices are fine, but older armv7 devices would not work. I will try to find a way around this issue

@Skycoder42
Copy link
Owner

I was able to confirm this. When I replace the "size_t" typedef in the generated bindings with "Uint32", the library works! Know I need to find a way to get the correct bindings based on architecture (which is not as easy as it sounds, as dart typically does not provide this information)

@Skycoder42
Copy link
Owner

Okay so I manage to get it to work by enforcing "size_t" to be interpreted as "IntPtr" - this works in regards of sizing, as for all supported system IntPtr has the same size as size_t, however, this messes up signed-ness, as IntPtr is signed, while size_t is not. I fixed things up so all test should succeed, but I have to walk through the whole library again to ensure all sizes are correctly interpreted.

However, the current status should already be good enough to test out if it works for you as well, so if you have the time, please check out the latest master of sodium and overwrite it in your pubspec.

@cremfert
Copy link
Author

Thanks a lot for your effort and your work - I really appreciate your help! I will have a chance to test next week and will provide feedback.

@Skycoder42
Copy link
Owner

I assume everything worked? If not, please reopen

@cremfert
Copy link
Author

Sorry, forgot to answer - yes, everything works! Thanks for your effort. I was just curious, if you also implemented the helper methods for conversion, like bin2hex and hex2bin etc.?

@Skycoder42
Copy link
Owner

No, i did not implement the helpers, as dart itself provides similar functions, which are much more efficient as no conversion between dart and the native code is required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants