Home | History | Annotate | Line # | Download | only in opencrypto
h_aesctr2.c revision 1.2.8.2
      1  1.2.8.2  tls /* $NetBSD: h_aesctr2.c,v 1.2.8.2 2014/08/20 00:04:46 tls Exp $ */
      2  1.2.8.2  tls 
      3  1.2.8.2  tls /*-
      4  1.2.8.2  tls  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.2.8.2  tls  * All rights reserved.
      6  1.2.8.2  tls  *
      7  1.2.8.2  tls  * Redistribution and use in source and binary forms, with or without
      8  1.2.8.2  tls  * modification, are permitted provided that the following conditions
      9  1.2.8.2  tls  * are met:
     10  1.2.8.2  tls  * 1. Redistributions of source code must retain the above copyright
     11  1.2.8.2  tls  *    notice, this list of conditions and the following disclaimer.
     12  1.2.8.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.8.2  tls  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.8.2  tls  *    documentation and/or other materials provided with the distribution.
     15  1.2.8.2  tls  *
     16  1.2.8.2  tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.2.8.2  tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.2.8.2  tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.2.8.2  tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.2.8.2  tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.2.8.2  tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.2.8.2  tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.2.8.2  tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.2.8.2  tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.2.8.2  tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.2.8.2  tls  * POSSIBILITY OF SUCH DAMAGE.
     27  1.2.8.2  tls  */
     28  1.2.8.2  tls 
     29  1.2.8.2  tls #include <err.h>
     30  1.2.8.2  tls #include <fcntl.h>
     31  1.2.8.2  tls #include <stdio.h>
     32  1.2.8.2  tls #include <string.h>
     33  1.2.8.2  tls 
     34  1.2.8.2  tls #include <sys/ioctl.h>
     35  1.2.8.2  tls #include <sys/time.h>
     36  1.2.8.2  tls 
     37  1.2.8.2  tls #include <crypto/cryptodev.h>
     38  1.2.8.2  tls 
     39  1.2.8.2  tls unsigned char key[20] = {0xae, 0x68, 0x52, 0xf8, 0x12, 0x10, 0x67, 0xcc,
     40  1.2.8.2  tls 			 0x4b, 0xf7, 0xa5, 0x76, 0x55, 0x77, 0xf3, 0x9e,
     41  1.2.8.2  tls 	0x00, 0x00, 0x00, 0x30};
     42  1.2.8.2  tls unsigned char iv[8] = {0};
     43  1.2.8.2  tls char plaintx[16] = "Single block msg";
     44  1.2.8.2  tls const unsigned char ciphertx[16] = {
     45  1.2.8.2  tls 	0xe4, 0x09, 0x5d, 0x4f, 0xb7, 0xa7, 0xb3, 0x79,
     46  1.2.8.2  tls 	0x2d, 0x61, 0x75, 0xa3, 0x26, 0x13, 0x11, 0xb8
     47  1.2.8.2  tls };
     48  1.2.8.2  tls 
     49  1.2.8.2  tls int
     50  1.2.8.2  tls main(void)
     51  1.2.8.2  tls {
     52  1.2.8.2  tls 	int fd, res;
     53  1.2.8.2  tls 	struct session_op cs;
     54  1.2.8.2  tls 	struct crypt_op co;
     55  1.2.8.2  tls 	unsigned char ibuf[24];
     56  1.2.8.2  tls 	unsigned char obuf[24];
     57  1.2.8.2  tls 
     58  1.2.8.2  tls 	fd = open("/dev/crypto", O_RDWR, 0);
     59  1.2.8.2  tls 	if (fd < 0)
     60  1.2.8.2  tls 		err(1, "open");
     61  1.2.8.2  tls 	memset(&cs, 0, sizeof(cs));
     62  1.2.8.2  tls 	cs.cipher = CRYPTO_AES_CTR;
     63  1.2.8.2  tls 	cs.keylen = 20;
     64  1.2.8.2  tls 	cs.key = key;
     65  1.2.8.2  tls 	res = ioctl(fd, CIOCGSESSION, &cs);
     66  1.2.8.2  tls 	if (res < 0)
     67  1.2.8.2  tls 		err(1, "CIOCGSESSION");
     68  1.2.8.2  tls 
     69  1.2.8.2  tls 	memcpy(ibuf, iv, 8);
     70  1.2.8.2  tls 	memcpy(ibuf + 8, plaintx, 16);
     71  1.2.8.2  tls 	memset(&co, 0, sizeof(co));
     72  1.2.8.2  tls 	co.ses = cs.ses;
     73  1.2.8.2  tls 	co.op = COP_ENCRYPT;
     74  1.2.8.2  tls 	co.len = sizeof(ibuf);
     75  1.2.8.2  tls 	co.src = ibuf;
     76  1.2.8.2  tls 	co.dst = obuf;
     77  1.2.8.2  tls 	co.dst_len = sizeof(obuf);
     78  1.2.8.2  tls 	res = ioctl(fd, CIOCCRYPT, &co);
     79  1.2.8.2  tls 	if (res < 0)
     80  1.2.8.2  tls 		err(1, "CIOCCRYPT");
     81  1.2.8.2  tls 	memset(ibuf, 0, sizeof(ibuf));
     82  1.2.8.2  tls 	memset(&co, 0, sizeof(co));
     83  1.2.8.2  tls 	co.ses = cs.ses;
     84  1.2.8.2  tls 	co.op = COP_DECRYPT;
     85  1.2.8.2  tls 	co.len = sizeof(obuf);
     86  1.2.8.2  tls 	co.src = obuf;
     87  1.2.8.2  tls 	co.dst = ibuf;
     88  1.2.8.2  tls 	co.dst_len = sizeof(ibuf);
     89  1.2.8.2  tls 	res = ioctl(fd, CIOCCRYPT, &co);
     90  1.2.8.2  tls 	if (res < 0)
     91  1.2.8.2  tls 		err(1, "CIOCCRYPT");
     92  1.2.8.2  tls 
     93  1.2.8.2  tls 	if (memcmp((char *)co.dst + 8, plaintx, sizeof(plaintx)))
     94  1.2.8.2  tls 		warnx("verification failed");
     95  1.2.8.2  tls 
     96  1.2.8.2  tls 	return 0;
     97  1.2.8.2  tls }
     98