Home | History | Annotate | Line # | Download | only in hash
t_hmac.c revision 1.2
      1  1.2  christos /*	$NetBSD: t_hmac.c,v 1.2 2018/02/07 13:18:33 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2016 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.2  christos __RCSID("$NetBSD: t_hmac.c,v 1.2 2018/02/07 13:18:33 christos Exp $");
     33  1.1  christos 
     34  1.1  christos #include <atf-c.h>
     35  1.1  christos #include <string.h>
     36  1.1  christos #include <stdlib.h>
     37  1.1  christos #include <openssl/evp.h>
     38  1.1  christos #include <openssl/hmac.h>
     39  1.1  christos 
     40  1.1  christos static void
     41  1.1  christos test(void)
     42  1.1  christos {
     43  1.1  christos 	uint8_t         tmp1[EVP_MAX_MD_SIZE];
     44  1.1  christos 	uint8_t         tmp2[EVP_MAX_MD_SIZE];
     45  1.1  christos 	uint8_t key[256];
     46  1.1  christos 	uint8_t data[4096];
     47  1.1  christos 	unsigned int tmp1len;
     48  1.1  christos 	size_t tmp2len;
     49  1.1  christos 	int stop;
     50  1.1  christos 	void *e1;
     51  1.1  christos 	const void *evps[] = {
     52  1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000L
     53  1.1  christos 		EVP_md2(),
     54  1.2  christos #endif
     55  1.1  christos 		EVP_md4(),
     56  1.1  christos 		EVP_md5(),
     57  1.1  christos 		EVP_ripemd160(),
     58  1.1  christos 		EVP_sha1(),
     59  1.1  christos 		EVP_sha224(),
     60  1.1  christos 		EVP_sha256(),
     61  1.1  christos 		EVP_sha384(),
     62  1.1  christos 		EVP_sha512(),
     63  1.1  christos 	};
     64  1.1  christos 	const char *names[] = {
     65  1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000L
     66  1.1  christos 		"md2",
     67  1.2  christos #endif
     68  1.1  christos 		"md4",
     69  1.1  christos 		"md5",
     70  1.1  christos 		"rmd160",
     71  1.1  christos 		"sha1",
     72  1.1  christos 		"sha224",
     73  1.1  christos 		"sha256",
     74  1.1  christos 		"sha384",
     75  1.1  christos 		"sha512",
     76  1.1  christos 	};
     77  1.1  christos 
     78  1.1  christos 	for (size_t k = 0; k < sizeof(key); k++)
     79  1.1  christos 		key[k] = k;
     80  1.1  christos 	for (size_t d = 0; d < sizeof(data); d++)
     81  1.1  christos 		data[d] = d % 256;
     82  1.1  christos 
     83  1.1  christos 	for (size_t t = 0; t < __arraycount(names); t++)
     84  1.1  christos 	    for (size_t i = 1; i < sizeof(key); i += 9)
     85  1.1  christos 		for (size_t j = 3; j < sizeof(data); j += 111) {
     86  1.1  christos 			stop = 0;
     87  1.1  christos #ifdef DEBUG
     88  1.1  christos 			printf("%s: keysize = %zu datasize = %zu\n", names[t],
     89  1.1  christos 			    i, j);
     90  1.1  christos #endif
     91  1.1  christos 			memset(tmp1, 0, sizeof(tmp1));
     92  1.1  christos 			memset(tmp2, 0, sizeof(tmp2));
     93  1.1  christos 			e1 = HMAC(evps[t], key, i, data, j, tmp1, &tmp1len);
     94  1.1  christos 			ATF_REQUIRE(e1 != NULL);
     95  1.1  christos 			tmp2len = hmac(names[t], key, i, data, j, tmp2,
     96  1.1  christos 			    sizeof(tmp2));
     97  1.1  christos 			ATF_REQUIRE_MSG(tmp1len == tmp2len, "hash %s len %u "
     98  1.1  christos 			    "!= %zu", names[t], tmp1len, tmp2len);
     99  1.1  christos 			for (size_t k = 0; k < tmp2len; k++)
    100  1.1  christos 				if (tmp1[k] != tmp2[k]) {
    101  1.1  christos #ifdef DEBUG
    102  1.1  christos 					printf("%zu %.2x %.2x\n",
    103  1.1  christos 					    k, tmp1[k], tmp2[k]);
    104  1.1  christos #endif
    105  1.1  christos 					stop = 1;
    106  1.1  christos 					break;
    107  1.1  christos 				}
    108  1.1  christos 			ATF_REQUIRE_MSG(!stop, "hash %s failed for "
    109  1.1  christos 				"keylen=%zu, datalen=%zu", names[t], i, j);
    110  1.1  christos 		}
    111  1.1  christos }
    112  1.1  christos 
    113  1.1  christos ATF_TC(t_hmac);
    114  1.1  christos 
    115  1.1  christos ATF_TC_HEAD(t_hmac, tc)
    116  1.1  christos {
    117  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    118  1.1  christos 	    "Test hmac functions for consistent results");
    119  1.1  christos }
    120  1.1  christos 
    121  1.1  christos ATF_TC_BODY(t_hmac, tc)
    122  1.1  christos {
    123  1.1  christos 	test();
    124  1.1  christos }
    125  1.1  christos 
    126  1.1  christos ATF_TP_ADD_TCS(tp)
    127  1.1  christos {
    128  1.1  christos 	ATF_TP_ADD_TC(tp, t_hmac);
    129  1.1  christos 	return atf_no_error();
    130  1.1  christos }
    131  1.1  christos 
    132