2 * Copyright (c) 1997-1999 The Stanford SRP Authentication Project
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
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.
26 * In addition, the following conditions apply:
28 * 1. Any software that incorporates the SRP authentication technology
29 * must display the following acknowlegment:
30 * "This product uses the 'Secure Remote Password' cryptographic
31 * authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
33 * 2. Any software that incorporates all or part of the SRP distribution
34 * itself must also display the following acknowledgment:
35 * "This product includes software developed by Tom Wu and Eugene
36 * Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
38 * 3. Redistributions in source or binary form must retain an intact copy
39 * of this copyright notice and list of conditions.
42 /*#define _POSIX_SOURCE*/
44 #include "t_defines.h"
50 if(c >= '0' && c <= '9')
52 else if(c >= 'a' && c <= 'f')
54 else if(c >= 'A' && c <= 'F')
61 * Convert a hex string to a string of bytes; return size of dst
65 register char *dst, *src;
67 register char *chp = dst;
68 register unsigned size = strlen(src);
70 /* FIXME: handle whitespace and non-hex digits by setting size and src
74 *chp++ = hexDigitToInt(*src++);
78 *chp++ = (hexDigitToInt(*src) << 4) | hexDigitToInt(*(src + 1));
86 * Convert a string of bytes to their hex representation
89 t_tohex(dst, src, size)
90 register char *dst, *src;
91 register unsigned size;
95 register char *chp = dst;
97 if(notleading || *src != '\0') {
99 sprintf(chp, "%.2x", * (unsigned char *) src);
103 } while (--size != 0);
107 static char b64table[] =
108 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
111 * Convert a base64 string into raw byte array representation.
115 register char *dst, *src;
122 while(*src && (*src == ' ' || *src == '\t' || *src == '\n'))
126 a = malloc((size + 1) * sizeof(unsigned char));
127 if(a == (unsigned char *) 0)
132 loc = strchr(b64table, src[i]);
133 if(loc == (char *) 0)
136 a[i] = loc - b64table;
147 a[j] |= (a[i] & 3) << 6;
149 a[j] = (unsigned char) ((a[i] & 0x3c) >> 2);
152 a[j] |= (a[i] & 0xf) << 4;
154 a[j] = (unsigned char) ((a[i] & 0x30) >> 4);
164 while(j <= size && a[j] == 0)
167 memcpy(dst, a + j, size - j + 1);
173 * Convert a raw byte string into a null-terminated base64 ASCII string.
176 t_tob64(dst, src, size)
177 register char *dst, *src;
178 register unsigned size;
180 int c, pos = size % 3;
181 unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
195 c = (b0 & 0xfc) >> 2;
196 if(notleading || c != 0) {
197 *dst++ = b64table[c];
200 c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
201 if(notleading || c != 0) {
202 *dst++ = b64table[c];
205 c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
206 if(notleading || c != 0) {
207 *dst++ = b64table[c];
211 if(notleading || c != 0) {
212 *dst++ = b64table[c];