public override byte[] GenerateAgreement(AsymmetricKeyParameter peerPublicKey)
    {
        mBasicAgreement.Init(mPrivateKey);
        BigInteger n = mBasicAgreement.CalculateAgreement(peerPublicKey);

        if (mTruncateAgreement)
        {
            return(BigIntegers.AsUnsignedByteArray(n));
        }
        return(BigIntegers.AsUnsignedByteArray(mBasicAgreement.GetFieldSize(), n));
    }
        public virtual byte[] GenerateAgreement(AsymmetricKeyParameter serverPublicKey)
        {
            basicAgreement.Init(clientPrivateKey);
            BigInteger agreementValue = basicAgreement.CalculateAgreement(serverPublicKey);

            if (truncateAgreement)
            {
                return(BigIntegers.AsUnsignedByteArray(agreementValue));
            }

            return(BigIntegers.AsUnsignedByteArray(basicAgreement.GetFieldSize(), agreementValue));
        }
Пример #3
0
    public virtual byte[] ProcessBlock(byte[] input, int inOff, int inLen)
    {
        agree.Init(privParam);
        BigInteger n = agree.CalculateAgreement(pubParam);

        byte[] array = BigIntegers.AsUnsignedByteArray(agree.GetFieldSize(), n);
        try
        {
            return(forEncryption ? EncryptBlock(input, inOff, inLen, array) : DecryptBlock(input, inOff, inLen, array));
        }
        finally
        {
            Array.Clear(array, 0, array.Length);
        }
    }
Пример #4
0
        public byte[] ProcessBlock(
            byte[]  input,
            int inOff,
            int inLen)
        {
            agree.Init(privParam);

            BigInteger z = agree.CalculateAgreement(pubParam);

            byte[] zBytes = BigIntegers.AsUnsignedByteArray(agree.GetFieldSize(), z);

            return(forEncryption
                ?       EncryptBlock(input, inOff, inLen, zBytes)
                :       DecryptBlock(input, inOff, inLen, zBytes));
        }
Пример #5
0
        public byte[] ProcessBlock(
            byte[] input,
            int inOff,
            int inLen,
            byte[] macData)
        {
            // Compute the common value and convert to byte array.
            _agreement.Init(_privParam);
            BigInteger zAsInteger = _agreement.CalculateAgreement(_pubParam);

            byte[] z = BigIntegers.AsUnsignedByteArray(_agreement.GetFieldSize(), zAsInteger);

            _kdfKey = _optimizedKdf.Derive(z);

            return(_forEncryption
                ? EncryptBlock(input, inOff, inLen, macData)
                : DecryptBlock(input, inOff, inLen, macData));
        }
Пример #6
0
        public byte[] ProcessBlock(
            byte[] input,
            int inOff,
            int inLen,
            byte[] macData)
        {
            // Compute the common value and convert to byte array.
            _agreement.Init(_privParam);
            BigInteger zAsInteger = _agreement.CalculateAgreement(_pubParam);

            byte[] z = BigIntegers.AsUnsignedByteArray(_agreement.GetFieldSize(), zAsInteger);

            // Create input to KDF.
            byte[] vz;
//        if (V.Length != 0)
//        {
//            VZ = new byte[V.Length + Z.Length];
//            Array.Copy(V, 0, VZ, 0, V.Length);
//            Array.Copy(Z, 0, VZ, V.Length, Z.Length);
//        }
//        else
            {
                vz = z;
            }

            // Initialise the KDF.
            IDerivationParameters kdfParam;

            if (_kdf is Mgf1BytesGenerator)
            {
                kdfParam = new MgfParameters(vz);
            }
            else
            {
                kdfParam = new KdfParameters(vz, _iesParameters.GetDerivationV());
            }
            _kdf.Init(kdfParam);

            return(_forEncryption
                ? EncryptBlock(input, inOff, inLen, macData)
                : DecryptBlock(input, inOff, inLen, macData));
        }
Пример #7
0
        public virtual byte[] ProcessBlock(
            byte[] @in,
            int inOff,
            int inLen)
        {
            if (ForEncryption)
            {
                if (KeyPairGenerator != null)
                {
                    EphemeralKeyPair ephKeyPair = KeyPairGenerator.Generate();

                    PrivParam = ephKeyPair.GetKeyPair().Private;
                    V         = ephKeyPair.GetEncodedPublicKey();
                }
            }
            else
            {
                if (KeyParser != null)
                {
                    MemoryStream bIn = new MemoryStream(@in, inOff, inLen)
                    {
                        Position = 0
                    };
                    try
                    {
                        PubParam = KeyParser.ReadKey(bIn);
                    }
                    catch (IOException e)
                    {
                        throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.Message, e);
                    }
                    catch (ArgumentException e)
                    {
                        throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.Message, e);
                    }

                    int encLength = (inLen - (int)(bIn.Length - bIn.Position));
                    V = Arrays.CopyOfRange(@in, inOff, inOff + encLength);
                }
            }

            // Compute the common value and convert to byte array.
            Agree.Init(PrivParam);
            BigInteger z = Agree.CalculateAgreement(PubParam);

            byte[] bigZ = BigIntegers.AsUnsignedByteArray(Agree.GetFieldSize(), z);

            // Create input to KDF.
            if (V.Length != 0)
            {
                byte[] vz = Arrays.Concatenate(V, bigZ);
                Arrays.Fill(bigZ, 0);
                bigZ = vz;
            }

            try
            {
                // Initialise the KDF.
                KdfParameters kdfParam = new KdfParameters(bigZ, _param.GetDerivationV());
                Kdf.Init(kdfParam);

                return(ForEncryption
                    ? EncryptBlock(@in, inOff, inLen)
                    : DecryptBlock(@in, inOff, inLen));
            }
            finally
            {
                Arrays.Fill(bigZ, 0);
            }
        }