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