auth.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package encoding
  2. import (
  3. "crypto/md5"
  4. "hash/fnv"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/serial"
  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 = serial.Uint32ToBytes(Authenticate(plaintext), dst)
  44. return append(dst, plaintext...)
  45. }
  46. // Open implements AEAD.Open().
  47. func (*FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  48. if serial.BytesToUint32(ciphertext[:4]) != Authenticate(ciphertext[4:]) {
  49. return dst, newError("invalid authentication")
  50. }
  51. return append(dst, ciphertext[4:]...), nil
  52. }
  53. // GenerateChacha20Poly1305Key generates a 32-byte key from a given 16-byte array.
  54. func GenerateChacha20Poly1305Key(b []byte) []byte {
  55. key := make([]byte, 32)
  56. t := md5.Sum(b)
  57. copy(key, t[:])
  58. t = md5.Sum(key[:16])
  59. copy(key[16:], t[:])
  60. return key
  61. }
  62. type ShakeSizeParser struct {
  63. shake sha3.ShakeHash
  64. buffer [2]byte
  65. }
  66. func NewShakeSizeParser(nonce []byte) *ShakeSizeParser {
  67. shake := sha3.NewShake128()
  68. common.Must2(shake.Write(nonce))
  69. return &ShakeSizeParser{
  70. shake: shake,
  71. }
  72. }
  73. func (*ShakeSizeParser) SizeBytes() int32 {
  74. return 2
  75. }
  76. func (s *ShakeSizeParser) next() uint16 {
  77. common.Must2(s.shake.Read(s.buffer[:]))
  78. return serial.BytesToUint16(s.buffer[:])
  79. }
  80. func (s *ShakeSizeParser) Decode(b []byte) (uint16, error) {
  81. mask := s.next()
  82. size := serial.BytesToUint16(b)
  83. return mask ^ size, nil
  84. }
  85. func (s *ShakeSizeParser) Encode(size uint16, b []byte) []byte {
  86. mask := s.next()
  87. return serial.Uint16ToBytes(mask^size, b[:0])
  88. }
  89. func (s *ShakeSizeParser) NextPaddingLen() uint16 {
  90. return s.next() % 64
  91. }
  92. func (s *ShakeSizeParser) MaxPaddingLen() uint16 {
  93. return 64
  94. }