djbdnscurve6 45
djbdnscurve6
Loading...
Searching...
No Matches
base32.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <string.h>
3#include "base32.h"
4#include "byte.h"
5#include "uint_t.h"
6#include "errno.h"
7
8/* DNSCurve uses its own sophomoric base32 implementation:
9
10 1. base32_encode: used for DNS labels
11 2. base32_clientkey: used for client key generation only
12 3. base32_serverkey: used for server key generation only
13 3. base32_decode: additional 'mode' to decode all
14
15 Note: This implementation is neither RFC 3548 nor 4684 compatible!
16*/
17
18/*
19 Algorithm & Alfabet:
20
21 base32_char = '0123456789bcdfghjklmnpqrstuvwxyz'
22 ','.join('%2d' % base32_char.find(chr(x).lower()) for x in xrange(256))
23 x < 128; otherwise invalid
24*/
25
26static const char base32_char[32] = "0123456789bcdfghjklmnpqrstuvwxyz";
27
28static const uint8 base32_map[128] = {
29// 0 1 2 3 4 5 6 7 8 9 a b c d e f
30 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99, // 0
31 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99, // 1
32 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99, // 2
33 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,99,99,99,99,99,99, // 3
34 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99, // 4
35 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99, // 5
36 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99, // 6
37 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99 // 7
38};
39
40unsigned int base32_bytessize(unsigned int len)
41{
42 len = (8 * len + 4) / 5;
43 return len + (len + 49) / 50;
44}
45
46unsigned int base32_decode(uint8 *out,const char *in,unsigned int len,int mode)
47{
48 unsigned int i;
49 unsigned int j;
50 unsigned int v;
51 unsigned int bits;
52 uint8 x;
53
54 i = j = v = bits = 0;
55
56 for (i = 0; i < len; ++i) {
57 if (in[i] & 0x80) return 0;
58 x = base32_map[(uint8) in[i]];
59 if (x > 31) return 0;
60 v |= ((unsigned) x) << bits;
61 bits += 5;
62
63 if (bits >= 8) {
64 out[j++] = v;
65 v >>= 8;
66 bits -= 8;
67 }
68 }
69 if (mode && bits) {
70 out[j++] = v;
71 } else if (bits >= 5 || v)
72 return 0;
73
74 return j;
75}
76
77void base32_encode(uint8 *out,const char *in,unsigned int len)
78{
79 unsigned int i;
80 unsigned int x;
81 unsigned int v;
82 unsigned int bits;
83
84 x = v = bits = 0;
85
86 for (i = 0; i < len; ++i) {
87 v |= ((unsigned int) (uint8) in[i]) << bits;
88 bits += 8;
89
90 do {
91 out[++x] = base32_char[v & 31];
92 v >>= 5;
93 bits -= 5;
94 if (x == 50) { // new label
95 *out = x;
96 out += 1 + x;
97 x = 0;
98 }
99 } while (bits >= 5);
100 }
101
102 if (bits) out[++x] = base32_char[v & 31];
103 if (x) *out = x;
104}
105
106void base32_clientkey(uint8 *out,const char *key)
107{
108 unsigned int i;
109 unsigned int v;
110 unsigned int bits;
111
112 byte_copy(out,4,"\66x1a");
113 out += 4;
114 v = bits = 0;
115
116 for (i = 0; i < 32; ++i) {
117 v |= ((unsigned int) (uint8) key[i]) << bits;
118 bits += 8;
119 do {
120 *out++ = base32_char[v & 31];
121 v >>= 5;
122 bits -= 5;
123 } while (bits >= 5);
124 }
125}
126
127unsigned int base32_serverkey(uint8 *out,const char *in,unsigned int len)
128{
129 unsigned int i = 0;
130 unsigned int j = 0;
131 unsigned int v = 0;
132 unsigned int bits = 0;
133
134 while (j < len) {
135 v |= ((uint8) in[j++]) << bits;
136 bits += 8;
137
138 while (bits >= 5) {
139 out[i++] = base32_char[v & 31];
140 bits -= 5;
141 v >>= 5;
142 }
143 }
144
145 if (bits)
146 out[i++] = base32_char[v & 31];
147
148 return i;
149}
uint16 len
Definition: axfrdns.c:319
unsigned int base32_bytessize(unsigned int len)
Definition: base32.c:40
void base32_clientkey(uint8 *out, const char *key)
Definition: base32.c:106
unsigned int base32_decode(uint8 *out, const char *in, unsigned int len, int mode)
Definition: base32.c:46
void base32_encode(uint8 *out, const char *in, unsigned int len)
Definition: base32.c:77
unsigned int base32_serverkey(uint8 *out, const char *in, unsigned int len)
Definition: base32.c:127
struct line * x
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
char mode
Definition: tinydns-edit.c:45