Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp4610953pxb; Mon, 27 Sep 2021 23:06:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYQsWmJv0Qzf7jr/ols9mzVIJwkDOSWX+NY9CjzoDKwSk9id6evhCDHMIDoHdcbrl4AEDf X-Received: by 2002:a17:90a:ba14:: with SMTP id s20mr3600215pjr.20.1632809181112; Mon, 27 Sep 2021 23:06:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632809181; cv=none; d=google.com; s=arc-20160816; b=GyZF+Xfa7Rh+xbCV8cBOvmqonZcjSbmIGr5tfvZo9yG7LsXIq2c5QfnG1M2tgjxOxp T7Wqt8dDpQ5j7jrFLK67h5vLGkK9Ga7lXJvQHNABg7ASZidYqwjXvdK39RdC8zAiB1XR w/7Q7mRD29DcHlV5sG9cWOb+dn3DlROljmHs2V26uxzX5PoKh004sdYiYjxMd/fEb38M Ezty3FMhOyxAD02Kipus27X/Mo1pCLYhttDFrFQDLAtSvnHGAZdZrE6D1MeAwJG4uzAk JEgNgVnshC1lnfs+wXKdiCAx+w6fmiB+6IOkHj+jAj6sVfg3/ywsqpx7RIkTFGnebs4y Yo4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=S8un4PKeR6L7QoTK65EhYsB7UQCSve8TLUHAyYCXVdw=; b=SUBq5lGL2UAyjf5TFo46X/qcaLQ3H0H+/YpsSDUcGIb+cxGb5g3QDX2caYx+FXBdB7 RN+E1KWfDkRkCmCO+sVgqzPomdmkYVoShn5u3peD9+5EL3KlWNvV95DTCU8LhZnfaQJj x0IGHT7Hj3LpBegVJAmqNnUBANS27bQfTUmfZnT3WmZ1ogclUvsyvO4PN+3N1eGmVDMH TEZ5DTlbRpB+soWU+vYTUcplZkK72NfEA7AENhJoxox0SsCWXEX7FG41sbK5u4vfzC5G DZ5DTqISGSIyVUYWEVpDC+DgrHuy530ftYowFY8MlyzeyxNf6XL7UYA18+Dm7RhOiv5Y fMQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=EbPOAi0O; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=FUYvhi87; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f10si26270177pgv.175.2021.09.27.23.06.08; Mon, 27 Sep 2021 23:06:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=EbPOAi0O; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=FUYvhi87; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239105AbhI1GGn (ORCPT + 99 others); Tue, 28 Sep 2021 02:06:43 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:51516 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239532AbhI1GFr (ORCPT ); Tue, 28 Sep 2021 02:05:47 -0400 Received: from relay1.suse.de (relay1.suse.de [149.44.160.133]) by smtp-out1.suse.de (Postfix) with ESMTP id CA37522309; Tue, 28 Sep 2021 06:04:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1632809047; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S8un4PKeR6L7QoTK65EhYsB7UQCSve8TLUHAyYCXVdw=; b=EbPOAi0OxICdSrFHeY9iwc2yHZphMbcHTMuMUWMOqlp+6uGEoaJFJ4ty5qc5PQXFb40X5L EGm4NxTabncXDFx1Eormjyox9isviZs6kH6wbcdqazwco8adpyXximtPcQH7LFY9TnoeAH Wn5lR+HV5UYFbzZdTSvQIo9W6DZexvc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1632809047; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S8un4PKeR6L7QoTK65EhYsB7UQCSve8TLUHAyYCXVdw=; b=FUYvhi87BlKy/H4n4WaZCdqAjojRfx26gRS8NuOyjyjlMQQl9BUa9RvO8kPEv/J2v8h2qJ 72E2xAMO9AyHmICA== Received: from adalid.arch.suse.de (adalid.arch.suse.de [10.161.8.13]) by relay1.suse.de (Postfix) with ESMTP id 7A83625E62; Tue, 28 Sep 2021 06:04:06 +0000 (UTC) Received: by adalid.arch.suse.de (Postfix, from userid 16045) id 4F4E5518EE26; Tue, 28 Sep 2021 08:04:06 +0200 (CEST) From: Hannes Reinecke To: Sagi Grimberg Cc: Christoph Hellwig , Keith Busch , Herbert Xu , "David S . Miller" , linux-nvme@lists.infradead.org, linux-crypto@vger.kernel.org, Hannes Reinecke Subject: [PATCH 04/12] lib/base64: RFC4648-compliant base64 encoding Date: Tue, 28 Sep 2021 08:03:48 +0200 Message-Id: <20210928060356.27338-5-hare@suse.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210928060356.27338-1-hare@suse.de> References: <20210928060356.27338-1-hare@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Add RFC4648-compliant base64 encoding and decoding routines, based on the base64url encoding in fs/crypto/fname.c. Signed-off-by: Hannes Reinecke --- include/linux/base64.h | 16 +++++++ lib/Makefile | 2 +- lib/base64.c | 100 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 include/linux/base64.h create mode 100644 lib/base64.c diff --git a/include/linux/base64.h b/include/linux/base64.h new file mode 100644 index 000000000000..660d4cb1ef31 --- /dev/null +++ b/include/linux/base64.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * base64 encoding, lifted from fs/crypto/fname.c. + */ + +#ifndef _LINUX_BASE64_H +#define _LINUX_BASE64_H + +#include + +#define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) + +int base64_encode(const u8 *src, int len, char *dst); +int base64_decode(const char *src, int len, u8 *dst); + +#endif /* _LINUX_BASE64_H */ diff --git a/lib/Makefile b/lib/Makefile index 5efd1b435a37..ce964f013412 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -46,7 +46,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \ bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \ list_sort.o uuid.o iov_iter.o clz_ctz.o \ bsearch.o find_bit.o llist.o memweight.o kfifo.o \ - percpu-refcount.o rhashtable.o \ + percpu-refcount.o rhashtable.o base64.o \ once.o refcount.o usercopy.o errseq.o bucket_locks.o \ generic-radix-tree.o obj-$(CONFIG_STRING_SELFTEST) += test_string.o diff --git a/lib/base64.c b/lib/base64.c new file mode 100644 index 000000000000..9f271665cbb1 --- /dev/null +++ b/lib/base64.c @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * base64.c - RFC4648-compliant base64 encoding + * + * Copyright (c) 2020 Hannes Reinecke, SUSE + * + * Based on the base64url routines from fs/crypto/fname.c + * (which are using the URL-safe base64 encoding), + * modified to use the standard coding table from RFC4648 section 4. + */ + +#include +#include +#include +#include +#include + +static const char base64_table[65] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/** + * base64_encode() - base64-encode some binary data + * @src: the binary data to encode + * @srclen: the length of @src in bytes + * @dst: (output) the base64-encoded string. Not NUL-terminated. + * + * Encodes data using base64 encoding, i.e. the "Base 64 Encoding" specified + * by RFC 4648, including the '='-padding. + * + * Return: the length of the resulting base64url-encoded string in bytes. + */ +int base64_encode(const u8 *src, int srclen, char *dst) +{ + u32 ac = 0; + int bits = 0; + int i; + char *cp = dst; + + for (i = 0; i < srclen; i++) { + ac = (ac << 8) | src[i]; + bits += 8; + do { + bits -= 6; + *cp++ = base64_table[(ac >> bits) & 0x3f]; + } while (bits >= 6); + } + if (bits) { + *cp++ = base64_table[(ac << (6 - bits)) & 0x3f]; + bits -= 6; + } + while (bits < 0) { + *cp++ = '='; + bits += 2; + } + return cp - dst; +} +EXPORT_SYMBOL_GPL(base64_encode); + +/** + * base64_decode() - base64-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" + * specified by RFC 4648, including the '='-padding. + * + * 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 base64 string. + */ +int base64_decode(const char *src, int srclen, u8 *dst) +{ + u32 ac = 0; + int bits = 0; + int i; + u8 *bp = dst; + + for (i = 0; i < srclen; i++) { + const char *p = strchr(base64_table, src[i]); + + if (src[i] == '=') { + ac = (ac << 6); + continue; + } + if (p == NULL || src[i] == 0) + return -1; + ac = (ac << 6) | (p - base64_table); + bits += 6; + if (bits >= 8) { + bits -= 8; + *bp++ = (u8)(ac >> bits); + } + } + if (ac & ((1 << bits) - 1)) + return -1; + return bp - dst; +} +EXPORT_SYMBOL_GPL(base64_decode); -- 2.29.2