diff --git a/Inc/bit_manipulation.h b/Inc/bit_manipulation.h index 72880cc..47b747a 100644 --- a/Inc/bit_manipulation.h +++ b/Inc/bit_manipulation.h @@ -91,4 +91,15 @@ bool BitManipulation_clearBit(uint32_t data, uint8_t n, uint32_t* out); */ bool BitManipulation_toggleBit(uint32_t data, uint8_t n, uint32_t* out); + +/** + * @brief This function rotates the bits of a 32-bit variable to the left. + * + * @param[in] data The input data to rotate. + * @param[in] n_bits The number of bits in the data to be rotated. + * + * @return The rotated data. + */ +uint32_t BitManipulation_rotl32(uint32_t data, uint32_t n_bits); + #endif /* UTILITY_BIT_MANIPULATION_H_ */ diff --git a/Inc/crypto/chacha20.h b/Inc/crypto/chacha20.h new file mode 100644 index 0000000..d4c2455 --- /dev/null +++ b/Inc/crypto/chacha20.h @@ -0,0 +1,50 @@ +/**************************************************************************** + * + * Copyright (c) 2024 IMProject Development Team. All rights reserved. + * Authors: Igor Misic + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name IMProject nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef UTILITY_CHACHA20_H_ +#define UTILITY_CHACHA20_H_ + +#include "typedefs.h" + +#define CHACHA20_KEY_SIZE 32U +#define CHACHA20_NONCE_SIZE 12U + +void chacha20(const byte_t* plaintext, + uint32_t plaintext_len, + const byte_t key[CHACHA20_KEY_SIZE], + const byte_t nonce[CHACHA20_NONCE_SIZE], + uint32_t inc, + byte_t* encrypted_message); + +#endif /* UTILITY_CHACHA20_H_ */ diff --git a/Makefile b/Makefile index 26836d3..c0f70f5 100644 --- a/Makefile +++ b/Makefile @@ -106,6 +106,7 @@ IMUTILITY_FILES=\ Src/crc/crc32_variants/crc32_q.c \ Src/crc/crc32_variants/crc32_xfer.c \ Src/crypto/caesar_cipher.c \ + Src/crypto/chacha20.c \ Src/sort/bubble_sort.c \ Src/sort/heap_sort.c \ Src/sort/insertion_sort.c \ @@ -126,6 +127,7 @@ SRC_FILES+=$(IMUTILITY_FILES) \ Tests/test_bit_manipulation.c \ Tests/test_bubble_sort.c \ Tests/test_caesar_cipher.c \ + Tests/test_chacha20.c \ Tests/test_crc8.c \ Tests/test_crc16.c \ Tests/test_crc32.c \ @@ -138,14 +140,16 @@ SRC_FILES+=$(IMUTILITY_FILES) \ Tests/test_scheduler.c \ Tests/test_selection_sort.c \ Tests/test_utils.c -INC_DIRS_CODE=\ + +INC_DIRS_CODE= \ -IInc \ - -IInc/crypto \ -IInc/crc \ -IInc/crc/crc8_variants \ -IInc/crc/crc16_variants \ -IInc/crc/crc32_variants \ + -IInc/crypto \ -IInc/sort + INC_DIRS=$(INC_DIRS_CODE) -I$(UNITY_ROOT)/src -I$(UNITY_ROOT)/extras/fixture/src SYMBOLS = -DUNITY_FIXTURE_NO_EXTRAS SYMBOLS += -DUNITY_INCLUDE_DOUBLE diff --git a/README.md b/README.md index 997f181..38c5836 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ Join us on the Discord channel https://discord.gg/R6nZxZqDH3 - BitManipulation_setBit - BitManipulation_clearBit - BitManipulation_toggleBit +- BitManipulation_rotl32 ### Crc8 - Crc8Base_tableCalculator @@ -119,6 +120,7 @@ Join us on the Discord channel https://discord.gg/R6nZxZqDH3 ### Cryptography - CaesarCipher_encrypt - CaesarCipher_decrypt +- Chacha20 ### JSON - Json_startString diff --git a/Src/bit_manipulation.c b/Src/bit_manipulation.c index a5dd1bd..34e3ff1 100644 --- a/Src/bit_manipulation.c +++ b/Src/bit_manipulation.c @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2023 IMProject Development Team. All rights reserved. + * Copyright (c) 2023 - 2024 IMProject Development Team. All rights reserved. * Authors: Juraj Ciberlin * * Redistribution and use in source and binary forms, with or without @@ -103,3 +103,10 @@ BitManipulation_toggleBit(uint32_t data, uint8_t n, uint32_t* out) { } return status; } + +uint32_t +BitManipulation_rotl32(uint32_t data, uint32_t n_bits) { + + /* -E> hide MC3R1.R12.2 1 To optimize efficiency, we do not verify whether n_bits is between 0 and 31. */ + return ((data << n_bits) ^ (data >> (32U - n_bits))); +} diff --git a/Src/crypto/chacha20.c b/Src/crypto/chacha20.c new file mode 100644 index 0000000..005a2cf --- /dev/null +++ b/Src/crypto/chacha20.c @@ -0,0 +1,175 @@ +/**************************************************************************** + * + * Copyright (c) 2024 IMProject Development Team. All rights reserved. + * Authors: Igor Misic + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name IMProject nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +// Chacha20 definition document : https://datatracker.ietf.org/doc/html/rfc8439 + +#include "chacha20.h" + +#include "bit_manipulation.h" +#include "utils.h" + +#define KEY_STREAM_SIZE 64U +#define BLOCK_SIZE 64U + +// Hex representation of Magic number: "expand 32-byte k" +#define CHACHA_MAGIC_NUMBER_PART_1 (0x61707865U) +#define CHACHA_MAGIC_NUMBER_PART_2 (0x3320646eU) +#define CHACHA_MAGIC_NUMBER_PART_3 (0x79622d32U) +#define CHACHA_MAGIC_NUMBER_PART_4 (0x6b206574U) + +#define ARRAY_64_ZERO_VALUES { \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, \ + 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U \ + } + +static inline void quarterround(uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d); +static void chacha20_block(const byte_t key[CHACHA20_KEY_SIZE], const uint32_t counter, + const byte_t nonce[CHACHA20_NONCE_SIZE], + byte_t out[KEY_STREAM_SIZE]); + +static inline void +quarterround(uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d) { + *a += *b; + *d = BitManipulation_rotl32(*d ^ *a, 16); + *c += *d; + *b = BitManipulation_rotl32(*b ^ *c, 12); + *a += *b; + *d = BitManipulation_rotl32(*d ^ *a, 8); + *c += *d; + *b = BitManipulation_rotl32(*b ^ *c, 7); +} + +static void +chacha20_block(const byte_t key[CHACHA20_KEY_SIZE], const uint32_t counter, const byte_t nonce[CHACHA20_NONCE_SIZE], + byte_t out[KEY_STREAM_SIZE]) { + + uint32_t x0 = CHACHA_MAGIC_NUMBER_PART_1; + uint32_t x1 = CHACHA_MAGIC_NUMBER_PART_2; + uint32_t x2 = CHACHA_MAGIC_NUMBER_PART_3; + uint32_t x3 = CHACHA_MAGIC_NUMBER_PART_4; + uint32_t x4 = Utils_Deserialize32LE(&key[0]); + uint32_t x5 = Utils_Deserialize32LE(&key[4]); + uint32_t x6 = Utils_Deserialize32LE(&key[8]); + uint32_t x7 = Utils_Deserialize32LE(&key[12]); + uint32_t x8 = Utils_Deserialize32LE(&key[16]); + uint32_t x9 = Utils_Deserialize32LE(&key[20]); + uint32_t x10 = Utils_Deserialize32LE(&key[24]); + uint32_t x11 = Utils_Deserialize32LE(&key[28]); + uint32_t x12 = counter; + uint32_t x13 = Utils_Deserialize32LE(&nonce[0]); + uint32_t x14 = Utils_Deserialize32LE(&nonce[4]); + uint32_t x15 = Utils_Deserialize32LE(&nonce[8]); + + for (uint8_t i = 0; i < 10U; ++i) { + quarterround(&x0, &x4, &x8, &x12); + quarterround(&x1, &x5, &x9, &x13); + quarterround(&x2, &x6, &x10, &x14); + quarterround(&x3, &x7, &x11, &x15); + quarterround(&x0, &x5, &x10, &x15); + quarterround(&x1, &x6, &x11, &x12); + quarterround(&x2, &x7, &x8, &x13); + quarterround(&x3, &x4, &x9, &x14); + } + + x0 += CHACHA_MAGIC_NUMBER_PART_1; + x1 += CHACHA_MAGIC_NUMBER_PART_2; + x2 += CHACHA_MAGIC_NUMBER_PART_3; + x3 += CHACHA_MAGIC_NUMBER_PART_4; + x4 += Utils_Deserialize32LE(&key[0]); + x5 += Utils_Deserialize32LE(&key[4]); + x6 += Utils_Deserialize32LE(&key[8]); + x7 += Utils_Deserialize32LE(&key[12]); + x8 += Utils_Deserialize32LE(&key[16]); + x9 += Utils_Deserialize32LE(&key[20]); + x10 += Utils_Deserialize32LE(&key[24]); + x11 += Utils_Deserialize32LE(&key[28]); + x12 += counter; + x13 += Utils_Deserialize32LE(&nonce[0]); + x14 += Utils_Deserialize32LE(&nonce[4]); + x15 += Utils_Deserialize32LE(&nonce[8]); + + /* -E> compliant MC3R1.R18.6 16 automatic storage pointed with "out" is not copied to values from this function */ + Utils_Serialize32LE(&out[0], x0); + Utils_Serialize32LE(&out[4], x1); + Utils_Serialize32LE(&out[8], x2); + Utils_Serialize32LE(&out[12], x3); + Utils_Serialize32LE(&out[16], x4); + Utils_Serialize32LE(&out[20], x5); + Utils_Serialize32LE(&out[24], x6); + Utils_Serialize32LE(&out[28], x7); + Utils_Serialize32LE(&out[32], x8); + Utils_Serialize32LE(&out[36], x9); + Utils_Serialize32LE(&out[40], x10); + Utils_Serialize32LE(&out[44], x11); + Utils_Serialize32LE(&out[48], x12); + Utils_Serialize32LE(&out[52], x13); + Utils_Serialize32LE(&out[56], x14); + Utils_Serialize32LE(&out[60], x15); +} + +void +chacha20(const byte_t* plaintext, + uint32_t plaintext_len, + const byte_t key[CHACHA20_KEY_SIZE], + const byte_t nonce[CHACHA20_NONCE_SIZE], + uint32_t inc, + byte_t* encrypted_message) { + + uint32_t blocks = plaintext_len / BLOCK_SIZE; + uint32_t remaining_bytes = plaintext_len % BLOCK_SIZE; + + for (uint32_t j = 0; j < blocks; ++j) { + byte_t key_stream[KEY_STREAM_SIZE] = ARRAY_64_ZERO_VALUES; + chacha20_block(key, j + inc, nonce, key_stream); + + for (uint8_t i = 0; i < BLOCK_SIZE; ++i) { + encrypted_message[(j * BLOCK_SIZE) + i] = plaintext[(j * BLOCK_SIZE) + i] ^ key_stream[i]; + } + } + + if (0U != remaining_bytes) { + byte_t key_stream[KEY_STREAM_SIZE] = ARRAY_64_ZERO_VALUES; + chacha20_block(key, blocks + inc, nonce, key_stream); + + for (uint32_t i = 0; i < remaining_bytes; ++i) { + encrypted_message[(blocks * BLOCK_SIZE) + i] = plaintext[(blocks * BLOCK_SIZE) + i] ^ key_stream[i]; + } + } +} diff --git a/Tests/test_bit_manipulation.c b/Tests/test_bit_manipulation.c index 1d6d4d4..cb4e75b 100644 --- a/Tests/test_bit_manipulation.c +++ b/Tests/test_bit_manipulation.c @@ -17,6 +17,7 @@ TEST_GROUP_RUNNER(BitManipulation) { RUN_TEST_CASE(BitManipulation, BitManipulation_setBit); RUN_TEST_CASE(BitManipulation, BitManipulation_clearBit); RUN_TEST_CASE(BitManipulation, BitManipulation_toggleBit); + RUN_TEST_CASE(BitManipulation, BitManipulation_rotl32); } TEST(BitManipulation, BitManipulation_reflect) { @@ -83,3 +84,23 @@ TEST(BitManipulation, BitManipulation_toggleBit) { TEST_ASSERT_TRUE(BitManipulation_toggleBit(data, 4U, &out_data)); TEST_ASSERT_EQUAL_UINT32(0b10100011U, out_data); } + +TEST(BitManipulation, BitManipulation_rotl32) { + const uint32_t data = 0b10110011U; + + uint8_t n_bits = 2U; + TEST_ASSERT_BITS(0xFFFFFFFF, 0b00000000000000000000001011001100U, BitManipulation_rotl32(data, n_bits)); + + n_bits = 3U; + TEST_ASSERT_BITS(0xFFFFFFFF, 0b00000000000000000000010110011000U, BitManipulation_rotl32(data, n_bits)); + + n_bits = 4U; + TEST_ASSERT_BITS(0xFFFFFFFF, 0b00000000000000000000101100110000U, BitManipulation_rotl32(data, n_bits)); + + n_bits = 16U; + TEST_ASSERT_BITS(0xFFFFFFFF, 0b00000000101100110000000000000000U, BitManipulation_rotl32(data, n_bits)); + + n_bits = 32U; + TEST_ASSERT_BITS(0xFFFFFFFF, 0b00000000000000000000000010110011U, BitManipulation_rotl32(data, n_bits)); +} + diff --git a/Tests/test_chacha20.c b/Tests/test_chacha20.c new file mode 100644 index 0000000..025c07e --- /dev/null +++ b/Tests/test_chacha20.c @@ -0,0 +1,168 @@ +#include "chacha20.h" +#include "unity.h" +#include "unity_fixture.h" +#include + +TEST_GROUP(Chacha20); + +TEST_SETUP(Chacha20) { +} + +TEST_TEAR_DOWN(Chacha20) { +} + +TEST_GROUP_RUNNER(Chacha20) { + RUN_TEST_CASE(Chacha20, Chacha20_01); + RUN_TEST_CASE(Chacha20, Chacha20_02); + RUN_TEST_CASE(Chacha20, Chacha20_03); + RUN_TEST_CASE(Chacha20, Chacha20_04); +} + +TEST(Chacha20, Chacha20_01) { + + byte_t key[CHACHA20_KEY_SIZE] = { + 0x00U, 0x01U, 0x02U, 0x03U, 0x04U, 0x05U, 0x06U, 0x07U, 0x08U, 0x09U, 0x0aU, 0x0bU, 0x0cU, 0x0dU, 0x0eU, 0x0fU, + 0x10U, 0x11U, 0x12U, 0x13U, 0x14U, 0x15U, 0x16U, 0x17U, 0x18U, 0x19U, 0x1aU, 0x1bU, 0x1cU, 0x1dU, 0x1eU, 0x1fU + }; + byte_t nonce[CHACHA20_NONCE_SIZE] = { + 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x4aU, 0x00U, 0x00U, 0x00U, 0x00U + }; + byte_t plaintext[] = + "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."; + + uint32_t initial_block_counter = 1U; + + byte_t result[sizeof(plaintext)] = {0x00U}; + + byte_t expected_encrypted_msg[] = { + 0x6eU, 0x2eU, 0x35U, 0x9aU, 0x25U, 0x68U, 0xf9U, 0x80U, 0x41U, 0xbaU, 0x07U, 0x28U, 0xddU, 0x0dU, 0x69U, 0x81U, + 0xe9U, 0x7eU, 0x7aU, 0xecU, 0x1dU, 0x43U, 0x60U, 0xc2U, 0x0aU, 0x27U, 0xafU, 0xccU, 0xfdU, 0x9fU, 0xaeU, 0x0bU, + 0xf9U, 0x1bU, 0x65U, 0xc5U, 0x52U, 0x47U, 0x33U, 0xabU, 0x8fU, 0x59U, 0x3dU, 0xabU, 0xcdU, 0x62U, 0xb3U, 0x57U, + 0x16U, 0x39U, 0xd6U, 0x24U, 0xe6U, 0x51U, 0x52U, 0xabU, 0x8fU, 0x53U, 0x0cU, 0x35U, 0x9fU, 0x08U, 0x61U, 0xd8U, + 0x07U, 0xcaU, 0x0dU, 0xbfU, 0x50U, 0x0dU, 0x6aU, 0x61U, 0x56U, 0xa3U, 0x8eU, 0x08U, 0x8aU, 0x22U, 0xb6U, 0x5eU, + 0x52U, 0xbcU, 0x51U, 0x4dU, 0x16U, 0xccU, 0xf8U, 0x06U, 0x81U, 0x8cU, 0xe9U, 0x1aU, 0xb7U, 0x79U, 0x37U, 0x36U, + 0x5aU, 0xf9U, 0x0bU, 0xbfU, 0x74U, 0xa3U, 0x5bU, 0xe6U, 0xb4U, 0x0bU, 0x8eU, 0xedU, 0xf2U, 0x78U, 0x5eU, 0x42U, + 0x87U, 0x4dU + }; + + // Encryption + chacha20(plaintext, strlen((char*)plaintext), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(expected_encrypted_msg, result, sizeof(expected_encrypted_msg)); + + // Decryption + chacha20(expected_encrypted_msg, sizeof(expected_encrypted_msg), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(plaintext, result, strlen((char*)plaintext)); +} + +TEST(Chacha20, Chacha20_02) { + + byte_t key[CHACHA20_KEY_SIZE] = {0x00U}; + byte_t nonce[CHACHA20_NONCE_SIZE] = {0x00U}; + byte_t plaintext[64] = {0x00U}; + uint32_t initial_block_counter = 0U; + + byte_t result[sizeof(plaintext)] = {0x00U}; + + byte_t expected_encrypted_msg[] = { + 0x76U, 0xb8U, 0xe0U, 0xadU, 0xa0U, 0xf1U, 0x3dU, 0x90U, 0x40U, 0x5dU, 0x6aU, 0xe5U, 0x53U, 0x86U, 0xbdU, 0x28U, + 0xbdU, 0xd2U, 0x19U, 0xb8U, 0xa0U, 0x8dU, 0xedU, 0x1aU, 0xa8U, 0x36U, 0xefU, 0xccU, 0x8bU, 0x77U, 0x0dU, 0xc7U, + 0xdaU, 0x41U, 0x59U, 0x7cU, 0x51U, 0x57U, 0x48U, 0x8dU, 0x77U, 0x24U, 0xe0U, 0x3fU, 0xb8U, 0xd8U, 0x4aU, 0x37U, + 0x6aU, 0x43U, 0xb8U, 0xf4U, 0x15U, 0x18U, 0xa1U, 0x1cU, 0xc3U, 0x87U, 0xb6U, 0x69U, 0xb2U, 0xeeU, 0x65U, 0x86U + }; + + // Encryption + chacha20(plaintext, sizeof(plaintext), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(expected_encrypted_msg, result, sizeof(expected_encrypted_msg)); + + // Decryption + chacha20(expected_encrypted_msg, sizeof(expected_encrypted_msg), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(plaintext, result, sizeof(plaintext)); +} + +TEST(Chacha20, Chacha20_03) { + + byte_t key[CHACHA20_KEY_SIZE] = { + 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, + 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x01U + }; + byte_t nonce[CHACHA20_NONCE_SIZE] = { + 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U + }; + byte_t plaintext[] = { + "Any submission to the IETF intended by the Contributor for publication as all or part of an IETF Internet-Draft or RFC and any statement made within the context of an IETF activity is considered an \"IETF Contribution\". Such statements include oral statements in IETF sessions, as well as written and electronic communications made at any time or place, which are addressed to" + }; + uint32_t initial_block_counter = 1U; + + byte_t result[sizeof(plaintext)] = {0x00U}; + + byte_t expected_encrypted_msg[] = { + 0xa3U, 0xfbU, 0xf0U, 0x7dU, 0xf3U, 0xfaU, 0x2fU, 0xdeU, 0x4fU, 0x37U, 0x6cU, 0xa2U, 0x3eU, 0x82U, 0x73U, 0x70U, + 0x41U, 0x60U, 0x5dU, 0x9fU, 0x4fU, 0x4fU, 0x57U, 0xbdU, 0x8cU, 0xffU, 0x2cU, 0x1dU, 0x4bU, 0x79U, 0x55U, 0xecU, + 0x2aU, 0x97U, 0x94U, 0x8bU, 0xd3U, 0x72U, 0x29U, 0x15U, 0xc8U, 0xf3U, 0xd3U, 0x37U, 0xf7U, 0xd3U, 0x70U, 0x05U, + 0x0eU, 0x9eU, 0x96U, 0xd6U, 0x47U, 0xb7U, 0xc3U, 0x9fU, 0x56U, 0xe0U, 0x31U, 0xcaU, 0x5eU, 0xb6U, 0x25U, 0x0dU, + 0x40U, 0x42U, 0xe0U, 0x27U, 0x85U, 0xecU, 0xecU, 0xfaU, 0x4bU, 0x4bU, 0xb5U, 0xe8U, 0xeaU, 0xd0U, 0x44U, 0x0eU, + 0x20U, 0xb6U, 0xe8U, 0xdbU, 0x09U, 0xd8U, 0x81U, 0xa7U, 0xc6U, 0x13U, 0x2fU, 0x42U, 0x0eU, 0x52U, 0x79U, 0x50U, + 0x42U, 0xbdU, 0xfaU, 0x77U, 0x73U, 0xd8U, 0xa9U, 0x05U, 0x14U, 0x47U, 0xb3U, 0x29U, 0x1cU, 0xe1U, 0x41U, 0x1cU, + 0x68U, 0x04U, 0x65U, 0x55U, 0x2aU, 0xa6U, 0xc4U, 0x05U, 0xb7U, 0x76U, 0x4dU, 0x5eU, 0x87U, 0xbeU, 0xa8U, 0x5aU, + 0xd0U, 0x0fU, 0x84U, 0x49U, 0xedU, 0x8fU, 0x72U, 0xd0U, 0xd6U, 0x62U, 0xabU, 0x05U, 0x26U, 0x91U, 0xcaU, 0x66U, + 0x42U, 0x4bU, 0xc8U, 0x6dU, 0x2dU, 0xf8U, 0x0eU, 0xa4U, 0x1fU, 0x43U, 0xabU, 0xf9U, 0x37U, 0xd3U, 0x25U, 0x9dU, + 0xc4U, 0xb2U, 0xd0U, 0xdfU, 0xb4U, 0x8aU, 0x6cU, 0x91U, 0x39U, 0xddU, 0xd7U, 0xf7U, 0x69U, 0x66U, 0xe9U, 0x28U, + 0xe6U, 0x35U, 0x55U, 0x3bU, 0xa7U, 0x6cU, 0x5cU, 0x87U, 0x9dU, 0x7bU, 0x35U, 0xd4U, 0x9eU, 0xb2U, 0xe6U, 0x2bU, + 0x08U, 0x71U, 0xcdU, 0xacU, 0x63U, 0x89U, 0x39U, 0xe2U, 0x5eU, 0x8aU, 0x1eU, 0x0eU, 0xf9U, 0xd5U, 0x28U, 0x0fU, + 0xa8U, 0xcaU, 0x32U, 0x8bU, 0x35U, 0x1cU, 0x3cU, 0x76U, 0x59U, 0x89U, 0xcbU, 0xcfU, 0x3dU, 0xaaU, 0x8bU, 0x6cU, + 0xccU, 0x3aU, 0xafU, 0x9fU, 0x39U, 0x79U, 0xc9U, 0x2bU, 0x37U, 0x20U, 0xfcU, 0x88U, 0xdcU, 0x95U, 0xedU, 0x84U, + 0xa1U, 0xbeU, 0x05U, 0x9cU, 0x64U, 0x99U, 0xb9U, 0xfdU, 0xa2U, 0x36U, 0xe7U, 0xe8U, 0x18U, 0xb0U, 0x4bU, 0x0bU, + 0xc3U, 0x9cU, 0x1eU, 0x87U, 0x6bU, 0x19U, 0x3bU, 0xfeU, 0x55U, 0x69U, 0x75U, 0x3fU, 0x88U, 0x12U, 0x8cU, 0xc0U, + 0x8aU, 0xaaU, 0x9bU, 0x63U, 0xd1U, 0xa1U, 0x6fU, 0x80U, 0xefU, 0x25U, 0x54U, 0xd7U, 0x18U, 0x9cU, 0x41U, 0x1fU, + 0x58U, 0x69U, 0xcaU, 0x52U, 0xc5U, 0xb8U, 0x3fU, 0xa3U, 0x6fU, 0xf2U, 0x16U, 0xb9U, 0xc1U, 0xd3U, 0x00U, 0x62U, + 0xbeU, 0xbcU, 0xfdU, 0x2dU, 0xc5U, 0xbcU, 0xe0U, 0x91U, 0x19U, 0x34U, 0xfdU, 0xa7U, 0x9aU, 0x86U, 0xf6U, 0xe6U, + 0x98U, 0xceU, 0xd7U, 0x59U, 0xc3U, 0xffU, 0x9bU, 0x64U, 0x77U, 0x33U, 0x8fU, 0x3dU, 0xa4U, 0xf9U, 0xcdU, 0x85U, + 0x14U, 0xeaU, 0x99U, 0x82U, 0xccU, 0xafU, 0xb3U, 0x41U, 0xb2U, 0x38U, 0x4dU, 0xd9U, 0x02U, 0xf3U, 0xd1U, 0xabU, + 0x7aU, 0xc6U, 0x1dU, 0xd2U, 0x9cU, 0x6fU, 0x21U, 0xbaU, 0x5bU, 0x86U, 0x2fU, 0x37U, 0x30U, 0xe3U, 0x7cU, 0xfdU, + 0xc4U, 0xfdU, 0x80U, 0x6cU, 0x22U, 0xf2U, 0x21U + }; + + // Encryption + chacha20(plaintext, strlen((char*)plaintext), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(expected_encrypted_msg, result, sizeof(expected_encrypted_msg)); + + // Decryption + chacha20(expected_encrypted_msg, sizeof(expected_encrypted_msg), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(plaintext, result, strlen((char*)plaintext)); +} + +TEST(Chacha20, Chacha20_04) { + + byte_t key[CHACHA20_KEY_SIZE] = { + 0x1cU, 0x92U, 0x40U, 0xa5U, 0xebU, 0x55U, 0xd3U, 0x8aU, 0xf3U, 0x33U, 0x88U, 0x86U, 0x04U, 0xf6U, 0xb5U, 0xf0U, + 0x47U, 0x39U, 0x17U, 0xc1U, 0x40U, 0x2bU, 0x80U, 0x09U, 0x9dU, 0xcaU, 0x5cU, 0xbcU, 0x20U, 0x70U, 0x75U, 0xc0U + }; + byte_t nonce[CHACHA20_NONCE_SIZE] = { + 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U + }; + byte_t plaintext[] = { + "'Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe." + }; + uint32_t initial_block_counter = 42U; + + byte_t result[sizeof(plaintext)] = {0x00U}; + + byte_t expected_encrypted_msg[] = { + 0x62U, 0xe6U, 0x34U, 0x7fU, 0x95U, 0xedU, 0x87U, 0xa4U, 0x5fU, 0xfaU, 0xe7U, 0x42U, 0x6fU, 0x27U, 0xa1U, 0xdfU, + 0x5fU, 0xb6U, 0x91U, 0x10U, 0x04U, 0x4cU, 0x0dU, 0x73U, 0x11U, 0x8eU, 0xffU, 0xa9U, 0x5bU, 0x01U, 0xe5U, 0xcfU, + 0x16U, 0x6dU, 0x3dU, 0xf2U, 0xd7U, 0x21U, 0xcaU, 0xf9U, 0xb2U, 0x1eU, 0x5fU, 0xb1U, 0x4cU, 0x61U, 0x68U, 0x71U, + 0xfdU, 0x84U, 0xc5U, 0x4fU, 0x9dU, 0x65U, 0xb2U, 0x83U, 0x19U, 0x6cU, 0x7fU, 0xe4U, 0xf6U, 0x05U, 0x53U, 0xebU, + 0xf3U, 0x9cU, 0x64U, 0x02U, 0xc4U, 0x22U, 0x34U, 0xe3U, 0x2aU, 0x35U, 0x6bU, 0x3eU, 0x76U, 0x43U, 0x12U, 0xa6U, + 0x1aU, 0x55U, 0x32U, 0x05U, 0x57U, 0x16U, 0xeaU, 0xd6U, 0x96U, 0x25U, 0x68U, 0xf8U, 0x7dU, 0x3fU, 0x3fU, 0x77U, + 0x04U, 0xc6U, 0xa8U, 0xd1U, 0xbcU, 0xd1U, 0xbfU, 0x4dU, 0x50U, 0xd6U, 0x15U, 0x4bU, 0x6dU, 0xa7U, 0x31U, 0xb1U, + 0x87U, 0xb5U, 0x8dU, 0xfdU, 0x72U, 0x8aU, 0xfaU, 0x36U, 0x75U, 0x7aU, 0x79U, 0x7aU, 0xc1U, 0x88U, 0xd1U + }; + + // Encryption + chacha20(plaintext, strlen((char*)plaintext), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(expected_encrypted_msg, result, sizeof(expected_encrypted_msg)); + + // Decryption + chacha20(expected_encrypted_msg, sizeof(expected_encrypted_msg), key, nonce, initial_block_counter, result); + TEST_ASSERT_EQUAL_MEMORY(plaintext, result, strlen((char*)plaintext)); +} diff --git a/Tests/test_main.c b/Tests/test_main.c index 761ccfc..dcebf44 100644 --- a/Tests/test_main.c +++ b/Tests/test_main.c @@ -7,6 +7,7 @@ RunAllTests(void) { RUN_TEST_GROUP(BitManipulation); RUN_TEST_GROUP(BubbleSort); RUN_TEST_GROUP(CaesarCipher); + RUN_TEST_GROUP(Chacha20); RUN_TEST_GROUP(Crc8); RUN_TEST_GROUP(Crc16); RUN_TEST_GROUP(Crc32);