1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2013 Saso Kiselkov. All rights reserved. 25 */ 26 27 /* 28 * This is just to keep the compiler happy about sys/time.h not declaring 29 * gettimeofday due to -D_KERNEL (we can do this since we're actually 30 * running in userspace, but we need -D_KERNEL for the remaining Edon-R code). 31 */ 32 33 #include <sys/edonr.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <stdio.h> 37 #include <sys/time.h> 38 #include <sys/stdtypes.h> 39 40 /* 41 * Test messages from: 42 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf 43 */ 44 static const char *test_msg0 = "abc"; 45 static const char *test_msg1 = "abcdefghbcdefghicdefghijdefghijkefghijklfgh" 46 "ijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; 47 48 static const uint8_t edonr_512_test_digests[][64] = { 49 { 50 /* for test_msg0 */ 51 0x1b, 0x14, 0xdb, 0x15, 0x5f, 0x1d, 0x40, 0x65, 52 0x94, 0xb8, 0xce, 0xf7, 0x0a, 0x43, 0x62, 0xec, 53 0x6b, 0x5d, 0xe6, 0xa5, 0xda, 0xf5, 0x0e, 0xc9, 54 0x99, 0xe9, 0x87, 0xc1, 0x9d, 0x30, 0x49, 0xe2, 55 0xde, 0x59, 0x77, 0xbb, 0x05, 0xb1, 0xbb, 0x22, 56 0x00, 0x50, 0xa1, 0xea, 0x5b, 0x46, 0xa9, 0xf1, 57 0x74, 0x0a, 0xca, 0xfb, 0xf6, 0xb4, 0x50, 0x32, 58 0xad, 0xc9, 0x0c, 0x62, 0x83, 0x72, 0xc2, 0x2b 59 }, 60 { 61 /* no test vector for test_msg1 */ 62 0 63 }, 64 { 65 /* for test_msg1 */ 66 0x53, 0x51, 0x07, 0x0d, 0xc5, 0x1c, 0x3b, 0x2b, 67 0xac, 0xa5, 0xa6, 0x0d, 0x02, 0x52, 0xcc, 0xb4, 68 0xe4, 0x92, 0x1a, 0x96, 0xfe, 0x5a, 0x69, 0xe7, 69 0x6d, 0xad, 0x48, 0xfd, 0x21, 0xa0, 0x84, 0x5a, 70 0xd5, 0x7f, 0x88, 0x0b, 0x3e, 0x4a, 0x90, 0x7b, 71 0xc5, 0x03, 0x15, 0x18, 0x42, 0xbb, 0x94, 0x9e, 72 0x1c, 0xba, 0x74, 0x39, 0xa6, 0x40, 0x9a, 0x34, 73 0xb8, 0x43, 0x6c, 0xb4, 0x69, 0x21, 0x58, 0x3c 74 } 75 }; 76 77 int 78 main(int argc, char *argv[]) 79 { 80 boolean_t failed = B_FALSE; 81 uint64_t cpu_mhz = 0; 82 83 if (argc == 2) 84 cpu_mhz = atoi(argv[1]); 85 86 #define EDONR_ALGO_TEST(_m, mode, testdigest) \ 87 do { \ 88 EdonRState ctx; \ 89 uint8_t digest[mode / 8]; \ 90 EdonRInit(&ctx); \ 91 EdonRUpdate(&ctx, (const uint8_t *) _m, strlen(_m) * 8);\ 92 EdonRFinal(&ctx, digest); \ 93 (void) printf("Edon-R-%-6sMessage: " #_m \ 94 "\tResult: ", #mode); \ 95 if (memcmp(digest, testdigest, mode / 8) == 0) { \ 96 (void) printf("OK\n"); \ 97 } else { \ 98 (void) printf("FAILED!\n"); \ 99 failed = B_TRUE; \ 100 } \ 101 } while (0) 102 103 #define EDONR_PERF_TEST(mode) \ 104 do { \ 105 EdonRState ctx; \ 106 uint8_t digest[mode / 8]; \ 107 uint8_t block[131072]; \ 108 uint64_t delta; \ 109 double cpb = 0; \ 110 int i; \ 111 struct timeval start, end; \ 112 memset(block, 0, sizeof (block)); \ 113 (void) gettimeofday(&start, NULL); \ 114 EdonRInit(&ctx); \ 115 for (i = 0; i < 8192; i++) \ 116 EdonRUpdate(&ctx, block, sizeof (block) * 8); \ 117 EdonRFinal(&ctx, digest); \ 118 (void) gettimeofday(&end, NULL); \ 119 delta = (end.tv_sec * 1000000llu + end.tv_usec) - \ 120 (start.tv_sec * 1000000llu + start.tv_usec); \ 121 if (cpu_mhz != 0) { \ 122 cpb = (cpu_mhz * 1e6 * ((double)delta / \ 123 1000000)) / (8192 * 128 * 1024); \ 124 } \ 125 (void) printf("Edon-R-%-6s%llu us (%.02f CPB)\n", #mode,\ 126 (u_longlong_t)delta, cpb); \ 127 } while (0) 128 129 (void) printf("Running algorithm correctness tests:\n"); 130 EDONR_ALGO_TEST(test_msg0, 512, edonr_512_test_digests[0]); 131 EDONR_ALGO_TEST(test_msg1, 512, edonr_512_test_digests[2]); 132 if (failed) 133 return (1); 134 135 (void) printf("Running performance tests (hashing 1024 MiB of " 136 "data):\n"); 137 EDONR_PERF_TEST(512); 138 139 return (0); 140 } 141