1130803Smarcel/* $NetBSD$ */
2130803Smarcel
3130803Smarcel/*-
4130803Smarcel * Copyright (c) 2014 The NetBSD Foundation, Inc.
5130803Smarcel * All rights reserved.
6130803Smarcel *
7130803Smarcel * Redistribution and use in source and binary forms, with or without
8130803Smarcel * modification, are permitted provided that the following conditions
9130803Smarcel * are met:
10130803Smarcel * 1. Redistributions of source code must retain the above copyright
11130803Smarcel *    notice, this list of conditions and the following disclaimer.
12130803Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13130803Smarcel *    notice, this list of conditions and the following disclaimer in the
14130803Smarcel *    documentation and/or other materials provided with the distribution.
15130803Smarcel *
16130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17130803Smarcel * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18130803Smarcel * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19130803Smarcel * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20130803Smarcel * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21130803Smarcel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22130803Smarcel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23130803Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24130803Smarcel * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25130803Smarcel * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26130803Smarcel * POSSIBILITY OF SUCH DAMAGE.
27130803Smarcel */
28130803Smarcel
29130803Smarcel#include <err.h>
30130803Smarcel#include <fcntl.h>
31130803Smarcel#include <string.h>
32130803Smarcel#include <zlib.h>
33130803Smarcel
34130803Smarcel#include <sys/ioctl.h>
35130803Smarcel#include <sys/time.h>
36130803Smarcel
37130803Smarcel#include <crypto/cryptodev.h>
38130803Smarcel
39130803Smarcelchar text[10000] = {0};
40130803Smarcel
41130803Smarcelint
42130803Smarcelmain(void)
43130803Smarcel{
44130803Smarcel	int fd, res;
45130803Smarcel	struct session_op cs;
46130803Smarcel	struct crypt_op co1;
47130803Smarcel	unsigned char buf1[10000], buf2[10000];
48130803Smarcel	z_stream z;
49130803Smarcel
50130803Smarcel	fd = open("/dev/crypto", O_RDWR, 0);
51130803Smarcel	if (fd < 0)
52130803Smarcel		err(1, "open");
53130803Smarcel	memset(&cs, 0, sizeof(cs));
54130803Smarcel	cs.comp_alg = CRYPTO_DEFLATE_COMP;
55130803Smarcel	res = ioctl(fd, CIOCGSESSION, &cs);
56130803Smarcel	if (res < 0)
57130803Smarcel		err(1, "CIOCGSESSION");
58130803Smarcel
59130803Smarcel	memset(&co1, 0, sizeof(co1));
60130803Smarcel	co1.ses = cs.ses;
61130803Smarcel	co1.op = COP_COMP;
62130803Smarcel	co1.len = sizeof(text);
63130803Smarcel	co1.src = text;
64130803Smarcel	co1.dst = buf1;
65130803Smarcel	co1.dst_len = sizeof(buf1);
66130803Smarcel	co1.flags = COP_F_BATCH;
67130803Smarcel	res = ioctl(fd, CIOCCRYPT, &co1);
68130803Smarcel	if (res < 0)
69130803Smarcel		err(1, "CIOCCRYPT");
70130803Smarcel
71130803Smarcel	memset(&z, 0, sizeof(z));
72130803Smarcel	z.next_in = buf1;
73130803Smarcel	z.avail_in = co1.dst_len;
74130803Smarcel	z.zalloc = Z_NULL;
75130803Smarcel	z.zfree = Z_NULL;
76130803Smarcel	z.opaque = 0;
77130803Smarcel	z.next_out = buf2;
78130803Smarcel	z.avail_out = sizeof(buf2);
79130803Smarcel	res = inflateInit2(&z, -15);
80130803Smarcel	if (res != Z_OK)
81130803Smarcel		errx(1, "inflateInit: %d", res);
82130803Smarcel	do {
83130803Smarcel		res = inflate(&z, Z_SYNC_FLUSH);
84	} while (res == Z_OK);
85	if (res != Z_STREAM_END)
86		errx(1, "inflate: %d", res);
87	if (z.total_out != sizeof(text))
88		errx(1, "decomp len %lu", z.total_out);
89	if (memcmp(buf2, text, sizeof(text)))
90		errx(1, "decomp data mismatch");
91	return 0;
92}
93