mrdcvlsc / ChaCha20-Poly1305

A C++ implementation of ChaCha20 & Poly1305 stream cipher described in RFC - 8439.
MIT License
15 stars 1 forks source link
aead aead-authenticated-cryptography-mac arm authenticated-encryption chacha20 chacha20-poly1305 cplusplus cpp cryptography decryption encryption key library poly1305 rfc-8439 scratch-implementation security stream-cipher uint512 x86-64

ChaCha20 & Poly1305

build License: MIT

A from scratch C++ implementation of ChaCha20 & Poly1305 stream cipher described in RFC - 8439.

tests:

gcc-gnu clang mingw64 msvc

All test from RFC-8439 is implemented here and is inside the tests folder.


Clone the repository and download the submodule

git clone https://github.com/mrdcvlsc/ChaCha20-Poly1305.git
cd ChaCha20-Poly1305
git submodule update --init --recursive

Environment Requirements

If your system does not have these requirements, it might produce wrong results or might not even compile.


Main Functions

Note: one byte is equivalent to one unsigned char, If we have an array with a size of 5 bytes, that is an array of type unsigned char with 5 elements.

The sizes of the parameter arrays are mentioned in the documentation comments in the example code below:

/**Encryption.
 * 
 * @param outputCipher resulting cipher text output, 
 * The size of outputCipher in bytes is equivalent to textLen.
 * 
 * @param outputTag resulting 128bit / 16 byte array,
 * GENERATED after ENCRYPTION.
 * 
 * @param inputText plain text to be ENCRYPTED.
 * 
 * @param textLen size of the inputText in bytes.
 * 
 * @param AAD Arbitrary length additional authenticated data.
 * 
 * @param AAD_len size of the AAD in bytes.
 * 
 * @param key a 256-bit key or 32 byte unsigned char array.
 * 
 * @param nonce number only/use once, a 12 byte array with random values
*/
ChaCha20_Poly1305::aead_encrypt(
    unsigned char       *outputCipher,
    unsigned char       *outputTag,
    const unsigned char *inputText,
    size_t               textLen,
    const unsigned char *AAD,
    size_t               AAD_len,
    const unsigned char *key,
    const unsigned char *nonce
);

/**Decryption.
 * 
 * @param outputText recovered plain text output, 
 * The size of outputText in bytes is equivalent to cipherLen.
 * 
 * @param outputTag resulting 128bit / 16 byte array,
 * GENERATED after DECRYPTION.
 * 
 * @param inputCipher cipher text to be DECRYPTED.
 * 
 * @param cipherLen size of the input inputCipher in bytes.
 * 
 * @param AAD Arbitrary length additional authenticated data.
 * 
 * @param AAD_len size of the AAD in bytes.
 * 
 * @param key a 256-bit key or 32 byte unsigned char array.
 * 
 * @param nonce number only/use once, a 12 byte array with random values
*/
ChaCha20_Poly1305::aead_decrypt(
    unsigned char       *outputText,
    unsigned char       *outputTag,
    const unsigned char *inputCipher,
    size_t               cipherLen,
    const unsigned char *AAD,
    size_t               AAD_len,
    const unsigned char *key,
    const unsigned char *nonce
);

To know if the message is authentic:

The outputTag of generated by encryption should match the outputTag generated by decryption.

  1. The sender will send the encrypted cipher text along with the encrypted tag.

  2. Then the receiver will decrypt the cipher text, the decryption function will generate the output recovered plain text and the output decryption tag.

  3. The receiver then compares the sender's encryption tag to the generated output decryption tag; if both tag matched / is-equal then the message was authentic / not altered.


Compilation

There are TWO WAYS to use the library, either using the header only mode, or building the static library.




You can also modify the installation path in linux by setting up a new value for the INSTALL_PREFIX= when executing the make build commands similar to what is shown in the windows build.