Line data Source code
1 : /*
2 : * Testing an implementation of RC4 encryption algorithm based off
3 : * verified plaintext->hash conversions
4 : */
5 : #include <gtest/gtest.h>
6 : #include <iostream>
7 : #include <limits.h>
8 : #include <openGPMP/nt/rc4.hpp>
9 : #include <sstream>
10 : #include <string>
11 :
12 : using ::testing::DoubleLE;
13 : using ::testing::FloatLE;
14 : using ::testing::InitGoogleTest;
15 :
16 : namespace {
17 : gpmp::RC4 rc;
18 :
19 : // BBF316E8D940AF0AD3
20 : char *key_0 = (char *)"Key";
21 : char *text_0 = (char *)"Plaintext";
22 : // 1021BF0420
23 : char *key_1 = (char *)"Wiki";
24 : char *text_1 = (char *)"pedia";
25 : // 45A01F645FC35B383552544B9BF5
26 : char *key_2 = (char *)"Secret";
27 : char *text_2 = (char *)"Attack at dawn";
28 :
29 4 : TEST(RC4Test, hash_XOR) {
30 1 : int x_swap = 0;
31 1 : std::string ptext_0 = "|xbb||xf3||x16||xe8||xd9||x40||xaf||x0a||xd3|";
32 : // allocate memory for the ciphertext pointer
33 : unsigned char *hashtext_0 =
34 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_0));
35 1 : rc.compute(key_0, text_0, hashtext_0, x_swap);
36 1 : std::string displayed_0 = rc.store_hash(text_0, hashtext_0, x_swap);
37 1 : EXPECT_EQ(ptext_0, displayed_0);
38 : /*<------------------------------------------------------------------>*/
39 2 : std::string ptext_01 = "|x10||x21||xbf||x04||x20|";
40 : unsigned char *hashtext_01 =
41 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_1));
42 1 : rc.compute(key_1, text_1, hashtext_01, x_swap);
43 2 : std::string displayed_01 = rc.store_hash(text_1, hashtext_01, x_swap);
44 1 : EXPECT_EQ(ptext_01, displayed_01);
45 : /*<------------------------------------------------------------------>*/
46 : std::string ptext_02 = "|x45||xa0||x1f||x64||x5f||xc3||x5b||x38||"
47 2 : "x35||x52||x54||x4b||x9b||xf5|";
48 : unsigned char *hashtext_02 =
49 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_2));
50 1 : rc.compute(key_2, text_2, hashtext_02, x_swap);
51 2 : std::string displayed_02 = rc.store_hash(text_2, hashtext_02, x_swap);
52 1 : EXPECT_EQ(ptext_02, displayed_02);
53 1 : }
54 :
55 4 : TEST(RC4Test, hash_TRADITIONAL) {
56 1 : int t_swap = 1;
57 1 : std::string ptext_1 = "BBF316E8D940AF0AD3";
58 : unsigned char *hashtext_10 =
59 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_0));
60 1 : rc.compute(key_0, text_0, hashtext_10, t_swap);
61 1 : std::string displayed_10 = rc.store_hash(text_0, hashtext_10, t_swap);
62 1 : EXPECT_EQ(ptext_1, displayed_10);
63 : /*<------------------------------------------------------------------>*/
64 2 : std::string ptext_11 = "1021BF0420";
65 : unsigned char *hashtext_11 =
66 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_1));
67 1 : rc.compute(key_1, text_1, hashtext_11, t_swap);
68 2 : std::string displayed_11 = rc.store_hash(text_1, hashtext_11, t_swap);
69 1 : EXPECT_EQ(ptext_11, displayed_11);
70 : /*<------------------------------------------------------------------>*/
71 2 : std::string ptext_12 = "45A01F645FC35B383552544B9BF5";
72 : unsigned char *hashtext_12 =
73 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_2));
74 1 : rc.compute(key_2, text_2, hashtext_12, t_swap);
75 2 : std::string displayed_12 = rc.store_hash(text_2, hashtext_12, t_swap);
76 1 : EXPECT_EQ(ptext_12, displayed_12);
77 1 : }
78 :
79 4 : TEST(RC4Test, hash_BYTE) {
80 1 : int b_swap = 2;
81 1 : std::string ptext_2 = "BBF316E8D940AF0AD3";
82 : unsigned char *hashtext_20 =
83 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_0));
84 1 : rc.compute(key_0, text_0, hashtext_20, b_swap);
85 1 : std::string displayed_20 = rc.store_hash(text_0, hashtext_20, b_swap);
86 1 : EXPECT_EQ(ptext_2, displayed_20);
87 : /*<------------------------------------------------------------------>*/
88 2 : std::string ptext_21 = "1021BF0420";
89 : unsigned char *hashtext_21 =
90 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_1));
91 1 : rc.compute(key_1, text_1, hashtext_21, b_swap);
92 2 : std::string displayed_21 = rc.store_hash(text_1, hashtext_21, b_swap);
93 1 : EXPECT_EQ(ptext_21, displayed_21);
94 : /*<------------------------------------------------------------------>*/
95 2 : std::string ptext_22 = "45A01F645FC35B383552544B9BF5";
96 : unsigned char *hashtext_22 =
97 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_2));
98 1 : rc.compute(key_2, text_2, hashtext_22, b_swap);
99 2 : std::string displayed_22 = rc.store_hash(text_2, hashtext_22, b_swap);
100 1 : EXPECT_EQ(ptext_22, displayed_22);
101 1 : }
102 :
103 4 : TEST(RC4Test, SWAP_error_4) {
104 1 : int swap_FALSE = 4;
105 1 : std::string expected_error_text = "[-] Invalid swap_type";
106 :
107 : /*BEGIN RC4 ITERATIONS*/
108 1 : std::string ptext_FALSE = "1021BF0420";
109 : unsigned char *hashtext_FALSE =
110 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_2));
111 :
112 1 : EXPECT_THROW(rc.compute(key_2, text_2, hashtext_FALSE, swap_FALSE),
113 1 : std::runtime_error);
114 :
115 1 : ASSERT_THROW(rc.compute(key_2, text_2, hashtext_FALSE, swap_FALSE),
116 1 : std::runtime_error);
117 1 : }
118 :
119 4 : TEST(RC4Test, SWAP_error_127) {
120 1 : int swap_FALSE_127 = 127;
121 1 : std::string expected_error_text_127 = "[-] Invalid swap_type";
122 :
123 : /*BEGIN RC4 ITERATIONS*/
124 1 : std::string ptext_FALSE_127 = "1021BF0420";
125 : unsigned char *hashtext_FALSE_127 =
126 1 : (unsigned char *)malloc(sizeof(int) * strlen(text_2));
127 :
128 1 : EXPECT_THROW(rc.compute(key_2, text_2, hashtext_FALSE_127, swap_FALSE_127),
129 1 : std::runtime_error);
130 :
131 1 : ASSERT_THROW(rc.compute(key_2, text_2, hashtext_FALSE_127, swap_FALSE_127),
132 1 : std::runtime_error);
133 1 : }
134 :
135 4 : TEST(RC4Test, NULL_ciphertext) {
136 1 : int swap_NULL = 0;
137 1 : std::string expected_error_text_127 = "[-] Error Allocating Memory";
138 :
139 : /*BEGIN RC4 ITERATIONS*/
140 1 : std::string ptext_NULL = "1021BF0420";
141 1 : unsigned char *hashtext_NULL = nullptr;
142 : //(unsigned char *)malloc(sizeof(int) *
143 : // strlen(swap_NULL));
144 :
145 1 : EXPECT_THROW(rc.compute(key_2, text_2, hashtext_NULL, swap_NULL),
146 1 : std::runtime_error);
147 :
148 1 : ASSERT_THROW(rc.compute(key_2, text_2, hashtext_NULL, swap_NULL),
149 1 : std::runtime_error);
150 1 : }
151 :
152 : } // namespace
|