@stablelib/chacha
Version:
ChaCha stream cipher
37 lines (36 loc) • 1.79 kB
TypeScript
/**
* Encrypt src with ChaCha20 stream generated for the given 32-byte key and
* 8-byte (as in original implementation) or 12-byte (as in RFC7539) nonce and
* write the result into dst and return it.
*
* dst and src may be the same, but otherwise must not overlap.
*
* If nonce is 12 bytes, users should not encrypt more than 256 GiB with the
* same key and nonce, otherwise the stream will repeat. The function will
* throw error if counter overflows to prevent this.
*
* If nonce is 8 bytes, the output is practically unlimited (2^70 bytes, which
* is more than a million petabytes). However, it is not recommended to
* generate 8-byte nonces randomly, as the chance of collision is high.
*
* Never use the same key and nonce to encrypt more than one message.
*
* If nonceInplaceCounterLength is not 0, the nonce is assumed to be a 16-byte
* array with stream counter in first nonceInplaceCounterLength bytes and nonce
* in the last remaining bytes. The counter will be incremented inplace for
* each ChaCha block. This is useful if you need to encrypt one stream of data
* in chunks.
*/
export declare function streamXOR(key: Uint8Array, nonce: Uint8Array, src: Uint8Array, dst: Uint8Array, nonceInplaceCounterLength?: number): Uint8Array;
/**
* Generate ChaCha20 stream for the given 32-byte key and 8-byte or 12-byte
* nonce and write it into dst and return it.
*
* Never use the same key and nonce to generate more than one stream.
*
* If nonceInplaceCounterLength is not 0, it behaves the same with respect to
* the nonce as described in the streamXOR documentation.
*
* stream is like streamXOR with all-zero src.
*/
export declare function stream(key: Uint8Array, nonce: Uint8Array, dst: Uint8Array, nonceInplaceCounterLength?: number): Uint8Array;