test-prf-sha1.c (3648B)
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 prf_data { 35 const char *key; 36 unsigned int key_len; 37 const char *prefix; 38 unsigned int prefix_len; 39 const char *data; 40 unsigned int data_len; 41 const char *prf; 42 }; 43 44 static void prf_test(const void *data) 45 { 46 const struct prf_data *test = data; 47 unsigned int prf_len; 48 unsigned char output[512]; 49 char prf[1024]; 50 unsigned int i; 51 bool result; 52 53 prf_len = strlen(test->prf) / 2; 54 55 printf("PRF = %s (%d octets)\n", test->prf, prf_len); 56 57 result = prf_sha1(test->key, test->key_len, test->prefix, 58 test->prefix_len, test->data, test->data_len, 59 output, prf_len); 60 61 assert(result == true); 62 63 for (i = 0; i < prf_len; i++) 64 sprintf(prf + (i * 2), "%02x", output[i]); 65 66 printf("Result = %s\n", prf); 67 68 assert(strcmp(test->prf, prf) == 0); 69 } 70 71 static const struct prf_data test_case_1 = { 72 .key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b" 73 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 74 .key_len = 20, 75 .prefix = "prefix", 76 .prefix_len = 6, 77 .data = "Hi There", 78 .data_len = 8, 79 .prf = "bcd4c650b30b9684951829e0d75f9d54" 80 "b862175ed9f00606e17d8da35402ffee" 81 "75df78c3d31e0f889f012120c0862beb" 82 "67753e7439ae242edb8373698356cf5a", 83 }; 84 85 static const struct prf_data test_case_2 = { 86 .key = "Jefe", 87 .key_len = 4, 88 .prefix = "prefix", 89 .prefix_len = 6, 90 .data = "what do ya want for nothing?", 91 .data_len = 28, 92 .prf = "51f4de5b33f249adf81aeb713a3c20f4" 93 "fe631446fabdfa58244759ae58ef9009" 94 "a99abf4eac2ca5fa87e692c440eb4002" 95 "3e7babb206d61de7b92f41529092b8fc", 96 }; 97 98 static const struct prf_data test_case_3 = { 99 .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" 100 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 101 .key_len = 20, 102 .prefix = "prefix", 103 .prefix_len = 6, 104 .data = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" 105 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" 106 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" 107 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd" 108 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd", 109 .data_len = 50, 110 .prf = "e1ac546ec4cb636f9976487be5c86be1" 111 "7a0252ca5d8d8df12cfb0473525249ce" 112 "9dd8d177ead710bc9b590547239107ae" 113 "f7b4abd43d87f0a68f1cbd9e2b6f7607", 114 }; 115 116 static bool test_precheck(const void *data) 117 { 118 return l_checksum_is_supported(L_CHECKSUM_SHA1, true); 119 } 120 121 #define add_test(name, func, data) l_test_add_data_func_precheck(name, data, \ 122 func, test_precheck, 0) 123 124 int main(int argc, char *argv[]) 125 { 126 l_test_init(&argc, &argv); 127 128 add_test("/prf-sha1/Test case 1", prf_test, &test_case_1); 129 add_test("/prf-sha1/Test case 2", prf_test, &test_case_2); 130 add_test("/prf-sha1/Test case 3", prf_test, &test_case_3); 131 132 return l_test_run(); 133 }