1 /* base64.h
2 
3    Base-64 encoding and decoding.
4 
5    Copyright (C) 2002 Niels Möller, Dan Egnor
6 
7    This file is part of GNU Nettle.
8 
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11 
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15 
16    or
17 
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21 
22    or both in parallel, as here.
23 
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28 
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33 
34 #ifndef NETTLE_BASE64_H_INCLUDED
35 #define NETTLE_BASE64_H_INCLUDED
36 
37 #include "nettle-types.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /* Name mangling */
44 #define base64_encode_init nettle_base64_encode_init
45 #define base64url_encode_init nettle_base64url_encode_init
46 #define base64_encode_single nettle_base64_encode_single
47 #define base64_encode_update nettle_base64_encode_update
48 #define base64_encode_final nettle_base64_encode_final
49 #define base64_encode_raw nettle_base64_encode_raw
50 #define base64_encode_group nettle_base64_encode_group
51 #define base64_decode_init nettle_base64_decode_init
52 #define base64url_decode_init nettle_base64url_decode_init
53 #define base64_decode_single nettle_base64_decode_single
54 #define base64_decode_update nettle_base64_decode_update
55 #define base64_decode_final nettle_base64_decode_final
56 
57 #define BASE64_BINARY_BLOCK_SIZE 3
58 #define BASE64_TEXT_BLOCK_SIZE 4
59 
60 /* Base64 encoding */
61 
62 /* Maximum length of output for base64_encode_update. NOTE: Doesn't
63  * include any padding that base64_encode_final may add. */
64 /* We have at most 4 buffered bits, and a total of (4 + length * 8) bits. */
65 #define BASE64_ENCODE_LENGTH(length) (((length) * 8 + 4)/6)
66 
67 /* Maximum length of output generated by base64_encode_final. */
68 #define BASE64_ENCODE_FINAL_LENGTH 3
69 
70 /* Exact length of output generated by base64_encode_raw, including
71  * padding. */
72 #define BASE64_ENCODE_RAW_LENGTH(length) ((((length) + 2)/3)*4)
73 
74 struct base64_encode_ctx
75 {
76   const char *alphabet;    /* Alphabet to use for encoding */
77   unsigned short word;     /* Leftover bits */
78   unsigned char bits;      /* Number of bits, always 0, 2, or 4. */
79 };
80 
81 /* Initialize encoding context for base-64 */
82 void
83 base64_encode_init(struct base64_encode_ctx *ctx);
84 
85 /* Initialize encoding context for URL safe alphabet, RFC 4648. */
86 void
87 base64url_encode_init(struct base64_encode_ctx *ctx);
88 
89 /* Encodes a single byte. Returns amount of output (always 1 or 2). */
90 size_t
91 base64_encode_single(struct base64_encode_ctx *ctx,
92 		     char *dst,
93 		     uint8_t src);
94 
95 /* Returns the number of output characters. DST should point to an
96  * area of size at least BASE64_ENCODE_LENGTH(length). */
97 size_t
98 base64_encode_update(struct base64_encode_ctx *ctx,
99 		     char *dst,
100 		     size_t length,
101 		     const uint8_t *src);
102 
103 /* DST should point to an area of size at least
104  * BASE64_ENCODE_FINAL_LENGTH */
105 size_t
106 base64_encode_final(struct base64_encode_ctx *ctx,
107 		    char *dst);
108 
109 /* Lower level functions */
110 
111 /* Encodes a string in one go, including any padding at the end.
112  * Generates exactly BASE64_ENCODE_RAW_LENGTH(length) bytes of output.
113  * Supports overlapped operation, if src <= dst. FIXME: Use of overlap
114  * is deprecated, if needed there should be a separate public fucntion
115  * to do that.*/
116 void
117 base64_encode_raw(char *dst, size_t length, const uint8_t *src);
118 
119 void
120 base64_encode_group(char *dst, uint32_t group);
121 
122 
123 /* Base64 decoding */
124 
125 /* Maximum length of output for base64_decode_update. */
126 /* We have at most 6 buffered bits, and a total of (length + 1) * 6 bits. */
127 #define BASE64_DECODE_LENGTH(length) ((((length) + 1) * 6) / 8)
128 
129 struct base64_decode_ctx
130 {
131   const signed char *table; /* Decoding table */
132   unsigned short word;      /* Leftover bits */
133   unsigned char bits;       /* Number buffered bits */
134 
135   /* Number of padding characters encountered */
136   unsigned char padding;
137 };
138 
139 /* Initialize decoding context for base-64 */
140 void
141 base64_decode_init(struct base64_decode_ctx *ctx);
142 
143 /* Initialize encoding context for URL safe alphabet, RFC 4648. */
144 void
145 base64url_decode_init(struct base64_decode_ctx *ctx);
146 
147 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
148  * errors. */
149 int
150 base64_decode_single(struct base64_decode_ctx *ctx,
151 		     uint8_t *dst,
152 		     char src);
153 
154 /* Returns 1 on success, 0 on error. DST should point to an area of
155  * size at least BASE64_DECODE_LENGTH(length). The amount of data
156  * generated is returned in *DST_LENGTH. */
157 int
158 base64_decode_update(struct base64_decode_ctx *ctx,
159 		     size_t *dst_length,
160 		     uint8_t *dst,
161 		     size_t src_length,
162 		     const char *src);
163 
164 /* Returns 1 on success. */
165 int
166 base64_decode_final(struct base64_decode_ctx *ctx);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* NETTLE_BASE64_H_INCLUDED */
173