auth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package encoding
  2. import (
  3. "crypto/md5"
  4. "hash/fnv"
  5. "golang.org/x/crypto/sha3"
  6. "v2ray.com/core/common/crypto"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/common/serial"
  9. )
  10. // Authenticate authenticates a byte array using Fnv hash.
  11. func Authenticate(b []byte) uint32 {
  12. fnv1hash := fnv.New32a()
  13. fnv1hash.Write(b)
  14. return fnv1hash.Sum32()
  15. }
  16. type NoOpAuthenticator struct{}
  17. func (NoOpAuthenticator) NonceSize() int {
  18. return 0
  19. }
  20. func (NoOpAuthenticator) Overhead() int {
  21. return 0
  22. }
  23. // Seal implements AEAD.Seal().
  24. func (NoOpAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
  25. return append(dst[:0], plaintext...)
  26. }
  27. // Open implements AEAD.Open().
  28. func (NoOpAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  29. return append(dst[:0], ciphertext...), nil
  30. }
  31. // FnvAuthenticator is an AEAD based on Fnv hash.
  32. type FnvAuthenticator struct {
  33. }
  34. // NonceSize implements AEAD.NonceSize().
  35. func (v *FnvAuthenticator) NonceSize() int {
  36. return 0
  37. }
  38. // Overhead impelements AEAD.Overhead().
  39. func (v *FnvAuthenticator) Overhead() int {
  40. return 4
  41. }
  42. // Seal implements AEAD.Seal().
  43. func (v *FnvAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
  44. dst = serial.Uint32ToBytes(Authenticate(plaintext), dst)
  45. return append(dst, plaintext...)
  46. }
  47. // Open implements AEAD.Open().
  48. func (v *FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  49. if serial.BytesToUint32(ciphertext[:4]) != Authenticate(ciphertext[4:]) {
  50. return dst, newError("invalid authentication")
  51. }
  52. return append(dst, ciphertext[4:]...), nil
  53. }
  54. // GenerateChacha20Poly1305Key generates a 32-byte key from a given 16-byte array.
  55. func GenerateChacha20Poly1305Key(b []byte) []byte {
  56. key := make([]byte, 32)
  57. t := md5.Sum(b)
  58. copy(key, t[:])
  59. t = md5.Sum(key[:16])
  60. copy(key[16:], t[:])
  61. return key
  62. }
  63. type ShakeSizeParser struct {
  64. shake sha3.ShakeHash
  65. buffer [2]byte
  66. }
  67. func NewShakeSizeParser(nonce []byte) *ShakeSizeParser {
  68. shake := sha3.NewShake128()
  69. shake.Write(nonce)
  70. return &ShakeSizeParser{
  71. shake: shake,
  72. }
  73. }
  74. func (s *ShakeSizeParser) SizeBytes() int {
  75. return 2
  76. }
  77. func (s *ShakeSizeParser) next() uint16 {
  78. s.shake.Read(s.buffer[:])
  79. return serial.BytesToUint16(s.buffer[:])
  80. }
  81. func (s *ShakeSizeParser) Decode(b []byte) (uint16, error) {
  82. mask := s.next()
  83. size := serial.BytesToUint16(b)
  84. return mask ^ size, nil
  85. }
  86. func (s *ShakeSizeParser) Encode(size uint16, b []byte) []byte {
  87. mask := s.next()
  88. return serial.Uint16ToBytes(mask^size, b[:0])
  89. }
  90. func GetStreamMode(request *protocol.RequestHeader) crypto.StreamMode {
  91. if request.Command == protocol.RequestCommandTCP {
  92. return crypto.ModeStream
  93. }
  94. return crypto.ModePacket
  95. }