1 /*- 2 * Copyright (c) 2016 The Go Authors. All rights reserved. 3 * Copyright (c) 2024 Robert Clausecker <fuz@freebsd.org> 4 * 5 * Adapted from Go's crypto/sha1/sha1block_amd64.go. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following disclaimer 15 * in the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Google Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <machine/specialreg.h> 35 #include <sha.h> 36 #include <x86/ifunc.h> 37 38 extern void _libmd_sha1block_scalar(SHA1_CTX *, const void *, size_t); 39 extern void _libmd_sha1block_avx2(SHA1_CTX *, const void *, size_t); 40 extern void _libmd_sha1block_shani(SHA1_CTX *, const void *, size_t); 41 static void sha1block_avx2_wrapper(SHA1_CTX *, const void *, size_t); 42 43 #define AVX2_STDEXT_NEEDED \ 44 (CPUID_STDEXT_BMI1 | CPUID_STDEXT_AVX2 | CPUID_STDEXT_BMI2) 45 46 DEFINE_UIFUNC(, void, sha1_block, (SHA1_CTX *, const void *, size_t)) 47 { 48 if (cpu_stdext_feature & CPUID_STDEXT_SHA) 49 return (_libmd_sha1block_shani); 50 if ((cpu_stdext_feature & AVX2_STDEXT_NEEDED) == AVX2_STDEXT_NEEDED) 51 return (sha1block_avx2_wrapper); 52 else 53 return (_libmd_sha1block_scalar); 54 } 55 56 static void 57 sha1block_avx2_wrapper(SHA1_CTX *c, const void *data, size_t len) 58 { 59 if (len >= 256) { 60 /* 61 * sha1block_avx2 calculates sha1 for 2 block per iteration. 62 * It also interleaves the precalculation for next the block. 63 * So it may read up-to 192 bytes past the end of p. 64 * We may add checks inside sha1block_avx2, but this will 65 * just turn it into a copy of sha1block_scalar, 66 * so call it directly, instead. 67 */ 68 size_t safe_len = len - 128; 69 70 if (safe_len % 128 != 0) 71 safe_len -= 64; 72 73 _libmd_sha1block_avx2(c, data, safe_len); 74 _libmd_sha1block_scalar(c, data + safe_len, len - safe_len); 75 } else 76 _libmd_sha1block_scalar(c, data, len); 77 } 78