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

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