Skip to content

Commit 464e5fe

Browse files
authored
ML-KEM: Fix missing check for SubjectPublicKeyInfo size
1 parent 46c3ac3 commit 464e5fe

File tree

2 files changed

+58
-1
lines changed
  • src/libraries/Common

2 files changed

+58
-1
lines changed

src/libraries/Common/src/System/Security/Cryptography/MLKem.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,14 @@ public static MLKem ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source)
13161316
AsnValueReader reader = new(source, AsnEncodingRules.DER);
13171317
SubjectPublicKeyInfoAsn.Decode(ref reader, manager.Memory, out SubjectPublicKeyInfoAsn spki);
13181318
MLKemAlgorithm algorithm = GetAlgorithmIdentifier(ref spki.Algorithm);
1319-
return MLKemImplementation.ImportEncapsulationKeyImpl(algorithm, spki.SubjectPublicKey.Span);
1319+
ReadOnlySpan<byte> subjectPublicKey = spki.SubjectPublicKey.Span;
1320+
1321+
if (subjectPublicKey.Length != algorithm.EncapsulationKeySizeInBytes)
1322+
{
1323+
throw new CryptographicException(SR.Argument_KemInvalidEncapsulationKeyLength);
1324+
}
1325+
1326+
return MLKemImplementation.ImportEncapsulationKeyImpl(algorithm, subjectPublicKey);
13201327
}
13211328
}
13221329
}

src/libraries/Common/tests/System/Security/Cryptography/MLKemTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,56 @@ public static void ImportSubjectPublicKeyInfo_NullSource()
164164
MLKem.ImportSubjectPublicKeyInfo((byte[])null));
165165
}
166166

167+
[Fact]
168+
public static void ImportSubjectPublicKeyInfo_WrongAlgorithm()
169+
{
170+
byte[] ecP256Spki = Convert.FromBase64String(@"
171+
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEuiPJ2IV089LVrXZGDo9Mc542UZZE
172+
UtPQVd60Ckb/u5OXHAlmITVzFPThKI+N/bUMEnnHEmF8ZDUtLiQPBaKiMQ==");
173+
Assert.Throws<CryptographicException>(() => MLKem.ImportSubjectPublicKeyInfo(ecP256Spki));
174+
}
175+
176+
[Fact]
177+
public static void ImportSubjectPublicKeyInfo_WrongParameters()
178+
{
179+
byte[] mlKem512BadParameters = (
180+
"30820342301B0609608648016503040401040E62616420706172616D65746572" +
181+
"730382032100002645126709F87B5C6DF9116BA175020895C5C3031AFBBFCDF9" +
182+
"5AF2839095396A21F23BB1232091EB8F983BCBC95400D4A335C555187FC2311B" +
183+
"79402A261AEB276F1DE4CA62458D5AA772F0C25C3A4824DC095AB63584CF863C" +
184+
"D4DCA9736CAAFADC1CA2249475946B4BF4B208334EC419C916157E84C9056DE7" +
185+
"13CD055D69009D295C1C1A6A07E008297BD1B167D4641ED946E0432CDD285A5E" +
186+
"9794E6D3CF7422012999AC50C42CACC68E659C37CDD93A70166192719CC0E22D" +
187+
"1317C1A3F00715004D505A2B606055626421611BCC64425D2F1452E32B8D68B2" +
188+
"584A66180D648B8925467DB306DFC39859EAC383AB60B1F9793051C8AE215162" +
189+
"1166B3F862B52C44CD796602C80F45D3C095749EA9D65EB6024752761B364891" +
190+
"C7669441E3AE30E43178D13B2D1C3B3AFC999FD4B444E3591320A766ACCA6C40" +
191+
"59B8F34289211F82C5A333EA029154C716B640AAC1BC7D516DFB00881EFA9472" +
192+
"725A9BC57DD1781E7A0689C5E399CB1B1F41A031D33C3528D421047A48A29C91" +
193+
"222074FCC538814242C78520D201171615518D36CC98BC885F2127CE182BA150" +
194+
"41B07737A8ABA7ACC4C0A22624D49C4854674A54F11AFFB03D7AE6B90E7413B8" +
195+
"54A131A6496D02744A66BA7E4B8DF973577EA902E7D1B7CA40CA7AA63F2C04B5" +
196+
"D6DA87A735148F003F78395010B11AC9C87E1AB7BDCD8B18CEBA7226E6899DD3" +
197+
"A7F2937686D52F7BA21D1A439A7DA06B367C240F644058C434337880FFE79DE9" +
198+
"F2A277B13B0B82907E22104F0134423A3F06167FF8254276922361C0368CBC73" +
199+
"BFB8C35CD1C0A79076C420B053E604C7098C74344F9A5094983B17ADD46C3498" +
200+
"4CA317CDED2B0A1ECC12C52124655389FDB813DBBA8B04310259F0A9EE57B03A" +
201+
"B04B18079ABFA336FFF854DA70BD82760A39FB3D9BB2837DF052E925449C8868" +
202+
"0242B73A39AB9D332A9C0371CD06663FC6A65968CA27663E44984B91C8B51A2C" +
203+
"B0B6364CCE2097DB286E01FB8D2C871472C68117EA6497196F5F56A3CE778D48" +
204+
"B687DAF440BB483748B7C2F0889F02DAB06EC64F33E59E49931A20084C7E7856" +
205+
"3A766B5223909DC385C5BC4BD8AFF51B5CC52F60FD181D8AC43537254ABC2F29" +
206+
"E8FCB8698CD4").HexToByteArray();
207+
Assert.Throws<CryptographicException>(() => MLKem.ImportSubjectPublicKeyInfo(mlKem512BadParameters));
208+
}
209+
210+
[Fact]
211+
public static void ImportSubjectPublicKeyInfo_WrongSize()
212+
{
213+
byte[] mlKem512BadEncapKey = "3014300B060960864801650304040103050000264512".HexToByteArray();
214+
Assert.Throws<CryptographicException>(() => MLKem.ImportSubjectPublicKeyInfo(mlKem512BadEncapKey));
215+
}
216+
167217
[Fact]
168218
public static void ImportPkcs8PrivateKey_NullSource()
169219
{

0 commit comments

Comments
 (0)