1 USING: cryptlib.libcl cryptlib prettyprint kernel alien sequences libc math
2 tools.test io io.files continuations alien.c-types splitting generic.math
5 "=========================================================" print
6 "Envelope/de-envelop test..." print
7 "=========================================================" print
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
17 envelope-handle flush-data
18 envelope-handle 1024 pop-data
25 envelope-handle get-pop-buffer get-bytes-copied push-data
27 envelope-handle flush-data
28 envelope-handle get-bytes-copied pop-data
30 [ "Hello world" ] [ pop-buffer-string ] unit-test
35 "=========================================================" print
36 "Password encryption test..." print
37 "=========================================================" print
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
48 envelope-handle flush-data
49 envelope-handle 1024 pop-data
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
65 envelope-handle flush-data
66 envelope-handle get-bytes-copied pop-data
68 [ "Hello world" ] [ pop-buffer-string ] unit-test
72 "=========================================================" print
73 "Compression test..." print
74 "=========================================================" print
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
85 envelope-handle flush-data
86 envelope-handle 1024 pop-data
93 envelope-handle get-pop-buffer get-bytes-copied push-data
95 envelope-handle flush-data
96 envelope-handle get-bytes-copied pop-data
98 [ "Hello world" ] [ pop-buffer-string ] unit-test
102 "=========================================================" print
103 "Conventional encryption test..." print
104 "=========================================================" print
108 CRYPT_FORMAT_CRYPTLIB [
110 context-handle CRYPT_CTXINFO_KEY "0123456789ABCDEF" set-attribute-string
111 envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int set-attribute
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
119 envelope-handle flush-data
120 envelope-handle 1024 pop-data
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"
132 envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int
141 envelope-handle flush-data
142 envelope-handle get-bytes-copied pop-data
144 [ "Hello world" ] [ pop-buffer-string ] unit-test
148 "=========================================================" print
149 "Large data size envelope/de-envelop test..." print
150 "=========================================================" print
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
163 envelope-handle flush-data
164 envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
166 ! pop-buffer-string .
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
175 envelope-handle flush-data
176 envelope-handle get-bytes-copied pop-data
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
186 "=========================================================" print
187 "Large data size password encryption test..." print
188 "=========================================================" print
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
203 envelope-handle flush-data
204 envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
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
222 envelope-handle flush-data
223 envelope-handle get-bytes-copied pop-data
225 ! pop-buffer-string .
227 [ "/opt/local/lib/libcl.dylib(dylib1.o):" ]
228 [ pop-buffer-string "\n" split first ] unit-test
230 [ "00000000 t __mh_dylib_header" ]
231 [ pop-buffer-string "\n" split last/first first ] unit-test
235 "=========================================================" print
236 "Generating a key pair test..." print
237 "=========================================================" print
241 context-handle CRYPT_CTXINFO_LABEL "private key" set-attribute-string
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
246 context-handle generate-key
248 CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
249 CRYPT_KEYOPT_CREATE [
250 "password" add-private-key
257 "=========================================================" print
258 "Simple certificate creation test..." print
259 "=========================================================" print
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"
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
287 "=========================================================" print
288 "SSH session test..." print
289 "=========================================================" print
291 ! start client connection with:
292 ! ssh -v localhost -p3000
293 "waiting for: ssh -v localhost -p3000" print flush
295 ! Are you sure you want to continue connecting (yes/no)? yes
297 ! <at> localhost's password: (any password will be accepted)
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.
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
308 CRYPT_SESSION_SSH_SERVER [
310 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
313 session-handle CRYPT_SESSINFO_SERVER_PORT 3000 set-attribute
315 session-handle CRYPT_SESSINFO_PRIVATEKEY
317 context-handle *int set-attribute
319 [ session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute ] [
320 dup CRYPT_ENVELOPE_RESOURCE = [
321 session-handle CRYPT_SESSINFO_AUTHRESPONSE 1
324 session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute
326 "Welcome to cryptlib, now go away.\r\n" set-pop-buffer
328 session-handle get-pop-buffer dup alien>char-string
331 session-handle flush-data
344 "=========================================================" print
345 "SSL session test..." print
346 "=========================================================" print
348 ! start client connection with:
349 ! curl -k https://localhost:3000
350 "waiting for: curl -k https://localhost:3000" print flush
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
357 CRYPT_SESSION_SSL_SERVER [
358 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
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
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
371 session-handle flush-data