Arduino Cryptography Library
Public Member Functions | Static Public Member Functions | Friends | List of all members
ChaCha Class Reference

ChaCha stream cipher. More...

#include <ChaCha.h>

Inheritance diagram for ChaCha:
Cipher

Public Member Functions

 ChaCha (uint8_t numRounds=20)
 Constructs a new ChaCha stream cipher. More...
 
size_t keySize () const
 Default size of the key for this cipher, in bytes. More...
 
size_t ivSize () const
 Size of the initialization vector for this cipher, in bytes. More...
 
uint8_t numRounds () const
 Returns the number of encryption rounds; usually 8, 12, or 20. More...
 
void setNumRounds (uint8_t numRounds)
 Sets the number of encryption rounds. More...
 
bool setKey (const uint8_t *key, size_t len)
 Sets the key to use for future encryption and decryption operations. More...
 
bool setIV (const uint8_t *iv, size_t len)
 Sets the initialization vector to use for future encryption and decryption operations. More...
 
bool setCounter (const uint8_t *counter, size_t len)
 Sets the starting counter for encryption. More...
 
void encrypt (uint8_t *output, const uint8_t *input, size_t len)
 Encrypts an input buffer and writes the ciphertext to an output buffer. More...
 
void decrypt (uint8_t *output, const uint8_t *input, size_t len)
 Decrypts an input buffer and writes the plaintext to an output buffer. More...
 
void clear ()
 Clears all security-sensitive state from this cipher. More...
 
- Public Member Functions inherited from Cipher
 Cipher ()
 Constructs a new cipher object.
 
virtual ~Cipher ()
 Destroys this cipher object. More...
 

Static Public Member Functions

static void hashCore (uint32_t *output, const uint32_t *input, uint8_t rounds)
 Executes the ChaCha hash core on an input memory block. More...
 

Friends

class ChaChaPoly
 

Detailed Description

ChaCha stream cipher.

ChaCha is a stream cipher that takes a key, an 8-byte nonce/IV, and a counter and hashes them to generate a keystream to XOR with the plaintext. Variations on the ChaCha cipher use 8, 12, or 20 rounds of hashing operations with either 128-bit or 256-bit keys.

Reference: http://cr.yp.to/chacha.html

Definition at line 30 of file ChaCha.h.

Constructor & Destructor Documentation

◆ ChaCha()

ChaCha::ChaCha ( uint8_t  numRounds = 20)
explicit

Constructs a new ChaCha stream cipher.

Parameters
numRoundsNumber of encryption rounds to use; usually 8, 12, or 20.

Definition at line 47 of file ChaCha.cpp.

Member Function Documentation

◆ clear()

void ChaCha::clear ( )
virtual

Clears all security-sensitive state from this cipher.

Security-sensitive information includes key schedules, initialization vectors, and any temporary state that is used by encrypt() or decrypt() which is stored in the cipher itself.

Implements Cipher.

Definition at line 218 of file ChaCha.cpp.

◆ decrypt()

void ChaCha::decrypt ( uint8_t *  output,
const uint8_t *  input,
size_t  len 
)
virtual

Decrypts an input buffer and writes the plaintext to an output buffer.

Parameters
outputThe output buffer to write to, which may be the same buffer as input. The output buffer must have at least as many bytes as the input buffer.
inputThe input buffer to read from.
lenThe number of bytes to decrypt.

The decrypt() function can be called multiple times with different regions of the ciphertext data.

See also
encrypt()

Implements Cipher.

Definition at line 190 of file ChaCha.cpp.

◆ encrypt()

void ChaCha::encrypt ( uint8_t *  output,
const uint8_t *  input,
size_t  len 
)
virtual

Encrypts an input buffer and writes the ciphertext to an output buffer.

Parameters
outputThe output buffer to write to, which may be the same buffer as input. The output buffer must have at least as many bytes as the input buffer.
inputThe input buffer to read from.
lenThe number of bytes to encrypt.

The encrypt() function can be called multiple times with different regions of the plaintext data.

See also
decrypt()

Implements Cipher.

Definition at line 158 of file ChaCha.cpp.

◆ hashCore()

void ChaCha::hashCore ( uint32_t *  output,
const uint32_t *  input,
uint8_t  rounds 
)
static

Executes the ChaCha hash core on an input memory block.

Parameters
outputOutput memory block, must be at least 16 words in length and must not overlap with input.
inputInput memory block, must be at least 16 words in length.
roundsNumber of ChaCha rounds to perform; usually 8, 12, or 20.

This function is provided for the convenience of applications that need access to the ChaCha hash core without the higher-level processing that turns the core into a stream cipher.

Definition at line 253 of file ChaCha.cpp.

◆ ivSize()

size_t ChaCha::ivSize ( ) const
virtual

Size of the initialization vector for this cipher, in bytes.

If the cipher does not need an initialization vector, this function will return zero.

Implements Cipher.

Definition at line 65 of file ChaCha.cpp.

◆ keySize()

size_t ChaCha::keySize ( ) const
virtual

Default size of the key for this cipher, in bytes.

If the cipher supports variable-sized keys, keySize() indicates the default or recommended key size. The cipher may support other key sizes.

See also
setKey(), ivSize()

Implements Cipher.

Definition at line 59 of file ChaCha.cpp.

◆ numRounds()

uint8_t ChaCha::numRounds ( ) const
inline

Returns the number of encryption rounds; usually 8, 12, or 20.

See also
setNumRounds()

Definition at line 39 of file ChaCha.h.

◆ setCounter()

bool ChaCha::setCounter ( const uint8_t *  counter,
size_t  len 
)

Sets the starting counter for encryption.

Parameters
counterA 4-byte or 8-byte value to use for the starting counter instead of the default value of zero.
lenThe length of the counter, which must be 4 or 8.
Returns
Returns false if len is not 4 or 8.

This function must be called after setIV() and before the first call to encrypt(). It is used to specify a different starting value than zero for the counter portion of the hash input.

See also
setIV()

Definition at line 145 of file ChaCha.cpp.

◆ setIV()

bool ChaCha::setIV ( const uint8_t *  iv,
size_t  len 
)
virtual

Sets the initialization vector to use for future encryption and decryption operations.

Parameters
ivThe initialization vector to use.
lenThe length of the initialization vector in bytes.
Returns
Returns false if the length is not supported.

Initialization vectors should be set before the first call to encrypt() or decrypt() after a setKey() call. If the initialization vector is changed after encryption or decryption begins, then the behaviour is undefined.

Note
The IV is not encoded into the output stream by encrypt(). The caller is responsible for communicating the IV to the other party.
See also
ivSize()

Implements Cipher.

Definition at line 111 of file ChaCha.cpp.

◆ setKey()

bool ChaCha::setKey ( const uint8_t *  key,
size_t  len 
)
virtual

Sets the key to use for future encryption and decryption operations.

Parameters
keyThe key to use.
lenThe length of the key in bytes.
Returns
Returns false if the key length is not supported, or the key is somehow "weak" and unusable by this cipher.

Use clear() or the destructor to remove the key and any other sensitive data from the object once encryption or decryption is complete.

Calling setKey() resets the cipher. Any temporary data that was being retained for encrypting partial blocks will be abandoned.

See also
keySize(), clear()

Implements Cipher.

Definition at line 87 of file ChaCha.cpp.

◆ setNumRounds()

void ChaCha::setNumRounds ( uint8_t  numRounds)
inline

Sets the number of encryption rounds.

Parameters
numRoundsThe number of encryption rounds; usually 8, 12, or 20.
See also
numRounds()

Definition at line 40 of file ChaCha.h.


The documentation for this class was generated from the following files: