1*57718be8SEnji Cooper /* $NetBSD: h_camellia.c,v 1.3 2014/01/17 19:39:51 pgoyette Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper * are met:
10*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper *
16*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
27*57718be8SEnji Cooper */
28*57718be8SEnji Cooper
29*57718be8SEnji Cooper #include <err.h>
30*57718be8SEnji Cooper #include <fcntl.h>
31*57718be8SEnji Cooper #include <stdio.h>
32*57718be8SEnji Cooper #include <string.h>
33*57718be8SEnji Cooper
34*57718be8SEnji Cooper #include <sys/ioctl.h>
35*57718be8SEnji Cooper #include <sys/time.h>
36*57718be8SEnji Cooper
37*57718be8SEnji Cooper #include <crypto/cryptodev.h>
38*57718be8SEnji Cooper
39*57718be8SEnji Cooper /* Test vector from RFC3713 */
40*57718be8SEnji Cooper unsigned char key[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
41*57718be8SEnji Cooper 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
42*57718be8SEnji Cooper 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
43*57718be8SEnji Cooper 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
44*57718be8SEnji Cooper unsigned char iv[16] = {0};
45*57718be8SEnji Cooper char plaintx[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
46*57718be8SEnji Cooper 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
47*57718be8SEnji Cooper const unsigned char ciphertx[16] = {
48*57718be8SEnji Cooper 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
49*57718be8SEnji Cooper 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
50*57718be8SEnji Cooper };
51*57718be8SEnji Cooper
52*57718be8SEnji Cooper int
main(void)53*57718be8SEnji Cooper main(void)
54*57718be8SEnji Cooper {
55*57718be8SEnji Cooper int fd, res;
56*57718be8SEnji Cooper struct session_op cs;
57*57718be8SEnji Cooper struct crypt_op co;
58*57718be8SEnji Cooper unsigned char buf[16];
59*57718be8SEnji Cooper
60*57718be8SEnji Cooper fd = open("/dev/crypto", O_RDWR, 0);
61*57718be8SEnji Cooper if (fd < 0)
62*57718be8SEnji Cooper err(1, "open");
63*57718be8SEnji Cooper memset(&cs, 0, sizeof(cs));
64*57718be8SEnji Cooper cs.cipher = CRYPTO_CAMELLIA_CBC;
65*57718be8SEnji Cooper cs.keylen = 32;
66*57718be8SEnji Cooper cs.key = key;
67*57718be8SEnji Cooper res = ioctl(fd, CIOCGSESSION, &cs);
68*57718be8SEnji Cooper if (res < 0)
69*57718be8SEnji Cooper err(1, "CIOCGSESSION");
70*57718be8SEnji Cooper
71*57718be8SEnji Cooper memset(&co, 0, sizeof(co));
72*57718be8SEnji Cooper co.ses = cs.ses;
73*57718be8SEnji Cooper co.op = COP_ENCRYPT;
74*57718be8SEnji Cooper co.len = sizeof(plaintx);
75*57718be8SEnji Cooper co.src = plaintx;
76*57718be8SEnji Cooper co.dst = buf;
77*57718be8SEnji Cooper co.dst_len = sizeof(buf);
78*57718be8SEnji Cooper co.iv = iv;
79*57718be8SEnji Cooper res = ioctl(fd, CIOCCRYPT, &co);
80*57718be8SEnji Cooper if (res < 0)
81*57718be8SEnji Cooper err(1, "CIOCCRYPT");
82*57718be8SEnji Cooper
83*57718be8SEnji Cooper if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
84*57718be8SEnji Cooper warnx("verification failed");
85*57718be8SEnji Cooper
86*57718be8SEnji Cooper return 0;
87*57718be8SEnji Cooper }
88