1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  *
9  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
10  */
11 #include "tomcrypt.h"
12 
13 /**
14   @file base64_decode.c
15   Compliant base64 code donated by Wayne Scott (wscott@bitmover.com)
16   base64 URL Safe variant (RFC 4648 section 5) by Karel Miko
17 */
18 
19 
20 #if defined(LTC_BASE64) || defined (LTC_BASE64_URL)
21 
22 #if defined(LTC_BASE64)
23 static const unsigned char map_base64[256] = {
24 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
25 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
26 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
27 255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
28  52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
29 255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
30   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
31  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
32 255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
33  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
34  49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
35 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
36 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
37 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
38 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
39 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
40 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
41 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
42 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
43 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
44 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
45 255, 255, 255, 255 };
46 #endif /* LTC_BASE64 */
47 
48 #if defined(LTC_BASE64_URL)
49 static const unsigned char map_base64url[256] = {
50 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
51 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
52 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
53 255, 255, 255, 255, 255, 255, 255, 255, 255,  62, 255, 255,
54  52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
55 255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
56   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
57  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255,  63,
58 255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
59  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
60  49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
61 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
62 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
63 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
64 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
65 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
66 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
67 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
68 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
69 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
70 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
71 255, 255, 255, 255 };
72 #endif /* LTC_BASE64_URL */
73 
_base64_decode_internal(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen,const unsigned char * map)74 static int _base64_decode_internal(const unsigned char *in,  unsigned long inlen,
75                                  unsigned char *out, unsigned long *outlen,
76                            const unsigned char *map)
77 {
78    unsigned long t, x, y, z;
79    unsigned char c;
80    int           g;
81 
82    LTC_ARGCHK(in     != NULL);
83    LTC_ARGCHK(out    != NULL);
84    LTC_ARGCHK(outlen != NULL);
85 
86    g = 3;
87    for (x = y = z = t = 0; x < inlen; x++) {
88        c = map[in[x]&0xFF];
89        if (c == 255) continue;
90        /* the final = symbols are read and used to trim the remaining bytes */
91        if (c == 254) {
92           c = 0;
93           /* prevent g < 0 which would potentially allow an overflow later */
94           if (--g < 0) {
95              return CRYPT_INVALID_PACKET;
96           }
97        } else if (g != 3) {
98           /* we only allow = to be at the end */
99           return CRYPT_INVALID_PACKET;
100        }
101 
102        t = (t<<6)|c;
103 
104        if (++y == 4) {
105           if (z + g > *outlen) {
106              return CRYPT_BUFFER_OVERFLOW;
107           }
108           out[z++] = (unsigned char)((t>>16)&255);
109           if (g > 1) out[z++] = (unsigned char)((t>>8)&255);
110           if (g > 2) out[z++] = (unsigned char)(t&255);
111           y = t = 0;
112        }
113    }
114    if (y != 0) {
115        return CRYPT_INVALID_PACKET;
116    }
117    *outlen = z;
118    return CRYPT_OK;
119 }
120 
121 #if defined(LTC_BASE64)
122 /**
123    base64 decode a block of memory
124    @param in       The base64 data to decode
125    @param inlen    The length of the base64 data
126    @param out      [out] The destination of the binary decoded data
127    @param outlen   [in/out] The max size and resulting size of the decoded data
128    @return CRYPT_OK if successful
129 */
base64_decode(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)130 int base64_decode(const unsigned char *in,  unsigned long inlen,
131                         unsigned char *out, unsigned long *outlen)
132 {
133     return _base64_decode_internal(in, inlen, out, outlen, map_base64);
134 }
135 #endif /* LTC_BASE64 */
136 
137 #if defined(LTC_BASE64_URL)
138 /**
139    base64 (URL Safe, RFC 4648 section 5) decode a block of memory
140    @param in       The base64 data to decode
141    @param inlen    The length of the base64 data
142    @param out      [out] The destination of the binary decoded data
143    @param outlen   [in/out] The max size and resulting size of the decoded data
144    @return CRYPT_OK if successful
145 */
base64url_decode(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)146 int base64url_decode(const unsigned char *in,  unsigned long inlen,
147                            unsigned char *out, unsigned long *outlen)
148 {
149     return _base64_decode_internal(in, inlen, out, outlen, map_base64url);
150 }
151 #endif /* LTC_BASE64_URL */
152 
153 #endif
154 
155 
156 /* $Source$ */
157 /* $Revision$ */
158 /* $Date$ */
159