1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "crypto_buffer.h"
/* after this s->e_ptr and s->e_end are updated */
static int firstread(crypto_buffer *s)
{
register u8 *ptr1, *ptr2, *buffer;
register u32 size;
u32 r, i;
/* look for the 0x5a block */
buffer = s->e_buffer;
size = s->block_size;
for(;;){
ptr1 = ptr2 = buffer;
r = s->op(s->fd, buffer, s->e_len);
if (r == 0) return 0; /* end of file */
if (r == -1){
if (errno == EINTR) continue; /* reread */
return r; /* read error */
}
if (r <= (size << 1)) return -1; /* no data */
i = r;
do{
i -= (ptr1-ptr2);
if (i==0) { ptr2 = NULL; break; } /* force reread */
ptr2 = memchr(ptr1, 0x5a, i);
if (ptr2 == NULL) break; /* reread */
ptr1 = ptr2;
for(;;) { if(*ptr1++!=0x5a) break; }
}while(ptr1-ptr2 < size);
if (ptr2 != NULL) break;
}
/* now ptr2 points to the 0x5a block */
ptr2 += size;
/* decrypt info */
s->cipher(&s->ctx,buffer, ptr2);
s->data_st.bytes = ((struct info *)buffer)->bytes;
s->data_st.strlen = ((struct info *)buffer)->strlen;
s->e_ptr = ptr2 + size;
r -= (size << 1);
i = s->e_ptr - (u8*)s->e_data - size;
if(i>0){
memmove(s->e_ptr-i, s->e_ptr, r-i);
s->e_ptr -= i;
if(s->op(s->fd, s->e_ptr+r-i, i)<=0) return r; /* really bad */
}
s->e_end = s->e_ptr + r;
return r;
}
/* after this s->e_ptr and s->e_end are updated */
static int oneread(crypto_buffer *s)
{
int r;
for (;;) {
r = s->op(s->fd,s->e_buffer,s->e_len);
if (r == -1) if (errno == EINTR) continue;
s->e_ptr = s->e_buffer;
s->e_end = s->e_ptr + r;
return r;
}
}
/* after this s->e_ptr is updated */
static int eatinfo(crypto_buffer *s, u8* start)
{
register u32 size;
register u8* buffer;
register u8* current;
int ret;
/* be really carefull */
current = start;
size = s->block_size;
buffer = s->e_buffer;
if(current == s->e_end){
ret = oneread(s);
if(ret<=0) return ret; /* error */
current = buffer;
}
/* TODO ?? verify that it's a magic block */
current += size; /* jump over 0x5a block */
if(current == s->e_end){
ret = oneread(s);
if(ret<=0) return ret; /* error */
current = buffer;
}
s->cipher(&s->ctx,buffer,current);
s->data_st.bytes = ((struct info *)buffer)->bytes;
s->data_st.strlen = ((struct info *)buffer)->strlen;
current += size; /* jump over info */
if(current == s->e_end){
ret = oneread(s);
if(ret<=0) return ret; /* error */
current = buffer;
}
s->e_ptr = current;
return 1;
}
/* afer this s->c_ptr is updated */
static int getthis(crypto_buffer *s, u8 *buf, u32 len)
{
int available;
available = s->c_end - s->c_ptr;
if (len > available) len = available;
memcpy(buf, s->c_ptr, len);
s->c_ptr += len;
return len;
}
int crypto_buffer_get(crypto_buffer *s, u8 *buf, u32 len)
{
register u32 need;
register u32 size;
register u8* e_end;
register u8* e_ptr;
int r, ret;
r = 0;
need = len;
size = s->block_size;
if(s->e_ptr == s->e_buffer){
ret = firstread(s);
if(ret <=0) return ret;
}
if (s->c_ptr != s->c_buffer){ /* TODO maybe else if */
r = getthis(s,buf,need);
if (r == need )return r;
need -= r;
buf += r;
}
/* now s->c_buffer is empty */
e_end = s->e_end;
e_ptr = s->e_ptr;
while(need){
if(e_ptr == e_end){
/* buffer empty */
ret = oneread(s);
if(ret < 0) return ret;
if(ret == 0) return r;
e_ptr = s->e_ptr;
e_end = s->e_end;
}
if(s->data_st.bytes == 0) {
/* end of raw encrypted data */
ret = eatinfo(s,e_ptr);
if(ret < 0) return ret;
if(ret == 0) return r;
e_ptr = s->e_ptr;
}
s->cipher(&s->ctx, s->c_buffer, e_ptr);
s->data_st.bytes -= size;
e_ptr += size;
/* now c_buffer is full */
if(s->data_st.strlen < size){
/* non fully used block */
s->c_ptr = s->c_end - s->data_st.strlen;
memmove(s->c_ptr, s->c_buffer, s->data_st.strlen);
} else {
s->data_st.strlen -= size;
s->c_ptr = s->c_buffer;
}
ret = getthis(s,buf,need);
need -= ret;
buf += ret;
r+= ret;
}
s->e_ptr = e_ptr;
return r;
}
|