]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/cryptlib/cryptlib-tests.factor
tools.test: Make the flag public. Finish porting tester changes to fuzzer.
[factor.git] / unmaintained / cryptlib / cryptlib-tests.factor
1 USING: cryptlib.libcl cryptlib prettyprint kernel alien sequences libc math
2 tools.test io io.files continuations alien.c-types splitting generic.math
3 io.encodings.binary ;
4
5 "=========================================================" print
6 "Envelope/de-envelop test..." print
7 "=========================================================" print
8
9 [
10     ! envelope
11     CRYPT_FORMAT_CRYPTLIB [
12         "Hello world" set-pop-buffer
13         envelope-handle CRYPT_ENVINFO_DATASIZE
14         get-pop-buffer alien>char-string length set-attribute
15         envelope-handle get-pop-buffer dup alien>char-string length push-data
16         get-bytes-copied .
17         envelope-handle flush-data
18         envelope-handle 1024 pop-data
19         get-bytes-copied .
20         pop-buffer-string .
21     ] with-envelope
22
23     ! de-envelope
24     CRYPT_FORMAT_AUTO [
25         envelope-handle get-pop-buffer get-bytes-copied push-data
26         get-bytes-copied .
27         envelope-handle flush-data
28         envelope-handle get-bytes-copied pop-data
29         get-bytes-copied .
30         [ "Hello world" ] [ pop-buffer-string ] unit-test
31     ] with-envelope
32
33 ] with-cryptlib
34
35 "=========================================================" print
36 "Password encryption test..." print
37 "=========================================================" print
38
39 [
40     ! envelope
41     CRYPT_FORMAT_CRYPTLIB [
42         envelope-handle CRYPT_ENVINFO_PASSWORD "password" set-attribute-string
43         "Hello world" set-pop-buffer
44         envelope-handle CRYPT_ENVINFO_DATASIZE
45         get-pop-buffer alien>char-string length set-attribute
46         envelope-handle get-pop-buffer dup alien>char-string length push-data
47         get-bytes-copied .
48         envelope-handle flush-data
49         envelope-handle 1024 pop-data
50         get-bytes-copied .
51         pop-buffer-string .
52     ] with-envelope
53
54         ! de-envelope
55     CRYPT_FORMAT_AUTO [
56         [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
57             dup CRYPT_ENVELOPE_RESOURCE = [
58                 envelope-handle CRYPT_ENVINFO_PASSWORD
59                 "password" set-attribute-string
60             ] [
61                 rethrow
62             ] if
63         ] recover drop
64         get-bytes-copied .
65         envelope-handle flush-data
66         envelope-handle get-bytes-copied pop-data
67         get-bytes-copied .
68         [ "Hello world" ] [ pop-buffer-string ] unit-test
69     ] with-envelope
70 ] with-cryptlib
71
72 "=========================================================" print
73 "Compression test..." print
74 "=========================================================" print
75
76 [
77     ! envelope
78     CRYPT_FORMAT_CRYPTLIB [
79         envelope-handle CRYPT_ENVINFO_COMPRESSION CRYPT_UNUSED set-attribute
80         "Hello world" set-pop-buffer
81         envelope-handle CRYPT_ENVINFO_DATASIZE
82         get-pop-buffer alien>char-string length set-attribute
83         envelope-handle get-pop-buffer dup alien>char-string length push-data
84         get-bytes-copied .
85         envelope-handle flush-data
86         envelope-handle 1024 pop-data
87         get-bytes-copied .
88         pop-buffer-string .
89     ] with-envelope
90
91     ! de-envelope
92     CRYPT_FORMAT_AUTO [
93         envelope-handle get-pop-buffer get-bytes-copied push-data
94         get-bytes-copied .
95         envelope-handle flush-data
96         envelope-handle get-bytes-copied pop-data
97         get-bytes-copied .
98         [ "Hello world" ] [ pop-buffer-string ] unit-test
99     ] with-envelope
100 ] with-cryptlib
101
102 "=========================================================" print
103 "Conventional encryption test..." print
104 "=========================================================" print
105
106 [
107     ! envelope
108     CRYPT_FORMAT_CRYPTLIB [
109         CRYPT_ALGO_IDEA [
110             context-handle CRYPT_CTXINFO_KEY "0123456789ABCDEF" set-attribute-string
111             envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int set-attribute
112         ] with-context
113
114         "Hello world" set-pop-buffer
115         envelope-handle CRYPT_ENVINFO_DATASIZE
116         get-pop-buffer alien>char-string length set-attribute
117         envelope-handle get-pop-buffer dup alien>char-string length push-data
118         get-bytes-copied .
119         envelope-handle flush-data
120         envelope-handle 1024 pop-data
121         get-bytes-copied .
122         pop-buffer-string .
123     ] with-envelope
124
125     ! de-envelope
126     CRYPT_FORMAT_AUTO [
127         [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
128             dup CRYPT_ENVELOPE_RESOURCE = [
129                 CRYPT_ALGO_IDEA create-context
130                 context-handle CRYPT_CTXINFO_KEY "0123456789ABCDEF"
131                 set-attribute-string
132                 envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int
133                 set-attribute
134             ] [
135                 rethrow
136             ] if
137         ] recover drop
138
139         get-bytes-copied .
140         destroy-context
141         envelope-handle flush-data
142         envelope-handle get-bytes-copied pop-data
143         get-bytes-copied .
144         [ "Hello world" ] [ pop-buffer-string ] unit-test
145     ] with-envelope
146 ] with-cryptlib
147
148 "=========================================================" print
149 "Large data size envelope/de-envelop test..." print
150 "=========================================================" print
151
152 [
153     ! envelope
154     CRYPT_FORMAT_CRYPTLIB [
155         "extra/cryptlib/test/large_data.txt" resource-path
156         binary file-contents set-pop-buffer
157         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
158         get-pop-buffer alien>char-string length 10000 + set-attribute
159         envelope-handle CRYPT_ENVINFO_DATASIZE
160         get-pop-buffer alien>char-string length set-attribute
161         envelope-handle get-pop-buffer dup alien>char-string length push-data
162         get-bytes-copied .
163         envelope-handle flush-data
164         envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
165         get-bytes-copied .
166         ! pop-buffer-string .
167     ] with-envelope
168
169     ! de-envelope
170     CRYPT_FORMAT_AUTO [
171         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
172         get-pop-buffer alien>char-string length 10000 + set-attribute
173         envelope-handle get-pop-buffer get-bytes-copied push-data
174         get-bytes-copied .
175         envelope-handle flush-data
176         envelope-handle get-bytes-copied pop-data
177         get-bytes-copied .
178         ! pop-buffer-string .
179         [ "/opt/local/lib/libcl.dylib(dylib1.o):" ]
180         [ pop-buffer-string "\n" split first ] unit-test
181         [ "00000000 t __mh_dylib_header" ]
182         [ pop-buffer-string "\n" split last/first first ] unit-test
183     ] with-envelope
184 ] with-cryptlib
185
186 "=========================================================" print
187 "Large data size password encryption test..." print
188 "=========================================================" print
189
190 [
191
192     ! envelope
193     CRYPT_FORMAT_CRYPTLIB [
194         envelope-handle CRYPT_ENVINFO_PASSWORD "password" set-attribute-string
195         "extra/cryptlib/test/large_data.txt" resource-path
196         binary file-contents set-pop-buffer
197         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
198         get-pop-buffer alien>char-string length 10000 + set-attribute
199         envelope-handle CRYPT_ENVINFO_DATASIZE
200         get-pop-buffer alien>char-string length set-attribute
201         envelope-handle get-pop-buffer dup alien>char-string length push-data
202         get-bytes-copied .
203         envelope-handle flush-data
204         envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
205         get-bytes-copied .
206         pop-buffer-string .
207     ] with-envelope
208
209     ! de-envelope
210     CRYPT_FORMAT_AUTO [
211         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE 130000 set-attribute
212         [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
213             dup CRYPT_ENVELOPE_RESOURCE = [
214                 envelope-handle CRYPT_ENVINFO_PASSWORD
215                 "password" set-attribute-string
216             ] [
217                 rethrow
218             ] if
219         ] recover drop
220
221         get-bytes-copied .
222         envelope-handle flush-data
223         envelope-handle get-bytes-copied pop-data
224         get-bytes-copied .
225         ! pop-buffer-string .
226
227         [ "/opt/local/lib/libcl.dylib(dylib1.o):" ]
228         [ pop-buffer-string "\n" split first ] unit-test
229
230         [ "00000000 t __mh_dylib_header" ]
231         [ pop-buffer-string "\n" split last/first first ] unit-test
232     ] with-envelope
233 ] with-cryptlib
234
235 "=========================================================" print
236 "Generating a key pair test..." print
237 "=========================================================" print
238
239 [
240     CRYPT_ALGO_RSA [
241         context-handle CRYPT_CTXINFO_LABEL "private key" set-attribute-string
242
243         ! a particular key length can be set (e.g. 1536-bit/192-byte key)
244         context-handle CRYPT_CTXINFO_KEYSIZE 1536 8 / set-attribute
245
246         context-handle generate-key
247
248         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
249         CRYPT_KEYOPT_CREATE [
250             "password" add-private-key
251         ] with-keyset
252     ] with-context
253 ] with-cryptlib
254
255 "Passed" print
256
257 "=========================================================" print
258 "Simple certificate creation test..." print
259 "=========================================================" print
260
261 [
262     CRYPT_ALGO_RSA [
263         context-handle CRYPT_CTXINFO_LABEL "private key" set-attribute-string
264         context-handle generate-key
265         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
266         CRYPT_KEYOPT_CREATE [
267             "password" add-private-key
268             CRYPT_CERTTYPE_CERTIFICATE [
269                 certificate-handle CRYPT_CERTINFO_XYZZY 1 set-attribute
270                 certificate-handle CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO
271                 context-handle *int set-attribute
272                 certificate-handle CRYPT_CERTINFO_COMMONNAME "Dave Smith"
273                 set-attribute-string
274                 sign-certificate
275                 check-certificate
276                 add-public-key
277                 f 0 CRYPT_CERTFORMAT_TEXT_CERTIFICATE export-certificate
278                 get-cert-length *int dup malloc swap
279                 CRYPT_CERTFORMAT_TEXT_CERTIFICATE export-certificate
280                 get-cert-buffer alien>char-string print
281             ] with-certificate
282         ] with-keyset
283     ] with-context
284 ] with-cryptlib
285
286 : ssh-session ( -- )
287     "=========================================================" print
288     "SSH session test..." print
289     "=========================================================" print
290
291     ! start client connection with:
292     ! ssh -v localhost -p3000
293     "waiting for: ssh -v localhost -p3000" print flush
294
295     ! Are you sure you want to continue connecting (yes/no)? yes
296     ! ...
297     ! <at> localhost's password: (any password will be accepted)
298
299     ! If you want to run the test again you should clean the [localhost]:3000
300     ! ssh-rsa entry in the known_hosts file, in your home directory under the .ssh
301     ! folder, since the test generates a new RSA certificate on every run.
302
303     [
304         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
305         CRYPT_KEYOPT_READONLY [
306             CRYPT_KEYID_NAME "private key" "password" get-private-key
307
308             CRYPT_SESSION_SSH_SERVER [
309
310                 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
311                 set-attribute-string
312
313                 session-handle CRYPT_SESSINFO_SERVER_PORT 3000 set-attribute
314
315                 session-handle CRYPT_SESSINFO_PRIVATEKEY
316
317                 context-handle *int set-attribute
318
319                 [ session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute ] [
320                     dup CRYPT_ENVELOPE_RESOURCE = [
321                         session-handle CRYPT_SESSINFO_AUTHRESPONSE 1
322                         set-attribute
323
324                         session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute
325
326                         "Welcome to cryptlib, now go away.\r\n" set-pop-buffer
327
328                         session-handle  get-pop-buffer dup alien>char-string
329                         length push-data
330
331                         session-handle flush-data
332                     ] [
333                         rethrow
334                     ] if
335                 ] recover drop
336             ] with-session
337         ] with-keyset
338     ] with-cryptlib
339
340     "Passed" print
341 ;
342
343 : ssl-session ( -- )
344     "=========================================================" print
345     "SSL session test..." print
346     "=========================================================" print
347
348     ! start client connection with:
349     ! curl -k https://localhost:3000
350     "waiting for: curl -k https://localhost:3000" print flush
351
352     [
353         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
354         CRYPT_KEYOPT_READONLY [
355             CRYPT_KEYID_NAME "private key" "password" get-private-key
356
357             CRYPT_SESSION_SSL_SERVER [
358                 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
359                 set-attribute-string
360                 session-handle CRYPT_SESSINFO_SERVER_PORT 3000 set-attribute
361                 session-handle CRYPT_OPTION_NET_WRITETIMEOUT 10 set-attribute
362                 session-handle CRYPT_OPTION_NET_READTIMEOUT 10 set-attribute
363                 session-handle CRYPT_OPTION_NET_CONNECTTIMEOUT 10 set-attribute
364                 session-handle CRYPT_SESSINFO_PRIVATEKEY
365                 context-handle *int set-attribute
366
367                 session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute
368                 "Welcome to cryptlib, now go away.\r\n" set-pop-buffer
369                 session-handle  get-pop-buffer dup alien>char-string
370                 length push-data
371                 session-handle flush-data
372             ] with-session
373         ] with-keyset
374     ] with-cryptlib
375
376     "Passed" print
377 ;