-
Notifications
You must be signed in to change notification settings - Fork 5k
PKCS#8 support for ML-DSA #115569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PranavSenthilnathan
merged 6 commits into
dotnet:main
from
PranavSenthilnathan:mldsa-pkcs8
May 20, 2025
Merged
PKCS#8 support for ML-DSA #115569
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5795607
ML-DSA pkcs8
PranavSenthilnathan 7bc363c
Apply suggestions from code review
PranavSenthilnathan 3a5d949
fix tests
PranavSenthilnathan 70ddb9a
address PR feedback
PranavSenthilnathan c2b820f
Update src/libraries/Common/tests/System/Security/Cryptography/Algori…
PranavSenthilnathan ebec88e
Remove unnecessary null check
PranavSenthilnathan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/libraries/Common/src/System/Security/Cryptography/Asn1/MLDsaPrivateKeyAsn.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<asn:Choice | ||
xmlns:asn="http://schemas.dot.net/asnxml/201808/" | ||
name="MLDsaPrivateKeyAsn" | ||
namespace="System.Security.Cryptography.Asn1"> | ||
|
||
<!-- | ||
https://github.com/lamps-wg/dilithium-certificates/blob/5b23428b08a53aacdb89d93422b81228433e34d8/draft-ietf-lamps-dilithium-certificates.md | ||
|
||
ML-DSA-44-PrivateKey ::= CHOICE { | ||
seed [0] OCTET STRING (SIZE (32)), | ||
expandedKey OCTET STRING (SIZE (2560)), | ||
both SEQUENCE { | ||
seed OCTET STRING (SIZE (32)), | ||
expandedKey OCTET STRING (SIZE (2560)) | ||
} | ||
} | ||
|
||
ML-DSA-65-PrivateKey ::= CHOICE { | ||
seed [0] OCTET STRING (SIZE (32)), | ||
expandedKey OCTET STRING (SIZE (4032)), | ||
both SEQUENCE { | ||
seed OCTET STRING (SIZE (32)), | ||
expandedKey OCTET STRING (SIZE (4032)) | ||
} | ||
} | ||
|
||
ML-DSA-87-PrivateKey ::= CHOICE { | ||
seed [0] OCTET STRING (SIZE (32)), | ||
expandedKey OCTET STRING (SIZE (4896)), | ||
both SEQUENCE { | ||
seed OCTET STRING (SIZE (32)), | ||
expandedKey OCTET STRING (SIZE (4896)) | ||
} | ||
} | ||
--> | ||
<asn:OctetString name="Seed" implicitTag="0" /> | ||
<asn:OctetString name="ExpandedKey" /> | ||
<asn:AsnType name="Both" typeName="System.Security.Cryptography.Asn1.MLDsaPrivateKeyBothAsn" /> | ||
</asn:Choice> |
150 changes: 150 additions & 0 deletions
150
src/libraries/Common/src/System/Security/Cryptography/Asn1/MLDsaPrivateKeyAsn.xml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma warning disable SA1028 // ignore whitespace warnings for generated code | ||
using System; | ||
using System.Formats.Asn1; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Security.Cryptography.Asn1 | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal partial struct MLDsaPrivateKeyAsn | ||
{ | ||
internal ReadOnlyMemory<byte>? Seed; | ||
internal ReadOnlyMemory<byte>? ExpandedKey; | ||
internal System.Security.Cryptography.Asn1.MLDsaPrivateKeyBothAsn? Both; | ||
|
||
#if DEBUG | ||
static MLDsaPrivateKeyAsn() | ||
{ | ||
var usedTags = new System.Collections.Generic.Dictionary<Asn1Tag, string>(); | ||
Action<Asn1Tag, string> ensureUniqueTag = (tag, fieldName) => | ||
{ | ||
if (usedTags.TryGetValue(tag, out string? existing)) | ||
{ | ||
throw new InvalidOperationException($"Tag '{tag}' is in use by both '{existing}' and '{fieldName}'"); | ||
} | ||
|
||
usedTags.Add(tag, fieldName); | ||
}; | ||
|
||
ensureUniqueTag(new Asn1Tag(TagClass.ContextSpecific, 0), "Seed"); | ||
ensureUniqueTag(Asn1Tag.PrimitiveOctetString, "ExpandedKey"); | ||
ensureUniqueTag(Asn1Tag.Sequence, "Both"); | ||
} | ||
#endif | ||
|
||
internal readonly void Encode(AsnWriter writer) | ||
{ | ||
bool wroteValue = false; | ||
|
||
if (Seed.HasValue) | ||
{ | ||
if (wroteValue) | ||
throw new CryptographicException(); | ||
|
||
writer.WriteOctetString(Seed.Value.Span, new Asn1Tag(TagClass.ContextSpecific, 0)); | ||
wroteValue = true; | ||
} | ||
|
||
if (ExpandedKey.HasValue) | ||
{ | ||
if (wroteValue) | ||
throw new CryptographicException(); | ||
|
||
writer.WriteOctetString(ExpandedKey.Value.Span); | ||
wroteValue = true; | ||
} | ||
|
||
if (Both.HasValue) | ||
{ | ||
if (wroteValue) | ||
throw new CryptographicException(); | ||
|
||
Both.Value.Encode(writer); | ||
wroteValue = true; | ||
} | ||
|
||
if (!wroteValue) | ||
{ | ||
throw new CryptographicException(); | ||
} | ||
} | ||
|
||
internal static MLDsaPrivateKeyAsn Decode(ReadOnlyMemory<byte> encoded, AsnEncodingRules ruleSet) | ||
{ | ||
try | ||
{ | ||
AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet); | ||
|
||
DecodeCore(ref reader, encoded, out MLDsaPrivateKeyAsn decoded); | ||
reader.ThrowIfNotEmpty(); | ||
return decoded; | ||
} | ||
catch (AsnContentException e) | ||
{ | ||
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); | ||
} | ||
} | ||
|
||
internal static void Decode(ref AsnValueReader reader, ReadOnlyMemory<byte> rebind, out MLDsaPrivateKeyAsn decoded) | ||
{ | ||
try | ||
{ | ||
DecodeCore(ref reader, rebind, out decoded); | ||
} | ||
catch (AsnContentException e) | ||
{ | ||
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); | ||
} | ||
} | ||
|
||
private static void DecodeCore(ref AsnValueReader reader, ReadOnlyMemory<byte> rebind, out MLDsaPrivateKeyAsn decoded) | ||
{ | ||
decoded = default; | ||
Asn1Tag tag = reader.PeekTag(); | ||
ReadOnlySpan<byte> rebindSpan = rebind.Span; | ||
int offset; | ||
ReadOnlySpan<byte> tmpSpan; | ||
|
||
if (tag.HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 0))) | ||
{ | ||
|
||
if (reader.TryReadPrimitiveOctetString(out tmpSpan, new Asn1Tag(TagClass.ContextSpecific, 0))) | ||
{ | ||
decoded.Seed = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); | ||
} | ||
else | ||
{ | ||
decoded.Seed = reader.ReadOctetString(new Asn1Tag(TagClass.ContextSpecific, 0)); | ||
} | ||
|
||
} | ||
else if (tag.HasSameClassAndValue(Asn1Tag.PrimitiveOctetString)) | ||
{ | ||
|
||
if (reader.TryReadPrimitiveOctetString(out tmpSpan)) | ||
{ | ||
decoded.ExpandedKey = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); | ||
} | ||
else | ||
{ | ||
decoded.ExpandedKey = reader.ReadOctetString(); | ||
} | ||
|
||
} | ||
else if (tag.HasSameClassAndValue(Asn1Tag.Sequence)) | ||
{ | ||
System.Security.Cryptography.Asn1.MLDsaPrivateKeyBothAsn tmpBoth; | ||
System.Security.Cryptography.Asn1.MLDsaPrivateKeyBothAsn.Decode(ref reader, rebind, out tmpBoth); | ||
decoded.Both = tmpBoth; | ||
|
||
} | ||
else | ||
{ | ||
throw new CryptographicException(); | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/libraries/Common/src/System/Security/Cryptography/Asn1/MLDsaPrivateKeyBothAsn.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<asn:Sequence | ||
xmlns:asn="http://schemas.dot.net/asnxml/201808/" | ||
name="MLDsaPrivateKeyBothAsn" | ||
namespace="System.Security.Cryptography.Asn1"> | ||
|
||
<!-- | ||
https://github.com/lamps-wg/dilithium-certificates/blob/5b23428b08a53aacdb89d93422b81228433e34d8/draft-ietf-lamps-dilithium-certificates.md | ||
|
||
both SEQUENCE { | ||
seed OCTET STRING (SIZE (32)), | ||
expandedKey OCTET STRING (SIZE (2560)) | ||
} | ||
--> | ||
<asn:OctetString name="Seed" /> | ||
<asn:OctetString name="ExpandedKey" /> | ||
</asn:Sequence> |
101 changes: 101 additions & 0 deletions
101
src/libraries/Common/src/System/Security/Cryptography/Asn1/MLDsaPrivateKeyBothAsn.xml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma warning disable SA1028 // ignore whitespace warnings for generated code | ||
using System; | ||
using System.Formats.Asn1; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Security.Cryptography.Asn1 | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal partial struct MLDsaPrivateKeyBothAsn | ||
{ | ||
internal ReadOnlyMemory<byte> Seed; | ||
internal ReadOnlyMemory<byte> ExpandedKey; | ||
|
||
internal readonly void Encode(AsnWriter writer) | ||
{ | ||
Encode(writer, Asn1Tag.Sequence); | ||
} | ||
|
||
internal readonly void Encode(AsnWriter writer, Asn1Tag tag) | ||
{ | ||
writer.PushSequence(tag); | ||
|
||
writer.WriteOctetString(Seed.Span); | ||
writer.WriteOctetString(ExpandedKey.Span); | ||
writer.PopSequence(tag); | ||
} | ||
|
||
internal static MLDsaPrivateKeyBothAsn Decode(ReadOnlyMemory<byte> encoded, AsnEncodingRules ruleSet) | ||
{ | ||
return Decode(Asn1Tag.Sequence, encoded, ruleSet); | ||
} | ||
|
||
internal static MLDsaPrivateKeyBothAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory<byte> encoded, AsnEncodingRules ruleSet) | ||
{ | ||
try | ||
{ | ||
AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet); | ||
|
||
DecodeCore(ref reader, expectedTag, encoded, out MLDsaPrivateKeyBothAsn decoded); | ||
reader.ThrowIfNotEmpty(); | ||
return decoded; | ||
} | ||
catch (AsnContentException e) | ||
{ | ||
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); | ||
} | ||
} | ||
|
||
internal static void Decode(ref AsnValueReader reader, ReadOnlyMemory<byte> rebind, out MLDsaPrivateKeyBothAsn decoded) | ||
{ | ||
Decode(ref reader, Asn1Tag.Sequence, rebind, out decoded); | ||
} | ||
|
||
internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory<byte> rebind, out MLDsaPrivateKeyBothAsn decoded) | ||
{ | ||
try | ||
{ | ||
DecodeCore(ref reader, expectedTag, rebind, out decoded); | ||
} | ||
catch (AsnContentException e) | ||
{ | ||
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e); | ||
} | ||
} | ||
|
||
private static void DecodeCore(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory<byte> rebind, out MLDsaPrivateKeyBothAsn decoded) | ||
{ | ||
decoded = default; | ||
AsnValueReader sequenceReader = reader.ReadSequence(expectedTag); | ||
ReadOnlySpan<byte> rebindSpan = rebind.Span; | ||
int offset; | ||
ReadOnlySpan<byte> tmpSpan; | ||
|
||
|
||
if (sequenceReader.TryReadPrimitiveOctetString(out tmpSpan)) | ||
{ | ||
decoded.Seed = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); | ||
} | ||
else | ||
{ | ||
decoded.Seed = sequenceReader.ReadOctetString(); | ||
} | ||
|
||
|
||
if (sequenceReader.TryReadPrimitiveOctetString(out tmpSpan)) | ||
{ | ||
decoded.ExpandedKey = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray(); | ||
} | ||
else | ||
{ | ||
decoded.ExpandedKey = sequenceReader.ReadOctetString(); | ||
} | ||
|
||
|
||
sequenceReader.ThrowIfNotEmpty(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.