Home | History | Annotate | Line # | Download | only in tests
crypto.c revision 1.1.1.4
      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((char*)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 #ifdef OPENSSL
     46 	const char* PKT_DATA = "abcdefgh0123";
     47 	const int PKT_LEN = strlen(PKT_DATA);
     48 	const char* EXPECTED_DIGEST =
     49 		"\x17\xaa\x82\x97\xc7\x17\x13\x6a\x9b\xa9"
     50 		"\x63\x85\xb4\xce\xbe\x94\xa0\x97\x16\x1d";
     51 	char actual[SHA1_LENGTH];
     52 
     53 	struct key sha1;
     54 	sha1.next = NULL;
     55 	sha1.key_id = 20;
     56 	sha1.key_len = 7;
     57 	memcpy(&sha1.key_seq, "sha1seq", sha1.key_len);
     58 	memcpy(&sha1.type, "SHA1", 5);
     59 
     60 	TEST_ASSERT_EQUAL(SHA1_LENGTH,
     61 			  make_mac((char*)PKT_DATA, PKT_LEN, SHA1_LENGTH, &sha1, actual));
     62 
     63 	TEST_ASSERT_EQUAL_MEMORY(EXPECTED_DIGEST, actual, SHA1_LENGTH);
     64 #else
     65 	TEST_IGNORE_MESSAGE("OpenSSL not found, skipping...");
     66 #endif	/* OPENSSL */
     67 }
     68 
     69 
     70 void
     71 test_VerifyCorrectMD5(void) {
     72 	const char* PKT_DATA =
     73 		"sometestdata"		// Data
     74 		"\0\0\0\0"			// Key-ID (unused)
     75 		"\xc7\x58\x99\xdd\x99\x32\x0f\x71" // MAC
     76 		"\x2b\x7b\xfe\x4f\xa2\x32\xcf\xac";
     77 	const int PKT_LEN = 12;
     78 
     79 	struct key md5;
     80 	md5.next = NULL;
     81 	md5.key_id = 0;
     82 	md5.key_len = 6;
     83 	memcpy(&md5.key_seq, "md5key", md5.key_len);
     84 	memcpy(&md5.type, "MD5", 4);
     85 
     86 	TEST_ASSERT_TRUE(auth_md5((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5));
     87 }
     88 
     89 
     90 void
     91 test_VerifySHA1(void) {
     92 #ifdef OPENSSL
     93 	const char* PKT_DATA =
     94 		"sometestdata"		// Data
     95 		"\0\0\0\0"			// Key-ID (unused)
     96 		"\xad\x07\xde\x36\x39\xa6\x77\xfa\x5b\xce" // MAC
     97 		"\x2d\x8a\x7d\x06\x96\xe6\x0c\xbc\xed\xe1";
     98 	const int PKT_LEN = 12;
     99 
    100 	struct key sha1;
    101 	sha1.next = NULL;
    102 	sha1.key_id = 0;
    103 	sha1.key_len = 7;
    104 	memcpy(&sha1.key_seq, "sha1key", sha1.key_len);
    105 	memcpy(&sha1.type, "SHA1", 5);
    106 
    107 	TEST_ASSERT_TRUE(auth_md5((char*)PKT_DATA, PKT_LEN, SHA1_LENGTH, &sha1));
    108 #else
    109 	TEST_IGNORE_MESSAGE("OpenSSL not found, skipping...");
    110 #endif	/* OPENSSL */
    111 }
    112 
    113 void
    114 test_VerifyFailure(void) {
    115 	/* We use a copy of the MD5 verification code, but modify
    116 	 * the last bit to make sure verification fails. */
    117 	const char* PKT_DATA =
    118 		"sometestdata"		// Data
    119 		"\0\0\0\0"			// Key-ID (unused)
    120 		"\xc7\x58\x99\xdd\x99\x32\x0f\x71"	// MAC
    121 		"\x2b\x7b\xfe\x4f\xa2\x32\xcf\x00"; // Last byte is wrong!
    122 	const int PKT_LEN = 12;
    123 
    124 	struct key md5;
    125 	md5.next = NULL;
    126 	md5.key_id = 0;
    127 	md5.key_len = 6;
    128 	memcpy(&md5.key_seq, "md5key", md5.key_len);
    129 	memcpy(&md5.type, "MD5", 4);
    130 
    131 	TEST_ASSERT_FALSE(auth_md5((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5));
    132 }
    133 
    134 
    135 void
    136 test_PacketSizeNotMultipleOfFourBytes(void) {
    137 	const char* PKT_DATA = "123456";
    138 	const int PKT_LEN = 6;
    139 	char actual[MD5_LENGTH];
    140 
    141 	struct key md5;
    142 	md5.next = NULL;
    143 	md5.key_id = 10;
    144 	md5.key_len = 6;
    145 	memcpy(&md5.key_seq, "md5seq", md5.key_len);
    146 	memcpy(&md5.type, "MD5", 4);
    147 
    148 	TEST_ASSERT_EQUAL(0, make_mac((char*)PKT_DATA, PKT_LEN, MD5_LENGTH, &md5, actual));
    149 }
    150