Beispiel #1
0
        public unsafe KeySchedule(IConnectionStateTls13 state, EphemeralBufferPoolWindows pool, ReadableBuffer resumptionSecret)
        {
            _pool      = pool;
            _stateData = pool.Rent();
            _state     = state;
            _hashSize  = CryptoProvider.HashProvider.HashSize(CipherSuite.HashType);

            _stateData.Memory.TryGetPointer(out _secret);
            _clientHandshakeTrafficSecret = ((byte *)_secret) + _hashSize;
            _serverHandshakeTrafficSecret = _clientHandshakeTrafficSecret + _hashSize;
            _masterSecret = _serverHandshakeTrafficSecret + _hashSize;
            _clientApplicationTrafficSecret = _masterSecret + _hashSize;
            _serverApplicationTrafficSecret = _clientApplicationTrafficSecret + _hashSize;

            void *resumptionPointer = null;
            int   secretLength      = 0;

            if (resumptionSecret.Length > 0)
            {
                var stackSecret = stackalloc byte[resumptionSecret.Length];
                resumptionSecret.CopyTo(new Span <byte>(stackSecret, resumptionSecret.Length));
                secretLength      = resumptionSecret.Length;
                resumptionPointer = stackSecret;
            }
            HkdfFunctions.HkdfExtract(CryptoProvider.HashProvider, CipherSuite.HashType, null, 0, resumptionPointer, secretLength, _secret, _hashSize);
        }
        internal unsafe AeadBulkCipherInstance(SafeBCryptAlgorithmHandle algo, EphemeralBufferPoolWindows bufferPool, BulkCipherType cipherType)
        {
            _bufferPool = bufferPool;
            _algo       = algo;
            switch (cipherType)
            {
            case BulkCipherType.AES_128_GCM:
                _chainingMode = BCRYPT_CHAIN_MODE_GCM;
                _keyLength    = 16;
                _iVLength     = 16;
                _overhead     = 16;
                break;

            case BulkCipherType.AES_256_GCM:
                _chainingMode = BCRYPT_CHAIN_MODE_GCM;
                _keyLength    = 32;
                _iVLength     = 16;
                _overhead     = 16;
                break;

            default:
                Internal.ExceptionHelper.ThrowException(new InvalidOperationException());
                return;
            }
            _keyStore = bufferPool.Rent(0);
            void *tmpPointer;

            if (!_keyStore.Memory.TryGetPointer(out tmpPointer))
            {
                throw new InvalidOperationException("Could not get keystore pointer");
            }
            _ivPointer = (IntPtr)tmpPointer;
        }
        internal unsafe AeadBulkCipherInstance(IntPtr cipherType, EphemeralBufferPoolWindows bufferPool, int ivLength, int keySize, int overhead)
        {
            _overhead   = overhead;
            _bufferPool = bufferPool;
            _cipherType = cipherType;
            _iVLength   = EVP_CIPHER_iv_length(cipherType);
            _sequence   = new byte[_iVLength];
            _keyLength  = keySize;
            _keyStore   = bufferPool.Rent();
            void *tmpPointer;

            if (!_keyStore.Memory.TryGetPointer(out tmpPointer))
            {
                throw new InvalidOperationException("Could not get keystore pointer");
            }
            _keyPointer = (IntPtr)tmpPointer;
            _ivPointer  = IntPtr.Add(_keyPointer, _keyLength);
        }