Skip to content

Commit d94a982

Browse files
committed
my_crypt unittest
1 parent e238d6c commit d94a982

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

unittest/mysys/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
# along with this program; if not, write to the Free Software
1414
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1515

16-
MY_ADD_TESTS(bitmap base64 my_vsnprintf my_atomic my_rdtsc lf my_malloc
16+
MY_ADD_TESTS(bitmap base64 my_vsnprintf my_atomic my_rdtsc lf my_malloc aes
1717
LINK_LIBRARIES mysys)
1818

19+
ADD_DEFINITIONS(${SSL_DEFINES})
20+
1921
MY_ADD_TESTS(ma_dyncol
2022
LINK_LIBRARIES mysqlclient)
2123

unittest/mysys/aes-t.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* Copyright (c) 2003, 2006, 2007 MySQL AB, 2009 Sun Microsystems, Inc.
2+
Use is subject to license terms.
3+
4+
This program is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU General Public License as
6+
published by the Free Software Foundation; version 2 of the License.
7+
8+
This program is distributed in the hope that it will be useful, but
9+
WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16+
17+
#include <my_global.h>
18+
#include <my_sys.h>
19+
#include <my_crypt.h>
20+
#include <tap.h>
21+
#include <string.h>
22+
#include <ctype.h>
23+
24+
#define DO_TEST_P(mode, slen, fill, dlen, hash) \
25+
do { \
26+
memset(src, fill, src_len= slen); \
27+
ok(my_aes_encrypt_ ## mode(src, src_len, dst, &dst_len, \
28+
key, sizeof(key), iv, sizeof(iv), 0) == MY_AES_OK, \
29+
"encrypt " #mode " %u", src_len); \
30+
ok(dst_len <= my_aes_get_size(src_len), \
31+
"my_aes_get_size(%u) >= %u", src_len, dst_len); \
32+
my_md5(md5, (char*)dst, dst_len); \
33+
ok(dst_len == dlen && memcmp(md5, hash, sizeof(md5)) == 0, "md5"); \
34+
ok(my_aes_decrypt_ ## mode(dst, dst_len, ddst, &ddst_len, \
35+
key, sizeof(key), iv, sizeof(iv), 0) == MY_AES_OK, \
36+
"decrypt " #mode " %u", dst_len); \
37+
ok(ddst_len == src_len && memcmp(src, ddst, src_len) == 0, "memcmp"); \
38+
} while(0)
39+
40+
#define DO_TEST_N(mode, slen, fill, dlen, hash) \
41+
do { \
42+
memset(src, fill, src_len= slen); \
43+
ok(my_aes_encrypt_ ## mode(src, src_len, dst, &dst_len, \
44+
key, sizeof(key), iv, sizeof(iv), 1) == MY_AES_OK, \
45+
"encrypt " #mode " %u nopad", src_len); \
46+
my_md5(md5, (char*)dst, dst_len); \
47+
ok(dst_len == dlen && memcmp(md5, hash, sizeof(md5)) == 0, "md5"); \
48+
ok(my_aes_decrypt_ ## mode(dst, dst_len, ddst, &ddst_len, \
49+
key, sizeof(key), iv, sizeof(iv), 1) == MY_AES_OK, \
50+
"decrypt " #mode " %u", dst_len); \
51+
ok(ddst_len == src_len && memcmp(src, ddst, src_len) == 0, "memcmp"); \
52+
} while(0)
53+
54+
#ifdef HAVE_YASSL
55+
#define DO_TEST_X(mode, slen, fill, dlen, hash) \
56+
skip(4, #mode " is not supported")
57+
#else
58+
#define DO_TEST_X(mode, slen, fill, dlen, hash) \
59+
do { \
60+
memset(src, fill, src_len= slen); \
61+
ok(my_aes_encrypt_ ## mode(src, src_len, dst, &dst_len, \
62+
key, sizeof(key), iv, sizeof(iv)) == MY_AES_OK, \
63+
"encrypt " #mode " %u", src_len); \
64+
my_md5(md5, (char*)dst, dst_len); \
65+
ok(dst_len == dlen && memcmp(md5, hash, sizeof(md5)) == 0, "md5"); \
66+
ok(my_aes_decrypt_ ## mode(dst, dst_len, ddst, &ddst_len, \
67+
key, sizeof(key), iv, sizeof(iv)) == MY_AES_OK, \
68+
"decrypt " #mode " %u", dst_len); \
69+
ok(ddst_len == src_len && memcmp(src, ddst, src_len) == 0, "memcmp"); \
70+
} while(0)
71+
#endif
72+
73+
/* useful macro for debugging */
74+
#define PRINT_MD5() \
75+
do { \
76+
uint i; \
77+
printf("\""); \
78+
for (i=0; i < sizeof(md5); i++) \
79+
printf("\\x%02x", md5[i]); \
80+
printf("\"\n"); \
81+
} while(0);
82+
83+
84+
int
85+
main(int argc __attribute__((unused)),char *argv[])
86+
{
87+
uchar key[16]= {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
88+
uchar iv[16]= {2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7};
89+
uchar src[1000], dst[1100], ddst[1000];
90+
uchar md5[MY_MD5_HASH_SIZE];
91+
uint src_len, dst_len, ddst_len;
92+
93+
MY_INIT(argv[0]);
94+
95+
plan(84);
96+
DO_TEST_P(ecb, 200, '.', 208, "\xd8\x73\x8e\x3a\xbc\x66\x99\x13\x7f\x90\x23\x52\xee\x97\x6f\x9a");
97+
DO_TEST_P(ecb, 128, '?', 144, "\x19\x58\x33\x85\x4c\xaa\x7f\x06\xd1\xb2\xec\xd7\xb7\x6a\xa9\x5b");
98+
DO_TEST_P(cbc, 159, '%', 160, "\x4b\x03\x18\x3d\xf1\xa7\xcd\xa1\x46\xb3\xc6\x8a\x92\xc0\x0f\xc9");
99+
DO_TEST_P(cbc, 192, '@', 208, "\x54\xc4\x75\x1d\xff\xe0\xf6\x80\xf0\x85\xbb\x8b\xda\x07\x21\x17");
100+
DO_TEST_N(ecb, 200, '.', 200, "\xbf\xec\x43\xd1\x66\x8d\x01\xad\x3a\x25\xee\xa6\x3d\xc6\xc4\x68");
101+
DO_TEST_N(ecb, 128, '?', 128, "\x5b\x44\x20\xf3\xd9\xb4\x9d\x74\x5e\xb7\x5a\x0a\xe7\x32\x35\xc3");
102+
DO_TEST_N(cbc, 159, '%', 159, "\xf3\x6e\x40\x00\x3c\x08\xa0\xb1\x2d\x1f\xcf\xce\x54\xc9\x73\x83");
103+
DO_TEST_N(cbc, 192, '@', 192, "\x30\xe5\x28\x8c\x4a\x3b\x02\xd7\x56\x40\x59\x25\xac\x58\x09\x22");
104+
DO_TEST_X(ctr, 200, '.', 200, "\x5a\x77\x19\xea\x67\x50\xe3\xab\x7f\x39\x6f\xc4\xa8\x09\xc5\x88");
105+
DO_TEST_X(gcm, 128, '?', 144, "\x54\x6a\x7c\xa2\x04\xdc\x6e\x80\x1c\xcd\x5f\x7a\x7b\x08\x9e\x9d");
106+
107+
/* test short inputs (less that one block) */
108+
DO_TEST_P(ecb, 1, '.', 16, "\x6c\xd7\x66\x5b\x1b\x1e\x3a\x04\xfd\xb1\x91\x8d\x0e\xfd\xf1\x86");
109+
DO_TEST_P(ecb, 2, '?', 16, "\xdb\x84\x9e\xaf\x5f\xcc\xdb\x6b\xf2\x1c\xeb\x53\x75\xa3\x53\x5e");
110+
DO_TEST_P(cbc, 3, '%', 16, "\x60\x8e\x45\x9a\x07\x39\x63\xce\x02\x19\xdd\x52\xe3\x09\x2a\x66");
111+
DO_TEST_P(cbc, 4, '@', 16, "\x90\xc2\x6b\xf8\x84\x79\x83\xbd\xc1\x60\x71\x04\x55\x6a\xce\x9e");
112+
DO_TEST_N(ecb, 5, '.', 5, "\x6b\x60\xdc\xa4\x24\x9b\x02\xbb\x24\x41\x9b\xb0\xd1\x01\xcd\xba");
113+
DO_TEST_N(ecb, 6, '?', 6, "\x35\x8f\xb7\x9d\xd9\x61\x21\xcf\x25\x66\xd5\x9e\x91\xc1\x42\x7e");
114+
DO_TEST_N(cbc, 7, '%', 7, "\x94\x5e\x80\x71\x41\x7a\x64\x5d\x6f\x2e\x5b\x66\x9b\x5a\x3d\xda");
115+
DO_TEST_N(cbc, 8, '@', 8, "\xb8\x53\x97\xb9\x40\xa6\x98\xaf\x0c\x7b\x9a\xac\xad\x7e\x3c\xe0");
116+
DO_TEST_X(gcm, 9, '?', 25, "\x5e\x05\xfd\xb2\x8e\x17\x04\x1e\xff\x6d\x71\x81\xcd\x85\x8d\xb5");
117+
118+
my_end(0);
119+
return exit_status();
120+
}

0 commit comments

Comments
 (0)