// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package srtp

import (
	
	

	
)

// incrementCTR increments a big-endian integer of arbitrary size.
func incrementCTR( []byte) {
	for  := len() - 1;  >= 0; -- {
		[]++
		if [] != 0 {
			break
		}
	}
}

var xorBufferPool = sync.Pool{ // nolint:gochecknoglobals
	New: func() interface{} {
		return make([]byte, 1500)
	},
}

// xorBytesCTR performs CTR encryption and decryption.
// It is equivalent to cipher.NewCTR followed by XORKeyStream.
func xorBytesCTR( cipher.Block,  []byte, ,  []byte) error {
	if len() != .BlockSize() {
		return errBadIVLength
	}

	 := xorBufferPool.Get()
	defer xorBufferPool.Put()
	,  := .([]byte)
	if ! {
		return errFailedTypeAssertion
	}

	 := [:len()]
	copy(, )
	 := .BlockSize()
	 := [len() : len()+]

	 := 0
	for  < len() {
		.Encrypt(, )
		incrementCTR()
		 := xor.XorBytes([:], [:], )
		if  == 0 {
			break
		}
		 += 
	}

	return nil
}