From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/3] lib: base64: add support for base64url
Date: Mon, 23 Oct 2023 16:31:21 +0200 [thread overview]
Message-ID: <20231023143122.1760217-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20231023143122.1760217-1-a.fatoum@pengutronix.de>
base64url has some small differences to our current base64 implementation:
- Instead of encoding to `+', `-' is used
- Instead of encoding to `/', `_' is used
- Padding with = to reach four byte boundary is optional
- Invalid characters aren't silently skipped
Everything else is the same though, so let's reuse the code and have an
optional url bool parameter control whether to decode base64 or
base64url.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/base64.h | 1 +
lib/base64.c | 60 ++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 56 insertions(+), 5 deletions(-)
diff --git a/include/base64.h b/include/base64.h
index 993a366b48b0..7da35e21bac5 100644
--- a/include/base64.h
+++ b/include/base64.h
@@ -5,6 +5,7 @@
void uuencode(char *p, const char *src, int length);
int decode_base64(char *dst, int dst_len, const char *src);
+int decode_base64url(char *dst, int dst_len, const char *src);
#define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
diff --git a/lib/base64.c b/lib/base64.c
index ac165ab168c4..d5ab217528db 100644
--- a/lib/base64.c
+++ b/lib/base64.c
@@ -25,6 +25,25 @@ static const char uuenc_tbl_base64[65 + 1] = {
'\0' /* needed for uudecode.c only */
};
+static char base64_trchr(char ch, bool url)
+{
+ if (!url)
+ return ch;
+
+ switch (ch) {
+ case '+':
+ return '-';
+ case '/':
+ return '_';
+ case '-':
+ return '+';
+ case '_':
+ return '/';
+ default:
+ return ch;
+ }
+}
+
/*
* Encode bytes at S of length LENGTH to uuencode or base64 format and place it
* to STORE. STORE will be 0-terminated, and must point to a writable
@@ -68,13 +87,14 @@ EXPORT_SYMBOL(uuencode);
* Decode base64 encoded string. Stops on '\0'.
*
*/
-int decode_base64(char *p_dst, int dst_len, const char *src)
+static int __decode_base64(char *p_dst, int dst_len, const char *src, bool url)
{
const char *src_tail;
char *dst = p_dst;
int length = 0;
+ bool end_reached = false;
- while (dst_len > 0) {
+ while (dst_len > 0 && !end_reached) {
unsigned char six_bit[4];
int count = 0;
@@ -101,13 +121,23 @@ int decode_base64(char *p_dst, int dst_len, const char *src)
* because we did fully decode
* the string (to "ABC").
*/
- if (count == 0)
+ if (count == 0) {
src_tail = src;
+ } else if (url) {
+ end_reached = true;
+ goto out;
+ }
+
goto ret;
}
src++;
- table_ptr = strchr(uuenc_tbl_base64, ch);
- } while (!table_ptr);
+ table_ptr = strchr(uuenc_tbl_base64, base64_trchr(ch, url));
+ } while (!table_ptr && !url);
+
+ if (!table_ptr) {
+ end_reached = true;
+ goto out;
+ }
/* Convert encoded character to decimal */
ch = table_ptr - uuenc_tbl_base64;
@@ -119,6 +149,7 @@ int decode_base64(char *p_dst, int dst_len, const char *src)
six_bit[count] = ch;
count++;
}
+out:
/*
* Transform 6-bit values to 8-bit ones.
@@ -151,4 +182,23 @@ int decode_base64(char *p_dst, int dst_len, const char *src)
return length;
}
+
+/*
+ * Decode base64 encoded string. Stops on '\0'.
+ *
+ */
+int decode_base64(char *p_dst, int dst_len, const char *src)
+{
+ return __decode_base64(p_dst, dst_len, src, false);
+}
EXPORT_SYMBOL(decode_base64);
+
+/*
+ * Decode base64url encoded string. Stops on '\0'.
+ *
+ */
+int decode_base64url(char *p_dst, int dst_len, const char *src)
+{
+ return __decode_base64(p_dst, dst_len, src, true);
+}
+EXPORT_SYMBOL(decode_base64url);
--
2.39.2
next prev parent reply other threads:[~2023-10-23 14:33 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-23 14:31 [PATCH 0/3] crypto: add JSON Web Token (JWT) support Ahmad Fatoum
2023-10-23 14:31 ` Ahmad Fatoum [this message]
2023-10-23 14:31 ` [PATCH 2/3] " Ahmad Fatoum
2023-11-01 9:13 ` Sascha Hauer
2023-10-23 14:31 ` [PATCH 3/3] test: self: add JSON Web Token tests Ahmad Fatoum
2023-11-02 7:20 ` Sascha Hauer
2023-11-02 8:07 ` Ahmad Fatoum
2023-11-01 9:10 ` [PATCH 0/3] crypto: add JSON Web Token (JWT) support Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231023143122.1760217-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox