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 #include <sys/pcpu.h> 38 #if defined(__amd64__) || defined(__i386__) 39 #include <machine/cpufunc.h> 40 #include <machine/cputypes.h> 41 #include <machine/md_var.h> 42 #include <machine/specialreg.h> 43 #endif 44 #include <machine/pcb.h> 45 46 #include <opencrypto/cryptodev.h> 47 #include <opencrypto/xform.h> 48 49 #include <crypto/via/padlock.h> 50 51 /* 52 * Implementation notes. 53 * 54 * Some VIA CPUs provides SHA1 and SHA256 acceleration. 55 * We implement all HMAC algorithms provided by crypto(9) framework, but we do 56 * the crypto work in software unless this is HMAC/SHA1 or HMAC/SHA256 and 57 * our CPU can accelerate it. 58 * 59 * Additional CPU instructions, which preform SHA1 and SHA256 are one-shot 60 * functions - we have only one chance to give the data, CPU itself will add 61 * the padding and calculate hash automatically. 62 * This means, it is not possible to implement common init(), update(), final() 63 * methods. 64 * The way I've choosen is to keep adding data to the buffer on update() 65 * (reallocating the buffer if necessary) and call XSHA{1,256} instruction on 66 * final(). 67 */ 68 69 struct padlock_sha_ctx { 70 uint8_t *psc_buf; 71 int psc_offset; 72 int psc_size; 73 }; 74 CTASSERT(sizeof(struct padlock_sha_ctx) <= sizeof(union authctx)); 75 76 static void padlock_sha_init(void *vctx); 77 static int padlock_sha_update(void *vctx, const void *buf, u_int bufsize); 78 static void padlock_sha1_final(uint8_t *hash, void *vctx); 79 static void padlock_sha256_final(uint8_t *hash, void *vctx); 80 81 static struct auth_hash padlock_hmac_sha1 = { 82 .type = CRYPTO_SHA1_HMAC, 83 .name = "HMAC-SHA1", 84 .keysize = SHA1_BLOCK_LEN, 85 .hashsize = SHA1_HASH_LEN, 86 .ctxsize = sizeof(struct padlock_sha_ctx), 87 .blocksize = SHA1_BLOCK_LEN, 88 .Init = padlock_sha_init, 89 .Update = padlock_sha_update, 90 .Final = padlock_sha1_final, 91 }; 92 93 static struct auth_hash padlock_hmac_sha256 = { 94 .type = CRYPTO_SHA2_256_HMAC, 95 .name = "HMAC-SHA2-256", 96 .keysize = SHA2_256_BLOCK_LEN, 97 .hashsize = SHA2_256_HASH_LEN, 98 .ctxsize = sizeof(struct padlock_sha_ctx), 99 .blocksize = SHA2_256_BLOCK_LEN, 100 .Init = padlock_sha_init, 101 .Update = padlock_sha_update, 102 .Final = padlock_sha256_final, 103 }; 104 105 MALLOC_DECLARE(M_PADLOCK); 106 107 static __inline void 108 padlock_output_block(uint32_t *src, uint32_t *dst, size_t count) 109 { 110 111 while (count-- > 0) 112 *dst++ = bswap32(*src++); 113 } 114 115 static void 116 padlock_do_sha1(const u_char *in, u_char *out, int count) 117 { 118 u_char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */ 119 u_char *result = PADLOCK_ALIGN(buf); 120 121 ((uint32_t *)result)[0] = 0x67452301; 122 ((uint32_t *)result)[1] = 0xEFCDAB89; 123 ((uint32_t *)result)[2] = 0x98BADCFE; 124 ((uint32_t *)result)[3] = 0x10325476; 125 ((uint32_t *)result)[4] = 0xC3D2E1F0; 126 127 #ifdef __GNUCLIKE_ASM 128 __asm __volatile( 129 ".byte 0xf3, 0x0f, 0xa6, 0xc8" /* rep xsha1 */ 130 : "+S"(in), "+D"(result) 131 : "c"(count), "a"(0) 132 ); 133 #endif 134 135 padlock_output_block((uint32_t *)result, (uint32_t *)out, 136 SHA1_HASH_LEN / sizeof(uint32_t)); 137 } 138 139 static void 140 padlock_do_sha256(const char *in, char *out, int count) 141 { 142 char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */ 143 char *result = PADLOCK_ALIGN(buf); 144 145 ((uint32_t *)result)[0] = 0x6A09E667; 146 ((uint32_t *)result)[1] = 0xBB67AE85; 147 ((uint32_t *)result)[2] = 0x3C6EF372; 148 ((uint32_t *)result)[3] = 0xA54FF53A; 149 ((uint32_t *)result)[4] = 0x510E527F; 150 ((uint32_t *)result)[5] = 0x9B05688C; 151 ((uint32_t *)result)[6] = 0x1F83D9AB; 152 ((uint32_t *)result)[7] = 0x5BE0CD19; 153 154 #ifdef __GNUCLIKE_ASM 155 __asm __volatile( 156 ".byte 0xf3, 0x0f, 0xa6, 0xd0" /* rep xsha256 */ 157 : "+S"(in), "+D"(result) 158 : "c"(count), "a"(0) 159 ); 160 #endif 161 162 padlock_output_block((uint32_t *)result, (uint32_t *)out, 163 SHA2_256_HASH_LEN / sizeof(uint32_t)); 164 } 165 166 static void 167 padlock_sha_init(void *vctx) 168 { 169 struct padlock_sha_ctx *ctx; 170 171 ctx = vctx; 172 ctx->psc_buf = NULL; 173 ctx->psc_offset = 0; 174 ctx->psc_size = 0; 175 } 176 177 static int 178 padlock_sha_update(void *vctx, const void *buf, u_int bufsize) 179 { 180 struct padlock_sha_ctx *ctx; 181 182 ctx = vctx; 183 if (ctx->psc_size - ctx->psc_offset < bufsize) { 184 ctx->psc_size = MAX(ctx->psc_size * 2, ctx->psc_size + bufsize); 185 ctx->psc_buf = realloc(ctx->psc_buf, ctx->psc_size, M_PADLOCK, 186 M_NOWAIT); 187 if(ctx->psc_buf == NULL) 188 return (ENOMEM); 189 } 190 bcopy(buf, ctx->psc_buf + ctx->psc_offset, bufsize); 191 ctx->psc_offset += bufsize; 192 return (0); 193 } 194 195 static void 196 padlock_sha_free(void *vctx) 197 { 198 struct padlock_sha_ctx *ctx; 199 200 ctx = vctx; 201 if (ctx->psc_buf != NULL) { 202 //bzero(ctx->psc_buf, ctx->psc_size); 203 free(ctx->psc_buf, M_PADLOCK); 204 ctx->psc_buf = NULL; 205 ctx->psc_offset = 0; 206 ctx->psc_size = 0; 207 } 208 } 209 210 static void 211 padlock_sha1_final(uint8_t *hash, void *vctx) 212 { 213 struct padlock_sha_ctx *ctx; 214 215 ctx = vctx; 216 padlock_do_sha1(ctx->psc_buf, hash, ctx->psc_offset); 217 padlock_sha_free(ctx); 218 } 219 220 static void 221 padlock_sha256_final(uint8_t *hash, void *vctx) 222 { 223 struct padlock_sha_ctx *ctx; 224 225 ctx = vctx; 226 padlock_do_sha256(ctx->psc_buf, hash, ctx->psc_offset); 227 padlock_sha_free(ctx); 228 } 229 230 static void 231 padlock_copy_ctx(struct auth_hash *axf, void *sctx, void *dctx) 232 { 233 234 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && 235 (axf->type == CRYPTO_SHA1_HMAC || 236 axf->type == CRYPTO_SHA2_256_HMAC)) { 237 struct padlock_sha_ctx *spctx = sctx, *dpctx = dctx; 238 239 dpctx->psc_offset = spctx->psc_offset; 240 dpctx->psc_size = spctx->psc_size; 241 dpctx->psc_buf = malloc(dpctx->psc_size, M_PADLOCK, M_WAITOK); 242 bcopy(spctx->psc_buf, dpctx->psc_buf, dpctx->psc_size); 243 } else { 244 bcopy(sctx, dctx, axf->ctxsize); 245 } 246 } 247 248 static void 249 padlock_free_ctx(struct auth_hash *axf, void *ctx) 250 { 251 252 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && 253 (axf->type == CRYPTO_SHA1_HMAC || 254 axf->type == CRYPTO_SHA2_256_HMAC)) { 255 padlock_sha_free(ctx); 256 } 257 } 258 259 static void 260 padlock_hash_key_setup(struct padlock_session *ses, const uint8_t *key, 261 int klen) 262 { 263 struct auth_hash *axf; 264 265 axf = ses->ses_axf; 266 267 /* 268 * Try to free contexts before using them, because 269 * padlock_hash_key_setup() can be called twice - once from 270 * padlock_newsession() and again from padlock_process(). 271 */ 272 padlock_free_ctx(axf, ses->ses_ictx); 273 padlock_free_ctx(axf, ses->ses_octx); 274 275 hmac_init_ipad(axf, key, klen, ses->ses_ictx); 276 hmac_init_opad(axf, key, klen, ses->ses_octx); 277 } 278 279 /* 280 * Compute keyed-hash authenticator. 281 */ 282 static int 283 padlock_authcompute(struct padlock_session *ses, struct cryptop *crp) 284 { 285 u_char hash[HASH_MAX_LEN], hash2[HASH_MAX_LEN]; 286 struct auth_hash *axf; 287 union authctx ctx; 288 int error; 289 290 axf = ses->ses_axf; 291 292 padlock_copy_ctx(axf, ses->ses_ictx, &ctx); 293 error = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length, 294 axf->Update, &ctx); 295 if (error != 0) { 296 padlock_free_ctx(axf, &ctx); 297 return (error); 298 } 299 error = crypto_apply(crp, crp->crp_payload_start, 300 crp->crp_payload_length, axf->Update, &ctx); 301 if (error != 0) { 302 padlock_free_ctx(axf, &ctx); 303 return (error); 304 } 305 axf->Final(hash, &ctx); 306 307 padlock_copy_ctx(axf, ses->ses_octx, &ctx); 308 axf->Update(&ctx, hash, axf->hashsize); 309 axf->Final(hash, &ctx); 310 311 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) { 312 crypto_copydata(crp, crp->crp_digest_start, ses->ses_mlen, 313 hash2); 314 if (timingsafe_bcmp(hash, hash2, ses->ses_mlen) != 0) 315 return (EBADMSG); 316 } else 317 crypto_copyback(crp, crp->crp_digest_start, ses->ses_mlen, 318 hash); 319 return (0); 320 } 321 322 /* Find software structure which describes HMAC algorithm. */ 323 static struct auth_hash * 324 padlock_hash_lookup(int alg) 325 { 326 struct auth_hash *axf; 327 328 switch (alg) { 329 case CRYPTO_NULL_HMAC: 330 axf = &auth_hash_null; 331 break; 332 case CRYPTO_SHA1_HMAC: 333 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0) 334 axf = &padlock_hmac_sha1; 335 else 336 axf = &auth_hash_hmac_sha1; 337 break; 338 case CRYPTO_RIPEMD160_HMAC: 339 axf = &auth_hash_hmac_ripemd_160; 340 break; 341 case CRYPTO_SHA2_256_HMAC: 342 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0) 343 axf = &padlock_hmac_sha256; 344 else 345 axf = &auth_hash_hmac_sha2_256; 346 break; 347 case CRYPTO_SHA2_384_HMAC: 348 axf = &auth_hash_hmac_sha2_384; 349 break; 350 case CRYPTO_SHA2_512_HMAC: 351 axf = &auth_hash_hmac_sha2_512; 352 break; 353 default: 354 axf = NULL; 355 break; 356 } 357 return (axf); 358 } 359 360 bool 361 padlock_hash_check(const struct crypto_session_params *csp) 362 { 363 364 return (padlock_hash_lookup(csp->csp_auth_alg) != NULL); 365 } 366 367 int 368 padlock_hash_setup(struct padlock_session *ses, 369 const struct crypto_session_params *csp) 370 { 371 372 ses->ses_axf = padlock_hash_lookup(csp->csp_auth_alg); 373 if (csp->csp_auth_mlen == 0) 374 ses->ses_mlen = ses->ses_axf->hashsize; 375 else 376 ses->ses_mlen = csp->csp_auth_mlen; 377 378 /* Allocate memory for HMAC inner and outer contexts. */ 379 ses->ses_ictx = malloc(ses->ses_axf->ctxsize, M_PADLOCK, 380 M_ZERO | M_NOWAIT); 381 ses->ses_octx = malloc(ses->ses_axf->ctxsize, M_PADLOCK, 382 M_ZERO | M_NOWAIT); 383 if (ses->ses_ictx == NULL || ses->ses_octx == NULL) 384 return (ENOMEM); 385 386 /* Setup key if given. */ 387 if (csp->csp_auth_key != NULL) { 388 padlock_hash_key_setup(ses, csp->csp_auth_key, 389 csp->csp_auth_klen); 390 } 391 return (0); 392 } 393 394 int 395 padlock_hash_process(struct padlock_session *ses, struct cryptop *crp, 396 const struct crypto_session_params *csp) 397 { 398 struct thread *td; 399 int error; 400 401 td = curthread; 402 fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL | FPU_KERN_KTHR); 403 if (crp->crp_auth_key != NULL) 404 padlock_hash_key_setup(ses, crp->crp_auth_key, 405 csp->csp_auth_klen); 406 407 error = padlock_authcompute(ses, crp); 408 fpu_kern_leave(td, ses->ses_fpu_ctx); 409 return (error); 410 } 411 412 void 413 padlock_hash_free(struct padlock_session *ses) 414 { 415 416 if (ses->ses_ictx != NULL) { 417 padlock_free_ctx(ses->ses_axf, ses->ses_ictx); 418 bzero(ses->ses_ictx, ses->ses_axf->ctxsize); 419 free(ses->ses_ictx, M_PADLOCK); 420 ses->ses_ictx = NULL; 421 } 422 if (ses->ses_octx != NULL) { 423 padlock_free_ctx(ses->ses_axf, ses->ses_octx); 424 bzero(ses->ses_octx, ses->ses_axf->ctxsize); 425 free(ses->ses_octx, M_PADLOCK); 426 ses->ses_octx = NULL; 427 } 428 } 429