comparison src/testdir/test_crypt.vim @ 32299:3d4e28569a6d v9.0.1481

patch 9.0.1481: decrypting with libsodium may fail if the library changes Commit: https://github.com/vim/vim/commit/aae583441bcdbb0bfef3b8a1d193e04ae09ca95d Author: Christian Brabandt <cb@256bit.org> Date: Sun Apr 23 17:50:22 2023 +0100 patch 9.0.1481: decrypting with libsodium may fail if the library changes Problem: Decrypting with libsodium may fail if the library changes. Solution: Add parameters used to the encrypted file header. (Christian Brabandt, closes #12279)
author Bram Moolenaar <Bram@vim.org>
date Sun, 23 Apr 2023 19:00:05 +0200
parents 10e6088a2a72
children 6761c71f4b25
comparison
equal deleted inserted replaced
32298:dace7bf9cd79 32299:3d4e28569a6d
79 func Test_crypt_sodium() 79 func Test_crypt_sodium()
80 CheckFeature sodium 80 CheckFeature sodium
81 call Crypt_uncrypt('xchacha20') 81 call Crypt_uncrypt('xchacha20')
82 endfunc 82 endfunc
83 83
84 func Test_crypt_sodium_v2()
85 CheckFeature sodium
86 call Crypt_uncrypt('xchacha20v2')
87 endfunc
88
84 func Uncrypt_stable(method, crypted_text, key, uncrypted_text) 89 func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
85 split Xtest.txt 90 split Xtest.txt
86 set bin noeol key= fenc=latin1 91 set bin noeol key= fenc=latin1
87 exe "set cryptmethod=" . a:method 92 exe "set cryptmethod=" . a:method
88 call setline(1, a:crypted_text) 93 call setline(1, a:crypted_text)
94 bwipe! 99 bwipe!
95 call delete('Xtest.txt') 100 call delete('Xtest.txt')
96 set key= 101 set key=
97 endfunc 102 endfunc
98 103
99 func Uncrypt_stable_xxd(method, hex, key, uncrypted_text) 104 func Uncrypt_stable_xxd(method, hex, key, uncrypted_text, verbose)
100 if empty(s:xxd_cmd) 105 if empty(s:xxd_cmd)
101 throw 'Skipped: xxd program missing' 106 throw 'Skipped: xxd program missing'
102 endif 107 endif
103 " use xxd to write the binary content 108 " use xxd to write the binary content
104 call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex) 109 call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex)
105 call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt') 110 let cmd = (a:verbose ? ':verbose' : '') ..
111 \ ":split Xtest.txt\<CR>" . a:key . "\<CR>"
112 call feedkeys(cmd, 'xt')
106 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text))) 113 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
107 bwipe! 114 bwipe!
108 call delete('Xtest.txt') 115 call delete('Xtest.txt')
109 set key= 116 set key=
110 endfunc 117 endfunc
136 \ '00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~.', 143 \ '00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~.',
137 \ '00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t.', 144 \ '00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t.',
138 \ '00000080: 72be 0136 84a1 d3 r..6...'] 145 \ '00000080: 72be 0136 84a1 d3 r..6...']
139 " the file should be in latin1 encoding, this makes sure that readfile() 146 " the file should be in latin1 encoding, this makes sure that readfile()
140 " retries several times converting the multi-byte characters 147 " retries several times converting the multi-byte characters
141 call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"]) 148 call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"], 0)
149 endfunc
150
151 func Test_uncrypt_xchacha20v2_custom()
152 CheckFeature sodium
153 " Test, reading xchacha20v2 with custom encryption parameters
154 let hex = ['00000000: 5669 6d43 7279 7074 7e30 3521 934b f288 VimCrypt~05!.K..',
155 \ '00000010: 10ba 8bc9 25a0 8876 f85c f135 6fb8 518b ....%..v.\.5o.Q.',
156 \ '00000020: b133 9af1 0300 0000 0000 0000 0000 0010 .3..............',
157 \ '00000030: 0000 0000 0200 0000 b973 5f33 80e9 54fc .........s_3..T.',
158 \ '00000040: 138f ba3e 046b 3135 90b7 7783 5eac 7fe3 ...>.k15..w.^...',
159 \ '00000050: 0cd2 14df ed75 4b65 8763 8205 035c ec81 .....uKe.c...\..',
160 \ "00000060: a4cf 33d2 7507 ec38 ba62 a327 9068 d8ad ..3.u..8.b.'.h..",
161 \ '00000070: 2607 3fa6 f95d 7ea8 9799 f997 4820 0c &.?..]~.....H .']
162 call Uncrypt_stable_xxd('xchacha20v2', hex, "foobar", ["", "foo", "bar", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], 1)
163 call assert_match('xchacha20v2: using custom \w\+ "\d\+" for Key derivation.', execute(':messages'))
164 endfunc
165
166 func Test_uncrypt_xchacha20v2()
167 CheckFeature sodium
168 " Test, reading xchacha20v2
169 let hex = [
170 \ '00000000: 5669 6d43 7279 7074 7e30 3521 9f20 4e14 VimCrypt~05!. N.',
171 \ '00000010: c7da c1bd 7dea 8fbc db6c 38e6 7a77 6fef ....}....l8.zwo.',
172 \ '00000020: 82dd 964b 0300 0000 0000 0000 0000 0010 ...K............',
173 \ '00000030: 0000 0000 0200 0000 a97c 2f00 0b9d 19eb .........|/.....',
174 \ '00000040: 1d92 1ea5 3f22 c179 4b3e 870a eb19 6380 ....?".yK>....c.',
175 \ '00000050: 63f8 222d b5d1 3c73 7be5 d580 47ea 44cc c."-..<s{...G.D.',
176 \ '00000060: 6c25 8078 3fd5 d836 c700 0122 bb30 7a59 l%.x?..6...".0zY',
177 \ '00000070: b184 2ae8 e7db 113a f732 938f 7a34 1333 ..*....:.2..z4.3',
178 \ '00000080: dc89 1491 51a0 67b9 0f3a b56c 1f9d 53b0 ....Q.g..:.l..S.',
179 \ '00000090: 2416 205a 8c4c 5fde 4dac 2611 8a48 24f0 $. Z.L_.M.&..H$.',
180 \ '000000a0: ba00 92c1 60 ....`']
181 call Uncrypt_stable_xxd('xchacha20v2', hex, "foo1234", ["abcdefghijklmnopqrstuvwxyzäöü", 'ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"'], 0)
142 endfunc 182 endfunc
143 183
144 func Test_uncrypt_xchacha20_invalid() 184 func Test_uncrypt_xchacha20_invalid()
145 CheckFeature sodium 185 CheckFeature sodium
146 186
163 func Test_uncrypt_xchacha20_2() 203 func Test_uncrypt_xchacha20_2()
164 CheckFeature sodium 204 CheckFeature sodium
165 205
166 sp Xcrypt_sodium.txt 206 sp Xcrypt_sodium.txt
167 " Create a larger file, so that Vim will write in several blocks 207 " Create a larger file, so that Vim will write in several blocks
168 call setline(1, range(1,4000)) 208 call setline(1, range(1, 4000))
169 call assert_equal(1, &swapfile) 209 call assert_equal(1, &swapfile)
170 set cryptmethod=xchacha20 210 set cryptmethod=xchacha20
171 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt') 211 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
172 " swapfile disabled 212 " swapfile disabled
173 call assert_equal(0, &swapfile) 213 call assert_equal(0, &swapfile)
184 " encryption removed (on MS-Windows the .* matches [unix]) 224 " encryption removed (on MS-Windows the .* matches [unix])
185 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message')) 225 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
186 bw! 226 bw!
187 call delete('Xcrypt_sodium.txt') 227 call delete('Xcrypt_sodium.txt')
188 set cryptmethod&vim 228 set cryptmethod&vim
229
230 endfunc
231
232 func Test_uncrypt_xchacha20v2_2()
233 CheckFeature sodium
234
235 sp Xcrypt_sodium_v2.txt
236 " Create a larger file, so that Vim will write in several blocks
237 call setline(1, range(1, 4000))
238 call assert_equal(1, &swapfile)
239 set cryptmethod=xchacha20v2
240 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
241 " swapfile disabled
242 call assert_equal(0, &swapfile)
243 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
244 w!
245 " encrypted using xchacha20
246 call assert_match("\[xchachav2\]", execute(':messages'))
247 bw!
248 call feedkeys(":verbose :sp Xcrypt_sodium_v2.txt\<CR>sodium\<CR>", 'xt')
249 " successfully decrypted
250 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
251 call assert_match('xchacha20v2: using default \w\+ "\d\+" for Key derivation.', execute(':messages'))
252 set key=
253 w! ++ff=unix
254 " encryption removed (on MS-Windows the .* matches [unix])
255 call assert_match('"Xcrypt_sodium_v2.txt".*4000L, 18893B written', execute(':message'))
256 bw!
257 call delete('Xcrypt_sodium_v2.txt')
258 set cryptmethod&vim
259
189 endfunc 260 endfunc
190 261
191 func Test_uncrypt_xchacha20_3_persistent_undo() 262 func Test_uncrypt_xchacha20_3_persistent_undo()
192 CheckFeature sodium 263 CheckFeature sodium
193 CheckFeature persistent_undo 264 CheckFeature persistent_undo
194 265
195 sp Xcrypt_sodium_undo.txt 266 for meth in ['xchacha20', 'xchacha20v2']
196 set cryptmethod=xchacha20 undofile 267
197 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt') 268 sp Xcrypt_sodium_undo.txt
198 call assert_equal(1, &undofile) 269 exe "set cryptmethod=" .. meth .. " undofile"
199 let ufile=undofile(@%) 270 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
200 call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) 271 call assert_equal(1, &undofile)
201 call cursor(1, 1) 272 let ufile=undofile(@%)
202 273 call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
203 set undolevels=100 274 call cursor(1, 1)
204 normal dd 275
205 set undolevels=100 276 set undolevels=100
206 normal dd 277 normal dd
207 set undolevels=100 278 set undolevels=100
208 normal dd 279 normal dd
209 set undolevels=100 280 set undolevels=100
210 w! 281 normal dd
211 call assert_equal(0, &undofile) 282 set undolevels=100
212 bw! 283 w!
213 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt') 284 call assert_equal(0, &undofile)
214 " should fail 285 bw!
215 norm! u 286 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt')
216 call assert_match('Already at oldest change', execute(':1mess')) 287 " should fail
217 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822') 288 norm! u
218 bw! 289 call assert_match('Already at oldest change', execute(':1mess'))
219 set undolevels& cryptmethod& undofile& 290 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822')
220 call delete('Xcrypt_sodium_undo.txt') 291 bw!
292 set undolevels& cryptmethod& undofile&
293 call delete('Xcrypt_sodium_undo.txt')
294
295 endfor
221 endfunc 296 endfunc
222 297
223 func Test_encrypt_xchacha20_missing() 298 func Test_encrypt_xchacha20_missing()
224 if has("sodium") 299 if has("sodium")
225 return 300 return
226 endif 301 endif
227 sp Xcrypt_sodium_undo.txt 302 sp Xcrypt_sodium_undo.txt
228 call assert_fails(':set cryptmethod=xchacha20', 'E474') 303 call assert_fails(':set cryptmethod=xchacha20', 'E474')
304 call assert_fails(':set cryptmethod=xchacha20v2', 'E474')
229 bw! 305 bw!
230 set cm& 306 set cm&
231 endfunc 307 endfunc
232 308
233 func Test_uncrypt_unknown_method() 309 func Test_uncrypt_unknown_method()