Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/checksum/edonr_test.c
48775 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2013 Saso Kiselkov. All rights reserved.24*/2526/*27* This is just to keep the compiler happy about sys/time.h not declaring28* gettimeofday due to -D_KERNEL (we can do this since we're actually29* running in userspace, but we need -D_KERNEL for the remaining Edon-R code).30*/3132#include <sys/edonr.h>33#include <stdlib.h>34#include <string.h>35#include <stdio.h>36#include <sys/time.h>37#include <sys/stdtypes.h>3839/*40* Test messages from:41* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf42*/43static const char *test_msg0 = "abc";44static const char *test_msg1 = "abcdefghbcdefghicdefghijdefghijkefghijklfgh"45"ijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";4647static const uint8_t edonr_512_test_digests[][64] = {48{49/* for test_msg0 */500x1b, 0x14, 0xdb, 0x15, 0x5f, 0x1d, 0x40, 0x65,510x94, 0xb8, 0xce, 0xf7, 0x0a, 0x43, 0x62, 0xec,520x6b, 0x5d, 0xe6, 0xa5, 0xda, 0xf5, 0x0e, 0xc9,530x99, 0xe9, 0x87, 0xc1, 0x9d, 0x30, 0x49, 0xe2,540xde, 0x59, 0x77, 0xbb, 0x05, 0xb1, 0xbb, 0x22,550x00, 0x50, 0xa1, 0xea, 0x5b, 0x46, 0xa9, 0xf1,560x74, 0x0a, 0xca, 0xfb, 0xf6, 0xb4, 0x50, 0x32,570xad, 0xc9, 0x0c, 0x62, 0x83, 0x72, 0xc2, 0x2b58},59{60/* no test vector for test_msg1 */61062},63{64/* for test_msg1 */650x53, 0x51, 0x07, 0x0d, 0xc5, 0x1c, 0x3b, 0x2b,660xac, 0xa5, 0xa6, 0x0d, 0x02, 0x52, 0xcc, 0xb4,670xe4, 0x92, 0x1a, 0x96, 0xfe, 0x5a, 0x69, 0xe7,680x6d, 0xad, 0x48, 0xfd, 0x21, 0xa0, 0x84, 0x5a,690xd5, 0x7f, 0x88, 0x0b, 0x3e, 0x4a, 0x90, 0x7b,700xc5, 0x03, 0x15, 0x18, 0x42, 0xbb, 0x94, 0x9e,710x1c, 0xba, 0x74, 0x39, 0xa6, 0x40, 0x9a, 0x34,720xb8, 0x43, 0x6c, 0xb4, 0x69, 0x21, 0x58, 0x3c73}74};7576int77main(int argc, char *argv[])78{79boolean_t failed = B_FALSE;80uint64_t cpu_mhz = 0;8182if (argc == 2)83cpu_mhz = atoi(argv[1]);8485#define EDONR_ALGO_TEST(_m, mode, testdigest) \86do { \87EdonRState ctx; \88uint8_t digest[mode / 8]; \89EdonRInit(&ctx); \90EdonRUpdate(&ctx, (const uint8_t *) _m, strlen(_m) * 8);\91EdonRFinal(&ctx, digest); \92(void) printf("Edon-R-%-6sMessage: " #_m \93"\tResult: ", #mode); \94if (memcmp(digest, testdigest, mode / 8) == 0) { \95(void) printf("OK\n"); \96} else { \97(void) printf("FAILED!\n"); \98failed = B_TRUE; \99} \100} while (0)101102#define EDONR_PERF_TEST(mode) \103do { \104EdonRState ctx; \105uint8_t digest[mode / 8]; \106uint8_t block[131072]; \107uint64_t delta; \108double cpb = 0; \109int i; \110struct timeval start, end; \111memset(block, 0, sizeof (block)); \112(void) gettimeofday(&start, NULL); \113EdonRInit(&ctx); \114for (i = 0; i < 8192; i++) \115EdonRUpdate(&ctx, block, sizeof (block) * 8); \116EdonRFinal(&ctx, digest); \117(void) gettimeofday(&end, NULL); \118delta = (end.tv_sec * 1000000llu + end.tv_usec) - \119(start.tv_sec * 1000000llu + start.tv_usec); \120if (cpu_mhz != 0) { \121cpb = (cpu_mhz * 1e6 * ((double)delta / \1221000000)) / (8192 * 128 * 1024); \123} \124(void) printf("Edon-R-%-6s%llu us (%.02f CPB)\n", #mode,\125(u_longlong_t)delta, cpb); \126} while (0)127128(void) printf("Running algorithm correctness tests:\n");129EDONR_ALGO_TEST(test_msg0, 512, edonr_512_test_digests[0]);130EDONR_ALGO_TEST(test_msg1, 512, edonr_512_test_digests[2]);131if (failed)132return (1);133134(void) printf("Running performance tests (hashing 1024 MiB of "135"data):\n");136EDONR_PERF_TEST(512);137138return (0);139}140141142