summaryrefslogtreecommitdiffstats
path: root/3rd_party/libsrp6a-sha512/t_conv.c
diff options
context:
space:
mode:
Diffstat (limited to '3rd_party/libsrp6a-sha512/t_conv.c')
-rw-r--r--3rd_party/libsrp6a-sha512/t_conv.c258
1 files changed, 258 insertions, 0 deletions
diff --git a/3rd_party/libsrp6a-sha512/t_conv.c b/3rd_party/libsrp6a-sha512/t_conv.c
new file mode 100644
index 0000000..f7f50e2
--- /dev/null
+++ b/3rd_party/libsrp6a-sha512/t_conv.c
@@ -0,0 +1,258 @@
1/*
2 * Copyright (c) 1997-2007 The Stanford SRP Authentication Project
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
21 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
23 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
24 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * Redistributions in source or binary form must retain an intact copy
27 * of this copyright notice.
28 */
29
30/*#define _POSIX_SOURCE*/
31#include <stdio.h>
32#include "t_defines.h"
33#include "cstr.h"
34
35static int
36hexDigitToInt(c)
37 char c;
38{
39 if(c >= '0' && c <= '9')
40 return c - '0';
41 else if(c >= 'a' && c <= 'f')
42 return c - 'a' + 10;
43 else if(c >= 'A' && c <= 'F')
44 return c - 'A' + 10;
45 else
46 return 0;
47}
48
49/*
50 * Convert a hex string to a string of bytes; return size of dst
51 */
52_TYPE( int )
53t_fromhex(dst, src)
54 char * dst;
55 const char * src;
56{
57 register char *chp = dst;
58 register unsigned size = strlen(src);
59
60 /* FIXME: handle whitespace and non-hex digits by setting size and src
61 appropriately. */
62
63 if(size % 2 == 1) {
64 *chp++ = hexDigitToInt(*src++);
65 --size;
66 }
67 while(size > 0) {
68 *chp++ = (hexDigitToInt(*src) << 4) | hexDigitToInt(*(src + 1));
69 src += 2;
70 size -= 2;
71 }
72 return chp - dst;
73}
74
75/*
76 * Convert a string of bytes to their hex representation
77 */
78_TYPE( char * )
79t_tohex(dst, src, size)
80 char * dst;
81 const char * src;
82 unsigned size;
83{
84 int notleading = 0;
85
86 register char *chp = dst;
87 *dst = '\0';
88 if (size != 0) do {
89 if(notleading || *src != '\0') {
90 if(!notleading && (*src & 0xf0) == 0) {
91 sprintf(chp, "%.1X", * (unsigned char *) src);
92 chp += 1;
93 }
94 else {
95 sprintf(chp, "%.2X", * (unsigned char *) src);
96 chp += 2;
97 }
98 notleading = 1;
99 }
100 ++src;
101 } while (--size != 0);
102 return dst;
103}
104
105_TYPE( char * )
106t_tohexcstr(dst, src, size)
107 cstr * dst;
108 const char * src;
109 unsigned size;
110{
111 cstr_set_length(dst, 2 * size + 1);
112 return t_tohex(dst->data, src, size);
113}
114
115static char b64table[] =
116 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
117
118/*
119 * Convert a base64 string into raw byte array representation.
120 */
121_TYPE( int )
122t_fromb64(dst, src)
123 char * dst;
124 const char * src;
125{
126 unsigned char *a;
127 char *loc;
128 int i, j;
129 unsigned int size;
130
131 while(*src && (*src == ' ' || *src == '\t' || *src == '\n'))
132 ++src;
133 size = strlen(src);
134
135 a = malloc((size + 1) * sizeof(unsigned char));
136 if(a == (unsigned char *) 0)
137 return -1;
138
139 i = 0;
140 while(i < size) {
141 loc = strchr(b64table, src[i]);
142 if(loc == (char *) 0)
143 break;
144 else
145 a[i] = loc - b64table;
146 ++i;
147 }
148 size = i;
149
150 i = size - 1;
151 j = size;
152 while(1) {
153 a[j] = a[i];
154 if(--i < 0)
155 break;
156 a[j] |= (a[i] & 3) << 6;
157 --j;
158 a[j] = (unsigned char) ((a[i] & 0x3c) >> 2);
159 if(--i < 0)
160 break;
161 a[j] |= (a[i] & 0xf) << 4;
162 --j;
163 a[j] = (unsigned char) ((a[i] & 0x30) >> 4);
164 if(--i < 0)
165 break;
166 a[j] |= (a[i] << 2);
167
168 a[--j] = 0;
169 if(--i < 0)
170 break;
171 }
172
173 while(a[j] == 0 && j <= size)
174 ++j;
175
176 memcpy(dst, a + j, size - j + 1);
177 free(a);
178 return size - j + 1;
179}
180
181_TYPE( int )
182t_cstrfromb64(dst, src)
183 cstr * dst;
184 const char * src;
185{
186 int len;
187 cstr_set_length(dst, (strlen(src) * 6 + 7) / 8);
188 len = t_fromb64(dst->data, src);
189 cstr_set_length(dst, len);
190 return len;
191}
192
193/*
194 * Convert a raw byte string into a null-terminated base64 ASCII string.
195 */
196_TYPE( char * )
197t_tob64(dst, src, size)
198 char * dst;
199 const char * src;
200 unsigned size;
201{
202 int c, pos = size % 3;
203 unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
204 char *olddst = dst;
205
206 switch(pos) {
207 case 1:
208 b2 = src[0];
209 break;
210 case 2:
211 b1 = src[0];
212 b2 = src[1];
213 break;
214 }
215
216 while(1) {
217 c = (b0 & 0xfc) >> 2;
218 if(notleading || c != 0) {
219 *dst++ = b64table[c];
220 notleading = 1;
221 }
222 c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
223 if(notleading || c != 0) {
224 *dst++ = b64table[c];
225 notleading = 1;
226 }
227 c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
228 if(notleading || c != 0) {
229 *dst++ = b64table[c];
230 notleading = 1;
231 }
232 c = b2 & 0x3f;
233 if(notleading || c != 0) {
234 *dst++ = b64table[c];
235 notleading = 1;
236 }
237 if(pos >= size)
238 break;
239 else {
240 b0 = src[pos++];
241 b1 = src[pos++];
242 b2 = src[pos++];
243 }
244 }
245
246 *dst++ = '\0';
247 return olddst;
248}
249
250_TYPE( char * )
251t_tob64cstr(dst, src, sz)
252 cstr * dst;
253 const char * src;
254 unsigned int sz;
255{
256 cstr_set_length(dst, (sz * 8 + 5) / 6 + 1);
257 return t_tob64(dst->data, src, sz);
258}