Annotation of OpenXM/src/ox_ntl/crypt/radix/radix64.c, Revision 1.1
1.1 ! iwane 1: /* $OpenXM$ */
! 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: {
! 52: int i, j, n = 0, m;
! 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: m = 3 - datalen % 3;
! 78: for (j = 0; j < datalen % 3 + 1; j++)
! 79: buf[n++] = RADIX_STR[s[j]];
! 80: if (n % 76 == 0)
! 81: buf[n++] = '\n';
! 82: for (j = 0; j < 3 - datalen % 3; j++)
! 83: buf[n++] = '=';
! 84:
! 85: _END:
! 86: buf[n] = '\0';
! 87: return (n);
! 88: }
! 89:
! 90: int
! 91: radix64_dec(const unsigned char *data, int datalen, unsigned char *buf, int buflen)
! 92: {
! 93: int i, j, n = 0;
! 94: unsigned char m[4];
! 95: unsigned char alf[128] = {'\0'};
! 96:
! 97: for (i = 0; i < 26; i++) {
! 98: alf['A' + i] = i + 1;
! 99: alf['a' + i] = i + 26 + 1;
! 100: }
! 101: for (i = 0; i < 10; i++)
! 102: alf['0' + i] = 52 + i + 1;
! 103: alf['+'] = 62 + 1;
! 104: alf['/'] = 63 + 1;
! 105:
! 106: j = 0;
! 107: for (i = 0; i < datalen; i++) {
! 108: if (data[i] > sizeof(alf))
! 109: continue;
! 110:
! 111: if (data[i] == '=')
! 112: break;
! 113:
! 114: if (alf[data[i]] == 0)
! 115: continue;
! 116:
! 117: m[j++] = alf[data[i]] - 1;
! 118:
! 119: if (j == 4) {
! 120: buf[n++] = (0x3f & m[0]) << 2 | ((m[1] >> 4) & 0x3);
! 121: buf[n++] = ((m[1] << 4) & 0xf0) | ((m[2] & 0xfc) >> 2);
! 122: buf[n++] = ((m[2] & 0x3) << 6) | (m[3] & 0x3f);
! 123:
! 124: j = 0;
! 125: }
! 126: }
! 127:
! 128: if (j != 0) {
! 129: for (i = j; i < 4; i++)
! 130: m[i] = '\0';
! 131:
! 132: buf[n++] = (0x3f & m[0]) << 2 | ((m[1] >> 4) & 0x3);
! 133: buf[n++] = ((m[1] << 4) & 0xf0) | ((m[2] & 0xfc) >> 2);
! 134: buf[n++] = ((m[2] & 0x3) << 6) | (m[3] & 0x3f);
! 135:
! 136: n = n - 4 + j;
! 137: }
! 138:
! 139: return (n);
! 140: }
! 141:
! 142:
! 143:
! 144: #ifdef RADIX64_DEBUG
! 145:
! 146: int
! 147: main()
! 148: {
! 149: int n;
! 150: int m = 0, i, len;
! 151: int x = 1000;
! 152: char str[1024], buf[1024];
! 153:
! 154: char *a = " yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS\n vBSFjNSiVHsuAA==\n";
! 155:
! 156: m=0;
! 157: str[m++] = '\x14';
! 158: str[m++] = '\xfb';
! 159: str[m++] = '\x9c';
! 160: str[m++] = '\x03';
! 161: str[m++] = '\xd9';
! 162: str[m++] = '\x7e';
! 163: radix64_enc(str, m, buf, x);
! 164: printf("%s\n", buf);
! 165:
! 166: radix64_enc(str, m - 1, buf, x);
! 167: printf("%s\n", buf);
! 168:
! 169: radix64_enc(str, m - 2, buf, x);
! 170: printf("%s\n", buf);
! 171:
! 172: len = radix64_dec(a, strlen(a), buf, x);
! 173:
! 174: printf("dec=%d\n0x", m);
! 175: for (i = 0; i < m; i++)
! 176: printf("%02x", buf[i] & 0xff);
! 177: printf("\n\n");
! 178:
! 179: m = radix64_enc(buf, m, str, x);
! 180:
! 181: for (i = 0; i < 80; i++)
! 182: printf("%d", i % 10);
! 183: printf("\n");
! 184:
! 185: printf(" %s\n", str);
! 186: printf("%s\n", a);
! 187:
! 188: m = crc_octets(buf, len);
! 189:
! 190: str[0] = (m >> 16) & 0xff;
! 191: str[1] = (m >> 8) & 0xff;
! 192: str[2] = m & 0xff;
! 193:
! 194: radix64_enc(str, 3, buf, x);
! 195:
! 196: printf(" %s : %06x\n", buf, m);
! 197:
! 198: return (0);
! 199: }
! 200:
! 201: #endif /* RADIX64_DEBUG */
! 202:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>