auth.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package crypto
  2. import (
  3. "crypto/cipher"
  4. "errors"
  5. "io"
  6. "v2ray.com/core/common/alloc"
  7. "v2ray.com/core/common/serial"
  8. )
  9. var (
  10. ErrAuthenticationFailed = errors.New("Authentication failed.")
  11. errInsufficientBuffer = errors.New("Insufficient buffer.")
  12. )
  13. type BytesGenerator func() []byte
  14. type AuthenticationReader struct {
  15. aead cipher.AEAD
  16. buffer *alloc.Buffer
  17. reader io.Reader
  18. ivGen BytesGenerator
  19. extraGen BytesGenerator
  20. chunk []byte
  21. }
  22. func NewAuthenticationReader(aead cipher.AEAD, reader io.Reader, ivGen BytesGenerator, extraGen BytesGenerator) *AuthenticationReader {
  23. return &AuthenticationReader{
  24. aead: aead,
  25. buffer: alloc.NewLocalBuffer(32 * 1024),
  26. reader: reader,
  27. ivGen: ivGen,
  28. extraGen: extraGen,
  29. }
  30. }
  31. func (v *AuthenticationReader) NextChunk() error {
  32. if v.buffer.Len() < 2 {
  33. return errInsufficientBuffer
  34. }
  35. size := int(serial.BytesToUint16(v.buffer.BytesTo(2)))
  36. if size > v.buffer.Len()-2 {
  37. return errInsufficientBuffer
  38. }
  39. if size == v.aead.Overhead() {
  40. return io.EOF
  41. }
  42. cipherChunk := v.buffer.BytesRange(2, size+2)
  43. plainChunk, err := v.aead.Open(cipherChunk, v.ivGen(), cipherChunk, v.extraGen())
  44. if err != nil {
  45. return err
  46. }
  47. v.chunk = plainChunk
  48. v.buffer.SliceFrom(size + 2)
  49. return nil
  50. }
  51. func (v *AuthenticationReader) CopyChunk(b []byte) int {
  52. nBytes := copy(b, v.chunk)
  53. if nBytes == len(v.chunk) {
  54. v.chunk = nil
  55. } else {
  56. v.chunk = v.chunk[nBytes:]
  57. }
  58. return nBytes
  59. }
  60. func (v *AuthenticationReader) Read(b []byte) (int, error) {
  61. if len(v.chunk) > 0 {
  62. nBytes := v.CopyChunk(b)
  63. return nBytes, nil
  64. }
  65. err := v.NextChunk()
  66. if err == errInsufficientBuffer {
  67. _, err = v.buffer.FillFrom(v.reader)
  68. }
  69. if err != nil {
  70. return 0, err
  71. }
  72. totalBytes := 0
  73. for {
  74. totalBytes += v.CopyChunk(b)
  75. if len(b) == 0 {
  76. break
  77. }
  78. if err := v.NextChunk(); err != nil {
  79. break
  80. }
  81. }
  82. return totalBytes, nil
  83. }
  84. type AuthenticationWriter struct {
  85. aead cipher.AEAD
  86. buffer []byte
  87. writer io.Writer
  88. ivGen BytesGenerator
  89. extraGen BytesGenerator
  90. }
  91. func NewAuthenticationWriter(aead cipher.AEAD, writer io.Writer, ivGen BytesGenerator, extraGen BytesGenerator) *AuthenticationWriter {
  92. return &AuthenticationWriter{
  93. aead: aead,
  94. buffer: make([]byte, 32*1024),
  95. writer: writer,
  96. ivGen: ivGen,
  97. extraGen: extraGen,
  98. }
  99. }
  100. func (v *AuthenticationWriter) Write(b []byte) (int, error) {
  101. cipherChunk := v.aead.Seal(v.buffer[2:], v.ivGen(), b, v.extraGen())
  102. serial.Uint16ToBytes(uint16(len(cipherChunk)), b[:0])
  103. _, err := v.writer.Write(v.buffer[:2+len(cipherChunk)])
  104. return len(b), err
  105. }