Annotation of OpenXM/src/ox_ntl/crypt/radix/radix64.c, Revision 1.2
1.2 ! iwane 1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/radix/radix64.c,v 1.1 2004/01/12 14:52:18 iwane Exp $ */
1.1 iwane 2: /* RFC 2440 */
3:
4: #include "radix64.h"
5:
6: #define CRC24_INIT 0xb704ceL
7: #define CRC24_POLY 0x1864cfbL
8:
9: crc24
10: crc_octets(const unsigned char *octets, size_t len)
11: {
12: crc24 crc = CRC24_INIT;
13: int i;
14:
15: while (len--) {
16: crc ^= (*octets++) << 16;
17: for (i = 0; i < 8; i++) {
18: crc <<= 1;
19: if (crc & 0x1000000) {
20: crc ^= CRC24_POLY;
21: }
22: }
23: }
24:
25: return (crc & 0xffffffL);
26: }
27:
28:
29:
30:
31: static const char *RADIX_STR =
32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33: "abcdefghijklmnopqrstuvwxyz"
34: "0123456789+/";
35:
36:
37:
38: #define RADIX64_CONV(data, x) \
39: do { \
40: s[0] = (data[0] >> 2) & 0x3f; \
41: s[1] = ((data[0] & 0x3) << 4) | ((data[1] & 0xff) >> 4); \
42: s[2] = ((data[1] & 0xf) << 2) | ((data[2] & 0xff) >> 6); \
43: s[3] = data[2] & 0x3f; \
44: } while (0)
45:
46: #define radix64_conv(data, x) RADIX64_CONV((data), (x))
47:
48:
49: int
50: radix64_enc(const unsigned char *data, int datalen, unsigned char *buf, int buflen)
51: {
1.2 ! iwane 52: int i, j, n = 0;
1.1 iwane 53: int s[4];
54: char x[3] = {'\0'};
55:
56: /* parameter check */
57: for (i = 0; i + 2 < datalen; i += 3) {
58: radix64_conv(data + i, s);
59:
60: for (j = 0; j < 4; j++)
61: buf[n++] = RADIX_STR[s[j]];
62: if (n % 76 == 0)
63: buf[n++] = '\n';
64: }
65:
66: switch (datalen % 3) {
67: case 0:
68: goto _END;
69: case 2:
70: x[1] = data[i + 1];
71: case 1:
72: x[0] = data[i];
73: break;
74: }
75:
76: radix64_conv(x, s);
77: for (j = 0; j < datalen % 3 + 1; j++)
78: buf[n++] = RADIX_STR[s[j]];
79: if (n % 76 == 0)
80: buf[n++] = '\n';
81: for (j = 0; j < 3 - datalen % 3; j++)
82: buf[n++] = '=';
83:
84: _END:
85: buf[n] = '\0';
86: return (n);
87: }
88:
89: int
90: radix64_dec(const unsigned char *data, int datalen, unsigned char *buf, int buflen)
91: {
92: int i, j, n = 0;
93: unsigned char m[4];
94: unsigned char alf[128] = {'\0'};
95:
96: for (i = 0; i < 26; i++) {
97: alf['A' + i] = i + 1;
98: alf['a' + i] = i + 26 + 1;
99: }
100: for (i = 0; i < 10; i++)
101: alf['0' + i] = 52 + i + 1;
102: alf['+'] = 62 + 1;
103: alf['/'] = 63 + 1;
104:
105: j = 0;
106: for (i = 0; i < datalen; i++) {
107: if (data[i] > sizeof(alf))
108: continue;
109:
110: if (data[i] == '=')
111: break;
112:
113: if (alf[data[i]] == 0)
114: continue;
115:
116: m[j++] = alf[data[i]] - 1;
117:
118: if (j == 4) {
119: buf[n++] = (0x3f & m[0]) << 2 | ((m[1] >> 4) & 0x3);
120: buf[n++] = ((m[1] << 4) & 0xf0) | ((m[2] & 0xfc) >> 2);
121: buf[n++] = ((m[2] & 0x3) << 6) | (m[3] & 0x3f);
122:
123: j = 0;
124: }
125: }
126:
127: if (j != 0) {
128: for (i = j; i < 4; i++)
129: m[i] = '\0';
130:
131: buf[n++] = (0x3f & m[0]) << 2 | ((m[1] >> 4) & 0x3);
132: buf[n++] = ((m[1] << 4) & 0xf0) | ((m[2] & 0xfc) >> 2);
133: buf[n++] = ((m[2] & 0x3) << 6) | (m[3] & 0x3f);
134:
135: n = n - 4 + j;
136: }
137:
138: return (n);
139: }
140:
141:
142:
143: #ifdef RADIX64_DEBUG
144:
145: int
146: main()
147: {
148: int n;
149: int m = 0, i, len;
150: int x = 1000;
151: char str[1024], buf[1024];
152:
153: char *a = " yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS\n vBSFjNSiVHsuAA==\n";
154:
155: m=0;
156: str[m++] = '\x14';
157: str[m++] = '\xfb';
158: str[m++] = '\x9c';
159: str[m++] = '\x03';
160: str[m++] = '\xd9';
161: str[m++] = '\x7e';
162: radix64_enc(str, m, buf, x);
163: printf("%s\n", buf);
164:
165: radix64_enc(str, m - 1, buf, x);
166: printf("%s\n", buf);
167:
168: radix64_enc(str, m - 2, buf, x);
169: printf("%s\n", buf);
170:
171: len = radix64_dec(a, strlen(a), buf, x);
172:
173: printf("dec=%d\n0x", m);
174: for (i = 0; i < m; i++)
175: printf("%02x", buf[i] & 0xff);
176: printf("\n\n");
177:
178: m = radix64_enc(buf, m, str, x);
179:
180: for (i = 0; i < 80; i++)
181: printf("%d", i % 10);
182: printf("\n");
183:
184: printf(" %s\n", str);
185: printf("%s\n", a);
186:
187: m = crc_octets(buf, len);
188:
189: str[0] = (m >> 16) & 0xff;
190: str[1] = (m >> 8) & 0xff;
191: str[2] = m & 0xff;
192:
193: radix64_enc(str, 3, buf, x);
194:
195: printf(" %s : %06x\n", buf, m);
196:
197: return (0);
198: }
199:
200: #endif /* RADIX64_DEBUG */
201:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>