Skip to content

Commit

Permalink
Merge pull request #130 from dorssel/ephemeral
Browse files Browse the repository at this point in the history
Add ephemeral state manager
  • Loading branch information
dorssel authored Jan 3, 2025
2 parents 7f379f9 + 797e393 commit 8bae681
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 14 deletions.
98 changes: 98 additions & 0 deletions UnitTests/UnitTests/XmssEphemeralStateManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// SPDX-FileCopyrightText: 2025 Frans van Dorsselaer
//
// SPDX-License-Identifier: MIT

using Dorssel.Security.Cryptography;

namespace UnitTests;

[TestClass]
sealed class XmssEphemeralStateManagerTests
{
[TestMethod]
public void Constructor()
{
_ = new XmssEphemeralStateManager();
}

[TestMethod]
public void Store()
{
var stateManager = new XmssEphemeralStateManager();
stateManager.Store(XmssKeyPart.Public, [1]);
}

[TestMethod]
public void StoreStoreStatefulPart()
{
var stateManager = new XmssEphemeralStateManager();
stateManager.Store(XmssKeyPart.PrivateStateful, [1]);

stateManager.StoreStatefulPart([1], [2]);
}

[TestMethod]
public void Load()
{
var data = new byte[] { 1, 2, 3 };

var stateManager = new XmssEphemeralStateManager();
stateManager.Store(XmssKeyPart.Public, data);

var read = new byte[data.Length];

Assert.ThrowsException<NotImplementedException>(() =>
{
stateManager.Load(XmssKeyPart.Public, read);
});
}

[TestMethod]
public void DeletePublicPart()
{
var stateManager = new XmssEphemeralStateManager();
stateManager.Store(XmssKeyPart.Public, [1]);

stateManager.DeletePublicPart();
}

[TestMethod]
public void DeletePublicPart_NotExists()
{
var stateManager = new XmssEphemeralStateManager();

stateManager.DeletePublicPart();
}

[TestMethod]
public void DeleteAll()
{
var stateManager = new XmssEphemeralStateManager();
stateManager.Store(XmssKeyPart.PrivateStateless, [1]);
stateManager.Store(XmssKeyPart.PrivateStateful, [2]);
stateManager.Store(XmssKeyPart.Public, [3]);

stateManager.DeleteAll();
}

[TestMethod]
public void DeleteAll_NotExist()
{
var stateManager = new XmssEphemeralStateManager();

stateManager.DeleteAll();
}

[TestMethod]
public async Task Use()
{
using var xmss = new Xmss();

xmss.GeneratePrivateKey(new XmssEphemeralStateManager(), XmssParameterSet.XMSS_SHA2_10_256, false);
await xmss.CalculatePublicKeyAsync();
var message = new byte[] { 1, 2, 3 };
var signature = xmss.Sign(message);

Assert.IsTrue(xmss.Verify(message, signature));
}
}
38 changes: 38 additions & 0 deletions Xmss/XmssEphemeralStateManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2025 Frans van Dorsselaer
//
// SPDX-License-Identifier: MIT

namespace Dorssel.Security.Cryptography;

/// <summary>
/// TODO
/// </summary>
public sealed class XmssEphemeralStateManager()
: IXmssStateManager
{
/// <inheritdoc/>
public void Store(XmssKeyPart part, ReadOnlySpan<byte> data)
{
}

/// <inheritdoc/>
public void StoreStatefulPart(ReadOnlySpan<byte> expected, ReadOnlySpan<byte> data)
{
}

/// <inheritdoc/>
public void Load(XmssKeyPart part, Span<byte> destination)
{
throw new NotImplementedException();
}

/// <inheritdoc/>
public void DeletePublicPart()
{
}

/// <inheritdoc/>
public void DeleteAll()
{
}
}
17 changes: 10 additions & 7 deletions Xmss/XmssException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,34 @@
namespace Dorssel.Security.Cryptography;

/// <summary>
/// TODO
/// The exception that is thrown when an error occurs during a cryptographic operation of the native XMSS library.
/// </summary>
public class XmssException
: CryptographicException
{
/// <summary>
/// TODO
/// Initializes a new instance of the <see cref="XmssException"/> class with default properties.
/// </summary>
public XmssException()
{
}

/// <summary>
/// TODO
/// Initializes a new instance of the <see cref="XmssException"/> class with a specified error message.
/// </summary>
/// <param name="message">TODO</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
public XmssException(string message) : base(message)
{
}

/// <summary>
/// TODO
/// Initializes a new instance of the <see cref="XmssException"/> class with a specified error message
/// and a reference to the inner exception that is the cause of this exception.
/// </summary>
/// <param name="message">TODO</param>
/// <param name="innerException">TODO</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception.
/// If the <paramref name="innerException"/> parameter is not <see langword="null"/>,
/// the current exception is raised in a <see langword="catch"/> block that handles the inner exception.</param>
public XmssException(string message, Exception innerException) : base(message, innerException)
{
}
Expand Down
18 changes: 11 additions & 7 deletions Xmss/XmssStateManagerException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@
//
// SPDX-License-Identifier: MIT

using System.Security.Cryptography;

namespace Dorssel.Security.Cryptography;

/// <summary>
/// TODO
/// The exception that is thrown when an error occurs during a cryptographic operation that requires state management.
/// </summary>
public class XmssStateManagerException
: IOException
: CryptographicException
{
/// <summary>
/// TODO
/// Initializes a new instance of the <see cref="XmssStateManagerException"/> class with default properties.
/// </summary>
public XmssStateManagerException()
{
}

/// <summary>
/// TODO
/// Initializes a new instance of the <see cref="XmssStateManagerException"/> class with a specified error message.
/// </summary>
/// <param name="message">TODO</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
public XmssStateManagerException(string message) : base(message)
{
}

/// <summary>
/// TODO
/// </summary>
/// <param name="message">TODO</param>
/// <param name="innerException">TODO</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception.
/// If the <paramref name="innerException"/> parameter is not <see langword="null"/>,
/// the current exception is raised in a <see langword="catch"/> block that handles the inner exception.</param>
public XmssStateManagerException(string message, Exception innerException) : base(message, innerException)
{
}
Expand Down

0 comments on commit 8bae681

Please sign in to comment.