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