auth.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package encoding
  2. import (
  3. "crypto/md5"
  4. "encoding/binary"
  5. "hash/fnv"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/crypto"
  8. "golang.org/x/crypto/sha3"
  9. )
  10. // Authenticate authenticates a byte array using Fnv hash.
  11. func Authenticate(b []byte) uint32 {
  12. fnv1hash := fnv.New32a()
  13. common.Must2(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. // NonceSize implements AEAD.NonceSize().
  34. func (*FnvAuthenticator) NonceSize() int {
  35. return 0
  36. }
  37. // Overhead impelements AEAD.Overhead().
  38. func (*FnvAuthenticator) Overhead() int {
  39. return 4
  40. }
  41. // Seal implements AEAD.Seal().
  42. func (*FnvAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
  43. dst = append(dst, 0, 0, 0, 0)
  44. binary.BigEndian.PutUint32(dst, Authenticate(plaintext))
  45. return append(dst, plaintext...)
  46. }
  47. // Open implements AEAD.Open().
  48. func (*FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  49. if binary.BigEndian.Uint32(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. common.Must2(shake.Write(nonce))
  70. return &ShakeSizeParser{
  71. shake: shake,
  72. }
  73. }
  74. func (*ShakeSizeParser) SizeBytes() int32 {
  75. return 2
  76. }
  77. func (s *ShakeSizeParser) next() uint16 {
  78. common.Must2(s.shake.Read(s.buffer[:]))
  79. return binary.BigEndian.Uint16(s.buffer[:])
  80. }
  81. func (s *ShakeSizeParser) Decode(b []byte) (uint16, error) {
  82. mask := s.next()
  83. size := binary.BigEndian.Uint16(b)
  84. return mask ^ size, nil
  85. }
  86. func (s *ShakeSizeParser) Encode(size uint16, b []byte) []byte {
  87. mask := s.next()
  88. binary.BigEndian.PutUint16(b, mask^size)
  89. return b[:2]
  90. }
  91. func (s *ShakeSizeParser) NextPaddingLen() uint16 {
  92. return s.next() % 64
  93. }
  94. func (s *ShakeSizeParser) MaxPaddingLen() uint16 {
  95. return 64
  96. }
  97. type AEADSizeParser struct {
  98. crypto.AEADChunkSizeParser
  99. }
  100. func NewAEADSizeParser(auth *crypto.AEADAuthenticator) *AEADSizeParser {
  101. return &AEADSizeParser{crypto.AEADChunkSizeParser{Auth: auth}}
  102. }