1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file base64_encode.c
7   Compliant base64 encoder donated by Wayne Scott (wscott@bitmover.com)
8   base64 URL Safe variant (RFC 4648 section 5) by Karel Miko
9 */
10 
11 
12 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
13 
14 #if defined(LTC_BASE64)
15 static const char * const codes_base64 =
16 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
17 #endif /* LTC_BASE64 */
18 
19 #if defined(LTC_BASE64_URL)
20 static const char * const codes_base64url =
21 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
22 #endif /* LTC_BASE64_URL */
23 
24 enum mode {
25    nopad = 0,
26    pad = 1,
27    lf = 2,
28    cr = 4,
29    ssh = 8,
30    crlf = lf | cr,
31 };
32 
s_base64_encode_internal(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen,const char * codes,unsigned int mode)33 static int s_base64_encode_internal(const unsigned char *in,    unsigned long inlen,
34                                                    char *out,   unsigned long *outlen,
35                                     const          char *codes, unsigned int  mode)
36 {
37    unsigned long i, len2, leven, linelen;
38    char *p;
39 
40    LTC_ARGCHK(outlen != NULL);
41 
42    linelen = (mode & ssh) ? 72 : 64;
43 
44    /* valid output size ? */
45    len2 = 4 * ((inlen + 2) / 3);
46    if ((mode & crlf) == lf) {
47       len2 += len2 / linelen;
48    } else if ((mode & crlf) == crlf) {
49       len2 += (len2 / linelen) * 2;
50    }
51    if (*outlen < len2 + 1) {
52       *outlen = len2 + 1;
53       return CRYPT_BUFFER_OVERFLOW;
54    }
55 
56    LTC_ARGCHK(in  != NULL);
57    LTC_ARGCHK(out != NULL);
58 
59    if ((void*)in == out) {
60       return CRYPT_INVALID_ARG;
61    }
62 
63    p = out;
64    leven = 3*(inlen / 3);
65    for (i = 0; i < leven; i += 3) {
66        *p++ = codes[(in[0] >> 2) & 0x3F];
67        *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
68        *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
69        *p++ = codes[in[2] & 0x3F];
70        in += 3;
71        if ((p - out) % linelen == 0) {
72           if (mode & cr) *p++ = '\r';
73           if (mode & lf) *p++ = '\n';
74        }
75    }
76    /* Pad it if necessary...  */
77    if (i < inlen) {
78        unsigned a = in[0];
79        unsigned b = (i+1 < inlen) ? in[1] : 0;
80 
81        *p++ = codes[(a >> 2) & 0x3F];
82        *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
83        if (mode & pad) {
84          *p++ = (i+1 < inlen) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
85          *p++ = '=';
86        }
87        else {
88          if (i+1 < inlen) *p++ = codes[(((b & 0xf) << 2)) & 0x3F];
89        }
90    }
91 
92    /* append a NULL byte */
93    *p = '\0';
94 
95    /* return ok */
96    *outlen = (unsigned long)(p - out); /* the length without terminating NUL */
97    return CRYPT_OK;
98 }
99 
100 #if defined(LTC_BASE64)
101 /**
102    base64 Encode a buffer (NUL terminated)
103    @param in      The input buffer to encode
104    @param inlen   The length of the input buffer
105    @param out     [out] The destination of the base64 encoded data
106    @param outlen  [in/out] The max size and resulting size
107    @return CRYPT_OK if successful
108 */
base64_encode(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen)109 int base64_encode(const unsigned char *in,  unsigned long inlen,
110                                  char *out, unsigned long *outlen)
111 {
112     return s_base64_encode_internal(in, inlen, out, outlen, codes_base64, pad);
113 }
114 
115 /**
116    base64 Encode a buffer for PEM output
117      (NUL terminated with line-break at 64 chars)
118    @param in       The input buffer to encode
119    @param inlen    The length of the input buffer
120    @param out      [out] The destination of the base64 encoded data
121    @param outlen   [in/out] The max size and resulting size
122    @param flags    \ref base64_pem_flags
123    @return CRYPT_OK if successful
124 */
base64_encode_pem(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen,unsigned int flags)125 int base64_encode_pem(const unsigned char *in,  unsigned long inlen,
126                                      char *out, unsigned long *outlen,
127                             unsigned int  flags)
128 {
129     int use_crlf = flags & BASE64_PEM_CRLF ? pad | crlf : pad | lf;
130     int ssh_style = flags & BASE64_PEM_SSH ? ssh : 0;
131     return s_base64_encode_internal(in, inlen, out, outlen, codes_base64, ssh_style | use_crlf);
132 }
133 #endif /* LTC_BASE64 */
134 
135 
136 #if defined(LTC_BASE64_URL)
137 /**
138    base64 (URL Safe, RFC 4648 section 5) Encode a buffer (NUL terminated)
139    @param in      The input buffer to encode
140    @param inlen   The length of the input buffer
141    @param out     [out] The destination of the base64 encoded data
142    @param outlen  [in/out] The max size and resulting size
143    @return CRYPT_OK if successful
144 */
base64url_encode(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen)145 int base64url_encode(const unsigned char *in,  unsigned long inlen,
146                                     char *out, unsigned long *outlen)
147 {
148     return s_base64_encode_internal(in, inlen, out, outlen, codes_base64url, nopad);
149 }
150 
base64url_strict_encode(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen)151 int base64url_strict_encode(const unsigned char *in,  unsigned long inlen,
152                                            char *out, unsigned long *outlen)
153 {
154     return s_base64_encode_internal(in, inlen, out, outlen, codes_base64url, pad);
155 }
156 #endif /* LTC_BASE64_URL */
157 
158 #endif
159 
160