test-kdf-sha256.c (2402B)
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 kdf_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 *kdf; 42 }; 43 44 static void kdf_test(const void *data) 45 { 46 const struct kdf_data *test = data; 47 unsigned int kdf_len; 48 unsigned char output[512]; 49 char kdf[128]; 50 unsigned int i; 51 bool result; 52 53 kdf_len = strlen(test->kdf) / 2; 54 55 printf("PRF = %s (%d octets)\n", test->kdf, kdf_len); 56 57 result = kdf_sha256(test->key, test->key_len, test->prefix, 58 test->prefix_len, test->data, test->data_len, 59 output, kdf_len); 60 61 assert(result == true); 62 63 for (i = 0; i < kdf_len; i++) 64 sprintf(kdf + (i * 2), "%02x", output[i]); 65 66 printf("Result = %s\n", kdf); 67 68 assert(strcmp(test->kdf, kdf) == 0); 69 } 70 71 static const struct kdf_data test_case_1 = { 72 .key = "abc", 73 .key_len = 3, 74 .prefix = "KDF test", 75 .prefix_len = 8, 76 .data = "data", 77 .data_len = 4, 78 .kdf = "9efd6eb02758cb73" 79 "70a86f8a305375d4" 80 "1f8f21c2e47447f5" 81 "84f7d2291143d4d4", 82 }; 83 84 static bool test_precheck(const void *data) 85 { 86 return l_checksum_is_supported(L_CHECKSUM_SHA256, 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("/kdf-sha256/Test case 1", kdf_test, &test_case_1); 97 98 return l_test_run(); 99 }