Home | History | Annotate | Line # | Download | only in opencrypto
h_aescbc.c revision 1.1.2.2
      1  1.1.2.2  bouyer /* $NetBSD: h_aescbc.c,v 1.1.2.2 2017/04/21 16:54:09 bouyer Exp $ */
      2  1.1.2.2  bouyer 
      3  1.1.2.2  bouyer /*-
      4  1.1.2.2  bouyer  * Copyright (c) 2017 Internet Initiative Japan Inc.
      5  1.1.2.2  bouyer  * All rights reserved.
      6  1.1.2.2  bouyer  *
      7  1.1.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.2  bouyer  * modification, are permitted provided that the following conditions
      9  1.1.2.2  bouyer  * are met:
     10  1.1.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     15  1.1.2.2  bouyer  *
     16  1.1.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1.2.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1.2.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1.2.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1.2.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1.2.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1.2.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1.2.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1.2.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1.2.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1.2.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1.2.2  bouyer  */
     28  1.1.2.2  bouyer 
     29  1.1.2.2  bouyer #include <err.h>
     30  1.1.2.2  bouyer #include <fcntl.h>
     31  1.1.2.2  bouyer #include <stdio.h>
     32  1.1.2.2  bouyer #include <string.h>
     33  1.1.2.2  bouyer #include <unistd.h>
     34  1.1.2.2  bouyer 
     35  1.1.2.2  bouyer #include <sys/ioctl.h>
     36  1.1.2.2  bouyer #include <sys/time.h>
     37  1.1.2.2  bouyer 
     38  1.1.2.2  bouyer #include <crypto/cryptodev.h>
     39  1.1.2.2  bouyer 
     40  1.1.2.2  bouyer /*
     41  1.1.2.2  bouyer  * Test vectors from RFC 3602
     42  1.1.2.2  bouyer  */
     43  1.1.2.2  bouyer 
     44  1.1.2.2  bouyer const struct {
     45  1.1.2.2  bouyer 	size_t len;
     46  1.1.2.2  bouyer 	size_t key_len;
     47  1.1.2.2  bouyer 	unsigned char key[16];
     48  1.1.2.2  bouyer 	unsigned char iv[16];
     49  1.1.2.2  bouyer 	unsigned char plaintx[64];
     50  1.1.2.2  bouyer 	unsigned char ciphertx[64];
     51  1.1.2.2  bouyer } tests[] = {
     52  1.1.2.2  bouyer 	/* Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key */
     53  1.1.2.2  bouyer 	{ 16, 16,
     54  1.1.2.2  bouyer 	  { 0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b,
     55  1.1.2.2  bouyer 	    0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06, },
     56  1.1.2.2  bouyer 	  { 0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30,
     57  1.1.2.2  bouyer 	    0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41, },
     58  1.1.2.2  bouyer 	  "Single block msg",
     59  1.1.2.2  bouyer 	  { 0xe3, 0x53, 0x77, 0x9c, 0x10, 0x79, 0xae, 0xb8,
     60  1.1.2.2  bouyer 	    0x27, 0x08, 0x94, 0x2d, 0xbe, 0x77, 0x18, 0x1a, },
     61  1.1.2.2  bouyer 	},
     62  1.1.2.2  bouyer 
     63  1.1.2.2  bouyer 	/* Case #2: Encrypting 32 bytes (2 blocks) using AES-CBC with 128-bit key */
     64  1.1.2.2  bouyer 	{ 32, 16,
     65  1.1.2.2  bouyer 	  { 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0,
     66  1.1.2.2  bouyer 	    0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a, },
     67  1.1.2.2  bouyer 	  { 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28,
     68  1.1.2.2  bouyer 	    0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58, },
     69  1.1.2.2  bouyer 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     70  1.1.2.2  bouyer 	    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     71  1.1.2.2  bouyer             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     72  1.1.2.2  bouyer 	    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, },
     73  1.1.2.2  bouyer 	  { 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a,
     74  1.1.2.2  bouyer 	    0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,
     75  1.1.2.2  bouyer 	    0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9,
     76  1.1.2.2  bouyer 	    0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1, },
     77  1.1.2.2  bouyer 	},
     78  1.1.2.2  bouyer 
     79  1.1.2.2  bouyer 	/* Case #3: Encrypting 48 bytes (3 blocks) using AES-CBC with 128-bit key */
     80  1.1.2.2  bouyer 	{ 48, 16,
     81  1.1.2.2  bouyer 	  { 0x6c, 0x3e, 0xa0, 0x47, 0x76, 0x30, 0xce, 0x21,
     82  1.1.2.2  bouyer 	    0xa2, 0xce, 0x33, 0x4a, 0xa7, 0x46, 0xc2, 0xcd, },
     83  1.1.2.2  bouyer 	  { 0xc7, 0x82, 0xdc, 0x4c, 0x09, 0x8c, 0x66, 0xcb,
     84  1.1.2.2  bouyer 	    0xd9, 0xcd, 0x27, 0xd8, 0x25, 0x68, 0x2c, 0x81, },
     85  1.1.2.2  bouyer 	  "This is a 48-byte message (exactly 3 AES blocks)",
     86  1.1.2.2  bouyer 	  { 0xd0, 0xa0, 0x2b, 0x38, 0x36, 0x45, 0x17, 0x53,
     87  1.1.2.2  bouyer 	    0xd4, 0x93, 0x66, 0x5d, 0x33, 0xf0, 0xe8, 0x86,
     88  1.1.2.2  bouyer 	    0x2d, 0xea, 0x54, 0xcd, 0xb2, 0x93, 0xab, 0xc7,
     89  1.1.2.2  bouyer 	    0x50, 0x69, 0x39, 0x27, 0x67, 0x72, 0xf8, 0xd5,
     90  1.1.2.2  bouyer 	    0x02, 0x1c, 0x19, 0x21, 0x6b, 0xad, 0x52, 0x5c,
     91  1.1.2.2  bouyer 	    0x85, 0x79, 0x69, 0x5d, 0x83, 0xba, 0x26, 0x84, },
     92  1.1.2.2  bouyer 	},
     93  1.1.2.2  bouyer 
     94  1.1.2.2  bouyer 	/* Case #4: Encrypting 64 bytes (4 blocks) using AES-CBC with 128-bit key */
     95  1.1.2.2  bouyer 	{ 64, 16,
     96  1.1.2.2  bouyer 	  { 0x56, 0xe4, 0x7a, 0x38, 0xc5, 0x59, 0x89, 0x74,
     97  1.1.2.2  bouyer 	    0xbc, 0x46, 0x90, 0x3d, 0xba, 0x29, 0x03, 0x49, },
     98  1.1.2.2  bouyer 	  { 0x8c, 0xe8, 0x2e, 0xef, 0xbe, 0xa0, 0xda, 0x3c,
     99  1.1.2.2  bouyer 	    0x44, 0x69, 0x9e, 0xd7, 0xdb, 0x51, 0xb7, 0xd9, },
    100  1.1.2.2  bouyer 	  { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
    101  1.1.2.2  bouyer 	    0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
    102  1.1.2.2  bouyer 	    0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
    103  1.1.2.2  bouyer 	    0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
    104  1.1.2.2  bouyer 	    0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
    105  1.1.2.2  bouyer 	    0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
    106  1.1.2.2  bouyer 	    0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
    107  1.1.2.2  bouyer 	    0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, },
    108  1.1.2.2  bouyer 	  { 0xc3, 0x0e, 0x32, 0xff, 0xed, 0xc0, 0x77, 0x4e,
    109  1.1.2.2  bouyer 	    0x6a, 0xff, 0x6a, 0xf0, 0x86, 0x9f, 0x71, 0xaa,
    110  1.1.2.2  bouyer 	    0x0f, 0x3a, 0xf0, 0x7a, 0x9a, 0x31, 0xa9, 0xc6,
    111  1.1.2.2  bouyer 	    0x84, 0xdb, 0x20, 0x7e, 0xb0, 0xef, 0x8e, 0x4e,
    112  1.1.2.2  bouyer 	    0x35, 0x90, 0x7a, 0xa6, 0x32, 0xc3, 0xff, 0xdf,
    113  1.1.2.2  bouyer 	    0x86, 0x8b, 0xb7, 0xb2, 0x9d, 0x3d, 0x46, 0xad,
    114  1.1.2.2  bouyer 	    0x83, 0xce, 0x9f, 0x9a, 0x10, 0x2e, 0xe9, 0x9d,
    115  1.1.2.2  bouyer 	    0x49, 0xa5, 0x3e, 0x87, 0xf4, 0xc3, 0xda, 0x55,
    116  1.1.2.2  bouyer 	  },
    117  1.1.2.2  bouyer 	},
    118  1.1.2.2  bouyer };
    119  1.1.2.2  bouyer 
    120  1.1.2.2  bouyer int
    121  1.1.2.2  bouyer main(void)
    122  1.1.2.2  bouyer {
    123  1.1.2.2  bouyer 	int fd, res;
    124  1.1.2.2  bouyer 	size_t i;
    125  1.1.2.2  bouyer 	struct session_op cs;
    126  1.1.2.2  bouyer 	struct crypt_op co;
    127  1.1.2.2  bouyer 	unsigned char buf[64];
    128  1.1.2.2  bouyer 
    129  1.1.2.2  bouyer 	for (i = 0; i < __arraycount(tests); i++) {
    130  1.1.2.2  bouyer 		fd = open("/dev/crypto", O_RDWR, 0);
    131  1.1.2.2  bouyer 		if (fd < 0)
    132  1.1.2.2  bouyer 			err(1, "open %zu", i);
    133  1.1.2.2  bouyer 		memset(&cs, 0, sizeof(cs));
    134  1.1.2.2  bouyer 		cs.cipher = CRYPTO_AES_CBC;
    135  1.1.2.2  bouyer 		cs.keylen = tests[i].key_len;
    136  1.1.2.2  bouyer 		cs.key = __UNCONST(&tests[i].key);
    137  1.1.2.2  bouyer 		res = ioctl(fd, CIOCGSESSION, &cs);
    138  1.1.2.2  bouyer 		if (res < 0)
    139  1.1.2.2  bouyer 			err(1, "CIOCGSESSION %zu", i);
    140  1.1.2.2  bouyer 
    141  1.1.2.2  bouyer 		memset(&co, 0, sizeof(co));
    142  1.1.2.2  bouyer 		co.ses = cs.ses;
    143  1.1.2.2  bouyer 		co.op = COP_ENCRYPT;
    144  1.1.2.2  bouyer 		co.len = tests[i].len;
    145  1.1.2.2  bouyer 		co.src = __UNCONST(&tests[i].plaintx);
    146  1.1.2.2  bouyer 		co.dst = buf;
    147  1.1.2.2  bouyer 		co.dst_len = sizeof(buf);
    148  1.1.2.2  bouyer 		co.iv = __UNCONST(&tests[i].iv);
    149  1.1.2.2  bouyer 		res = ioctl(fd, CIOCCRYPT, &co);
    150  1.1.2.2  bouyer 		if (res < 0)
    151  1.1.2.2  bouyer 			err(1, "CIOCCRYPT %zu", i);
    152  1.1.2.2  bouyer 
    153  1.1.2.2  bouyer 		if (memcmp(co.dst, tests[i].ciphertx, tests[i].len)) {
    154  1.1.2.2  bouyer 			size_t j;
    155  1.1.2.2  bouyer 			for (j = 0; j < tests[i].len; j++)
    156  1.1.2.2  bouyer 				printf("0x%2zu:  0x%2x    0x%2x\n", j,
    157  1.1.2.2  bouyer 					buf[j], tests[i].ciphertx[j]);
    158  1.1.2.2  bouyer 			errx(1, "verification failed %zu", i);
    159  1.1.2.2  bouyer 		}
    160  1.1.2.2  bouyer 		close(fd);
    161  1.1.2.2  bouyer 	}
    162  1.1.2.2  bouyer 	return 0;
    163  1.1.2.2  bouyer }
    164