20
20
#include < gtest/gtest.h>
21
21
22
22
TEST (TestShaEncryptUtils, TestAesEncryptDecrypt) {
23
- // 8 bytes key
24
- auto * key = " 1234abcd " ;
23
+ // 16 bytes key
24
+ auto * key = " 12345678abcdefgh " ;
25
25
auto * to_encrypt = " some test string" ;
26
26
27
+ auto key_len = static_cast <int32_t >(strlen (reinterpret_cast <const char *>(key)));
27
28
auto to_encrypt_len =
28
29
static_cast <int32_t >(strlen (reinterpret_cast <const char *>(to_encrypt)));
29
30
unsigned char cipher_1[64 ];
30
31
31
- int32_t cipher_1_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, cipher_1);
32
+ int32_t cipher_1_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, key_len, cipher_1);
32
33
33
34
unsigned char decrypted_1[64 ];
34
35
int32_t decrypted_1_len = gandiva::aes_decrypt (reinterpret_cast <const char *>(cipher_1),
35
- cipher_1_len, key, decrypted_1);
36
+ cipher_1_len, key, key_len, decrypted_1);
36
37
37
38
EXPECT_EQ (std::string (reinterpret_cast <const char *>(to_encrypt), to_encrypt_len),
38
39
std::string (reinterpret_cast <const char *>(decrypted_1), decrypted_1_len));
39
40
40
- // 16 bytes key
41
- key = " 12345678abcdefgh " ;
41
+ // 24 bytes key
42
+ key = " 12345678abcdefgh12345678 " ;
42
43
to_encrypt = " some\n test\n string" ;
43
44
45
+ key_len = static_cast <int32_t >(strlen (reinterpret_cast <const char *>(key)));
44
46
to_encrypt_len =
45
47
static_cast <int32_t >(strlen (reinterpret_cast <const char *>(to_encrypt)));
46
48
unsigned char cipher_2[64 ];
47
49
48
- int32_t cipher_2_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, cipher_2);
50
+ int32_t cipher_2_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, key_len, cipher_2);
49
51
50
52
unsigned char decrypted_2[64 ];
51
53
int32_t decrypted_2_len = gandiva::aes_decrypt (reinterpret_cast <const char *>(cipher_2),
52
- cipher_2_len, key, decrypted_2);
54
+ cipher_2_len, key, key_len, decrypted_2);
53
55
54
56
EXPECT_EQ (std::string (reinterpret_cast <const char *>(to_encrypt), to_encrypt_len),
55
57
std::string (reinterpret_cast <const char *>(decrypted_2), decrypted_2_len));
@@ -58,97 +60,51 @@ TEST(TestShaEncryptUtils, TestAesEncryptDecrypt) {
58
60
key = " 12345678abcdefgh12345678abcdefgh" ;
59
61
to_encrypt = " New\n test\n string" ;
60
62
63
+ key_len = static_cast <int32_t >(strlen (reinterpret_cast <const char *>(key)));
61
64
to_encrypt_len =
62
65
static_cast <int32_t >(strlen (reinterpret_cast <const char *>(to_encrypt)));
63
66
unsigned char cipher_3[64 ];
64
67
65
- int32_t cipher_3_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, cipher_3);
68
+ int32_t cipher_3_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, key_len, cipher_3);
66
69
67
70
unsigned char decrypted_3[64 ];
68
71
int32_t decrypted_3_len = gandiva::aes_decrypt (reinterpret_cast <const char *>(cipher_3),
69
- cipher_3_len, key, decrypted_3);
72
+ cipher_3_len, key, key_len, decrypted_3);
70
73
71
74
EXPECT_EQ (std::string (reinterpret_cast <const char *>(to_encrypt), to_encrypt_len),
72
75
std::string (reinterpret_cast <const char *>(decrypted_3), decrypted_3_len));
73
76
74
- // 64 bytes key
77
+ // check exception
78
+ char cipher[64 ] = " JBB7oJAQuqhDCx01fvBRi8PcljW1+nbnOSMk+R0Sz7E==" ;
79
+ int32_t cipher_len = static_cast <int32_t >(strlen (reinterpret_cast <const char *>(cipher)));
80
+ unsigned char plain_text[64 ];
81
+
75
82
key = " 12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh" ;
76
83
to_encrypt = " New\n test\n string" ;
77
84
85
+ key_len = static_cast <int32_t >(strlen (reinterpret_cast <const char *>(key)));
78
86
to_encrypt_len =
79
87
static_cast <int32_t >(strlen (reinterpret_cast <const char *>(to_encrypt)));
80
88
unsigned char cipher_4[64 ];
89
+ ASSERT_THROW ({
90
+ gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, key_len, cipher_4);
91
+ }, std::runtime_error);
81
92
82
- int32_t cipher_4_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, cipher_4);
83
-
84
- unsigned char decrypted_4[64 ];
85
- int32_t decrypted_4_len = gandiva::aes_decrypt (reinterpret_cast <const char *>(cipher_4),
86
- cipher_4_len, key, decrypted_4);
87
-
88
- EXPECT_EQ (std::string (reinterpret_cast <const char *>(to_encrypt), to_encrypt_len),
89
- std::string (reinterpret_cast <const char *>(decrypted_4), decrypted_4_len));
90
-
91
- // 128 bytes key
92
- key =
93
- " 12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12"
94
- " 345678abcdefgh12345678abcdefgh12345678abcdefgh" ;
95
- to_encrypt = " A much more longer string then the previous one, but without newline" ;
96
-
97
- to_encrypt_len =
98
- static_cast <int32_t >(strlen (reinterpret_cast <const char *>(to_encrypt)));
99
- unsigned char cipher_5[128 ];
100
-
101
- int32_t cipher_5_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, cipher_5);
102
-
103
- unsigned char decrypted_5[128 ];
104
- int32_t decrypted_5_len = gandiva::aes_decrypt (reinterpret_cast <const char *>(cipher_5),
105
- cipher_5_len, key, decrypted_5);
106
-
107
- EXPECT_EQ (std::string (reinterpret_cast <const char *>(to_encrypt), to_encrypt_len),
108
- std::string (reinterpret_cast <const char *>(decrypted_5), decrypted_5_len));
109
-
110
- // 192 bytes key
111
- key =
112
- " 12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12"
113
- " 345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh1234"
114
- " 5678abcdefgh12345678abcdefgh" ;
115
- to_encrypt =
116
- " A much more longer string then the previous one, but with \n newline, pretty cool, "
117
- " right?" ;
118
-
119
- to_encrypt_len =
120
- static_cast <int32_t >(strlen (reinterpret_cast <const char *>(to_encrypt)));
121
- unsigned char cipher_6[256 ];
122
-
123
- int32_t cipher_6_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, cipher_6);
124
-
125
- unsigned char decrypted_6[256 ];
126
- int32_t decrypted_6_len = gandiva::aes_decrypt (reinterpret_cast <const char *>(cipher_6),
127
- cipher_6_len, key, decrypted_6);
93
+ ASSERT_THROW ({
94
+ gandiva::aes_decrypt (cipher, cipher_len, key, key_len, plain_text);
95
+ }, std::runtime_error);
128
96
129
- EXPECT_EQ (std::string (reinterpret_cast <const char *>(to_encrypt), to_encrypt_len),
130
- std::string (reinterpret_cast <const char *>(decrypted_6), decrypted_6_len));
131
-
132
- // 256 bytes key
133
- key =
134
- " 12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12"
135
- " 345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh1234"
136
- " 5678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh123456"
137
- " 78abcdefgh" ;
138
- to_encrypt =
139
- " A much more longer string then the previous one, but with \n newline, pretty cool, "
140
- " right?" ;
97
+ key = " 12345678" ;
98
+ to_encrypt = " New\n test\n string" ;
141
99
100
+ key_len = static_cast <int32_t >(strlen (reinterpret_cast <const char *>(key)));
142
101
to_encrypt_len =
143
102
static_cast <int32_t >(strlen (reinterpret_cast <const char *>(to_encrypt)));
144
- unsigned char cipher_7[256 ];
145
-
146
- int32_t cipher_7_len = gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, cipher_7);
147
-
148
- unsigned char decrypted_7[256 ];
149
- int32_t decrypted_7_len = gandiva::aes_decrypt (reinterpret_cast <const char *>(cipher_7),
150
- cipher_7_len, key, decrypted_7);
151
-
152
- EXPECT_EQ (std::string (reinterpret_cast <const char *>(to_encrypt), to_encrypt_len),
153
- std::string (reinterpret_cast <const char *>(decrypted_7), decrypted_7_len));
103
+ unsigned char cipher_5[64 ];
104
+ ASSERT_THROW ({
105
+ gandiva::aes_encrypt (to_encrypt, to_encrypt_len, key, key_len, cipher_5);
106
+ }, std::runtime_error);
107
+ ASSERT_THROW ({
108
+ gandiva::aes_decrypt (cipher, cipher_len, key, key_len, plain_text);
109
+ }, std::runtime_error);
154
110
}
0 commit comments