From nobody Thu Dec 18 03:29:51 2025 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B63822DCC06 for ; Fri, 14 Nov 2025 06:02:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763100149; cv=none; b=smjHmzGtLRlF1Xn6OjuG9T8YxR6X8dD65UdXcT/ICRCzVGU347kw4JU868vxTFKzXWBbj+tkHtR0DNoaoy/eyrYZchaqNUXFnG9wZ2yN3dLRM3fuVVkkVgfmastqu25A+QkgchQjZCy13UEl9MpI2EIXZbJ2LCfJuWvHjDJ2XrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763100149; c=relaxed/simple; bh=I6hn5XyUpnrWRuQcjJdlpYH7Q9/3VAy8I4U1WzKLW0k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GiL0WU74AWl4ZzjJky43dD2VmC+gNgJF+PIK2J/Cs5iVTDMLKfCslI3+RINDsuifWOBxzGv5diXfBncIjL0cePGcZ/UzC6XFcqJe/RQLf4MKmRhIV0gv0RmVeDB7AkG0sS3b5Ujaes/lQrmaWvNjVFKuyRjXvWcaz9TQSObFnyo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gms.tku.edu.tw; spf=pass smtp.mailfrom=gms.tku.edu.tw; dkim=pass (2048-bit key) header.d=gms-tku-edu-tw.20230601.gappssmtp.com header.i=@gms-tku-edu-tw.20230601.gappssmtp.com header.b=KXYUukLq; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gms.tku.edu.tw Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gms.tku.edu.tw Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gms-tku-edu-tw.20230601.gappssmtp.com header.i=@gms-tku-edu-tw.20230601.gappssmtp.com header.b="KXYUukLq" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-298039e00c2so19941245ad.3 for ; Thu, 13 Nov 2025 22:02:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gms-tku-edu-tw.20230601.gappssmtp.com; s=20230601; t=1763100147; x=1763704947; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ERv4XtgoXPP2dPnpwcSEeS4AUqPV+CdFVlWhf34t39A=; b=KXYUukLqInLfvVAR8TiBnG8x2Q85rxVFmReEBqT7PAsEV3RUlxmViDSpKUJG30Pc+B jwnbGcunncfo82I3xk5tbH6J6PwbPZ09pydzcfB2peyKakQ+NYv1ffuZbPLThVYxRGFB 2W3y2OUIqngzCaNjsxzEwJsFkN/WCelB7f0dJQVoLJuL5B9zuxOCTCE0zV5lCLq+I+v8 8VcYP6HAszg8US8rz4QI1tAnYjMybTDBM0SRF6l7tTKvCEPIjVT/jVmIlSR31bk9Mqn4 tcsfB69z3x/+Yp77fKmUJHsAkaamPsIeviNtT05YMFx5+8Q30p4q9y3dsngWQKPxnJ5H 22Dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763100147; x=1763704947; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ERv4XtgoXPP2dPnpwcSEeS4AUqPV+CdFVlWhf34t39A=; b=pKyJOrfUNWp/GXk3fIMECJXHwiSRxeSMlWA0rYMNxBvFDMJrL0HfH8xisRjfp07GBe XvyUk69mTkVy4qK2CdqSWGes57/ahN+Is9MkEa+YkbBzxKh1vuZqLxHFkBS8MALNMpBE RFF954Vn1ojpeQx6JvrxznASqrn8KlCZj5pP+u+JLEx4pdQ58MEukUAQvlJYrL+qSzC0 yXi766PSxQ2VdTu3k4bk7TnQ0dvM9HyODxcfeTZTNVzpNCCRjQI75UAfUXIufEC8z2yT 2/b73Al7OYUFWICk5uXjOWZe9QNy2I5Fvgguhf+/7EAgSJTVKCCBQnG9I6qS8v8bBV+f pXoA== X-Forwarded-Encrypted: i=1; AJvYcCWndQQWslVTlEgPB6lW14TNgwsUcX2U4kiCV4dADxXfq4ahTtA8uTLnI6U1YvQOgS58jE1DH8rLl5wiOuc=@vger.kernel.org X-Gm-Message-State: AOJu0YynVCakUKnYggHcprJOlOmDriqH5OKrvWOJUdsJz+IE/d4M1mXw nSYwiLtbPEU8DQXvGBEF8ORrLaXos65i5ulX2st39/ePeIq+ShJYOtRj8Gd5vp2a/bo= X-Gm-Gg: ASbGnctS0EQjhJBFerb9uwGafaPAbHT/L2OmeCNlucDlBI/mYOGiqwEt5Gqpa0uQKX9 HCBTW2OtCdvxen1gJnGBzWd5Q99hkXgCSlkpjg3mhoM9PNfmb0S4bisTrekvmoh1U+6SBp7elVh BP6/TguQCvH9xUH1VQI27aXl9je4QA2w6TpZi1lb2V7PwfQRPWAGbCPru0X6sjK7coCy97QnxiP eb35QjNlRXdqtbxzb31jpOcBzQ8vTi4isBBl5jieGrOdwaKLyWXUJBIAa1B4w/DNKVZlG4nv1mx naed6OI3moBr/Qei04XV71Um5TMTfFAkDMB+/zthN4QExflf/zGix20zQb+WKAy8/MnJZEdAkb8 tJQcdpQOVcH//qyzohsy6bZDPOzjEClXAcdLdMwVh/wwI34hYZ42s2AxDB+FGphqbxGmIvhDUyj hIw1kfSi4PHTitbx7gBVYL3Cim X-Google-Smtp-Source: AGHT+IFs317cxuw2DDSKPoB1op+RLqXuzE2pstLiig2tLA8KeXhRrXBdYzwc2MB74Tur9nS/jMyR0Q== X-Received: by 2002:a17:902:cf08:b0:265:47:a7bd with SMTP id d9443c01a7336-2986a6bcff3mr20918665ad.4.1763100146747; Thu, 13 Nov 2025 22:02:26 -0800 (PST) Received: from wu-Pro-E500-G6-WS720T.. ([2001:288:7001:2703:22d2:323c:497d:adbd]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2bf17dsm42818665ad.94.2025.11.13.22.02.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Nov 2025 22:02:26 -0800 (PST) From: Guan-Chun Wu <409411716@gms.tku.edu.tw> To: 409411716@gms.tku.edu.tw Cc: akpm@linux-foundation.org, andriy.shevchenko@intel.com, axboe@kernel.dk, ceph-devel@vger.kernel.org, david.laight.linux@gmail.com, ebiggers@kernel.org, hch@lst.de, home7438072@gmail.com, idryomov@gmail.com, jaegeuk@kernel.org, kbusch@kernel.org, linux-fscrypt@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, sagi@grimberg.me, tytso@mit.edu, visitorckw@gmail.com, xiubli@redhat.com Subject: [PATCH v5 5/6] fscrypt: replace local base64url helpers with lib/base64 Date: Fri, 14 Nov 2025 14:02:21 +0800 Message-Id: <20251114060221.89734-1-409411716@gms.tku.edu.tw> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251114055829.87814-1-409411716@gms.tku.edu.tw> References: <20251114055829.87814-1-409411716@gms.tku.edu.tw> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the base64url encoding and decoding functions in fscrypt with the generic base64_encode() and base64_decode() helpers from lib/base64. This removes the custom implementation in fscrypt, reduces code duplication, and relies on the shared Base64 implementation in lib. The helpers preserve RFC 4648-compliant URL-safe Base64 encoding without padding, so there are no functional changes. This change also improves performance: encoding is about 2.7x faster and decoding achieves 43-52x speedups compared to the previous implementation. Reviewed-by: Kuan-Wei Chiu Signed-off-by: Guan-Chun Wu <409411716@gms.tku.edu.tw> --- fs/crypto/fname.c | 89 ++++------------------------------------------- 1 file changed, 6 insertions(+), 83 deletions(-) diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c index 8e4c213d418b..a9a4432d12ba 100644 --- a/fs/crypto/fname.c +++ b/fs/crypto/fname.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 #include "fscrypt_private.h" =20 @@ -71,7 +72,7 @@ struct fscrypt_nokey_name { =20 /* Encoded size of max-size no-key name */ #define FSCRYPT_NOKEY_NAME_MAX_ENCODED \ - FSCRYPT_BASE64URL_CHARS(FSCRYPT_NOKEY_NAME_MAX) + BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX) =20 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) { @@ -162,84 +163,6 @@ static int fname_decrypt(const struct inode *inode, return 0; } =20 -static const char base64url_table[65] =3D - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; - -#define FSCRYPT_BASE64URL_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) - -/** - * fscrypt_base64url_encode() - base64url-encode some binary data - * @src: the binary data to encode - * @srclen: the length of @src in bytes - * @dst: (output) the base64url-encoded string. Not NUL-terminated. - * - * Encodes data using base64url encoding, i.e. the "Base 64 Encoding with = URL - * and Filename Safe Alphabet" specified by RFC 4648. '=3D'-padding isn't= used, - * as it's unneeded and not required by the RFC. base64url is used instea= d of - * base64 to avoid the '/' character, which isn't allowed in filenames. - * - * Return: the length of the resulting base64url-encoded string in bytes. - * This will be equal to FSCRYPT_BASE64URL_CHARS(srclen). - */ -static int fscrypt_base64url_encode(const u8 *src, int srclen, char *dst) -{ - u32 ac =3D 0; - int bits =3D 0; - int i; - char *cp =3D dst; - - for (i =3D 0; i < srclen; i++) { - ac =3D (ac << 8) | src[i]; - bits +=3D 8; - do { - bits -=3D 6; - *cp++ =3D base64url_table[(ac >> bits) & 0x3f]; - } while (bits >=3D 6); - } - if (bits) - *cp++ =3D base64url_table[(ac << (6 - bits)) & 0x3f]; - return cp - dst; -} - -/** - * fscrypt_base64url_decode() - base64url-decode a string - * @src: the string to decode. Doesn't need to be NUL-terminated. - * @srclen: the length of @src in bytes - * @dst: (output) the decoded binary data - * - * Decodes a string using base64url encoding, i.e. the "Base 64 Encoding w= ith - * URL and Filename Safe Alphabet" specified by RFC 4648. '=3D'-padding i= sn't - * accepted, nor are non-encoding characters such as whitespace. - * - * This implementation hasn't been optimized for performance. - * - * Return: the length of the resulting decoded binary data in bytes, - * or -1 if the string isn't a valid base64url string. - */ -static int fscrypt_base64url_decode(const char *src, int srclen, u8 *dst) -{ - u32 ac =3D 0; - int bits =3D 0; - int i; - u8 *bp =3D dst; - - for (i =3D 0; i < srclen; i++) { - const char *p =3D strchr(base64url_table, src[i]); - - if (p =3D=3D NULL || src[i] =3D=3D 0) - return -1; - ac =3D (ac << 6) | (p - base64url_table); - bits +=3D 6; - if (bits >=3D 8) { - bits -=3D 8; - *bp++ =3D (u8)(ac >> bits); - } - } - if (ac & ((1 << bits) - 1)) - return -1; - return bp - dst; -} - bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy, u32 orig_len, u32 max_len, u32 *encrypted_len_ret) @@ -387,8 +310,8 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode, nokey_name.sha256); size =3D FSCRYPT_NOKEY_NAME_MAX; } - oname->len =3D fscrypt_base64url_encode((const u8 *)&nokey_name, size, - oname->name); + oname->len =3D base64_encode((const u8 *)&nokey_name, size, + oname->name, false, BASE64_URLSAFE); return 0; } EXPORT_SYMBOL(fscrypt_fname_disk_to_usr); @@ -467,8 +390,8 @@ int fscrypt_setup_filename(struct inode *dir, const str= uct qstr *iname, if (fname->crypto_buf.name =3D=3D NULL) return -ENOMEM; =20 - ret =3D fscrypt_base64url_decode(iname->name, iname->len, - fname->crypto_buf.name); + ret =3D base64_decode(iname->name, iname->len, + fname->crypto_buf.name, false, BASE64_URLSAFE); if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) || (ret > offsetof(struct fscrypt_nokey_name, sha256) && ret !=3D FSCRYPT_NOKEY_NAME_MAX)) { --=20 2.34.1