Home | History | Annotate | Line # | Download | only in libcrypto
      1  1.2  riastrad /*	$NetBSD: t_sha512trunc.c,v 1.2 2024/03/15 18:10:37 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/cdefs.h>
     30  1.2  riastrad __RCSID("$NetBSD: t_sha512trunc.c,v 1.2 2024/03/15 18:10:37 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include <stddef.h>
     33  1.1  riastrad 
     34  1.1  riastrad #include <atf-c.h>
     35  1.1  riastrad 
     36  1.1  riastrad #include <openssl/evp.h>
     37  1.1  riastrad 
     38  1.1  riastrad #include "h_macros.h"
     39  1.1  riastrad 
     40  1.1  riastrad struct testcase {
     41  1.1  riastrad 	const unsigned char in[128];
     42  1.1  riastrad 	size_t inlen;
     43  1.1  riastrad 	const unsigned char out[32];
     44  1.1  riastrad };
     45  1.1  riastrad 
     46  1.1  riastrad static void
     47  1.1  riastrad check(const struct testcase *C, size_t n, size_t digestlen, const EVP_MD *md)
     48  1.1  riastrad {
     49  1.1  riastrad 	enum { C0 = 0xc0, C1 = 0xc1 };
     50  1.1  riastrad 	unsigned char *buf, *digest, *p0, *p1;
     51  1.1  riastrad 	size_t i;
     52  1.1  riastrad 
     53  1.1  riastrad 	ATF_REQUIRE_MSG(digestlen <= INT_MAX, "digestlen=%zu", digestlen);
     54  1.1  riastrad 	ATF_REQUIRE_EQ_MSG((int)digestlen, EVP_MD_size(md),
     55  1.1  riastrad 	    "expected %d, got %d", (int)digestlen, EVP_MD_size(md));
     56  1.1  riastrad 
     57  1.1  riastrad 	ATF_REQUIRE_MSG(digestlen < SIZE_MAX - 2048,
     58  1.1  riastrad 	    "digestlen=%zu", digestlen);
     59  1.1  riastrad 	REQUIRE_LIBC(buf = malloc(digestlen + 2048), NULL);
     60  1.1  riastrad 	p0 = buf;
     61  1.1  riastrad 	digest = buf + 1;
     62  1.1  riastrad 	p1 = buf + 1 + digestlen;
     63  1.1  riastrad 
     64  1.1  riastrad 	for (i = 0; i < n; i++) {
     65  1.1  riastrad 		EVP_MD_CTX *ctx;
     66  1.1  riastrad 		unsigned digestlen1;
     67  1.1  riastrad 
     68  1.1  riastrad 		*p0 = C0;
     69  1.1  riastrad 		*p1 = C1;
     70  1.1  riastrad 
     71  1.1  riastrad #define	REQUIRE(x)	ATF_REQUIRE_MSG((x), "i=%zu", i)
     72  1.1  riastrad 		REQUIRE(ctx = EVP_MD_CTX_new());
     73  1.1  riastrad 		REQUIRE(EVP_DigestInit_ex(ctx, md, NULL));
     74  1.1  riastrad 		REQUIRE(EVP_DigestUpdate(ctx, C->in, C->inlen));
     75  1.1  riastrad 		REQUIRE(EVP_DigestFinal_ex(ctx, digest, &digestlen1));
     76  1.1  riastrad #undef	REQUIRE
     77  1.1  riastrad 		ATF_CHECK_MSG(digestlen == digestlen1,
     78  1.1  riastrad 		    "i=%zu: expected %zu got %u", i, digestlen, digestlen1);
     79  1.1  riastrad 		EVP_MD_CTX_free(ctx);
     80  1.1  riastrad 
     81  1.1  riastrad 		ATF_CHECK_MSG(memcmp(digest, C->out, digestlen) == 0,
     82  1.1  riastrad 		    "i=%zu", i);
     83  1.1  riastrad 
     84  1.1  riastrad 		ATF_CHECK_EQ_MSG(*p0, C0, "expected 0x%x got 0x%hhx", C0, *p0);
     85  1.1  riastrad 		ATF_CHECK_EQ_MSG(*p1, C1, "expected 0x%x got 0x%hhx", C1, *p1);
     86  1.1  riastrad 	}
     87  1.1  riastrad }
     88  1.1  riastrad 
     89  1.1  riastrad /*
     90  1.1  riastrad  * Test vectors from:
     91  1.1  riastrad  *
     92  1.1  riastrad  * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#Testing
     93  1.1  riastrad  */
     94  1.1  riastrad 
     95  1.1  riastrad ATF_TC(sha512_224);
     96  1.1  riastrad ATF_TC_HEAD(sha512_224, tc)
     97  1.1  riastrad {
     98  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "Test SHA512-224");
     99  1.1  riastrad }
    100  1.1  riastrad ATF_TC_BODY(sha512_224, tc)
    101  1.1  riastrad {
    102  1.1  riastrad 	static const struct testcase C[] = {
    103  1.1  riastrad 		[0] = {
    104  1.1  riastrad 			.inlen = 0,
    105  1.1  riastrad 			.out = {
    106  1.1  riastrad 				0x6e,0xd0,0xdd,0x02, 0x80,0x6f,0xa8,0x9e,
    107  1.1  riastrad 				0x25,0xde,0x06,0x0c, 0x19,0xd3,0xac,0x86,
    108  1.1  riastrad 				0xca,0xbb,0x87,0xd6, 0xa0,0xdd,0xd0,0x5c,
    109  1.1  riastrad 				0x33,0x3b,0x84,0xf4,
    110  1.1  riastrad 			},
    111  1.1  riastrad 		},
    112  1.1  riastrad 		[1] = {
    113  1.1  riastrad 			.inlen = 1,
    114  1.1  riastrad 			.in = {
    115  1.1  riastrad 				0xcf,
    116  1.1  riastrad 			},
    117  1.1  riastrad 			.out = {
    118  1.1  riastrad 				0x41,0x99,0x23,0x9e, 0x87,0xd4,0x7b,0x6f,
    119  1.1  riastrad 				0xed,0xa0,0x16,0x80, 0x2b,0xf3,0x67,0xfb,
    120  1.1  riastrad 				0x6e,0x8b,0x56,0x55, 0xef,0xf6,0x22,0x5c,
    121  1.1  riastrad 				0xb2,0x66,0x8f,0x4a,
    122  1.1  riastrad 			},
    123  1.1  riastrad 		},
    124  1.1  riastrad 	};
    125  1.1  riastrad 
    126  1.1  riastrad 	check(C, __arraycount(C), 28, EVP_sha512_224());
    127  1.1  riastrad }
    128  1.1  riastrad 
    129  1.1  riastrad ATF_TC(sha512_256);
    130  1.1  riastrad ATF_TC_HEAD(sha512_256, tc)
    131  1.1  riastrad {
    132  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "Test SHA512-256");
    133  1.1  riastrad }
    134  1.1  riastrad ATF_TC_BODY(sha512_256, tc)
    135  1.1  riastrad {
    136  1.1  riastrad 	static const struct testcase C[] = {
    137  1.1  riastrad 		[0] = {
    138  1.1  riastrad 			.inlen = 0,
    139  1.1  riastrad 			.out = {
    140  1.1  riastrad 				0xc6,0x72,0xb8,0xd1, 0xef,0x56,0xed,0x28,
    141  1.1  riastrad 				0xab,0x87,0xc3,0x62, 0x2c,0x51,0x14,0x06,
    142  1.1  riastrad 				0x9b,0xdd,0x3a,0xd7, 0xb8,0xf9,0x73,0x74,
    143  1.1  riastrad 				0x98,0xd0,0xc0,0x1e, 0xce,0xf0,0x96,0x7a,
    144  1.1  riastrad 			},
    145  1.1  riastrad 		},
    146  1.1  riastrad 		[1] = {
    147  1.1  riastrad 			.inlen = 1,
    148  1.1  riastrad 			.in = {
    149  1.1  riastrad 				0xfa,
    150  1.1  riastrad 			},
    151  1.1  riastrad 			.out = {
    152  1.1  riastrad 				0xc4,0xef,0x36,0x92, 0x3c,0x64,0xe5,0x1e,
    153  1.1  riastrad 				0x87,0x57,0x20,0xe5, 0x50,0x29,0x8a,0x5a,
    154  1.1  riastrad 				0xb8,0xa3,0xf2,0xf8, 0x75,0xb1,0xe1,0xa4,
    155  1.1  riastrad 				0xc9,0xb9,0x5b,0xab, 0xf7,0x34,0x4f,0xef,
    156  1.1  riastrad 			},
    157  1.1  riastrad 		},
    158  1.1  riastrad 	};
    159  1.1  riastrad 
    160  1.1  riastrad 	check(C, __arraycount(C), 32, EVP_sha512_256());
    161  1.1  riastrad }
    162  1.1  riastrad 
    163  1.1  riastrad ATF_TP_ADD_TCS(tp)
    164  1.1  riastrad {
    165  1.1  riastrad 
    166  1.1  riastrad 	ATF_TP_ADD_TC(tp, sha512_224);
    167  1.1  riastrad 	ATF_TP_ADD_TC(tp, sha512_256);
    168  1.1  riastrad 
    169  1.1  riastrad 	return atf_no_error();
    170  1.1  riastrad }
    171