blob: 4ffb7200e2ba3c6e4a22962c131e62ec1a3beefe [file] [log] [blame]
Bram Moolenaar680e0152016-09-25 20:54:11 +02001" Tests for encryption.
Bram Moolenaar1eceada2016-09-26 20:14:56 +02002
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +01003source shared.vim
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02004source check.vim
5CheckFeature cryptv
Bram Moolenaar680e0152016-09-25 20:54:11 +02006
Bram Moolenaar90e66ec2022-11-10 00:25:05 +00007" Use the xxd command from:
8" 1: $XXDPROG if set and it is executable
9" 2: the ../xxd directory if the executable is found there
10if !empty($XXDPROG) && executable($XXDPROG)
James McCoya5d4f3b2021-10-11 16:27:03 +010011 let s:xxd_cmd = $XXDPROG
Bram Moolenaar90e66ec2022-11-10 00:25:05 +000012elseif executable('..\xxd\xxd.exe')
13 " we're on MS-Windows
14 let s:xxd_cmd = '..\xxd\xxd.exe'
15elseif executable('../xxd/xxd')
16 " we're on something like Unix
17 let s:xxd_cmd = '../xxd/xxd'
18else
19 " looks like xxd wasn't build (yet)
20 let s:xxd_cmd = ''
James McCoya5d4f3b2021-10-11 16:27:03 +010021endif
22
Bram Moolenaar680e0152016-09-25 20:54:11 +020023func Common_head_only(text)
24 " This was crashing Vim
25 split Xtest.txt
26 call setline(1, a:text)
27 wq
28 call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", "tx")
29 call delete('Xtest.txt')
30 call assert_match('VimCrypt', getline(1))
31 bwipe!
32endfunc
33
34func Test_head_only_2()
35 call Common_head_only('VimCrypt~02!abc')
36endfunc
37
38func Test_head_only_3()
39 call Common_head_only('VimCrypt~03!abc')
40endfunc
Bram Moolenaar17777852016-09-27 21:30:22 +020041
Christian Brabandtf573c6e2021-06-20 14:02:16 +020042func Test_head_only_4()
43 CheckFeature sodium
44 call Common_head_only('VimCrypt~04!abc')
45endfunc
46
Bram Moolenaar17777852016-09-27 21:30:22 +020047func Crypt_uncrypt(method)
48 exe "set cryptmethod=" . a:method
49 " If the blowfish test fails 'cryptmethod' will be 'zip' now.
50 call assert_equal(a:method, &cryptmethod)
51
52 split Xtest.txt
ichizok35a2ec12023-04-25 15:27:27 +010053 let text =<< trim END
54 01234567890123456789012345678901234567,
55 line 2 foo bar blah,
56 line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
57 END
Bram Moolenaar17777852016-09-27 21:30:22 +020058 call setline(1, text)
59 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
Bram Moolenaar987411d2019-01-18 22:48:34 +010060 call assert_equal('*****', &key)
Bram Moolenaar17777852016-09-27 21:30:22 +020061 w!
62 bwipe!
63 call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", 'xt')
64 call assert_equal(text, getline(1, 3))
65 set key= cryptmethod&
66 bwipe!
67 call delete('Xtest.txt')
68endfunc
69
70func Test_crypt_zip()
71 call Crypt_uncrypt('zip')
72endfunc
73
74func Test_crypt_blowfish()
75 call Crypt_uncrypt('blowfish')
76endfunc
77
78func Test_crypt_blowfish2()
79 call Crypt_uncrypt('blowfish2')
80endfunc
81
Christian Brabandtf573c6e2021-06-20 14:02:16 +020082func Test_crypt_sodium()
83 CheckFeature sodium
84 call Crypt_uncrypt('xchacha20')
85endfunc
86
Christian Brabandtaae58342023-04-23 17:50:22 +010087func Test_crypt_sodium_v2()
88 CheckFeature sodium
89 call Crypt_uncrypt('xchacha20v2')
90endfunc
91
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +010092func Test_crypt_sodium_v2_startup()
93 CheckFeature sodium
94 CheckRunVimInTerminal
95
96 let buf = RunVimInTerminal('--cmd "set cm=xchacha20v2" -x Xfoo', #{wait_for_ruler: 0, rows: 6})
97 call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50)
98 call term_sendkeys(buf, "foo\<CR>foo\<CR>")
99 call term_sendkeys(buf, "ifoo\<Esc>")
100 call term_sendkeys(buf, "ZZ")
101 call TermWait(buf)
102
103 " Wait for Vim to write the file and exit. Then wipe out the terminal buffer.
104 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
105 exe buf .. 'bwipe!'
106 call assert_true(filereadable('Xfoo'))
107
Christian Brabandt19e6c4f2023-06-27 18:57:10 +0100108 let buf = RunVimInTerminal('--cmd "set ch=3 cm=xchacha20v2 key=foo" Xfoo', #{wait_for_ruler: 0, rows: 10})
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100109 call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50)
110 call StopVimInTerminal(buf)
111
112 call delete('Xfoo')
113endfunc
114
Bram Moolenaar17777852016-09-27 21:30:22 +0200115func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
116 split Xtest.txt
117 set bin noeol key= fenc=latin1
118 exe "set cryptmethod=" . a:method
119 call setline(1, a:crypted_text)
120 w!
121 bwipe!
122 set nobin
123 call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt')
124 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
125 bwipe!
126 call delete('Xtest.txt')
127 set key=
128endfunc
129
Christian Brabandtaae58342023-04-23 17:50:22 +0100130func Uncrypt_stable_xxd(method, hex, key, uncrypted_text, verbose)
James McCoya5d4f3b2021-10-11 16:27:03 +0100131 if empty(s:xxd_cmd)
132 throw 'Skipped: xxd program missing'
133 endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200134 " use xxd to write the binary content
James McCoya5d4f3b2021-10-11 16:27:03 +0100135 call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex)
Christian Brabandtaae58342023-04-23 17:50:22 +0100136 let cmd = (a:verbose ? ':verbose' : '') ..
137 \ ":split Xtest.txt\<CR>" . a:key . "\<CR>"
138 call feedkeys(cmd, 'xt')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200139 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
140 bwipe!
141 call delete('Xtest.txt')
142 set key=
143endfunc
144
Bram Moolenaar17777852016-09-27 21:30:22 +0200145func Test_uncrypt_zip()
146 call Uncrypt_stable('zip', "VimCrypt~01!\u0006\u001clV'\u00de}Mg\u00a0\u00ea\u00a3V\u00a9\u00e7\u0007E#3\u008e2U\u00e9\u0097", "foofoo", ["1234567890", "aábbccddeëff"])
147endfunc
148
149func Test_uncrypt_blowfish()
150 call Uncrypt_stable('blowfish', "VimCrypt~02!k)\u00be\u0017\u0097#\u0016\u00ddS\u009c\u00f5=\u00ba\u00e0\u00c8#\u00a5M\u00b4\u0086J\u00c3A\u00cd\u00a5M\u00b4\u0086!\u0080\u0015\u009b\u00f5\u000f\u00e1\u00d2\u0019\u0082\u0016\u0098\u00f7\u000d\u00da", "barbar", ["asdfasdfasdf", "0001112223333"])
151endfunc
152
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200153func Test_uncrypt_blowfish2a()
Bram Moolenaar17777852016-09-27 21:30:22 +0200154 call Uncrypt_stable('blowfish', "VimCrypt~03!\u001e\u00d1N\u00e3;\u00d3\u00c0\u00a0^C)\u0004\u00f7\u007f.\u00b6\u00abF\u000eS\u0019\u00e0\u008b6\u00d2[T\u00cb\u00a7\u0085\u00d8\u00be9\u000b\u00812\u000bQ\u00b3\u00cc@\u0097\u000f\u00df\u009a\u00adIv\u00aa.\u00d8\u00c9\u00ee\u009e`\u00bd$\u00af%\u00d0", "barburp", ["abcdefghijklmnopqrstuvwxyz", "!@#$%^&*()_+=-`~"])
155endfunc
Bram Moolenaar987411d2019-01-18 22:48:34 +0100156
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200157func Test_uncrypt_blowfish2()
158 call Uncrypt_stable('blowfish2', "VimCrypt~03!\u001e\u00d1N\u00e3;\u00d3\u00c0\u00a0^C)\u0004\u00f7\u007f.\u00b6\u00abF\u000eS\u0019\u00e0\u008b6\u00d2[T\u00cb\u00a7\u0085\u00d8\u00be9\u000b\u00812\u000bQ\u00b3\u00cc@\u0097\u000f\u00df\u009a\u00adIv\u00aa.\u00d8\u00c9\u00ee\u009e`\u00bd$\u00af%\u00d0", "barburp", ["abcdefghijklmnopqrstuvwxyz", "!@#$%^&*()_+=-`~"])
159endfunc
160
161func Test_uncrypt_xchacha20()
162 CheckFeature sodium
ichizok35a2ec12023-04-25 15:27:27 +0100163 let hex =<< trim END
164 00000000: 5669 6d43 7279 7074 7e30 3421 6b7d e607 vimCrypt~04!k}..
165 00000010: 4ea4 e99f 923e f67f 7b59 a80d 3bca 2f06 N....>..{Y..;./.
166 00000020: fa11 b951 8d09 0dc9 470f e7cf 8b90 4310 ...Q....G.....C.
167 00000030: 653b b83b e493 378b 0390 0e38 f912 626b e;.;..7....8..bk
168 00000040: a02e 4697 0254 2625 2d8e 3a0b 784b e89c ..F..T&%-.:.xK..
169 00000050: 0c67 a975 3c17 9319 8ffd 1463 7783 a1f3 .g.u<......cw...
170 00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~.
171 00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t.
172 00000080: 72be 0136 84a1 d3 r..6...
173 END
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200174 " the file should be in latin1 encoding, this makes sure that readfile()
175 " retries several times converting the multi-byte characters
Christian Brabandtaae58342023-04-23 17:50:22 +0100176 call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"], 0)
177endfunc
178
179func Test_uncrypt_xchacha20v2_custom()
180 CheckFeature sodium
181 " Test, reading xchacha20v2 with custom encryption parameters
ichizok35a2ec12023-04-25 15:27:27 +0100182 let hex =<< trim END
183 00000000: 5669 6d43 7279 7074 7e30 3521 934b f288 VimCrypt~05!.K..
184 00000010: 10ba 8bc9 25a0 8876 f85c f135 6fb8 518b ....%..v.\.5o.Q.
185 00000020: b133 9af1 0300 0000 0000 0000 0000 0010 .3..............
186 00000030: 0000 0000 0200 0000 b973 5f33 80e9 54fc .........s_3..T.
187 00000040: 138f ba3e 046b 3135 90b7 7783 5eac 7fe3 ...>.k15..w.^...
188 00000050: 0cd2 14df ed75 4b65 8763 8205 035c ec81 .....uKe.c...\..
189 00000060: a4cf 33d2 7507 ec38 ba62 a327 9068 d8ad ..3.u..8.b.'.h..
190 00000070: 2607 3fa6 f95d 7ea8 9799 f997 4820 0c &.?..]~.....H .
191 END
Christian Brabandtaae58342023-04-23 17:50:22 +0100192 call Uncrypt_stable_xxd('xchacha20v2', hex, "foobar", ["", "foo", "bar", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], 1)
193 call assert_match('xchacha20v2: using custom \w\+ "\d\+" for Key derivation.', execute(':messages'))
194endfunc
195
196func Test_uncrypt_xchacha20v2()
197 CheckFeature sodium
198 " Test, reading xchacha20v2
ichizok35a2ec12023-04-25 15:27:27 +0100199 let hex =<< trim END
200 00000000: 5669 6d43 7279 7074 7e30 3521 9f20 4e14 VimCrypt~05!. N.
201 00000010: c7da c1bd 7dea 8fbc db6c 38e6 7a77 6fef ....}....l8.zwo.
202 00000020: 82dd 964b 0300 0000 0000 0000 0000 0010 ...K............
203 00000030: 0000 0000 0200 0000 a97c 2f00 0b9d 19eb .........|/.....
204 00000040: 1d92 1ea5 3f22 c179 4b3e 870a eb19 6380 ....?".yK>....c.
205 00000050: 63f8 222d b5d1 3c73 7be5 d580 47ea 44cc c."-..<s{...G.D.
206 00000060: 6c25 8078 3fd5 d836 c700 0122 bb30 7a59 l%.x?..6...".0zY
207 00000070: b184 2ae8 e7db 113a f732 938f 7a34 1333 ..*....:.2..z4.3
208 00000080: dc89 1491 51a0 67b9 0f3a b56c 1f9d 53b0 ....Q.g..:.l..S.
209 00000090: 2416 205a 8c4c 5fde 4dac 2611 8a48 24f0 $. Z.L_.M.&..H$.
210 000000a0: ba00 92c1 60 ....`
211 END
Christian Brabandtaae58342023-04-23 17:50:22 +0100212 call Uncrypt_stable_xxd('xchacha20v2', hex, "foo1234", ["abcdefghijklmnopqrstuvwxyzäöü", 'ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"'], 0)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200213endfunc
214
215func Test_uncrypt_xchacha20_invalid()
216 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200217
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200218 " load an invalid encrypted file and verify it can be decrypted with an
219 " error message
220 try
221 call feedkeys(":split samples/crypt_sodium_invalid.txt\<CR>sodium\<CR>", 'xt')
222 call assert_false(1, 'should not happen')
223 catch
224 call assert_exception('pre-mature')
225 endtry
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200226 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200227
228 call assert_equal(0, &swapfile)
229 call assert_equal("xchacha20", &cryptmethod)
230 call assert_equal('311111111111111111111111', getline('$'))
231 bw!
232endfunc
233
234func Test_uncrypt_xchacha20_2()
235 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200236
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200237 sp Xcrypt_sodium.txt
238 " Create a larger file, so that Vim will write in several blocks
Christian Brabandtaae58342023-04-23 17:50:22 +0100239 call setline(1, range(1, 4000))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200240 call assert_equal(1, &swapfile)
241 set cryptmethod=xchacha20
242 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
243 " swapfile disabled
244 call assert_equal(0, &swapfile)
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200245 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200246 w!
247 " encrypted using xchacha20
248 call assert_match("\[xchacha20\]", execute(':messages'))
249 bw!
250 call feedkeys(":sp Xcrypt_sodium.txt\<CR>sodium\<CR>", 'xt')
251 " successfully decrypted
252 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
253 set key=
Bram Moolenaardb864722021-07-08 11:37:50 +0200254 w! ++ff=unix
Dominique Pelle81b573d2022-03-22 21:14:55 +0000255 " encryption removed (on MS-Windows the .* matches [unix])
Christian Brabandt16e26a32021-07-13 19:09:12 +0200256 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200257 bw!
258 call delete('Xcrypt_sodium.txt')
259 set cryptmethod&vim
Christian Brabandtaae58342023-04-23 17:50:22 +0100260
261endfunc
262
263func Test_uncrypt_xchacha20v2_2()
264 CheckFeature sodium
265
266 sp Xcrypt_sodium_v2.txt
267 " Create a larger file, so that Vim will write in several blocks
268 call setline(1, range(1, 4000))
269 call assert_equal(1, &swapfile)
270 set cryptmethod=xchacha20v2
271 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
272 " swapfile disabled
273 call assert_equal(0, &swapfile)
274 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
275 w!
276 " encrypted using xchacha20
277 call assert_match("\[xchachav2\]", execute(':messages'))
278 bw!
279 call feedkeys(":verbose :sp Xcrypt_sodium_v2.txt\<CR>sodium\<CR>", 'xt')
280 " successfully decrypted
281 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
282 call assert_match('xchacha20v2: using default \w\+ "\d\+" for Key derivation.', execute(':messages'))
283 set key=
284 w! ++ff=unix
285 " encryption removed (on MS-Windows the .* matches [unix])
286 call assert_match('"Xcrypt_sodium_v2.txt".*4000L, 18893B written', execute(':message'))
287 bw!
288 call delete('Xcrypt_sodium_v2.txt')
289 set cryptmethod&vim
290
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200291endfunc
292
293func Test_uncrypt_xchacha20_3_persistent_undo()
294 CheckFeature sodium
295 CheckFeature persistent_undo
Bram Moolenaardb864722021-07-08 11:37:50 +0200296
Christian Brabandtaae58342023-04-23 17:50:22 +0100297 for meth in ['xchacha20', 'xchacha20v2']
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200298
Christian Brabandtaae58342023-04-23 17:50:22 +0100299 sp Xcrypt_sodium_undo.txt
300 exe "set cryptmethod=" .. meth .. " undofile"
301 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
302 call assert_equal(1, &undofile)
303 let ufile=undofile(@%)
304 call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
305 call cursor(1, 1)
306
307 set undolevels=100
308 normal dd
309 set undolevels=100
310 normal dd
311 set undolevels=100
312 normal dd
313 set undolevels=100
314 w!
315 call assert_equal(0, &undofile)
316 bw!
317 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt')
318 " should fail
319 norm! u
320 call assert_match('Already at oldest change', execute(':1mess'))
321 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822')
322 bw!
323 set undolevels& cryptmethod& undofile&
324 call delete('Xcrypt_sodium_undo.txt')
325
326 endfor
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200327endfunc
328
329func Test_encrypt_xchacha20_missing()
330 if has("sodium")
331 return
332 endif
333 sp Xcrypt_sodium_undo.txt
334 call assert_fails(':set cryptmethod=xchacha20', 'E474')
Christian Brabandtaae58342023-04-23 17:50:22 +0100335 call assert_fails(':set cryptmethod=xchacha20v2', 'E474')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200336 bw!
337 set cm&
338endfunc
339
Bram Moolenaar987411d2019-01-18 22:48:34 +0100340func Test_uncrypt_unknown_method()
341 split Xuncrypt_unknown.txt
342 set bin noeol key= fenc=latin1
343 call setline(1, "VimCrypt~93!\u001e\u00d1")
344 w!
345 bwipe!
346 set nobin
347 call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
348
349 bwipe!
350 call delete('Xuncrypt_unknown.txt')
351 set key=
352endfunc
353
354func Test_crypt_key_mismatch()
355 set cryptmethod=blowfish
356
357 split Xtest.txt
358 call setline(1, 'nothing')
359 call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
360 call assert_match("Keys don't match!", execute(':2messages'))
361 call assert_equal('', &key)
362 call feedkeys("\<CR>\<CR>", 'xt')
363
364 set cryptmethod&
365 bwipe!
366endfunc
367
Bram Moolenaar76cb6832020-05-15 22:30:38 +0200368func Test_crypt_set_key_changes_buffer()
369
370 new Xtest1.txt
371 call setline(1, 'nothing')
372 set cryptmethod=blowfish2
373 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
374 call assert_fails(":q", "E37:")
375 w
376 set key=anotherkey
377 call assert_fails(":bw")
378 w
379 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
380 call assert_fails(":bw")
381 w
382 let winnr = winnr()
383 wincmd p
384 call setwinvar(winnr, '&key', 'yetanotherkey')
385 wincmd p
386 call assert_fails(":bw")
387 w
388
389 set cryptmethod&
390 set key=
391 bwipe!
392 call delete('Xtest1.txt')
393endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200394
Christian Brabandt19e6c4f2023-06-27 18:57:10 +0100395func Test_crypt_set_key_segfault()
396 CheckFeature sodium
397
398 defer delete('Xtest2.txt')
399 new Xtest2.txt
400 call setline(1, 'nothing')
401 set cryptmethod=xchacha20
402 set key=foobar
403 w
404 new Xtest3
405 put ='other content'
406 setl modified
407 sil! preserve
408 bwipe!
409
410 set cryptmethod&
411 set key=
412 bwipe!
413endfunc
414
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200415" vim: shiftwidth=2 sts=2 expandtab