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

     1  package chacha20
     2  
     3  import (
     4  	"git.sr.ht/~pingoo/stdx/crypto/chacha"
     5  )
     6  
     7  func New(key, nonce []byte) (StreamCipher, error) {
     8  	// TODO: here we use a 96-bit nonce, because IETF's ChaCha20 use a 96-bit nonce,
     9  	// but in the future we will want to use the original ChaCha20 with a 64-bit nonce
    10  	// so for now, messages are limited to (64 B * 2^32-1) = ~256 GB
    11  	var ietfNonce [12]byte
    12  
    13  	if len(key) != KeySize {
    14  		return nil, ErrBadKeyLength
    15  	}
    16  	if len(nonce) != NonceSize {
    17  		return nil, ErrBadNonceLength
    18  	}
    19  
    20  	copy(ietfNonce[4:12], nonce[0:8])
    21  	return chacha.NewCipher(ietfNonce, key, 20)
    22  }