// SPDX-License-Identifier: GPL-2.01/*2* base64.c - RFC4648-compliant base64 encoding3*4* Copyright (c) 2020 Hannes Reinecke, SUSE5*6* Based on the base64url routines from fs/crypto/fname.c7* (which are using the URL-safe base64 encoding),8* modified to use the standard coding table from RFC4648 section 4.9*/1011#include <linux/kernel.h>12#include <linux/types.h>13#include <linux/export.h>14#include <linux/string.h>15#include <linux/base64.h>1617static const char base64_table[65] =18"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";1920/**21* base64_encode() - base64-encode some binary data22* @src: the binary data to encode23* @srclen: the length of @src in bytes24* @dst: (output) the base64-encoded string. Not NUL-terminated.25*26* Encodes data using base64 encoding, i.e. the "Base 64 Encoding" specified27* by RFC 4648, including the '='-padding.28*29* Return: the length of the resulting base64-encoded string in bytes.30*/31int base64_encode(const u8 *src, int srclen, char *dst)32{33u32 ac = 0;34int bits = 0;35int i;36char *cp = dst;3738for (i = 0; i < srclen; i++) {39ac = (ac << 8) | src[i];40bits += 8;41do {42bits -= 6;43*cp++ = base64_table[(ac >> bits) & 0x3f];44} while (bits >= 6);45}46if (bits) {47*cp++ = base64_table[(ac << (6 - bits)) & 0x3f];48bits -= 6;49}50while (bits < 0) {51*cp++ = '=';52bits += 2;53}54return cp - dst;55}56EXPORT_SYMBOL_GPL(base64_encode);5758/**59* base64_decode() - base64-decode a string60* @src: the string to decode. Doesn't need to be NUL-terminated.61* @srclen: the length of @src in bytes62* @dst: (output) the decoded binary data63*64* Decodes a string using base64 encoding, i.e. the "Base 64 Encoding"65* specified by RFC 4648, including the '='-padding.66*67* This implementation hasn't been optimized for performance.68*69* Return: the length of the resulting decoded binary data in bytes,70* or -1 if the string isn't a valid base64 string.71*/72int base64_decode(const char *src, int srclen, u8 *dst)73{74u32 ac = 0;75int bits = 0;76int i;77u8 *bp = dst;7879for (i = 0; i < srclen; i++) {80const char *p = strchr(base64_table, src[i]);8182if (src[i] == '=') {83ac = (ac << 6);84bits += 6;85if (bits >= 8)86bits -= 8;87continue;88}89if (p == NULL || src[i] == 0)90return -1;91ac = (ac << 6) | (p - base64_table);92bits += 6;93if (bits >= 8) {94bits -= 8;95*bp++ = (u8)(ac >> bits);96}97}98if (ac & ((1 << bits) - 1))99return -1;100return bp - dst;101}102EXPORT_SYMBOL_GPL(base64_decode);103104105