test-hmac-md5.c (2486B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <assert.h> 30 #include <ell/ell.h> 31 32 #include "src/crypto.h" 33 34 struct hmac_data { 35 const char *key; 36 unsigned int key_len; 37 const char *data; 38 unsigned int data_len; 39 const char *hmac; 40 }; 41 42 static void hmac_test(const void *data) 43 { 44 const struct hmac_data *test = data; 45 unsigned int hmac_len; 46 unsigned char output[512]; 47 char hmac[128]; 48 unsigned int i; 49 bool result; 50 51 hmac_len = strlen(test->hmac) / 2; 52 53 printf("HMAC = %s (%d octets)\n", test->hmac, hmac_len); 54 55 result = hmac_md5(test->key, test->key_len, 56 test->data, test->data_len, output, hmac_len); 57 58 assert(result == true); 59 60 for (i = 0; i < hmac_len; i++) 61 sprintf(hmac + (i * 2), "%02x", output[i]); 62 63 printf("Result = %s\n", hmac); 64 65 assert(strcmp(test->hmac, hmac) == 0); 66 } 67 68 static const struct hmac_data test_case_1 = { 69 .key = "", 70 .key_len = 0, 71 .data = "", 72 .data_len = 0, 73 .hmac = "74e6f7298a9c2d168935f58c001bad88", 74 }; 75 76 static const struct hmac_data test_case_2 = { 77 .key = "key", 78 .key_len = 3, 79 .data = "The quick brown fox jumps over the lazy dog", 80 .data_len = 43, 81 .hmac = "80070713463e7749b90c2dc24911e275", 82 }; 83 84 static bool test_precheck(const void *data) 85 { 86 return l_checksum_is_supported(L_CHECKSUM_MD5, true); 87 } 88 89 #define add_test(name, func, data) l_test_add_data_func_precheck(name, data, \ 90 func, test_precheck, 0) 91 92 int main(int argc, char *argv[]) 93 { 94 l_test_init(&argc, &argv); 95 96 add_test("/hmac-md5/Test case 1", hmac_test, &test_case_1); 97 add_test("/hmac-md5/Test case 2", hmac_test, &test_case_2); 98 99 return l_test_run(); 100 }