Repository navigation
CompositeML-KEM support for EnvelopedCms #134449
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
672ce9d
Add Composite ML-KEM support to EnvelopedCms
vcsjones d0c98ab
Expand Composite ML-KEM EnvelopedCms coverage
vcsjones e860e1b
Harden Composite ML-KEM EnvelopedCms tests
vcsjones 15113bb
Remove bring-up test
vcsjones e082e55
Merge branch 'main' into comp-ml-kem-enveloped-cms
vcsjones 2faa458
Merge branch 'main' into comp-ml-kem-enveloped-cms
vcsjones afb52ca
Merge branch 'main' into comp-ml-kem-enveloped-cms
vcsjones 31b2b5e
Code review feedback
vcsjones 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
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
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
210 changes: 210 additions & 0 deletions
210
...braries/System.Security.Cryptography.Pkcs/tests/EnvelopedCms/CompositeMLKemCmsTestData.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,210 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Formats.Asn1; | ||
| using System.Linq; | ||
| using System.Security.Cryptography.Asn1; | ||
| using System.Security.Cryptography.Asn1.Pkcs7; | ||
| using System.Security.Cryptography.Pkcs.Asn1; | ||
| using System.Security.Cryptography.Tests; | ||
| using System.Security.Cryptography.X509Certificates; | ||
|
|
||
| using TestOids = System.Security.Cryptography.Pkcs.Tests.Oids; | ||
|
|
||
| namespace System.Security.Cryptography.Pkcs.EnvelopedCmsTests.Tests | ||
| { | ||
| internal static class CompositeMLKemCmsTestData | ||
| { | ||
| private const string IdSmimeOriKem = "1.2.840.113549.1.9.16.13.3"; | ||
|
|
||
| internal static CompositeMLKemTestVector X25519Vector { get; } = | ||
| GetVector(CompositeMLKemAlgorithm.MLKem768WithX25519); | ||
|
|
||
| internal static CompositeMLKemTestVector P256Vector { get; } = | ||
| GetVector(CompositeMLKemAlgorithm.MLKem768WithECDiffieHellmanP256); | ||
|
|
||
| internal static byte[] BuildEnvelopedData( | ||
| CompositeMLKemTestVector vector, | ||
| HashAlgorithmName kdfAlgorithm = default, | ||
| string kdfOid = null, | ||
| string wrapOid = null, | ||
| int kekLength = 32, | ||
| byte[] ukm = null, | ||
| int version = 0, | ||
| string kemOid = null, | ||
| byte[] kemCiphertext = null, | ||
| int? encryptedKeyLength = null, | ||
| bool includeKemParameters = false, | ||
| bool includeKdfParameters = false, | ||
| bool includeWrapParameters = false) | ||
| { | ||
| kdfAlgorithm = kdfAlgorithm == default ? HashAlgorithmName.SHA384 : kdfAlgorithm; | ||
| kdfOid ??= TestOids.HkdfSha384; | ||
| wrapOid ??= TestOids.Aes256Wrap; | ||
|
|
||
| using (X509Certificate2 certificate = X509CertificateLoader.LoadCertificate(vector.Certificate)) | ||
| { | ||
| kemOid ??= certificate.GetKeyAlgorithm(); | ||
| } | ||
|
|
||
| byte[] contentEncryptionKey = | ||
| [ | ||
| 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, | ||
| 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, | ||
| 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, | ||
| 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, | ||
| ]; | ||
| byte[] iv = | ||
| [ | ||
| 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, | ||
| 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, | ||
| ]; | ||
| byte[] keyEncryptionKey = new byte[kekLength]; | ||
| byte[] encryptedKey; | ||
| AlgorithmIdentifierAsn wrap = CreateAlgorithmIdentifier(wrapOid, includeWrapParameters); | ||
| ReadOnlyMemory<byte>? userKeyingMaterial = null; | ||
|
|
||
| if (ukm is not null) | ||
| { | ||
| userKeyingMaterial = ukm; | ||
| } | ||
|
|
||
| CmsOriForKemOtherInfoAsn kdfInfo = new CmsOriForKemOtherInfoAsn | ||
| { | ||
| Wrap = wrap, | ||
| KekLength = kekLength, | ||
| Ukm = userKeyingMaterial, | ||
| }; | ||
|
|
||
| HKDF.DeriveKey( | ||
| kdfAlgorithm, | ||
| vector.SharedSecret, | ||
| keyEncryptionKey, | ||
| salt: [], | ||
| Encode(kdfInfo)); | ||
|
|
||
| // If encryptedKeyLength has a value then it is testing invalid inputs such as incorrect ciphertext lengths | ||
| // or contents, so the ciphertext is a simple zero value. | ||
| if (encryptedKeyLength.HasValue) | ||
| { | ||
| encryptedKey = new byte[encryptedKeyLength.Value]; | ||
| } | ||
| else | ||
| { | ||
| using (Aes aes = Aes.Create()) | ||
| { | ||
| aes.SetKey(keyEncryptionKey); | ||
| encryptedKey = aes.EncryptKeyWrap(contentEncryptionKey); | ||
| } | ||
| } | ||
|
|
||
| byte[] encryptedContent; | ||
|
|
||
| using (Aes aes = Aes.Create()) | ||
| { | ||
| aes.SetKey(contentEncryptionKey); | ||
| encryptedContent = aes.EncryptCbc("hello world!"u8, iv, PaddingMode.PKCS7); | ||
| } | ||
|
|
||
| KemRecipientInfoAsn kemRecipientInfo = new KemRecipientInfoAsn | ||
| { | ||
| Version = version, | ||
| Rid = new RecipientIdentifierAsn | ||
| { | ||
| SubjectKeyIdentifier = new byte[] { 1, 2, 3 }, | ||
| }, | ||
| Kem = CreateAlgorithmIdentifier(kemOid, includeKemParameters), | ||
| Kemct = kemCiphertext ?? vector.Ciphertext.ToArray(), | ||
| Kdf = CreateAlgorithmIdentifier(kdfOid, includeKdfParameters), | ||
| KekLength = kekLength, | ||
| Ukm = userKeyingMaterial, | ||
| Wrap = wrap, | ||
| EncryptedKey = encryptedKey, | ||
| }; | ||
| RecipientInfoAsn recipientInfo = new RecipientInfoAsn | ||
| { | ||
| Ori = new OtherRecipientInfoAsn | ||
| { | ||
| OriType = IdSmimeOriKem, | ||
| OriValue = Encode(kemRecipientInfo), | ||
| }, | ||
| }; | ||
| EnvelopedDataAsn envelopedData = new EnvelopedDataAsn | ||
| { | ||
| Version = 3, | ||
| RecipientInfos = [recipientInfo], | ||
| EncryptedContentInfo = new EncryptedContentInfoAsn | ||
| { | ||
| ContentType = TestOids.Pkcs7Data, | ||
| ContentEncryptionAlgorithm = new AlgorithmIdentifierAsn | ||
| { | ||
| Algorithm = TestOids.Aes256, | ||
| Parameters = EncodeOctetString(iv), | ||
| }, | ||
| EncryptedContent = encryptedContent, | ||
| }, | ||
| }; | ||
| ContentInfoAsn contentInfo = new ContentInfoAsn | ||
| { | ||
| ContentType = TestOids.Pkcs7Enveloped, | ||
| Content = Encode(envelopedData), | ||
| }; | ||
|
|
||
| return Encode(contentInfo); | ||
| } | ||
|
|
||
| private static CompositeMLKemTestVector GetVector(CompositeMLKemAlgorithm algorithm) => | ||
| CompositeMLKemTestData.AllIetfVectors.Single(vector => vector.Algorithm == algorithm); | ||
|
|
||
| private static AlgorithmIdentifierAsn CreateAlgorithmIdentifier(string oid, bool includeNullParameters) | ||
| { | ||
| ReadOnlyMemory<byte>? parameters = null; | ||
|
|
||
| if (includeNullParameters) | ||
| { | ||
| parameters = new byte[] { 0x05, 0x00 }; | ||
| } | ||
|
|
||
| return new AlgorithmIdentifierAsn | ||
| { | ||
| Algorithm = oid, | ||
| Parameters = parameters, | ||
| }; | ||
| } | ||
|
|
||
| private static byte[] Encode(CmsOriForKemOtherInfoAsn value) | ||
| { | ||
| AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); | ||
| value.Encode(writer); | ||
| return writer.Encode(); | ||
| } | ||
|
|
||
| private static byte[] Encode(ContentInfoAsn value) | ||
| { | ||
| AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); | ||
| value.Encode(writer); | ||
| return writer.Encode(); | ||
| } | ||
|
|
||
| private static byte[] Encode(EnvelopedDataAsn value) | ||
| { | ||
| AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); | ||
| value.Encode(writer); | ||
| return writer.Encode(); | ||
| } | ||
|
|
||
| private static byte[] Encode(KemRecipientInfoAsn value) | ||
| { | ||
| AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); | ||
| value.Encode(writer); | ||
| return writer.Encode(); | ||
| } | ||
|
|
||
| private static byte[] EncodeOctetString(byte[] value) | ||
| { | ||
| AsnWriter writer = new AsnWriter(AsnEncodingRules.DER); | ||
| writer.WriteOctetString(value); | ||
| return writer.Encode(); | ||
| } | ||
| } | ||
| } | ||
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.