cipher.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package ws
  2. import (
  3. "encoding/binary"
  4. )
  5. // Cipher applies XOR cipher to the payload using mask.
  6. // Offset is used to cipher chunked data (e.g. in io.Reader implementations).
  7. //
  8. // To convert masked data into unmasked data, or vice versa, the following
  9. // algorithm is applied. The same algorithm applies regardless of the
  10. // direction of the translation, e.g., the same steps are applied to
  11. // mask the data as to unmask the data.
  12. func Cipher(payload []byte, mask [4]byte, offset int) {
  13. n := len(payload)
  14. if n < 8 {
  15. for i := 0; i < n; i++ {
  16. payload[i] ^= mask[(offset+i)%4]
  17. }
  18. return
  19. }
  20. // Calculate position in mask due to previously processed bytes number.
  21. mpos := offset % 4
  22. // Count number of bytes will processed one by one from the beginning of payload.
  23. ln := remain[mpos]
  24. // Count number of bytes will processed one by one from the end of payload.
  25. // This is done to process payload by 8 bytes in each iteration of main loop.
  26. rn := (n - ln) % 8
  27. for i := 0; i < ln; i++ {
  28. payload[i] ^= mask[(mpos+i)%4]
  29. }
  30. for i := n - rn; i < n; i++ {
  31. payload[i] ^= mask[(mpos+i)%4]
  32. }
  33. // NOTE: we use here binary.LittleEndian regardless of what is real
  34. // endianess on machine is. To do so, we have to use binary.LittleEndian in
  35. // the masking loop below as well.
  36. var (
  37. m = binary.LittleEndian.Uint32(mask[:])
  38. m2 = uint64(m)<<32 | uint64(m)
  39. )
  40. // Skip already processed right part.
  41. // Get number of uint64 parts remaining to process.
  42. n = (n - ln - rn) >> 3
  43. for i := 0; i < n; i++ {
  44. var (
  45. j = ln + (i << 3)
  46. chunk = payload[j : j+8]
  47. )
  48. p := binary.LittleEndian.Uint64(chunk)
  49. p = p ^ m2
  50. binary.LittleEndian.PutUint64(chunk, p)
  51. }
  52. }
  53. // remain maps position in masking key [0,4) to number
  54. // of bytes that need to be processed manually inside Cipher().
  55. var remain = [4]int{0, 3, 2, 1}