Example #1
0
    public TlsNullCipher(TlsContext context, IDigest clientWriteDigest, IDigest serverWriteDigest)
    {
        if (clientWriteDigest == null != (serverWriteDigest == null))
        {
            throw new TlsFatalAlert(80);
        }
        this.context = context;
        TlsMac tlsMac  = null;
        TlsMac tlsMac2 = null;

        if (clientWriteDigest != null)
        {
            int    num  = clientWriteDigest.GetDigestSize() + serverWriteDigest.GetDigestSize();
            byte[] key  = TlsUtilities.CalculateKeyBlock(context, num);
            int    num2 = 0;
            tlsMac  = new TlsMac(context, clientWriteDigest, key, num2, clientWriteDigest.GetDigestSize());
            num2   += clientWriteDigest.GetDigestSize();
            tlsMac2 = new TlsMac(context, serverWriteDigest, key, num2, serverWriteDigest.GetDigestSize());
            num2   += serverWriteDigest.GetDigestSize();
            if (num2 != num)
            {
                throw new TlsFatalAlert(80);
            }
        }
        if (context.IsServer)
        {
            writeMac = tlsMac2;
            readMac  = tlsMac;
        }
        else
        {
            writeMac = tlsMac;
            readMac  = tlsMac2;
        }
    }
    public TlsStreamCipher(TlsContext context, IStreamCipher clientWriteCipher, IStreamCipher serverWriteCipher, IDigest clientWriteDigest, IDigest serverWriteDigest, int cipherKeySize, bool usesNonce)
    {
        bool isServer = context.IsServer;

        this.context   = context;
        this.usesNonce = usesNonce;
        encryptCipher  = clientWriteCipher;
        decryptCipher  = serverWriteCipher;
        int num = 2 * cipherKeySize + clientWriteDigest.GetDigestSize() + serverWriteDigest.GetDigestSize();

        byte[] key    = TlsUtilities.CalculateKeyBlock(context, num);
        int    num2   = 0;
        TlsMac tlsMac = new TlsMac(context, clientWriteDigest, key, num2, clientWriteDigest.GetDigestSize());

        num2 += clientWriteDigest.GetDigestSize();
        TlsMac tlsMac2 = new TlsMac(context, serverWriteDigest, key, num2, serverWriteDigest.GetDigestSize());

        num2 += serverWriteDigest.GetDigestSize();
        KeyParameter keyParameter = new KeyParameter(key, num2, cipherKeySize);

        num2 += cipherKeySize;
        KeyParameter keyParameter2 = new KeyParameter(key, num2, cipherKeySize);

        num2 += cipherKeySize;
        if (num2 != num)
        {
            throw new TlsFatalAlert(80);
        }
        ICipherParameters parameters;
        ICipherParameters parameters2;

        if (isServer)
        {
            writeMac      = tlsMac2;
            readMac       = tlsMac;
            encryptCipher = serverWriteCipher;
            decryptCipher = clientWriteCipher;
            parameters    = keyParameter2;
            parameters2   = keyParameter;
        }
        else
        {
            writeMac      = tlsMac;
            readMac       = tlsMac2;
            encryptCipher = clientWriteCipher;
            decryptCipher = serverWriteCipher;
            parameters    = keyParameter;
            parameters2   = keyParameter2;
        }
        if (usesNonce)
        {
            byte[] iv = new byte[8];
            parameters  = new ParametersWithIV(parameters, iv);
            parameters2 = new ParametersWithIV(parameters2, iv);
        }
        encryptCipher.Init(forEncryption: true, parameters);
        decryptCipher.Init(forEncryption: false, parameters2);
    }
Example #3
0
 public TlsNullCipher(TlsContext context)
 {
     this.context = context;
     writeMac     = null;
     readMac      = null;
 }
    public TlsBlockCipher(TlsContext context, IBlockCipher clientWriteCipher, IBlockCipher serverWriteCipher, IDigest clientWriteDigest, IDigest serverWriteDigest, int cipherKeySize)
    {
        this.context = context;
        randomData   = new byte[256];
        context.NonceRandomGenerator.NextBytes(randomData);
        useExplicitIV  = TlsUtilities.IsTlsV11(context);
        encryptThenMac = context.SecurityParameters.encryptThenMac;
        int num = 2 * cipherKeySize + clientWriteDigest.GetDigestSize() + serverWriteDigest.GetDigestSize();

        if (!useExplicitIV)
        {
            num += clientWriteCipher.GetBlockSize() + serverWriteCipher.GetBlockSize();
        }
        byte[] array  = TlsUtilities.CalculateKeyBlock(context, num);
        int    num2   = 0;
        TlsMac tlsMac = new TlsMac(context, clientWriteDigest, array, num2, clientWriteDigest.GetDigestSize());

        num2 += clientWriteDigest.GetDigestSize();
        TlsMac tlsMac2 = new TlsMac(context, serverWriteDigest, array, num2, serverWriteDigest.GetDigestSize());

        num2 += serverWriteDigest.GetDigestSize();
        KeyParameter parameters = new KeyParameter(array, num2, cipherKeySize);

        num2 += cipherKeySize;
        KeyParameter parameters2 = new KeyParameter(array, num2, cipherKeySize);

        num2 += cipherKeySize;
        byte[] iv;
        byte[] iv2;
        if (useExplicitIV)
        {
            iv  = new byte[clientWriteCipher.GetBlockSize()];
            iv2 = new byte[serverWriteCipher.GetBlockSize()];
        }
        else
        {
            iv    = Arrays.CopyOfRange(array, num2, num2 + clientWriteCipher.GetBlockSize());
            num2 += clientWriteCipher.GetBlockSize();
            iv2   = Arrays.CopyOfRange(array, num2, num2 + serverWriteCipher.GetBlockSize());
            num2 += serverWriteCipher.GetBlockSize();
        }
        if (num2 != num)
        {
            throw new TlsFatalAlert(80);
        }
        ICipherParameters parameters3;
        ICipherParameters parameters4;

        if (context.IsServer)
        {
            mWriteMac     = tlsMac2;
            mReadMac      = tlsMac;
            encryptCipher = serverWriteCipher;
            decryptCipher = clientWriteCipher;
            parameters3   = new ParametersWithIV(parameters2, iv2);
            parameters4   = new ParametersWithIV(parameters, iv);
        }
        else
        {
            mWriteMac     = tlsMac;
            mReadMac      = tlsMac2;
            encryptCipher = clientWriteCipher;
            decryptCipher = serverWriteCipher;
            parameters3   = new ParametersWithIV(parameters, iv);
            parameters4   = new ParametersWithIV(parameters2, iv2);
        }
        encryptCipher.Init(forEncryption: true, parameters3);
        decryptCipher.Init(forEncryption: false, parameters4);
    }