Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fscrypt: replace local base64url helpers with lib/base64

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.

Link: https://lkml.kernel.org/r/20251114060221.89734-1-409411716@gms.tku.edu.tw
Reviewed-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Signed-off-by: Guan-Chun Wu <409411716@gms.tku.edu.tw>
Cc: Christoph Hellwig <hch@lst.de>
Cc: David Laight <david.laight.linux@gmail.com>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Ilya Dryomov <idryomov@gmail.com>
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Sagi Grimberg <sagi@grimberg.me>
Cc: "Theodore Y. Ts'o" <tytso@mit.edu>
Cc: Viacheslav Dubeyko <Slava.Dubeyko@ibm.com>
Cc: Xiubo Li <xiubli@redhat.com>
Cc: Yu-Sheng Huang <home7438072@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Guan-Chun Wu and committed by
Andrew Morton
7794510e 8b365c4f

+6 -83
+6 -83
fs/crypto/fname.c
··· 16 16 #include <linux/export.h> 17 17 #include <linux/namei.h> 18 18 #include <linux/scatterlist.h> 19 + #include <linux/base64.h> 19 20 20 21 #include "fscrypt_private.h" 21 22 ··· 72 71 73 72 /* Encoded size of max-size no-key name */ 74 73 #define FSCRYPT_NOKEY_NAME_MAX_ENCODED \ 75 - FSCRYPT_BASE64URL_CHARS(FSCRYPT_NOKEY_NAME_MAX) 74 + BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX) 76 75 77 76 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) 78 77 { ··· 161 160 162 161 oname->len = strnlen(oname->name, iname->len); 163 162 return 0; 164 - } 165 - 166 - static const char base64url_table[65] = 167 - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 168 - 169 - #define FSCRYPT_BASE64URL_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) 170 - 171 - /** 172 - * fscrypt_base64url_encode() - base64url-encode some binary data 173 - * @src: the binary data to encode 174 - * @srclen: the length of @src in bytes 175 - * @dst: (output) the base64url-encoded string. Not NUL-terminated. 176 - * 177 - * Encodes data using base64url encoding, i.e. the "Base 64 Encoding with URL 178 - * and Filename Safe Alphabet" specified by RFC 4648. '='-padding isn't used, 179 - * as it's unneeded and not required by the RFC. base64url is used instead of 180 - * base64 to avoid the '/' character, which isn't allowed in filenames. 181 - * 182 - * Return: the length of the resulting base64url-encoded string in bytes. 183 - * This will be equal to FSCRYPT_BASE64URL_CHARS(srclen). 184 - */ 185 - static int fscrypt_base64url_encode(const u8 *src, int srclen, char *dst) 186 - { 187 - u32 ac = 0; 188 - int bits = 0; 189 - int i; 190 - char *cp = dst; 191 - 192 - for (i = 0; i < srclen; i++) { 193 - ac = (ac << 8) | src[i]; 194 - bits += 8; 195 - do { 196 - bits -= 6; 197 - *cp++ = base64url_table[(ac >> bits) & 0x3f]; 198 - } while (bits >= 6); 199 - } 200 - if (bits) 201 - *cp++ = base64url_table[(ac << (6 - bits)) & 0x3f]; 202 - return cp - dst; 203 - } 204 - 205 - /** 206 - * fscrypt_base64url_decode() - base64url-decode a string 207 - * @src: the string to decode. Doesn't need to be NUL-terminated. 208 - * @srclen: the length of @src in bytes 209 - * @dst: (output) the decoded binary data 210 - * 211 - * Decodes a string using base64url encoding, i.e. the "Base 64 Encoding with 212 - * URL and Filename Safe Alphabet" specified by RFC 4648. '='-padding isn't 213 - * accepted, nor are non-encoding characters such as whitespace. 214 - * 215 - * This implementation hasn't been optimized for performance. 216 - * 217 - * Return: the length of the resulting decoded binary data in bytes, 218 - * or -1 if the string isn't a valid base64url string. 219 - */ 220 - static int fscrypt_base64url_decode(const char *src, int srclen, u8 *dst) 221 - { 222 - u32 ac = 0; 223 - int bits = 0; 224 - int i; 225 - u8 *bp = dst; 226 - 227 - for (i = 0; i < srclen; i++) { 228 - const char *p = strchr(base64url_table, src[i]); 229 - 230 - if (p == NULL || src[i] == 0) 231 - return -1; 232 - ac = (ac << 6) | (p - base64url_table); 233 - bits += 6; 234 - if (bits >= 8) { 235 - bits -= 8; 236 - *bp++ = (u8)(ac >> bits); 237 - } 238 - } 239 - if (ac & ((1 << bits) - 1)) 240 - return -1; 241 - return bp - dst; 242 163 } 243 164 244 165 bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy, ··· 310 387 nokey_name.sha256); 311 388 size = FSCRYPT_NOKEY_NAME_MAX; 312 389 } 313 - oname->len = fscrypt_base64url_encode((const u8 *)&nokey_name, size, 314 - oname->name); 390 + oname->len = base64_encode((const u8 *)&nokey_name, size, 391 + oname->name, false, BASE64_URLSAFE); 315 392 return 0; 316 393 } 317 394 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr); ··· 390 467 if (fname->crypto_buf.name == NULL) 391 468 return -ENOMEM; 392 469 393 - ret = fscrypt_base64url_decode(iname->name, iname->len, 394 - fname->crypto_buf.name); 470 + ret = base64_decode(iname->name, iname->len, 471 + fname->crypto_buf.name, false, BASE64_URLSAFE); 395 472 if (ret < (int)offsetof(struct fscrypt_nokey_name, bytes[1]) || 396 473 (ret > offsetof(struct fscrypt_nokey_name, sha256) && 397 474 ret != FSCRYPT_NOKEY_NAME_MAX)) {