blob: 85ff33a314a386a38ca36ced054fcedebf52b45c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * crypt.c: Generic encryption support.
12 */
13#include "vim.h"
14
15#if defined(FEAT_CRYPT) || defined(PROTO)
16/*
17 * Optional encryption support.
18 * Mohsin Ahmed, mosh@sasi.com, 1998-09-24
19 * Based on zip/crypt sources.
20 * Refactored by David Leadbeater, 2014.
21 *
22 * NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to
23 * most countries. There are a few exceptions, but that still should not be a
24 * problem since this code was originally created in Europe and India.
25 *
26 * Blowfish addition originally made by Mohsin Ahmed,
27 * http://www.cs.albany.edu/~mosh 2010-03-14
28 * Based on blowfish by Bruce Schneier (http://www.schneier.com/blowfish.html)
29 * and sha256 by Christophe Devine.
30 */
31
32typedef struct {
Bram Moolenaarc667da52019-11-30 20:52:27 +010033 char *name; // encryption name as used in 'cryptmethod'
34 char *magic; // magic bytes stored in file header
35 int salt_len; // length of salt, or 0 when not using salt
Christian Brabandtf573c6e2021-06-20 14:02:16 +020036 int seed_len; // length of seed, or 0 when not using seed
Bram Moolenaar987411d2019-01-18 22:48:34 +010037#ifdef CRYPT_NOT_INPLACE
Bram Moolenaarc667da52019-11-30 20:52:27 +010038 int works_inplace; // encryption/decryption can be done in-place
Bram Moolenaar987411d2019-01-18 22:48:34 +010039#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +010040 int whole_undofile; // whole undo file is encrypted
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020041
Bram Moolenaarc667da52019-11-30 20:52:27 +010042 // Optional function pointer for a self-test.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020043 int (* self_test_fn)();
44
Bram Moolenaarad3ec762019-04-21 00:00:13 +020045 // Function pointer for initializing encryption/decryption.
Bram Moolenaar6ee96582019-04-27 22:06:37 +020046 int (* init_fn)(cryptstate_T *state, char_u *key,
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020047 char_u *salt, int salt_len, char_u *seed, int seed_len);
48
Bram Moolenaarc667da52019-11-30 20:52:27 +010049 // Function pointers for encoding/decoding from one buffer into another.
50 // Optional, however, these or the _buffer ones should be configured.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020051 void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +020052 char_u *to, int last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020053 void (*decode_fn)(cryptstate_T *state, char_u *from, size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +020054 char_u *to, int last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020055
Bram Moolenaarc667da52019-11-30 20:52:27 +010056 // Function pointers for encoding and decoding, can buffer data if needed.
57 // Optional (however, these or the above should be configured).
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020058 long (*encode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +020059 char_u **newptr, int last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020060 long (*decode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +020061 char_u **newptr, int last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020062
Bram Moolenaarc667da52019-11-30 20:52:27 +010063 // Function pointers for in-place encoding and decoding, used for
64 // crypt_*_inplace(). "from" and "to" arguments will be equal.
65 // These may be the same as decode_fn and encode_fn above, however an
66 // algorithm may implement them in a way that is not interchangeable with
67 // the crypt_(en|de)code() interface (for example because it wishes to add
68 // padding to files).
69 // This method is used for swap and undo files which have a rigid format.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020070 void (*encode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +020071 char_u *p2, int last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020072 void (*decode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +020073 char_u *p2, int last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020074} cryptmethod_T;
75
Bram Moolenaarc667da52019-11-30 20:52:27 +010076// index is method_nr of cryptstate_T, CRYPT_M_*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020077static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = {
Bram Moolenaarc667da52019-11-30 20:52:27 +010078 // PK_Zip; very weak
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020079 {
80 "zip",
81 "VimCrypt~01!",
82 0,
83 0,
Bram Moolenaar987411d2019-01-18 22:48:34 +010084#ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020085 TRUE,
Bram Moolenaar987411d2019-01-18 22:48:34 +010086#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020087 FALSE,
88 NULL,
89 crypt_zip_init,
90 crypt_zip_encode, crypt_zip_decode,
91 NULL, NULL,
92 crypt_zip_encode, crypt_zip_decode,
93 },
94
Bram Moolenaarc667da52019-11-30 20:52:27 +010095 // Blowfish/CFB + SHA-256 custom key derivation; implementation issues.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020096 {
97 "blowfish",
98 "VimCrypt~02!",
99 8,
100 8,
Bram Moolenaar987411d2019-01-18 22:48:34 +0100101#ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200102 TRUE,
Bram Moolenaar987411d2019-01-18 22:48:34 +0100103#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200104 FALSE,
105 blowfish_self_test,
106 crypt_blowfish_init,
107 crypt_blowfish_encode, crypt_blowfish_decode,
108 NULL, NULL,
109 crypt_blowfish_encode, crypt_blowfish_decode,
110 },
111
Bram Moolenaarc667da52019-11-30 20:52:27 +0100112 // Blowfish/CFB + SHA-256 custom key derivation; fixed.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200113 {
114 "blowfish2",
115 "VimCrypt~03!",
116 8,
117 8,
Bram Moolenaar987411d2019-01-18 22:48:34 +0100118#ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200119 TRUE,
Bram Moolenaar987411d2019-01-18 22:48:34 +0100120#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200121 TRUE,
122 blowfish_self_test,
123 crypt_blowfish_init,
124 crypt_blowfish_encode, crypt_blowfish_decode,
125 NULL, NULL,
126 crypt_blowfish_encode, crypt_blowfish_decode,
127 },
Bram Moolenaard23a8232018-02-10 18:45:26 +0100128
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200129 // XChaCha20 using libsodium
130 {
131 "xchacha20",
132 "VimCrypt~04!",
133#ifdef FEAT_SODIUM
134 crypto_pwhash_argon2id_SALTBYTES, // 16
135#else
136 16,
137#endif
138 8,
139#ifdef CRYPT_NOT_INPLACE
140 FALSE,
141#endif
142 FALSE,
143 NULL,
144 crypt_sodium_init,
Christian Brabandt226b28b2021-06-21 21:08:08 +0200145 NULL, NULL,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200146 crypt_sodium_buffer_encode, crypt_sodium_buffer_decode,
Christian Brabandt226b28b2021-06-21 21:08:08 +0200147 NULL, NULL,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200148 },
149
Bram Moolenaarc667da52019-11-30 20:52:27 +0100150 // NOTE: when adding a new method, use some random bytes for the magic key,
151 // to avoid that a text file is recognized as encrypted.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200152};
153
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200154#ifdef FEAT_SODIUM
155typedef struct {
156 size_t count;
157 unsigned char key[crypto_box_SEEDBYTES];
158 // 32, same as crypto_secretstream_xchacha20poly1305_KEYBYTES
159 crypto_secretstream_xchacha20poly1305_state
160 state;
161} sodium_state_T;
162#endif
163
Bram Moolenaarc667da52019-11-30 20:52:27 +0100164#define CRYPT_MAGIC_LEN 12 // cannot change
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200165static char crypt_magic_head[] = "VimCrypt~";
166
167/*
168 * Return int value for crypt method name.
169 * 0 for "zip", the old method. Also for any non-valid value.
170 * 1 for "blowfish".
171 * 2 for "blowfish2".
172 */
173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100174crypt_method_nr_from_name(char_u *name)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200175{
176 int i;
177
178 for (i = 0; i < CRYPT_M_COUNT; ++i)
179 if (STRCMP(name, cryptmethods[i].name) == 0)
180 return i;
181 return 0;
182}
183
184/*
185 * Get the crypt method used for a file from "ptr[len]", the magic text at the
186 * start of the file.
187 * Returns -1 when no encryption used.
188 */
189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100190crypt_method_nr_from_magic(char *ptr, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200191{
192 int i;
193
194 if (len < CRYPT_MAGIC_LEN)
195 return -1;
196
197 for (i = 0; i < CRYPT_M_COUNT; i++)
198 if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0)
199 return i;
200
201 i = (int)STRLEN(crypt_magic_head);
202 if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000203 emsg(_(e_file_is_encrypted_with_unknown_method));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200204
205 return -1;
206}
207
Bram Moolenaar987411d2019-01-18 22:48:34 +0100208#ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200209/*
210 * Return TRUE if the crypt method for "method_nr" can be done in-place.
211 */
212 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100213crypt_works_inplace(cryptstate_T *state)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200214{
215 return cryptmethods[state->method_nr].works_inplace;
216}
Bram Moolenaar987411d2019-01-18 22:48:34 +0100217#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200218
219/*
220 * Get the crypt method for buffer "buf" as a number.
221 */
222 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100223crypt_get_method_nr(buf_T *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200224{
225 return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
226}
227
228/*
229 * Return TRUE when the buffer uses an encryption method that encrypts the
230 * whole undo file, not only the text.
231 */
232 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100233crypt_whole_undofile(int method_nr)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200234{
235 return cryptmethods[method_nr].whole_undofile;
236}
237
238/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100239 * Get crypt method specific length of the file header in bytes.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200240 */
241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100242crypt_get_header_len(int method_nr)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200243{
244 return CRYPT_MAGIC_LEN
245 + cryptmethods[method_nr].salt_len
246 + cryptmethods[method_nr].seed_len;
247}
248
Christian Brabandt226b28b2021-06-21 21:08:08 +0200249
Dominique Pelle748b3082022-01-08 12:41:16 +0000250#if defined(FEAT_SODIUM) || defined(PROTO)
Christian Brabandt226b28b2021-06-21 21:08:08 +0200251/*
252 * Get maximum crypt method specific length of the file header in bytes.
253 */
254 int
255crypt_get_max_header_len()
256{
257 int i;
258 int max = 0;
259 int temp = 0;
260
261 for (i = 0; i < CRYPT_M_COUNT; ++i)
262 {
263 temp = crypt_get_header_len(i);
264 if (temp > max)
265 max = temp;
266 }
267 return max;
268}
Dominique Pelle748b3082022-01-08 12:41:16 +0000269#endif
Christian Brabandt226b28b2021-06-21 21:08:08 +0200270
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200271/*
272 * Set the crypt method for buffer "buf" to "method_nr" using the int value as
273 * returned by crypt_method_nr_from_name().
274 */
275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100276crypt_set_cm_option(buf_T *buf, int method_nr)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200277{
278 free_string_option(buf->b_p_cm);
279 buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name);
280}
281
282/*
283 * If the crypt method for the current buffer has a self-test, run it and
284 * return OK/FAIL.
285 */
286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100287crypt_self_test(void)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200288{
289 int method_nr = crypt_get_method_nr(curbuf);
290
291 if (cryptmethods[method_nr].self_test_fn == NULL)
292 return OK;
293 return cryptmethods[method_nr].self_test_fn();
294}
295
296/*
297 * Allocate a crypt state and initialize it.
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200298 * Return NULL for failure.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200299 */
300 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100301crypt_create(
302 int method_nr,
303 char_u *key,
304 char_u *salt,
305 int salt_len,
306 char_u *seed,
307 int seed_len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200308{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200309 cryptstate_T *state = ALLOC_ONE(cryptstate_T);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200310
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200311 if (state == NULL)
312 return state;
313
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200314 state->method_nr = method_nr;
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200315 if (cryptmethods[method_nr].init_fn(
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200316 state, key, salt, salt_len, seed, seed_len) == FAIL)
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200317 {
318 vim_free(state);
319 return NULL;
320 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200321 return state;
322}
323
324/*
325 * Allocate a crypt state from a file header and initialize it.
326 * Assumes that header contains at least the number of bytes that
327 * crypt_get_header_len() returns for "method_nr".
328 */
329 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100330crypt_create_from_header(
331 int method_nr,
332 char_u *key,
333 char_u *header)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200334{
335 char_u *salt = NULL;
336 char_u *seed = NULL;
337 int salt_len = cryptmethods[method_nr].salt_len;
338 int seed_len = cryptmethods[method_nr].seed_len;
339
340 if (salt_len > 0)
341 salt = header + CRYPT_MAGIC_LEN;
342 if (seed_len > 0)
343 seed = header + CRYPT_MAGIC_LEN + salt_len;
344
345 return crypt_create(method_nr, key, salt, salt_len, seed, seed_len);
346}
347
348/*
349 * Read the crypt method specific header data from "fp".
350 * Return an allocated cryptstate_T or NULL on error.
351 */
352 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353crypt_create_from_file(FILE *fp, char_u *key)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200354{
355 int method_nr;
356 int header_len;
357 char magic_buffer[CRYPT_MAGIC_LEN];
358 char_u *buffer;
359 cryptstate_T *state;
360
361 if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1)
362 return NULL;
363 method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN);
364 if (method_nr < 0)
365 return NULL;
366
367 header_len = crypt_get_header_len(method_nr);
368 if ((buffer = alloc(header_len)) == NULL)
369 return NULL;
370 mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN);
371 if (header_len > CRYPT_MAGIC_LEN
372 && fread(buffer + CRYPT_MAGIC_LEN,
373 header_len - CRYPT_MAGIC_LEN, 1, fp) != 1)
374 {
375 vim_free(buffer);
376 return NULL;
377 }
378
379 state = crypt_create_from_header(method_nr, key, buffer);
380 vim_free(buffer);
381 return state;
382}
383
384/*
385 * Allocate a cryptstate_T for writing and initialize it with "key".
386 * Allocates and fills in the header and stores it in "header", setting
387 * "header_len". The header may include salt and seed, depending on
388 * cryptmethod. Caller must free header.
389 * Returns the state or NULL on failure.
390 */
391 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100392crypt_create_for_writing(
393 int method_nr,
394 char_u *key,
395 char_u **header,
396 int *header_len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200397{
398 int len = crypt_get_header_len(method_nr);
399 char_u *salt = NULL;
400 char_u *seed = NULL;
401 int salt_len = cryptmethods[method_nr].salt_len;
402 int seed_len = cryptmethods[method_nr].seed_len;
403 cryptstate_T *state;
404
405 *header_len = len;
406 *header = alloc(len);
407 if (*header == NULL)
408 return NULL;
409
410 mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN);
411 if (salt_len > 0 || seed_len > 0)
412 {
413 if (salt_len > 0)
414 salt = *header + CRYPT_MAGIC_LEN;
415 if (seed_len > 0)
416 seed = *header + CRYPT_MAGIC_LEN + salt_len;
417
Bram Moolenaarc667da52019-11-30 20:52:27 +0100418 // TODO: Should this be crypt method specific? (Probably not worth
419 // it). sha2_seed is pretty bad for large amounts of entropy, so make
420 // that into something which is suitable for anything.
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200421#ifdef FEAT_SODIUM
422 if (sodium_init() >= 0)
423 {
Christian Brabandt226b28b2021-06-21 21:08:08 +0200424 if (salt_len > 0)
425 randombytes_buf(salt, salt_len);
426 if (seed_len > 0)
427 randombytes_buf(seed, seed_len);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200428 }
429 else
430#endif
431 sha2_seed(salt, salt_len, seed, seed_len);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200432 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200433 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len);
434 if (state == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100435 VIM_CLEAR(*header);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200436 return state;
437}
438
439/*
440 * Free the crypt state.
441 */
442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100443crypt_free_state(cryptstate_T *state)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200444{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200445#ifdef FEAT_SODIUM
446 if (state->method_nr == CRYPT_M_SOD)
447 {
Bram Moolenaar131530a2021-07-29 20:37:49 +0200448 sodium_munlock(((sodium_state_T *)state->method_state)->key,
449 crypto_box_SEEDBYTES);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200450 sodium_memzero(state->method_state, sizeof(sodium_state_T));
451 sodium_free(state->method_state);
452 }
453 else
454#endif
455 vim_free(state->method_state);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200456 vim_free(state);
457}
458
Bram Moolenaar987411d2019-01-18 22:48:34 +0100459#ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200460/*
461 * Encode "from[len]" and store the result in a newly allocated buffer, which
462 * is stored in "newptr".
463 * Return number of bytes in "newptr", 0 for need more or -1 on error.
464 */
465 long
Bram Moolenaar7454a062016-01-30 15:14:10 +0100466crypt_encode_alloc(
467 cryptstate_T *state,
468 char_u *from,
469 size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200470 char_u **newptr,
471 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200472{
473 cryptmethod_T *method = &cryptmethods[state->method_nr];
474
475 if (method->encode_buffer_fn != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100476 // Has buffer function, pass through.
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200477 return method->encode_buffer_fn(state, from, len, newptr, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200478 if (len == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100479 // Not buffering, just return EOF.
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200480 return (long)len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200481
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200482 *newptr = alloc(len + 50);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200483 if (*newptr == NULL)
484 return -1;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200485 method->encode_fn(state, from, len, *newptr, last);
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200486 return (long)len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200487}
488
489/*
490 * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which
491 * is stored in "newptr".
492 * Return number of bytes in "newptr", 0 for need more or -1 on error.
493 */
494 long
Bram Moolenaar7454a062016-01-30 15:14:10 +0100495crypt_decode_alloc(
496 cryptstate_T *state,
497 char_u *ptr,
498 long len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200499 char_u **newptr,
500 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200501{
502 cryptmethod_T *method = &cryptmethods[state->method_nr];
503
504 if (method->decode_buffer_fn != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100505 // Has buffer function, pass through.
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200506 return method->decode_buffer_fn(state, ptr, len, newptr, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200507
508 if (len == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100509 // Not buffering, just return EOF.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200510 return len;
511
512 *newptr = alloc(len);
513 if (*newptr == NULL)
514 return -1;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200515 method->decode_fn(state, ptr, len, *newptr, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200516 return len;
517}
Bram Moolenaar987411d2019-01-18 22:48:34 +0100518#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200519
520/*
521 * Encrypting "from[len]" into "to[len]".
522 */
523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100524crypt_encode(
525 cryptstate_T *state,
526 char_u *from,
527 size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200528 char_u *to,
529 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200530{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200531 cryptmethods[state->method_nr].encode_fn(state, from, len, to, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200532}
533
Bram Moolenaar987411d2019-01-18 22:48:34 +0100534#if 0 // unused
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200535/*
536 * decrypting "from[len]" into "to[len]".
537 */
538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539crypt_decode(
540 cryptstate_T *state,
541 char_u *from,
542 size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200543 char_u *to,
544 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200545{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200546 cryptmethods[state->method_nr].decode_fn(state, from, len, to, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200547}
Bram Moolenaar987411d2019-01-18 22:48:34 +0100548#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200549
550/*
551 * Simple inplace encryption, modifies "buf[len]" in place.
552 */
553 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554crypt_encode_inplace(
555 cryptstate_T *state,
556 char_u *buf,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200557 size_t len,
558 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200559{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200560 cryptmethods[state->method_nr].encode_inplace_fn(state, buf, len,
561 buf, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200562}
563
564/*
565 * Simple inplace decryption, modifies "buf[len]" in place.
566 */
567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100568crypt_decode_inplace(
569 cryptstate_T *state,
570 char_u *buf,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200571 size_t len,
572 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200573{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200574 cryptmethods[state->method_nr].decode_inplace_fn(state, buf, len,
575 buf, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200576}
577
578/*
579 * Free an allocated crypt key. Clear the text to make sure it doesn't stay
580 * in memory anywhere.
581 */
582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100583crypt_free_key(char_u *key)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200584{
585 char_u *p;
586
587 if (key != NULL)
588 {
589 for (p = key; *p != NUL; ++p)
590 *p = 0;
591 vim_free(key);
592 }
593}
594
595/*
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100596 * Check the crypt method and give a warning if it's outdated.
597 */
598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100599crypt_check_method(int method)
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100600{
601 if (method < CRYPT_M_BF2)
602 {
603 msg_scroll = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100604 msg(_("Warning: Using a weak encryption method; see :help 'cm'"));
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100605 }
Christian Brabandt226b28b2021-06-21 21:08:08 +0200606}
607
608#ifdef FEAT_SODIUM
609 static void
610crypt_check_swapfile_curbuf(void)
611{
612 int method = crypt_get_method_nr(curbuf);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200613 if (method == CRYPT_M_SOD)
614 {
615 // encryption uses padding and MAC, that does not work very well with
616 // swap and undo files, so disable them
617 mf_close_file(curbuf, TRUE); // remove the swap file
618 set_option_value((char_u *)"swf", 0, NULL, OPT_LOCAL);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200619 msg_scroll = TRUE;
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200620 msg(_("Note: Encryption of swapfile not supported, disabling swap file"));
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200621 }
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100622}
Christian Brabandt226b28b2021-06-21 21:08:08 +0200623#endif
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100624
625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100626crypt_check_current_method(void)
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100627{
628 crypt_check_method(crypt_get_method_nr(curbuf));
629}
630
631/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200632 * Ask the user for a crypt key.
633 * When "store" is TRUE, the new key is stored in the 'key' option, and the
634 * 'key' option value is returned: Don't free it.
635 * When "store" is FALSE, the typed key is returned in allocated memory.
636 * Returns NULL on failure.
637 */
638 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100639crypt_get_key(
640 int store,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100641 int twice) // Ask for the key twice.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200642{
643 char_u *p1, *p2 = NULL;
644 int round;
645
646 for (round = 0; ; ++round)
647 {
648 cmdline_star = TRUE;
649 cmdline_row = msg_row;
650 p1 = getcmdline_prompt(NUL, round == 0
651 ? (char_u *)_("Enter encryption key: ")
652 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING,
653 NULL);
654 cmdline_star = FALSE;
655
656 if (p1 == NULL)
657 break;
658
659 if (round == twice)
660 {
661 if (p2 != NULL && STRCMP(p1, p2) != 0)
662 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100663 msg(_("Keys don't match!"));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200664 crypt_free_key(p1);
665 crypt_free_key(p2);
666 p2 = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100667 round = -1; // do it again
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200668 continue;
669 }
670
671 if (store)
672 {
673 set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL);
674 crypt_free_key(p1);
675 p1 = curbuf->b_p_key;
Christian Brabandt226b28b2021-06-21 21:08:08 +0200676#ifdef FEAT_SODIUM
677 crypt_check_swapfile_curbuf();
678#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200679 }
680 break;
681 }
682 p2 = p1;
683 }
684
Bram Moolenaarc667da52019-11-30 20:52:27 +0100685 // since the user typed this, no need to wait for return
Christian Brabandt226b28b2021-06-21 21:08:08 +0200686 if (crypt_get_method_nr(curbuf) != CRYPT_M_SOD)
687 {
688 if (msg_didout)
689 msg_putchar('\n');
690 need_wait_return = FALSE;
691 msg_didout = FALSE;
692 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200693
694 crypt_free_key(p2);
695 return p1;
696}
697
698
699/*
700 * Append a message to IObuff for the encryption/decryption method being used.
701 */
702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703crypt_append_msg(
704 buf_T *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200705{
706 if (crypt_get_method_nr(buf) == 0)
707 STRCAT(IObuff, _("[crypted]"));
708 else
709 {
710 STRCAT(IObuff, "[");
711 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
712 STRCAT(IObuff, "]");
713 }
714}
715
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200716 int
717crypt_sodium_init(
718 cryptstate_T *state UNUSED,
719 char_u *key UNUSED,
720 char_u *salt UNUSED,
721 int salt_len UNUSED,
722 char_u *seed UNUSED,
723 int seed_len UNUSED)
724{
725# ifdef FEAT_SODIUM
726 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
727 unsigned char dkey[crypto_box_SEEDBYTES]; // 32
728 sodium_state_T *sd_state;
Bram Moolenaar131530a2021-07-29 20:37:49 +0200729 int retval = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200730
731 if (sodium_init() < 0)
732 return FAIL;
733
734 sd_state = (sodium_state_T *)sodium_malloc(sizeof(sodium_state_T));
735 sodium_memzero(sd_state, sizeof(sodium_state_T));
736
737 // derive a key from the password
738 if (crypto_pwhash(dkey, sizeof(dkey), (const char *)key, STRLEN(key), salt,
739 crypto_pwhash_OPSLIMIT_INTERACTIVE, crypto_pwhash_MEMLIMIT_INTERACTIVE,
740 crypto_pwhash_ALG_DEFAULT) != 0)
741 {
742 // out of memory
743 sodium_free(sd_state);
744 return FAIL;
745 }
746 memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES);
Bram Moolenaar131530a2021-07-29 20:37:49 +0200747
748 retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES);
749 retval += sodium_mlock(key, STRLEN(key));
750
751 if (retval < 0)
752 {
753 emsg(_(e_encryption_sodium_mlock_failed));
754 sodium_free(sd_state);
755 return FAIL;
756 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200757 sd_state->count = 0;
758 state->method_state = sd_state;
759
760 return OK;
761# else
762 emsg(e_libsodium_not_built_in);
763 return FAIL;
764# endif
765}
766
767/*
768 * Encrypt "from[len]" into "to[len]".
769 * "from" and "to" can be equal to encrypt in place.
770 * Call needs to ensure that there is enough space in to (for the header)
771 */
Christian Brabandt226b28b2021-06-21 21:08:08 +0200772#if 0 // Currently unused
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200773 void
774crypt_sodium_encode(
775 cryptstate_T *state UNUSED,
776 char_u *from UNUSED,
777 size_t len UNUSED,
778 char_u *to UNUSED,
779 int last UNUSED)
780{
781# ifdef FEAT_SODIUM
782 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
783 sodium_state_T *sod_st = state->method_state;
784 unsigned char tag = last
785 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
786
787 if (sod_st->count == 0)
788 {
789 if (len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES)
790 {
791 emsg(e_libsodium_cannot_encrypt_header);
792 return;
793 }
794 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state,
795 to, sod_st->key);
796 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
797 }
798
799 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES)
800 {
801 emsg(e_libsodium_cannot_encrypt_buffer);
802 return;
803 }
804
805 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, to, NULL,
806 from, len, NULL, 0, tag);
807
808 sod_st->count++;
809# endif
810}
Christian Brabandt226b28b2021-06-21 21:08:08 +0200811#endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200812
Christian Brabandt226b28b2021-06-21 21:08:08 +0200813/*
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200814 * Decrypt "from[len]" into "to[len]".
815 * "from" and "to" can be equal to encrypt in place.
816 */
Christian Brabandt226b28b2021-06-21 21:08:08 +0200817#if 0 // Currently unused
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200818 void
819crypt_sodium_decode(
820 cryptstate_T *state UNUSED,
821 char_u *from UNUSED,
822 size_t len UNUSED,
823 char_u *to UNUSED,
824 int last UNUSED)
825{
826# ifdef FEAT_SODIUM
827 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
828 sodium_state_T *sod_st = state->method_state;
829 unsigned char tag;
830 unsigned long long buf_len;
831 char_u *p1 = from;
832 char_u *p2 = to;
833 char_u *buf_out;
834
835 if (sod_st->count == 0
836 && len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES)
837 {
838 emsg(e_libsodium_cannot_decrypt_header);
839 return;
840 }
841
842 buf_out = (char_u *)alloc(len);
843
844 if (buf_out == NULL)
845 {
846 emsg(e_libsodium_cannot_allocate_buffer);
847 return;
848 }
849 if (sod_st->count == 0)
850 {
851 if (crypto_secretstream_xchacha20poly1305_init_pull(
852 &sod_st->state, from, sod_st->key) != 0)
853 {
854 emsg(e_libsodium_decryption_failed_header_incomplete);
855 goto fail;
856 }
857
858 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
859 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES;
860
861 if (p1 == p2)
862 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
863 }
864
865 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES)
866 {
867 emsg(e_libsodium_cannot_decrypt_buffer);
Dominique Pellecb54bc62021-06-21 20:15:37 +0200868 goto fail;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200869 }
870 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state,
871 buf_out, &buf_len, &tag, from, len, NULL, 0) != 0)
872 {
Dominique Pellecb54bc62021-06-21 20:15:37 +0200873 emsg(e_libsodium_decryption_failed);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200874 goto fail;
875 }
876 sod_st->count++;
877
878 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last)
879 {
Dominique Pellecb54bc62021-06-21 20:15:37 +0200880 emsg(e_libsodium_decryption_failed_premature);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200881 goto fail;
882 }
883 if (p1 == p2)
884 mch_memmove(p2, buf_out, buf_len);
885
886fail:
887 vim_free(buf_out);
888# endif
889}
Christian Brabandt226b28b2021-06-21 21:08:08 +0200890#endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200891
892/*
893 * Encrypt "from[len]" into "to[len]".
894 * "from" and "to" can be equal to encrypt in place.
895 */
896 long
897crypt_sodium_buffer_encode(
898 cryptstate_T *state UNUSED,
899 char_u *from UNUSED,
900 size_t len UNUSED,
901 char_u **buf_out UNUSED,
902 int last UNUSED)
903{
904# ifdef FEAT_SODIUM
905 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
906 unsigned long long out_len;
907 char_u *ptr;
908 unsigned char tag = last
909 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
910 int length;
911 sodium_state_T *sod_st = state->method_state;
912 int first = (sod_st->count == 0);
913
Christian Brabandt226b28b2021-06-21 21:08:08 +0200914 length = (int)len + crypto_secretstream_xchacha20poly1305_ABYTES
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200915 + (first ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0);
916 *buf_out = alloc_clear(length);
917 if (*buf_out == NULL)
918 {
919 emsg(e_libsodium_cannot_allocate_buffer);
920 return -1;
921 }
922 ptr = *buf_out;
923
924 if (first)
925 {
926 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state,
927 ptr, sod_st->key);
928 ptr += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
929 }
930
931 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, ptr,
932 &out_len, from, len, NULL, 0, tag);
933
934 sod_st->count++;
935 return out_len + (first
936 ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0);
937# else
938 return -1;
939# endif
940}
941
942/*
943 * Decrypt "from[len]" into "to[len]".
944 * "from" and "to" can be equal to encrypt in place.
945 */
946 long
947crypt_sodium_buffer_decode(
948 cryptstate_T *state UNUSED,
949 char_u *from UNUSED,
950 size_t len UNUSED,
951 char_u **buf_out UNUSED,
952 int last UNUSED)
953{
954# ifdef FEAT_SODIUM
955 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
956 sodium_state_T *sod_st = state->method_state;
957 unsigned char tag;
958 unsigned long long out_len;
959 *buf_out = alloc_clear(len);
960 if (*buf_out == NULL)
961 {
962 emsg(e_libsodium_cannot_allocate_buffer);
963 return -1;
964 }
965
966 if (sod_st->count == 0)
967 {
968 if (crypto_secretstream_xchacha20poly1305_init_pull(&sod_st->state,
969 from, sod_st->key) != 0)
970 {
971 emsg(e_libsodium_decryption_failed_header_incomplete);
972 return -1;
973 }
974 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
975 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES;
976 sod_st->count++;
977 }
978 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state,
979 *buf_out, &out_len, &tag, from, len, NULL, 0) != 0)
980 {
Dominique Pellecb54bc62021-06-21 20:15:37 +0200981 emsg(e_libsodium_decryption_failed);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200982 return -1;
983 }
984
985 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last)
Dominique Pellecb54bc62021-06-21 20:15:37 +0200986 emsg(e_libsodium_decryption_failed_premature);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200987 return (long) out_len;
988# else
989 return -1;
990# endif
991}
992
Bram Moolenaarc667da52019-11-30 20:52:27 +0100993#endif // FEAT_CRYPT