Skip to content

Commit a4cf79e

Browse files
committed
Implement MISRA compliant vernam cipher algorithm
- Vernam cipher algorithm - MISRA compliant
1 parent f6ce64e commit a4cf79e

File tree

6 files changed

+202
-1
lines changed

6 files changed

+202
-1
lines changed

Inc/crypto/vernam_cipher.h

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2024 IMProject Development Team. All rights reserved.
4+
* Authors: Juraj Ciberlin <[email protected]>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* 3. Neither the name IMProject nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
****************************************************************************/
34+
35+
#ifndef VERNAM_CIPHER_H_
36+
#define VERNAM_CIPHER_H_
37+
38+
#include "typedefs.h"
39+
40+
/**
41+
* @brief Encryption using Vernam cipher algorithm.
42+
*
43+
* @param[in/out] *msg Pointer to message that will be encrypted.
44+
* @param[in] *key Pointer to key string.
45+
* @param[in] key_length Key length.
46+
*
47+
* @return Status of encryption function. True if message size is higher than or equal to key length, otherwise false.
48+
*/
49+
bool VernamCipher_encrypt(char* msg, const char* key, int32_t key_length);
50+
51+
/**
52+
* @brief Decryption using Vernam cipher algorithm.
53+
*
54+
* @param[in/out] *msg Pointer to message that will be decrypted.
55+
* @param[in] *key Pointer to key string.
56+
* @param[in] key_length Key length.
57+
*
58+
* @return Status of decryption function. True if message size is higher than or equal to key length, otherwise false.
59+
*/
60+
bool VernamCipher_decrypt(char* msg, const char* key, int32_t key_length);
61+
62+
#endif /* VERNAM_CIPHER_H_ */

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ IMUTILITY_FILES=\
107107
Src/crc/crc32_variants/crc32_xfer.c \
108108
Src/crypto/caesar_cipher.c \
109109
Src/crypto/chacha20.c \
110+
Src/crypto/vernam_cipher.c \
110111
Src/sort/bubble_sort.c \
111112
Src/sort/heap_sort.c \
112113
Src/sort/insertion_sort.c \
@@ -139,7 +140,8 @@ SRC_FILES+=$(IMUTILITY_FILES) \
139140
Tests/test_queue.c \
140141
Tests/test_scheduler.c \
141142
Tests/test_selection_sort.c \
142-
Tests/test_utils.c
143+
Tests/test_utils.c \
144+
Tests/test_vernam_cipher.c
143145

144146
INC_DIRS_CODE= \
145147
-IInc \

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Join us on the Discord channel https://discord.gg/R6nZxZqDH3
123123
- CaesarCipher_encrypt
124124
- CaesarCipher_decrypt
125125
- Chacha20_crypt
126+
- VernamCipher_encrypt
127+
- VernamCipher_decrypt
126128

127129
### JSON
128130
- Json_startString

Src/crypto/vernam_cipher.c

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2024 IMProject Development Team. All rights reserved.
4+
* Authors: Juraj Ciberlin <[email protected]>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* 3. Neither the name IMProject nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
****************************************************************************/
34+
35+
#include "vernam_cipher.h"
36+
37+
#include <string.h>
38+
39+
#include "utils.h"
40+
41+
#define UPPER_A_ASCII (65)
42+
#define LOWER_A_ASCII (97)
43+
#define NUM_OF_ALPHA (26)
44+
45+
bool
46+
VernamCipher_encrypt(char* msg, const char* key, int32_t key_length) {
47+
bool status = false;
48+
const size_t msg_length = strlen(msg);
49+
if ((int32_t)msg_length == key_length) {
50+
for (int32_t i = 0; i < key_length; ++i) {
51+
if (Utils_isAlpha(msg[i])) {
52+
int8_t c;
53+
if (Utils_isLowerChar(key[i])) {
54+
c = LOWER_A_ASCII;
55+
} else {
56+
c = UPPER_A_ASCII;
57+
}
58+
int32_t cipher;
59+
if (Utils_isUpperChar(msg[i])) {
60+
cipher = ((msg[i] - UPPER_A_ASCII + key[i] - c) % NUM_OF_ALPHA) + UPPER_A_ASCII;
61+
} else {
62+
cipher = ((msg[i] - LOWER_A_ASCII + key[i] - c) % NUM_OF_ALPHA) + LOWER_A_ASCII;
63+
}
64+
msg[i] = (char)cipher;
65+
}
66+
}
67+
status = true;
68+
}
69+
return status;
70+
}
71+
72+
bool
73+
VernamCipher_decrypt(char* msg, const char* key, int32_t key_length) {
74+
bool status = false;
75+
const size_t msg_length = strlen(msg);
76+
if ((int32_t)msg_length == key_length) {
77+
for (int32_t i = 0; i < key_length; ++i) {
78+
if (Utils_isAlpha(msg[i])) {
79+
int8_t c;
80+
if (Utils_isLowerChar(key[i])) {
81+
c = LOWER_A_ASCII;
82+
} else {
83+
c = UPPER_A_ASCII;
84+
}
85+
int32_t cipher;
86+
if (Utils_isUpperChar(msg[i])) {
87+
cipher = ((msg[i] - UPPER_A_ASCII) - (key[i] - c));
88+
cipher = (cipher < 0) ? (cipher + NUM_OF_ALPHA + UPPER_A_ASCII) : (cipher + UPPER_A_ASCII);
89+
} else {
90+
cipher = ((msg[i] - LOWER_A_ASCII) - (key[i] - c));
91+
cipher = (cipher < 0) ? (cipher + NUM_OF_ALPHA + LOWER_A_ASCII) : (cipher + LOWER_A_ASCII);
92+
}
93+
msg[i] = (char)cipher;
94+
}
95+
}
96+
status = true;
97+
}
98+
return status;
99+
}

Tests/test_main.c

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ RunAllTests(void) {
2020
RUN_TEST_GROUP(Scheduler);
2121
RUN_TEST_GROUP(SelectionSort);
2222
RUN_TEST_GROUP(Utils);
23+
RUN_TEST_GROUP(VernamCipher);
2324
}
2425

2526
int

Tests/test_vernam_cipher.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "vernam_cipher.h"
2+
3+
#include <string.h>
4+
5+
#include "unity.h"
6+
#include "unity_fixture.h"
7+
8+
TEST_GROUP(VernamCipher);
9+
10+
TEST_SETUP(VernamCipher) {
11+
}
12+
13+
TEST_TEAR_DOWN(VernamCipher) {
14+
}
15+
16+
TEST_GROUP_RUNNER(VernamCipher) {
17+
RUN_TEST_CASE(VernamCipher, VernamCipher_encryptDecrypt);
18+
}
19+
20+
TEST(VernamCipher, VernamCipher_encryptDecrypt) {
21+
const char expected_result[] = "ReSuLt1a";
22+
const char key1[] = "HELLOOab";
23+
const char key2[] = "HeLLo a/";
24+
char message[] = "ReSuLt1a";
25+
TEST_ASSERT_TRUE(VernamCipher_encrypt(message, key1, (int32_t)strlen(key1)));
26+
TEST_ASSERT_TRUE(VernamCipher_decrypt(message, key1, (int32_t)strlen(key1)));
27+
TEST_ASSERT_EQUAL_STRING(expected_result, message);
28+
29+
TEST_ASSERT_TRUE(VernamCipher_encrypt(message, key2, (int32_t)strlen(key2)));
30+
TEST_ASSERT_TRUE(VernamCipher_decrypt(message, key2, (int32_t)strlen(key2)));
31+
TEST_ASSERT_EQUAL_STRING(expected_result, message);
32+
33+
TEST_ASSERT_FALSE(VernamCipher_encrypt(message, key1, 3));
34+
TEST_ASSERT_FALSE(VernamCipher_decrypt(message, key1, 100));
35+
}

0 commit comments

Comments
 (0)