Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1199031pxv; Fri, 16 Jul 2021 04:05:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKnNTDwlqmCfNn07Q/n/qz2VrRMqSWcGPjCCLFwGdbBnzJ41BPGUsZ9ixJ/T/Ni0dTmVYb X-Received: by 2002:a05:6402:14c7:: with SMTP id f7mr14122428edx.84.1626433520403; Fri, 16 Jul 2021 04:05:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626433520; cv=none; d=google.com; s=arc-20160816; b=vHuo1vAPMtBlQEr63n/6frlworEQeHWcLPX4SEHhB9htstFZtQijylCk1POTx+1xDb ewXyMKAF+lGXKVyRS9o1U5mXSdMRjOfOGPnc+vkACUxmZp2rvVXAfBmImTzYYa55jj+Z qfcDiCfLXRh7++mDPqRvC/9asb52ShxrZM3t+kljWPRbaLZuBKzNsql7hsePcvZNyqMa jKAJTySEMcy5lM/ZhrOKueH6HMTt5N0gKH+pbd99NxxiJw9hAONrW3CyUPrzmpqEORZL UCyH3m9q8r83ckb+OzZAT6rJCyRdFZewqULEszPxOX9OcKJnoBg7XlSsZrjxjIT0QkRm XY0Q== 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=dvRAP17pLm/OVFpsyr76Iw1aMi9OCJjvP5bRYY3491M=; b=gzJs5k7Jlt/I+Tq3/41g7EVcRVRYhqynxPg0T+GaaYF0vJ3Uodh8OrS7gi6smrE/h+ R0B3X4P86WGtqMlMTYrKRVf9cfTjap0ay8aFAZvOjFs7Xa+h9yJNhTZ7dCvao2lmGt7c sZzq5WXzt8QHBUMa/4iIOK6H7BoUVg2tJVr/RJPY+Zqy6uA4+VcGVh8dyJH8ikWr4zVw UZPR0G6W3il27AVo9/QoTWyy7g35BgPZkDbVqB/zazvHFUNLSMPPEocvHKK7fXKFm2kO QmpbEbdOBX/tTcHcyu22ggzM2iOfKsjKILlvErk/8ynK9TyaKC1tvBAxra+53MSoF901 IAqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=btFKBG9N; dkim=neutral (no key) header.i=@suse.de header.b=1dMtVHWw; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dp16si2421603ejc.26.2021.07.16.04.04.55; Fri, 16 Jul 2021 04:05:20 -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=btFKBG9N; dkim=neutral (no key) header.i=@suse.de header.b=1dMtVHWw; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231386AbhGPLHj (ORCPT + 99 others); Fri, 16 Jul 2021 07:07:39 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:46824 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230319AbhGPLHh (ORCPT ); Fri, 16 Jul 2021 07:07:37 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 4904A205CE; Fri, 16 Jul 2021 11:04:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1626433482; 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=dvRAP17pLm/OVFpsyr76Iw1aMi9OCJjvP5bRYY3491M=; b=btFKBG9N7WxFjTTymsawxjLFhuMm88rvtiwmp0UXdBdD+bGfo3xqA055SFb5aszNp8Iufk U8SDT7RCjcBFQPmJ4eTl7SgWm9oS1zsJS253AKSu9FUZBfy/kcuzfWy4nvRLqUlcHI8kW+ F/D/UidFpR5uwxRShgXVv+VYmIxWzc4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1626433482; 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=dvRAP17pLm/OVFpsyr76Iw1aMi9OCJjvP5bRYY3491M=; b=1dMtVHWwZ0UmlObREgiYgPpj25/+ztiaFMC+/QP1ELM/xQ24jKn+HpaJVIXowx1leAHSIo egFOR/jhDX2nZQDg== Received: from adalid.arch.suse.de (adalid.arch.suse.de [10.161.8.13]) by relay2.suse.de (Postfix) with ESMTP id B7378A3BB5; Fri, 16 Jul 2021 11:04:41 +0000 (UTC) Received: by adalid.arch.suse.de (Postfix, from userid 16045) id 85E5B517160C; Fri, 16 Jul 2021 13:04:41 +0200 (CEST) From: Hannes Reinecke To: Christoph Hellwig Cc: Sagi Grimberg , Keith Busch , linux-nvme@lists.infradead.org, Herbert Xu , "David S . Miller" , linux-crypto@vger.kernel.org, Hannes Reinecke Subject: [PATCH 04/11] lib/base64: RFC4648-compliant base64 encoding Date: Fri, 16 Jul 2021 13:04:21 +0200 Message-Id: <20210716110428.9727-5-hare@suse.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210716110428.9727-1-hare@suse.de> References: <20210716110428.9727-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. Signed-off-by: Hannes Reinecke --- include/linux/base64.h | 16 ++++++ lib/Makefile | 2 +- lib/base64.c | 111 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 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 6d765d5fb8ac..92a428aa0e5f 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..a8929a3a04fe --- /dev/null +++ b/lib/base64.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * base64.c - RFC4648-compliant base64 encoding + * + * Copyright (c) 2020 Hannes Reinecke, SUSE + */ + +#include +#include +#include +#include +#include + +static const char lookup_table[65] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/** + * base64_encode() - base64-encode some bytes + * @src: the bytes to encode + * @len: number of bytes to encode + * @dst: (output) the base64-encoded string. Not NUL-terminated. + * + * Encodes the input string using characters from the set [A-Za-z0-9+,]. + * The encoded string is roughly 4/3 times the size of the input string. + * + * Return: length of the encoded string + */ +int base64_encode(const u8 *src, int len, char *dst) +{ + int i, bits = 0; + u32 ac = 0; + char *cp = dst; + + for (i = 0; i < len; i++) { + ac = (ac << 8) | src[i]; + bits += 8; + if (bits < 24) + continue; + do { + bits -= 6; + *cp++ = lookup_table[(ac >> bits) & 0x3f]; + } while (bits); + ac = 0; + } + if (bits) { + int more = 0; + + if (bits < 16) + more = 2; + ac = (ac << (2 + more)); + bits += (2 + more); + do { + bits -= 6; + *cp++ = lookup_table[(ac >> bits) & 0x3f]; + } while (bits); + *cp++ = '='; + if (more) + *cp++ = '='; + } + + return cp - dst; +} +EXPORT_SYMBOL_GPL(base64_encode); + +/** + * base64_decode() - base64-decode some bytes + * @src: the base64-encoded string to decode + * @len: number of bytes to decode + * @dst: (output) the decoded bytes. + * + * Decodes the base64-encoded bytes @src according to RFC 4648. + * + * Return: number of decoded bytes + */ +int base64_decode(const char *src, int len, u8 *dst) +{ + int i, bits = 0, pad = 0; + u32 ac = 0; + size_t dst_len = 0; + + for (i = 0; i < len; i++) { + int c, p = -1; + + if (src[i] == '=') { + pad++; + if (i + 1 < len && src[i + 1] == '=') + pad++; + break; + } + for (c = 0; c < strlen(lookup_table); c++) { + if (src[i] == lookup_table[c]) { + p = c; + break; + } + } + if (p < 0) + break; + ac = (ac << 6) | p; + bits += 6; + if (bits < 24) + continue; + while (bits) { + bits -= 8; + dst[dst_len++] = (ac >> bits) & 0xff; + } + ac = 0; + } + dst_len -= pad; + return dst_len; +} +EXPORT_SYMBOL_GPL(base64_decode); -- 2.29.2