Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utils, crypto updates #77

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions Inc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*
* @return true if the conversion was successful, false otherwise.
*/
bool Utils_StringToUint32(const char* str, uint8_t str_length, uint32_t* integer);
bool Utils_stringToUint32(const char* str, uint8_t str_length, uint32_t* integer);

/**
* @brief Swap two elements. Swap the content of two byte arrays of the same size.
Expand All @@ -76,7 +76,7 @@ void Utils_swapElements(byte_t* first, byte_t* second, uint32_t size);
* The lookup table contains the precomputed values of 10^0 to 10^9.
* If the given exponent is greater than 9, the function returns false.
*/
bool Utils_QuickUint32Pow10(uint8_t exponent, uint32_t* result);
bool Utils_quickUint32Pow10(uint8_t exponent, uint32_t* result);

/**
* @brief Serialize a 8-bit value into a buffer (for little-endian and big-endian).
Expand All @@ -85,7 +85,7 @@ bool Utils_QuickUint32Pow10(uint8_t exponent, uint32_t* result);
* @param value[in] The 8-bit value to be serialized.
*
*/
void Utils_Serialize8(byte_t* buf, uint8_t value);
void Utils_serialize8(byte_t* buf, uint8_t value);

/**
* @brief Deserializes a 8-bit value from a byte buffer (for little-endian and big-endian).
Expand All @@ -94,7 +94,7 @@ void Utils_Serialize8(byte_t* buf, uint8_t value);
* @return The deserialized 8-bit value.
*
*/
uint8_t Utils_Deserialize8(const byte_t* buf);
uint8_t Utils_deserialize8(const byte_t* buf);

/**
* @brief Serialize a 16-bit value in big-endian format into a buffer.
Expand All @@ -104,7 +104,7 @@ uint8_t Utils_Deserialize8(const byte_t* buf);
*
* @note The buffer must be at least 2 bytes long to accommodate the serialized value.
*/
void Utils_Serialize16BE(byte_t* buf, uint16_t value);
void Utils_serialize16BE(byte_t* buf, uint16_t value);

/**
* @brief Serialize a 24-bit value in big-endian format into a buffer.
Expand All @@ -114,7 +114,7 @@ void Utils_Serialize16BE(byte_t* buf, uint16_t value);
*
* @note The buffer must be at least 3 bytes long to accommodate the serialized value.
*/
void Utils_Serialize24BE(byte_t* buf, uint32_t value);
void Utils_serialize24BE(byte_t* buf, uint32_t value);

/**
* @brief Serialize a 32-bit value in big-endian format into a buffer.
Expand All @@ -124,7 +124,7 @@ void Utils_Serialize24BE(byte_t* buf, uint32_t value);
*
* @note The buffer must be at least 4 bytes long to accommodate the serialized value.
*/
void Utils_Serialize32BE(byte_t* buf, uint32_t value);
void Utils_serialize32BE(byte_t* buf, uint32_t value);

/**
* @brief Serialize a blob of data in big-endian format.
Expand All @@ -135,7 +135,7 @@ void Utils_Serialize32BE(byte_t* buf, uint32_t value);
* @param[in] src Pointer to the source data buffer to be serialized.
* @param[in] size Number of bytes to be serialized.
*/
void Utils_SerializeBlobBE(byte_t* buf, const byte_t* src, uint32_t size);
void Utils_serializeBlobBE(byte_t* buf, const byte_t* src, uint32_t size);

/**
* @brief Deserializes a 16-bit value from a big-endian byte buffer.
Expand All @@ -145,7 +145,7 @@ void Utils_SerializeBlobBE(byte_t* buf, const byte_t* src, uint32_t size);
*
* @note The buffer must be at least 2 bytes long to accommodate the deserialized value.
*/
uint16_t Utils_Deserialize16BE(const byte_t* buf);
uint16_t Utils_deserialize16BE(const byte_t* buf);

/**
* @brief Deserializes a 24-bit value from a big-endian byte buffer.
Expand All @@ -155,7 +155,7 @@ uint16_t Utils_Deserialize16BE(const byte_t* buf);
*
* @note The buffer must be at least 3 bytes long to accommodate the deserialized value.
*/
uint32_t Utils_Deserialize24BE(const byte_t* buf);
uint32_t Utils_deserialize24BE(const byte_t* buf);

/**
* @brief Deserializes a 32-bit value from a big-endian byte buffer.
Expand All @@ -165,7 +165,7 @@ uint32_t Utils_Deserialize24BE(const byte_t* buf);
*
* @note The buffer must be at least 4 bytes long to accommodate the deserialized value.
*/
uint32_t Utils_Deserialize32BE(const byte_t* buf);
uint32_t Utils_deserialize32BE(const byte_t* buf);

/**
* @brief Deserializes a blob(binary large object) in big-endian format.
Expand All @@ -174,7 +174,7 @@ uint32_t Utils_Deserialize32BE(const byte_t* buf);
* @param[out] dst The destination buffer where the deserialized blob will be stored.
* @param[in] size The size in bytes of the blob to deserialize.
*/
void Utils_DeserializeBlobBE(const byte_t* buf, byte_t* dst, uint32_t size);
void Utils_deserializeBlobBE(const byte_t* buf, byte_t* dst, uint32_t size);

/**
* @brief Serialize a 16-bit value in little-endian format into a buffer.
Expand All @@ -184,7 +184,7 @@ void Utils_DeserializeBlobBE(const byte_t* buf, byte_t* dst, uint32_t size);
*
* @note The buffer must be at least 2 bytes long to accommodate the serialized value.
*/
void Utils_Serialize16LE(byte_t* buf, uint16_t value);
void Utils_serialize16LE(byte_t* buf, uint16_t value);

/**
* @brief Serialize a 24-bit value in little-endian format into a buffer.
Expand All @@ -194,7 +194,7 @@ void Utils_Serialize16LE(byte_t* buf, uint16_t value);
*
* @note The buffer must be at least 3 bytes long to accommodate the serialized value.
*/
void Utils_Serialize24LE(byte_t* buf, uint32_t value);
void Utils_serialize24LE(byte_t* buf, uint32_t value);

/**
* @brief Serialize a 32-bit value in little-endian format into a buffer.
Expand All @@ -204,7 +204,7 @@ void Utils_Serialize24LE(byte_t* buf, uint32_t value);
*
* @note The buffer must be at least 4 bytes long to accommodate the serialized value.
*/
void Utils_Serialize32LE(byte_t* buf, uint32_t value);
void Utils_serialize32LE(byte_t* buf, uint32_t value);

/**
* @brief Serialize a blob of data in little-endian format.
Expand All @@ -215,7 +215,7 @@ void Utils_Serialize32LE(byte_t* buf, uint32_t value);
* @param[in] src Pointer to the source data buffer to be serialized.
* @param[in] size Number of bytes to be serialized.
*/
void Utils_SerializeBlobLE(byte_t* buf, const byte_t* src, uint32_t size);
void Utils_serializeBlobLE(byte_t* buf, const byte_t* src, uint32_t size);

/**
* @brief Deserializes a 16-bit value from a little-endian byte buffer.
Expand All @@ -225,7 +225,7 @@ void Utils_SerializeBlobLE(byte_t* buf, const byte_t* src, uint32_t size);
*
* @note The buffer must be at least 2 bytes long to accommodate the deserialized value.
*/
uint16_t Utils_Deserialize16LE(const byte_t* buf);
uint16_t Utils_deserialize16LE(const byte_t* buf);

/**
* @brief Deserializes a 24-bit value from a little-endian byte buffer.
Expand All @@ -235,7 +235,7 @@ uint16_t Utils_Deserialize16LE(const byte_t* buf);
*
* @note The buffer must be at least 3 bytes long to accommodate the deserialized value.
*/
uint32_t Utils_Deserialize24LE(const byte_t* buf);
uint32_t Utils_deserialize24LE(const byte_t* buf);

/**
* @brief Deserializes a 32-bit value from a little-endian byte buffer.
Expand All @@ -245,7 +245,7 @@ uint32_t Utils_Deserialize24LE(const byte_t* buf);
*
* @note The buffer must be at least 4 bytes long to accommodate the deserialized value.
*/
uint32_t Utils_Deserialize32LE(const byte_t* buf);
uint32_t Utils_deserialize32LE(const byte_t* buf);

/**
* @brief Deserializes a blob(binary large object) in little-endian format.
Expand All @@ -254,7 +254,7 @@ uint32_t Utils_Deserialize32LE(const byte_t* buf);
* @param[out] dst The destination buffer where the deserialized blob will be stored.
* @param[in] size The size in bytes of the blob to deserialize.
*/
void Utils_DeserializeBlobLE(const byte_t* buf, byte_t* dst, uint32_t size);
void Utils_deserializeBlobLE(const byte_t* buf, byte_t* dst, uint32_t size);

/**
* @brief Check if the given character is upper alphabetical letter.
Expand Down
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,27 @@ Join us on the Discord channel https://discord.gg/R6nZxZqDH3
- SelectionSort_sort

### Utils
- Utils_StringToUint32
- Utils_stringToUint32
- Utils_swapElements
- Utils_QuickUint32Pow10
- Utils_Serialize8
- Utils_Deserialize8
- Utils_Serialize16BE
- Utils_Serialize24BE
- Utils_Serialize32BE
- Utils_SerializeBlobBE
- Utils_Deserialize16BE
- Utils_Deserialize24BE
- Utils_Deserialize32BE
- Utils_DeserializeBlobBE
- Utils_Serialize16LE
- Utils_Serialize24LE
- Utils_Serialize32LE
- Utils_SerializeBlobLE
- Utils_Deserialize16LE
- Utils_Deserialize24LE
- Utils_Deserialize32LE
- Utils_DeserializeBlobLE
- Utils_quickUint32Pow10
- Utils_serialize8
- Utils_deserialize8
- Utils_serialize16BE
- Utils_serialize24BE
- Utils_serialize32BE
- Utils_serializeBlobBE
- Utils_deserialize16BE
- Utils_deserialize24BE
- Utils_deserialize32BE
- Utils_deserializeBlobBE
- Utils_serialize16LE
- Utils_serialize24LE
- Utils_serialize32LE
- Utils_serializeBlobLE
- Utils_deserialize16LE
- Utils_deserialize24LE
- Utils_deserialize32LE
- Utils_deserializeBlobLE
- Utils_isUpperChar
- Utils_isLowerChar
- Utils_isAlpha
14 changes: 6 additions & 8 deletions Src/crypto/caesar_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ CaesarCipher_encrypt(char* msg, uint8_t shift) {
const size_t length = strlen(msg);
for (size_t i = 0U; i < length; ++i) {
if (Utils_isAlpha(msg[i])) {
uint8_t encrypted_char;
uint8_t ascii_val = LOWER_A_ASCII;
if (Utils_isUpperChar(msg[i])) {
encrypted_char = (((uint8_t)msg[i] + shift - UPPER_A_ASCII) % MAX_SHIFT) + UPPER_A_ASCII;
} else {
encrypted_char = (((uint8_t)msg[i] + shift - LOWER_A_ASCII) % MAX_SHIFT) + LOWER_A_ASCII;
ascii_val = UPPER_A_ASCII;
}
uint8_t encrypted_char = (((uint8_t)msg[i] + shift - ascii_val) % MAX_SHIFT) + ascii_val;
msg[i] = (char)encrypted_char;
}
}
Expand All @@ -63,12 +62,11 @@ CaesarCipher_decrypt(char* msg, uint8_t shift) {
const size_t length = strlen(msg);
for (size_t i = 0U; i < length; ++i) {
if (Utils_isAlpha(msg[i])) {
uint8_t decrypted_char;
uint8_t ascii_val = LOWER_A_ASCII;
if (Utils_isUpperChar(msg[i])) {
decrypted_char = (((uint8_t)msg[i] - shift - UPPER_A_ASCII) % MAX_SHIFT) + UPPER_A_ASCII;
} else {
decrypted_char = (((uint8_t)msg[i] - shift - LOWER_A_ASCII) % MAX_SHIFT) + LOWER_A_ASCII;
ascii_val = UPPER_A_ASCII;
}
uint8_t decrypted_char = (((uint8_t)msg[i] - shift - ascii_val) % MAX_SHIFT) + ascii_val;
msg[i] = (char)decrypted_char;
}
}
Expand Down
76 changes: 38 additions & 38 deletions Src/crypto/chacha20.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ Block(const byte_t key[CHACHA20_KEY_SIZE], uint32_t counter, const byte_t nonce[
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 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]);
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 = 0U; i < CHACHA20_ROUNDS; ++i) {
QuarterRound(&x0, &x4, &x8, &x12);
Expand All @@ -109,36 +109,36 @@ Block(const byte_t key[CHACHA20_KEY_SIZE], uint32_t counter, const byte_t nonce[
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]);
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]);
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);
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
Expand Down
16 changes: 4 additions & 12 deletions Src/crypto/vernam_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ VernamCipher_encrypt(char* msg, const char* key, int32_t key_length) {
if ((int32_t)msg_length == key_length) {
for (int32_t i = 0; i < key_length; ++i) {
if (Utils_isAlpha(msg[i])) {
int8_t c;
int8_t c = UPPER_A_ASCII;
if (Utils_isLowerChar(key[i])) {
c = LOWER_A_ASCII;
} else {
c = UPPER_A_ASCII;
}

int32_t ascii_val;
int32_t ascii_val = LOWER_A_ASCII;
if (Utils_isUpperChar(msg[i])) {
ascii_val = UPPER_A_ASCII;
} else {
ascii_val = LOWER_A_ASCII;
}

int32_t temp = (((int8_t)msg[i] - ascii_val + (int8_t)key[i] - c) % NUM_OF_ALPHA);
Expand All @@ -80,18 +76,14 @@ VernamCipher_decrypt(char* msg, const char* key, int32_t key_length) {
if ((int32_t)msg_length == key_length) {
for (int32_t i = 0; i < key_length; ++i) {
if (Utils_isAlpha(msg[i])) {
int8_t c;
int8_t c = UPPER_A_ASCII;
if (Utils_isLowerChar(key[i])) {
c = LOWER_A_ASCII;
} else {
c = UPPER_A_ASCII;
}

int32_t ascii_val;
int32_t ascii_val = LOWER_A_ASCII;
if (Utils_isUpperChar(msg[i])) {
ascii_val = UPPER_A_ASCII;
} else {
ascii_val = LOWER_A_ASCII;
}

int32_t cipher = ((msg[i] - ascii_val) - (key[i] - c));
Expand Down
Loading
Loading