diff options
Diffstat (limited to '3rd_party/libsrp6a-sha512/t_conv.c')
-rw-r--r-- | 3rd_party/libsrp6a-sha512/t_conv.c | 239 |
1 files changed, 239 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..76d4e58 --- /dev/null +++ b/3rd_party/libsrp6a-sha512/t_conv.c | |||
@@ -0,0 +1,239 @@ | |||
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 | |||
35 | static int | ||
36 | hexDigitToInt(char c) | ||
37 | { | ||
38 | if(c >= '0' && c <= '9') | ||
39 | return c - '0'; | ||
40 | else if(c >= 'a' && c <= 'f') | ||
41 | return c - 'a' + 10; | ||
42 | else if(c >= 'A' && c <= 'F') | ||
43 | return c - 'A' + 10; | ||
44 | else | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | /* | ||
49 | * Convert a hex string to a string of bytes; return size of dst | ||
50 | */ | ||
51 | _TYPE( int ) | ||
52 | t_fromhex(char *dst, const char *src) | ||
53 | { | ||
54 | register char *chp = dst; | ||
55 | register unsigned size = strlen(src); | ||
56 | |||
57 | /* FIXME: handle whitespace and non-hex digits by setting size and src | ||
58 | appropriately. */ | ||
59 | |||
60 | if(size % 2 == 1) { | ||
61 | *chp++ = hexDigitToInt(*src++); | ||
62 | --size; | ||
63 | } | ||
64 | while(size > 0) { | ||
65 | *chp++ = (hexDigitToInt(*src) << 4) | hexDigitToInt(*(src + 1)); | ||
66 | src += 2; | ||
67 | size -= 2; | ||
68 | } | ||
69 | return chp - dst; | ||
70 | } | ||
71 | |||
72 | /* | ||
73 | * Convert a string of bytes to their hex representation | ||
74 | */ | ||
75 | _TYPE( char * ) | ||
76 | t_tohex(char *dst, const char *src, unsigned size) | ||
77 | { | ||
78 | int notleading = 0; | ||
79 | |||
80 | register char *chp = dst; | ||
81 | *dst = '\0'; | ||
82 | if (size != 0) do { | ||
83 | if(notleading || *src != '\0') { | ||
84 | if(!notleading && (*src & 0xf0) == 0) { | ||
85 | sprintf(chp, "%.1X", * (unsigned char *) src); | ||
86 | chp += 1; | ||
87 | } | ||
88 | else { | ||
89 | sprintf(chp, "%.2X", * (unsigned char *) src); | ||
90 | chp += 2; | ||
91 | } | ||
92 | notleading = 1; | ||
93 | } | ||
94 | ++src; | ||
95 | } while (--size != 0); | ||
96 | return dst; | ||
97 | } | ||
98 | |||
99 | _TYPE( char * ) | ||
100 | t_tohexcstr(cstr *dst, const char *src, unsigned size) | ||
101 | { | ||
102 | cstr_set_length(dst, 2 * size + 1); | ||
103 | return t_tohex(dst->data, src, size); | ||
104 | } | ||
105 | |||
106 | static char b64table[] = | ||
107 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"; | ||
108 | |||
109 | /* | ||
110 | * Convert a base64 string into raw byte array representation. | ||
111 | */ | ||
112 | _TYPE( int ) | ||
113 | t_fromb64(char *dst, const char *src) | ||
114 | { | ||
115 | unsigned char *a; | ||
116 | char *loc; | ||
117 | int i, j; | ||
118 | unsigned int size; | ||
119 | |||
120 | while(*src && (*src == ' ' || *src == '\t' || *src == '\n')) | ||
121 | ++src; | ||
122 | size = strlen(src); | ||
123 | |||
124 | a = malloc((size + 1) * sizeof(unsigned char)); | ||
125 | if(a == (unsigned char *) 0) | ||
126 | return -1; | ||
127 | |||
128 | i = 0; | ||
129 | while(i < size) { | ||
130 | loc = strchr(b64table, src[i]); | ||
131 | if(loc == (char *) 0) | ||
132 | break; | ||
133 | else | ||
134 | a[i] = loc - b64table; | ||
135 | ++i; | ||
136 | } | ||
137 | size = i; | ||
138 | |||
139 | i = size - 1; | ||
140 | j = size; | ||
141 | while(1) { | ||
142 | a[j] = a[i]; | ||
143 | if(--i < 0) | ||
144 | break; | ||
145 | a[j] |= (a[i] & 3) << 6; | ||
146 | --j; | ||
147 | a[j] = (unsigned char) ((a[i] & 0x3c) >> 2); | ||
148 | if(--i < 0) | ||
149 | break; | ||
150 | a[j] |= (a[i] & 0xf) << 4; | ||
151 | --j; | ||
152 | a[j] = (unsigned char) ((a[i] & 0x30) >> 4); | ||
153 | if(--i < 0) | ||
154 | break; | ||
155 | a[j] |= (a[i] << 2); | ||
156 | |||
157 | a[--j] = 0; | ||
158 | if(--i < 0) | ||
159 | break; | ||
160 | } | ||
161 | |||
162 | while(a[j] == 0 && j <= size) | ||
163 | ++j; | ||
164 | |||
165 | memcpy(dst, a + j, size - j + 1); | ||
166 | free(a); | ||
167 | return size - j + 1; | ||
168 | } | ||
169 | |||
170 | _TYPE( int ) | ||
171 | t_cstrfromb64(cstr *dst, const char *src) | ||
172 | { | ||
173 | int len; | ||
174 | cstr_set_length(dst, (strlen(src) * 6 + 7) / 8); | ||
175 | len = t_fromb64(dst->data, src); | ||
176 | cstr_set_length(dst, len); | ||
177 | return len; | ||
178 | } | ||
179 | |||
180 | /* | ||
181 | * Convert a raw byte string into a null-terminated base64 ASCII string. | ||
182 | */ | ||
183 | _TYPE( char * ) | ||
184 | t_tob64(char *dst, const char *src, unsigned size) | ||
185 | { | ||
186 | int c, pos = size % 3; | ||
187 | unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0; | ||
188 | char *olddst = dst; | ||
189 | |||
190 | switch(pos) { | ||
191 | case 1: | ||
192 | b2 = src[0]; | ||
193 | break; | ||
194 | case 2: | ||
195 | b1 = src[0]; | ||
196 | b2 = src[1]; | ||
197 | break; | ||
198 | } | ||
199 | |||
200 | while(1) { | ||
201 | c = (b0 & 0xfc) >> 2; | ||
202 | if(notleading || c != 0) { | ||
203 | *dst++ = b64table[c]; | ||
204 | notleading = 1; | ||
205 | } | ||
206 | c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4); | ||
207 | if(notleading || c != 0) { | ||
208 | *dst++ = b64table[c]; | ||
209 | notleading = 1; | ||
210 | } | ||
211 | c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6); | ||
212 | if(notleading || c != 0) { | ||
213 | *dst++ = b64table[c]; | ||
214 | notleading = 1; | ||
215 | } | ||
216 | c = b2 & 0x3f; | ||
217 | if(notleading || c != 0) { | ||
218 | *dst++ = b64table[c]; | ||
219 | notleading = 1; | ||
220 | } | ||
221 | if(pos >= size) | ||
222 | break; | ||
223 | else { | ||
224 | b0 = src[pos++]; | ||
225 | b1 = src[pos++]; | ||
226 | b2 = src[pos++]; | ||
227 | } | ||
228 | } | ||
229 | |||
230 | *dst++ = '\0'; | ||
231 | return olddst; | ||
232 | } | ||
233 | |||
234 | _TYPE( char * ) | ||
235 | t_tob64cstr(cstr *dst, const char *src, unsigned int sz) | ||
236 | { | ||
237 | cstr_set_length(dst, (sz * 8 + 5) / 6 + 1); | ||
238 | return t_tob64(dst->data, src, sz); | ||
239 | } | ||