Skip to content

Commit 1595d60

Browse files
Add Composite ML-KEM base APIs (#132440)
Adds the approved Composite ML-KEM base APIs, algorithm identifiers, import/export support, and contract tests. Platform implementations will follow. > [!NOTE] > GitHub Copilot helped create this PR. Contributes to #129633 --------- Copilot-Session: 5676db47-d54b-4946-97ec-7fc0a7392c30 Copilot-Session: 17bb5da2-badb-480f-a196-71dc1e23dc7b
1 parent f996208 commit 1595d60

17 files changed

Lines changed: 5533 additions & 1 deletion

‎src/libraries/Common/src/System/Security/Cryptography/CompositeMLKem.cs‎

Lines changed: 2259 additions & 0 deletions
Large diffs are not rendered by default.

‎src/libraries/Common/src/System/Security/Cryptography/CompositeMLKemAlgorithm.cs‎

Lines changed: 604 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#pragma warning disable IDE0060 // Remove unused parameter
5+
6+
namespace System.Security.Cryptography
7+
{
8+
internal sealed partial class CompositeMLKemImplementation : CompositeMLKem
9+
{
10+
public CompositeMLKemImplementation(CompositeMLKemAlgorithm algorithm)
11+
: base(algorithm)
12+
{
13+
throw new PlatformNotSupportedException();
14+
}
15+
16+
internal static partial bool IsAlgorithmSupportedImpl(CompositeMLKemAlgorithm algorithm) => false;
17+
18+
internal static partial CompositeMLKem GenerateKeyImpl(CompositeMLKemAlgorithm algorithm) =>
19+
throw new PlatformNotSupportedException();
20+
21+
internal static partial CompositeMLKem ImportEncapsulationKeyImpl(CompositeMLKemAlgorithm algorithm, ReadOnlySpan<byte> source) =>
22+
throw new PlatformNotSupportedException();
23+
24+
internal static partial CompositeMLKem ImportDecapsulationKeyImpl(CompositeMLKemAlgorithm algorithm, ReadOnlySpan<byte> source) =>
25+
throw new PlatformNotSupportedException();
26+
27+
protected override void EncapsulateCore(Span<byte> ciphertext, Span<byte> sharedSecret) =>
28+
throw new PlatformNotSupportedException();
29+
30+
protected override void DecapsulateCore(ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) =>
31+
throw new PlatformNotSupportedException();
32+
33+
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
34+
throw new PlatformNotSupportedException();
35+
36+
protected override int ExportEncapsulationKeyCore(Span<byte> destination) =>
37+
throw new PlatformNotSupportedException();
38+
39+
protected override int ExportDecapsulationKeyCore(Span<byte> destination) =>
40+
throw new PlatformNotSupportedException();
41+
}
42+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Security.Cryptography
5+
{
6+
internal sealed partial class CompositeMLKemImplementation : CompositeMLKem
7+
{
8+
internal static partial bool IsAlgorithmSupportedImpl(CompositeMLKemAlgorithm algorithm);
9+
10+
internal static partial CompositeMLKem GenerateKeyImpl(CompositeMLKemAlgorithm algorithm);
11+
12+
internal static partial CompositeMLKem ImportEncapsulationKeyImpl(CompositeMLKemAlgorithm algorithm, ReadOnlySpan<byte> source);
13+
14+
internal static partial CompositeMLKem ImportDecapsulationKeyImpl(CompositeMLKemAlgorithm algorithm, ReadOnlySpan<byte> source);
15+
}
16+
}

‎src/libraries/Common/src/System/Security/Cryptography/Oids.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ internal static partial class Oids
154154
internal const string MLDsa87WithRSA4096PssPreHashSha512 = "1.3.6.1.5.5.7.6.53";
155155
internal const string MLDsa87WithECDsaP521PreHashSha512 = "1.3.6.1.5.5.7.6.54";
156156

157+
internal const string MLKem768WithRsaOaep2048Sha3_256 = "1.3.6.1.5.5.7.6.55";
158+
internal const string MLKem768WithRsaOaep3072Sha3_256 = "1.3.6.1.5.5.7.6.56";
159+
internal const string MLKem768WithRsaOaep4096Sha3_256 = "1.3.6.1.5.5.7.6.57";
160+
internal const string MLKem768WithX25519Sha3_256 = "1.3.6.1.5.5.7.6.58";
161+
internal const string MLKem768WithECDiffieHellmanP256Sha3_256 = "1.3.6.1.5.5.7.6.59";
162+
internal const string MLKem768WithECDiffieHellmanP384Sha3_256 = "1.3.6.1.5.5.7.6.60";
163+
internal const string MLKem768WithECDiffieHellmanBrainpoolP256r1Sha3_256 = "1.3.6.1.5.5.7.6.61";
164+
internal const string MLKem1024WithRsaOaep3072Sha3_256 = "1.3.6.1.5.5.7.6.62";
165+
internal const string MLKem1024WithECDiffieHellmanP384Sha3_256 = "1.3.6.1.5.5.7.6.63";
166+
internal const string MLKem1024WithECDiffieHellmanBrainpoolP384r1Sha3_256 = "1.3.6.1.5.5.7.6.64";
167+
internal const string MLKem1024WithX448Sha3_256 = "1.3.6.1.5.5.7.6.65";
168+
internal const string MLKem1024WithECDiffieHellmanP521Sha3_256 = "1.3.6.1.5.5.7.6.66";
169+
157170
// PKCS#7
158171
internal const string NoSignature = "1.3.6.1.5.5.7.6.2";
159172

‎src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/CompositeMLKem/CompositeMLKemContractTests.cs‎

Lines changed: 993 additions & 0 deletions
Large diffs are not rendered by default.

‎src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/CompositeMLKem/CompositeMLKemFactoryTests.cs‎

Lines changed: 433 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace System.Security.Cryptography.Tests
7+
{
8+
internal sealed class CompositeMLKemMockImplementation : CompositeMLKem
9+
{
10+
internal static CompositeMLKemMockImplementation Create(CompositeMLKemAlgorithm algorithm) =>
11+
new CompositeMLKemMockImplementation(algorithm);
12+
13+
public CompositeMLKemMockImplementation(CompositeMLKemAlgorithm algorithm)
14+
: base(algorithm)
15+
{
16+
}
17+
18+
internal delegate void EncapsulateAction(Span<byte> ciphertext, Span<byte> sharedSecret);
19+
internal delegate void DecapsulateAction(ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret);
20+
internal delegate int ExportFunc(Span<byte> destination);
21+
internal delegate bool TryExportFunc(Span<byte> destination, out int written);
22+
internal delegate void DisposeAction(bool disposing);
23+
24+
public int EncapsulateCoreCallCount;
25+
public int DecapsulateCoreCallCount;
26+
public int ExportEncapsulationKeyCoreCallCount;
27+
public int ExportDecapsulationKeyCoreCallCount;
28+
public int TryExportPkcs8PrivateKeyCoreCallCount;
29+
public int DisposeCallCount;
30+
31+
public EncapsulateAction EncapsulateCoreHook { get; set; } = (_, _) => Assert.Fail();
32+
public DecapsulateAction DecapsulateCoreHook { get; set; } = (_, _) => Assert.Fail();
33+
public ExportFunc ExportEncapsulationKeyCoreHook { get; set; } = _ => { Assert.Fail(); return 0; };
34+
public ExportFunc ExportDecapsulationKeyCoreHook { get; set; } = _ => { Assert.Fail(); return 0; };
35+
public TryExportFunc TryExportPkcs8PrivateKeyCoreHook { get; set; } = (Span<byte> destination, out int bytesWritten) => { Assert.Fail(); bytesWritten = 0; return false; };
36+
public DisposeAction DisposeHook { get; set; } = _ => { };
37+
38+
protected override void EncapsulateCore(Span<byte> ciphertext, Span<byte> sharedSecret)
39+
{
40+
EncapsulateCoreCallCount++;
41+
EncapsulateCoreHook(ciphertext, sharedSecret);
42+
}
43+
44+
protected override void DecapsulateCore(ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret)
45+
{
46+
DecapsulateCoreCallCount++;
47+
DecapsulateCoreHook(ciphertext, sharedSecret);
48+
}
49+
50+
protected override int ExportEncapsulationKeyCore(Span<byte> destination)
51+
{
52+
ExportEncapsulationKeyCoreCallCount++;
53+
return ExportEncapsulationKeyCoreHook(destination);
54+
}
55+
56+
protected override int ExportDecapsulationKeyCore(Span<byte> destination)
57+
{
58+
ExportDecapsulationKeyCoreCallCount++;
59+
return ExportDecapsulationKeyCoreHook(destination);
60+
}
61+
62+
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten)
63+
{
64+
TryExportPkcs8PrivateKeyCoreCallCount++;
65+
return TryExportPkcs8PrivateKeyCoreHook(destination, out bytesWritten);
66+
}
67+
68+
protected override void Dispose(bool disposing)
69+
{
70+
DisposeCallCount++;
71+
DisposeHook(disposing);
72+
}
73+
74+
public void AddLengthAssertion()
75+
{
76+
EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook;
77+
EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) =>
78+
{
79+
oldEncapsulateCoreHook(ciphertext, sharedSecret);
80+
Assert.Equal(Algorithm.CiphertextSizeInBytes, ciphertext.Length);
81+
Assert.Equal(Algorithm.SharedSecretSizeInBytes, sharedSecret.Length);
82+
};
83+
84+
DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook;
85+
DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) =>
86+
{
87+
oldDecapsulateCoreHook(ciphertext, sharedSecret);
88+
Assert.Equal(Algorithm.CiphertextSizeInBytes, ciphertext.Length);
89+
Assert.Equal(Algorithm.SharedSecretSizeInBytes, sharedSecret.Length);
90+
};
91+
92+
ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook;
93+
ExportEncapsulationKeyCoreHook = (Span<byte> destination) =>
94+
{
95+
int ret = oldExportEncapsulationKeyCoreHook(destination);
96+
AssertExtensions.GreaterThanOrEqualTo(
97+
destination.Length,
98+
CompositeMLKemTestData.ExpectedEncapsulationKeySizeLowerBound(Algorithm));
99+
return ret;
100+
};
101+
102+
ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook;
103+
ExportDecapsulationKeyCoreHook = (Span<byte> destination) =>
104+
{
105+
int ret = oldExportDecapsulationKeyCoreHook(destination);
106+
AssertExtensions.GreaterThanOrEqualTo(
107+
destination.Length,
108+
CompositeMLKemTestData.ExpectedDecapsulationKeySizeLowerBound(Algorithm));
109+
return ret;
110+
};
111+
112+
TryExportFunc oldTryExportPkcs8PrivateKeyCoreHook = TryExportPkcs8PrivateKeyCoreHook;
113+
TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) =>
114+
{
115+
bool ret = oldTryExportPkcs8PrivateKeyCoreHook(destination, out bytesWritten);
116+
AssertExtensions.GreaterThanOrEqualTo(
117+
destination.Length,
118+
CompositeMLKemTestData.ExpectedDecapsulationKeySizeLowerBound(Algorithm));
119+
return ret;
120+
};
121+
}
122+
123+
public void AddDestinationBufferIsSameAssertion(ReadOnlyMemory<byte> buffer)
124+
{
125+
ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook;
126+
ExportEncapsulationKeyCoreHook = (Span<byte> destination) =>
127+
{
128+
int ret = oldExportEncapsulationKeyCoreHook(destination);
129+
AssertExtensions.Same(buffer.Span, destination);
130+
return ret;
131+
};
132+
133+
ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook;
134+
ExportDecapsulationKeyCoreHook = (Span<byte> destination) =>
135+
{
136+
int ret = oldExportDecapsulationKeyCoreHook(destination);
137+
AssertExtensions.Same(buffer.Span, destination);
138+
return ret;
139+
};
140+
}
141+
142+
public void AddCiphertextBufferIsSameAssertion(ReadOnlyMemory<byte> buffer)
143+
{
144+
EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook;
145+
EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) =>
146+
{
147+
oldEncapsulateCoreHook(ciphertext, sharedSecret);
148+
AssertExtensions.Same(buffer.Span, ciphertext);
149+
};
150+
151+
DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook;
152+
DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) =>
153+
{
154+
oldDecapsulateCoreHook(ciphertext, sharedSecret);
155+
AssertExtensions.Same(buffer.Span, ciphertext);
156+
};
157+
}
158+
159+
public void AddSharedSecretBufferIsSameAssertion(ReadOnlyMemory<byte> buffer)
160+
{
161+
EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook;
162+
EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) =>
163+
{
164+
oldEncapsulateCoreHook(ciphertext, sharedSecret);
165+
AssertExtensions.Same(buffer.Span, sharedSecret);
166+
};
167+
168+
DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook;
169+
DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) =>
170+
{
171+
oldDecapsulateCoreHook(ciphertext, sharedSecret);
172+
AssertExtensions.Same(buffer.Span, sharedSecret);
173+
};
174+
}
175+
176+
public void AddFillDestination(byte b)
177+
{
178+
EncapsulateAction oldEncapsulateCoreHook = EncapsulateCoreHook;
179+
EncapsulateCoreHook = (Span<byte> ciphertext, Span<byte> sharedSecret) =>
180+
{
181+
oldEncapsulateCoreHook(ciphertext, sharedSecret);
182+
ciphertext.Fill(b);
183+
sharedSecret.Fill(b);
184+
};
185+
186+
DecapsulateAction oldDecapsulateCoreHook = DecapsulateCoreHook;
187+
DecapsulateCoreHook = (ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret) =>
188+
{
189+
oldDecapsulateCoreHook(ciphertext, sharedSecret);
190+
sharedSecret.Fill(b);
191+
};
192+
193+
ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook;
194+
ExportEncapsulationKeyCoreHook = (Span<byte> destination) =>
195+
{
196+
_ = oldExportEncapsulationKeyCoreHook(destination);
197+
destination.Fill(b);
198+
return destination.Length;
199+
};
200+
201+
ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook;
202+
ExportDecapsulationKeyCoreHook = (Span<byte> destination) =>
203+
{
204+
_ = oldExportDecapsulationKeyCoreHook(destination);
205+
destination.Fill(b);
206+
return destination.Length;
207+
};
208+
209+
TryExportFunc oldTryExportPkcs8PrivateKeyCoreHook = TryExportPkcs8PrivateKeyCoreHook;
210+
TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) =>
211+
{
212+
bool ret = oldTryExportPkcs8PrivateKeyCoreHook(destination, out bytesWritten);
213+
destination.Fill(b);
214+
return ret;
215+
};
216+
}
217+
218+
public void SetNoOpHooks()
219+
{
220+
EncapsulateCoreHook = (_, _) => { };
221+
DecapsulateCoreHook = (_, _) => { };
222+
ExportEncapsulationKeyCoreHook = destination => destination.Length;
223+
ExportDecapsulationKeyCoreHook = destination => destination.Length;
224+
TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) =>
225+
{
226+
bytesWritten = destination.Length;
227+
return true;
228+
};
229+
}
230+
231+
public void AddFillDestination(byte[] fillContents)
232+
{
233+
ExportFunc oldExportEncapsulationKeyCoreHook = ExportEncapsulationKeyCoreHook;
234+
ExportEncapsulationKeyCoreHook = (Span<byte> destination) =>
235+
{
236+
_ = oldExportEncapsulationKeyCoreHook(destination);
237+
238+
if (fillContents.AsSpan().TryCopyTo(destination))
239+
{
240+
return fillContents.Length;
241+
}
242+
243+
return 0;
244+
};
245+
246+
ExportFunc oldExportDecapsulationKeyCoreHook = ExportDecapsulationKeyCoreHook;
247+
ExportDecapsulationKeyCoreHook = (Span<byte> destination) =>
248+
{
249+
_ = oldExportDecapsulationKeyCoreHook(destination);
250+
251+
if (fillContents.AsSpan().TryCopyTo(destination))
252+
{
253+
return fillContents.Length;
254+
}
255+
256+
return 0;
257+
};
258+
259+
TryExportFunc oldTryExportPkcs8PrivateKeyCoreHook = TryExportPkcs8PrivateKeyCoreHook;
260+
TryExportPkcs8PrivateKeyCoreHook = (Span<byte> destination, out int bytesWritten) =>
261+
{
262+
_ = oldTryExportPkcs8PrivateKeyCoreHook(destination, out _);
263+
264+
if (fillContents.AsSpan().TryCopyTo(destination))
265+
{
266+
bytesWritten = fillContents.Length;
267+
return true;
268+
}
269+
270+
bytesWritten = 0;
271+
return false;
272+
};
273+
}
274+
}
275+
}

0 commit comments

Comments
 (0)