Ejemplo n.º 1
0
        protected virtual TlsMac CreateTlsMac(IDigest digest, byte[] buf, ref int off)
        {
            int    len = digest.GetDigestSize();
            TlsMac mac = new TlsMac(digest, buf, off, len);

            off += len;
            return(mac);
        }
Ejemplo n.º 2
0
        public TlsBlockCipher(TlsClientContext context, IBlockCipher encryptCipher,
                              IBlockCipher decryptCipher, IDigest writeDigest, IDigest readDigest, int cipherKeySize)
        {
            this.context       = context;
            this.encryptCipher = encryptCipher;
            this.decryptCipher = decryptCipher;

            int prfSize = (2 * cipherKeySize) + writeDigest.GetDigestSize()
                          + readDigest.GetDigestSize() + encryptCipher.GetBlockSize()
                          + decryptCipher.GetBlockSize();

            SecurityParameters securityParameters = context.SecurityParameters;

            byte[] keyBlock = TlsUtilities.PRF(securityParameters.masterSecret, "key expansion",
                                               TlsUtilities.Concat(securityParameters.serverRandom, securityParameters.clientRandom),
                                               prfSize);

            int offset = 0;

            // Init MACs
            writeMac = CreateTlsMac(writeDigest, keyBlock, ref offset);
            readMac  = CreateTlsMac(readDigest, keyBlock, ref offset);

            // Build keys
            KeyParameter encryptKey = CreateKeyParameter(keyBlock, ref offset, cipherKeySize);
            KeyParameter decryptKey = CreateKeyParameter(keyBlock, ref offset, cipherKeySize);

            // Add IVs
            ParametersWithIV encryptParams = CreateParametersWithIV(encryptKey,
                                                                    keyBlock, ref offset, encryptCipher.GetBlockSize());
            ParametersWithIV decryptParams = CreateParametersWithIV(decryptKey,
                                                                    keyBlock, ref offset, decryptCipher.GetBlockSize());

            if (offset != prfSize)
            {
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }

            // Init Ciphers
            encryptCipher.Init(true, encryptParams);
            decryptCipher.Init(false, decryptParams);
        }