Home | History | Annotate | Line # | Download | only in hash
t_hmac.c revision 1.1
      1  1.1  christos /*	$NetBSD: t_hmac.c,v 1.1 2016/07/02 14:52:09 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.1  christos __RCSID("$NetBSD: t_hmac.c,v 1.1 2016/07/02 14:52:09 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.1  christos 		EVP_md2(),
     53  1.1  christos 		EVP_md4(),
     54  1.1  christos 		EVP_md5(),
     55  1.1  christos 		EVP_ripemd160(),
     56  1.1  christos 		EVP_sha1(),
     57  1.1  christos 		EVP_sha224(),
     58  1.1  christos 		EVP_sha256(),
     59  1.1  christos 		EVP_sha384(),
     60  1.1  christos 		EVP_sha512(),
     61  1.1  christos 	};
     62  1.1  christos 	const char *names[] = {
     63  1.1  christos 		"md2",
     64  1.1  christos 		"md4",
     65  1.1  christos 		"md5",
     66  1.1  christos 		"rmd160",
     67  1.1  christos 		"sha1",
     68  1.1  christos 		"sha224",
     69  1.1  christos 		"sha256",
     70  1.1  christos 		"sha384",
     71  1.1  christos 		"sha512",
     72  1.1  christos 	};
     73  1.1  christos 
     74  1.1  christos 	for (size_t k = 0; k < sizeof(key); k++)
     75  1.1  christos 		key[k] = k;
     76  1.1  christos 	for (size_t d = 0; d < sizeof(data); d++)
     77  1.1  christos 		data[d] = d % 256;
     78  1.1  christos 
     79  1.1  christos 	for (size_t t = 0; t < __arraycount(names); t++)
     80  1.1  christos 	    for (size_t i = 1; i < sizeof(key); i += 9)
     81  1.1  christos 		for (size_t j = 3; j < sizeof(data); j += 111) {
     82  1.1  christos 			stop = 0;
     83  1.1  christos #ifdef DEBUG
     84  1.1  christos 			printf("%s: keysize = %zu datasize = %zu\n", names[t],
     85  1.1  christos 			    i, j);
     86  1.1  christos #endif
     87  1.1  christos 			memset(tmp1, 0, sizeof(tmp1));
     88  1.1  christos 			memset(tmp2, 0, sizeof(tmp2));
     89  1.1  christos 			e1 = HMAC(evps[t], key, i, data, j, tmp1, &tmp1len);
     90  1.1  christos 			ATF_REQUIRE(e1 != NULL);
     91  1.1  christos 			tmp2len = hmac(names[t], key, i, data, j, tmp2,
     92  1.1  christos 			    sizeof(tmp2));
     93  1.1  christos 			ATF_REQUIRE_MSG(tmp1len == tmp2len, "hash %s len %u "
     94  1.1  christos 			    "!= %zu", names[t], tmp1len, tmp2len);
     95  1.1  christos 			for (size_t k = 0; k < tmp2len; k++)
     96  1.1  christos 				if (tmp1[k] != tmp2[k]) {
     97  1.1  christos #ifdef DEBUG
     98  1.1  christos 					printf("%zu %.2x %.2x\n",
     99  1.1  christos 					    k, tmp1[k], tmp2[k]);
    100  1.1  christos #endif
    101  1.1  christos 					stop = 1;
    102  1.1  christos 					break;
    103  1.1  christos 				}
    104  1.1  christos 			ATF_REQUIRE_MSG(!stop, "hash %s failed for "
    105  1.1  christos 				"keylen=%zu, datalen=%zu", names[t], i, j);
    106  1.1  christos 		}
    107  1.1  christos }
    108  1.1  christos 
    109  1.1  christos ATF_TC(t_hmac);
    110  1.1  christos 
    111  1.1  christos ATF_TC_HEAD(t_hmac, tc)
    112  1.1  christos {
    113  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    114  1.1  christos 	    "Test hmac functions for consistent results");
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos ATF_TC_BODY(t_hmac, tc)
    118  1.1  christos {
    119  1.1  christos 	test();
    120  1.1  christos }
    121  1.1  christos 
    122  1.1  christos ATF_TP_ADD_TCS(tp)
    123  1.1  christos {
    124  1.1  christos 	ATF_TP_ADD_TC(tp, t_hmac);
    125  1.1  christos 	return atf_no_error();
    126  1.1  christos }
    127  1.1  christos 
    128