1 USING: cryptlib.libcl cryptlib prettyprint kernel alien sequences libc math
2 tools.test io io.files continuations alien.c-types splitting generic.math ;
4 "=========================================================" print
5 "Envelope/de-envelop test..." print
6 "=========================================================" print
10 CRYPT_FORMAT_CRYPTLIB [
11 "Hello world" set-pop-buffer
12 envelope-handle CRYPT_ENVINFO_DATASIZE
13 get-pop-buffer alien>char-string length set-attribute
14 envelope-handle get-pop-buffer dup alien>char-string length push-data
16 envelope-handle flush-data
17 envelope-handle 1024 pop-data
24 envelope-handle get-pop-buffer get-bytes-copied push-data
26 envelope-handle flush-data
27 envelope-handle get-bytes-copied pop-data
29 [ "Hello world" ] [ pop-buffer-string ] unit-test
34 "=========================================================" print
35 "Password encryption test..." print
36 "=========================================================" print
40 CRYPT_FORMAT_CRYPTLIB [
41 envelope-handle CRYPT_ENVINFO_PASSWORD "password" set-attribute-string
42 "Hello world" set-pop-buffer
43 envelope-handle CRYPT_ENVINFO_DATASIZE
44 get-pop-buffer alien>char-string length set-attribute
45 envelope-handle get-pop-buffer dup alien>char-string length push-data
47 envelope-handle flush-data
48 envelope-handle 1024 pop-data
55 [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
56 dup CRYPT_ENVELOPE_RESOURCE = [
57 envelope-handle CRYPT_ENVINFO_PASSWORD
58 "password" set-attribute-string
64 envelope-handle flush-data
65 envelope-handle get-bytes-copied pop-data
67 [ "Hello world" ] [ pop-buffer-string ] unit-test
71 "=========================================================" print
72 "Compression test..." print
73 "=========================================================" print
77 CRYPT_FORMAT_CRYPTLIB [
78 envelope-handle CRYPT_ENVINFO_COMPRESSION CRYPT_UNUSED set-attribute
79 "Hello world" set-pop-buffer
80 envelope-handle CRYPT_ENVINFO_DATASIZE
81 get-pop-buffer alien>char-string length set-attribute
82 envelope-handle get-pop-buffer dup alien>char-string length push-data
84 envelope-handle flush-data
85 envelope-handle 1024 pop-data
92 envelope-handle get-pop-buffer get-bytes-copied push-data
94 envelope-handle flush-data
95 envelope-handle get-bytes-copied pop-data
97 [ "Hello world" ] [ pop-buffer-string ] unit-test
101 "=========================================================" print
102 "Conventional encryption test..." print
103 "=========================================================" print
107 CRYPT_FORMAT_CRYPTLIB [
109 context-handle CRYPT_CTXINFO_KEY "0123456789ABCDEF" set-attribute-string
110 envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int set-attribute
113 "Hello world" set-pop-buffer
114 envelope-handle CRYPT_ENVINFO_DATASIZE
115 get-pop-buffer alien>char-string length set-attribute
116 envelope-handle get-pop-buffer dup alien>char-string length push-data
118 envelope-handle flush-data
119 envelope-handle 1024 pop-data
126 [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
127 dup CRYPT_ENVELOPE_RESOURCE = [
128 CRYPT_ALGO_IDEA create-context
129 context-handle CRYPT_CTXINFO_KEY "0123456789ABCDEF"
131 envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int
140 envelope-handle flush-data
141 envelope-handle get-bytes-copied pop-data
143 [ "Hello world" ] [ pop-buffer-string ] unit-test
147 "=========================================================" print
148 "Large data size envelope/de-envelop test..." print
149 "=========================================================" print
153 CRYPT_FORMAT_CRYPTLIB [
154 "extra/cryptlib/test/large_data.txt" resource-path
155 file-contents set-pop-buffer
156 envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
157 get-pop-buffer alien>char-string length 10000 + set-attribute
158 envelope-handle CRYPT_ENVINFO_DATASIZE
159 get-pop-buffer alien>char-string length set-attribute
160 envelope-handle get-pop-buffer dup alien>char-string length push-data
162 envelope-handle flush-data
163 envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
165 ! pop-buffer-string .
170 envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
171 get-pop-buffer alien>char-string length 10000 + set-attribute
172 envelope-handle get-pop-buffer get-bytes-copied push-data
174 envelope-handle flush-data
175 envelope-handle get-bytes-copied pop-data
177 ! pop-buffer-string .
178 [ "/opt/local/lib/libcl.dylib(dylib1.o):" ]
179 [ pop-buffer-string "\n" split first ] unit-test
180 [ "00000000 t __mh_dylib_header" ]
181 [ pop-buffer-string "\n" split last/first first ] unit-test
185 "=========================================================" print
186 "Large data size password encryption test..." print
187 "=========================================================" print
192 CRYPT_FORMAT_CRYPTLIB [
193 envelope-handle CRYPT_ENVINFO_PASSWORD "password" set-attribute-string
194 "extra/cryptlib/test/large_data.txt" resource-path
195 file-contents set-pop-buffer
196 envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
197 get-pop-buffer alien>char-string length 10000 + set-attribute
198 envelope-handle CRYPT_ENVINFO_DATASIZE
199 get-pop-buffer alien>char-string length set-attribute
200 envelope-handle get-pop-buffer dup alien>char-string length push-data
202 envelope-handle flush-data
203 envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
210 envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE 130000 set-attribute
211 [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
212 dup CRYPT_ENVELOPE_RESOURCE = [
213 envelope-handle CRYPT_ENVINFO_PASSWORD
214 "password" set-attribute-string
221 envelope-handle flush-data
222 envelope-handle get-bytes-copied pop-data
224 ! pop-buffer-string .
226 [ "/opt/local/lib/libcl.dylib(dylib1.o):" ]
227 [ pop-buffer-string "\n" split first ] unit-test
229 [ "00000000 t __mh_dylib_header" ]
230 [ pop-buffer-string "\n" split last/first first ] unit-test
234 "=========================================================" print
235 "Generating a key pair test..." print
236 "=========================================================" print
240 context-handle CRYPT_CTXINFO_LABEL "private key" set-attribute-string
242 ! a particular key length can be set (e.g. 1536-bit/192-byte key)
243 context-handle CRYPT_CTXINFO_KEYSIZE 1536 8 / set-attribute
245 context-handle generate-key
247 CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
248 CRYPT_KEYOPT_CREATE [
249 "password" add-private-key
256 "=========================================================" print
257 "Simple certificate creation test..." print
258 "=========================================================" print
262 context-handle CRYPT_CTXINFO_LABEL "private key" set-attribute-string
263 context-handle generate-key
264 CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
265 CRYPT_KEYOPT_CREATE [
266 "password" add-private-key
267 CRYPT_CERTTYPE_CERTIFICATE [
268 certificate-handle CRYPT_CERTINFO_XYZZY 1 set-attribute
269 certificate-handle CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO
270 context-handle *int set-attribute
271 certificate-handle CRYPT_CERTINFO_COMMONNAME "Dave Smith"
276 f 0 CRYPT_CERTFORMAT_TEXT_CERTIFICATE export-certificate
277 get-cert-length *int dup malloc swap
278 CRYPT_CERTFORMAT_TEXT_CERTIFICATE export-certificate
279 get-cert-buffer alien>char-string print
286 "=========================================================" print
287 "SSH session test..." print
288 "=========================================================" print
290 ! start client connection with:
291 ! ssh -v localhost -p3000
292 "waiting for: ssh -v localhost -p3000" print flush
294 ! Are you sure you want to continue connecting (yes/no)? yes
296 ! <at> localhost's password: (any password will be accepted)
298 ! If you want to run the test again you should clean the [localhost]:3000
299 ! ssh-rsa entry in the known_hosts file, in your home directory under the .ssh
300 ! folder, since the test generates a new RSA certificate on every run.
303 CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
304 CRYPT_KEYOPT_READONLY [
305 CRYPT_KEYID_NAME "private key" "password" get-private-key
307 CRYPT_SESSION_SSH_SERVER [
309 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
312 session-handle CRYPT_SESSINFO_SERVER_PORT 3000 set-attribute
314 session-handle CRYPT_SESSINFO_PRIVATEKEY
316 context-handle *int set-attribute
318 [ session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute ] [
319 dup CRYPT_ENVELOPE_RESOURCE = [
320 session-handle CRYPT_SESSINFO_AUTHRESPONSE 1
323 session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute
325 "Welcome to cryptlib, now go away.\r\n" set-pop-buffer
327 session-handle get-pop-buffer dup alien>char-string
330 session-handle flush-data
343 "=========================================================" print
344 "SSL session test..." print
345 "=========================================================" print
347 ! start client connection with:
348 ! curl -k https://localhost:3000
349 "waiting for: curl -k https://localhost:3000" print flush
352 CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
353 CRYPT_KEYOPT_READONLY [
354 CRYPT_KEYID_NAME "private key" "password" get-private-key
356 CRYPT_SESSION_SSL_SERVER [
357 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
359 session-handle CRYPT_SESSINFO_SERVER_PORT 3000 set-attribute
360 session-handle CRYPT_OPTION_NET_WRITETIMEOUT 10 set-attribute
361 session-handle CRYPT_OPTION_NET_READTIMEOUT 10 set-attribute
362 session-handle CRYPT_OPTION_NET_CONNECTTIMEOUT 10 set-attribute
363 session-handle CRYPT_SESSINFO_PRIVATEKEY
364 context-handle *int set-attribute
366 session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute
367 "Welcome to cryptlib, now go away.\r\n" set-pop-buffer
368 session-handle get-pop-buffer dup alien>char-string
370 session-handle flush-data