blob: e5d0d9167b1a1af879652b16b3da6e3cf7c93ce6 [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;
K.Takata1a8825d2022-01-19 13:32:57 +0000162
163
164# ifdef DYNAMIC_SODIUM
165# define sodium_init load_sodium
166# define sodium_free dll_sodium_free
167# define sodium_malloc dll_sodium_malloc
168# define sodium_memzero dll_sodium_memzero
169# define sodium_mlock dll_sodium_mlock
170# define sodium_munlock dll_sodium_munlock
171# define crypto_secretstream_xchacha20poly1305_init_push \
172 dll_crypto_secretstream_xchacha20poly1305_init_push
173# define crypto_secretstream_xchacha20poly1305_push \
174 dll_crypto_secretstream_xchacha20poly1305_push
175# define crypto_secretstream_xchacha20poly1305_init_pull \
176 dll_crypto_secretstream_xchacha20poly1305_init_pull
177# define crypto_secretstream_xchacha20poly1305_pull \
178 dll_crypto_secretstream_xchacha20poly1305_pull
179# define crypto_pwhash dll_crypto_pwhash
180# define randombytes_buf dll_randombytes_buf
181
182static int (*dll_sodium_init)(void) = NULL;
183static void (*dll_sodium_free)(void *) = NULL;
184static void *(*dll_sodium_malloc)(const size_t) = NULL;
185static void (*dll_sodium_memzero)(void * const, const size_t) = NULL;
186static int (*dll_sodium_mlock)(void * const, const size_t) = NULL;
187static int (*dll_sodium_munlock)(void * const, const size_t) = NULL;
188static int (*dll_crypto_secretstream_xchacha20poly1305_init_push)
189 (crypto_secretstream_xchacha20poly1305_state *state,
190 unsigned char [],
191 const unsigned char []) = NULL;
192static int (*dll_crypto_secretstream_xchacha20poly1305_push)
193 (crypto_secretstream_xchacha20poly1305_state *state,
194 unsigned char *c, unsigned long long *clen_p,
195 const unsigned char *m, unsigned long long mlen,
196 const unsigned char *ad, unsigned long long adlen, unsigned char tag)
197 = NULL;
198static int (*dll_crypto_secretstream_xchacha20poly1305_init_pull)
199 (crypto_secretstream_xchacha20poly1305_state *state,
200 const unsigned char [],
201 const unsigned char []) = NULL;
202static int (*dll_crypto_secretstream_xchacha20poly1305_pull)
203 (crypto_secretstream_xchacha20poly1305_state *state,
204 unsigned char *m, unsigned long long *mlen_p, unsigned char *tag_p,
205 const unsigned char *c, unsigned long long clen,
206 const unsigned char *ad, unsigned long long adlen) = NULL;
207static int (*dll_crypto_pwhash)(unsigned char * const out,
208 unsigned long long outlen,
209 const char * const passwd, unsigned long long passwdlen,
210 const unsigned char * const salt,
211 unsigned long long opslimit, size_t memlimit, int alg)
212 = NULL;
213static void (*dll_randombytes_buf)(void * const buf, const size_t size);
214
215static struct {
216 const char *name;
217 FARPROC *ptr;
218} sodium_funcname_table[] = {
219 {"sodium_init", (FARPROC*)&dll_sodium_init},
220 {"sodium_free", (FARPROC*)&dll_sodium_free},
221 {"sodium_malloc", (FARPROC*)&dll_sodium_malloc},
222 {"sodium_memzero", (FARPROC*)&dll_sodium_memzero},
223 {"sodium_mlock", (FARPROC*)&dll_sodium_mlock},
224 {"sodium_munlock", (FARPROC*)&dll_sodium_munlock},
225 {"crypto_secretstream_xchacha20poly1305_init_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push},
226 {"crypto_secretstream_xchacha20poly1305_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_push},
227 {"crypto_secretstream_xchacha20poly1305_init_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull},
228 {"crypto_secretstream_xchacha20poly1305_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_pull},
229 {"crypto_pwhash", (FARPROC*)&dll_crypto_pwhash},
230 {"randombytes_buf", (FARPROC*)&dll_randombytes_buf},
231 {NULL, NULL}
232};
233
234 static int
235load_sodium(void)
236{
237 static HANDLE hsodium = NULL;
238 int i;
239
240 if (hsodium != NULL)
241 return 0;
242
243 hsodium = vimLoadLib("libsodium.dll");
244 if (hsodium == NULL)
245 {
246 // TODO: Show error message.
247 return -1;
248 }
249
250 for (i = 0; sodium_funcname_table[i].ptr; ++i)
251 {
252 if ((*sodium_funcname_table[i].ptr = GetProcAddress(hsodium,
253 sodium_funcname_table[i].name)) == NULL)
254 {
255 FreeLibrary(hsodium);
256 hsodium = NULL;
257 // TODO: Show error message.
258 return -1;
259 }
260 }
261 return dll_sodium_init();
262}
263# endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200264#endif
265
Bram Moolenaarc667da52019-11-30 20:52:27 +0100266#define CRYPT_MAGIC_LEN 12 // cannot change
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200267static char crypt_magic_head[] = "VimCrypt~";
268
269/*
270 * Return int value for crypt method name.
271 * 0 for "zip", the old method. Also for any non-valid value.
272 * 1 for "blowfish".
273 * 2 for "blowfish2".
274 */
275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100276crypt_method_nr_from_name(char_u *name)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200277{
278 int i;
279
280 for (i = 0; i < CRYPT_M_COUNT; ++i)
281 if (STRCMP(name, cryptmethods[i].name) == 0)
282 return i;
283 return 0;
284}
285
286/*
287 * Get the crypt method used for a file from "ptr[len]", the magic text at the
288 * start of the file.
289 * Returns -1 when no encryption used.
290 */
291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100292crypt_method_nr_from_magic(char *ptr, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200293{
294 int i;
295
296 if (len < CRYPT_MAGIC_LEN)
297 return -1;
298
299 for (i = 0; i < CRYPT_M_COUNT; i++)
300 if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0)
301 return i;
302
303 i = (int)STRLEN(crypt_magic_head);
304 if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000305 emsg(_(e_file_is_encrypted_with_unknown_method));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200306
307 return -1;
308}
309
Bram Moolenaar987411d2019-01-18 22:48:34 +0100310#ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200311/*
312 * Return TRUE if the crypt method for "method_nr" can be done in-place.
313 */
314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100315crypt_works_inplace(cryptstate_T *state)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200316{
317 return cryptmethods[state->method_nr].works_inplace;
318}
Bram Moolenaar987411d2019-01-18 22:48:34 +0100319#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200320
321/*
322 * Get the crypt method for buffer "buf" as a number.
323 */
324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100325crypt_get_method_nr(buf_T *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200326{
327 return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
328}
329
330/*
331 * Return TRUE when the buffer uses an encryption method that encrypts the
332 * whole undo file, not only the text.
333 */
334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100335crypt_whole_undofile(int method_nr)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200336{
337 return cryptmethods[method_nr].whole_undofile;
338}
339
340/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100341 * Get crypt method specific length of the file header in bytes.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200342 */
343 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100344crypt_get_header_len(int method_nr)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200345{
346 return CRYPT_MAGIC_LEN
347 + cryptmethods[method_nr].salt_len
348 + cryptmethods[method_nr].seed_len;
349}
350
Christian Brabandt226b28b2021-06-21 21:08:08 +0200351
Dominique Pelle748b3082022-01-08 12:41:16 +0000352#if defined(FEAT_SODIUM) || defined(PROTO)
Christian Brabandt226b28b2021-06-21 21:08:08 +0200353/*
354 * Get maximum crypt method specific length of the file header in bytes.
355 */
356 int
357crypt_get_max_header_len()
358{
359 int i;
360 int max = 0;
361 int temp = 0;
362
363 for (i = 0; i < CRYPT_M_COUNT; ++i)
364 {
365 temp = crypt_get_header_len(i);
366 if (temp > max)
367 max = temp;
368 }
369 return max;
370}
Dominique Pelle748b3082022-01-08 12:41:16 +0000371#endif
Christian Brabandt226b28b2021-06-21 21:08:08 +0200372
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200373/*
374 * Set the crypt method for buffer "buf" to "method_nr" using the int value as
375 * returned by crypt_method_nr_from_name().
376 */
377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100378crypt_set_cm_option(buf_T *buf, int method_nr)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200379{
380 free_string_option(buf->b_p_cm);
381 buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name);
382}
383
384/*
385 * If the crypt method for the current buffer has a self-test, run it and
386 * return OK/FAIL.
387 */
388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100389crypt_self_test(void)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200390{
391 int method_nr = crypt_get_method_nr(curbuf);
392
393 if (cryptmethods[method_nr].self_test_fn == NULL)
394 return OK;
395 return cryptmethods[method_nr].self_test_fn();
396}
397
398/*
399 * Allocate a crypt state and initialize it.
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200400 * Return NULL for failure.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200401 */
402 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100403crypt_create(
404 int method_nr,
405 char_u *key,
406 char_u *salt,
407 int salt_len,
408 char_u *seed,
409 int seed_len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200410{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200411 cryptstate_T *state = ALLOC_ONE(cryptstate_T);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200412
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200413 if (state == NULL)
414 return state;
415
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200416 state->method_nr = method_nr;
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200417 if (cryptmethods[method_nr].init_fn(
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200418 state, key, salt, salt_len, seed, seed_len) == FAIL)
Bram Moolenaar6ee96582019-04-27 22:06:37 +0200419 {
420 vim_free(state);
421 return NULL;
422 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200423 return state;
424}
425
426/*
427 * Allocate a crypt state from a file header and initialize it.
428 * Assumes that header contains at least the number of bytes that
429 * crypt_get_header_len() returns for "method_nr".
430 */
431 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100432crypt_create_from_header(
433 int method_nr,
434 char_u *key,
435 char_u *header)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200436{
437 char_u *salt = NULL;
438 char_u *seed = NULL;
439 int salt_len = cryptmethods[method_nr].salt_len;
440 int seed_len = cryptmethods[method_nr].seed_len;
441
442 if (salt_len > 0)
443 salt = header + CRYPT_MAGIC_LEN;
444 if (seed_len > 0)
445 seed = header + CRYPT_MAGIC_LEN + salt_len;
446
447 return crypt_create(method_nr, key, salt, salt_len, seed, seed_len);
448}
449
450/*
451 * Read the crypt method specific header data from "fp".
452 * Return an allocated cryptstate_T or NULL on error.
453 */
454 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100455crypt_create_from_file(FILE *fp, char_u *key)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200456{
457 int method_nr;
458 int header_len;
459 char magic_buffer[CRYPT_MAGIC_LEN];
460 char_u *buffer;
461 cryptstate_T *state;
462
463 if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1)
464 return NULL;
465 method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN);
466 if (method_nr < 0)
467 return NULL;
468
469 header_len = crypt_get_header_len(method_nr);
470 if ((buffer = alloc(header_len)) == NULL)
471 return NULL;
472 mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN);
473 if (header_len > CRYPT_MAGIC_LEN
474 && fread(buffer + CRYPT_MAGIC_LEN,
475 header_len - CRYPT_MAGIC_LEN, 1, fp) != 1)
476 {
477 vim_free(buffer);
478 return NULL;
479 }
480
481 state = crypt_create_from_header(method_nr, key, buffer);
482 vim_free(buffer);
483 return state;
484}
485
486/*
487 * Allocate a cryptstate_T for writing and initialize it with "key".
488 * Allocates and fills in the header and stores it in "header", setting
489 * "header_len". The header may include salt and seed, depending on
490 * cryptmethod. Caller must free header.
491 * Returns the state or NULL on failure.
492 */
493 cryptstate_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100494crypt_create_for_writing(
495 int method_nr,
496 char_u *key,
497 char_u **header,
498 int *header_len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200499{
500 int len = crypt_get_header_len(method_nr);
501 char_u *salt = NULL;
502 char_u *seed = NULL;
503 int salt_len = cryptmethods[method_nr].salt_len;
504 int seed_len = cryptmethods[method_nr].seed_len;
505 cryptstate_T *state;
506
507 *header_len = len;
508 *header = alloc(len);
509 if (*header == NULL)
510 return NULL;
511
512 mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN);
513 if (salt_len > 0 || seed_len > 0)
514 {
515 if (salt_len > 0)
516 salt = *header + CRYPT_MAGIC_LEN;
517 if (seed_len > 0)
518 seed = *header + CRYPT_MAGIC_LEN + salt_len;
519
Bram Moolenaarc667da52019-11-30 20:52:27 +0100520 // TODO: Should this be crypt method specific? (Probably not worth
521 // it). sha2_seed is pretty bad for large amounts of entropy, so make
522 // that into something which is suitable for anything.
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200523#ifdef FEAT_SODIUM
524 if (sodium_init() >= 0)
525 {
Christian Brabandt226b28b2021-06-21 21:08:08 +0200526 if (salt_len > 0)
527 randombytes_buf(salt, salt_len);
528 if (seed_len > 0)
529 randombytes_buf(seed, seed_len);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200530 }
531 else
532#endif
533 sha2_seed(salt, salt_len, seed, seed_len);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200534 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200535 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len);
536 if (state == NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100537 VIM_CLEAR(*header);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200538 return state;
539}
540
541/*
542 * Free the crypt state.
543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545crypt_free_state(cryptstate_T *state)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200546{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200547#ifdef FEAT_SODIUM
548 if (state->method_nr == CRYPT_M_SOD)
549 {
Bram Moolenaar131530a2021-07-29 20:37:49 +0200550 sodium_munlock(((sodium_state_T *)state->method_state)->key,
551 crypto_box_SEEDBYTES);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200552 sodium_memzero(state->method_state, sizeof(sodium_state_T));
553 sodium_free(state->method_state);
554 }
555 else
556#endif
557 vim_free(state->method_state);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200558 vim_free(state);
559}
560
Bram Moolenaar987411d2019-01-18 22:48:34 +0100561#ifdef CRYPT_NOT_INPLACE
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200562/*
563 * Encode "from[len]" and store the result in a newly allocated buffer, which
564 * is stored in "newptr".
565 * Return number of bytes in "newptr", 0 for need more or -1 on error.
566 */
567 long
Bram Moolenaar7454a062016-01-30 15:14:10 +0100568crypt_encode_alloc(
569 cryptstate_T *state,
570 char_u *from,
571 size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200572 char_u **newptr,
573 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200574{
575 cryptmethod_T *method = &cryptmethods[state->method_nr];
576
577 if (method->encode_buffer_fn != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100578 // Has buffer function, pass through.
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200579 return method->encode_buffer_fn(state, from, len, newptr, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200580 if (len == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100581 // Not buffering, just return EOF.
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200582 return (long)len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200583
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200584 *newptr = alloc(len + 50);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200585 if (*newptr == NULL)
586 return -1;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200587 method->encode_fn(state, from, len, *newptr, last);
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200588 return (long)len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200589}
590
591/*
592 * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which
593 * is stored in "newptr".
594 * Return number of bytes in "newptr", 0 for need more or -1 on error.
595 */
596 long
Bram Moolenaar7454a062016-01-30 15:14:10 +0100597crypt_decode_alloc(
598 cryptstate_T *state,
599 char_u *ptr,
600 long len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200601 char_u **newptr,
602 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200603{
604 cryptmethod_T *method = &cryptmethods[state->method_nr];
605
606 if (method->decode_buffer_fn != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100607 // Has buffer function, pass through.
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200608 return method->decode_buffer_fn(state, ptr, len, newptr, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200609
610 if (len == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100611 // Not buffering, just return EOF.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200612 return len;
613
614 *newptr = alloc(len);
615 if (*newptr == NULL)
616 return -1;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200617 method->decode_fn(state, ptr, len, *newptr, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200618 return len;
619}
Bram Moolenaar987411d2019-01-18 22:48:34 +0100620#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200621
622/*
623 * Encrypting "from[len]" into "to[len]".
624 */
625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100626crypt_encode(
627 cryptstate_T *state,
628 char_u *from,
629 size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200630 char_u *to,
631 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200632{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200633 cryptmethods[state->method_nr].encode_fn(state, from, len, to, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200634}
635
Bram Moolenaar987411d2019-01-18 22:48:34 +0100636#if 0 // unused
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200637/*
638 * decrypting "from[len]" into "to[len]".
639 */
640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641crypt_decode(
642 cryptstate_T *state,
643 char_u *from,
644 size_t len,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200645 char_u *to,
646 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200647{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200648 cryptmethods[state->method_nr].decode_fn(state, from, len, to, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200649}
Bram Moolenaar987411d2019-01-18 22:48:34 +0100650#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200651
652/*
653 * Simple inplace encryption, modifies "buf[len]" in place.
654 */
655 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100656crypt_encode_inplace(
657 cryptstate_T *state,
658 char_u *buf,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200659 size_t len,
660 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200661{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200662 cryptmethods[state->method_nr].encode_inplace_fn(state, buf, len,
663 buf, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200664}
665
666/*
667 * Simple inplace decryption, modifies "buf[len]" in place.
668 */
669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100670crypt_decode_inplace(
671 cryptstate_T *state,
672 char_u *buf,
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200673 size_t len,
674 int last)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200675{
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200676 cryptmethods[state->method_nr].decode_inplace_fn(state, buf, len,
677 buf, last);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200678}
679
680/*
681 * Free an allocated crypt key. Clear the text to make sure it doesn't stay
682 * in memory anywhere.
683 */
684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100685crypt_free_key(char_u *key)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200686{
687 char_u *p;
688
689 if (key != NULL)
690 {
691 for (p = key; *p != NUL; ++p)
692 *p = 0;
693 vim_free(key);
694 }
695}
696
697/*
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100698 * Check the crypt method and give a warning if it's outdated.
699 */
700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701crypt_check_method(int method)
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100702{
703 if (method < CRYPT_M_BF2)
704 {
705 msg_scroll = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100706 msg(_("Warning: Using a weak encryption method; see :help 'cm'"));
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100707 }
Christian Brabandt226b28b2021-06-21 21:08:08 +0200708}
709
710#ifdef FEAT_SODIUM
711 static void
712crypt_check_swapfile_curbuf(void)
713{
714 int method = crypt_get_method_nr(curbuf);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200715 if (method == CRYPT_M_SOD)
716 {
717 // encryption uses padding and MAC, that does not work very well with
718 // swap and undo files, so disable them
719 mf_close_file(curbuf, TRUE); // remove the swap file
720 set_option_value((char_u *)"swf", 0, NULL, OPT_LOCAL);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200721 msg_scroll = TRUE;
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200722 msg(_("Note: Encryption of swapfile not supported, disabling swap file"));
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200723 }
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100724}
Christian Brabandt226b28b2021-06-21 21:08:08 +0200725#endif
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100726
727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100728crypt_check_current_method(void)
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100729{
730 crypt_check_method(crypt_get_method_nr(curbuf));
731}
732
733/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200734 * Ask the user for a crypt key.
735 * When "store" is TRUE, the new key is stored in the 'key' option, and the
736 * 'key' option value is returned: Don't free it.
737 * When "store" is FALSE, the typed key is returned in allocated memory.
738 * Returns NULL on failure.
739 */
740 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100741crypt_get_key(
742 int store,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100743 int twice) // Ask for the key twice.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200744{
745 char_u *p1, *p2 = NULL;
746 int round;
747
748 for (round = 0; ; ++round)
749 {
750 cmdline_star = TRUE;
751 cmdline_row = msg_row;
752 p1 = getcmdline_prompt(NUL, round == 0
753 ? (char_u *)_("Enter encryption key: ")
754 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING,
755 NULL);
756 cmdline_star = FALSE;
757
758 if (p1 == NULL)
759 break;
760
761 if (round == twice)
762 {
763 if (p2 != NULL && STRCMP(p1, p2) != 0)
764 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100765 msg(_("Keys don't match!"));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200766 crypt_free_key(p1);
767 crypt_free_key(p2);
768 p2 = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100769 round = -1; // do it again
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200770 continue;
771 }
772
773 if (store)
774 {
775 set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL);
776 crypt_free_key(p1);
777 p1 = curbuf->b_p_key;
Christian Brabandt226b28b2021-06-21 21:08:08 +0200778#ifdef FEAT_SODIUM
779 crypt_check_swapfile_curbuf();
780#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200781 }
782 break;
783 }
784 p2 = p1;
785 }
786
Bram Moolenaarc667da52019-11-30 20:52:27 +0100787 // since the user typed this, no need to wait for return
Christian Brabandt226b28b2021-06-21 21:08:08 +0200788 if (crypt_get_method_nr(curbuf) != CRYPT_M_SOD)
789 {
790 if (msg_didout)
791 msg_putchar('\n');
792 need_wait_return = FALSE;
793 msg_didout = FALSE;
794 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200795
796 crypt_free_key(p2);
797 return p1;
798}
799
800
801/*
802 * Append a message to IObuff for the encryption/decryption method being used.
803 */
804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100805crypt_append_msg(
806 buf_T *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200807{
808 if (crypt_get_method_nr(buf) == 0)
809 STRCAT(IObuff, _("[crypted]"));
810 else
811 {
812 STRCAT(IObuff, "[");
813 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
814 STRCAT(IObuff, "]");
815 }
816}
817
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200818 int
819crypt_sodium_init(
820 cryptstate_T *state UNUSED,
821 char_u *key UNUSED,
822 char_u *salt UNUSED,
823 int salt_len UNUSED,
824 char_u *seed UNUSED,
825 int seed_len UNUSED)
826{
827# ifdef FEAT_SODIUM
828 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
829 unsigned char dkey[crypto_box_SEEDBYTES]; // 32
830 sodium_state_T *sd_state;
Bram Moolenaar131530a2021-07-29 20:37:49 +0200831 int retval = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200832
833 if (sodium_init() < 0)
834 return FAIL;
835
836 sd_state = (sodium_state_T *)sodium_malloc(sizeof(sodium_state_T));
837 sodium_memzero(sd_state, sizeof(sodium_state_T));
838
839 // derive a key from the password
840 if (crypto_pwhash(dkey, sizeof(dkey), (const char *)key, STRLEN(key), salt,
841 crypto_pwhash_OPSLIMIT_INTERACTIVE, crypto_pwhash_MEMLIMIT_INTERACTIVE,
842 crypto_pwhash_ALG_DEFAULT) != 0)
843 {
844 // out of memory
845 sodium_free(sd_state);
846 return FAIL;
847 }
848 memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES);
Bram Moolenaar131530a2021-07-29 20:37:49 +0200849
850 retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES);
851 retval += sodium_mlock(key, STRLEN(key));
852
853 if (retval < 0)
854 {
855 emsg(_(e_encryption_sodium_mlock_failed));
856 sodium_free(sd_state);
857 return FAIL;
858 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200859 sd_state->count = 0;
860 state->method_state = sd_state;
861
862 return OK;
863# else
864 emsg(e_libsodium_not_built_in);
865 return FAIL;
866# endif
867}
868
869/*
870 * Encrypt "from[len]" into "to[len]".
871 * "from" and "to" can be equal to encrypt in place.
872 * Call needs to ensure that there is enough space in to (for the header)
873 */
Christian Brabandt226b28b2021-06-21 21:08:08 +0200874#if 0 // Currently unused
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200875 void
876crypt_sodium_encode(
877 cryptstate_T *state UNUSED,
878 char_u *from UNUSED,
879 size_t len UNUSED,
880 char_u *to UNUSED,
881 int last UNUSED)
882{
883# ifdef FEAT_SODIUM
884 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
885 sodium_state_T *sod_st = state->method_state;
886 unsigned char tag = last
887 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
888
889 if (sod_st->count == 0)
890 {
891 if (len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES)
892 {
893 emsg(e_libsodium_cannot_encrypt_header);
894 return;
895 }
896 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state,
897 to, sod_st->key);
898 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
899 }
900
901 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES)
902 {
903 emsg(e_libsodium_cannot_encrypt_buffer);
904 return;
905 }
906
907 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, to, NULL,
908 from, len, NULL, 0, tag);
909
910 sod_st->count++;
911# endif
912}
Christian Brabandt226b28b2021-06-21 21:08:08 +0200913#endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200914
Christian Brabandt226b28b2021-06-21 21:08:08 +0200915/*
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200916 * Decrypt "from[len]" into "to[len]".
917 * "from" and "to" can be equal to encrypt in place.
918 */
Christian Brabandt226b28b2021-06-21 21:08:08 +0200919#if 0 // Currently unused
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200920 void
921crypt_sodium_decode(
922 cryptstate_T *state UNUSED,
923 char_u *from UNUSED,
924 size_t len UNUSED,
925 char_u *to UNUSED,
926 int last UNUSED)
927{
928# ifdef FEAT_SODIUM
929 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
930 sodium_state_T *sod_st = state->method_state;
931 unsigned char tag;
932 unsigned long long buf_len;
933 char_u *p1 = from;
934 char_u *p2 = to;
935 char_u *buf_out;
936
937 if (sod_st->count == 0
938 && len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES)
939 {
940 emsg(e_libsodium_cannot_decrypt_header);
941 return;
942 }
943
944 buf_out = (char_u *)alloc(len);
945
946 if (buf_out == NULL)
947 {
948 emsg(e_libsodium_cannot_allocate_buffer);
949 return;
950 }
951 if (sod_st->count == 0)
952 {
953 if (crypto_secretstream_xchacha20poly1305_init_pull(
954 &sod_st->state, from, sod_st->key) != 0)
955 {
956 emsg(e_libsodium_decryption_failed_header_incomplete);
957 goto fail;
958 }
959
960 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
961 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES;
962
963 if (p1 == p2)
964 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
965 }
966
967 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES)
968 {
969 emsg(e_libsodium_cannot_decrypt_buffer);
Dominique Pellecb54bc62021-06-21 20:15:37 +0200970 goto fail;
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200971 }
972 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state,
973 buf_out, &buf_len, &tag, from, len, NULL, 0) != 0)
974 {
Dominique Pellecb54bc62021-06-21 20:15:37 +0200975 emsg(e_libsodium_decryption_failed);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200976 goto fail;
977 }
978 sod_st->count++;
979
980 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last)
981 {
Dominique Pellecb54bc62021-06-21 20:15:37 +0200982 emsg(e_libsodium_decryption_failed_premature);
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200983 goto fail;
984 }
985 if (p1 == p2)
986 mch_memmove(p2, buf_out, buf_len);
987
988fail:
989 vim_free(buf_out);
990# endif
991}
Christian Brabandt226b28b2021-06-21 21:08:08 +0200992#endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200993
994/*
995 * Encrypt "from[len]" into "to[len]".
996 * "from" and "to" can be equal to encrypt in place.
997 */
998 long
999crypt_sodium_buffer_encode(
1000 cryptstate_T *state UNUSED,
1001 char_u *from UNUSED,
1002 size_t len UNUSED,
1003 char_u **buf_out UNUSED,
1004 int last UNUSED)
1005{
1006# ifdef FEAT_SODIUM
1007 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
1008 unsigned long long out_len;
1009 char_u *ptr;
1010 unsigned char tag = last
1011 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
1012 int length;
1013 sodium_state_T *sod_st = state->method_state;
1014 int first = (sod_st->count == 0);
1015
Christian Brabandt226b28b2021-06-21 21:08:08 +02001016 length = (int)len + crypto_secretstream_xchacha20poly1305_ABYTES
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001017 + (first ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0);
1018 *buf_out = alloc_clear(length);
1019 if (*buf_out == NULL)
1020 {
1021 emsg(e_libsodium_cannot_allocate_buffer);
1022 return -1;
1023 }
1024 ptr = *buf_out;
1025
1026 if (first)
1027 {
1028 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state,
1029 ptr, sod_st->key);
1030 ptr += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
1031 }
1032
1033 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, ptr,
1034 &out_len, from, len, NULL, 0, tag);
1035
1036 sod_st->count++;
1037 return out_len + (first
1038 ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0);
1039# else
1040 return -1;
1041# endif
1042}
1043
1044/*
1045 * Decrypt "from[len]" into "to[len]".
1046 * "from" and "to" can be equal to encrypt in place.
1047 */
1048 long
1049crypt_sodium_buffer_decode(
1050 cryptstate_T *state UNUSED,
1051 char_u *from UNUSED,
1052 size_t len UNUSED,
1053 char_u **buf_out UNUSED,
1054 int last UNUSED)
1055{
1056# ifdef FEAT_SODIUM
1057 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
1058 sodium_state_T *sod_st = state->method_state;
1059 unsigned char tag;
1060 unsigned long long out_len;
1061 *buf_out = alloc_clear(len);
1062 if (*buf_out == NULL)
1063 {
1064 emsg(e_libsodium_cannot_allocate_buffer);
1065 return -1;
1066 }
1067
1068 if (sod_st->count == 0)
1069 {
1070 if (crypto_secretstream_xchacha20poly1305_init_pull(&sod_st->state,
1071 from, sod_st->key) != 0)
1072 {
1073 emsg(e_libsodium_decryption_failed_header_incomplete);
1074 return -1;
1075 }
1076 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
1077 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES;
1078 sod_st->count++;
1079 }
1080 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state,
1081 *buf_out, &out_len, &tag, from, len, NULL, 0) != 0)
1082 {
Dominique Pellecb54bc62021-06-21 20:15:37 +02001083 emsg(e_libsodium_decryption_failed);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001084 return -1;
1085 }
1086
1087 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last)
Dominique Pellecb54bc62021-06-21 20:15:37 +02001088 emsg(e_libsodium_decryption_failed_premature);
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001089 return (long) out_len;
1090# else
1091 return -1;
1092# endif
1093}
1094
K.Takata1a8825d2022-01-19 13:32:57 +00001095# if defined(FEAT_SODIUM) || defined(PROTO)
1096 int
1097crypt_sodium_munlock(void *const addr, const size_t len)
1098{
1099 return sodium_munlock(addr, len);
1100}
1101
1102 void
1103crypt_sodium_randombytes_buf(void *const buf, const size_t size)
1104{
1105 randombytes_buf(buf, size);
1106}
1107# endif
1108
Bram Moolenaarc667da52019-11-30 20:52:27 +01001109#endif // FEAT_CRYPT