|  | @@ -4,32 +4,39 @@ import (
 | 
											
												
													
														|  |  	"hash/fnv"
 |  |  	"hash/fnv"
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	"crypto/md5"
 |  |  	"crypto/md5"
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |  	"v2ray.com/core/common/crypto"
 |  |  	"v2ray.com/core/common/crypto"
 | 
											
												
													
														|  |  	"v2ray.com/core/common/serial"
 |  |  	"v2ray.com/core/common/serial"
 | 
											
												
													
														|  |  )
 |  |  )
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +// Authenticate authenticates a byte array using Fnv hash.
 | 
											
												
													
														|  |  func Authenticate(b []byte) uint32 {
 |  |  func Authenticate(b []byte) uint32 {
 | 
											
												
													
														|  |  	fnv1hash := fnv.New32a()
 |  |  	fnv1hash := fnv.New32a()
 | 
											
												
													
														|  |  	fnv1hash.Write(b)
 |  |  	fnv1hash.Write(b)
 | 
											
												
													
														|  |  	return fnv1hash.Sum32()
 |  |  	return fnv1hash.Sum32()
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +// FnvAuthenticator is an AEAD based on Fnv hash.
 | 
											
												
													
														|  |  type FnvAuthenticator struct {
 |  |  type FnvAuthenticator struct {
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +// NonceSize implements AEAD.NonceSize().
 | 
											
												
													
														|  |  func (v *FnvAuthenticator) NonceSize() int {
 |  |  func (v *FnvAuthenticator) NonceSize() int {
 | 
											
												
													
														|  |  	return 0
 |  |  	return 0
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +// Overhead impelements AEAD.Overhead().
 | 
											
												
													
														|  |  func (v *FnvAuthenticator) Overhead() int {
 |  |  func (v *FnvAuthenticator) Overhead() int {
 | 
											
												
													
														|  |  	return 4
 |  |  	return 4
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +// Seal implements AEAD.Seal().
 | 
											
												
													
														|  |  func (v *FnvAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
 |  |  func (v *FnvAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
 | 
											
												
													
														|  |  	dst = serial.Uint32ToBytes(Authenticate(plaintext), dst)
 |  |  	dst = serial.Uint32ToBytes(Authenticate(plaintext), dst)
 | 
											
												
													
														|  |  	return append(dst, plaintext...)
 |  |  	return append(dst, plaintext...)
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +// Open implements AEAD.Open().
 | 
											
												
													
														|  |  func (v *FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
 |  |  func (v *FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
 | 
											
												
													
														|  |  	if serial.BytesToUint32(ciphertext[:4]) != Authenticate(ciphertext[4:]) {
 |  |  	if serial.BytesToUint32(ciphertext[:4]) != Authenticate(ciphertext[4:]) {
 | 
											
												
													
														|  |  		return dst, crypto.ErrAuthenticationFailed
 |  |  		return dst, crypto.ErrAuthenticationFailed
 | 
											
										
											
												
													
														|  | @@ -37,6 +44,7 @@ func (v *FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) (
 | 
											
												
													
														|  |  	return append(dst, ciphertext[4:]...), nil
 |  |  	return append(dst, ciphertext[4:]...), nil
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +// GenerateChacha20Poly1305Key generates a 32-byte key from a given 16-byte array.
 | 
											
												
													
														|  |  func GenerateChacha20Poly1305Key(b []byte) []byte {
 |  |  func GenerateChacha20Poly1305Key(b []byte) []byte {
 | 
											
												
													
														|  |  	key := make([]byte, 32)
 |  |  	key := make([]byte, 32)
 | 
											
												
													
														|  |  	t := md5.Sum(b)
 |  |  	t := md5.Sum(b)
 |