|
4 | 4 |
|
5 | 5 | using Org.BouncyCastle.Asn1;
|
6 | 6 | using Org.BouncyCastle.Asn1.BC;
|
7 |
| -using Org.BouncyCastle.Asn1.Nist; |
8 | 7 | using Org.BouncyCastle.Asn1.Pkcs;
|
9 | 8 | using Org.BouncyCastle.Asn1.X509;
|
10 | 9 | using Org.BouncyCastle.Crypto;
|
@@ -232,21 +231,53 @@ internal static DilithiumPublicKeyParameters GetDilithiumPublicKey(DilithiumPara
|
232 | 231 |
|
233 | 232 | private static AsymmetricKeyParameter LmsConverter(SubjectPublicKeyInfo keyInfo, object defaultParams)
|
234 | 233 | {
|
235 |
| - byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); |
| 234 | + DerBitString publicKey = keyInfo.PublicKey; |
236 | 235 |
|
237 |
| - if (Pack.BE_To_UInt32(keyEnc, 0) == 1U) |
238 |
| - { |
239 |
| - return LmsPublicKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); |
240 |
| - } |
241 |
| - else |
| 236 | + if (publicKey.IsOctetAligned()) |
242 | 237 | {
|
243 |
| - // public key with extra tree height |
244 |
| - if (keyEnc.Length == 64) |
| 238 | + //int expectedLength = ???; |
| 239 | + |
| 240 | + //int bytesLength = publicKey.GetBytesLength(); |
| 241 | + //if (bytesLength == expectedLength) |
| 242 | + // return GetLmsKeyParameters(publicKey.GetOctets()); |
| 243 | + |
| 244 | + // TODO[pqc] Remove support for legacy/prototype formats? |
| 245 | + //if (bytesLength > expectedLength) |
245 | 246 | {
|
246 |
| - keyEnc = Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length); |
| 247 | + try |
| 248 | + { |
| 249 | + Asn1Object obj = Asn1Object.FromMemoryStream(publicKey.GetOctetMemoryStream()); |
| 250 | + if (obj is Asn1OctetString oct) |
| 251 | + { |
| 252 | + //if (oct.GetOctetsLength() == expectedLength) |
| 253 | + { |
| 254 | + return GetLmsKeyParameters(oct.GetOctets()); |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + catch (Exception) |
| 259 | + { |
| 260 | + } |
247 | 261 | }
|
248 |
| - return HssPublicKeyParameters.GetInstance(keyEnc); |
| 262 | + |
| 263 | + return GetLmsKeyParameters(publicKey.GetOctets()); |
249 | 264 | }
|
| 265 | + |
| 266 | + throw new ArgumentException($"invalid LMS public key"); |
| 267 | + } |
| 268 | + |
| 269 | + private static LmsKeyParameters GetLmsKeyParameters(byte[] keyEnc) |
| 270 | + { |
| 271 | + if (Pack.BE_To_UInt32(keyEnc, 0) == 1U) |
| 272 | + return LmsPublicKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); |
| 273 | + |
| 274 | + // public key with extra tree height |
| 275 | + if (keyEnc.Length == 64) |
| 276 | + { |
| 277 | + keyEnc = Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length); |
| 278 | + } |
| 279 | + |
| 280 | + return HssPublicKeyParameters.GetInstance(keyEnc); |
250 | 281 | }
|
251 | 282 |
|
252 | 283 | #pragma warning disable CS0618 // Type or member is obsolete
|
|
0 commit comments