|
| 1 | +/**************************************************************************** |
| 2 | + * apps/fsutils/passwd/passwd_base64.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | + |
| 29 | +#include <errno.h> |
| 30 | +#include <stdint.h> |
| 31 | + |
| 32 | +#include "passwd_base64.h" |
| 33 | + |
| 34 | +/**************************************************************************** |
| 35 | + * Pre-processor Definitions |
| 36 | + ****************************************************************************/ |
| 37 | + |
| 38 | +/* RFC 4648 section 5 base64url alphabet (no padding). */ |
| 39 | + |
| 40 | +static const char g_base64url[] = |
| 41 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 42 | + |
| 43 | +/**************************************************************************** |
| 44 | + * Private Functions |
| 45 | + ****************************************************************************/ |
| 46 | + |
| 47 | +static int passwd_base64url_val(char c) |
| 48 | +{ |
| 49 | + if (c >= 'A' && c <= 'Z') |
| 50 | + { |
| 51 | + return c - 'A'; |
| 52 | + } |
| 53 | + |
| 54 | + if (c >= 'a' && c <= 'z') |
| 55 | + { |
| 56 | + return c - 'a' + 26; |
| 57 | + } |
| 58 | + |
| 59 | + if (c >= '0' && c <= '9') |
| 60 | + { |
| 61 | + return c - '0' + 52; |
| 62 | + } |
| 63 | + |
| 64 | + if (c == '-') |
| 65 | + { |
| 66 | + return 62; |
| 67 | + } |
| 68 | + |
| 69 | + if (c == '_') |
| 70 | + { |
| 71 | + return 63; |
| 72 | + } |
| 73 | + |
| 74 | + return -1; |
| 75 | +} |
| 76 | + |
| 77 | +/**************************************************************************** |
| 78 | + * Public Functions |
| 79 | + ****************************************************************************/ |
| 80 | + |
| 81 | +/**************************************************************************** |
| 82 | + * Name: passwd_base64url_encode |
| 83 | + * |
| 84 | + * Description: |
| 85 | + * Encode binary data as unpadded base64url (RFC 4648 section 5). |
| 86 | + * |
| 87 | + ****************************************************************************/ |
| 88 | + |
| 89 | +int passwd_base64url_encode(FAR const uint8_t *in, size_t inlen, |
| 90 | + FAR char *out, size_t outlen) |
| 91 | +{ |
| 92 | + uint32_t acc = 0; |
| 93 | + size_t i; |
| 94 | + size_t o = 0; |
| 95 | + int bits = 0; |
| 96 | + |
| 97 | + for (i = 0; i < inlen; i++) |
| 98 | + { |
| 99 | + acc = (acc << 8) | in[i]; |
| 100 | + bits += 8; |
| 101 | + |
| 102 | + while (bits >= 6) |
| 103 | + { |
| 104 | + if (o + 1 >= outlen) |
| 105 | + { |
| 106 | + return -E2BIG; |
| 107 | + } |
| 108 | + |
| 109 | + bits -= 6; |
| 110 | + out[o++] = g_base64url[(acc >> bits) & 0x3f]; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (bits > 0) |
| 115 | + { |
| 116 | + if (o + 1 >= outlen) |
| 117 | + { |
| 118 | + return -E2BIG; |
| 119 | + } |
| 120 | + |
| 121 | + out[o++] = g_base64url[(acc << (6 - bits)) & 0x3f]; |
| 122 | + } |
| 123 | + |
| 124 | + if (o >= outlen) |
| 125 | + { |
| 126 | + return -E2BIG; |
| 127 | + } |
| 128 | + |
| 129 | + out[o] = '\0'; |
| 130 | + return OK; |
| 131 | +} |
| 132 | + |
| 133 | +/**************************************************************************** |
| 134 | + * Name: passwd_base64url_decode |
| 135 | + * |
| 136 | + * Description: |
| 137 | + * Decode unpadded base64url (RFC 4648 section 5). |
| 138 | + * |
| 139 | + ****************************************************************************/ |
| 140 | + |
| 141 | +int passwd_base64url_decode(FAR const char *in, |
| 142 | + FAR uint8_t *out, size_t outmax, |
| 143 | + FAR size_t *outlen) |
| 144 | +{ |
| 145 | + uint32_t acc = 0; |
| 146 | + size_t o = 0; |
| 147 | + int bits = 0; |
| 148 | + int v; |
| 149 | + |
| 150 | + *outlen = 0; |
| 151 | + |
| 152 | + while (*in != '\0') |
| 153 | + { |
| 154 | + if (*in == '$' || *in == ':') |
| 155 | + { |
| 156 | + break; |
| 157 | + } |
| 158 | + |
| 159 | + v = passwd_base64url_val(*in++); |
| 160 | + if (v < 0) |
| 161 | + { |
| 162 | + return -EINVAL; |
| 163 | + } |
| 164 | + |
| 165 | + acc = (acc << 6) | (uint32_t)v; |
| 166 | + bits += 6; |
| 167 | + |
| 168 | + if (bits >= 8) |
| 169 | + { |
| 170 | + bits -= 8; |
| 171 | + if (o >= outmax) |
| 172 | + { |
| 173 | + return -E2BIG; |
| 174 | + } |
| 175 | + |
| 176 | + out[o++] = (uint8_t)((acc >> bits) & 0xff); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if (bits >= 6) |
| 181 | + { |
| 182 | + return -EINVAL; |
| 183 | + } |
| 184 | + |
| 185 | + *outlen = o; |
| 186 | + return OK; |
| 187 | +} |
0 commit comments