h_sha2hmac.c revision 1.1
1/* $NetBSD: h_sha2hmac.c,v 1.1 2019/12/03 04:20:45 hikaru 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 34#include <sys/ioctl.h> 35#include <sys/time.h> 36 37#include <crypto/cryptodev.h> 38 39/* Test data from RFC4868 */ 40const struct { 41 int num; 42 int alg; 43 size_t key_len; 44 size_t len; 45 size_t mac_len; 46 unsigned char key[80]; 47 unsigned char data[80]; 48 unsigned char mac[80]; 49} tests[] = { 50 { 1, CRYPTO_SHA2_256_HMAC, 32, 8, 16, 51 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 52 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 53 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 54 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }, 55 "Hi There", 56 { 0x19, 0x8a, 0x60, 0x7e, 0xb4, 0x4b, 0xfb, 0xc6, 57 0x99, 0x03, 0xa0, 0xf1, 0xcf, 0x2b, 0xbd, 0xc5 } 58 }, 59 { 2, CRYPTO_SHA2_256_HMAC, 32, 28, 16, 60 "JefeJefeJefeJefe" 61 "JefeJefeJefeJefe", 62 "what do ya want " 63 "for nothing?", 64 { 0x16, 0x7f, 0x92, 0x85, 0x88, 0xc5, 0xcc, 0x2e, 65 0xef, 0x8e, 0x30, 0x93, 0xca, 0xa0, 0xe8, 0x7c } 66 }, 67 { 3, CRYPTO_SHA2_256_HMAC, 32, 50, 16, 68 { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 69 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 70 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 71 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }, 72 { 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 73 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 74 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 75 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 76 0xdd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b, 0x0b, 101 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 102 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 103 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }, 104 "Hi There", 105 { 0xb6, 0xa8, 0xd5, 0x63, 0x6f, 0x5c, 0x6a, 0x72, 106 0x24, 0xf9, 0x97, 0x7d, 0xcf, 0x7e, 0xe6, 0xc7, 107 0xfb, 0x6d, 0x0c, 0x48, 0xcb, 0xde, 0xe9, 0x73 } 108 }, 109 { 6, CRYPTO_SHA2_384_HMAC, 48, 28, 24, 110 "JefeJefeJefeJefe" 111 "JefeJefeJefeJefe" 112 "JefeJefeJefeJefe", 113 "what do ya want " 114 "for nothing?", 115 { 0x2c, 0x73, 0x53, 0x97, 0x4f, 0x18, 0x42, 0xfd, 116 0x66, 0xd5, 0x3c, 0x45, 0x2c, 0xa4, 0x21, 0x22, 117 0xb2, 0x8c, 0x0b, 0x59, 0x4c, 0xfb, 0x18, 0x4d } 118 }, 119 { 7, CRYPTO_SHA2_384_HMAC, 48, 50, 24, 120 { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 121 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 122 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 123 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 124 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 125 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }, 126 { 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 127 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 128 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 129 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 130 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 131 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 132 0xdd, 0xdd }, 133 { 0x80, 0x9f, 0x43, 0x9b, 0xe0, 0x02, 0x74, 0x32, 134 0x1d, 0x4a, 0x53, 0x86, 0x52, 0x16, 0x4b, 0x53, 135 0x55, 0x4a, 0x50, 0x81, 0x84, 0xa0, 0xc3, 0x16 } 136 }, 137 { 8, CRYPTO_SHA2_384_HMAC, 48, 50, 24, 138 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 139 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 140 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 141 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 142 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 143 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 }, 144 { 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 145 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 146 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 147 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 148 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 149 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 150 0xcd, 0xcd }, 151 { 0x5b, 0x54, 0x00, 0x85, 0xc6, 0xe6, 0x35, 0x80, 152 0x96, 0x53, 0x2b, 0x24, 0x93, 0x60, 0x9e, 0xd1, 153 0xcb, 0x29, 0x8f, 0x77, 0x4f, 0x87, 0xbb, 0x5c } 154 }, 155 { 9, CRYPTO_SHA2_512_HMAC, 64, 8, 32, 156 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 157 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 158 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 159 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 160 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 161 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 162 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 163 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }, 164 "Hi There", 165 { 0x63, 0x7e, 0xdc, 0x6e, 0x01, 0xdc, 0xe7, 0xe6, 166 0x74, 0x2a, 0x99, 0x45, 0x1a, 0xae, 0x82, 0xdf, 167 0x23, 0xda, 0x3e, 0x92, 0x43, 0x9e, 0x59, 0x0e, 168 0x43, 0xe7, 0x61, 0xb3, 0x3e, 0x91, 0x0f, 0xb8 } 169 }, 170 { 10, CRYPTO_SHA2_512_HMAC, 64, 28, 32, 171 "JefeJefeJefeJefe" 172 "JefeJefeJefeJefe" 173 "JefeJefeJefeJefe" 174 "JefeJefeJefeJefe", 175 "what do ya want " 176 "for nothing?", 177 { 0xcb, 0x37, 0x09, 0x17, 0xae, 0x8a, 0x7c, 0xe2, 178 0x8c, 0xfd, 0x1d, 0x8f, 0x47, 0x05, 0xd6, 0x14, 179 0x1c, 0x17, 0x3b, 0x2a, 0x93, 0x62, 0xc1, 0x5d, 180 0xf2, 0x35, 0xdf, 0xb2, 0x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x03, 0x04, 0x05, 0x06, 0x07, 0x08, 206 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 207 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 208 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 209 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 210 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 211 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 212 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40 }, 213 { 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 214 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 215 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 216 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 217 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 218 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 219 0xcd, 0xcd }, 220 { 0x5e, 0x66, 0x88, 0xe5, 0xa3, 0xda, 0xec, 0x82, 221 0x6c, 0xa3, 0x2e, 0xae, 0xa2, 0x24, 0xef, 0xf5, 222 0xe7, 0x00, 0x62, 0x89, 0x47, 0x47, 0x0e, 0x13, 223 0xad, 0x01, 0x30, 0x25, 0x61, 0xba, 0xb1, 0x08 } 224 }, 225}; 226 227int 228main(void) 229{ 230 size_t i; 231 int fd, res; 232 struct session_op cs; 233 struct crypt_op co; 234 unsigned char buf[80]; 235 236 fd = open("/dev/crypto", O_RDWR, 0); 237 if (fd < 0) 238 err(1, "open"); 239 for (i = 0; i < __arraycount(tests); i++) { 240 memset(&cs, 0, sizeof(cs)); 241 cs.mac = tests[i].alg; 242 cs.mackeylen = tests[i].key_len; 243 cs.mackey = __UNCONST(&tests[i].key); 244 res = ioctl(fd, CIOCGSESSION, &cs); 245 if (res < 0) 246 err(1, "CIOCGSESSION test %d", tests[i].num); 247 248 memset(&co, 0, sizeof(co)); 249 memset(buf, 0, sizeof(buf)); 250 co.ses = cs.ses; 251 co.op = COP_ENCRYPT; 252 co.len = tests[i].len; 253 co.src = __UNCONST(&tests[i].data); 254 co.mac = buf; 255 res = ioctl(fd, CIOCCRYPT, &co); 256 if (res < 0) 257 err(1, "CIOCCRYPT test %d", tests[i].num); 258 259 /* compare with trailing zeros */ 260 if (memcmp(co.mac, &tests[i].mac, sizeof(tests[i].mac))) 261 errx(1, "verification failed test %d", tests[i].num); 262 263 res = ioctl(fd, CIOCFSESSION, &cs.ses); 264 if (res < 0) 265 err(1, "CIOCFSESSION test %d", tests[i].num); 266 } 267 return 0; 268} 269