Skip to content

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

Add SRP auth scaffolding (#43)

Leendert de Borstc6e3c41759b3
diff --git a/ENCRYPTION.md b/ENCRYPTION.md
new file mode 100644
index 000000000..e877befab
--- /dev/null
+++ b/ENCRYPTION.md
@@ -0,0 +1,13 @@
+# Encryption
+This document describes the encryption used in AliasVault.
+
+## SRP
+The application uses the Secure Remote Password (SRP) protocol for authentication. The SRP protocol is a password-authenticated key agreement protocol. This means that the client and server can authenticate each other using a password, without sending the password over the network.
+
+With the use of SRP the master password never leaves the client. The client sends a verifier to the server, which is a value derived from the master password. The server uses this verifier to authenticate the client. With this the server can authenticate the client without having ever seen the actual master password.
+
+## Argon2id
+The application uses the Argon2id key derivation function to derive a key from the master password. Argon2id is a memory-hard function, which makes it difficult to perform large-scale custom hardware attacks. This makes it a good choice for password hashing.
+
+## AES
+AES-256 IV is used to encrypt the data. The data is encrypted with a key derived from the master password using Argon2id. The Initialization Vector (IV) is generated randomly for each encryption.
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/AuthFinishModel.cs b/src/AliasVault.Shared/Models/WebApi/Auth/AuthFinishModel.cs
new file mode 100644
index 000000000..b2b18708f
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/AuthFinishModel.cs
@@ -0,0 +1,42 @@
+//-----------------------------------------------------------------------
+// <copyright file="AuthFinishModel.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Shared.Models.WebApi.Auth;
+
+/// <summary>
+/// Auth finish model using SRP (Secure Remote Password) protocol.
+/// </summary>
+public class AuthFinishModel
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="AuthFinishModel"/> class.
+ /// </summary>
+ /// <param name="email">Email.</param>
+ /// <param name="a">A.</param>
+ /// <param name="m1">M1.</param>
+ public AuthFinishModel(string email, string a, string m1)
+ {
+ Email = email;
+ A = a;
+ M1 = m1;
+ }
+
+ /// <summary>
+ /// Gets or sets the email.
+ /// </summary>
+ public string Email { get; set; }
+
+ /// <summary>
+ /// Gets or sets A which is a value that is used to verify the user's identity.
+ /// </summary>
+ public string A { get; set; }
+
+ /// <summary>
+ /// Gets or sets M1 which is a value that is used to verify the user's identity.
+ /// </summary>
+ public string M1 { get; set; }
+}
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/AuthStartModel.cs b/src/AliasVault.Shared/Models/WebApi/Auth/AuthStartModel.cs
new file mode 100644
index 000000000..3833b2822
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/AuthStartModel.cs
@@ -0,0 +1,28 @@
+//-----------------------------------------------------------------------
+// <copyright file="AuthStartModel.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Shared.Models.WebApi.Auth;
+
+/// <summary>
+/// Auth start model using SRP (Secure Remote Password) protocol.
+/// </summary>
+public class AuthStartModel
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="AuthStartModel"/> class.
+ /// </summary>
+ /// <param name="email">Email.</param>
+ public AuthStartModel(string email)
+ {
+ this.Email = email;
+ }
+
+ /// <summary>
+ /// Gets or sets the email.
+ /// </summary>
+ public string Email { get; set; }
+}
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/RegisterModel.cs b/src/AliasVault.Shared/Models/WebApi/Auth/RegisterModel.cs
new file mode 100644
index 000000000..889f2702f
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/RegisterModel.cs
@@ -0,0 +1,43 @@
+//-----------------------------------------------------------------------
+// <copyright file="RegisterModel.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Shared.Models.WebApi.Auth;
+
+/// <summary>
+/// This class represents the model for registering a new user
+/// using SRP (Secure Remote Password) protocol.
+/// </summary>
+public class RegisterModel
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="RegisterModel"/> class.
+ /// </summary>
+ /// <param name="email">Email.</param>
+ /// <param name="salt">Salt.</param>
+ /// <param name="verifier">Verifier.</param>
+ public RegisterModel(string email, string salt, string verifier)
+ {
+ Email = email;
+ Salt = salt;
+ Verifier = verifier;
+ }
+
+ /// <summary>
+ /// Gets or sets the email.
+ /// </summary>
+ public string Email { get; set; } = null!;
+
+ /// <summary>
+ /// Gets or sets the salt.
+ /// </summary>
+ public string Salt { get; set; }
+
+ /// <summary>
+ /// Gets or sets the verifier.
+ /// </summary>
+ public string Verifier { get; set; }
+}
diff --git a/src/AliasVault.WebApp/AliasVault.WebApp.csproj b/src/AliasVault.WebApp/AliasVault.WebApp.csproj
index a4b5b47a3..aeada7dec 100644
--- a/src/AliasVault.WebApp/AliasVault.WebApp.csproj
+++ b/src/AliasVault.WebApp/AliasVault.WebApp.csproj
@@ -18,14 +18,14 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\net8.0\AliasVault.WebApp.xml</DocumentationFile>
- <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
+ <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<CacheBuster>dev</CacheBuster>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<DocumentationFile>bin\Release\net8.0\AliasVault.WebApp.xml</DocumentationFile>
- <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
+ <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<Optimize>True</Optimize>
<CacheBuster>$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</CacheBuster>
</PropertyGroup>
@@ -57,6 +57,7 @@
<ProjectReference Include="..\AliasDb\AliasDb.csproj" />
<ProjectReference Include="..\AliasGenerators\AliasGenerators.csproj" />
<ProjectReference Include="..\AliasVault.Shared\AliasVault.Shared.csproj" />
+ <ProjectReference Include="..\Utilities\Cryptography\Cryptography.csproj" />
<ProjectReference Include="..\Utilities\FaviconExtractor\FaviconExtractor.csproj" />
</ItemGroup>
diff --git a/src/AliasVault.WebApp/Auth/Pages/Login.razor b/src/AliasVault.WebApp/Auth/Pages/Login.razor
index 0e5d21a30..e7af1aa1e 100644
--- a/src/AliasVault.WebApp/Auth/Pages/Login.razor
+++ b/src/AliasVault.WebApp/Auth/Pages/Login.razor
@@ -68,6 +68,14 @@
_loadingIndicator.Show();
_serverValidationErrors.Clear();
+ // Create argon2id hash of the password locally
+ var hash = await Cryptography.Cryptography.DeriveKeyFromPasswordAsync(_loginModel.Password, "aliasvault");
+
+ //var hash = "hello";
+ Console.WriteLine("Hash: " + hash);
+ //_loadingIndicator.Hide();
+ return;
+
try
{
var result = await Http.PostAsJsonAsync("api/v1/Auth/login", _loginModel);
diff --git a/src/Tests/AliasVault.UnitTests/CryptographyTests.cs b/src/Tests/AliasVault.UnitTests/CryptographyTests.cs
index f2cdddc8f..2ea5857be 100644
--- a/src/Tests/AliasVault.UnitTests/CryptographyTests.cs
+++ b/src/Tests/AliasVault.UnitTests/CryptographyTests.cs
@@ -35,7 +35,7 @@ public class CryptographyTests
// Derive a key from the password using Argon2id
byte[] key = Cryptography.Cryptography.DeriveKeyFromPassword(password, salt);
- Console.Write($"Derived key: {key})");
+ Console.WriteLine($"Derived key: {key.Length} bytes (hex: {BitConverter.ToString(key).Replace("-", string.Empty)})");
// Encrypt the plaintext
string encrypted = Cryptography.Cryptography.Encrypt(plaintext, key);
@@ -73,4 +73,52 @@ public class CryptographyTests
Assert.Throws<CryptographicException>(() => Cryptography.Cryptography.Decrypt(encrypted, key2));
}
+
+ /// <summary>
+ /// Test the SRP authentication flow to ensure it works correctly.
+ /// </summary>
+ [Test]
+ public void TestSRPAuthentication()
+ {
+ var email = "[email protected]";
+ var password = "myPassword";
+ var argon2IdSalt = "AliasVault";
+
+ // Registration -----------------------------
+
+ // Derive a key from the password using Argon2id
+ byte[] passwordHash = Cryptography.Cryptography.DeriveKeyFromPassword(password, argon2IdSalt);
+
+ // Convert to string
+ string passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
+
+ // Client generates a salt and verifier.
+ string salt = Cryptography.Srp.GenerateSalt()!;
+ string verifier = Cryptography.Srp.GenerateVerifier(email, passwordHashString, salt);
+
+ // Login -----------------------------------
+ // 1. Client generates an ephemeral value.
+ var clientEphemeral = Cryptography.Srp.GenerateEphemeral();
+
+ // --> Then client sends request to server.
+
+ // 2. Server retrieves salt and verifier from database.
+ // Then server generates an ephemeral value as well.
+ var serverEphemeral = Cryptography.Srp.GenerateEphemeral();
+
+ // --> Send serverEphemeral.Public to client.
+
+ // 3. Client derives shared session key.
+ var clientSession = Cryptography.Srp.DeriveSessionClient(passwordHashString, clientEphemeral.Secret, serverEphemeral.Public, salt, email);
+
+ // --> send session.Proof to server.
+
+ // 4. Server verifies the proof.
+ var serverSession = Cryptography.Srp.DeriveSessionServer(serverEphemeral.Secret, clientEphemeral.Public, salt, email, verifier, clientSession.Proof);
+
+ // --> send serverSession.Proof to client.
+
+ // 5. Client verifies the proof.
+ Cryptography.Srp.VerifySession(clientEphemeral.Public, clientSession, serverSession.Proof);
+ }
}
diff --git a/src/Utilities/Cryptography/Cryptography.cs b/src/Utilities/Cryptography/Cryptography.cs
index b618c69b1..f4d7635e1 100644
--- a/src/Utilities/Cryptography/Cryptography.cs
+++ b/src/Utilities/Cryptography/Cryptography.cs
@@ -30,14 +30,36 @@ public static class Cryptography
var argon2 = new Argon2id(passwordBytes)
{
Salt = saltBytes,
- DegreeOfParallelism = 8,
- MemorySize = 65536,
+ DegreeOfParallelism = 2,
+ MemorySize = 4096,
Iterations = 4,
};
return argon2.GetBytes(32); // Generate a 256-bit key
}
+ /// <summary>
+ /// Derive a key used for encryption/decryption based on a user password and system salt.
+ /// </summary>
+ /// <param name="password">User password.</param>
+ /// <param name="salt">The salt to use for the Argon2id hash.</param>
+ /// <returns>Encryption key as byte array.</returns>
+ public static async Task<byte[]> DeriveKeyFromPasswordAsync(string password, string salt)
+ {
+ byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
+ byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
+
+ var argon2 = new Argon2id(passwordBytes)
+ {
+ Salt = saltBytes,
+ DegreeOfParallelism = 4,
+ MemorySize = 8192,
+ Iterations = 1,
+ };
+
+ return await argon2.GetBytesAsync(32); // Generate a 256-bit key
+ }
+
/// <summary>
/// Encrypt a plaintext string using AES-256.
/// </summary>
diff --git a/src/Utilities/Cryptography/Cryptography.csproj b/src/Utilities/Cryptography/Cryptography.csproj
index c500a66fe..0aad782ff 100644
--- a/src/Utilities/Cryptography/Cryptography.csproj
+++ b/src/Utilities/Cryptography/Cryptography.csproj
@@ -22,6 +22,7 @@
<ItemGroup>
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0" />
+ <PackageReference Include="srp" Version="1.0.7" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
diff --git a/src/Utilities/Cryptography/Srp.cs b/src/Utilities/Cryptography/Srp.cs
new file mode 100644
index 000000000..958887a74
--- /dev/null
+++ b/src/Utilities/Cryptography/Srp.cs
@@ -0,0 +1,109 @@
+//-----------------------------------------------------------------------
+// <copyright file="Srp.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace Cryptography;
+
+using System.Security.Cryptography;
+using SecureRemotePassword;
+
+/// <summary>
+/// SRP is a secure remote password protocol that allows a user to authenticate themselves
+/// to a server without sending the password over the network.
+/// </summary>
+public class Srp
+{
+ /// <summary>
+ /// Generate a random salt.
+ /// </summary>
+ /// <returns>Byte array.</returns>
+ public static string? GenerateSalt()
+ {
+ var client = new SrpClient();
+ var salt = client.GenerateSalt();
+ return salt;
+ }
+
+ /// <summary>
+ /// Generate a verifier for a user.
+ /// </summary>
+ /// <param name="email">Email.</param>
+ /// <param name="passwordHash">Hash as output of Argon2id.</param>
+ /// <param name="salt">Salt.</param>
+ /// <returns>Verifier as string.</returns>
+ public static string GenerateVerifier(string email, string passwordHash, string salt)
+ {
+ var client = new SrpClient();
+ var privateKey = client.DerivePrivateKey(salt, email, passwordHash);
+ var verifier = client.DeriveVerifier(privateKey);
+ return verifier;
+ }
+
+ /// <summary>
+ /// Generate an ephemeral value for the client.
+ /// </summary>
+ /// <returns>Ephemeral as string.</returns>
+ public static SrpEphemeral GenerateEphemeral()
+ {
+ var client = new SrpClient();
+ return client.GenerateEphemeral();
+ }
+
+ /// <summary>
+ /// Derive a shared session key on the client side.
+ /// </summary>
+ /// <param name="passwordHash">Password hash.</param>
+ /// <param name="clientEphemeralSecret">Client ephemeral secret.</param>
+ /// <param name="serverPublicEphemeral">Server public ephemeral.</param>
+ /// <param name="salt">Salt.</param>
+ /// <param name="email">Email.</param>
+ /// <returns>session.</returns>
+ public static SrpSession DeriveSessionClient(string passwordHash, string clientEphemeralSecret, string serverPublicEphemeral, string salt, string email)
+ {
+ var client = new SrpClient();
+ var privateKey = client.DerivePrivateKey(salt, email, passwordHash);
+ return client.DeriveSession(
+ clientEphemeralSecret,
+ serverPublicEphemeral,
+ salt,
+ email,
+ privateKey);
+ }
+
+ /// <summary>
+ /// Derive a shared session key on the server side.
+ /// </summary>
+ /// <param name="serverEphemeralSecret">serverEphemeralSecret.</param>
+ /// <param name="clientEphemeralPublic">clientEphemeralPublic.</param>
+ /// <param name="salt">Salt.</param>
+ /// <param name="email">Email.</param>
+ /// <param name="verifier">Verifier.</param>
+ /// <param name="clientSessionProof">Client session proof.</param>
+ /// <returns>SrpSession.</returns>
+ public static SrpSession DeriveSessionServer(string serverEphemeralSecret, string clientEphemeralPublic, string salt, string email, string verifier, string clientSessionProof)
+ {
+ var server = new SrpServer();
+ return server.DeriveSession(
+ serverEphemeralSecret,
+ clientEphemeralPublic,
+ salt,
+ email,
+ verifier,
+ clientSessionProof);
+ }
+
+ /// <summary>
+ /// Verify the session on the client side.
+ /// </summary>
+ /// <param name="clientEphemeralPublic">clientEphemeralPublic.</param>
+ /// <param name="clientSession">clientSession.</param>
+ /// <param name="serverSessionProof">serverSessionProof.</param>
+ public static void VerifySession(string clientEphemeralPublic, SrpSession clientSession, string serverSessionProof)
+ {
+ var client = new SrpClient();
+ client.VerifySession(clientEphemeralPublic, clientSession, serverSessionProof);
+ }
+}