Пример #1
0
        private Pair <Chain, uint> getReceiverChain(ECPublicKey senderEphemeral)
        {
            IList <Chain> receiverChains = sessionStructure.ReceiverChainsList;
            uint          index          = 0;

            foreach (Chain receiverChain in receiverChains)
            {
                try
                {
                    ECPublicKey chainSenderRatchetKey = Curve.decodePoint(receiverChain.SenderRatchetKey.ToByteArray(), 0);

                    if (chainSenderRatchetKey.Equals(senderEphemeral))
                    {
                        return(new Pair <Chain, uint>(receiverChain, index));
                    }
                }
                catch (InvalidKeyException e)
                {
                    Debug.WriteLine(e.ToString(), "SessionRecordV2");
                }

                index++;
            }

            return(null);
        }
Пример #2
0
        public override bool Equals(Object other)
        {
            if (other == null)
            {
                return(false);
            }
            if (!(other is IdentityKey))
            {
                return(false);
            }

            return(publicKey.Equals(((IdentityKey)other).getPublicKey()));
        }
Пример #3
0
        static ECPrivateKey DecodePrivateKeyEC(AsnElt ak, ECCurve curve)
        {
            ak.CheckNumSubMin(2);
            ak.GetSub(0).CheckTag(AsnElt.INTEGER);
            ak.GetSub(1).CheckTag(AsnElt.OCTET_STRING);
            long kt = ak.GetSub(0).GetInteger();

            if (kt != 1)
            {
                throw new AsnException(
                          "Unsupported EC key type: " + kt);
            }
            byte[] x   = ak.GetSub(1).CopyValue();
            byte[] pub = null;
            int    n   = ak.Sub.Length;
            int    p   = 2;

            if (p < n)
            {
                AsnElt acc = ak.GetSub(p);
                if (acc.TagClass == AsnElt.CONTEXT &&
                    acc.TagValue == 0)
                {
                    acc.CheckNumSub(1);
                    acc = acc.GetSub(0);
                    ECCurve curve2 = DecodeCurve(acc);

                    /*
                     * Here, we support only named curves.
                     */
                    /* obsolete
                     */
                    if (curve == null)
                    {
                        curve = curve2;
                    }
                    else if (!curve.Equals(curve2))
                    {
                        throw new AsnException(string.Format(
                                                   "Inconsistent curve"
                                                   + " specification ({0} / {1})",
                                                   curve.Name, curve2.Name));
                    }

                    p++;
                }
            }
            if (p < n)
            {
                AsnElt acc = ak.GetSub(p);
                if (acc.TagClass == AsnElt.CONTEXT &&
                    acc.TagValue == 1)
                {
                    acc.CheckNumSub(1);
                    acc = acc.GetSub(0);
                    acc.CheckTag(AsnElt.BIT_STRING);
                    pub = acc.GetBitString();
                }
            }

            if (curve == null)
            {
                throw new AsnException("No curve specified for EC key");
            }
            ECPrivateKey esk = new ECPrivateKey(curve, x);

            if (pub != null)
            {
                ECPublicKey epk = new ECPublicKey(curve, pub);
                if (!epk.Equals(esk.PublicKey))
                {
                    throw new CryptoException(
                              "EC key pair public/private mismatch");
                }
            }
            return(esk);
        }