1 /*- 2 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/malloc.h> 35 #include <sys/libkern.h> 36 #include <sys/endian.h> 37 #if defined(__amd64__) || (defined(__i386__) && !defined(PC98)) 38 #include <machine/cpufunc.h> 39 #include <machine/cputypes.h> 40 #include <machine/md_var.h> 41 #include <machine/specialreg.h> 42 #endif 43 44 #include <opencrypto/cryptodev.h> 45 #include <opencrypto/cryptosoft.h> /* for hmac_ipad_buffer and hmac_opad_buffer */ 46 #include <opencrypto/xform.h> 47 48 #include <crypto/via/padlock.h> 49 50 /* 51 * Implementation notes. 52 * 53 * Some VIA CPUs provides SHA1 and SHA256 acceleration. 54 * We implement all HMAC algorithms provided by crypto(9) framework, but we do 55 * the crypto work in software unless this is HMAC/SHA1 or HMAC/SHA256 and 56 * our CPU can accelerate it. 57 * 58 * Additional CPU instructions, which preform SHA1 and SHA256 are one-shot 59 * functions - we have only one chance to give the data, CPU itself will add 60 * the padding and calculate hash automatically. 61 * This means, it is not possible to implement common init(), update(), final() 62 * methods. 63 * The way I've choosen is to keep adding data to the buffer on update() 64 * (reallocating the buffer if necessary) and call XSHA{1,256} instruction on 65 * final(). 66 */ 67 68 struct padlock_sha_ctx { 69 uint8_t *psc_buf; 70 int psc_offset; 71 int psc_size; 72 }; 73 CTASSERT(sizeof(struct padlock_sha_ctx) <= sizeof(union authctx)); 74 75 static void padlock_sha_init(struct padlock_sha_ctx *ctx); 76 static int padlock_sha_update(struct padlock_sha_ctx *ctx, uint8_t *buf, 77 uint16_t bufsize); 78 static void padlock_sha1_final(uint8_t *hash, struct padlock_sha_ctx *ctx); 79 static void padlock_sha256_final(uint8_t *hash, struct padlock_sha_ctx *ctx); 80 81 static struct auth_hash padlock_hmac_sha1 = { 82 CRYPTO_SHA1_HMAC, "HMAC-SHA1", 83 20, SHA1_HASH_LEN, SHA1_HMAC_BLOCK_LEN, sizeof(struct padlock_sha_ctx), 84 (void (*)(void *))padlock_sha_init, 85 (int (*)(void *, uint8_t *, uint16_t))padlock_sha_update, 86 (void (*)(uint8_t *, void *))padlock_sha1_final 87 }; 88 89 static struct auth_hash padlock_hmac_sha256 = { 90 CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256", 91 32, SHA2_256_HASH_LEN, SHA2_256_HMAC_BLOCK_LEN, sizeof(struct padlock_sha_ctx), 92 (void (*)(void *))padlock_sha_init, 93 (int (*)(void *, uint8_t *, uint16_t))padlock_sha_update, 94 (void (*)(uint8_t *, void *))padlock_sha256_final 95 }; 96 97 MALLOC_DECLARE(M_PADLOCK); 98 99 static __inline void 100 padlock_output_block(uint32_t *src, uint32_t *dst, size_t count) 101 { 102 103 while (count-- > 0) 104 *dst++ = bswap32(*src++); 105 } 106 107 static void 108 padlock_do_sha1(const u_char *in, u_char *out, int count) 109 { 110 u_char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */ 111 u_char *result = PADLOCK_ALIGN(buf); 112 113 ((uint32_t *)result)[0] = 0x67452301; 114 ((uint32_t *)result)[1] = 0xEFCDAB89; 115 ((uint32_t *)result)[2] = 0x98BADCFE; 116 ((uint32_t *)result)[3] = 0x10325476; 117 ((uint32_t *)result)[4] = 0xC3D2E1F0; 118 119 #ifdef __GNUCLIKE_ASM 120 __asm __volatile( 121 ".byte 0xf3, 0x0f, 0xa6, 0xc8" /* rep xsha1 */ 122 : "+S"(in), "+D"(result) 123 : "c"(count), "a"(0) 124 ); 125 #endif 126 127 padlock_output_block((uint32_t *)result, (uint32_t *)out, 128 SHA1_HASH_LEN / sizeof(uint32_t)); 129 } 130 131 static void 132 padlock_do_sha256(const char *in, char *out, int count) 133 { 134 char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */ 135 char *result = PADLOCK_ALIGN(buf); 136 137 ((uint32_t *)result)[0] = 0x6A09E667; 138 ((uint32_t *)result)[1] = 0xBB67AE85; 139 ((uint32_t *)result)[2] = 0x3C6EF372; 140 ((uint32_t *)result)[3] = 0xA54FF53A; 141 ((uint32_t *)result)[4] = 0x510E527F; 142 ((uint32_t *)result)[5] = 0x9B05688C; 143 ((uint32_t *)result)[6] = 0x1F83D9AB; 144 ((uint32_t *)result)[7] = 0x5BE0CD19; 145 146 #ifdef __GNUCLIKE_ASM 147 __asm __volatile( 148 ".byte 0xf3, 0x0f, 0xa6, 0xd0" /* rep xsha256 */ 149 : "+S"(in), "+D"(result) 150 : "c"(count), "a"(0) 151 ); 152 #endif 153 154 padlock_output_block((uint32_t *)result, (uint32_t *)out, 155 SHA2_256_HASH_LEN / sizeof(uint32_t)); 156 } 157 158 static void 159 padlock_sha_init(struct padlock_sha_ctx *ctx) 160 { 161 162 ctx->psc_buf = NULL; 163 ctx->psc_offset = 0; 164 ctx->psc_size = 0; 165 } 166 167 static int 168 padlock_sha_update(struct padlock_sha_ctx *ctx, uint8_t *buf, uint16_t bufsize) 169 { 170 171 if (ctx->psc_size - ctx->psc_offset < bufsize) { 172 ctx->psc_size = MAX(ctx->psc_size * 2, ctx->psc_size + bufsize); 173 ctx->psc_buf = realloc(ctx->psc_buf, ctx->psc_size, M_PADLOCK, 174 M_WAITOK); 175 } 176 bcopy(buf, ctx->psc_buf + ctx->psc_offset, bufsize); 177 ctx->psc_offset += bufsize; 178 return (0); 179 } 180 181 static void 182 padlock_sha_free(struct padlock_sha_ctx *ctx) 183 { 184 185 if (ctx->psc_buf != NULL) { 186 //bzero(ctx->psc_buf, ctx->psc_size); 187 free(ctx->psc_buf, M_PADLOCK); 188 ctx->psc_buf = NULL; 189 ctx->psc_offset = 0; 190 ctx->psc_size = 0; 191 } 192 } 193 194 static void 195 padlock_sha1_final(uint8_t *hash, struct padlock_sha_ctx *ctx) 196 { 197 198 padlock_do_sha1(ctx->psc_buf, hash, ctx->psc_offset); 199 padlock_sha_free(ctx); 200 } 201 202 static void 203 padlock_sha256_final(uint8_t *hash, struct padlock_sha_ctx *ctx) 204 { 205 206 padlock_do_sha256(ctx->psc_buf, hash, ctx->psc_offset); 207 padlock_sha_free(ctx); 208 } 209 210 static void 211 padlock_copy_ctx(struct auth_hash *axf, void *sctx, void *dctx) 212 { 213 214 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && 215 (axf->type == CRYPTO_SHA1_HMAC || 216 axf->type == CRYPTO_SHA2_256_HMAC)) { 217 struct padlock_sha_ctx *spctx = sctx, *dpctx = dctx; 218 219 dpctx->psc_offset = spctx->psc_offset; 220 dpctx->psc_size = spctx->psc_size; 221 dpctx->psc_buf = malloc(dpctx->psc_size, M_PADLOCK, M_WAITOK); 222 bcopy(spctx->psc_buf, dpctx->psc_buf, dpctx->psc_size); 223 } else { 224 bcopy(sctx, dctx, axf->ctxsize); 225 } 226 } 227 228 static void 229 padlock_free_ctx(struct auth_hash *axf, void *ctx) 230 { 231 232 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && 233 (axf->type == CRYPTO_SHA1_HMAC || 234 axf->type == CRYPTO_SHA2_256_HMAC)) { 235 padlock_sha_free(ctx); 236 } 237 } 238 239 static void 240 padlock_hash_key_setup(struct padlock_session *ses, caddr_t key, int klen) 241 { 242 struct auth_hash *axf; 243 int i; 244 245 klen /= 8; 246 axf = ses->ses_axf; 247 248 /* 249 * Try to free contexts before using them, because 250 * padlock_hash_key_setup() can be called twice - once from 251 * padlock_newsession() and again from padlock_process(). 252 */ 253 padlock_free_ctx(axf, ses->ses_ictx); 254 padlock_free_ctx(axf, ses->ses_octx); 255 256 for (i = 0; i < klen; i++) 257 key[i] ^= HMAC_IPAD_VAL; 258 259 axf->Init(ses->ses_ictx); 260 axf->Update(ses->ses_ictx, key, klen); 261 axf->Update(ses->ses_ictx, hmac_ipad_buffer, axf->blocksize - klen); 262 263 for (i = 0; i < klen; i++) 264 key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL); 265 266 axf->Init(ses->ses_octx); 267 axf->Update(ses->ses_octx, key, klen); 268 axf->Update(ses->ses_octx, hmac_opad_buffer, axf->blocksize - klen); 269 270 for (i = 0; i < klen; i++) 271 key[i] ^= HMAC_OPAD_VAL; 272 } 273 274 /* 275 * Compute keyed-hash authenticator. 276 */ 277 static int 278 padlock_authcompute(struct padlock_session *ses, struct cryptodesc *crd, 279 caddr_t buf, int flags) 280 { 281 u_char hash[HASH_MAX_LEN]; 282 struct auth_hash *axf; 283 union authctx ctx; 284 int error; 285 286 axf = ses->ses_axf; 287 288 padlock_copy_ctx(axf, ses->ses_ictx, &ctx); 289 error = crypto_apply(flags, buf, crd->crd_skip, crd->crd_len, 290 (int (*)(void *, void *, unsigned int))axf->Update, (caddr_t)&ctx); 291 if (error != 0) { 292 padlock_free_ctx(axf, &ctx); 293 return (error); 294 } 295 axf->Final(hash, &ctx); 296 297 padlock_copy_ctx(axf, ses->ses_octx, &ctx); 298 axf->Update(&ctx, hash, axf->hashsize); 299 axf->Final(hash, &ctx); 300 301 /* Inject the authentication data */ 302 crypto_copyback(flags, buf, crd->crd_inject, 303 ses->ses_mlen == 0 ? axf->hashsize : ses->ses_mlen, hash); 304 return (0); 305 } 306 307 int 308 padlock_hash_setup(struct padlock_session *ses, struct cryptoini *macini) 309 { 310 311 ses->ses_mlen = macini->cri_mlen; 312 313 /* Find software structure which describes HMAC algorithm. */ 314 switch (macini->cri_alg) { 315 case CRYPTO_NULL_HMAC: 316 ses->ses_axf = &auth_hash_null; 317 break; 318 case CRYPTO_MD5_HMAC: 319 ses->ses_axf = &auth_hash_hmac_md5; 320 break; 321 case CRYPTO_SHA1_HMAC: 322 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0) 323 ses->ses_axf = &padlock_hmac_sha1; 324 else 325 ses->ses_axf = &auth_hash_hmac_sha1; 326 break; 327 case CRYPTO_RIPEMD160_HMAC: 328 ses->ses_axf = &auth_hash_hmac_ripemd_160; 329 break; 330 case CRYPTO_SHA2_256_HMAC: 331 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0) 332 ses->ses_axf = &padlock_hmac_sha256; 333 else 334 ses->ses_axf = &auth_hash_hmac_sha2_256; 335 break; 336 case CRYPTO_SHA2_384_HMAC: 337 ses->ses_axf = &auth_hash_hmac_sha2_384; 338 break; 339 case CRYPTO_SHA2_512_HMAC: 340 ses->ses_axf = &auth_hash_hmac_sha2_512; 341 break; 342 } 343 344 /* Allocate memory for HMAC inner and outer contexts. */ 345 ses->ses_ictx = malloc(ses->ses_axf->ctxsize, M_PADLOCK, 346 M_ZERO | M_NOWAIT); 347 ses->ses_octx = malloc(ses->ses_axf->ctxsize, M_PADLOCK, 348 M_ZERO | M_NOWAIT); 349 if (ses->ses_ictx == NULL || ses->ses_octx == NULL) 350 return (ENOMEM); 351 352 /* Setup key if given. */ 353 if (macini->cri_key != NULL) { 354 padlock_hash_key_setup(ses, macini->cri_key, 355 macini->cri_klen); 356 } 357 return (0); 358 } 359 360 int 361 padlock_hash_process(struct padlock_session *ses, struct cryptodesc *maccrd, 362 struct cryptop *crp) 363 { 364 int error; 365 366 if ((maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) 367 padlock_hash_key_setup(ses, maccrd->crd_key, maccrd->crd_klen); 368 369 error = padlock_authcompute(ses, maccrd, crp->crp_buf, crp->crp_flags); 370 return (error); 371 } 372 373 void 374 padlock_hash_free(struct padlock_session *ses) 375 { 376 377 if (ses->ses_ictx != NULL) { 378 padlock_free_ctx(ses->ses_axf, ses->ses_ictx); 379 bzero(ses->ses_ictx, ses->ses_axf->ctxsize); 380 free(ses->ses_ictx, M_PADLOCK); 381 ses->ses_ictx = NULL; 382 } 383 if (ses->ses_octx != NULL) { 384 padlock_free_ctx(ses->ses_axf, ses->ses_octx); 385 bzero(ses->ses_octx, ses->ses_axf->ctxsize); 386 free(ses->ses_octx, M_PADLOCK); 387 ses->ses_octx = NULL; 388 } 389 } 390