private CngKeyBuilderImpl GetBuilderFor(Asn1Token.KnownOid oid) { switch (oid) { case Asn1Token.KnownOid.EcPublicKey: return(new EllipticCurveCngKeyBuilder()); case Asn1Token.KnownOid.RsaEncryptionPkcs1: return(new RsaCngKeyBuilder()); } throw new UnsupportedCurveException("Unsupported key type with oid"); }
private static int GetMagicNumber(Asn1Token.KnownOid curve) { switch (curve) { case Asn1Token.KnownOid.AnsiX9P256R1: return(0x31534345); case Asn1Token.KnownOid.Secp384R1: return(0x33534345); case Asn1Token.KnownOid.Secp521R1: return(0x35534345); } throw new UnsupportedCurveException("Unsupported elliptic curve domain"); }
private unsafe CngKey BuildEcKey(byte[] x, byte[] y, Asn1Token.KnownOid curve) { int headerSize = Marshal.SizeOf(typeof(BCRYPT_ECCKEY_BLOB)); int blobSize = headerSize + x.Length + y.Length; byte[] blobBytes = new byte[blobSize]; fixed(byte *pBlobBytes = blobBytes) { BCRYPT_ECCKEY_BLOB *pBcryptEccBlob = (BCRYPT_ECCKEY_BLOB *)pBlobBytes; pBcryptEccBlob->KeyBlobMagicNumber = GetMagicNumber(curve); pBcryptEccBlob->KeySizeBytes = x.Length; Buffer.BlockCopy(x, 0, blobBytes, headerSize, x.Length); Buffer.BlockCopy(y, 0, blobBytes, headerSize + x.Length, y.Length); } new KeyContainerPermission(KeyContainerPermissionFlags.Import).Assert(); var key = CngKey.Import(blobBytes, CngKeyBlobFormat.EccPublicBlob); CodeAccessPermission.RevertAssert(); return(key); }
private static bool isSupportedCurve(Asn1Token.KnownOid curve) { return(curve == Asn1Token.KnownOid.AnsiX9P256R1 || curve == Asn1Token.KnownOid.Secp384R1 || curve == Asn1Token.KnownOid.Secp521R1); }