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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "crypto_buffer.h"
int main(void)
{
static int fd, ret;
static crypto_buffer out_buffer;
static crypto_buffer in_buffer;
static u8 phrase[64]="blah_et_vlan_et_rantanplan_=_rien_de_cohérent.";
static u8 decrypted[64]="";
/* blowfish */
printf("** crypto buffer tests with BLOWFISH **\n\n");
/* write */
fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd == -1) return -1;
ret = write(fd,"AAA",3);
assert(ret==3);
if(crypto_buffer_init(&out_buffer, fd, u8_unix_write, 4,
BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
crypto_buffer_putflush(&out_buffer,phrase,5);
close(fd);
printf("file written\n");
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, u8_unix_read, 5,
BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,5)==0);
memset(decrypted,0,64);
/* write */
fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd == -1) return -1;
ret = write(fd,"AAA",3);
assert(ret==3);
if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4,
BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
crypto_buffer_putflush(&out_buffer,phrase,8);
close(fd);
printf("file written\n");
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,8)==0);
memset(decrypted,0,64);
/* write */
fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd == -1) return -1;
ret = write(fd,"AAA",0);
assert(ret==0);
if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4,
BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
crypto_buffer_putflush(&out_buffer,phrase,64);
close(fd);
printf("file written\n");
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,8));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+8,15));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+23,24));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+47,5));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
memset(decrypted,0,64);
/* write */
fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd == -1) return -1;
ret = write(fd,"AAAAA",5);
assert(ret==5);
if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4,
BLOWFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
crypto_buffer_put(&out_buffer,phrase,12);
crypto_buffer_putflush(&out_buffer,phrase+12,14);
crypto_buffer_putflush(&out_buffer,phrase+26,23);
close(fd);
printf("file written\n");
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
BLOWFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,9));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+9,9));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+18,20));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+38,22));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
/* twofish */
printf("** crypto buffer tests with TWOFISH **\n\n");
/* write */
fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd == -1) return -1;
ret = write(fd,"AAA",3);
assert(ret==3);
if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4,
TWOFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
crypto_buffer_putflush(&out_buffer,phrase,64);
close(fd);
printf("file written\n");
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,9));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+9,9));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+18,20));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+38,22));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
memset(decrypted,0,64);
/* write */
fd = open("./crypto_file",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd == -1) return -1;
ret = write(fd,"AAAA",4);
assert(ret==4);
if(crypto_buffer_init(&out_buffer, fd, &u8_unix_write, 4,
TWOFISH|ENCRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
crypto_buffer_put(&out_buffer,phrase,12);
crypto_buffer_putflush(&out_buffer,phrase+12,14);
crypto_buffer_putflush(&out_buffer,phrase+26,21);
close(fd);
printf("file written\n");
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,64));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
memset(decrypted,0,64);
/* read */
fd = open("./crypto_file",O_RDONLY);
if(fd == -1) return -1;
if(crypto_buffer_init(&in_buffer, fd, &u8_unix_read, 5,
TWOFISH|DECRYPT, (u8*)"secret_key_hah!!", 16)!=0) return -1;
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted,16));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+16,7));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+23,28));
printf("read : %d\n",crypto_buffer_get(&in_buffer,decrypted+51,13));
printf("decrypted => '%s'\n\n",decrypted);
close(fd);
assert(memcmp(phrase,decrypted,64)==0);
memset(decrypted,0,64);
printf("tests ok\n");
return 0;
}
|