2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * Get the appropriate Base64 character for a numeric value in the
29 * 0..63 range. This is constant-time.
35 * Values 0 to 25 map to 0x41..0x5A ('A' to 'Z')
36 * Values 26 to 51 map to 0x61..0x7A ('a' to 'z')
37 * Values 52 to 61 map to 0x30..0x39 ('0' to '9')
38 * Value 62 maps to 0x2B ('+')
39 * Value 63 maps to 0x2F ('/')
48 * Looking at bits 8..15 of values a, b and c:
51 * ---------------------
57 return (char)(((x + 0x41) & ((a & b & c) >> 8))
58 | ((x + (0x61 - 26)) & ((~a & b & c) >> 8))
59 | ((x - (52 - 0x30)) & ((~a & ~b & c) >> 8))
60 | ((0x2B + ((x & 1) << 2)) & (~(a | b | c) >> 8)));
63 /* see bearssl_pem.h */
65 br_pem_encode(void *dest, const void *data, size_t len,
66 const char *banner, unsigned flags)
68 size_t dlen, banner_len, lines;
74 banner_len = strlen(banner);
75 /* FIXME: try to avoid divisions here, as they may pull
76 an extra libc function. */
77 if ((flags & BR_PEM_LINE64) != 0) {
78 lines = (len + 47) / 48;
80 lines = (len + 56) / 57;
82 dlen = (banner_len << 1) + 30 + (((len + 2) / 3) << 2)
84 if ((flags & BR_PEM_CRLF) != 0) {
95 * We always move the source data to the end of output buffer;
96 * the encoding process never "catches up" except at the very
97 * end. This also handles all conditions of partial or total
100 buf = (unsigned char *)d + dlen - len;
101 memmove(buf, data, len);
103 memcpy(d, "-----BEGIN ", 11);
105 memcpy(d, banner, banner_len);
107 memcpy(d, "-----", 5);
109 if ((flags & BR_PEM_CRLF) != 0) {
115 lim = (flags & BR_PEM_LINE64) != 0 ? 16 : 19;
116 for (u = 0; (u + 2) < len; u += 3) {
119 w = ((uint32_t)buf[u] << 16)
120 | ((uint32_t)buf[u + 1] << 8)
121 | (uint32_t)buf[u + 2];
122 *d ++ = b64char(w >> 18);
123 *d ++ = b64char((w >> 12) & 0x3F);
124 *d ++ = b64char((w >> 6) & 0x3F);
125 *d ++ = b64char(w & 0x3F);
128 if ((flags & BR_PEM_CRLF) != 0) {
137 w = (uint32_t)buf[u] << 16;
139 w |= (uint32_t)buf[u + 1] << 8;
141 *d ++ = b64char(w >> 18);
142 *d ++ = b64char((w >> 12) & 0x3F);
144 *d ++ = b64char((w >> 6) & 0x3F);
152 if ((flags & BR_PEM_CRLF) != 0) {
158 memcpy(d, "-----END ", 9);
160 memcpy(d, banner, banner_len);
162 memcpy(d, "-----", 5);
164 if ((flags & BR_PEM_CRLF) != 0) {
169 /* Final zero, not counted in returned length. */