auth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package encoding
  2. import (
  3. "crypto/md5"
  4. "encoding/binary"
  5. "hash/fnv"
  6. "v2ray.com/core/common"
  7. "golang.org/x/crypto/sha3"
  8. )
  9. // Authenticate authenticates a byte array using Fnv hash.
  10. func Authenticate(b []byte) uint32 {
  11. fnv1hash := fnv.New32a()
  12. common.Must2(fnv1hash.Write(b))
  13. return fnv1hash.Sum32()
  14. }
  15. type NoOpAuthenticator struct{}
  16. func (NoOpAuthenticator) NonceSize() int {
  17. return 0
  18. }
  19. func (NoOpAuthenticator) Overhead() int {
  20. return 0
  21. }
  22. // Seal implements AEAD.Seal().
  23. func (NoOpAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
  24. return append(dst[:0], plaintext...)
  25. }
  26. // Open implements AEAD.Open().
  27. func (NoOpAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  28. return append(dst[:0], ciphertext...), nil
  29. }
  30. // FnvAuthenticator is an AEAD based on Fnv hash.
  31. type FnvAuthenticator struct {
  32. }
  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. }