1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file base64_encode.c
14   Compliant base64 encoder donated by Wayne Scott (wscott@bitmover.com)
15   base64 URL Safe variant (RFC 4648 section 5) by Karel Miko
16 */
17 
18 
19 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
20 
21 #if defined(LTC_BASE64)
22 static const char * const codes_base64 =
23 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24 #endif /* LTC_BASE64 */
25 
26 #if defined(LTC_BASE64_URL)
27 static const char * const codes_base64url =
28 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
29 #endif /* LTC_BASE64_URL */
30 
_base64_encode_internal(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen,const char * codes,int pad)31 static int _base64_encode_internal(const unsigned char *in,  unsigned long inlen,
32                                  char *out, unsigned long *outlen,
33                                  const char *codes, int pad)
34 {
35    unsigned long i, len2, leven;
36    char *p;
37 
38    LTC_ARGCHK(in     != NULL);
39    LTC_ARGCHK(out    != NULL);
40    LTC_ARGCHK(outlen != NULL);
41 
42    /* valid output size ? */
43    len2 = 4 * ((inlen + 2) / 3);
44    if (*outlen < len2 + 1) {
45       *outlen = len2 + 1;
46       return CRYPT_BUFFER_OVERFLOW;
47    }
48    p = out;
49    leven = 3*(inlen / 3);
50    for (i = 0; i < leven; i += 3) {
51        *p++ = codes[(in[0] >> 2) & 0x3F];
52        *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
53        *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
54        *p++ = codes[in[2] & 0x3F];
55        in += 3;
56    }
57    /* Pad it if necessary...  */
58    if (i < inlen) {
59        unsigned a = in[0];
60        unsigned b = (i+1 < inlen) ? in[1] : 0;
61 
62        *p++ = codes[(a >> 2) & 0x3F];
63        *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
64        if (pad) {
65          *p++ = (i+1 < inlen) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
66          *p++ = '=';
67        }
68        else {
69          if (i+1 < inlen) *p++ = codes[(((b & 0xf) << 2)) & 0x3F];
70        }
71    }
72 
73    /* append a NULL byte */
74    *p = '\0';
75 
76    /* return ok */
77    *outlen = (unsigned long)(p - out); /* the length without terminating NUL */
78    return CRYPT_OK;
79 }
80 
81 #if defined(LTC_BASE64)
82 /**
83    base64 Encode a buffer (NUL terminated)
84    @param in      The input buffer to encode
85    @param inlen   The length of the input buffer
86    @param out     [out] The destination of the base64 encoded data
87    @param outlen  [in/out] The max size and resulting size
88    @return CRYPT_OK if successful
89 */
base64_encode(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen)90 int base64_encode(const unsigned char *in,  unsigned long inlen,
91                                  char *out, unsigned long *outlen)
92 {
93     return _base64_encode_internal(in, inlen, out, outlen, codes_base64, 1);
94 }
95 #endif /* LTC_BASE64 */
96 
97 
98 #if defined(LTC_BASE64_URL)
99 /**
100    base64 (URL Safe, RFC 4648 section 5) Encode a buffer (NUL terminated)
101    @param in      The input buffer to encode
102    @param inlen   The length of the input buffer
103    @param out     [out] The destination of the base64 encoded data
104    @param outlen  [in/out] The max size and resulting size
105    @return CRYPT_OK if successful
106 */
base64url_encode(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen)107 int base64url_encode(const unsigned char *in,  unsigned long inlen,
108                                     char *out, unsigned long *outlen)
109 {
110     return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0);
111 }
112 
base64url_strict_encode(const unsigned char * in,unsigned long inlen,char * out,unsigned long * outlen)113 int base64url_strict_encode(const unsigned char *in,  unsigned long inlen,
114                                            char *out, unsigned long *outlen)
115 {
116     return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 1);
117 }
118 #endif /* LTC_BASE64_URL */
119 
120 #endif
121 
122 
123 /* ref:         $Format:%D$ */
124 /* git commit:  $Format:%H$ */
125 /* commit time: $Format:%ai$ */
126