59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
1 |
#include <stdio.h> |
2 |
#include <string.h> |
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
3 |
#include <errno.h> |
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
4 |
|
5 |
#define u8 unsigned char
|
|
6 |
||
7 |
/**
|
|
8 |
* Temporary holding place for functions relating to filename
|
|
9 |
* encryption
|
|
10 |
*/
|
|
11 |
||
12 |
/* 64 characters forming a 6-bit target field */
|
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
13 |
static unsigned char *portable_filename_chars = ("-.0123456789ABCD" |
14 |
"EFGHIJKLMNOPQRST"
|
|
15 |
"UVWXYZabcdefghij"
|
|
16 |
"klmnopqrstuvwxyz"); |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
17 |
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
18 |
static unsigned char filename_rev_map[] = { |
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
19 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */ |
20 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */ |
|
21 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */ |
|
22 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */ |
|
23 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */ |
|
24 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */ |
|
25 |
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */ |
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
26 |
0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */ |
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
27 |
0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */ |
28 |
0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */ |
|
29 |
0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */ |
|
30 |
0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */ |
|
31 |
0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */ |
|
32 |
0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */ |
|
33 |
0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */ |
|
34 |
0x3D, 0x3E, 0x3F |
|
35 |
};
|
|
36 |
||
37 |
/**
|
|
38 |
* ecryptfs_encode_for_filename
|
|
39 |
* @src_size Size of the source in bytes
|
|
40 |
*
|
|
41 |
*/
|
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
42 |
int ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size, |
43 |
unsigned char *src, size_t src_size) |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
44 |
{
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
45 |
size_t num_blocks; |
46 |
size_t block_num = 0; |
|
47 |
unsigned char last_block[3]; |
|
48 |
size_t dst_offset = 0; |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
49 |
int rc = 0; |
50 |
||
195
by Mike Halcrow
Update userspace filename crypto test code. |
51 |
if (src_size == 0) { |
52 |
rc = -EINVAL; |
|
53 |
goto out; |
|
54 |
}
|
|
55 |
num_blocks = (src_size / 3); |
|
56 |
if ((src_size % 3) == 0) { |
|
57 |
memcpy(last_block, (&src[src_size - 3]), 3); |
|
58 |
} else { |
|
59 |
num_blocks++; |
|
60 |
last_block[2] = 0x00; |
|
61 |
switch(src_size % 3) { |
|
62 |
case 1: |
|
63 |
last_block[0] = src[src_size - 1]; |
|
64 |
last_block[1] = 0x00; |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
65 |
break; |
66 |
case 2: |
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
67 |
last_block[0] = src[src_size - 2]; |
68 |
last_block[1] = src[src_size - 1]; |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
69 |
}
|
195
by Mike Halcrow
Update userspace filename crypto test code. |
70 |
}
|
71 |
(*dst_size) = (num_blocks * 4); |
|
72 |
if (!dst) |
|
73 |
goto out; |
|
74 |
while (block_num < num_blocks) { |
|
75 |
unsigned char *src_block; |
|
76 |
unsigned char dst_block[4]; |
|
77 |
||
78 |
if (block_num == (num_blocks - 1)) |
|
79 |
src_block = last_block; |
|
80 |
else
|
|
81 |
src_block = &src[block_num * 3]; |
|
82 |
dst_block[0] = ((src_block[0] >> 2) & 0x3f); |
|
83 |
dst_block[1] = (((src_block[0] << 4) & 0x30) |
|
84 |
| ((src_block[1] >> 4) & 0x0f)); |
|
85 |
dst_block[2] = (((src_block[1] << 2) & 0x3c) |
|
86 |
| ((src_block[2] >> 6) & 0x03)); |
|
87 |
dst_block[3] = (src_block[2] & 0x3f); |
|
88 |
dst[dst_offset++] = portable_filename_chars[dst_block[0]]; |
|
89 |
dst[dst_offset++] = portable_filename_chars[dst_block[1]]; |
|
90 |
dst[dst_offset++] = portable_filename_chars[dst_block[2]]; |
|
91 |
dst[dst_offset++] = portable_filename_chars[dst_block[3]]; |
|
92 |
block_num++; |
|
93 |
}
|
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
94 |
out: |
95 |
return rc; |
|
96 |
}
|
|
97 |
||
195
by Mike Halcrow
Update userspace filename crypto test code. |
98 |
int ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size, |
99 |
unsigned char *src, size_t src_size) |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
100 |
{
|
101 |
size_t src_byte_offset = 0; |
|
102 |
size_t dst_byte_offset = 0; |
|
103 |
u8 current_bit_offset = 0; |
|
104 |
int rc = 0; |
|
105 |
||
106 |
while (src_byte_offset < src_size) { |
|
107 |
src[src_byte_offset] = filename_rev_map[src[src_byte_offset]]; |
|
108 |
switch (current_bit_offset) { |
|
109 |
case 0: |
|
110 |
dst[dst_byte_offset] = (src[src_byte_offset] << 2); |
|
111 |
current_bit_offset = 6; |
|
112 |
break; |
|
113 |
case 6: |
|
114 |
dst[dst_byte_offset++] |= (src[src_byte_offset] >> 4); |
|
115 |
dst[dst_byte_offset] = ((src[src_byte_offset] & 0xF) |
|
116 |
<< 4); |
|
117 |
current_bit_offset = 4; |
|
118 |
break; |
|
119 |
case 4: |
|
120 |
dst[dst_byte_offset++] |= (src[src_byte_offset] >> 2); |
|
121 |
dst[dst_byte_offset] = (src[src_byte_offset] << 6); |
|
122 |
current_bit_offset = 2; |
|
123 |
break; |
|
124 |
case 2: |
|
125 |
dst[dst_byte_offset++] |= (src[src_byte_offset]); |
|
126 |
dst[dst_byte_offset] = 0; |
|
127 |
current_bit_offset = 0; |
|
128 |
break; |
|
129 |
}
|
|
130 |
src_byte_offset++; |
|
131 |
}
|
|
132 |
(*dst_size) = dst_byte_offset; |
|
133 |
out: |
|
134 |
return rc; |
|
135 |
}
|
|
136 |
||
137 |
||
138 |
||
195
by Mike Halcrow
Update userspace filename crypto test code. |
139 |
/* char *str = "This is a test of the eCryptfs packet encoding
|
140 |
* code. If this were an actual chunk of kernel code, your video card
|
|
141 |
* would spout 6-inch flames and your neighbor's inodes would flash
|
|
142 |
* your television's BIOS."; */
|
|
143 |
||
144 |
unsigned char str[43] = { 0x46, 0x29, 0x7f, 0xa0, 0x6f, 0x4b, 0x66, 0xfc, |
|
145 |
0xde, 0x02, 0x07, 0xf8, 0x31, 0x61, 0xb2, 0xee, |
|
146 |
0x65, 0x9e, 0x68, 0xdf, 0x81, 0x0b, 0x54, 0x70, |
|
147 |
0x0c, 0x2c, 0xe6, 0xf4, 0x79, 0xb7, 0xd3, 0xbf, |
|
148 |
0xa5, 0x96, 0x11, 0x11, 0xf5, 0xc1, 0x5b, 0x87, |
|
149 |
0x16, 0x4e, 0xed }; |
|
150 |
||
151 |
/*unsigned char encoded[61] = {
|
|
152 |
0x46, 0x29, 0x7f, 0xa0, 0x6f, 0x4b, 0x66, 0xfc, 0xde, 0x02, 0x07, 0xaf, 0x33, 0xdc, 0xf4, 0xfd,
|
|
153 |
0xa1, 0x40, 0xf2, 0x77, 0x86, 0xfa, 0x86, 0xbc, 0x48, 0xed, 0x26, 0x75, 0xc2, 0x9a, 0x7d, 0xf7,
|
|
154 |
0x42, 0x27, 0xfe, 0x55, 0xa7, 0x4e, 0x3b, 0x4a, 0xb3, 0x2a, 0xc7, 0x00, 0x00
|
|
155 |
||
156 |
}; */
|
|
157 |
||
158 |
unsigned char *encoded = ""; |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
159 |
|
160 |
int main() |
|
161 |
{
|
|
162 |
char enc[1024]; |
|
163 |
char dec[1024]; |
|
164 |
size_t enc_size; |
|
165 |
size_t dec_size; |
|
166 |
int i; |
|
167 |
int rc = 0; |
|
168 |
||
195
by Mike Halcrow
Update userspace filename crypto test code. |
169 |
ecryptfs_encode_for_filename(NULL, &enc_size, str, 43); |
170 |
printf("enc_size = [%d]\n", enc_size); |
|
171 |
ecryptfs_encode_for_filename(enc, &enc_size, str, 43); |
|
172 |
enc[enc_size] = '\0'; |
|
173 |
printf("Encoded: [%s]\n", enc); |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
174 |
ecryptfs_decode_from_filename(dec, &dec_size, enc, enc_size); |
195
by Mike Halcrow
Update userspace filename crypto test code. |
175 |
printf("Decoded:\n"); |
176 |
for (i = 0; i < dec_size; i++) { |
|
177 |
if ((i % 16) == 0) |
|
178 |
printf("\n"); |
|
179 |
printf("0x%.2x.", (unsigned char)dec[i]); |
|
180 |
}
|
|
181 |
printf("\n"); |
|
59
by mike@halcrow.us
Some functions to facilitate encoding and decoding arbitrary binary |
182 |
out: |
183 |
return rc; |
|
184 |
}
|