Skip to content

Commit 46e3188

Browse files
jciberlinIgor-Misic
authored andcommitted
Implement MISRA compliant Vernam cipher/One Time Pad algorithm
- Vernam cipher/One Time Pad algorithm - MISRA compliant
1 parent 85f4ecf commit 46e3188

File tree

6 files changed

+208
-1
lines changed

6 files changed

+208
-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/One Time Pad 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/One Time Pad 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

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
59+
int32_t ascii_val;
60+
if (Utils_isUpperChar(msg[i])) {
61+
ascii_val = UPPER_A_ASCII;
62+
} else {
63+
ascii_val = LOWER_A_ASCII;
64+
}
65+
66+
int32_t temp = (((int8_t)msg[i] - ascii_val + (int8_t)key[i] - c) % NUM_OF_ALPHA);
67+
int32_t cipher = temp + ascii_val;
68+
msg[i] = (char)cipher;
69+
}
70+
}
71+
status = true;
72+
}
73+
return status;
74+
}
75+
76+
bool
77+
VernamCipher_decrypt(char* msg, const char* key, int32_t key_length) {
78+
bool status = false;
79+
const size_t msg_length = strlen(msg);
80+
if ((int32_t)msg_length == key_length) {
81+
for (int32_t i = 0; i < key_length; ++i) {
82+
if (Utils_isAlpha(msg[i])) {
83+
int8_t c;
84+
if (Utils_isLowerChar(key[i])) {
85+
c = LOWER_A_ASCII;
86+
} else {
87+
c = UPPER_A_ASCII;
88+
}
89+
90+
int32_t ascii_val;
91+
if (Utils_isUpperChar(msg[i])) {
92+
ascii_val = UPPER_A_ASCII;
93+
} else {
94+
ascii_val = LOWER_A_ASCII;
95+
}
96+
97+
int32_t cipher = ((msg[i] - ascii_val) - (key[i] - c));
98+
cipher = (cipher < 0) ? (cipher + NUM_OF_ALPHA + ascii_val) : (cipher + ascii_val);
99+
msg[i] = (char)cipher;
100+
}
101+
}
102+
status = true;
103+
}
104+
return status;
105+
}

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[] = "OSIJEK11";
23+
const char key2[] = "OSijek!/";
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)