| 1 |
/* HEADER testing SCAPI hashing functions */ |
| 2 |
#include <net-snmp/net-snmp-config.h> |
| 3 |
#include <net-snmp/net-snmp-includes.h> |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
#ifdef NETSNMP_USE_OPENSSL |
| 8 |
#include <openssl/hmac.h> |
| 9 |
#include <openssl/evp.h> |
| 10 |
#include <openssl/rand.h> |
| 11 |
#include <openssl/des.h> |
| 12 |
#ifdef HAVE_AES |
| 13 |
#include <openssl/aes.h> |
| 14 |
#endif |
| 15 |
#endif /* HAVE_OPENSSL */ |
| 16 |
|
| 17 |
#ifdef EVP_MAX_MD_SIZE |
| 18 |
#define MAX_HASH_LEN EVP_MAX_MD_SIZE |
| 19 |
#else |
| 20 |
#define MAX_HASH_LEN BYTESIZE(SNMP_TRANS_AUTHLEN_HMAC384SHA512) |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
static void print_hash(const char *label, u_char *MAC, size_t MAC_LEN); |
| 25 |
|
| 26 |
int |
| 27 |
main(int argc, char **argv) { |
| 28 |
u_char buf[] = "wes hardaker"; |
| 29 |
u_char MAC[MAX_HASH_LEN]; |
| 30 |
size_t MAC_LEN = sizeof(MAC); |
| 31 |
u_char sha1key[20] __attribute__((nonstring)) = "55555555555555555555"; |
| 32 |
u_char md5key[16] __attribute__((nonstring)) = "5555555555555555"; |
| 33 |
|
| 34 |
u_char sha1proper[20] = { 0x4a, 0x55, 0x2f, 0x65, 0x79, 0x3a, 0x49, 0x35, |
| 35 |
0x37, 0x91, 0x51, 0x1d, |
| 36 |
0xa0, 0x8c, 0x7a, 0x45, 0x50, 0x34, 0xd4, 0x23}; |
| 37 |
u_char md5proper[16] = { 0xe5, 0x92, 0xfa, 0x4b, 0x06, 0xe6, 0x27, 0xd7, |
| 38 |
0xc8, 0x18, 0xfa, 0x7a, |
| 39 |
0xd0, 0x82, 0xeb, 0x66}; |
| 40 |
int result; |
| 41 |
|
| 42 |
printf("1..2\n"); |
| 43 |
|
| 44 |
memset(MAC, 0, MAC_LEN); |
| 45 |
sc_hash(usmHMACSHA1AuthProtocol, OID_LENGTH(usmHMACSHA1AuthProtocol), |
| 46 |
buf, sizeof(buf)-1, |
| 47 |
MAC, &MAC_LEN); |
| 48 |
|
| 49 |
print_hash("sha1 hash", MAC, MAC_LEN); |
| 50 |
|
| 51 |
memset(MAC, 0, MAC_LEN); |
| 52 |
sc_hash(usmHMACMD5AuthProtocol, OID_LENGTH(usmHMACMD5AuthProtocol), |
| 53 |
buf, sizeof(buf)-1, |
| 54 |
MAC, &MAC_LEN); |
| 55 |
|
| 56 |
print_hash("md5 hash", MAC, MAC_LEN); |
| 57 |
|
| 58 |
MAC_LEN = 20; |
| 59 |
sc_generate_keyed_hash(usmHMACSHA1AuthProtocol, |
| 60 |
OID_LENGTH(usmHMACSHA1AuthProtocol), |
| 61 |
sha1key, sizeof(sha1key), |
| 62 |
buf, sizeof(buf)-1, |
| 63 |
MAC, &MAC_LEN); |
| 64 |
|
| 65 |
print_hash("sha1 keyed", MAC, MAC_LEN); |
| 66 |
|
| 67 |
result = |
| 68 |
sc_check_keyed_hash(usmHMACSHA1AuthProtocol, |
| 69 |
OID_LENGTH(usmHMACSHA1AuthProtocol), |
| 70 |
sha1key, sizeof(sha1key), |
| 71 |
buf, sizeof(buf)-1, |
| 72 |
sha1proper, 12); |
| 73 |
if (0 == result) { |
| 74 |
printf("ok: 1 - sha1 keyed compare was equal\n"); |
| 75 |
} else { |
| 76 |
printf("not ok: 1 - sha1 keyed compare was not equal\n"); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
MAC_LEN = 16; |
| 81 |
sc_generate_keyed_hash(usmHMACMD5AuthProtocol, |
| 82 |
OID_LENGTH(usmHMACMD5AuthProtocol), |
| 83 |
md5key, sizeof(md5key), |
| 84 |
buf, sizeof(buf)-1, |
| 85 |
MAC, &MAC_LEN); |
| 86 |
|
| 87 |
print_hash("md5 keyed", MAC, MAC_LEN); |
| 88 |
|
| 89 |
|
| 90 |
MAC_LEN = 16; |
| 91 |
result = |
| 92 |
sc_check_keyed_hash(usmHMACMD5AuthProtocol, |
| 93 |
OID_LENGTH(usmHMACMD5AuthProtocol), |
| 94 |
md5key, sizeof(md5key), |
| 95 |
buf, sizeof(buf)-1, |
| 96 |
md5proper, 12); |
| 97 |
|
| 98 |
if (0 == result) { |
| 99 |
printf("ok: 2 - md5 keyed compare was equal\n"); |
| 100 |
} else { |
| 101 |
printf("not ok: 2 - md5 keyed compare was not equal\n"); |
| 102 |
} |
| 103 |
|
| 104 |
MAC_LEN = sizeof(MAC); |
| 105 |
memset(MAC, 0, MAC_LEN); |
| 106 |
generate_Ku(usmHMACSHA1AuthProtocol, |
| 107 |
OID_LENGTH(usmHMACSHA1AuthProtocol), |
| 108 |
buf, sizeof(buf)-1, |
| 109 |
MAC, &MAC_LEN); |
| 110 |
print_hash("sha1 Ku", MAC, MAC_LEN); |
| 111 |
|
| 112 |
MAC_LEN = sizeof(MAC); |
| 113 |
memset(MAC, 0, MAC_LEN); |
| 114 |
generate_Ku(usmHMACMD5AuthProtocol, |
| 115 |
OID_LENGTH(usmHMACMD5AuthProtocol), |
| 116 |
buf, sizeof(buf)-1, |
| 117 |
MAC, &MAC_LEN); |
| 118 |
print_hash("md5 Ku", MAC, MAC_LEN); |
| 119 |
|
| 120 |
/* XXX: todo: compare results and ensure they're always the same |
| 121 |
values; the algorithms aren't time-dependent. */ |
| 122 |
return (0); |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
print_hash(const char *label, u_char *MAC, size_t MAC_LEN) { |
| 127 |
int i; |
| 128 |
printf("# %-10s %" NETSNMP_PRIz "u:\n", label, MAC_LEN); |
| 129 |
for(i=0; i < MAC_LEN; i++) { |
| 130 |
printf("# %02x ", MAC[i]); |
| 131 |
} |
| 132 |
printf("\n"); |
| 133 |
} |