crypto.c revision 1.1 1 1.1 christos #include "config.h"
2 1.1 christos #include "unity.h"
3 1.1 christos #include "ntp_types.h"
4 1.1 christos
5 1.1 christos #include "sntptest.h"
6 1.1 christos #include "crypto.h"
7 1.1 christos
8 1.1 christos #define MD5_LENGTH 16
9 1.1 christos #define SHA1_LENGTH 20
10 1.1 christos
11 1.1 christos void test_MakeMd5Mac(void) {
12 1.1 christos
13 1.1 christos const char* PKT_DATA = "abcdefgh0123";
14 1.1 christos const int PKT_LEN = strlen(PKT_DATA);
15 1.1 christos const char* EXPECTED_DIGEST =
16 1.1 christos "\x52\x6c\xb8\x38\xaf\x06\x5a\xfb\x6c\x98\xbb\xc0\x9b\x0a\x7a\x1b";
17 1.1 christos char actual[MD5_LENGTH];
18 1.1 christos
19 1.1 christos struct key md5;
20 1.1 christos md5.next = NULL;
21 1.1 christos md5.key_id = 10;
22 1.1 christos md5.key_len = 6;
23 1.1 christos memcpy(&md5.key_seq, "md5seq", md5.key_len);
24 1.1 christos memcpy(&md5.type, "MD5", 4);
25 1.1 christos
26 1.1 christos TEST_ASSERT_EQUAL(MD5_LENGTH,
27 1.1 christos make_mac((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5, actual));
28 1.1 christos
29 1.1 christos TEST_ASSERT_TRUE(memcmp(EXPECTED_DIGEST, actual, MD5_LENGTH) == 0);
30 1.1 christos }
31 1.1 christos
32 1.1 christos
33 1.1 christos void test_MakeSHA1Mac(void) {
34 1.1 christos #ifdef OPENSSL
35 1.1 christos const char* PKT_DATA = "abcdefgh0123";
36 1.1 christos const int PKT_LEN = strlen(PKT_DATA);
37 1.1 christos const char* EXPECTED_DIGEST =
38 1.1 christos "\x17\xaa\x82\x97\xc7\x17\x13\x6a\x9b\xa9"
39 1.1 christos "\x63\x85\xb4\xce\xbe\x94\xa0\x97\x16\x1d";
40 1.1 christos char actual[SHA1_LENGTH];
41 1.1 christos
42 1.1 christos struct key sha1;
43 1.1 christos sha1.next = NULL;
44 1.1 christos sha1.key_id = 20;
45 1.1 christos sha1.key_len = 7;
46 1.1 christos memcpy(&sha1.key_seq, "sha1seq", sha1.key_len);
47 1.1 christos memcpy(&sha1.type, "SHA1", 5);
48 1.1 christos
49 1.1 christos TEST_ASSERT_EQUAL(SHA1_LENGTH,
50 1.1 christos make_mac((char*)PKT_DATA, PKT_LEN, SHA1_LENGTH, &sha1, actual));
51 1.1 christos
52 1.1 christos TEST_ASSERT_TRUE(memcmp(EXPECTED_DIGEST, actual, SHA1_LENGTH) == 0);
53 1.1 christos #else
54 1.1 christos TEST_IGNORE_MESSAGE("OpenSSL not found, skipping...");
55 1.1 christos #endif /* OPENSSL */
56 1.1 christos }
57 1.1 christos
58 1.1 christos
59 1.1 christos void test_VerifyCorrectMD5(void) {
60 1.1 christos const char* PKT_DATA =
61 1.1 christos "sometestdata" // Data
62 1.1 christos "\0\0\0\0" // Key-ID (unused)
63 1.1 christos "\xc7\x58\x99\xdd\x99\x32\x0f\x71" // MAC
64 1.1 christos "\x2b\x7b\xfe\x4f\xa2\x32\xcf\xac";
65 1.1 christos const int PKT_LEN = 12;
66 1.1 christos
67 1.1 christos struct key md5;
68 1.1 christos md5.next = NULL;
69 1.1 christos md5.key_id = 0;
70 1.1 christos md5.key_len = 6;
71 1.1 christos memcpy(&md5.key_seq, "md5key", md5.key_len);
72 1.1 christos memcpy(&md5.type, "MD5", 4);
73 1.1 christos
74 1.1 christos TEST_ASSERT_TRUE(auth_md5((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5));
75 1.1 christos }
76 1.1 christos
77 1.1 christos
78 1.1 christos void test_VerifySHA1(void) {
79 1.1 christos #ifdef OPENSSL
80 1.1 christos const char* PKT_DATA =
81 1.1 christos "sometestdata" // Data
82 1.1 christos "\0\0\0\0" // Key-ID (unused)
83 1.1 christos "\xad\x07\xde\x36\x39\xa6\x77\xfa\x5b\xce" // MAC
84 1.1 christos "\x2d\x8a\x7d\x06\x96\xe6\x0c\xbc\xed\xe1";
85 1.1 christos const int PKT_LEN = 12;
86 1.1 christos
87 1.1 christos struct key sha1;
88 1.1 christos sha1.next = NULL;
89 1.1 christos sha1.key_id = 0;
90 1.1 christos sha1.key_len = 7;
91 1.1 christos memcpy(&sha1.key_seq, "sha1key", sha1.key_len);
92 1.1 christos memcpy(&sha1.type, "SHA1", 5);
93 1.1 christos
94 1.1 christos TEST_ASSERT_TRUE(auth_md5((char*)PKT_DATA, PKT_LEN, SHA1_LENGTH, &sha1));
95 1.1 christos #else
96 1.1 christos TEST_IGNORE_MESSAGE("OpenSSL not found, skipping...");
97 1.1 christos #endif /* OPENSSL */
98 1.1 christos }
99 1.1 christos
100 1.1 christos void test_VerifyFailure(void) {
101 1.1 christos /* We use a copy of the MD5 verification code, but modify
102 1.1 christos * the last bit to make sure verification fails. */
103 1.1 christos const char* PKT_DATA =
104 1.1 christos "sometestdata" // Data
105 1.1 christos "\0\0\0\0" // Key-ID (unused)
106 1.1 christos "\xc7\x58\x99\xdd\x99\x32\x0f\x71" // MAC
107 1.1 christos "\x2b\x7b\xfe\x4f\xa2\x32\xcf\x00"; // Last byte is wrong!
108 1.1 christos const int PKT_LEN = 12;
109 1.1 christos
110 1.1 christos struct key md5;
111 1.1 christos md5.next = NULL;
112 1.1 christos md5.key_id = 0;
113 1.1 christos md5.key_len = 6;
114 1.1 christos memcpy(&md5.key_seq, "md5key", md5.key_len);
115 1.1 christos memcpy(&md5.type, "MD5", 4);
116 1.1 christos
117 1.1 christos TEST_ASSERT_FALSE(auth_md5((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5));
118 1.1 christos }
119 1.1 christos
120 1.1 christos void test_PacketSizeNotMultipleOfFourBytes(void) {
121 1.1 christos const char* PKT_DATA = "123456";
122 1.1 christos const int PKT_LEN = 6;
123 1.1 christos char actual[MD5_LENGTH];
124 1.1 christos
125 1.1 christos struct key md5;
126 1.1 christos md5.next = NULL;
127 1.1 christos md5.key_id = 10;
128 1.1 christos md5.key_len = 6;
129 1.1 christos memcpy(&md5.key_seq, "md5seq", md5.key_len);
130 1.1 christos memcpy(&md5.type, "MD5", 4);
131 1.1 christos
132 1.1 christos TEST_ASSERT_EQUAL(0, make_mac((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5, actual));
133 1.1 christos }
134 1.1 christos
135