@@ -13,9 +13,10 @@ Rebol [
13
13
test-crypt : func [ port key iv plain cipher] [
14
14
modify port 'direction 'encrypt
15
15
modify port 'key key
16
- modify port 'iv iv
16
+ modify port 'init-vector iv
17
17
--assert cipher = try [read write port plain]
18
18
modify port 'direction 'decrypt
19
+ modify port 'init-vector iv ; must reset IV, because it was changed internally!
19
20
--assert plain = try [read write port cipher]
20
21
]
21
22
tests: [
@@ -109,6 +110,69 @@ foreach [cipher tests] tests [
109
110
close port
110
111
]
111
112
]
113
+ --test-- "AES-128-CBC with chunked input"
114
+ port: open make port! [
115
+ scheme: 'crypt
116
+ algorithm: 'AES-128 -CBC
117
+ key: #{ 2B7E151628AED2A6ABF7158809CF4F3C }
118
+ ]
119
+ iv: #{ 000102030405060708090A0B0C0D0E0F }
120
+ modify port 'init-vector :iv
121
+ ; writing data in 2 chunks
122
+ write port #{ 6BC1BEE22E409F96 }
123
+ write port #{ E93D7E117393172A }
124
+ ; no need to `update`, because the input has full block size now
125
+ --assert #{ 7649ABAC8119B246CEE98E9B12E9197D } = read port
126
+
127
+ modify port 'init-vector :iv
128
+ write port #{ 6BC1BEE22E409F96 }
129
+
130
+ --assert none? read port ; because the input was not long enough
131
+ crypted: read update port ; the input is padded by `update` call
132
+ --assert crypted = #{ 6F8BB83BE51C9A7DE442745E517D4377 }
133
+ --assert none? read update port ; there are no more data
134
+ --assert none? take port
135
+
136
+ modify port 'direction 'decrypt
137
+ modify port 'init-vector :iv
138
+ ; result contains padded data!
139
+ --assert #{ 6BC1BEE22E409F960000000000000000 } = read write port :crypted
140
+
141
+ --test-- "AES-128-CBC with chunked input which needs padding"
142
+ c-port: open make port! [
143
+ scheme: 'crypt
144
+ algorithm: 'AES-128 -CBC
145
+ key: #{ 2B7E151628AED2A6ABF7158809CF4F3C }
146
+ init-vector: #{ 000102030405060708090A0B0C0D0E0F }
147
+ ]
148
+ ;d-port: open make port! [
149
+ ; scheme: 'crypt
150
+ ; direction: 'decrypt
151
+ ; algorithm: 'AES-128-CBC
152
+ ; key: #{2B7E151628AED2A6ABF7158809CF4F3C}
153
+ ; init-vector: #{000102030405060708090A0B0C0D0E0F}
154
+ ;]
155
+ ;- above code can be also done using:
156
+ d-port: open crypt://AES-128-CBC#decrypt
157
+ modify d-port 'key #{ 2B7E151628AED2A6ABF7158809CF4F3C }
158
+ modify d-port 'init-vector #{ 000102030405060708090A0B0C0D0E0F }
159
+
160
+ write c-port #{ 6BC1BEE22E409F96 }
161
+ write c-port #{ E93D7E117393172ADEADBEEF }
162
+ ; notice that there is just a partial output, because the input is longer than a block!
163
+ crypted: read c-port
164
+ --assert crypted = #{ 7649ABAC8119B246CEE98E9B12E9197D }
165
+ write d-port :crypted
166
+ crypted: take c-port ; same as `read update c-port` -> take forces the padding
167
+ --assert crypted = #{ 00CA0B3A31A8BD78A2815653B0C4C9DB }
168
+ write d-port :crypted
169
+ plain: take d-port
170
+ ; the output is padded!
171
+ --assert plain = #{ 6BC1BEE22E409F96E93D7E117393172ADEADBEEF000000000000000000000000 }
172
+ close c-port
173
+ close d-port
174
+
175
+
112
176
===end-group===
113
177
114
178
@@ -118,7 +182,7 @@ foreach [cipher tests] tests [
118
182
monte-carlo-test-crypt : function [ port key iv plain cipher] [
119
183
modify port 'direction 'encrypt
120
184
modify port 'key key
121
- modify port 'iv iv
185
+ modify port 'init-vector iv
122
186
x: plain
123
187
loop 10000 [ x: read write port x ]
124
188
--assert cipher = x
@@ -172,7 +236,7 @@ foreach [cipher tests] monte-carlo-ecb-tests [
172
236
monte-carlo-cbc-test-crypt : function [ port key iv plain cipher] [
173
237
modify port 'direction 'encrypt
174
238
modify port 'key key
175
- modify port 'iv iv
239
+ modify port 'init-vector iv
176
240
pt: plain
177
241
ct-1: iv
178
242
loop 10000 [
@@ -283,10 +347,11 @@ foreach [cipher tests] monte-carlo-cbc-tests [
283
347
foreach [cipher tests] tests [
284
348
unless find system/catalog/ciphers cipher [continue]
285
349
foreach [k i p c] tests [
286
- port: open [scheme: 'crypt algorithm: cipher key: k iv : i]
350
+ port: open [scheme: 'crypt algorithm: cipher key: k init-vector : i]
287
351
--test-- join "Crypt port: " cipher
288
352
--assert c = read write port p
289
353
modify port 'direction 'decrypt
354
+ modify port 'init-vector i ; must reset IV, because it was changed internally!
290
355
--assert p = read write port c
291
356
close port
292
357
]
@@ -330,7 +395,7 @@ if find system/catalog/ciphers 'CHACHA20 [
330
395
foreach [key nonce plain cipher] tests [
331
396
--test-- join "Test Vector #" n
332
397
modify port 'key key
333
- modify port 'iv nonce
398
+ modify port 'init-vector nonce
334
399
--assert cipher = read write port plain
335
400
++ n
336
401
]
@@ -339,7 +404,7 @@ if find system/catalog/ciphers 'CHACHA20 [
339
404
340
405
===start-group=== "The ChaCha20 encryption from RFC7539"
341
406
;https://datatracker.ietf.org/doc/rfc7539/
342
- port: open crypt:// CHACHA20
407
+ port: open crypt:CHACHA20
343
408
344
409
tests: [
345
410
#{ 0000000000000000000000000000000000000000000000000000000000000000 }
@@ -371,7 +436,7 @@ if find system/catalog/ciphers 'CHACHA20 [
371
436
foreach [key nonce plain cipher] tests [
372
437
--test-- join "Test Vector #" n
373
438
modify port 'key key
374
- modify port 'iv nonce
439
+ modify port 'init-vector nonce
375
440
--assert cipher = read write port plain
376
441
++ n
377
442
]
0 commit comments