Skip to content

Commit

Permalink
use typedefs for JS states
Browse files Browse the repository at this point in the history
  • Loading branch information
Skycoder42 committed May 27, 2021
1 parent 3c4660c commit f011d73
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import '../../../bindings/sodium.js.dart';
import '../../../bindings/to_safe_int.dart';
import 'secret_stream_message_tag_jsx.dart';

class SecretStreamPullTransformerSinkJS
extends SecretStreamPullTransformerSink<num> {
class SecretStreamPullTransformerSinkJS extends SecretStreamPullTransformerSink<
SecretstreamXchacha20poly1305State> {
final LibSodiumJS sodium;

SecretStreamPullTransformerSinkJS(
Expand All @@ -26,7 +26,7 @@ class SecretStreamPullTransformerSinkJS
sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES.toSafeUInt32();

@override
num initialize(
SecretstreamXchacha20poly1305State initialize(
SecureKey key,
Uint8List header,
) =>
Expand All @@ -40,14 +40,14 @@ class SecretStreamPullTransformerSinkJS
);

@override
void rekey(num cryptoState) => JsError.wrap(
void rekey(SecretstreamXchacha20poly1305State cryptoState) => JsError.wrap(
// ignore result, as it is always true
() => sodium.crypto_secretstream_xchacha20poly1305_rekey(cryptoState),
);

@override
SecretStreamPlainMessage decryptMessage(
num cryptoState,
SecretstreamXchacha20poly1305State cryptoState,
SecretStreamCipherMessage event,
) {
final dynamic pullResult = JsError.wrap<dynamic>(
Expand Down Expand Up @@ -76,11 +76,12 @@ class SecretStreamPullTransformerSinkJS
}

@override
void disposeState(num cryptoState) {}
void disposeState(SecretstreamXchacha20poly1305State cryptoState) {}
}

@internal
class SecretStreamPullTransformerJS extends SecretStreamPullTransformer<num> {
class SecretStreamPullTransformerJS
extends SecretStreamPullTransformer<SecretstreamXchacha20poly1305State> {
final LibSodiumJS sodium;

const SecretStreamPullTransformerJS(
Expand All @@ -91,6 +92,7 @@ class SecretStreamPullTransformerJS extends SecretStreamPullTransformer<num> {
) : super(key, requireFinalized);

@override
SecretStreamPullTransformerSink<num> createSink(bool requireFinalized) =>
SecretStreamPullTransformerSinkJS(sodium, requireFinalized);
SecretStreamPullTransformerSink<SecretstreamXchacha20poly1305State>
createSink(bool requireFinalized) =>
SecretStreamPullTransformerSinkJS(sodium, requireFinalized);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import '../../../bindings/sodium.js.dart';
import 'secret_stream_message_tag_jsx.dart';

@internal
class SecretStreamPushTransformerSinkJS
extends SecretStreamPushTransformerSink<num> {
class SecretStreamPushTransformerSinkJS extends SecretStreamPushTransformerSink<
SecretstreamXchacha20poly1305State> {
final LibSodiumJS sodium;

SecretStreamPushTransformerSinkJS(this.sodium);

@override
InitPushResult<num> initialize(SecureKey key) {
InitPushResult<SecretstreamXchacha20poly1305State> initialize(SecureKey key) {
final initResult = JsError.wrap(
() => key.runUnlockedSync(
(keyData) => sodium.crypto_secretstream_xchacha20poly1305_init_push(
Expand All @@ -32,14 +32,14 @@ class SecretStreamPushTransformerSinkJS
}

@override
void rekey(num cryptoState) => JsError.wrap(
void rekey(SecretstreamXchacha20poly1305State cryptoState) => JsError.wrap(
// always returns true, ignore result
() => sodium.crypto_secretstream_xchacha20poly1305_rekey(cryptoState),
);

@override
SecretStreamCipherMessage encryptMessage(
num cryptoState,
SecretstreamXchacha20poly1305State cryptoState,
SecretStreamPlainMessage event,
) {
final cipherText = JsError.wrap(
Expand All @@ -58,16 +58,17 @@ class SecretStreamPushTransformerSinkJS
}

@override
void disposeState(num cryptoState) {}
void disposeState(SecretstreamXchacha20poly1305State cryptoState) {}
}

@internal
class SecretStreamPushTransformerJS extends SecretStreamPushTransformer<num> {
class SecretStreamPushTransformerJS
extends SecretStreamPushTransformer<SecretstreamXchacha20poly1305State> {
final LibSodiumJS sodium;

const SecretStreamPushTransformerJS(this.sodium, SecureKey key) : super(key);

@override
SecretStreamPushTransformerSink<num> createSink() =>
SecretStreamPushTransformerSinkJS(sodium);
SecretStreamPushTransformerSink<SecretstreamXchacha20poly1305State>
createSink() => SecretStreamPushTransformerSinkJS(sodium);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ mixin SignConsumerJSMixin<T extends Object>
LibSodiumJS get sodium;

final _signatureCompleter = Completer<T>();
late final num _state;
late final SignState _state;

@protected
Future<T> get result => _signatureCompleter.future;

@protected
T finalize(num state);
T finalize(SignState state);

@protected
void initState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SignatureConsumerJS
Future<Uint8List> get signature => result;

@override
Uint8List finalize(num state) => JsError.wrap(
Uint8List finalize(SignState state) => JsError.wrap(
() => secretKey.runUnlockedSync(
(secretKeyData) => sodium.crypto_sign_final_create(
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class VerificationConsumerJS
Future<bool> get signatureValid => result;

@override
bool finalize(num state) => JsError.wrap(
bool finalize(SignState state) => JsError.wrap(
() => sodium.crypto_sign_final_verify(
state,
signature,
Expand Down
58 changes: 21 additions & 37 deletions packages/sodium/lib/src/js/bindings/sodium.js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ library sodium.js;
import 'dart:typed_data';

import 'package:js/js.dart';
import 'package:meta/meta.dart';

typedef SecretstreamXchacha20poly1305State = num;

typedef SignState = num;

typedef GenerichashState = num;

typedef HashSha256State = num;

typedef HashSha512State = num;

typedef OnetimeauthState = num;

@JS()
@anonymous
Expand Down Expand Up @@ -82,34 +93,6 @@ class SecretStreamPull {
});
}

@JS()
@anonymous
class GenerichashState {
@visibleForTesting
external factory GenerichashState();
}

@JS()
@anonymous
class HashSha256State {
@visibleForTesting
external factory HashSha256State();
}

@JS()
@anonymous
class HashSha512State {
@visibleForTesting
external factory HashSha512State();
}

@JS()
@anonymous
class OnetimeauthState {
@visibleForTesting
external factory OnetimeauthState();
}

@JS()
@anonymous
class LibSodiumJS {
Expand Down Expand Up @@ -1141,7 +1124,8 @@ class LibSodiumJS {
Uint8List key,
);

external num crypto_secretstream_xchacha20poly1305_init_pull(
external SecretstreamXchacha20poly1305State
crypto_secretstream_xchacha20poly1305_init_pull(
Uint8List header,
Uint8List key,
);
Expand All @@ -1153,20 +1137,20 @@ class LibSodiumJS {
external Uint8List crypto_secretstream_xchacha20poly1305_keygen();

external dynamic crypto_secretstream_xchacha20poly1305_pull(
num state_address,
SecretstreamXchacha20poly1305State state_address,
Uint8List cipher,
Uint8List? ad,
);

external Uint8List crypto_secretstream_xchacha20poly1305_push(
num state_address,
SecretstreamXchacha20poly1305State state_address,
Uint8List message_chunk,
Uint8List? ad,
num tag,
);

external bool crypto_secretstream_xchacha20poly1305_rekey(
num state_address,
SecretstreamXchacha20poly1305State state_address,
);

external Uint8List crypto_shorthash(
Expand Down Expand Up @@ -1208,17 +1192,17 @@ class LibSodiumJS {
);

external Uint8List crypto_sign_final_create(
num state_address,
SignState state_address,
Uint8List privateKey,
);

external bool crypto_sign_final_verify(
num state_address,
SignState state_address,
Uint8List signature,
Uint8List publicKey,
);

external num crypto_sign_init();
external SignState crypto_sign_init();

external KeyPair crypto_sign_keypair();

Expand All @@ -1232,7 +1216,7 @@ class LibSodiumJS {
);

external void crypto_sign_update(
num state_address,
SignState state_address,
Uint8List message_chunk,
);

Expand Down
8 changes: 4 additions & 4 deletions packages/sodium/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ environment:
sdk: '>=2.13.0 <3.0.0'

dependencies:
ffi: ^1.0.0
ffi: ^1.1.0
freezed_annotation: ^0.14.2
js: ^0.6.3
meta: ^1.3.0
meta: ^1.4.0

dev_dependencies:
build_runner: ^2.0.4
coverage: ^1.0.2
coverage: ^1.0.3
dart_pre_commit: ^2.3.1
ffigen: ^3.0.0
freezed: ^0.14.2
lint: ^1.5.3
mocktail: ^0.1.3
path: ^1.8.0
test: ^1.17.4
test: ^1.17.5
tuple: ^2.0.0

ffigen:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class LibraryGenerator {
..writeln("import 'dart:typed_data';")
..writeln()
..writeln("import 'package:js/js.dart';")
..writeln("import 'package:meta/meta.dart';")
..writeln();
}

Expand Down
50 changes: 25 additions & 25 deletions packages/sodium/tool/libsodium_js_generator/type_mappings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import 'file_loader.dart';

class TypeInfo {
final String dartType;
final String? template;
final String? typeDef;
final bool force;

const TypeInfo(this.dartType, {this.template, this.force = false});
const TypeInfo(
this.dartType, {
this.typeDef,
this.force = false,
});
}

class TypeMappings {
static const _classNameParam = '%{className}';

static const _mappings = <String, TypeInfo>{
// simple types
'void': TypeInfo('void'),
Expand Down Expand Up @@ -59,20 +61,23 @@ class TypeMappings {
),
'crypto_sign_keypair_result': TypeInfo('KeyPair'),
'crypto_sign_seed_keypair_result': TypeInfo('KeyPair'),
// opaque types (states)
'generichash_state': TypeInfo('GenerichashState', template: 'opaque_type'),
// state typedefs
'secretstream_xchacha20poly1305_state': TypeInfo(
'SecretstreamXchacha20poly1305State',
typeDef: 'num',
),
'secretstream_xchacha20poly1305_state_address':
TypeInfo('SecretstreamXchacha20poly1305State'),
'sign_state': TypeInfo('SignState', typeDef: 'num'),
'sign_state_address': TypeInfo('SignState'),
'generichash_state': TypeInfo('GenerichashState', typeDef: 'num'),
'generichash_state_address': TypeInfo('GenerichashState'),
'hash_sha256_state': TypeInfo('HashSha256State', template: 'opaque_type'),
'hash_sha256_state': TypeInfo('HashSha256State', typeDef: 'num'),
'hash_sha256_state_address': TypeInfo('HashSha256State'),
'hash_sha512_state': TypeInfo('HashSha512State', template: 'opaque_type'),
'hash_sha512_state': TypeInfo('HashSha512State', typeDef: 'num'),
'hash_sha512_state_address': TypeInfo('HashSha512State'),
'onetimeauth_state': TypeInfo('OnetimeauthState', template: 'opaque_type'),
'onetimeauth_state': TypeInfo('OnetimeauthState', typeDef: 'num'),
'onetimeauth_state_address': TypeInfo('OnetimeauthState'),
// num states
'secretstream_xchacha20poly1305_state': TypeInfo('num'),
'secretstream_xchacha20poly1305_state_address': TypeInfo('num'),
'sign_state': TypeInfo('num'),
'sign_state_address': TypeInfo('num'),
// hidden types
'randombytes_implementation': TypeInfo('Never'),
'randombytes_set_implementation_result': TypeInfo('Never'),
Expand Down Expand Up @@ -102,23 +107,18 @@ class TypeMappings {
bool isForced(String type) => _mappings[type]?.force ?? false;

Future<void> writeTypeDefinitions(StringSink sink) async {
for (final info in _mappings.values) {
if (info.typeDef != null) {
sink.writeln('typedef ${info.dartType} = ${info.typeDef};\n');
}
}

final typeFiles = await fileLoader.listFilesSorted(
'.',
(file) => file.path.endsWith('.dart.type'),
);

for (final typeFile in typeFiles) {
sink.writeln(await typeFile.readAsString());
}

for (final info in _mappings.values) {
if (info.template != null) {
final templateStr = await fileLoader.loadFile(
'${info.template}.dart.template',
);
final outStr = templateStr.replaceAll(_classNameParam, info.dartType);
sink.writeln(outStr);
}
}
}
}

This file was deleted.

0 comments on commit f011d73

Please sign in to comment.