From nobody Mon Dec 20 14:02:15 2021 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 797E918FCA78; Mon, 20 Dec 2021 14:02:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JHh9N1hChz3Fyx; Mon, 20 Dec 2021 14:02:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 19B4517B0F; Mon, 20 Dec 2021 14:02:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1BKE2Ff3042819; Mon, 20 Dec 2021 14:02:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BKE2FGj042818; Mon, 20 Dec 2021 14:02:15 GMT (envelope-from git) Date: Mon, 20 Dec 2021 14:02:15 GMT Message-Id: <202112201402.1BKE2FGj042818@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andrew Turner Subject: git: 757a39b1b27c - stable/13 - Use arm64 sha256 intrinsics in libmd List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: andrew X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 757a39b1b27c63eb3fda31418927aea52d854b72 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1640008936; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=u/6Q3a92Khg/MdVn++94+eUuIk1rPXd+gAUnAkkyt5A=; b=eR+8fagkAlYGR//OCa0Yd2guh7FCQl7etRyc2HfxVd3KYC6pu0QHxcMR+BOrhQd9Ii6rMQ e/COs5IkqTQzX9e7E0Jflzx6n2Bgxj3dpWXC3B8v8qDNgqMOYue8oduetPd3VKP04+pig7 dqultfHeyhe/2zr9yFL5pE25OXNMys8bsBtzKVlcfLYWqtQ3/DBZApc0usb1HP52J/Se93 Pqd9FxA//NLN3+aGxEGuG+ENCYci4D8RTTfCz7vGO8GMsm8Nt+Q/zMfIU7FU+iPKcnqHlQ VsrQ9XMO6DjstN2l5ab6fAcGFjWT9ML6PDw5hoJ8UhvZ3SLLGdGYDDc61nPXwQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1640008936; a=rsa-sha256; cv=none; b=vEeVU8VgEwr1sdbwPm7tE1jQs5xqok1gaqSATl/brJXR6pXbDE4pzjWv4KNw48ZJ90SxAo QjHyWIbAzoTu8N0+XA2qTuOP4tgOdkkV80cS3et5LYPTg6TIcwZK/K6RC2Faj0pPJjiIJi 5nZHlQ49gfyMxAv3291Syy7iHO7jL0isoqXn/wlBnpzQV38wvH5U3uFqx2lX7XIU9yqv0A V1zJ2fBfwWvFlt3DGg1PYmDQBluwa93y+y5eWHIo164U8dwxJm3akEnxuEW4xiagYDan2U iHjpC5luQeGRUQTnl9WkUUhEOaVF53mGgnEKTM1CKOnAWlieB1ErkqOI8zRDCw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=757a39b1b27c63eb3fda31418927aea52d854b72 commit 757a39b1b27c63eb3fda31418927aea52d854b72 Author: Andrew Turner AuthorDate: 2021-07-23 09:14:03 +0000 Commit: Andrew Turner CommitDate: 2021-12-20 11:09:26 +0000 Use arm64 sha256 intrinsics in libmd Summary: When running on a CPU that supports the arm64 sha256 intrinsics use them to improve perfromance of sha256 calculations. With this changethe following improvement has been seen on an Apple M1 with FreeBS running under Parallels, with similar results on a Neoverse-N1 r3p1. x sha256.orig + sha256.arm64 +--------------------------------------------------------------------+ |++ x x| |+++ xxx| ||A |A|| +--------------------------------------------------------------------+ N Min Max Median Avg Stddev x 5 3.41 3.5 3.46 3.458 0.042661458 + 5 0.47 0.54 0.5 0.504 0.027018512 Difference at 95.0% confidence -2.954 +/- 0.0520768 -85.4251% +/- 0.826831% (Student's t, pooled s = 0.0357071) Reviewed by: cem Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D31284 (cherry picked from commit 69d8dc20be6ca10cf87c98223121c4121aff42cc) --- lib/libmd/Makefile | 5 +++ sys/crypto/sha2/sha256c.c | 35 ++++++++++++++- sys/crypto/sha2/sha256c_arm64.c | 97 +++++++++++++++++++++++++++++++++++++++++ sys/crypto/sha2/sha256c_impl.h | 37 ++++++++++++++++ 4 files changed, 173 insertions(+), 1 deletion(-) diff --git a/lib/libmd/Makefile b/lib/libmd/Makefile index f90101112ba9..480796ae5029 100644 --- a/lib/libmd/Makefile +++ b/lib/libmd/Makefile @@ -129,6 +129,11 @@ skein_block.o skein_block.pico: Makefile .if exists(${MACHINE_ARCH}/sha.S) || exists(${MACHINE_ARCH}/rmd160.S) || exists(${MACHINE_ARCH}/skein_block_asm.S) ACFLAGS+= -DELF -Wa,--noexecstack .endif +.if ${MACHINE_CPUARCH} == "aarch64" +SRCS+= sha256c_arm64.c +CFLAGS+= -DARM64_SHA2 +CFLAGS.sha256c_arm64.c+= -march=armv8-a+crypto +.endif .endif # ${USE_ASM_SOURCES} != 0 md4hl.c: mdXhl.c diff --git a/sys/crypto/sha2/sha256c.c b/sys/crypto/sha2/sha256c.c index 135fb68ccc65..63805c97a4bb 100644 --- a/sys/crypto/sha2/sha256c.c +++ b/sys/crypto/sha2/sha256c.c @@ -38,6 +38,12 @@ __FBSDID("$FreeBSD$"); #include "sha224.h" #include "sha256.h" +#include "sha256c_impl.h" + +#if defined(ARM64_SHA2) +#include +#include +#endif #if BYTE_ORDER == BIG_ENDIAN @@ -132,7 +138,7 @@ static const uint32_t K[64] = { * the 512-bit input block to produce a new state. */ static void -SHA256_Transform(uint32_t * state, const unsigned char block[64]) +SHA256_Transform_c(uint32_t * state, const unsigned char block[64]) { uint32_t W[64]; uint32_t S[8]; @@ -188,6 +194,33 @@ SHA256_Transform(uint32_t * state, const unsigned char block[64]) state[i] += S[i]; } +#if defined(ARM64_SHA2) +static void +SHA256_Transform_arm64(uint32_t * state, const unsigned char block[64]) +{ + SHA256_Transform_arm64_impl(state, block, K); +} + +DEFINE_UIFUNC(static, void, SHA256_Transform, + (uint32_t * state, const unsigned char block[64])) +{ + u_long hwcap; + + if (elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)) == 0) { + if ((hwcap & HWCAP_SHA2) != 0) + return (SHA256_Transform_arm64); + } + + return (SHA256_Transform_c); +} +#else +static void +SHA256_Transform(uint32_t * state, const unsigned char block[64]) +{ + SHA256_Transform_c(state, block); +} +#endif + static unsigned char PAD[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, diff --git a/sys/crypto/sha2/sha256c_arm64.c b/sys/crypto/sha2/sha256c_arm64.c new file mode 100644 index 000000000000..8c0153c2b603 --- /dev/null +++ b/sys/crypto/sha2/sha256c_arm64.c @@ -0,0 +1,97 @@ +/*- + * Copyright (c) 2021 The FreeBSD Foundation + * + * This software was developed by Andrew Turner under sponsorship from + * the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include + +#include + +#include "sha256c_impl.h" + +void __hidden +SHA256_Transform_arm64_impl(uint32_t * state, const unsigned char block[64], + const uint32_t K[64]) +{ + uint32x4_t W[4]; + uint32x4_t S[2]; + uint32x4_t S_start[2]; + uint32x4_t K_tmp, S_tmp; + int i; + +#define A64_LOAD_W(x) \ + W[x] = vld1q_u32((const uint32_t *)(&block[(x) * 16])); \ + W[x] = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(W[x]))) + + /* 1. Prepare the first part of the message schedule W. */ + A64_LOAD_W(0); + A64_LOAD_W(1); + A64_LOAD_W(2); + A64_LOAD_W(3); + + /* 2. Initialize working variables. */ + S[0] = vld1q_u32(&state[0]); + S[1] = vld1q_u32(&state[4]); + + S_start[0] = S[0]; + S_start[1] = S[1]; + + /* 3. Mix. */ + for (i = 0; i < 64; i += 16) { +#define A64_RNDr(i, ii) \ + K_tmp = vaddq_u32(W[i], vld1q_u32(&K[ii + i * 4])); \ + S_tmp = vsha256hq_u32(S[0], S[1], K_tmp); \ + S[1] = vsha256h2q_u32(S[1], S[0], K_tmp); \ + S[0] = S_tmp + + A64_RNDr(0, i); + A64_RNDr(1, i); + A64_RNDr(2, i); + A64_RNDr(3, i); + + if (i == 48) + break; + +#define A64_MSCH(x) \ + W[x] = vsha256su0q_u32(W[x], W[(x + 1) % 4]); \ + W[x] = vsha256su1q_u32(W[x], W[(x + 2) % 4], W[(x + 3) % 4]) + + A64_MSCH(0); + A64_MSCH(1); + A64_MSCH(2); + A64_MSCH(3); + } + + /* 4. Mix local working variables into global state */ + S[0] = vaddq_u32(S[0], S_start[0]); + S[1] = vaddq_u32(S[1], S_start[1]); + + vst1q_u32(&state[0], S[0]); + vst1q_u32(&state[4], S[1]); +} diff --git a/sys/crypto/sha2/sha256c_impl.h b/sys/crypto/sha2/sha256c_impl.h new file mode 100644 index 000000000000..ed96e6706699 --- /dev/null +++ b/sys/crypto/sha2/sha256c_impl.h @@ -0,0 +1,37 @@ +/*- + * Copyright (c) 2021 The FreeBSD Foundation + * + * This software was developed by Andrew Turner under sponsorship from + * the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _SHA256_IMPL_H_ +#define _SHA256_IMPL_H_ + +#if defined(ARM64_SHA2) +void SHA256_Transform_arm64_impl(uint32_t * state, + const unsigned char block[64], const uint32_t K[64]); +#endif + +#endif /* !_SHA256_IMPL_H_ */