crypto.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package srtp
  2. import (
  3. "crypto/cipher"
  4. )
  5. // xorBytes computes the exclusive-or of src1 and src2 and stores it in dst.
  6. // It returns the number of bytes written.
  7. func xorBytes(dst, src1, src2 []byte) int {
  8. n := len(src1)
  9. if len(src2) < n {
  10. n = len(src2)
  11. }
  12. if len(dst) < n {
  13. n = len(dst)
  14. }
  15. for i := 0; i < n; i++ {
  16. dst[i] = src1[i] ^ src2[i]
  17. }
  18. return n
  19. }
  20. // incrementCTR increments a big-endian integer of arbitrary size.
  21. func incrementCTR(ctr []byte) {
  22. for i := len(ctr) - 1; i >= 0; i-- {
  23. ctr[i]++
  24. if ctr[i] != 0 {
  25. break
  26. }
  27. }
  28. }
  29. // xorBytesCTR performs CTR encryption and decryption.
  30. // It is equivalent to cipher.NewCTR followed by XORKeyStream.
  31. func xorBytesCTR(block cipher.Block, iv []byte, dst, src []byte) error {
  32. if len(iv) != block.BlockSize() {
  33. return errBadIVLength
  34. }
  35. ctr := make([]byte, len(iv))
  36. copy(ctr, iv)
  37. bs := block.BlockSize()
  38. stream := make([]byte, bs)
  39. i := 0
  40. for i < len(src) {
  41. block.Encrypt(stream, ctr)
  42. incrementCTR(ctr)
  43. n := xorBytes(dst[i:], src[i:], stream)
  44. if n == 0 {
  45. break
  46. }
  47. i += n
  48. }
  49. return nil
  50. }