xref: /netbsd-src/tests/crypto/opencrypto/h_aesctr1.c (revision 0746ac4120f6a26d004eaf92e7edf980aa30085e)
1*0746ac41Spgoyette /* $NetBSD: h_aesctr1.c,v 1.4 2014/01/19 13:40:59 pgoyette Exp $ */
29bee98a7Spgoyette 
39bee98a7Spgoyette /*-
49bee98a7Spgoyette  * Copyright (c) 2014 The NetBSD Foundation, Inc.
59bee98a7Spgoyette  * All rights reserved.
69bee98a7Spgoyette  *
79bee98a7Spgoyette  * Redistribution and use in source and binary forms, with or without
89bee98a7Spgoyette  * modification, are permitted provided that the following conditions
99bee98a7Spgoyette  * are met:
109bee98a7Spgoyette  * 1. Redistributions of source code must retain the above copyright
119bee98a7Spgoyette  *    notice, this list of conditions and the following disclaimer.
129bee98a7Spgoyette  * 2. Redistributions in binary form must reproduce the above copyright
139bee98a7Spgoyette  *    notice, this list of conditions and the following disclaimer in the
149bee98a7Spgoyette  *    documentation and/or other materials provided with the distribution.
159bee98a7Spgoyette  *
169bee98a7Spgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
179bee98a7Spgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
189bee98a7Spgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
199bee98a7Spgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
209bee98a7Spgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
219bee98a7Spgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
229bee98a7Spgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
239bee98a7Spgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
249bee98a7Spgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
259bee98a7Spgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
269bee98a7Spgoyette  * POSSIBILITY OF SUCH DAMAGE.
279bee98a7Spgoyette  */
289bee98a7Spgoyette 
299bee98a7Spgoyette #include <err.h>
309bee98a7Spgoyette #include <fcntl.h>
319bee98a7Spgoyette #include <stdio.h>
329bee98a7Spgoyette #include <string.h>
335e565c16Spgoyette #include <unistd.h>
349bee98a7Spgoyette 
359bee98a7Spgoyette #include <sys/ioctl.h>
369bee98a7Spgoyette #include <sys/time.h>
379bee98a7Spgoyette 
389bee98a7Spgoyette #include <crypto/cryptodev.h>
399bee98a7Spgoyette 
405e565c16Spgoyette /*
415e565c16Spgoyette  * Test vectors from RFC 3686
425e565c16Spgoyette  *
435e565c16Spgoyette  * Test vectors 3, 6, and 9 are disabled because we don't support
445e565c16Spgoyette  * 36-byte (ie, unpadded) operations.
455e565c16Spgoyette  */
465e565c16Spgoyette 
475e565c16Spgoyette const struct {
485e565c16Spgoyette 	size_t len;
495e565c16Spgoyette 	size_t key_len;
505e565c16Spgoyette 	unsigned char key[36];		/* Includes 32-bit nonce */
515e565c16Spgoyette 	unsigned char iv[8];
525e565c16Spgoyette 	unsigned char plaintx[36];
535e565c16Spgoyette 	unsigned char ciphertx[36];
545e565c16Spgoyette } tests[] = {
555e565c16Spgoyette 	/* Test Vector #1: Encrypting 16 octets using AES-CTR w/ 128-bit key*/
565e565c16Spgoyette 	{ 16, 20,
575e565c16Spgoyette 	  { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
585e565c16Spgoyette 	    0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E,
595e565c16Spgoyette 	    0x00, 0x00, 0x00, 0x30 },
605e565c16Spgoyette 	  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
615e565c16Spgoyette 	  { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
625e565c16Spgoyette 	    0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
635e565c16Spgoyette 	  { 0xE4, 0x09, 0x5D, 0x4F, 0xB7, 0xA7, 0xB3, 0x79,
645e565c16Spgoyette 	    0x2D, 0x61, 0x75, 0xA3, 0x26, 0x13, 0x11, 0xB8 }
655e565c16Spgoyette 	},
665e565c16Spgoyette 
675e565c16Spgoyette 	/* Test Vector #2: Encrypting 32 octets using AES-CTR w/ 128-bit key */
685e565c16Spgoyette 	{ 32, 20,
695e565c16Spgoyette 	  { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
705e565c16Spgoyette 	    0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63,
715e565c16Spgoyette 	    0x00, 0x6C, 0xB6, 0xDB },
725e565c16Spgoyette 	  { 0xC0, 0x54, 0x3B, 0x59, 0xDA, 0x48, 0xD9, 0x0B },
735e565c16Spgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
745e565c16Spgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
755e565c16Spgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
765e565c16Spgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
775e565c16Spgoyette 	  { 0x51, 0x04, 0xA1, 0x06, 0x16, 0x8A, 0x72, 0xD9,
785e565c16Spgoyette 	    0x79, 0x0D, 0x41, 0xEE, 0x8E, 0xDA, 0xD3, 0x88,
795e565c16Spgoyette 	    0xEB, 0x2E, 0x1E, 0xFC, 0x46, 0xDA, 0x57, 0xC8,
805e565c16Spgoyette 	    0xFC, 0xE6, 0x30, 0xDF, 0x91, 0x41, 0xBE, 0x28 }
815e565c16Spgoyette 	},
825e565c16Spgoyette 
835e565c16Spgoyette 	/* Test Vector #3: Encrypting 36 octets using AES-CTR w/ 128-bit key */
845e565c16Spgoyette /*	{ 36, 20,
855e565c16Spgoyette 	  { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
865e565c16Spgoyette 	    0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC,
875e565c16Spgoyette 	    0x00, 0xE0, 0x01, 0x7B },
885e565c16Spgoyette 	  { 0x27, 0x77, 0x7F, 0x3F, 0x4A, 0x17, 0x86, 0xF0 },
895e565c16Spgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
905e565c16Spgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
915e565c16Spgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
925e565c16Spgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
935e565c16Spgoyette 	    0x20, 0x21, 0x22, 0x23 },
945e565c16Spgoyette 	  { 0xC1, 0xCF, 0x48, 0xA8, 0x9F, 0x2F, 0xFD, 0xD9,
955e565c16Spgoyette 	    0xCF, 0x46, 0x52, 0xE9, 0xEF, 0xDB, 0x72, 0xD7,
965e565c16Spgoyette 	    0x45, 0x40, 0xA4, 0x2B, 0xDE, 0x6D, 0x78, 0x36,
975e565c16Spgoyette 	    0xD5, 0x9A, 0x5C, 0xEA, 0xAE, 0xF3, 0x10, 0x53,
985e565c16Spgoyette 	    0x25, 0xB2, 0x07, 0x2F }
995e565c16Spgoyette 	},
1005e565c16Spgoyette */
1015e565c16Spgoyette 	/* Test Vector #4: Encrypting 16 octets using AES-CTR w/ 192-bit key */
1025e565c16Spgoyette 	{ 16, 28,
1035e565c16Spgoyette 	  { 0x16, 0xAF, 0x5B, 0x14, 0x5F, 0xC9, 0xF5, 0x79,
1045e565c16Spgoyette 	    0xC1, 0x75, 0xF9, 0x3E, 0x3B, 0xFB, 0x0E, 0xED,
1055e565c16Spgoyette 	    0x86, 0x3D, 0x06, 0xCC, 0xFD, 0xB7, 0x85, 0x15,
1065e565c16Spgoyette 	    0x00, 0x00, 0x00, 0x48 },
1075e565c16Spgoyette 	  { 0x36, 0x73, 0x3C, 0x14, 0x7D, 0x6D, 0x93, 0xCB },
1085e565c16Spgoyette 	  { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
1095e565c16Spgoyette 	    0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
1105e565c16Spgoyette 	  { 0x4B, 0x55, 0x38, 0x4F, 0xE2, 0x59, 0xC9, 0xC8,
1115e565c16Spgoyette 	    0x4E, 0x79, 0x35, 0xA0, 0x03, 0xCB, 0xE9, 0x28 }
1125e565c16Spgoyette 	},
1135e565c16Spgoyette 
1145e565c16Spgoyette 	/* Test Vector #5: Encrypting 32 octets using AES-CTR w/ 192-bit key */
1155e565c16Spgoyette 	{ 32, 28,
1165e565c16Spgoyette 	  { 0x7C, 0x5C, 0xB2, 0x40, 0x1B, 0x3D, 0xC3, 0x3C,
1175e565c16Spgoyette 	    0x19, 0xE7, 0x34, 0x08, 0x19, 0xE0, 0xF6, 0x9C,
1185e565c16Spgoyette 	    0x67, 0x8C, 0x3D, 0xB8, 0xE6, 0xF6, 0xA9, 0x1A,
1195e565c16Spgoyette 	    0x00, 0x96, 0xB0, 0x3B },
1205e565c16Spgoyette 	  { 0x02, 0x0C, 0x6E, 0xAD, 0xC2, 0xCB, 0x50, 0x0D },
1215e565c16Spgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1225e565c16Spgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1235e565c16Spgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1245e565c16Spgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
1255e565c16Spgoyette 	  { 0x45, 0x32, 0x43, 0xFC, 0x60, 0x9B, 0x23, 0x32,
1265e565c16Spgoyette 	    0x7E, 0xDF, 0xAA, 0xFA, 0x71, 0x31, 0xCD, 0x9F,
1275e565c16Spgoyette 	    0x84, 0x90, 0x70, 0x1C, 0x5A, 0xD4, 0xA7, 0x9C,
1285e565c16Spgoyette 	    0xFC, 0x1F, 0xE0, 0xFF, 0x42, 0xF4, 0xFB, 0x00 }
1295e565c16Spgoyette 	},
1305e565c16Spgoyette 
1315e565c16Spgoyette 	/* Test Vector #6: Encrypting 36 octets using AES-CTR w/ 192-bit key */
1325e565c16Spgoyette /*
1335e565c16Spgoyette 	{ 36, 28,
1345e565c16Spgoyette 	  { 0x02, 0xBF, 0x39, 0x1E, 0xE8, 0xEC, 0xB1, 0x59,
1355e565c16Spgoyette 	    0xB9, 0x59, 0x61, 0x7B, 0x09, 0x65, 0x27, 0x9B,
1365e565c16Spgoyette 	    0xF5, 0x9B, 0x60, 0xA7, 0x86, 0xD3, 0xE0, 0xFE,
1375e565c16Spgoyette 	    0x00, 0x07, 0xBD, 0xFD },
1385e565c16Spgoyette 	  { 0x5C, 0xBD, 0x60, 0x27, 0x8D, 0xCC, 0x09, 0x12 },
1395e565c16Spgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1405e565c16Spgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1415e565c16Spgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1425e565c16Spgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
1435e565c16Spgoyette 	    0x20, 0x21, 0x22, 0x23 },
1445e565c16Spgoyette 	  { 0x96, 0x89, 0x3F, 0xC5, 0x5E, 0x5C, 0x72, 0x2F,
1455e565c16Spgoyette 	    0x54, 0x0B, 0x7D, 0xD1, 0xDD, 0xF7, 0xE7, 0x58,
1465e565c16Spgoyette 	    0xD2, 0x88, 0xBC, 0x95, 0xC6, 0x91, 0x65, 0x88,
1475e565c16Spgoyette 	    0x45, 0x36, 0xC8, 0x11, 0x66, 0x2F, 0x21, 0x88,
1485e565c16Spgoyette 	    0xAB, 0xEE, 0x09, 0x35 },
1495e565c16Spgoyette 	},
1505e565c16Spgoyette */
1515e565c16Spgoyette 	/* Test Vector #7: Encrypting 16 octets using AES-CTR w/ 256-bit key */
1525e565c16Spgoyette 	{ 16, 36,
1535e565c16Spgoyette 	  { 0x77, 0x6B, 0xEF, 0xF2, 0x85, 0x1D, 0xB0, 0x6F,
1545e565c16Spgoyette 	    0x4C, 0x8A, 0x05, 0x42, 0xC8, 0x69, 0x6F, 0x6C,
1555e565c16Spgoyette 	    0x6A, 0x81, 0xAF, 0x1E, 0xEC, 0x96, 0xB4, 0xD3,
1565e565c16Spgoyette 	    0x7F, 0xC1, 0xD6, 0x89, 0xE6, 0xC1, 0xC1, 0x04,
1575e565c16Spgoyette 	    0x00, 0x00, 0x00, 0x60 },
1585e565c16Spgoyette 	  { 0xDB, 0x56, 0x72, 0xC9, 0x7A, 0xA8, 0xF0, 0xB2 },
1595e565c16Spgoyette 	  { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
1605e565c16Spgoyette 	    0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
1615e565c16Spgoyette 	  { 0x14, 0x5A, 0xD0, 0x1D, 0xBF, 0x82, 0x4E, 0xC7,
1625e565c16Spgoyette 	    0x56, 0x08, 0x63, 0xDC, 0x71, 0xE3, 0xE0, 0xC0 },
1635e565c16Spgoyette 	},
1645e565c16Spgoyette 
1655e565c16Spgoyette 	/* Test Vector #8: Encrypting 32 octets using AES-CTR w/ 256-bit key */
1665e565c16Spgoyette 	{ 32, 36,
1675e565c16Spgoyette 	  { 0xF6, 0xD6, 0x6D, 0x6B, 0xD5, 0x2D, 0x59, 0xBB,
1685e565c16Spgoyette 	    0x07, 0x96, 0x36, 0x58, 0x79, 0xEF, 0xF8, 0x86,
1695e565c16Spgoyette 	    0xC6, 0x6D, 0xD5, 0x1A, 0x5B, 0x6A, 0x99, 0x74,
1705e565c16Spgoyette 	    0x4B, 0x50, 0x59, 0x0C, 0x87, 0xA2, 0x38, 0x84,
1715e565c16Spgoyette 	    0x00, 0xFA, 0xAC, 0x24 },
1725e565c16Spgoyette 	  { 0xC1, 0x58, 0x5E, 0xF1, 0x5A, 0x43, 0xD8, 0x75 },
1735e565c16Spgoyette 	  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1745e565c16Spgoyette 	    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1755e565c16Spgoyette 	    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1765e565c16Spgoyette 	    0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
1775e565c16Spgoyette 	  { 0xF0, 0x5E, 0x23, 0x1B, 0x38, 0x94, 0x61, 0x2C,
1785e565c16Spgoyette 	    0x49, 0xEE, 0x00, 0x0B, 0x80, 0x4E, 0xB2, 0xA9,
1795e565c16Spgoyette 	    0xB8, 0x30, 0x6B, 0x50, 0x8F, 0x83, 0x9D, 0x6A,
1805e565c16Spgoyette 	    0x55, 0x30, 0x83, 0x1D, 0x93, 0x44, 0xAF, 0x1C },
1815e565c16Spgoyette 	},
1825e565c16Spgoyette 
1835e565c16Spgoyette 	/* Test Vector #9: Encrypting 36 octets using AES-CTR w/ 256-bit key */
1845e565c16Spgoyette /*
1855e565c16Spgoyette 	{ 36, 36,
1865e565c16Spgoyette 	  { 0xFF 0x7A 0x61 0x7C 0xE6 0x91 0x48 0xE4,
1875e565c16Spgoyette 	    0xF1 0x72 0x6E 0x2F 0x43 0x58 0x1D 0xE2,
1885e565c16Spgoyette 	    0xAA 0x62 0xD9 0xF8 0x05 0x53 0x2E 0xDF,
1895e565c16Spgoyette 	    0xF1 0xEE 0xD6 0x87 0xFB 0x54 0x15 0x3D,
1905e565c16Spgoyette 	    0x00 0x1C 0xC5 0xB7 },
1915e565c16Spgoyette 	  { 0x51 0xA5 0x1D 0x70 0xA1 0xC1 0x11 0x48 },
1925e565c16Spgoyette 	  { 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07,
1935e565c16Spgoyette 	    0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F,
1945e565c16Spgoyette 	    0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17,
1955e565c16Spgoyette 	    0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F,
1965e565c16Spgoyette 	    0x20 0x21 0x22 0x23 },
1975e565c16Spgoyette 	  { 0xEB 0x6C 0x52 0x82 0x1D 0x0B 0xBB 0xF7,
1985e565c16Spgoyette 	    0xCE 0x75 0x94 0x46 0x2A 0xCA 0x4F 0xAA,
1995e565c16Spgoyette 	    0xB4 0x07 0xDF 0x86 0x65 0x69 0xFD 0x07,
2005e565c16Spgoyette 	    0xF4 0x8C 0xC0 0xB5 0x83 0xD6 0x07 0x1F,
2015e565c16Spgoyette 	    0x1E 0xC0 0xE6 0xB8 },
2025e565c16Spgoyette 	},
2035e565c16Spgoyette */
2049bee98a7Spgoyette };
2059bee98a7Spgoyette 
2069bee98a7Spgoyette int
main(void)2079bee98a7Spgoyette main(void)
2089bee98a7Spgoyette {
2099bee98a7Spgoyette 	int fd, res;
2105e565c16Spgoyette 	size_t i;
2119bee98a7Spgoyette 	struct session_op cs;
2129bee98a7Spgoyette 	struct crypt_op co;
2135e565c16Spgoyette 	unsigned char buf[36];
2149bee98a7Spgoyette 
2155e565c16Spgoyette 	for (i = 0; i < __arraycount(tests); i++) {
2169bee98a7Spgoyette 		fd = open("/dev/crypto", O_RDWR, 0);
2179bee98a7Spgoyette 		if (fd < 0)
2185e565c16Spgoyette 			err(1, "open %zu", i);
2199bee98a7Spgoyette 		memset(&cs, 0, sizeof(cs));
2209bee98a7Spgoyette 		cs.cipher = CRYPTO_AES_CTR;
2215e565c16Spgoyette 		cs.keylen = tests[i].key_len;
222*0746ac41Spgoyette 		cs.key = __UNCONST(&tests[i].key);
2239bee98a7Spgoyette 		res = ioctl(fd, CIOCGSESSION, &cs);
2249bee98a7Spgoyette 		if (res < 0)
2255e565c16Spgoyette 			err(1, "CIOCGSESSION %zu", i);
2269bee98a7Spgoyette 
2279bee98a7Spgoyette 		memset(&co, 0, sizeof(co));
2289bee98a7Spgoyette 		co.ses = cs.ses;
2299bee98a7Spgoyette 		co.op = COP_ENCRYPT;
2305e565c16Spgoyette 		co.len = tests[i].len;
231*0746ac41Spgoyette 		co.src = __UNCONST(&tests[i].plaintx);
2329bee98a7Spgoyette 		co.dst = buf;
2339bee98a7Spgoyette 		co.dst_len = sizeof(buf);
234*0746ac41Spgoyette 		co.iv = __UNCONST(&tests[i].iv);
2359bee98a7Spgoyette 		res = ioctl(fd, CIOCCRYPT, &co);
2369bee98a7Spgoyette 		if (res < 0)
2375e565c16Spgoyette 			err(1, "CIOCCRYPT %zu", i);
2381b0aa2a2Spgoyette 
2395e565c16Spgoyette 		if (memcmp(co.dst, tests[i].ciphertx, tests[i].len)) {
2405e565c16Spgoyette 			size_t j;
2415e565c16Spgoyette 			printf(" Loc  Actual  Golden\n");
2425e565c16Spgoyette 			for (j = 0; j < tests[i].len; j++)
2435e565c16Spgoyette 				printf("0x%2zu:  0x%2x    0x%2x\n", j,
2445e565c16Spgoyette 					buf[j], tests[i].ciphertx[j]);
2455e565c16Spgoyette 			warnx("verification failed %zu", i);
2465e565c16Spgoyette 		}
2475e565c16Spgoyette 		close(fd);
2485e565c16Spgoyette 	}
2499bee98a7Spgoyette 	return 0;
2509bee98a7Spgoyette }
251