crypto update
This commit is contained in:
@@ -32,7 +32,6 @@
|
||||
/build/
|
||||
|
||||
# Web related
|
||||
lib/generated_plugin_registrant.dart
|
||||
|
||||
# Symbolication related
|
||||
app.*.symbols
|
||||
|
||||
@@ -141,6 +141,11 @@ abstract class VeilidCryptoSystem {
|
||||
CryptoKind kind();
|
||||
Future<SharedSecret> cachedDH(PublicKey key, SecretKey secret);
|
||||
Future<SharedSecret> computeDH(PublicKey key, SecretKey secret);
|
||||
Future<Uint8List> randomBytes(int len);
|
||||
Future<int> defaultSaltLength();
|
||||
Future<String> hashPassword(Uint8List password, Uint8List salt);
|
||||
Future<bool> verifyPassword(Uint8List password, String passwordHash);
|
||||
Future<SharedSecret> deriveSharedSecret(Uint8List password, Uint8List salt);
|
||||
Future<Nonce> randomNonce();
|
||||
Future<SharedSecret> randomSharedSecret();
|
||||
Future<KeyPair> generateKeyPair();
|
||||
|
||||
@@ -213,6 +213,28 @@ typedef _CryptoComputeDHC = Void Function(
|
||||
Int64, Uint32, Pointer<Utf8>, Pointer<Utf8>);
|
||||
typedef _CryptoComputeDHDart = void Function(
|
||||
int, int, Pointer<Utf8>, Pointer<Utf8>);
|
||||
// fn crypto_random_bytes(port: i64, kind: u32, len: u32)
|
||||
typedef _CryptoRandomBytesC = Void Function(Int64, Uint32, Uint32);
|
||||
typedef _CryptoRandomBytesDart = void Function(int, int, int);
|
||||
// fn crypto_default_salt_length(port: i64, kind: u32)
|
||||
typedef _CryptoDefaultSaltLengthC = Void Function(Int64, Uint32);
|
||||
typedef _CryptoDefaultSaltLengthDart = void Function(int, int);
|
||||
// fn crypto_hash_password(port: i64, kind: u32, password: FfiStr, salt: FfiStr )
|
||||
typedef _CryptoHashPasswordC = Void Function(
|
||||
Int64, Uint32, Pointer<Utf8>, Pointer<Utf8>);
|
||||
typedef _CryptoHashPasswordDart = void Function(
|
||||
int, int, Pointer<Utf8>, Pointer<Utf8>);
|
||||
// fn crypto_verify_password(port: i64, kind: u32, password: FfiStr, password_hash: FfiStr )
|
||||
typedef _CryptoVerifyPasswordC = Void Function(
|
||||
Int64, Uint32, Pointer<Utf8>, Pointer<Utf8>);
|
||||
typedef _CryptoVerifyPasswordDart = void Function(
|
||||
int, int, Pointer<Utf8>, Pointer<Utf8>);
|
||||
// fn crypto_derive_shared_secret(port: i64, kind: u32, password: FfiStr, salt: FfiStr )
|
||||
typedef _CryptoDeriveSharedSecretC = Void Function(
|
||||
Int64, Uint32, Pointer<Utf8>, Pointer<Utf8>);
|
||||
typedef _CryptoDeriveSharedSecretDart = void Function(
|
||||
int, int, Pointer<Utf8>, Pointer<Utf8>);
|
||||
|
||||
// fn crypto_random_nonce(port: i64, kind: u32)
|
||||
typedef _CryptoRandomNonceC = Void Function(Int64, Uint32);
|
||||
typedef _CryptoRandomNonceDart = void Function(int, int);
|
||||
@@ -884,7 +906,7 @@ class VeilidTableDBFFI extends VeilidTableDB {
|
||||
// FFI implementation of VeilidCryptoSystem
|
||||
class VeilidCryptoSystemFFI implements VeilidCryptoSystem {
|
||||
final CryptoKind _kind;
|
||||
VeilidFFI _ffi;
|
||||
final VeilidFFI _ffi;
|
||||
|
||||
VeilidCryptoSystemFFI._(this._ffi, this._kind);
|
||||
|
||||
@@ -915,6 +937,59 @@ class VeilidCryptoSystemFFI implements VeilidCryptoSystem {
|
||||
return processFutureJson(SharedSecret.fromJson, recvPort.first);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> randomBytes(int len) async {
|
||||
final recvPort = ReceivePort("crypto_random_bytes");
|
||||
final sendPort = recvPort.sendPort;
|
||||
_ffi._cryptoRandomBytes(sendPort.nativePort, _kind, len);
|
||||
final out = await processFuturePlain(recvPort.first);
|
||||
return base64UrlNoPadDecode(out);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> defaultSaltLength() {
|
||||
final recvPort = ReceivePort("crypto_default_salt_length");
|
||||
final sendPort = recvPort.sendPort;
|
||||
_ffi._cryptoDefaultSaltLength(sendPort.nativePort, _kind);
|
||||
return processFuturePlain(recvPort.first);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> hashPassword(Uint8List password, Uint8List salt) {
|
||||
final nativeEncodedPassword = base64UrlNoPadEncode(password).toNativeUtf8();
|
||||
final nativeEncodedSalt = base64UrlNoPadEncode(salt).toNativeUtf8();
|
||||
|
||||
final recvPort = ReceivePort("crypto_hash_password");
|
||||
final sendPort = recvPort.sendPort;
|
||||
_ffi._cryptoHashPassword(
|
||||
sendPort.nativePort, _kind, nativeEncodedPassword, nativeEncodedSalt);
|
||||
return processFuturePlain(recvPort.first);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> verifyPassword(Uint8List password, String passwordHash) {
|
||||
final nativeEncodedPassword = base64UrlNoPadEncode(password).toNativeUtf8();
|
||||
final nativeEncodedPasswordHash = passwordHash.toNativeUtf8();
|
||||
|
||||
final recvPort = ReceivePort("crypto_verify_password");
|
||||
final sendPort = recvPort.sendPort;
|
||||
_ffi._cryptoVerifyPassword(sendPort.nativePort, _kind,
|
||||
nativeEncodedPassword, nativeEncodedPasswordHash);
|
||||
return processFuturePlain(recvPort.first);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SharedSecret> deriveSharedSecret(Uint8List password, Uint8List salt) {
|
||||
final nativeEncodedPassword = base64UrlNoPadEncode(password).toNativeUtf8();
|
||||
final nativeEncodedSalt = base64UrlNoPadEncode(salt).toNativeUtf8();
|
||||
|
||||
final recvPort = ReceivePort("crypto_derive_shared_secret");
|
||||
final sendPort = recvPort.sendPort;
|
||||
_ffi._cryptoHashPassword(
|
||||
sendPort.nativePort, _kind, nativeEncodedPassword, nativeEncodedSalt);
|
||||
return processFutureJson(SharedSecret.fromJson, recvPort.first);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Nonce> randomNonce() {
|
||||
final recvPort = ReceivePort("crypto_random_nonce");
|
||||
@@ -1134,6 +1209,13 @@ class VeilidFFI implements Veilid {
|
||||
|
||||
final _CryptoCachedDHDart _cryptoCachedDH;
|
||||
final _CryptoComputeDHDart _cryptoComputeDH;
|
||||
|
||||
final _CryptoRandomBytesDart _cryptoRandomBytes;
|
||||
final _CryptoDefaultSaltLengthDart _cryptoDefaultSaltLength;
|
||||
final _CryptoHashPasswordDart _cryptoHashPassword;
|
||||
final _CryptoVerifyPasswordDart _cryptoVerifyPassword;
|
||||
final _CryptoDeriveSharedSecretDart _cryptoDeriveSharedSecret;
|
||||
|
||||
final _CryptoRandomNonceDart _cryptoRandomNonce;
|
||||
final _CryptoRandomSharedSecretDart _cryptoRandomSharedSecret;
|
||||
final _CryptoGenerateKeyPairDart _cryptoGenerateKeyPair;
|
||||
@@ -1295,6 +1377,20 @@ class VeilidFFI implements Veilid {
|
||||
_cryptoComputeDH =
|
||||
dylib.lookupFunction<_CryptoComputeDHC, _CryptoComputeDHDart>(
|
||||
'crypto_compute_dh'),
|
||||
_cryptoRandomBytes =
|
||||
dylib.lookupFunction<_CryptoRandomBytesC, _CryptoRandomBytesDart>(
|
||||
'crypto_random_bytes'),
|
||||
_cryptoDefaultSaltLength = dylib.lookupFunction<
|
||||
_CryptoDefaultSaltLengthC,
|
||||
_CryptoDefaultSaltLengthDart>('crypto_default_salt_length'),
|
||||
_cryptoHashPassword =
|
||||
dylib.lookupFunction<_CryptoHashPasswordC, _CryptoHashPasswordDart>(
|
||||
'crypto_hash_password'),
|
||||
_cryptoVerifyPassword = dylib.lookupFunction<_CryptoVerifyPasswordC,
|
||||
_CryptoVerifyPasswordDart>('crypto_verify_password'),
|
||||
_cryptoDeriveSharedSecret = dylib.lookupFunction<
|
||||
_CryptoDeriveSharedSecretC,
|
||||
_CryptoVerifyPasswordDart>('crypto_derive_shared_secret'),
|
||||
_cryptoRandomNonce =
|
||||
dylib.lookupFunction<_CryptoRandomNonceC, _CryptoRandomNonceDart>(
|
||||
'crypto_random_nonce'),
|
||||
|
||||
@@ -157,7 +157,10 @@ class VeilidCryptoSystemJS implements VeilidCryptoSystem {
|
||||
final CryptoKind _kind;
|
||||
final VeilidJS _js;
|
||||
|
||||
VeilidCryptoSystemJS._(this._js, this._kind);
|
||||
VeilidCryptoSystemJS._(this._js, this._kind) {
|
||||
// Keep the reference
|
||||
_js;
|
||||
}
|
||||
|
||||
@override
|
||||
CryptoKind kind() {
|
||||
@@ -178,6 +181,41 @@ class VeilidCryptoSystemJS implements VeilidCryptoSystem {
|
||||
[_kind, jsonEncode(key), jsonEncode(secret)]))));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> randomBytes(int len) async {
|
||||
return base64UrlNoPadDecode(await _wrapApiPromise(
|
||||
js_util.callMethod(wasm, "crypto_random_bytes", [_kind, len])));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> defaultSaltLength() {
|
||||
return _wrapApiPromise(
|
||||
js_util.callMethod(wasm, "crypto_default_salt_length", [_kind]));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> hashPassword(Uint8List password, Uint8List salt) {
|
||||
return _wrapApiPromise(js_util.callMethod(wasm, "crypto_hash_password",
|
||||
[_kind, base64UrlNoPadEncode(password), base64UrlNoPadEncode(salt)]));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> verifyPassword(Uint8List password, String passwordHash) {
|
||||
return _wrapApiPromise(js_util.callMethod(wasm, "crypto_verify_password",
|
||||
[_kind, base64UrlNoPadEncode(password), passwordHash]));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SharedSecret> deriveSharedSecret(
|
||||
Uint8List password, Uint8List salt) async {
|
||||
return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
||||
.callMethod(wasm, "crypto_derive_shared_secret", [
|
||||
_kind,
|
||||
base64UrlNoPadEncode(password),
|
||||
base64UrlNoPadEncode(salt)
|
||||
]))));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Nonce> randomNonce() async {
|
||||
return Nonce.fromJson(jsonDecode(await _wrapApiPromise(
|
||||
|
||||
@@ -1091,6 +1091,109 @@ pub extern "C" fn crypto_compute_dh(port: i64, kind: u32, key: FfiStr, secret: F
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn crypto_random_bytes(port: i64, kind: u32, len: u32) {
|
||||
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
|
||||
|
||||
DartIsolateWrapper::new(port).spawn_result(async move {
|
||||
let veilid_api = get_veilid_api().await?;
|
||||
let crypto = veilid_api.crypto()?;
|
||||
let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_random_bytes", "kind", kind.to_string()))?;
|
||||
let out = csv.random_bytes(len);
|
||||
let out = data_encoding::BASE64URL_NOPAD.encode(&out);
|
||||
APIResult::Ok(out)
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn crypto_default_salt_length(port: i64, kind: u32) {
|
||||
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
|
||||
|
||||
DartIsolateWrapper::new(port).spawn_result(async move {
|
||||
let veilid_api = get_veilid_api().await?;
|
||||
let crypto = veilid_api.crypto()?;
|
||||
let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_default_salt_length", "kind", kind.to_string()))?;
|
||||
let out = csv.default_salt_length();
|
||||
APIResult::Ok(out)
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn crypto_hash_password(port: i64, kind: u32, password: FfiStr, salt: FfiStr ) {
|
||||
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
|
||||
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(
|
||||
password.into_opt_string()
|
||||
.unwrap()
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
let salt: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(
|
||||
salt.into_opt_string()
|
||||
.unwrap()
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
DartIsolateWrapper::new(port).spawn_result(async move {
|
||||
let veilid_api = get_veilid_api().await?;
|
||||
let crypto = veilid_api.crypto()?;
|
||||
let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_hash_password", "kind", kind.to_string()))?;
|
||||
let out = csv.hash_password(&password, &salt)?;
|
||||
APIResult::Ok(out)
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn crypto_verify_password(port: i64, kind: u32, password: FfiStr, password_hash: FfiStr ) {
|
||||
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
|
||||
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(
|
||||
password.into_opt_string()
|
||||
.unwrap()
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
let password_hash = password_hash.into_opt_string().unwrap();
|
||||
|
||||
DartIsolateWrapper::new(port).spawn_result(async move {
|
||||
let veilid_api = get_veilid_api().await?;
|
||||
let crypto = veilid_api.crypto()?;
|
||||
let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_verify_password", "kind", kind.to_string()))?;
|
||||
let out = csv.verify_password(&password, &password_hash)?;
|
||||
APIResult::Ok(out)
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn crypto_derive_shared_secret(port: i64, kind: u32, password: FfiStr, salt: FfiStr ) {
|
||||
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
|
||||
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(
|
||||
password.into_opt_string()
|
||||
.unwrap()
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
let salt: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(
|
||||
salt.into_opt_string()
|
||||
.unwrap()
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
DartIsolateWrapper::new(port).spawn_result_json(async move {
|
||||
let veilid_api = get_veilid_api().await?;
|
||||
let crypto = veilid_api.crypto()?;
|
||||
let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_derive_shared_secret", "kind", kind.to_string()))?;
|
||||
let out = csv.derive_shared_secret(&password, &salt)?;
|
||||
APIResult::Ok(out)
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn crypto_random_nonce(port: i64, kind: u32) {
|
||||
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
|
||||
|
||||
Reference in New Issue
Block a user