at commit 205cccd0
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * base64.c - Base64 with support for multiple variants |
| 4 | * |
| 5 | * Copyright (c) 2020 Hannes Reinecke, SUSE |
| 6 | * |
| 7 | * Based on the base64url routines from fs/crypto/fname.c |
| 8 | * (which are using the URL-safe Base64 encoding), |
| 9 | * modified to support multiple Base64 variants. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/base64.h> |
| 17 | |
| 18 | static const char base64_tables[][65] = { |
| 19 | [BASE64_STD] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", |
| 20 | [BASE64_URLSAFE] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", |
| 21 | [BASE64_IMAP] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,", |
| 22 | }; |
| 23 | |
| 24 | /* |
| 25 | * Initialize the base64 reverse mapping for a single character |
| 26 | * This macro maps a character to its corresponding base64 value, |
| 27 | * returning -1 if the character is invalid. |
| 28 | * char 'A'-'Z' maps to 0-25, 'a'-'z' maps to 26-51, '0'-'9' maps to 52-61, |
| 29 | * ch_62 maps to 62, ch_63 maps to 63, and other characters return -1 |
| 30 | */ |
| 31 | #define INIT_1(v, ch_62, ch_63) \ |
| 32 | [v] = (v) >= 'A' && (v) <= 'Z' ? (v) - 'A' \ |
| 33 | : (v) >= 'a' && (v) <= 'z' ? (v) - 'a' + 26 \ |
| 34 | : (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \ |
| 35 | : (v) == (ch_62) ? 62 : (v) == (ch_63) ? 63 : -1 |
| 36 | |
| 37 | /* |
| 38 | * Recursive macros to generate multiple Base64 reverse mapping table entries. |
| 39 | * Each macro generates a sequence of entries in the lookup table: |
| 40 | * INIT_2 generates 2 entries, INIT_4 generates 4, INIT_8 generates 8, and so on up to INIT_32. |
| 41 | */ |
| 42 | #define INIT_2(v, ...) INIT_1(v, __VA_ARGS__), INIT_1((v) + 1, __VA_ARGS__) |
| 43 | #define INIT_4(v, ...) INIT_2(v, __VA_ARGS__), INIT_2((v) + 2, __VA_ARGS__) |
| 44 | #define INIT_8(v, ...) INIT_4(v, __VA_ARGS__), INIT_4((v) + 4, __VA_ARGS__) |
| 45 | #define INIT_16(v, ...) INIT_8(v, __VA_ARGS__), INIT_8((v) + 8, __VA_ARGS__) |
| 46 | #define INIT_32(v, ...) INIT_16(v, __VA_ARGS__), INIT_16((v) + 16, __VA_ARGS__) |
| 47 | |
| 48 | #define BASE64_REV_INIT(ch_62, ch_63) { \ |
| 49 | [0 ... 0x1f] = -1, \ |
| 50 | INIT_32(0x20, ch_62, ch_63), \ |
| 51 | INIT_32(0x40, ch_62, ch_63), \ |
| 52 | INIT_32(0x60, ch_62, ch_63), \ |
| 53 | [0x80 ... 0xff] = -1 } |
| 54 | |
| 55 | static const s8 base64_rev_maps[][256] = { |
| 56 | [BASE64_STD] = BASE64_REV_INIT('+', '/'), |
| 57 | [BASE64_URLSAFE] = BASE64_REV_INIT('-', '_'), |
| 58 | [BASE64_IMAP] = BASE64_REV_INIT('+', ',') |
| 59 | }; |
| 60 | |
| 61 | #undef BASE64_REV_INIT |
| 62 | #undef INIT_32 |
| 63 | #undef INIT_16 |
| 64 | #undef INIT_8 |
| 65 | #undef INIT_4 |
| 66 | #undef INIT_2 |
| 67 | #undef INIT_1 |
| 68 | /** |
| 69 | * base64_encode() - Base64-encode some binary data |
| 70 | * @src: the binary data to encode |
| 71 | * @srclen: the length of @src in bytes |
| 72 | * @dst: (output) the Base64-encoded string. Not NUL-terminated. |
| 73 | * @padding: whether to append '=' padding characters |
| 74 | * @variant: which base64 variant to use |
| 75 | * |
| 76 | * Encodes data using the selected Base64 variant. |
| 77 | * |
| 78 | * Return: the length of the resulting Base64-encoded string in bytes. |
| 79 | */ |
| 80 | int base64_encode(const u8 *src, int srclen, char *dst, bool padding, enum base64_variant variant) |
| 81 | { |
| 82 | u32 ac = 0; |
| 83 | char *cp = dst; |
| 84 | const char *base64_table = base64_tables[variant]; |
| 85 | |
| 86 | while (srclen >= 3) { |
| 87 | ac = src[0] << 16 | src[1] << 8 | src[2]; |
| 88 | *cp++ = base64_table[ac >> 18]; |
| 89 | *cp++ = base64_table[(ac >> 12) & 0x3f]; |
| 90 | *cp++ = base64_table[(ac >> 6) & 0x3f]; |
| 91 | *cp++ = base64_table[ac & 0x3f]; |
| 92 | |
| 93 | src += 3; |
| 94 | srclen -= 3; |
| 95 | } |
| 96 | |
| 97 | switch (srclen) { |
| 98 | case 2: |
| 99 | ac = src[0] << 16 | src[1] << 8; |
| 100 | *cp++ = base64_table[ac >> 18]; |
| 101 | *cp++ = base64_table[(ac >> 12) & 0x3f]; |
| 102 | *cp++ = base64_table[(ac >> 6) & 0x3f]; |
| 103 | if (padding) |
| 104 | *cp++ = '='; |
| 105 | break; |
| 106 | case 1: |
| 107 | ac = src[0] << 16; |
| 108 | *cp++ = base64_table[ac >> 18]; |
| 109 | *cp++ = base64_table[(ac >> 12) & 0x3f]; |
| 110 | if (padding) { |
| 111 | *cp++ = '='; |
| 112 | *cp++ = '='; |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | return cp - dst; |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(base64_encode); |
| 119 | |
| 120 | /** |
| 121 | * base64_decode() - Base64-decode a string |
| 122 | * @src: the string to decode. Doesn't need to be NUL-terminated. |
| 123 | * @srclen: the length of @src in bytes |
| 124 | * @dst: (output) the decoded binary data |
| 125 | * @padding: whether the input is expected to include '=' padding characters |
| 126 | * @variant: which base64 variant to use |
| 127 | * |
| 128 | * Decodes a string using the selected Base64 variant. |
| 129 | * |
| 130 | * Return: the length of the resulting decoded binary data in bytes, |
| 131 | * or -1 if the string isn't a valid Base64 string. |
| 132 | */ |
| 133 | int base64_decode(const char *src, int srclen, u8 *dst, bool padding, enum base64_variant variant) |
| 134 | { |
| 135 | u8 *bp = dst; |
| 136 | s8 input[4]; |
| 137 | s32 val; |
| 138 | const u8 *s = (const u8 *)src; |
| 139 | const s8 *base64_rev_tables = base64_rev_maps[variant]; |
| 140 | |
| 141 | while (srclen >= 4) { |
| 142 | input[0] = base64_rev_tables[s[0]]; |
| 143 | input[1] = base64_rev_tables[s[1]]; |
| 144 | input[2] = base64_rev_tables[s[2]]; |
| 145 | input[3] = base64_rev_tables[s[3]]; |
| 146 | |
| 147 | val = input[0] << 18 | input[1] << 12 | input[2] << 6 | input[3]; |
| 148 | |
| 149 | if (unlikely(val < 0)) { |
| 150 | if (!padding || srclen != 4 || s[3] != '=') |
| 151 | return -1; |
| 152 | padding = 0; |
| 153 | srclen = s[2] == '=' ? 2 : 3; |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | *bp++ = val >> 16; |
| 158 | *bp++ = val >> 8; |
| 159 | *bp++ = val; |
| 160 | |
| 161 | s += 4; |
| 162 | srclen -= 4; |
| 163 | } |
| 164 | |
| 165 | if (likely(!srclen)) |
| 166 | return bp - dst; |
| 167 | if (padding || srclen == 1) |
| 168 | return -1; |
| 169 | |
| 170 | val = (base64_rev_tables[s[0]] << 12) | (base64_rev_tables[s[1]] << 6); |
| 171 | |
| 172 | if (srclen == 2) { |
| 173 | if (val & 0x800003ff) |
| 174 | return -1; |
| 175 | *bp++ = val >> 10; |
| 176 | } else { |
| 177 | val |= base64_rev_tables[s[2]]; |
| 178 | if (val & 0x80000003) |
| 179 | return -1; |
| 180 | *bp++ = val >> 10; |
| 181 | *bp++ = val >> 2; |
| 182 | } |
| 183 | return bp - dst; |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(base64_decode); |