auth.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. return nil
  49. }
  50. func (v *AuthenticationReader) CopyChunk(b []byte) int {
  51. nBytes := copy(b, v.chunk)
  52. if nBytes == len(v.chunk) {
  53. v.chunk = nil
  54. } else {
  55. v.chunk = v.chunk[nBytes:]
  56. }
  57. return nBytes
  58. }
  59. func (v *AuthenticationReader) Read(b []byte) (int, error) {
  60. if len(v.chunk) > 0 {
  61. nBytes := v.CopyChunk(b)
  62. return nBytes, nil
  63. }
  64. err := v.NextChunk()
  65. if err == errInsufficientBuffer {
  66. _, err = v.buffer.FillFrom(v.reader)
  67. }
  68. if err != nil {
  69. return 0, err
  70. }
  71. totalBytes := 0
  72. for {
  73. totalBytes += v.CopyChunk(b)
  74. if len(b) == 0 {
  75. break
  76. }
  77. if err := v.NextChunk(); err != nil {
  78. break
  79. }
  80. }
  81. return totalBytes, nil
  82. }
  83. type AuthenticationWriter struct {
  84. aead cipher.AEAD
  85. buffer []byte
  86. writer io.Writer
  87. ivGen BytesGenerator
  88. extraGen BytesGenerator
  89. }
  90. func NewAuthenticationWriter(aead cipher.AEAD, writer io.Writer, ivGen BytesGenerator, extraGen BytesGenerator) *AuthenticationWriter {
  91. return &AuthenticationWriter{
  92. aead: aead,
  93. buffer: make([]byte, 32*1024),
  94. writer: writer,
  95. ivGen: ivGen,
  96. extraGen: extraGen,
  97. }
  98. }
  99. func (v *AuthenticationWriter) Write(b []byte) (int, error) {
  100. cipherChunk := v.aead.Seal(v.buffer[2:], v.ivGen(), b, v.extraGen())
  101. serial.Uint16ToBytes(uint16(len(cipherChunk)), b[:0])
  102. _, err := v.writer.Write(v.buffer[:2+len(cipherChunk)])
  103. return len(b), err
  104. }