source: openpam/trunk/lib/liboath/oath_base32.c @ 771

Last change on this file since 771 was 771, checked in by Dag-Erling Smørgrav, 7 years ago

Replace base{32,64}_decode() with table-driven implementations. The new
code is less strict about padding, thus ensuring compatibility with
implementations which do not understand padding, such as MIME::Base32.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/*-
2 * Copyright (c) 2013-2014 Universitetet i Oslo
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $Id: oath_base32.c 771 2014-03-06 17:54:58Z des $
30 */
31
32#ifdef HAVE_CONFIG_H
33# include "config.h"
34#endif
35
36#include <sys/types.h>
37
38#include <errno.h>
39#include <stdint.h>
40#include <stdio.h>
41
42#include <security/oath.h>
43
44static const char b32enc[] =
45    "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
46
47static const uint8_t b32dec[256] = {
48        ['A'] =  0, ['B'] =  1, ['C'] =  2, ['D'] =  3,
49        ['E'] =  4, ['F'] =  5, ['G'] =  6, ['H'] =  7,
50        ['I'] =  8, ['J'] =  9, ['K'] = 10, ['L'] = 11,
51        ['M'] = 12, ['N'] = 13, ['O'] = 14, ['P'] = 15,
52        ['Q'] = 16, ['R'] = 17, ['S'] = 18, ['T'] = 19,
53        ['U'] = 20, ['V'] = 21, ['W'] = 22, ['X'] = 23,
54        ['Y'] = 24, ['Z'] = 25,
55
56        ['a'] =  0, ['b'] =  1, ['c'] =  2, ['d'] =  3,
57        ['e'] =  4, ['f'] =  5, ['g'] =  6, ['h'] =  7,
58        ['i'] =  8, ['j'] =  9, ['k'] = 10, ['l'] = 11,
59        ['m'] = 12, ['n'] = 13, ['o'] = 14, ['p'] = 15,
60        ['q'] = 16, ['r'] = 17, ['s'] = 18, ['t'] = 19,
61        ['u'] = 20, ['v'] = 21, ['w'] = 22, ['x'] = 23,
62        ['y'] = 24, ['z'] = 25,
63
64        ['2'] = 26, ['3'] = 27, ['4'] = 28, ['5'] = 29,
65        ['6'] = 30, ['7'] = 31,
66};
67
68/*
69 * Encode data in RFC 4648 base 32 representation.  The target buffer must
70 * have room for base32_enclen(len) characters and a terminating NUL.
71 */
72int
73base32_enc(const uint8_t *in, size_t ilen, char *out, size_t *olen)
74{
75        uint64_t bits;
76
77        if (*olen <= base32_enclen(ilen)) {
78                errno = ENOSPC;
79                return (-1);
80        }
81        *olen = 0;
82        while (ilen >= 5) {
83                bits = 0;
84                bits |= (uint64_t)in[0] << 32;
85                bits |= (uint64_t)in[1] << 24;
86                bits |= (uint64_t)in[2] << 16;
87                bits |= (uint64_t)in[3] << 8;
88                bits |= (uint64_t)in[4];
89                ilen -= 5;
90                in += 5;
91                out[0] = b32enc[bits >> 35 & 0x1f];
92                out[1] = b32enc[bits >> 30 & 0x1f];
93                out[2] = b32enc[bits >> 25 & 0x1f];
94                out[3] = b32enc[bits >> 20 & 0x1f];
95                out[4] = b32enc[bits >> 15 & 0x1f];
96                out[5] = b32enc[bits >> 10 & 0x1f];
97                out[6] = b32enc[bits >> 5 & 0x1f];
98                out[7] = b32enc[bits & 0x1f];
99                *olen += 8;
100                out += 8;
101        }
102        if (ilen > 0) {
103                bits = 0;
104                switch (ilen) {
105                case 4:
106                        bits |= (uint64_t)in[3] << 8;
107                case 3:
108                        bits |= (uint64_t)in[2] << 16;
109                case 2:
110                        bits |= (uint64_t)in[1] << 24;
111                case 1:
112                        bits |= (uint64_t)in[0] << 32;
113                }
114                out[0] = b32enc[bits >> 35 & 0x1f];
115                out[1] = b32enc[bits >> 30 & 0x1f];
116                out[2] = ilen > 1 ? b32enc[bits >> 25 & 0x1f] : '=';
117                out[3] = ilen > 1 ? b32enc[bits >> 20 & 0x1f] : '=';
118                out[4] = ilen > 2 ? b32enc[bits >> 15 & 0x1f] : '=';
119                out[5] = ilen > 3 ? b32enc[bits >> 10 & 0x1f] : '=';
120                out[6] = ilen > 3 ? b32enc[bits >> 5 & 0x1f] : '=';
121                out[7] = '=';
122                *olen += 8;
123                out += 8;
124        }
125        out[0] = '\0';
126        ++*olen;
127        return (0);
128}
129
130/*
131 * Decode data in RFC 4648 base 32 representation, stopping at the
132 * terminating NUL, the first invalid (non-base32, non-whitespace)
133 * character or after len characters, whichever comes first.
134 *
135 * Padding is handled sloppily: any padding character following the data
136 * is silently consumed.  This not only simplifies the code but ensures
137 * compatibility with implementations which do not emit or understand
138 * padding.
139 *
140 * The olen argument is used by the caller to pass the size of the buffer
141 * and by base32_dec() to return the amount of data successfully decoded.
142 * If the buffer is too small, base32_dec() discards the excess data, but
143 * returns the total amount.
144 */
145int
146base32_dec(const char *in, size_t ilen, uint8_t *out, size_t *olen)
147{
148        size_t len;
149        int bits, shift, padding;
150
151        for (bits = shift = padding = len = 0; ilen && *in; --ilen, ++in) {
152                if (*in == ' ' || *in == '\t' || *in == '\r' || *in == '\n' ||
153                    (padding && *in == '=')) {
154                        /* consume */
155                        continue;
156                } else if (!padding && b32dec[(int)*in]) {
157                        /* shift into accumulator */
158                        shift += 5;
159                        bits = bits << 5 | b32dec[(int)*in];
160                } else if (!padding && shift && *in == '=') {
161                        /* final byte */
162                        shift = 0;
163                        padding = 1;
164                } else {
165                        /* error */
166                        *olen = 0;
167                        errno = EINVAL;
168                        return (-1);
169                }
170                if (shift >= 8) {
171                        /* output accumulated byte */
172                        shift -= 8;
173                        if (len++ < *olen)
174                                *out++ = (bits >> shift) & 0xff;
175                }
176        }
177        /* report decoded length */
178        *olen = len;
179        if (len > *olen) {
180                /* overflow */
181                errno = ENOSPC;
182                return (-1);
183        }
184        return (0);
185}
Note: See TracBrowser for help on using the repository browser.