From nobody Fri Jan 10 15:04:04 2025 X-Original-To: dev-commits-src-main@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 4YV4g85fZlz5kTc5; Fri, 10 Jan 2025 15:04:04 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YV4g82B23z4wyB; Fri, 10 Jan 2025 15:04:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736521444; 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=ebtpVg/nZtDqLeEvmolQVaTa4Qcgbec8gjkkd5oZ6mA=; b=mT0ZNCQUMxhqk5+Ug2R9xmYTIEqEg5ZX9MH5RIrKMFnT6UVG9nWE2CgF0wbGcLVQTEiDhE 5C2TT2+tRjlLCtmPDymG+wgfx2no4lMItt4SCKgA+LXrepeenxY1xzOwSNfTkcqQdnflU1 JoTCW/ydyMIt7nSc9rVVZ9q5GJsPNSRED4P46STEuwIrczkS4i/auY23z2EVJ2jQERBHjX DGVM90H0jPOVfxJ/MQcXCl9xdOKjpDwse1R19EV4TP/RsQMWZGLnNUlI8kSNqNR/KKWpns enCsmp6WCilw4bFLtO7kyRLZONRGZWdjx9gaXSlEldlskSxT8lZqnfx0S8QhIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736521444; 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=ebtpVg/nZtDqLeEvmolQVaTa4Qcgbec8gjkkd5oZ6mA=; b=jXjmxaUroRKqi83IQeapZOIrPkF1ihw9Q1VOxlNVsWfK6fxx6qvmy6I3A3mXTObIB181dj oSNrLhAVZA314mJI7nF0OsCf2yeZlGHigeZV+TL5R+lU+GLbf+xTnf0+nAtCJ3r98ISuW8 XMZDiL9/i0lWctgam7KP0zPdq0Ww/47+WPWi9Km7v5/cx2IN+UrWR3i8SzYGzvNUDPt2yU IzXTwJfDSwdeuB1/5NXBs0tr0geczdebs266f9NvKvM82acrPkh+Be9QMv/leaTYwkOxLK QgTdkr9HcqFp1zf6yCuj4B+hvzCVbIA+AHiBhgggALbTD2DPrMsWF8ZRY7C8nw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1736521444; a=rsa-sha256; cv=none; b=CHVc1+x1DyA9gHH2edrFqENhxgZcgxhGPX01X56Q29sRapMOqeZGUUD7XtKrrRSOhUjA0z groJDboNysstT4tovM2FBtDJc88g6mEfF7ZdHSC/QTiMVgLsrUtoPY/zYu3F5REHOg88Hk hkJZ4EpD/R2e/EGt/IMGX8PZupZS7Cwb5jmzs1x7B+MOuNiJR0ZnIQ3UG69hkf+by8nRBk HkjDMZAPy3WtUe7BlCLeq0pcJdQdNLM4I0sVeh8HJx2KBxSLGlHXbS+oOPm2os/RcRcoEF j9ONOs6z2YdeHnn2ISXeT9IpzoZgUfVIbuiefGkwl9gKqDNykJXNQxvWLiIPcA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4YV4g81lzKz1q6; Fri, 10 Jan 2025 15:04:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 50AF44lB057549; Fri, 10 Jan 2025 15:04:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50AF441b057546; Fri, 10 Jan 2025 15:04:04 GMT (envelope-from git) Date: Fri, 10 Jan 2025 15:04:04 GMT Message-Id: <202501101504.50AF441b057546@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: f2c98669fc1b - main - lib/libc/aarch64/string: add ASIMD-enhanced timingsafe_bcmp implementation List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f2c98669fc1b3fd2dbc7a7e3eedd098970a10dec Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=f2c98669fc1b3fd2dbc7a7e3eedd098970a10dec commit f2c98669fc1b3fd2dbc7a7e3eedd098970a10dec Author: Robert Clausecker AuthorDate: 2024-12-09 09:49:49 +0000 Commit: Robert Clausecker CommitDate: 2025-01-10 15:02:41 +0000 lib/libc/aarch64/string: add ASIMD-enhanced timingsafe_bcmp implementation A straightforward port of the amd64 implementation. Approved by: security (cperciva) Reviewed by: getz, cperciva Event: EuroBSDcon 2024 Differential Revision: https://reviews.freebsd.org/D46757 --- lib/libc/aarch64/string/Makefile.inc | 1 + lib/libc/aarch64/string/timingsafe_bcmp.S | 113 ++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/lib/libc/aarch64/string/Makefile.inc b/lib/libc/aarch64/string/Makefile.inc index 752cc6d9900b..8019ab4adafc 100644 --- a/lib/libc/aarch64/string/Makefile.inc +++ b/lib/libc/aarch64/string/Makefile.inc @@ -31,6 +31,7 @@ MDSRCS+= \ strncat.c \ strlcat.c \ strlen.S \ + timingsafe_bcmp.S \ bcopy.c \ bzero.c diff --git a/lib/libc/aarch64/string/timingsafe_bcmp.S b/lib/libc/aarch64/string/timingsafe_bcmp.S new file mode 100644 index 000000000000..baa5c6f0940c --- /dev/null +++ b/lib/libc/aarch64/string/timingsafe_bcmp.S @@ -0,0 +1,113 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Robert Clausecker + */ + +#include + +ENTRY(timingsafe_bcmp) + cmp x2, #32 // at least 33 bytes to process? + bhi .Lgt32 + + cmp x2, #16 // at least 17 bytes to process? + bhi .L1732 + + cmp x2, #8 // at least 9 bytes to process? + bhi .L0916 + + cmp x2, #4 // at least 5 bytes to process? + bhi .L0508 + + cmp x2, #2 // at least 3 bytes to process? + bhi .L0304 + + cbnz x2, .L0102 // buffer empty? + + mov w0, #0 // empty buffer always matches + ret + +.L0102: ldrb w3, [x0] // load first bytes + ldrb w4, [x1] + sub x2, x2, #1 + ldrb w5, [x0, x2] // load last bytes + ldrb w6, [x1, x2] + eor w3, w3, w4 + eor w5, w5, w6 + orr w0, w3, w5 + ret + +.L0304: ldrh w3, [x0] // load first halfwords + ldrh w4, [x1] + sub x2, x2, #2 + ldrh w5, [x0, x2] // load last halfwords + ldrh w6, [x1, x2] + eor w3, w3, w4 + eor w5, w5, w6 + orr w0, w3, w5 + ret + +.L0508: ldr w3, [x0] // load first words + ldr w4, [x1] + sub x2, x2, #4 + ldr w5, [x0, x2] // load last words + ldr w6, [x1, x2] + eor w3, w3, w4 + eor w5, w5, w6 + orr w0, w3, w5 + ret + +.L0916: ldr x3, [x0] + ldr x4, [x1] + sub x2, x2, #8 + ldr x5, [x0, x2] + ldr x6, [x1, x2] + eor x3, x3, x4 + eor x5, x5, x6 + orr x0, x3, x5 + orr x0, x0, x0, lsr #32 // ensure low 32 bits are nonzero iff mismatch + ret + +.L1732: ldr q0, [x0] + ldr q1, [x1] + sub x2, x2, #16 + ldr q2, [x0, x2] + ldr q3, [x1, x2] + eor v0.16b, v0.16b, v1.16b + eor v2.16b, v2.16b, v3.16b + orr v0.16b, v0.16b, v2.16b + umaxv s0, v0.4s // get a nonzero word if any + mov w0, v0.s[0] + ret + + /* more than 32 bytes: process buffer in a loop */ +.Lgt32: ldp q0, q1, [x0], #32 + ldp q2, q3, [x1], #32 + eor v0.16b, v0.16b, v2.16b + eor v1.16b, v1.16b, v3.16b + orr v4.16b, v0.16b, v1.16b + subs x2, x2, #64 // enough left for another iteration? + bls .Ltail + +0: ldp q0, q1, [x0], #32 + ldp q2, q3, [x1], #32 + eor v0.16b, v0.16b, v2.16b + eor v1.16b, v1.16b, v3.16b + orr v0.16b, v0.16b, v1.16b + orr v4.16b, v4.16b, v0.16b + subs x2, x2, #32 + bhi 0b + + /* process last 32 bytes */ +.Ltail: add x0, x0, x2 // point to the last 32 bytes in the buffer + add x1, x1, x2 + ldp q0, q1, [x0] + ldp q2, q3, [x1] + eor v0.16b, v0.16b, v2.16b + eor v1.16b, v1.16b, v3.16b + orr v0.16b, v0.16b, v1.16b + orr v4.16b, v4.16b, v0.16b + umaxv s0, v4.4s // get a nonzero word if any + mov w0, v0.s[0] + ret +END(timingsafe_bcmp)