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

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

When I changed the argument type from uint8_t * to char *, I forgot that
they were being used as array indices. Cast them back to uint8_t.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.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 779 2014-03-09 14:11:44Z 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
41#include <security/oath.h>
42
43#include "oath_impl.h"
44
45static const char b32enc[] =
46    "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
47
48static const char b32dec[256] = {
49        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
50        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
51        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
52        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
53        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55        0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
56        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57        0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
58        0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
59        0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
60        0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
61        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
81};
82
83/*
84 * Encode data in RFC 4648 base 32 representation.  The target buffer must
85 * have room for base32_enclen(len) characters and a terminating NUL.
86 */
87int
88base32_enc(const char *cin, size_t ilen, char *out, size_t *olen)
89{
90        const uint8_t *in = (uint8_t *)cin;
91        uint64_t bits;
92
93        if (*olen <= base32_enclen(ilen)) {
94                *olen = base32_enclen(ilen) + 1;
95                errno = ENOSPC;
96                return (-1);
97        }
98        *olen = 0;
99        while (ilen >= 5) {
100                bits = 0;
101                bits |= (uint64_t)in[0] << 32;
102                bits |= (uint64_t)in[1] << 24;
103                bits |= (uint64_t)in[2] << 16;
104                bits |= (uint64_t)in[3] << 8;
105                bits |= (uint64_t)in[4];
106                ilen -= 5;
107                in += 5;
108                out[0] = b32enc[bits >> 35 & 0x1f];
109                out[1] = b32enc[bits >> 30 & 0x1f];
110                out[2] = b32enc[bits >> 25 & 0x1f];
111                out[3] = b32enc[bits >> 20 & 0x1f];
112                out[4] = b32enc[bits >> 15 & 0x1f];
113                out[5] = b32enc[bits >> 10 & 0x1f];
114                out[6] = b32enc[bits >> 5 & 0x1f];
115                out[7] = b32enc[bits & 0x1f];
116                *olen += 8;
117                out += 8;
118        }
119        if (ilen > 0) {
120                bits = 0;
121                switch (ilen) {
122                case 4:
123                        bits |= (uint64_t)in[3] << 8;
124                case 3:
125                        bits |= (uint64_t)in[2] << 16;
126                case 2:
127                        bits |= (uint64_t)in[1] << 24;
128                case 1:
129                        bits |= (uint64_t)in[0] << 32;
130                COVERAGE_NO_DEFAULT_CASE
131                }
132                out[0] = b32enc[bits >> 35 & 0x1f];
133                out[1] = b32enc[bits >> 30 & 0x1f];
134                out[2] = ilen > 1 ? b32enc[bits >> 25 & 0x1f] : '=';
135                out[3] = ilen > 1 ? b32enc[bits >> 20 & 0x1f] : '=';
136                out[4] = ilen > 2 ? b32enc[bits >> 15 & 0x1f] : '=';
137                out[5] = ilen > 3 ? b32enc[bits >> 10 & 0x1f] : '=';
138                out[6] = ilen > 3 ? b32enc[bits >> 5 & 0x1f] : '=';
139                out[7] = '=';
140                *olen += 8;
141                out += 8;
142        }
143        out[0] = '\0';
144        ++*olen;
145        return (0);
146}
147
148/*
149 * Decode data in RFC 4648 base 32 representation, stopping at the
150 * terminating NUL, the first invalid (non-base32, non-whitespace)
151 * character or after len characters, whichever comes first.
152 *
153 * Padding is handled sloppily: any padding character following the data
154 * is silently consumed.  This not only simplifies the code but ensures
155 * compatibility with implementations which do not emit or understand
156 * padding.
157 *
158 * The olen argument is used by the caller to pass the size of the buffer
159 * and by base32_dec() to return the amount of data successfully decoded.
160 * If the buffer is too small, base32_dec() discards the excess data, but
161 * returns the total amount.
162 */
163int
164base32_dec(const char *cin, size_t ilen, char *out, size_t *olen)
165{
166        const uint8_t *in = (uint8_t *)cin;
167        size_t len;
168        int bits, shift, padding;
169
170        for (bits = shift = padding = len = 0; ilen && *in; --ilen, ++in) {
171                if (*in == ' ' || *in == '\t' || *in == '\r' || *in == '\n' ||
172                    (padding && *in == '=')) {
173                        /* consume */
174                        continue;
175                } else if (!padding && b32dec[*in] >= 0) {
176                        /* shift into accumulator */
177                        shift += 5;
178                        bits = bits << 5 | b32dec[*in];
179                } else if (!padding && shift > 0 && shift < 5 && *in == '=') {
180                        /* final byte */
181                        shift = 0;
182                        padding = 1;
183                } else {
184                        /* error */
185                        *olen = 0;
186                        errno = EINVAL;
187                        return (-1);
188                }
189                if (shift >= 8) {
190                        /* output accumulated byte */
191                        shift -= 8;
192                        if (len++ < *olen)
193                                *out++ = (bits >> shift) & 0xff;
194                }
195        }
196        /* report decoded length */
197        if (len > *olen) {
198                /* overflow */
199                *olen = len;
200                errno = ENOSPC;
201                return (-1);
202        }
203        *olen = len;
204        return (0);
205}
Note: See TracBrowser for help on using the repository browser.