Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor SRP related methods to RustCoreService (#1404)

Leendert de Borst50e10e9400b3
diff --git a/apps/server/AliasVault.Client/Auth/Pages/Login.razor b/apps/server/AliasVault.Client/Auth/Pages/Login.razor
index f4356c6d8..ecf03e9ba 100644
--- a/apps/server/AliasVault.Client/Auth/Pages/Login.razor
+++ b/apps/server/AliasVault.Client/Auth/Pages/Login.razor
@@ -11,7 +11,7 @@
@using AliasVault.Cryptography.Client
@using AliasVault.Client.Services.JsInterop.RustCore
@using Microsoft.Extensions.Localization
-@inject SrpService SrpService
+@inject RustCoreService RustCoreService
@if (_showTwoFactorAuthStep)
{
@@ -272,9 +272,9 @@ else
_passwordHash = await Encryption.DeriveKeyFromPasswordAsync(_loginModel.Password, loginResponse.Salt, loginResponse.EncryptionType, loginResponse.EncryptionSettings);
var passwordHashString = BitConverter.ToString(_passwordHash).Replace("-", string.Empty);
- _clientEphemeral = await SrpService.GenerateEphemeralAsync();
- var privateKey = await SrpService.DerivePrivateKeyAsync(loginResponse.Salt, srpIdentity, passwordHashString);
- _clientSession = await SrpService.DeriveSessionClientAsync(
+ _clientEphemeral = await RustCoreService.SrpGenerateEphemeralAsync();
+ var privateKey = await RustCoreService.SrpDerivePrivateKeyAsync(loginResponse.Salt, srpIdentity, passwordHashString);
+ _clientSession = await RustCoreService.SrpDeriveSessionClientAsync(
privateKey,
_clientEphemeral.Secret,
loginResponse.ServerEphemeral,
@@ -432,7 +432,7 @@ else
private async Task<List<string>> ProcessLoginVerify(ValidateLoginResponse validateLoginResponse)
{
// 5. Client verifies proof.
- await SrpService.VerifySessionAsync(_clientEphemeral.Public, _clientSession, validateLoginResponse.ServerSessionProof);
+ await RustCoreService.SrpVerifySessionAsync(_clientEphemeral.Public, _clientSession, validateLoginResponse.ServerSessionProof);
// Store the tokens in local storage.
await AuthService.StoreAccessTokenAsync(validateLoginResponse.Token!.Token);
diff --git a/apps/server/AliasVault.Client/Main/Pages/Settings/Security/ChangePassword.razor b/apps/server/AliasVault.Client/Main/Pages/Settings/Security/ChangePassword.razor
index c35c0be7c..e71e0d8c1 100644
--- a/apps/server/AliasVault.Client/Main/Pages/Settings/Security/ChangePassword.razor
+++ b/apps/server/AliasVault.Client/Main/Pages/Settings/Security/ChangePassword.razor
@@ -8,7 +8,7 @@
@using Microsoft.Extensions.Localization
@inherits MainBase
@inject HttpClient Http
-@inject SrpService SrpService
+@inject RustCoreService RustCoreService
<LayoutPageTitle>@Localizer["PageTitle"]</LayoutPageTitle>
@@ -165,15 +165,15 @@ else
var currentPasswordHash = await Encryption.DeriveKeyFromPasswordAsync(PasswordChangeFormModel.CurrentPassword, CurrentSalt, CurrentEncryptionType, CurrentEncryptionSettings);
var currentPasswordHashString = BitConverter.ToString(currentPasswordHash).Replace("-", string.Empty);
- ClientEphemeral = await SrpService.GenerateEphemeralAsync();
+ ClientEphemeral = await RustCoreService.SrpGenerateEphemeralAsync();
var username = await GetUsernameAsync();
// Use srpIdentity from server response if available, otherwise fall back to username.
// Note: the fallback can be removed in the future after 0.26.0+ is deployed.
var srpIdentity = CurrentSrpIdentity ?? username.ToLowerInvariant();
- PrivateKey = await SrpService.DerivePrivateKeyAsync(CurrentSalt, srpIdentity, currentPasswordHashString);
- ClientSession = await SrpService.DeriveSessionClientAsync(
+ PrivateKey = await RustCoreService.SrpDerivePrivateKeyAsync(CurrentSalt, srpIdentity, currentPasswordHashString);
+ ClientSession = await RustCoreService.SrpDeriveSessionClientAsync(
PrivateKey,
ClientEphemeral.Secret,
CurrentServerEphemeral,
@@ -184,7 +184,7 @@ else
// IMPORTANT: We must use the same salt for both key derivation AND SRP verifier generation.
// Otherwise login will fail because the server returns this salt, but the password hash
// was derived with a different salt.
- var newSalt = await SrpService.GenerateSaltAsync();
+ var newSalt = await RustCoreService.SrpGenerateSaltAsync();
byte[] newPasswordHash = await Encryption.DeriveKeyFromPasswordAsync(PasswordChangeFormModel.NewPassword, newSalt);
var newPasswordHashString = BitConverter.ToString(newPasswordHash).Replace("-", string.Empty);
@@ -197,8 +197,8 @@ else
await AuthService.StoreEncryptionKeyAsync(newPasswordHash);
// Derive the SRP verifier using the same salt that was used for key derivation.
- var newPrivateKey = await SrpService.DerivePrivateKeyAsync(newSalt, srpIdentity, newPasswordHashString);
- var newVerifier = await SrpService.DeriveVerifierAsync(newPrivateKey);
+ var newPrivateKey = await RustCoreService.SrpDerivePrivateKeyAsync(newSalt, srpIdentity, newPasswordHashString);
+ var newVerifier = await RustCoreService.SrpDeriveVerifierAsync(newPrivateKey);
// Prepare new vault model to update to.
var encryptedBase64String = await DbService.GetEncryptedDatabaseBase64String();
diff --git a/apps/server/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor b/apps/server/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor
index ed2c1f356..9211e3772 100644
--- a/apps/server/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor
+++ b/apps/server/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor
@@ -9,7 +9,7 @@
@using Microsoft.Extensions.Localization
@using AliasVault.Client.Resources
@inject HttpClient Http
-@inject SrpService SrpService
+@inject RustCoreService RustCoreService
<LayoutPageTitle>@Localizer["PageTitle"]</LayoutPageTitle>
@@ -185,9 +185,9 @@
var passwordHash = await Encryption.DeriveKeyFromPasswordAsync(_passwordModel.Password, loginResponse.Salt, loginResponse.EncryptionType, loginResponse.EncryptionSettings);
var passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
- ClientEphemeral = await SrpService.GenerateEphemeralAsync();
- PrivateKey = await SrpService.DerivePrivateKeyAsync(loginResponse.Salt, srpIdentity, passwordHashString);
- ClientSession = await SrpService.DeriveSessionClientAsync(
+ ClientEphemeral = await RustCoreService.SrpGenerateEphemeralAsync();
+ PrivateKey = await RustCoreService.SrpDerivePrivateKeyAsync(loginResponse.Salt, srpIdentity, passwordHashString);
+ ClientSession = await RustCoreService.SrpDeriveSessionClientAsync(
PrivateKey,
ClientEphemeral.Secret,
loginResponse.ServerEphemeral,
diff --git a/apps/server/AliasVault.Client/Program.cs b/apps/server/AliasVault.Client/Program.cs
index 8ae18c127..79aea0829 100644
--- a/apps/server/AliasVault.Client/Program.cs
+++ b/apps/server/AliasVault.Client/Program.cs
@@ -102,7 +102,6 @@ builder.Services.AddScoped<ConfirmModalService>();
builder.Services.AddScoped<QuickCreateStateService>();
builder.Services.AddScoped<LanguageService>();
builder.Services.AddScoped<RustCoreService>();
-builder.Services.AddScoped<SrpService>();
builder.Services.AddAuthorizationCore();
builder.Services.AddBlazoredLocalStorage();
diff --git a/apps/server/AliasVault.Client/Services/Auth/UserRegistrationService.cs b/apps/server/AliasVault.Client/Services/Auth/UserRegistrationService.cs
index 73338df6e..44d15d9f8 100644
--- a/apps/server/AliasVault.Client/Services/Auth/UserRegistrationService.cs
+++ b/apps/server/AliasVault.Client/Services/Auth/UserRegistrationService.cs
@@ -24,8 +24,8 @@ using Microsoft.Extensions.Localization;
/// <param name="authService">The service handling authentication operations.</param>
/// <param name="config">The application configuration.</param>
/// <param name="localizerFactory">The string localizer factory for localization.</param>
-/// <param name="srpService">The SRP service for secure authentication.</param>
-public class UserRegistrationService(HttpClient httpClient, AuthenticationStateProvider authStateProvider, AuthService authService, Config config, IStringLocalizerFactory localizerFactory, SrpService srpService)
+/// <param name="rustCoreService">The Rust core service for secure authentication.</param>
+public class UserRegistrationService(HttpClient httpClient, AuthenticationStateProvider authStateProvider, AuthService authService, Config config, IStringLocalizerFactory localizerFactory, RustCoreService rustCoreService)
{
private readonly IStringLocalizer _apiErrorLocalizer = localizerFactory.Create("ApiErrors", "AliasVault.Client");
@@ -52,14 +52,14 @@ public class UserRegistrationService(HttpClient httpClient, AuthenticationStateP
}
// Generate salt using Rust WASM
- var salt = await srpService.GenerateSaltAsync();
+ var salt = await rustCoreService.SrpGenerateSaltAsync();
var passwordHash = await Encryption.DeriveKeyFromPasswordAsync(password, salt, encryptionType, encryptionSettings);
var passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
// Derive SRP private key and verifier using the same salt
- var privateKey = await srpService.DerivePrivateKeyAsync(salt, srpIdentity, passwordHashString);
- var srpVerifier = await srpService.DeriveVerifierAsync(privateKey);
+ var privateKey = await rustCoreService.SrpDerivePrivateKeyAsync(salt, srpIdentity, passwordHashString);
+ var srpVerifier = await rustCoreService.SrpDeriveVerifierAsync(privateKey);
var registerRequest = new RegisterRequest(username, salt, srpVerifier, encryptionType, encryptionSettings, srpIdentity);
var result = await httpClient.PostAsJsonAsync("v1/Auth/register", registerRequest);
diff --git a/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs b/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs
index 1c1a825d2..c4ac37471 100644
--- a/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs
+++ b/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs
@@ -63,34 +63,6 @@ public class RustCoreService : IAsyncDisposable
}
}
- /// <summary>
- /// Wait for the Rust WASM module to become available with retries.
- /// Uses exponential backoff for more robust loading in slow environments (e.g., E2E tests, mobile devices).
- /// Default timeout is ~30 seconds to handle slow network conditions.
- /// </summary>
- /// <param name="maxRetries">Maximum number of retry attempts.</param>
- /// <param name="initialDelayMs">Initial delay between retries in milliseconds.</param>
- /// <returns>True if the WASM module became available.</returns>
- public async Task<bool> WaitForAvailabilityAsync(int maxRetries = 30, int initialDelayMs = 100)
- {
- var currentDelay = initialDelayMs;
-
- for (int i = 0; i < maxRetries; i++)
- {
- if (await IsAvailableAsync())
- {
- return true;
- }
-
- await Task.Delay(currentDelay);
-
- // Exponential backoff with cap at 2 seconds
- currentDelay = Math.Min(currentDelay * 2, 2000);
- }
-
- return false;
- }
-
/// <summary>
/// Merge two vaults using Last-Write-Wins (LWW) strategy.
/// </summary>
@@ -231,9 +203,165 @@ public class RustCoreService : IAsyncDisposable
}
}
+ /// <summary>
+ /// Generate a random salt for SRP registration.
+ /// </summary>
+ /// <returns>64-character uppercase hex string (32 bytes).</returns>
+ /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
+ public async Task<string> SrpGenerateSaltAsync()
+ {
+ if (!await WaitForAvailabilityAsync())
+ {
+ throw new InvalidOperationException("Rust WASM module is not available.");
+ }
+
+ return await jsRuntime.InvokeAsync<string>("rustCoreSrpGenerateSalt");
+ }
+
+ /// <summary>
+ /// Derive a private key from salt, identity, and password hash.
+ /// </summary>
+ /// <param name="salt">The salt (hex string).</param>
+ /// <param name="identity">The SRP identity (username or GUID), will be lowercased.</param>
+ /// <param name="passwordHash">The password hash (hex string).</param>
+ /// <returns>64-character uppercase hex string (32 bytes).</returns>
+ /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
+ public async Task<string> SrpDerivePrivateKeyAsync(string salt, string identity, string passwordHash)
+ {
+ if (!await WaitForAvailabilityAsync())
+ {
+ throw new InvalidOperationException("Rust WASM module is not available.");
+ }
+
+ // Make sure the identity is lowercase as the SRP protocol is case sensitive.
+ identity = identity.ToLowerInvariant();
+
+ return await jsRuntime.InvokeAsync<string>("rustCoreSrpDerivePrivateKey", salt, identity, passwordHash);
+ }
+
+ /// <summary>
+ /// Derive a verifier from a private key.
+ /// </summary>
+ /// <param name="privateKey">The private key (hex string).</param>
+ /// <returns>512-character uppercase hex string (256 bytes).</returns>
+ /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
+ public async Task<string> SrpDeriveVerifierAsync(string privateKey)
+ {
+ if (!await WaitForAvailabilityAsync())
+ {
+ throw new InvalidOperationException("Rust WASM module is not available.");
+ }
+
+ return await jsRuntime.InvokeAsync<string>("rustCoreSrpDeriveVerifier", privateKey);
+ }
+
+ /// <summary>
+ /// Generate client ephemeral keypair.
+ /// </summary>
+ /// <returns>Ephemeral object with Public and Secret hex strings.</returns>
+ /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
+ public async Task<SrpEphemeral> SrpGenerateEphemeralAsync()
+ {
+ if (!await WaitForAvailabilityAsync())
+ {
+ throw new InvalidOperationException("Rust WASM module is not available.");
+ }
+
+ return await jsRuntime.InvokeAsync<SrpEphemeral>("rustCoreSrpGenerateEphemeral");
+ }
+
+ /// <summary>
+ /// Derive client session from ephemeral values.
+ /// </summary>
+ /// <param name="clientSecret">Client ephemeral secret (hex string).</param>
+ /// <param name="serverPublic">Server ephemeral public (hex string).</param>
+ /// <param name="salt">The salt (hex string).</param>
+ /// <param name="identity">The SRP identity, will be lowercased.</param>
+ /// <param name="privateKey">The private key (hex string).</param>
+ /// <returns>Session object with Key and Proof hex strings.</returns>
+ /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
+ public async Task<SrpSession> SrpDeriveSessionAsync(string clientSecret, string serverPublic, string salt, string identity, string privateKey)
+ {
+ if (!await WaitForAvailabilityAsync())
+ {
+ throw new InvalidOperationException("Rust WASM module is not available.");
+ }
+
+ // Make sure the identity is lowercase as the SRP protocol is case sensitive.
+ identity = identity.ToLowerInvariant();
+
+ return await jsRuntime.InvokeAsync<SrpSession>("rustCoreSrpDeriveSession", clientSecret, serverPublic, salt, identity, privateKey);
+ }
+
+ /// <summary>
+ /// Derive session client-side (convenience method with reordered parameters).
+ /// </summary>
+ /// <param name="privateKey">The private key.</param>
+ /// <param name="clientSecretEphemeral">Client ephemeral secret.</param>
+ /// <param name="serverEphemeralPublic">Server public ephemeral.</param>
+ /// <param name="salt">Salt.</param>
+ /// <param name="identity">Identity.</param>
+ /// <returns>SrpSession.</returns>
+ public async Task<SrpSession> SrpDeriveSessionClientAsync(string privateKey, string clientSecretEphemeral, string serverEphemeralPublic, string salt, string identity)
+ {
+ return await SrpDeriveSessionAsync(clientSecretEphemeral, serverEphemeralPublic, salt, identity, privateKey);
+ }
+
+ /// <summary>
+ /// Verify the server's session proof (M2) on the client side.
+ /// </summary>
+ /// <param name="clientPublic">Client public ephemeral (A).</param>
+ /// <param name="clientSession">Client session containing proof (M1) and key (K).</param>
+ /// <param name="serverProof">Server proof (M2) to verify.</param>
+ /// <returns>A task representing the asynchronous operation.</returns>
+ /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
+ /// <exception cref="System.Security.SecurityException">Thrown if verification fails.</exception>
+ public async Task SrpVerifySessionAsync(string clientPublic, SrpSession clientSession, string serverProof)
+ {
+ if (!await WaitForAvailabilityAsync())
+ {
+ throw new InvalidOperationException("Rust WASM module is not available.");
+ }
+
+ var result = await jsRuntime.InvokeAsync<bool>("rustCoreSrpVerifySession", clientPublic, clientSession.Proof, clientSession.Key, serverProof);
+
+ if (!result)
+ {
+ throw new System.Security.SecurityException("Server session proof verification failed.");
+ }
+ }
+
/// <inheritdoc/>
public ValueTask DisposeAsync()
{
return ValueTask.CompletedTask;
}
+
+ /// <summary>
+ /// Wait for the Rust WASM module to become available with retries.
+ /// Uses exponential backoff for more robust loading in slow environments (e.g., E2E tests, mobile devices).
+ /// Default timeout is ~30 seconds to handle slow network conditions.
+ /// </summary>
+ /// <param name="maxRetries">Maximum number of retry attempts.</param>
+ /// <param name="initialDelayMs">Initial delay between retries in milliseconds.</param>
+ /// <returns>True if the WASM module became available.</returns>
+ private async Task<bool> WaitForAvailabilityAsync(int maxRetries = 30, int initialDelayMs = 100)
+ {
+ var currentDelay = initialDelayMs;
+
+ for (int i = 0; i < maxRetries; i++)
+ {
+ if (await IsAvailableAsync())
+ {
+ return true;
+ }
+
+ await Task.Delay(currentDelay);
+
+ // Exponential backoff with cap at 2 seconds
+ currentDelay = Math.Min(currentDelay * 2, 2000);
+ }
+
+ return false;
+ }
}
diff --git a/apps/server/AliasVault.Client/Services/JsInterop/RustCore/SrpService.cs b/apps/server/AliasVault.Client/Services/JsInterop/RustCore/SrpService.cs
deleted file mode 100644
index 54c3c0576..000000000
--- a/apps/server/AliasVault.Client/Services/JsInterop/RustCore/SrpService.cs
+++ /dev/null
@@ -1,180 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="SrpService.cs" company="aliasvault">
-// Copyright (c) aliasvault. All rights reserved.
-// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace AliasVault.Client.Services.JsInterop.RustCore;
-
-using Microsoft.JSInterop;
-
-/// <summary>
-/// JavaScript interop wrapper for the Rust WASM SRP (Secure Remote Password) library.
-/// Provides SRP authentication functionality via WASM.
-/// </summary>
-public class SrpService : IAsyncDisposable
-{
- private readonly IJSRuntime jsRuntime;
- private readonly RustCoreService rustCoreService;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="SrpService"/> class.
- /// </summary>
- /// <param name="jsRuntime">The JS runtime for interop.</param>
- /// <param name="rustCoreService">The Rust core service for WASM availability checks.</param>
- public SrpService(IJSRuntime jsRuntime, RustCoreService rustCoreService)
- {
- this.jsRuntime = jsRuntime;
- this.rustCoreService = rustCoreService;
- }
-
- /// <summary>
- /// Generate a random salt for SRP registration.
- /// </summary>
- /// <returns>64-character uppercase hex string (32 bytes).</returns>
- /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
- public async Task<string> GenerateSaltAsync()
- {
- if (!await rustCoreService.WaitForAvailabilityAsync())
- {
- throw new InvalidOperationException("Rust WASM module is not available.");
- }
-
- return await jsRuntime.InvokeAsync<string>("rustCoreSrpGenerateSalt");
- }
-
- /// <summary>
- /// Derive a private key from salt, identity, and password hash.
- /// </summary>
- /// <param name="salt">The salt (hex string).</param>
- /// <param name="identity">The SRP identity (username or GUID), will be lowercased.</param>
- /// <param name="passwordHash">The password hash (hex string).</param>
- /// <returns>64-character uppercase hex string (32 bytes).</returns>
- /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
- public async Task<string> DerivePrivateKeyAsync(string salt, string identity, string passwordHash)
- {
- if (!await rustCoreService.WaitForAvailabilityAsync())
- {
- throw new InvalidOperationException("Rust WASM module is not available.");
- }
-
- // Make sure the identity is lowercase as the SRP protocol is case sensitive.
- identity = identity.ToLowerInvariant();
-
- return await jsRuntime.InvokeAsync<string>("rustCoreSrpDerivePrivateKey", salt, identity, passwordHash);
- }
-
- /// <summary>
- /// Derive a verifier from a private key.
- /// </summary>
- /// <param name="privateKey">The private key (hex string).</param>
- /// <returns>512-character uppercase hex string (256 bytes).</returns>
- /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
- public async Task<string> DeriveVerifierAsync(string privateKey)
- {
- if (!await rustCoreService.WaitForAvailabilityAsync())
- {
- throw new InvalidOperationException("Rust WASM module is not available.");
- }
-
- return await jsRuntime.InvokeAsync<string>("rustCoreSrpDeriveVerifier", privateKey);
- }
-
- /// <summary>
- /// Generate client ephemeral keypair.
- /// </summary>
- /// <returns>Ephemeral object with Public and Secret hex strings.</returns>
- /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
- public async Task<SrpEphemeral> GenerateEphemeralAsync()
- {
- if (!await rustCoreService.WaitForAvailabilityAsync())
- {
- throw new InvalidOperationException("Rust WASM module is not available.");
- }
-
- return await jsRuntime.InvokeAsync<SrpEphemeral>("rustCoreSrpGenerateEphemeral");
- }
-
- /// <summary>
- /// Derive client session from ephemeral values.
- /// </summary>
- /// <param name="clientSecret">Client ephemeral secret (hex string).</param>
- /// <param name="serverPublic">Server ephemeral public (hex string).</param>
- /// <param name="salt">The salt (hex string).</param>
- /// <param name="identity">The SRP identity, will be lowercased.</param>
- /// <param name="privateKey">The private key (hex string).</param>
- /// <returns>Session object with Key and Proof hex strings.</returns>
- /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
- public async Task<SrpSession> DeriveSessionAsync(string clientSecret, string serverPublic, string salt, string identity, string privateKey)
- {
- if (!await rustCoreService.WaitForAvailabilityAsync())
- {
- throw new InvalidOperationException("Rust WASM module is not available.");
- }
-
- // Make sure the identity is lowercase as the SRP protocol is case sensitive.
- identity = identity.ToLowerInvariant();
-
- return await jsRuntime.InvokeAsync<SrpSession>("rustCoreSrpDeriveSession", clientSecret, serverPublic, salt, identity, privateKey);
- }
-
- /// <summary>
- /// Prepare password change/registration by generating salt and verifier.
- /// </summary>
- /// <param name="identity">The SRP identity (username or GUID).</param>
- /// <param name="passwordHashString">The password hash as hex string.</param>
- /// <returns>Tuple with Salt and Verifier.</returns>
- public async Task<(string Salt, string Verifier)> PreparePasswordChangeAsync(string identity, string passwordHashString)
- {
- var salt = await GenerateSaltAsync();
- var privateKey = await DerivePrivateKeyAsync(salt, identity, passwordHashString);
- var verifier = await DeriveVerifierAsync(privateKey);
-
- return (salt, verifier);
- }
-
- /// <summary>
- /// Derive session client-side (convenience method matching old Srp.DeriveSessionClient signature).
- /// </summary>
- /// <param name="privateKey">The private key.</param>
- /// <param name="clientSecretEphemeral">Client ephemeral secret.</param>
- /// <param name="serverEphemeralPublic">Server public ephemeral.</param>
- /// <param name="salt">Salt.</param>
- /// <param name="identity">Identity.</param>
- /// <returns>SrpSession.</returns>
- public async Task<SrpSession> DeriveSessionClientAsync(string privateKey, string clientSecretEphemeral, string serverEphemeralPublic, string salt, string identity)
- {
- return await DeriveSessionAsync(clientSecretEphemeral, serverEphemeralPublic, salt, identity, privateKey);
- }
-
- /// <summary>
- /// Verify the server's session proof (M2) on the client side.
- /// </summary>
- /// <param name="clientPublic">Client public ephemeral (A).</param>
- /// <param name="clientSession">Client session containing proof (M1) and key (K).</param>
- /// <param name="serverProof">Server proof (M2) to verify.</param>
- /// <returns>A task representing the asynchronous operation.</returns>
- /// <exception cref="InvalidOperationException">Thrown if WASM module is unavailable.</exception>
- /// <exception cref="System.Security.SecurityException">Thrown if verification fails.</exception>
- public async Task VerifySessionAsync(string clientPublic, SrpSession clientSession, string serverProof)
- {
- if (!await rustCoreService.WaitForAvailabilityAsync())
- {
- throw new InvalidOperationException("Rust WASM module is not available.");
- }
-
- var result = await jsRuntime.InvokeAsync<bool>("rustCoreSrpVerifySession", clientPublic, clientSession.Proof, clientSession.Key, serverProof);
-
- if (!result)
- {
- throw new System.Security.SecurityException("Server session proof verification failed.");
- }
- }
-
- /// <inheritdoc/>
- public ValueTask DisposeAsync()
- {
- return ValueTask.CompletedTask;
- }
-}