NetBSD/tests/crypto/opencrypto/h_sha2hmac.c

269 lines
10 KiB
C

/* $NetBSD: h_sha2hmac.c,v 1.1 2019/12/03 04:20:45 hikaru Exp $ */
/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <crypto/cryptodev.h>
/* Test data from RFC4868 */
const struct {
int num;
int alg;
size_t key_len;
size_t len;
size_t mac_len;
unsigned char key[80];
unsigned char data[80];
unsigned char mac[80];
} tests[] = {
{ 1, CRYPTO_SHA2_256_HMAC, 32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d, 0xcd, 0xcd, 0xcd,
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0xcd, 0xcd },
{ 0x5e, 0x66, 0x88, 0xe5, 0xa3, 0xda, 0xec, 0x82,
0x6c, 0xa3, 0x2e, 0xae, 0xa2, 0x24, 0xef, 0xf5,
0xe7, 0x00, 0x62, 0x89, 0x47, 0x47, 0x0e, 0x13,
0xad, 0x01, 0x30, 0x25, 0x61, 0xba, 0xb1, 0x08 }
},
};
int
main(void)
{
size_t i;
int fd, res;
struct session_op cs;
struct crypt_op co;
unsigned char buf[80];
fd = open("/dev/crypto", O_RDWR, 0);
if (fd < 0)
err(1, "open");
for (i = 0; i < __arraycount(tests); i++) {
memset(&cs, 0, sizeof(cs));
cs.mac = tests[i].alg;
cs.mackeylen = tests[i].key_len;
cs.mackey = __UNCONST(&tests[i].key);
res = ioctl(fd, CIOCGSESSION, &cs);
if (res < 0)
err(1, "CIOCGSESSION test %d", tests[i].num);
memset(&co, 0, sizeof(co));
memset(buf, 0, sizeof(buf));
co.ses = cs.ses;
co.op = COP_ENCRYPT;
co.len = tests[i].len;
co.src = __UNCONST(&tests[i].data);
co.mac = buf;
res = ioctl(fd, CIOCCRYPT, &co);
if (res < 0)
err(1, "CIOCCRYPT test %d", tests[i].num);
/* compare with trailing zeros */
if (memcmp(co.mac, &tests[i].mac, sizeof(tests[i].mac)))
errx(1, "verification failed test %d", tests[i].num);
res = ioctl(fd, CIOCFSESSION, &cs.ses);
if (res < 0)
err(1, "CIOCFSESSION test %d", tests[i].num);
}
return 0;
}