github.com/imannamdari/v2ray-core/v5@v5.0.5/proxy/vmess/aead/kdf.go (about)

     1  package aead
     2  
     3  import (
     4  	"crypto/hmac"
     5  	"crypto/sha256"
     6  	"hash"
     7  )
     8  
     9  func KDF(key []byte, path ...string) []byte {
    10  	hmacCreator := &hMacCreator{value: []byte(KDFSaltConstVMessAEADKDF)}
    11  	for _, v := range path {
    12  		hmacCreator = &hMacCreator{value: []byte(v), parent: hmacCreator}
    13  	}
    14  	hmacf := hmacCreator.Create()
    15  	hmacf.Write(key)
    16  	return hmacf.Sum(nil)
    17  }
    18  
    19  type hMacCreator struct {
    20  	parent *hMacCreator
    21  	value  []byte
    22  }
    23  
    24  func (h *hMacCreator) Create() hash.Hash {
    25  	if h.parent == nil {
    26  		return hmac.New(sha256.New, h.value)
    27  	}
    28  	return hmac.New(h.parent.Create, h.value)
    29  }
    30  
    31  func KDF16(key []byte, path ...string) []byte {
    32  	r := KDF(key, path...)
    33  	return r[:16]
    34  }