crypto work

This commit is contained in:
Christien Rioux 2024-05-27 22:58:37 -04:00
parent e04fd7ee77
commit 8a5af51ec7
8 changed files with 53 additions and 36 deletions

View file

@ -27,6 +27,9 @@ const int watchRenewalDenominator = 5;
// Maximum number of concurrent DHT operations to perform on the network
const int maxDHTConcurrency = 8;
// DHT crypto domain
const String cryptoDomainDHT = 'dht';
typedef DHTRecordPoolLogger = void Function(String message);
/// Record pool that managed DHTRecords and allows for tagged deletion
@ -547,9 +550,9 @@ class DHTRecordPool with TableDBBackedJson<DHTRecordPoolAllocations> {
writer: writer ??
openedRecordInfo.shared.recordDescriptor.ownerKeyPair(),
crypto: crypto ??
await VeilidCryptoPrivate.fromTypedKeyPair(openedRecordInfo
await privateCryptoFromTypedSecret(openedRecordInfo
.shared.recordDescriptor
.ownerTypedKeyPair()!));
.ownerTypedSecret()!));
openedRecordInfo.records.add(rec);
@ -612,8 +615,8 @@ class DHTRecordPool with TableDBBackedJson<DHTRecordPoolAllocations> {
writer: writer,
sharedDHTRecordData: openedRecordInfo.shared,
crypto: crypto ??
await VeilidCryptoPrivate.fromTypedKeyPair(
TypedKeyPair.fromKeyPair(recordKey.kind, writer)));
await privateCryptoFromTypedSecret(
TypedKey(kind: recordKey.kind, value: writer.secret)));
openedRecordInfo.records.add(rec);
@ -663,6 +666,11 @@ class DHTRecordPool with TableDBBackedJson<DHTRecordPoolAllocations> {
}
}
/// Generate default VeilidCrypto for a writer
static Future<VeilidCrypto> privateCryptoFromTypedSecret(
TypedKey typedSecret) async =>
VeilidCryptoPrivate.fromTypedKey(typedSecret, cryptoDomainDHT);
/// Handle the DHT record updates coming from Veilid
void processRemoteValueChange(VeilidUpdateValueChange updateValueChange) {
if (updateValueChange.subkeys.isNotEmpty) {

View file

@ -125,13 +125,14 @@ extension IdentityMasterExtension on IdentityMaster {
}
Future<List<AccountRecordInfo>> readAccountsFromIdentity(
{required SharedSecret identitySecret,
required String accountKey}) async {
{required SecretKey identitySecret, required String accountKey}) async {
// Read the identity key to get the account keys
final pool = DHTRecordPool.instance;
final identityRecordCrypto = await VeilidCryptoPrivate.fromSecret(
identityRecordKey.kind, identitySecret);
final identityRecordCrypto =
await DHTRecordPool.privateCryptoFromTypedSecret(
TypedKey(kind: identityRecordKey.kind, value: identitySecret),
);
late final List<AccountRecordInfo> accountRecordInfo;
await (await pool.openRecordRead(identityRecordKey,
@ -157,7 +158,7 @@ extension IdentityMasterExtension on IdentityMaster {
/// Creates a new Account associated with master identity and store it in the
/// identity key.
Future<AccountRecordInfo> addAccountToIdentity<T extends GeneratedMessage>({
required SharedSecret identitySecret,
required SecretKey identitySecret,
required String accountKey,
required Future<T> Function(TypedKey parent) createAccountCallback,
int maxAccounts = 1,

View file

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import '../../../veilid_support.dart';
@ -16,15 +17,24 @@ class VeilidCryptoPrivate implements VeilidCrypto {
final VeilidCryptoSystem _cryptoSystem;
final SharedSecret _secretKey;
static Future<VeilidCryptoPrivate> fromTypedKeyPair(
TypedKeyPair typedKeyPair) async {
final cryptoSystem =
await Veilid.instance.getCryptoSystem(typedKeyPair.kind);
final secretKey = typedKeyPair.secret;
static Future<VeilidCryptoPrivate> fromTypedKey(
TypedKey typedKey, String domain) async {
final cryptoSystem = await Veilid.instance.getCryptoSystem(typedKey.kind);
final keyMaterial = Uint8List(0)
..addAll(typedKey.value.decode())
..addAll(utf8.encode(domain));
final secretKey = await cryptoSystem.generateHash(keyMaterial);
return VeilidCryptoPrivate._(cryptoSystem, secretKey);
}
static Future<VeilidCryptoPrivate> fromSecret(
static Future<VeilidCryptoPrivate> fromTypedKeyPair(
TypedKeyPair typedKeyPair, String domain) async {
final typedSecret =
TypedKey(kind: typedKeyPair.kind, value: typedKeyPair.secret);
return fromTypedKey(typedSecret, domain);
}
static Future<VeilidCryptoPrivate> fromSharedSecret(
CryptoKind kind, SharedSecret secretKey) async {
final cryptoSystem = await Veilid.instance.getCryptoSystem(kind);
return VeilidCryptoPrivate._(cryptoSystem, secretKey);