crypto.c (285169) | crypto.c (289764) |
---|---|
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 | 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 |
11void test_MakeMd5Mac(void) { | |
12 | 11 |
12void test_MakeMd5Mac(void); 13void test_MakeSHA1Mac(void); 14void test_VerifyCorrectMD5(void); 15void test_VerifySHA1(void); 16void test_VerifyFailure(void); 17void test_PacketSizeNotMultipleOfFourBytes(void); 18 19 20void 21test_MakeMd5Mac(void) { 22 |
|
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; --- 4 unchanged lines hidden (view full) --- 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 | 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; --- 4 unchanged lines hidden (view full) --- 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 |
33void test_MakeSHA1Mac(void) { | 43void 44test_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 | 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 |
52 TEST_ASSERT_TRUE(memcmp(EXPECTED_DIGEST, actual, SHA1_LENGTH) == 0); | 63 TEST_ASSERT_EQUAL_MEMORY(EXPECTED_DIGEST, actual, SHA1_LENGTH); |
53#else 54 TEST_IGNORE_MESSAGE("OpenSSL not found, skipping..."); 55#endif /* OPENSSL */ 56} 57 58 | 64#else 65 TEST_IGNORE_MESSAGE("OpenSSL not found, skipping..."); 66#endif /* OPENSSL */ 67} 68 69 |
59void test_VerifyCorrectMD5(void) { | 70void 71test_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 | 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 |
78void test_VerifySHA1(void) { | 90void 91test_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 --- 5 unchanged lines hidden (view full) --- 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 | 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 --- 5 unchanged lines hidden (view full) --- 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 |
100void test_VerifyFailure(void) { | 113void 114test_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 | 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 |
120void test_PacketSizeNotMultipleOfFourBytes(void) { | 134 135void 136test_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} | 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} |
134 | |