Skip to content

Commit

Permalink
get sodium working on 32 bit systems (should help with #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skycoder42 committed Nov 20, 2021
1 parent 07d8e10 commit 880af13
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/sodium/lib/src/ffi/api/pwhash_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class PwhashFFI with PwHashValidations implements Pwhash {
@override
int get bytesMin => sodium.crypto_pwhash_bytes_min();
@override
int get bytesMax => sodium.crypto_pwhash_bytes_max();
int get bytesMax =>
sodium.crypto_pwhash_bytes_max().toUnsigned(sizeOf<IntPtr>() * 8);

@override
int get memLimitMin => sodium.crypto_pwhash_memlimit_min();
Expand All @@ -32,7 +33,8 @@ class PwhashFFI with PwHashValidations implements Pwhash {
@override
int get memLimitSensitive => sodium.crypto_pwhash_memlimit_sensitive();
@override
int get memLimitMax => sodium.crypto_pwhash_memlimit_max();
int get memLimitMax =>
sodium.crypto_pwhash_memlimit_max().toUnsigned(sizeOf<IntPtr>() * 8);

@override
int get opsLimitMin => sodium.crypto_pwhash_opslimit_min();
Expand All @@ -43,12 +45,14 @@ class PwhashFFI with PwHashValidations implements Pwhash {
@override
int get opsLimitSensitive => sodium.crypto_pwhash_opslimit_sensitive();
@override
int get opsLimitMax => sodium.crypto_pwhash_opslimit_max();
int get opsLimitMax =>
sodium.crypto_pwhash_opslimit_max().toUnsigned(sizeOf<IntPtr>() * 8);

@override
int get passwdMin => sodium.crypto_pwhash_passwd_min();
@override
int get passwdMax => sodium.crypto_pwhash_passwd_max();
int get passwdMax =>
sodium.crypto_pwhash_passwd_max().toUnsigned(sizeOf<IntPtr>() * 8);

@override
int get saltBytes => sodium.crypto_pwhash_saltbytes();
Expand Down
4 changes: 2 additions & 2 deletions packages/sodium/lib/src/ffi/api/sodium_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SodiumFFI implements Sodium {
Uint8List pad(Uint8List buf, int blocksize) {
final maxLen = buf.length + blocksize;
SodiumPointer<Uint8>? extendedBuffer;
SodiumPointer<Uint64>? paddedLength;
SodiumPointer<size_t>? paddedLength;
try {
extendedBuffer = SodiumPointer.alloc(sodium, count: maxLen)..fill(buf);
paddedLength = SodiumPointer.alloc(sodium, zeroMemory: true);
Expand All @@ -56,7 +56,7 @@ class SodiumFFI implements Sodium {
@override
Uint8List unpad(Uint8List buf, int blocksize) {
SodiumPointer<Uint8>? extendedBuffer;
SodiumPointer<Uint64>? unpaddedLength;
SodiumPointer<size_t>? unpaddedLength;
try {
extendedBuffer = buf.toSodiumPointer(
sodium,
Expand Down
2 changes: 1 addition & 1 deletion packages/sodium/lib/src/ffi/bindings/libsodium.ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10599,7 +10599,7 @@ class __fsid_t extends ffi.Struct {
external ffi.Array<ffi.Int32> __val;
}

typedef size_t = ffi.Uint64;
typedef size_t = ffi.IntPtr;

class crypto_aead_aes256gcm_state_ extends ffi.Struct {
@ffi.Array.multi([512])
Expand Down
2 changes: 2 additions & 0 deletions packages/sodium/lib/src/ffi/bindings/sodium_pointer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ abstract class _StaticallyTypedSizeOf {
return sizeOf<Float>();
case Double:
return sizeOf<Double>();
case IntPtr:
return sizeOf<IntPtr>();
default:
throw UnsupportedError(
'Cannot create a SodiumPointer for $T. T must be a primitive type',
Expand Down
10 changes: 8 additions & 2 deletions packages/sodium/test/integration/cases/pwhash_test_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import '../test_case.dart';
import '../test_runner.dart';

class PwhashTestCase extends TestCase {
PwhashTestCase(TestRunner runner) : super(runner);
final bool is32Bit;

PwhashTestCase(TestRunner runner, {this.is32Bit = false}) : super(runner);

@override
String get name => 'pwhash';
Expand All @@ -24,7 +26,11 @@ class PwhashTestCase extends TestCase {
expect(sut.memLimitInteractive, 67108864, reason: 'memLimitInteractive');
expect(sut.memLimitModerate, 268435456, reason: 'memLimitModerate');
expect(sut.memLimitSensitive, 1073741824, reason: 'memLimitSensitive');
expect(sut.memLimitMax, 4398046510080, reason: 'memLimitMax');
expect(
sut.memLimitMax,
is32Bit ? 2147483648 : 4398046510080,
reason: 'memLimitMax',
);

expect(sut.opsLimitMin, 1, reason: 'opsLimitMin');
expect(sut.opsLimitInteractive, 2, reason: 'opsLimitInteractive');
Expand Down
7 changes: 5 additions & 2 deletions packages/sodium/test/integration/test_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ abstract class TestRunner {

final bool isSumoTest;

bool get is32Bit => false;

TestRunner({
required this.isSumoTest,
});
Expand All @@ -50,7 +52,7 @@ abstract class TestRunner {
SignTestCase(this),
GenericHashTestCase(this),
ShortHashTestCase(this),
PwhashTestCase(this),
PwhashTestCase(this, is32Bit: is32Bit),
KdfTestCase(this),
KxTestCase(this),
];
Expand Down Expand Up @@ -89,7 +91,8 @@ abstract class TestRunner {

// ignore: avoid_print
print(
'Running integration tests with libsodium version: ${_sodium.version}',
'Running integration tests with libsodium version: ${_sodium.version} '
'(${is32Bit ? '32 bit' : '64 bit'})',
);
});

Expand Down
3 changes: 3 additions & 0 deletions packages/sodium/test/integration/vm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import 'test_runner.dart';
class VmTestRunner extends TestRunner {
VmTestRunner() : super(isSumoTest: true);

@override
bool get is32Bit => sizeOf<IntPtr>() == 4;

@override
Future<Sodium> loadSodium() async {
String libSodiumPath;
Expand Down
6 changes: 6 additions & 0 deletions packages/sodium_libs/example/integration_test/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import 'package:sodium_libs_example/main.dart' show MyApp;

import '../../../sodium/test/integration/test_runner.dart';

import 'arch_detection_fallback.dart'
if (dart.library.ffi) 'arch_detection_ffi.dart' as arch;

class FlutterTestRunner extends TestRunner {
FlutterTestRunner() : super(isSumoTest: true);

@override
bool get is32Bit => arch.is32Bit;

@override
Future<Sodium> loadSodium() => SodiumInit.init();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const is32Bit = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'dart:ffi';

final is32Bit = sizeOf<IntPtr>() == 4;

0 comments on commit 880af13

Please sign in to comment.