git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/chacha/hchacha20.go (about)

     1  package chacha
     2  
     3  import "errors"
     4  
     5  const (
     6  	HChaCha20KeySize   = 32
     7  	HChaCha20NonceSize = 16
     8  )
     9  
    10  var (
    11  	ErrBadHChaCha20KeySize   = errors.New("chacha: bad HChaCha20 key size")
    12  	ErrBadHChaCha20NonceSize = errors.New("chacha: bad HChaCha20 nonce size")
    13  )
    14  
    15  // HChaCha20 generates 32 pseudo-random bytes from a 128 bit nonce and a 256 bit secret key.
    16  // It can be used as a key-derivation-function (KDF).
    17  func HChaCha20(key, nonce []byte) ([]byte, error) {
    18  	// This function is split into a wrapper so that the slice allocation will
    19  	// be inlined, and depending on how the caller uses the return value, won't
    20  	// escape to the heap.
    21  	out := make([]byte, 32)
    22  	return hChaCha20(out, key, nonce)
    23  }