auth.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. size := int(serial.BytesToUint16(v.buffer.BytesTo(2)))
  33. if size > v.buffer.Len()-2 {
  34. return errInsufficientBuffer
  35. }
  36. cipherChunk := v.buffer.BytesRange(2, size+2)
  37. plainChunk, err := v.aead.Open(cipherChunk, v.ivGen(), cipherChunk, v.extraGen())
  38. if err != nil {
  39. return err
  40. }
  41. v.chunk = plainChunk
  42. return nil
  43. }
  44. func (v *AuthenticationReader) CopyChunk(b []byte) int {
  45. nBytes := copy(b, v.chunk)
  46. if nBytes == len(v.chunk) {
  47. v.chunk = nil
  48. } else {
  49. v.chunk = v.chunk[nBytes:]
  50. }
  51. return nBytes
  52. }
  53. func (v *AuthenticationReader) Read(b []byte) (int, error) {
  54. if len(v.chunk) > 0 {
  55. nBytes := v.CopyChunk(b)
  56. return nBytes, nil
  57. }
  58. err := v.NextChunk()
  59. if err == errInsufficientBuffer {
  60. v.buffer.FillFrom(v.reader)
  61. } else if err != nil {
  62. return 0, io.ErrUnexpectedEOF
  63. }
  64. totalBytes := 0
  65. for {
  66. totalBytes += v.CopyChunk(b)
  67. if len(b) == 0 {
  68. break
  69. }
  70. if err := v.NextChunk(); err != nil {
  71. break
  72. }
  73. }
  74. return totalBytes, nil
  75. }
  76. type AuthenticationWriter struct {
  77. aead cipher.AEAD
  78. buffer []byte
  79. writer io.Writer
  80. ivGen BytesGenerator
  81. extraGen BytesGenerator
  82. }
  83. func NewAuthenticationWriter(aead cipher.AEAD, writer io.Writer, ivGen BytesGenerator, extraGen BytesGenerator) *AuthenticationWriter {
  84. return &AuthenticationWriter{
  85. aead: aead,
  86. buffer: make([]byte, 32*1024),
  87. writer: writer,
  88. ivGen: ivGen,
  89. extraGen: extraGen,
  90. }
  91. }
  92. func (v *AuthenticationWriter) Write(b []byte) (int, error) {
  93. cipherChunk := v.aead.Seal(v.buffer[2:], v.ivGen(), b, v.extraGen())
  94. serial.Uint16ToBytes(uint16(len(cipherChunk)), b[:0])
  95. _, err := v.writer.Write(v.buffer[:2+len(cipherChunk)])
  96. return len(b), err
  97. }