Skip to content

Commit

Permalink
Merge pull request #12 from mrdcvlsc/renames
Browse files Browse the repository at this point in the history
renamed constants and namespaces
  • Loading branch information
mrdcvlsc committed Apr 4, 2022
2 parents 275d4d3 + 1aa4965 commit 597a644
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 94 deletions.
62 changes: 31 additions & 31 deletions ChaCha20-Poly1305.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _CHACHA20_CPP_mrdcvlsc_
#define _CHACHA20_CPP_mrdcvlsc_
#ifndef CHACHA20_CPP_mrdcvlsc
#define CHACHA20_CPP_mrdcvlsc
#include <iostream>

#ifdef _MAKE_LIB
Expand All @@ -11,7 +11,7 @@
#include "ChaCha20-Poly1305.hpp"
#endif

namespace __internal_chacha20
namespace chacha20
{
inline unsigned int bit_left_roll(unsigned int num, size_t n) {

Expand Down Expand Up @@ -63,7 +63,7 @@ namespace __internal_chacha20
void apply_20rounds(unsigned int *output, const unsigned int *input) {

// copy initial state to state
for(size_t i=0; i<__CHAx220_STATE_DWORDS__; ++i)
for(size_t i=0; i<CHACHA20_STATE_DWORDS; ++i)
output[i] = input[i];

// chacha 20 rounds
Expand All @@ -83,7 +83,7 @@ namespace __internal_chacha20
}

// add initialized state to the output state
for(size_t i=0; i<__CHAx220_STATE_DWORDS__; ++i)
for(size_t i=0; i<CHACHA20_STATE_DWORDS; ++i)
output[i] += input[i];
}

Expand Down Expand Up @@ -113,14 +113,14 @@ namespace __internal_chacha20

unsigned int *cipher_blocked = (unsigned int*) outputCipher;

size_t blocks = textLen/__CHAx220_BLK_FUNC_OUTPUT_BYTES__;
size_t lastblock_bytes = (textLen%__CHAx220_BLK_FUNC_OUTPUT_BYTES__);
size_t blocks = textLen/CHACHA20_BLK_FUNC_OUTPUT_BYTES;
size_t lastblock_bytes = (textLen%CHACHA20_BLK_FUNC_OUTPUT_BYTES);

unsigned int *chacha_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int *key_stream = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int *chacha_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int *key_stream = new unsigned int[CHACHA20_STATE_DWORDS];

// initialize ChaCha20 state
__internal_chacha20::init_state(
chacha20::init_state(
chacha_state,
(unsigned int*)key,
counter,
Expand All @@ -130,18 +130,18 @@ namespace __internal_chacha20
for(size_t i=0; i<blocks; ++i) {

// perform ChaCha20 Block Function and get the key_stream output
__internal_chacha20::apply_20rounds(
chacha20::apply_20rounds(
key_stream,
chacha_state
);

// increment the chacha state's counter index
++chacha_state[__CHACHA_STATE_COUNTER_INDEX__];
++chacha_state[CHACHA_STATE_COUNTER_INDEX];

if(blocks) {
for( // loop condition
size_t j=(__CHAx220_BLK_FUNC_OUTPUT_DWORDS__*i), k=0;
j<((__CHAx220_BLK_FUNC_OUTPUT_DWORDS__*i)+__CHAx220_BLK_FUNC_OUTPUT_DWORDS__);
size_t j=(CHACHA20_BLK_FUNC_OUTPUT_DWORDS*i), k=0;
j<((CHACHA20_BLK_FUNC_OUTPUT_DWORDS*i)+CHACHA20_BLK_FUNC_OUTPUT_DWORDS);
++j) {

cipher_blocked[j] = plaintext_blocked[j] ^ key_stream[k++];
Expand All @@ -152,25 +152,25 @@ namespace __internal_chacha20
// XOR remaining key_stream bytes and plaintext bytes
if(lastblock_bytes) {

unsigned char *padded_last_bytes = new unsigned char[__CHAx220_BLK_FUNC_OUTPUT_BYTES__];
unsigned char *padded_last_bytes = new unsigned char[CHACHA20_BLK_FUNC_OUTPUT_BYTES];
unsigned int *padded_last_block = (unsigned int*) padded_last_bytes;
memcpy(padded_last_bytes,inputText+(__CHAx220_BLK_FUNC_OUTPUT_BYTES__*blocks),lastblock_bytes);
memcpy(padded_last_bytes,inputText+(CHACHA20_BLK_FUNC_OUTPUT_BYTES*blocks),lastblock_bytes);

// perform ChaCha20 Block Function and get the key_stream output
__internal_chacha20::apply_20rounds(
chacha20::apply_20rounds(
key_stream,
chacha_state
);

for( // loop condition
size_t j=(__CHAx220_BLK_FUNC_OUTPUT_DWORDS__*blocks), k=0;
j<((__CHAx220_BLK_FUNC_OUTPUT_DWORDS__*blocks)+__CHAx220_BLK_FUNC_OUTPUT_DWORDS__);
size_t j=(CHACHA20_BLK_FUNC_OUTPUT_DWORDS*blocks), k=0;
j<((CHACHA20_BLK_FUNC_OUTPUT_DWORDS*blocks)+CHACHA20_BLK_FUNC_OUTPUT_DWORDS);
++j, ++k) {

padded_last_block[k] ^= key_stream[k];
}

memcpy(outputCipher+(__CHAx220_BLK_FUNC_OUTPUT_BYTES__*blocks),padded_last_bytes,lastblock_bytes);
memcpy(outputCipher+(CHACHA20_BLK_FUNC_OUTPUT_BYTES*blocks),padded_last_bytes,lastblock_bytes);

delete [] padded_last_bytes;
}
Expand All @@ -180,7 +180,7 @@ namespace __internal_chacha20
}
}

namespace __internal_poly1305 {
namespace poly1305 {

void clamp(unsigned char r[HALF_KEY_BYTES]) {
r[3] &= 15;
Expand Down Expand Up @@ -238,11 +238,11 @@ namespace __internal_poly1305 {

void key_gen(unsigned char *output, const unsigned char *key, const unsigned int *nonce, unsigned int counter) {

unsigned int *initial_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int *transformed_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int *transformed_state = new unsigned int[CHACHA20_STATE_DWORDS];

__internal_chacha20::init_state(initial_state,(unsigned int*)key,counter,nonce);
__internal_chacha20::apply_20rounds((unsigned int*)transformed_state,initial_state);
chacha20::init_state(initial_state,(unsigned int*)key,counter,nonce);
chacha20::apply_20rounds((unsigned int*)transformed_state,initial_state);

// We take the first 256 bits of the serialized state, and use those as the
// one-time Poly1305 key:
Expand Down Expand Up @@ -313,9 +313,9 @@ namespace ChaCha20_Poly1305
) {

unsigned char *poly1305_key = new unsigned char[32];
__internal_poly1305::key_gen(poly1305_key,key,(unsigned int*)nonce);
poly1305::key_gen(poly1305_key,key,(unsigned int*)nonce);

__internal_chacha20::encrypt(outputCipher,key,1,(unsigned char*)nonce,inputText,textLen);
chacha20::encrypt(outputCipher,key,1,(unsigned char*)nonce,inputText,textLen);

size_t padding1 = PADDING16(AAD_len);
size_t padding2 = PADDING16(textLen);
Expand All @@ -333,7 +333,7 @@ namespace ChaCha20_Poly1305
memcpy(mac_data+(curr_pos+=padding2), &AAD_len,8);
memcpy(mac_data+(curr_pos+=8), &textLen,8);

__internal_poly1305::mac(outputTag,poly1305_key,mac_data,mac_len);
poly1305::mac(outputTag,poly1305_key,mac_data,mac_len);

delete [] mac_data;
delete [] poly1305_key;
Expand All @@ -351,9 +351,9 @@ namespace ChaCha20_Poly1305
) {

unsigned char *poly1305_key = new unsigned char[32];
__internal_poly1305::key_gen(poly1305_key,key,(unsigned int*)nonce);
poly1305::key_gen(poly1305_key,key,(unsigned int*)nonce);

__internal_chacha20::encrypt(outputText,key,1,(unsigned char*)nonce,inputCipher,cipherLen);
chacha20::encrypt(outputText,key,1,(unsigned char*)nonce,inputCipher,cipherLen);

size_t padding1 = PADDING16(AAD_len);
size_t padding2 = PADDING16(cipherLen);
Expand All @@ -371,7 +371,7 @@ namespace ChaCha20_Poly1305
memcpy(mac_data+(curr_pos+=padding2), &AAD_len,8);
memcpy(mac_data+(curr_pos+=8), &cipherLen,8);

__internal_poly1305::mac(outputTag,poly1305_key,mac_data,mac_len);
poly1305::mac(outputTag,poly1305_key,mac_data,mac_len);

delete [] mac_data;
delete [] poly1305_key;
Expand Down
35 changes: 19 additions & 16 deletions ChaCha20-Poly1305.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef uidMRDCVLSC_ChaCha20_POLY1305_HPP
#define uidMRDCVLSC_ChaCha20_POLY1305_HPP
#ifndef idMRDCVLSC_ChaCha20_POLY1305_HP
#define idMRDCVLSC_ChaCha20_POLY1305_HP

#include <iostream>
#include <cstring>
Expand Down Expand Up @@ -154,41 +154,43 @@ class uint512 {
//=====================================================================

/// Number of bytes(unsigned char) inside a ChaCha20 State.
static const int __CHAx220_STATE_BYTES__ = 64;
#define CHACHA20_STATE_BYTES 64

/// Number of dwords(unsigned int) inside a ChaCha20 State.
static const int __CHAx220_STATE_DWORDS__ = 16;
#define CHACHA20_STATE_DWORDS 16

/// Number of bytes(unsigned char) inside a ChaCha20 key.
static const int __CHAx220_KEY_BYTES__ = 32;
#define CHACHA20_KEY_BYTES 32

/// Number of dwords(unsigned int) inside a ChaCha20 key.
static const int __CHAx220_KEY_DWORDS__ = 8;
#define CHACHA20_KEY_DWORDS 8

/// Number of bytes(unsigned char) inside a ChaCha20 nonce.
static const int __CHAx220_NONCE_BYTES__ = 12;
#define CHACHA20_NONCE_BYTES 12

/// Number of dwords(unsigned int) inside a ChaCha20 nonce.
static const int __CHAx220_NONCE_DWORDS__ = 3;
#define CHACHA20_NONCE_DWORDS 3

/// Number of bytes(unsigned char) inside a ChaCha20 block function output.
static const int __CHAx220_BLK_FUNC_OUTPUT_BYTES__ = 64;
#define CHACHA20_BLK_FUNC_OUTPUT_BYTES 64

/// Number of dwords(unsigned int) inside a ChaCha20 block function output.
static const int __CHAx220_BLK_FUNC_OUTPUT_DWORDS__ = 16;

#define CHACHA20_BLK_FUNC_OUTPUT_DWORDS 16

/// Number of bytes(unsigned char) inside a Poly1305 tag/mac output.
static const int __POLY1305_MAC_BYTES__ = 16;
#define POLY1305_MAC_BYTES 16

#define HALF_KEY_BYTES 16
#define __CHACHA_STATE_COUNTER_INDEX__ 12
/// Index of the counter in the chacha state.
#define CHACHA_STATE_COUNTER_INDEX 12

/// Half of the size of the key in bytes.
#define HALF_KEY_BYTES 16

/// get the padding needed for the textLen to become a multiple of 16
#define PADDING16(textLen) (textLen%16==0) ? 0 : (16-(textLen%16))

namespace __internal_chacha20
/// internal namespace containing functions for chacha20 in aead encryption.
namespace chacha20
{
inline unsigned int bit_left_roll(unsigned int num, size_t n);

Expand Down Expand Up @@ -258,7 +260,8 @@ namespace __internal_chacha20
);
}

namespace __internal_poly1305 {
/// internal namespace containing functions for poly1305 in aead encryption.
namespace poly1305 {

void clamp(unsigned char r[HALF_KEY_BYTES]);

Expand Down
10 changes: 5 additions & 5 deletions tests/BlockFunction_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ int main()
0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9, 0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e
};

unsigned int *initial_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int* output_state = new unsigned int[__CHAx220_BLK_FUNC_OUTPUT_DWORDS__];
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];

__internal_chacha20::init_state(initial_state,(unsigned int*)key,1,(unsigned int*)nonce);
__internal_chacha20::apply_20rounds(output_state,initial_state);
chacha20::init_state(initial_state,(unsigned int*)key,1,(unsigned int*)nonce);
chacha20::apply_20rounds(output_state,initial_state);

unsigned char* serialize = (unsigned char*) output_state;

bool serial_passed = true;
for(size_t i=0; i<__CHAx220_BLK_FUNC_OUTPUT_BYTES__; ++i) {
for(size_t i=0; i<CHACHA20_BLK_FUNC_OUTPUT_BYTES; ++i) {
if(serialize[i] != correct_serial[i]) {
serial_passed = false;
break;
Expand Down
8 changes: 4 additions & 4 deletions tests/BlockFunction_test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ int main() {
unsigned int counter = 0;

// ANSWERS
unsigned int *initial_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int* output_state = new unsigned int[__CHAx220_BLK_FUNC_OUTPUT_DWORDS__];
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];

__internal_chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
__internal_chacha20::apply_20rounds(output_state,initial_state);
chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
chacha20::apply_20rounds(output_state,initial_state);

unsigned char* serialize = (unsigned char*) output_state;

Expand Down
8 changes: 4 additions & 4 deletions tests/BlockFunction_test2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ int main() {
unsigned int counter = 1;

// ANSWERS
unsigned int *initial_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int* output_state = new unsigned int[__CHAx220_BLK_FUNC_OUTPUT_DWORDS__];
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];

__internal_chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
__internal_chacha20::apply_20rounds(output_state,initial_state);
chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
chacha20::apply_20rounds(output_state,initial_state);

unsigned char* serialize = (unsigned char*) output_state;

Expand Down
8 changes: 4 additions & 4 deletions tests/BlockFunction_test3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ int main() {
unsigned int counter = 1;

// ANSWERS
unsigned int *initial_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int* output_state = new unsigned int[__CHAx220_BLK_FUNC_OUTPUT_DWORDS__];
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];

__internal_chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
__internal_chacha20::apply_20rounds(output_state,initial_state);
chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
chacha20::apply_20rounds(output_state,initial_state);

unsigned char* serialize = (unsigned char*) output_state;

Expand Down
8 changes: 4 additions & 4 deletions tests/BlockFunction_test4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ int main() {
unsigned int counter = 2;

// ANSWERS
unsigned int *initial_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int* output_state = new unsigned int[__CHAx220_BLK_FUNC_OUTPUT_DWORDS__];
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];

__internal_chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
__internal_chacha20::apply_20rounds(output_state,initial_state);
chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
chacha20::apply_20rounds(output_state,initial_state);

unsigned char* serialize = (unsigned char*) output_state;

Expand Down
8 changes: 4 additions & 4 deletions tests/BlockFunction_test5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ int main() {
unsigned int counter = 0;

// ANSWERS
unsigned int *initial_state = new unsigned int[__CHAx220_STATE_DWORDS__];
unsigned int* output_state = new unsigned int[__CHAx220_BLK_FUNC_OUTPUT_DWORDS__];
unsigned int *initial_state = new unsigned int[CHACHA20_STATE_DWORDS];
unsigned int* output_state = new unsigned int[CHACHA20_BLK_FUNC_OUTPUT_DWORDS];

__internal_chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
__internal_chacha20::apply_20rounds(output_state,initial_state);
chacha20::init_state(initial_state,(unsigned int*)key,counter,(unsigned int*)nonce);
chacha20::apply_20rounds(output_state,initial_state);

unsigned char* serialize = (unsigned char*) output_state;

Expand Down
2 changes: 1 addition & 1 deletion tests/ChaCha20Encryption_t1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main() {

// ANSWERS

unsigned char *cipher_out = __internal_chacha20::encrypt(key,counter,nonce,plain_text,64);
unsigned char *cipher_out = chacha20::encrypt(key,counter,nonce,plain_text,64);

// TESTING ANSWER
std::cout << "cipher_text size = " << sizeof(cipher_text) << "\n";
Expand Down
2 changes: 1 addition & 1 deletion tests/ChaCha20Encryption_t2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int main() {

// ANSWERS

unsigned char *cipher_out = __internal_chacha20::encrypt(key,counter,nonce,plain_text,sizeof(plain_text));
unsigned char *cipher_out = chacha20::encrypt(key,counter,nonce,plain_text,sizeof(plain_text));

// TESTING ANSWER
std::cout << "cipher_text size = " << sizeof(cipher_text) << "\n";
Expand Down
2 changes: 1 addition & 1 deletion tests/ChaCha20Encryption_t3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int main() {

// ANSWERS

unsigned char *cipher_out = __internal_chacha20::encrypt(key,counter,nonce,plain_text,sizeof(plain_text));
unsigned char *cipher_out = chacha20::encrypt(key,counter,nonce,plain_text,sizeof(plain_text));

// TESTING ANSWER
ASSERT_ARRAY<unsigned char>(cipher_out,cipher_text,sizeof(cipher_text),"ChaCha20 encryption ",TEST_RESULTS);
Expand Down
2 changes: 1 addition & 1 deletion tests/Encryption_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main()
0x87, 0x4d
};

unsigned char *cipher = __internal_chacha20::encrypt(key,initial_counter,nonce,plaintext,114);
unsigned char *cipher = chacha20::encrypt(key,initial_counter,nonce,plaintext,114);

bool passed = true;
for(size_t i=0; i<114; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Poly1305_keygen_t1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main() {
// ANSWERS

unsigned char *genkey = new unsigned char[32];
__internal_poly1305::key_gen(genkey,chacha20key,(unsigned int*)nonce);
poly1305::key_gen(genkey,chacha20key,(unsigned int*)nonce);

// TESTING ANSWER
ASSERT_ARRAY<unsigned char>(genkey,polykey,sizeof(polykey),"Poly1305 key generation test ",TEST_RESULTS);
Expand Down
Loading

0 comments on commit 597a644

Please sign in to comment.