Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/checksum/sha2_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 SHA2 code).30*/3132#include <stdarg.h>33#include <stdlib.h>34#include <string.h>35#include <stdio.h>3637#include <sys/time.h>38#include <sys/sha2.h>39#include <sys/stdtypes.h>40#include <sys/zfs_impl.h>4142/*43* Test messages from:44* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf45*/4647static const char *test_msg0 = "abc";48static const char *test_msg1 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklm"49"nlmnomnopnopq";50static const char *test_msg2 = "abcdefghbcdefghicdefghijdefghijkefghijklfgh"51"ijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";5253/*54* Test digests from:55* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf56*/57static const uint8_t sha256_test_digests[][32] = {58{59/* for test_msg0 */600xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,610x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,620xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,630xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD64},65{66/* for test_msg1 */670x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,680xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,690xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,700xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC171}72/* no test vector for test_msg2 */73};7475static const uint8_t sha512_test_digests[][64] = {76{77/* for test_msg0 */780xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA,790xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31,800x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2,810x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A,820x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8,830x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD,840x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E,850x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F86},87{88/* no test vector for test_msg1 */89090},91{92/* for test_msg2 */930x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA,940x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F,950x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1,960x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18,970x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4,980x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A,990xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54,1000x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09101}102};103104static const uint8_t sha512_256_test_digests[][32] = {105{106/* for test_msg0 */1070x53, 0x04, 0x8E, 0x26, 0x81, 0x94, 0x1E, 0xF9,1080x9B, 0x2E, 0x29, 0xB7, 0x6B, 0x4C, 0x7D, 0xAB,1090xE4, 0xC2, 0xD0, 0xC6, 0x34, 0xFC, 0x6D, 0x46,1100xE0, 0xE2, 0xF1, 0x31, 0x07, 0xE7, 0xAF, 0x23111},112{113/* no test vector for test_msg1 */1140115},116{117/* for test_msg2 */1180x39, 0x28, 0xE1, 0x84, 0xFB, 0x86, 0x90, 0xF8,1190x40, 0xDA, 0x39, 0x88, 0x12, 0x1D, 0x31, 0xBE,1200x65, 0xCB, 0x9D, 0x3E, 0xF8, 0x3E, 0xE6, 0x14,1210x6F, 0xEA, 0xC8, 0x61, 0xE1, 0x9B, 0x56, 0x3A122}123};124125int126main(int argc, char *argv[])127{128boolean_t failed = B_FALSE;129uint64_t cpu_mhz = 0;130131const zfs_impl_t *sha256 = zfs_impl_get_ops("sha256");132const zfs_impl_t *sha512 = zfs_impl_get_ops("sha512");133uint32_t id;134135if (argc == 2)136cpu_mhz = atoi(argv[1]);137138if (!sha256)139return (1);140141if (!sha512)142return (1);143144#define SHA2_ALGO_TEST(_m, mode, diglen, testdigest) \145do { \146SHA2_CTX ctx; \147uint8_t digest[diglen / 8]; \148SHA2Init(mode, &ctx); \149SHA2Update(&ctx, _m, strlen(_m)); \150SHA2Final(digest, &ctx); \151(void) printf("SHA%-9sMessage: " #_m \152"\tResult: ", #mode); \153if (memcmp(digest, testdigest, diglen / 8) == 0) { \154(void) printf("OK\n"); \155} else { \156(void) printf("FAILED!\n"); \157failed = B_TRUE; \158} \159} while (0)160161#define SHA2_PERF_TEST(mode, diglen, name) \162do { \163SHA2_CTX ctx; \164uint8_t digest[diglen / 8]; \165uint8_t block[131072]; \166uint64_t delta; \167double cpb = 0; \168int i; \169struct timeval start, end; \170memset(block, 0, sizeof (block)); \171(void) gettimeofday(&start, NULL); \172SHA2Init(mode, &ctx); \173for (i = 0; i < 8192; i++) \174SHA2Update(&ctx, block, sizeof (block)); \175SHA2Final(digest, &ctx); \176(void) gettimeofday(&end, NULL); \177delta = (end.tv_sec * 1000000llu + end.tv_usec) - \178(start.tv_sec * 1000000llu + start.tv_usec); \179if (cpu_mhz != 0) { \180cpb = (cpu_mhz * 1e6 * ((double)delta / \1811000000)) / (8192 * 128 * 1024); \182} \183(void) printf("sha%s-%-9s%7llu us (%.02f CPB)\n", #mode,\184name, (u_longlong_t)delta, cpb); \185} while (0)186187(void) printf("Running algorithm correctness tests:\n");188SHA2_ALGO_TEST(test_msg0, SHA256, 256, sha256_test_digests[0]);189SHA2_ALGO_TEST(test_msg1, SHA256, 256, sha256_test_digests[1]);190SHA2_ALGO_TEST(test_msg0, SHA512, 512, sha512_test_digests[0]);191SHA2_ALGO_TEST(test_msg2, SHA512, 512, sha512_test_digests[2]);192SHA2_ALGO_TEST(test_msg0, SHA512_256, 256, sha512_256_test_digests[0]);193SHA2_ALGO_TEST(test_msg2, SHA512_256, 256, sha512_256_test_digests[2]);194195if (failed)196return (1);197198(void) printf("Running performance tests (hashing 1024 MiB of "199"data):\n");200201for (id = 0; id < sha256->getcnt(); id++) {202sha256->setid(id);203const char *name = sha256->getname();204SHA2_PERF_TEST(SHA256, 256, name);205}206207for (id = 0; id < sha512->getcnt(); id++) {208sha512->setid(id);209const char *name = sha512->getname();210SHA2_PERF_TEST(SHA512, 512, name);211}212213return (0);214}215216217