Skip to content

Commit 58c0cc9

Browse files
authored
Merge pull request #128 from dorssel/register
Reintroduce RegisterWithCryptoConfig
2 parents 7ed2d93 + d65811f commit 58c0cc9

File tree

5 files changed

+65
-26
lines changed

5 files changed

+65
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ runtimes/
1010
TestResults/
1111

1212
*.user
13+
*.lutconfig

Examples/ConsoleApp/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ static Program()
1717
static async Task Main()
1818
{
1919
{
20+
Xmss.RegisterWithCryptoConfig();
21+
2022
var oid = CryptoConfig.MapNameToOID("XMSS");
2123
Console.WriteLine($"Found OID for 'XMSS': {oid}");
2224
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code

UnitTests/Internal.UnitTests/VersionTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5+
using Dorssel.Security.Cryptography;
56
using Dorssel.Security.Cryptography.Internal;
67

78
namespace Internal.UnitTests;
@@ -38,4 +39,19 @@ public void XMSS_LIBRARY_GET_VERSION_PATCH()
3839
{
3940
Assert.AreEqual(0x56, Defines.XMSS_LIBRARY_GET_VERSION_PATCH(0x00123456));
4041
}
42+
43+
[TestMethod]
44+
public void ThrowIfVersionsNotEqual()
45+
{
46+
Xmss.ThrowIfVersionsNotEqual(Defines.XMSS_LIBRARY_VERSION, Defines.XMSS_LIBRARY_VERSION);
47+
}
48+
49+
[TestMethod]
50+
public void ThrowIfVersionsNotEqual_Throws()
51+
{
52+
Assert.ThrowsException<DllNotFoundException>(() =>
53+
{
54+
Xmss.ThrowIfVersionsNotEqual(Defines.XMSS_LIBRARY_VERSION, uint.MaxValue);
55+
});
56+
}
4157
}

UnitTests/UnitTests/CryptoConfigTests.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ namespace UnitTests;
1111
[TestClass]
1212
sealed class CryptoConfigTests
1313
{
14+
[TestMethod]
15+
public void IdAlgXmssHashsig()
16+
{
17+
Assert.IsNotNull(Xmss.IdAlgXmssHashsig.Value);
18+
Assert.IsNotNull(Xmss.IdAlgXmssHashsig.FriendlyName);
19+
}
20+
21+
[TestMethod]
22+
public void RegisterWithCryptoConfig()
23+
{
24+
Xmss.RegisterWithCryptoConfig();
25+
26+
Assert.AreEqual(Xmss.IdAlgXmssHashsig.Value, CryptoConfig.MapNameToOID(Xmss.IdAlgXmssHashsig.FriendlyName!));
27+
}
28+
29+
[TestMethod]
30+
public void RegisterWithCryptoConfig_Twice()
31+
{
32+
Xmss.RegisterWithCryptoConfig();
33+
Xmss.RegisterWithCryptoConfig();
34+
35+
Assert.AreEqual(Xmss.IdAlgXmssHashsig.Value, CryptoConfig.MapNameToOID(Xmss.IdAlgXmssHashsig.FriendlyName!));
36+
}
37+
1438
[TestMethod]
1539
public void Create()
1640
{
@@ -23,11 +47,10 @@ public void Create()
2347
[RequiresUnreferencedCode("Calls Dorssel.Security.Cryptography.Xmss.Create(String)")]
2448
public void Create_Name()
2549
{
26-
Assert.IsNotNull(Xmss.IdAlgXmssHashsig.FriendlyName);
27-
Assert.AreEqual("xmss", Xmss.IdAlgXmssHashsig.FriendlyName);
50+
Xmss.RegisterWithCryptoConfig();
2851

2952
#pragma warning disable SYSLIB0045 // Type or member is obsolete
30-
using var xmss = Xmss.Create("xmss");
53+
using var xmss = Xmss.Create(Xmss.IdAlgXmssHashsig.FriendlyName!);
3154
#pragma warning restore SYSLIB0045 // Type or member is obsolete
3255

3356
Assert.IsNotNull(xmss);

Xmss/Xmss.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Diagnostics.CodeAnalysis;
99
using System.Formats.Asn1;
1010
using System.Runtime.InteropServices;
11+
using System.Runtime.Versioning;
1112
using System.Security.Cryptography;
1213
using System.Security.Cryptography.X509Certificates;
1314
using Dorssel.Security.Cryptography.Internal;
@@ -24,20 +25,15 @@ public sealed class Xmss
2425
#region Construction
2526
static Xmss()
2627
{
27-
VerifyLibraryVersion();
28-
TryRegisterWithCryptoConfig();
28+
var runtimeVersion = SafeNativeMethods.xmss_library_get_version();
29+
ThrowIfVersionsNotEqual(Defines.XMSS_LIBRARY_VERSION, runtimeVersion);
2930
}
3031

31-
/// <summary>
32-
/// Throws if either the library cannot be loaded, or it does not expose the expected version (i.e., it is the wrong one).
33-
/// </summary>
34-
[ExcludeFromCodeCoverage(Justification = "Not testable.")]
35-
static void VerifyLibraryVersion()
32+
internal static void ThrowIfVersionsNotEqual(uint expected, uint actual)
3633
{
37-
var runtimeVersion = SafeNativeMethods.xmss_library_get_version();
38-
if (runtimeVersion != Defines.XMSS_LIBRARY_VERSION)
34+
if (actual != expected)
3935
{
40-
throw new DllNotFoundException($"XMSS library version mismatch ({runtimeVersion})");
36+
throw new DllNotFoundException($"XMSS library version mismatch ({actual})");
4137
}
4238
}
4339

@@ -72,22 +68,23 @@ public Xmss()
7268
/// <seealso href="https://www.ietf.org/archive/id/draft-ietf-lamps-x509-shbs-13.html#name-xmss-algorithm-identifier" />
7369
public static Oid IdAlgXmssHashsig { get; } = new("1.3.6.1.5.5.7.6.34", "xmss");
7470

75-
static void RegisterWithCryptoConfig()
76-
{
77-
CryptoConfig.AddAlgorithm(typeof(Xmss), IdAlgXmssHashsig.FriendlyName!);
78-
CryptoConfig.AddOID(IdAlgXmssHashsig.Value!, IdAlgXmssHashsig.FriendlyName!);
79-
}
71+
static readonly object RegistrationLock = new();
72+
static bool TriedRegisterOnce;
8073

81-
[ExcludeFromCodeCoverage(Justification = "Not testable; WASM only.")]
82-
static void TryRegisterWithCryptoConfig()
74+
/// <summary>
75+
/// TODO
76+
/// </summary>
77+
[UnsupportedOSPlatform("browser")]
78+
public static void RegisterWithCryptoConfig()
8379
{
84-
try
80+
lock (RegistrationLock)
8581
{
86-
RegisterWithCryptoConfig();
87-
}
88-
catch (PlatformNotSupportedException)
89-
{
90-
// CryptoConfig is unsupported for WASM.
82+
if (!TriedRegisterOnce)
83+
{
84+
TriedRegisterOnce = true;
85+
CryptoConfig.AddAlgorithm(typeof(Xmss), IdAlgXmssHashsig.FriendlyName!);
86+
CryptoConfig.AddOID(IdAlgXmssHashsig.Value!, IdAlgXmssHashsig.FriendlyName!);
87+
}
9188
}
9289
}
9390
#endregion

0 commit comments

Comments
 (0)