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