Home | History | Annotate | Line # | Download | only in opencrypto
      1  1.4  pgoyette /* $NetBSD: h_aesctr1.c,v 1.4 2014/01/19 13:40:59 pgoyette Exp $ */
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.1  pgoyette  * modification, are permitted provided that the following conditions
      9  1.1  pgoyette  * are met:
     10  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.1  pgoyette  *
     16  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  pgoyette  */
     28  1.1  pgoyette 
     29  1.1  pgoyette #include <err.h>
     30  1.1  pgoyette #include <fcntl.h>
     31  1.1  pgoyette #include <stdio.h>
     32  1.1  pgoyette #include <string.h>
     33  1.3  pgoyette #include <unistd.h>
     34  1.1  pgoyette 
     35  1.1  pgoyette #include <sys/ioctl.h>
     36  1.1  pgoyette #include <sys/time.h>
     37  1.1  pgoyette 
     38  1.1  pgoyette #include <crypto/cryptodev.h>
     39  1.1  pgoyette 
     40  1.3  pgoyette /*
     41  1.3  pgoyette  * Test vectors from RFC 3686
     42  1.3  pgoyette  *
     43  1.3  pgoyette  * Test vectors 3, 6, and 9 are disabled because we don't support
     44  1.3  pgoyette  * 36-byte (ie, unpadded) operations.
     45  1.3  pgoyette  */
     46  1.3  pgoyette 
     47  1.3  pgoyette const struct {
     48  1.3  pgoyette 	size_t len;
     49  1.3  pgoyette 	size_t key_len;
     50  1.3  pgoyette 	unsigned char key[36];		/* Includes 32-bit nonce */
     51  1.3  pgoyette 	unsigned char iv[8];
     52  1.3  pgoyette 	unsigned char plaintx[36];
     53  1.3  pgoyette 	unsigned char ciphertx[36];
     54  1.3  pgoyette } tests[] = {
     55  1.3  pgoyette 	/* Test Vector #1: Encrypting 16 octets using AES-CTR w/ 128-bit key*/
     56  1.3  pgoyette 	{ 16, 20,
     57  1.3  pgoyette 	  { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
     58  1.3  pgoyette 	    0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E,
     59  1.3  pgoyette 	    0x00, 0x00, 0x00, 0x30 },
     60  1.3  pgoyette 	  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
     61  1.3  pgoyette 	  { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
     62  1.3  pgoyette 	    0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
     63  1.3  pgoyette 	  { 0xE4, 0x09, 0x5D, 0x4F, 0xB7, 0xA7, 0xB3, 0x79,
     64  1.3  pgoyette 	    0x2D, 0x61, 0x75, 0xA3, 0x26, 0x13, 0x11, 0xB8 }
     65  1.3  pgoyette 	},
     66  1.3  pgoyette 
     67  1.3  pgoyette 	/* Test Vector #2: Encrypting 32 octets using AES-CTR w/ 128-bit key */
     68  1.3  pgoyette 	{ 32, 20,
     69  1.3  pgoyette 	  { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
     70  1.3  pgoyette 	    0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63,
     71  1.3  pgoyette 	    0x00, 0x6C, 0xB6, 0xDB },
     72  1.3  pgoyette 	  { 0xC0, 0x54, 0x3B, 0x59, 0xDA, 0x48, 0xD9, 0x0B },
     73  1.3  pgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     74  1.3  pgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
     75  1.3  pgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     76  1.3  pgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
     77  1.3  pgoyette 	  { 0x51, 0x04, 0xA1, 0x06, 0x16, 0x8A, 0x72, 0xD9,
     78  1.3  pgoyette 	    0x79, 0x0D, 0x41, 0xEE, 0x8E, 0xDA, 0xD3, 0x88,
     79  1.3  pgoyette 	    0xEB, 0x2E, 0x1E, 0xFC, 0x46, 0xDA, 0x57, 0xC8,
     80  1.3  pgoyette 	    0xFC, 0xE6, 0x30, 0xDF, 0x91, 0x41, 0xBE, 0x28 }
     81  1.3  pgoyette 	},
     82  1.3  pgoyette 
     83  1.3  pgoyette 	/* Test Vector #3: Encrypting 36 octets using AES-CTR w/ 128-bit key */
     84  1.3  pgoyette /*	{ 36, 20,
     85  1.3  pgoyette 	  { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
     86  1.3  pgoyette 	    0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC,
     87  1.3  pgoyette 	    0x00, 0xE0, 0x01, 0x7B },
     88  1.3  pgoyette 	  { 0x27, 0x77, 0x7F, 0x3F, 0x4A, 0x17, 0x86, 0xF0 },
     89  1.3  pgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
     90  1.3  pgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
     91  1.3  pgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
     92  1.3  pgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
     93  1.3  pgoyette 	    0x20, 0x21, 0x22, 0x23 },
     94  1.3  pgoyette 	  { 0xC1, 0xCF, 0x48, 0xA8, 0x9F, 0x2F, 0xFD, 0xD9,
     95  1.3  pgoyette 	    0xCF, 0x46, 0x52, 0xE9, 0xEF, 0xDB, 0x72, 0xD7,
     96  1.3  pgoyette 	    0x45, 0x40, 0xA4, 0x2B, 0xDE, 0x6D, 0x78, 0x36,
     97  1.3  pgoyette 	    0xD5, 0x9A, 0x5C, 0xEA, 0xAE, 0xF3, 0x10, 0x53,
     98  1.3  pgoyette 	    0x25, 0xB2, 0x07, 0x2F }
     99  1.3  pgoyette 	},
    100  1.3  pgoyette */
    101  1.3  pgoyette 	/* Test Vector #4: Encrypting 16 octets using AES-CTR w/ 192-bit key */
    102  1.3  pgoyette 	{ 16, 28,
    103  1.3  pgoyette 	  { 0x16, 0xAF, 0x5B, 0x14, 0x5F, 0xC9, 0xF5, 0x79,
    104  1.3  pgoyette 	    0xC1, 0x75, 0xF9, 0x3E, 0x3B, 0xFB, 0x0E, 0xED,
    105  1.3  pgoyette 	    0x86, 0x3D, 0x06, 0xCC, 0xFD, 0xB7, 0x85, 0x15,
    106  1.3  pgoyette 	    0x00, 0x00, 0x00, 0x48 },
    107  1.3  pgoyette 	  { 0x36, 0x73, 0x3C, 0x14, 0x7D, 0x6D, 0x93, 0xCB },
    108  1.3  pgoyette 	  { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
    109  1.3  pgoyette 	    0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
    110  1.3  pgoyette 	  { 0x4B, 0x55, 0x38, 0x4F, 0xE2, 0x59, 0xC9, 0xC8,
    111  1.3  pgoyette 	    0x4E, 0x79, 0x35, 0xA0, 0x03, 0xCB, 0xE9, 0x28 }
    112  1.3  pgoyette 	},
    113  1.3  pgoyette 
    114  1.3  pgoyette 	/* Test Vector #5: Encrypting 32 octets using AES-CTR w/ 192-bit key */
    115  1.3  pgoyette 	{ 32, 28,
    116  1.3  pgoyette 	  { 0x7C, 0x5C, 0xB2, 0x40, 0x1B, 0x3D, 0xC3, 0x3C,
    117  1.3  pgoyette 	    0x19, 0xE7, 0x34, 0x08, 0x19, 0xE0, 0xF6, 0x9C,
    118  1.3  pgoyette 	    0x67, 0x8C, 0x3D, 0xB8, 0xE6, 0xF6, 0xA9, 0x1A,
    119  1.3  pgoyette 	    0x00, 0x96, 0xB0, 0x3B },
    120  1.3  pgoyette 	  { 0x02, 0x0C, 0x6E, 0xAD, 0xC2, 0xCB, 0x50, 0x0D },
    121  1.3  pgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    122  1.3  pgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
    123  1.3  pgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    124  1.3  pgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
    125  1.3  pgoyette 	  { 0x45, 0x32, 0x43, 0xFC, 0x60, 0x9B, 0x23, 0x32,
    126  1.3  pgoyette 	    0x7E, 0xDF, 0xAA, 0xFA, 0x71, 0x31, 0xCD, 0x9F,
    127  1.3  pgoyette 	    0x84, 0x90, 0x70, 0x1C, 0x5A, 0xD4, 0xA7, 0x9C,
    128  1.3  pgoyette 	    0xFC, 0x1F, 0xE0, 0xFF, 0x42, 0xF4, 0xFB, 0x00 }
    129  1.3  pgoyette 	},
    130  1.3  pgoyette 
    131  1.3  pgoyette 	/* Test Vector #6: Encrypting 36 octets using AES-CTR w/ 192-bit key */
    132  1.3  pgoyette /*
    133  1.3  pgoyette 	{ 36, 28,
    134  1.3  pgoyette 	  { 0x02, 0xBF, 0x39, 0x1E, 0xE8, 0xEC, 0xB1, 0x59,
    135  1.3  pgoyette 	    0xB9, 0x59, 0x61, 0x7B, 0x09, 0x65, 0x27, 0x9B,
    136  1.3  pgoyette 	    0xF5, 0x9B, 0x60, 0xA7, 0x86, 0xD3, 0xE0, 0xFE,
    137  1.3  pgoyette 	    0x00, 0x07, 0xBD, 0xFD },
    138  1.3  pgoyette 	  { 0x5C, 0xBD, 0x60, 0x27, 0x8D, 0xCC, 0x09, 0x12 },
    139  1.3  pgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    140  1.3  pgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
    141  1.3  pgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    142  1.3  pgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
    143  1.3  pgoyette 	    0x20, 0x21, 0x22, 0x23 },
    144  1.3  pgoyette 	  { 0x96, 0x89, 0x3F, 0xC5, 0x5E, 0x5C, 0x72, 0x2F,
    145  1.3  pgoyette 	    0x54, 0x0B, 0x7D, 0xD1, 0xDD, 0xF7, 0xE7, 0x58,
    146  1.3  pgoyette 	    0xD2, 0x88, 0xBC, 0x95, 0xC6, 0x91, 0x65, 0x88,
    147  1.3  pgoyette 	    0x45, 0x36, 0xC8, 0x11, 0x66, 0x2F, 0x21, 0x88,
    148  1.3  pgoyette 	    0xAB, 0xEE, 0x09, 0x35 },
    149  1.3  pgoyette 	},
    150  1.3  pgoyette */
    151  1.3  pgoyette 	/* Test Vector #7: Encrypting 16 octets using AES-CTR w/ 256-bit key */
    152  1.3  pgoyette 	{ 16, 36,
    153  1.3  pgoyette 	  { 0x77, 0x6B, 0xEF, 0xF2, 0x85, 0x1D, 0xB0, 0x6F,
    154  1.3  pgoyette 	    0x4C, 0x8A, 0x05, 0x42, 0xC8, 0x69, 0x6F, 0x6C,
    155  1.3  pgoyette 	    0x6A, 0x81, 0xAF, 0x1E, 0xEC, 0x96, 0xB4, 0xD3,
    156  1.3  pgoyette 	    0x7F, 0xC1, 0xD6, 0x89, 0xE6, 0xC1, 0xC1, 0x04,
    157  1.3  pgoyette 	    0x00, 0x00, 0x00, 0x60 },
    158  1.3  pgoyette 	  { 0xDB, 0x56, 0x72, 0xC9, 0x7A, 0xA8, 0xF0, 0xB2 },
    159  1.3  pgoyette 	  { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
    160  1.3  pgoyette 	    0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
    161  1.3  pgoyette 	  { 0x14, 0x5A, 0xD0, 0x1D, 0xBF, 0x82, 0x4E, 0xC7,
    162  1.3  pgoyette 	    0x56, 0x08, 0x63, 0xDC, 0x71, 0xE3, 0xE0, 0xC0 },
    163  1.3  pgoyette 	},
    164  1.3  pgoyette 
    165  1.3  pgoyette 	/* Test Vector #8: Encrypting 32 octets using AES-CTR w/ 256-bit key */
    166  1.3  pgoyette 	{ 32, 36,
    167  1.3  pgoyette 	  { 0xF6, 0xD6, 0x6D, 0x6B, 0xD5, 0x2D, 0x59, 0xBB,
    168  1.3  pgoyette 	    0x07, 0x96, 0x36, 0x58, 0x79, 0xEF, 0xF8, 0x86,
    169  1.3  pgoyette 	    0xC6, 0x6D, 0xD5, 0x1A, 0x5B, 0x6A, 0x99, 0x74,
    170  1.3  pgoyette 	    0x4B, 0x50, 0x59, 0x0C, 0x87, 0xA2, 0x38, 0x84,
    171  1.3  pgoyette 	    0x00, 0xFA, 0xAC, 0x24 },
    172  1.3  pgoyette 	  { 0xC1, 0x58, 0x5E, 0xF1, 0x5A, 0x43, 0xD8, 0x75 },
    173  1.3  pgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    174  1.3  pgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
    175  1.3  pgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    176  1.3  pgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
    177  1.3  pgoyette 	  { 0xF0, 0x5E, 0x23, 0x1B, 0x38, 0x94, 0x61, 0x2C,
    178  1.3  pgoyette 	    0x49, 0xEE, 0x00, 0x0B, 0x80, 0x4E, 0xB2, 0xA9,
    179  1.3  pgoyette 	    0xB8, 0x30, 0x6B, 0x50, 0x8F, 0x83, 0x9D, 0x6A,
    180  1.3  pgoyette 	    0x55, 0x30, 0x83, 0x1D, 0x93, 0x44, 0xAF, 0x1C },
    181  1.3  pgoyette 	},
    182  1.3  pgoyette 
    183  1.3  pgoyette 	/* Test Vector #9: Encrypting 36 octets using AES-CTR w/ 256-bit key */
    184  1.3  pgoyette /*
    185  1.3  pgoyette 	{ 36, 36,
    186  1.3  pgoyette 	  { 0xFF 0x7A 0x61 0x7C 0xE6 0x91 0x48 0xE4,
    187  1.3  pgoyette 	    0xF1 0x72 0x6E 0x2F 0x43 0x58 0x1D 0xE2,
    188  1.3  pgoyette 	    0xAA 0x62 0xD9 0xF8 0x05 0x53 0x2E 0xDF,
    189  1.3  pgoyette 	    0xF1 0xEE 0xD6 0x87 0xFB 0x54 0x15 0x3D,
    190  1.3  pgoyette 	    0x00 0x1C 0xC5 0xB7 },
    191  1.3  pgoyette 	  { 0x51 0xA5 0x1D 0x70 0xA1 0xC1 0x11 0x48 },
    192  1.3  pgoyette 	  { 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07,
    193  1.3  pgoyette 	    0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F,
    194  1.3  pgoyette 	    0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17,
    195  1.3  pgoyette 	    0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F,
    196  1.3  pgoyette 	    0x20 0x21 0x22 0x23 },
    197  1.3  pgoyette 	  { 0xEB 0x6C 0x52 0x82 0x1D 0x0B 0xBB 0xF7,
    198  1.3  pgoyette 	    0xCE 0x75 0x94 0x46 0x2A 0xCA 0x4F 0xAA,
    199  1.3  pgoyette 	    0xB4 0x07 0xDF 0x86 0x65 0x69 0xFD 0x07,
    200  1.3  pgoyette 	    0xF4 0x8C 0xC0 0xB5 0x83 0xD6 0x07 0x1F,
    201  1.3  pgoyette 	    0x1E 0xC0 0xE6 0xB8 },
    202  1.3  pgoyette 	},
    203  1.3  pgoyette */
    204  1.1  pgoyette };
    205  1.1  pgoyette 
    206  1.1  pgoyette int
    207  1.1  pgoyette main(void)
    208  1.1  pgoyette {
    209  1.1  pgoyette 	int fd, res;
    210  1.3  pgoyette 	size_t i;
    211  1.1  pgoyette 	struct session_op cs;
    212  1.1  pgoyette 	struct crypt_op co;
    213  1.3  pgoyette 	unsigned char buf[36];
    214  1.2  pgoyette 
    215  1.3  pgoyette 	for (i = 0; i < __arraycount(tests); i++) {
    216  1.3  pgoyette 		fd = open("/dev/crypto", O_RDWR, 0);
    217  1.3  pgoyette 		if (fd < 0)
    218  1.3  pgoyette 			err(1, "open %zu", i);
    219  1.3  pgoyette 		memset(&cs, 0, sizeof(cs));
    220  1.3  pgoyette 		cs.cipher = CRYPTO_AES_CTR;
    221  1.3  pgoyette 		cs.keylen = tests[i].key_len;
    222  1.4  pgoyette 		cs.key = __UNCONST(&tests[i].key);
    223  1.3  pgoyette 		res = ioctl(fd, CIOCGSESSION, &cs);
    224  1.3  pgoyette 		if (res < 0)
    225  1.3  pgoyette 			err(1, "CIOCGSESSION %zu", i);
    226  1.3  pgoyette 
    227  1.3  pgoyette 		memset(&co, 0, sizeof(co));
    228  1.3  pgoyette 		co.ses = cs.ses;
    229  1.3  pgoyette 		co.op = COP_ENCRYPT;
    230  1.3  pgoyette 		co.len = tests[i].len;
    231  1.4  pgoyette 		co.src = __UNCONST(&tests[i].plaintx);
    232  1.3  pgoyette 		co.dst = buf;
    233  1.3  pgoyette 		co.dst_len = sizeof(buf);
    234  1.4  pgoyette 		co.iv = __UNCONST(&tests[i].iv);
    235  1.3  pgoyette 		res = ioctl(fd, CIOCCRYPT, &co);
    236  1.3  pgoyette 		if (res < 0)
    237  1.3  pgoyette 			err(1, "CIOCCRYPT %zu", i);
    238  1.3  pgoyette 
    239  1.3  pgoyette 		if (memcmp(co.dst, tests[i].ciphertx, tests[i].len)) {
    240  1.3  pgoyette 			size_t j;
    241  1.3  pgoyette 			printf(" Loc  Actual  Golden\n");
    242  1.3  pgoyette 			for (j = 0; j < tests[i].len; j++)
    243  1.3  pgoyette 				printf("0x%2zu:  0x%2x    0x%2x\n", j,
    244  1.3  pgoyette 					buf[j], tests[i].ciphertx[j]);
    245  1.3  pgoyette 			warnx("verification failed %zu", i);
    246  1.3  pgoyette 		}
    247  1.3  pgoyette 		close(fd);
    248  1.3  pgoyette 	}
    249  1.1  pgoyette 	return 0;
    250  1.1  pgoyette }
    251