annotate src/crypt.c @ 24990:85d1e82ed134 v8.2.3032

patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium Commit: https://github.com/vim/vim/commit/226b28b96150e59375d2bff44e0aadd382b0c3f1 Author: Christian Brabandt <cb@256bit.org> Date: Mon Jun 21 21:08:08 2021 +0200 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium Problem: Build problems with MSVC, other crypt issues with libsodium. Solution: Adjust MSVC makefile. Disable swap file only when 'key' is set. Adjust error message used when key is wrong. Fix Coverity issues. (Christian Brabandt, closes #8420, closes #8411)
author Bram Moolenaar <Bram@vim.org>
date Mon, 21 Jun 2021 21:15:04 +0200
parents fa31a0ea09e1
children 68a7e6d70a5e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10042
4aead6a9b7a9 commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents: 7817
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
2 *
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
4 *
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
8 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
9
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 * crypt.c: Generic encryption support.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13 #include "vim.h"
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
14
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
15 #ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
16 # include <sodium.h>
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
17 #endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
18
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 #if defined(FEAT_CRYPT) || defined(PROTO)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21 * Optional encryption support.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22 * Mohsin Ahmed, mosh@sasi.com, 1998-09-24
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 * Based on zip/crypt sources.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24 * Refactored by David Leadbeater, 2014.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25 *
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 * NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27 * most countries. There are a few exceptions, but that still should not be a
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 * problem since this code was originally created in Europe and India.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29 *
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 * Blowfish addition originally made by Mohsin Ahmed,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 * http://www.cs.albany.edu/~mosh 2010-03-14
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
32 * Based on blowfish by Bruce Schneier (http://www.schneier.com/blowfish.html)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33 * and sha256 by Christophe Devine.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 typedef struct {
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
37 char *name; // encryption name as used in 'cryptmethod'
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
38 char *magic; // magic bytes stored in file header
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
39 int salt_len; // length of salt, or 0 when not using salt
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
40 int seed_len; // length of seed, or 0 when not using seed
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
41 #ifdef CRYPT_NOT_INPLACE
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
42 int works_inplace; // encryption/decryption can be done in-place
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
43 #endif
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
44 int whole_undofile; // whole undo file is encrypted
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
46 // Optional function pointer for a self-test.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
47 int (* self_test_fn)();
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48
16378
3d6b282e2d6e patch 8.1.1194: typos and small problems in source files
Bram Moolenaar <Bram@vim.org>
parents: 15967
diff changeset
49 // Function pointer for initializing encryption/decryption.
16429
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
50 int (* init_fn)(cryptstate_T *state, char_u *key,
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
51 char_u *salt, int salt_len, char_u *seed, int seed_len);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
53 // Function pointers for encoding/decoding from one buffer into another.
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
54 // Optional, however, these or the _buffer ones should be configured.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
56 char_u *to, int last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 void (*decode_fn)(cryptstate_T *state, char_u *from, size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
58 char_u *to, int last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
60 // Function pointers for encoding and decoding, can buffer data if needed.
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
61 // Optional (however, these or the above should be configured).
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 long (*encode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
63 char_u **newptr, int last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 long (*decode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
65 char_u **newptr, int last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
67 // Function pointers for in-place encoding and decoding, used for
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
68 // crypt_*_inplace(). "from" and "to" arguments will be equal.
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
69 // These may be the same as decode_fn and encode_fn above, however an
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
70 // algorithm may implement them in a way that is not interchangeable with
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
71 // the crypt_(en|de)code() interface (for example because it wishes to add
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
72 // padding to files).
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
73 // This method is used for swap and undo files which have a rigid format.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74 void (*encode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
75 char_u *p2, int last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76 void (*decode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
77 char_u *p2, int last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 } cryptmethod_T;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
80 // index is method_nr of cryptstate_T, CRYPT_M_*
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = {
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
82 // PK_Zip; very weak
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84 "zip",
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 "VimCrypt~01!",
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 0,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 0,
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
88 #ifdef CRYPT_NOT_INPLACE
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 TRUE,
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
90 #endif
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 FALSE,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 NULL,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93 crypt_zip_init,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94 crypt_zip_encode, crypt_zip_decode,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95 NULL, NULL,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 crypt_zip_encode, crypt_zip_decode,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 },
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
99 // Blowfish/CFB + SHA-256 custom key derivation; implementation issues.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101 "blowfish",
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 "VimCrypt~02!",
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103 8,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 8,
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
105 #ifdef CRYPT_NOT_INPLACE
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106 TRUE,
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
107 #endif
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 FALSE,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109 blowfish_self_test,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 crypt_blowfish_init,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 crypt_blowfish_encode, crypt_blowfish_decode,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112 NULL, NULL,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 crypt_blowfish_encode, crypt_blowfish_decode,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 },
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
116 // Blowfish/CFB + SHA-256 custom key derivation; fixed.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 "blowfish2",
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 "VimCrypt~03!",
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
120 8,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 8,
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
122 #ifdef CRYPT_NOT_INPLACE
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
123 TRUE,
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
124 #endif
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 TRUE,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126 blowfish_self_test,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 crypt_blowfish_init,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 crypt_blowfish_encode, crypt_blowfish_decode,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 NULL, NULL,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 crypt_blowfish_encode, crypt_blowfish_decode,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 },
13244
ac42c4b11dbc patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents: 10042
diff changeset
132
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
133 // XChaCha20 using libsodium
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
134 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
135 "xchacha20",
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
136 "VimCrypt~04!",
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
137 #ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
138 crypto_pwhash_argon2id_SALTBYTES, // 16
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
139 #else
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
140 16,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
141 #endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
142 8,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
143 #ifdef CRYPT_NOT_INPLACE
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
144 FALSE,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
145 #endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
146 FALSE,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
147 NULL,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
148 crypt_sodium_init,
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
149 NULL, NULL,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
150 crypt_sodium_buffer_encode, crypt_sodium_buffer_decode,
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
151 NULL, NULL,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
152 },
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
153
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
154 // NOTE: when adding a new method, use some random bytes for the magic key,
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
155 // to avoid that a text file is recognized as encrypted.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
156 };
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
157
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
158 #ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
159 typedef struct {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
160 size_t count;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
161 unsigned char key[crypto_box_SEEDBYTES];
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
162 // 32, same as crypto_secretstream_xchacha20poly1305_KEYBYTES
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
163 crypto_secretstream_xchacha20poly1305_state
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
164 state;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
165 } sodium_state_T;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
166 #endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
167
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
168 #define CRYPT_MAGIC_LEN 12 // cannot change
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
169 static char crypt_magic_head[] = "VimCrypt~";
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
170
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
171 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
172 * Return int value for crypt method name.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
173 * 0 for "zip", the old method. Also for any non-valid value.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
174 * 1 for "blowfish".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
175 * 2 for "blowfish2".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
176 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
177 int
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
178 crypt_method_nr_from_name(char_u *name)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
179 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
180 int i;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
181
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
182 for (i = 0; i < CRYPT_M_COUNT; ++i)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
183 if (STRCMP(name, cryptmethods[i].name) == 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
184 return i;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
185 return 0;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
186 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
187
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
188 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
189 * Get the crypt method used for a file from "ptr[len]", the magic text at the
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
190 * start of the file.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
191 * Returns -1 when no encryption used.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
192 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
193 int
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
194 crypt_method_nr_from_magic(char *ptr, int len)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
195 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
196 int i;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
197
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
198 if (len < CRYPT_MAGIC_LEN)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
199 return -1;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
200
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
201 for (i = 0; i < CRYPT_M_COUNT; i++)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
202 if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
203 return i;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
204
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
205 i = (int)STRLEN(crypt_magic_head);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
206 if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0)
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 13244
diff changeset
207 emsg(_("E821: File is encrypted with unknown method"));
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
208
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
209 return -1;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
210 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
211
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
212 #ifdef CRYPT_NOT_INPLACE
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
213 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
214 * Return TRUE if the crypt method for "method_nr" can be done in-place.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
215 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
216 int
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
217 crypt_works_inplace(cryptstate_T *state)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
218 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
219 return cryptmethods[state->method_nr].works_inplace;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
220 }
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
221 #endif
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
222
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
223 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
224 * Get the crypt method for buffer "buf" as a number.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
225 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
226 int
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
227 crypt_get_method_nr(buf_T *buf)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
228 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
229 return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
230 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
231
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
232 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
233 * Return TRUE when the buffer uses an encryption method that encrypts the
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
234 * whole undo file, not only the text.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
235 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
236 int
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
237 crypt_whole_undofile(int method_nr)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
238 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
239 return cryptmethods[method_nr].whole_undofile;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
240 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
241
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
242 /*
18498
9e6d5a4abb1c patch 8.1.2243: typos in comments
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
243 * Get crypt method specific length of the file header in bytes.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
244 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
245 int
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
246 crypt_get_header_len(int method_nr)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
247 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
248 return CRYPT_MAGIC_LEN
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
249 + cryptmethods[method_nr].salt_len
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
250 + cryptmethods[method_nr].seed_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
251 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
252
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
253
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
254 /*
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
255 * Get maximum crypt method specific length of the file header in bytes.
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
256 */
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
257 int
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
258 crypt_get_max_header_len()
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
259 {
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
260 int i;
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
261 int max = 0;
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
262 int temp = 0;
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
263
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
264 for (i = 0; i < CRYPT_M_COUNT; ++i)
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
265 {
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
266 temp = crypt_get_header_len(i);
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
267 if (temp > max)
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
268 max = temp;
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
269 }
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
270 return max;
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
271 }
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
272
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
273 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
274 * Set the crypt method for buffer "buf" to "method_nr" using the int value as
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
275 * returned by crypt_method_nr_from_name().
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
276 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
277 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
278 crypt_set_cm_option(buf_T *buf, int method_nr)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
279 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
280 free_string_option(buf->b_p_cm);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
281 buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
282 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
283
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
284 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
285 * If the crypt method for the current buffer has a self-test, run it and
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
286 * return OK/FAIL.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
287 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
288 int
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
289 crypt_self_test(void)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
290 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
291 int method_nr = crypt_get_method_nr(curbuf);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
292
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
293 if (cryptmethods[method_nr].self_test_fn == NULL)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
294 return OK;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
295 return cryptmethods[method_nr].self_test_fn();
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
296 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
297
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
298 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
299 * Allocate a crypt state and initialize it.
16429
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
300 * Return NULL for failure.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
301 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
302 cryptstate_T *
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
303 crypt_create(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
304 int method_nr,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
305 char_u *key,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
306 char_u *salt,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
307 int salt_len,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
308 char_u *seed,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
309 int seed_len)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
310 {
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16782
diff changeset
311 cryptstate_T *state = ALLOC_ONE(cryptstate_T);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
312
16429
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
313 if (state == NULL)
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
314 return state;
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
315
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
316 state->method_nr = method_nr;
16429
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
317 if (cryptmethods[method_nr].init_fn(
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
318 state, key, salt, salt_len, seed, seed_len) == FAIL)
16429
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
319 {
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
320 vim_free(state);
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
321 return NULL;
a1229400434a patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents: 16378
diff changeset
322 }
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
323 return state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
324 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
325
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
326 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
327 * Allocate a crypt state from a file header and initialize it.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
328 * Assumes that header contains at least the number of bytes that
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
329 * crypt_get_header_len() returns for "method_nr".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
330 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
331 cryptstate_T *
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
332 crypt_create_from_header(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
333 int method_nr,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
334 char_u *key,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
335 char_u *header)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
336 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
337 char_u *salt = NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
338 char_u *seed = NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
339 int salt_len = cryptmethods[method_nr].salt_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
340 int seed_len = cryptmethods[method_nr].seed_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
341
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
342 if (salt_len > 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
343 salt = header + CRYPT_MAGIC_LEN;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
344 if (seed_len > 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
345 seed = header + CRYPT_MAGIC_LEN + salt_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
346
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
347 return crypt_create(method_nr, key, salt, salt_len, seed, seed_len);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
348 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
349
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
350 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
351 * Read the crypt method specific header data from "fp".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
352 * Return an allocated cryptstate_T or NULL on error.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
353 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
354 cryptstate_T *
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
355 crypt_create_from_file(FILE *fp, char_u *key)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
356 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
357 int method_nr;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
358 int header_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
359 char magic_buffer[CRYPT_MAGIC_LEN];
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
360 char_u *buffer;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
361 cryptstate_T *state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
362
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
363 if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
364 return NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
365 method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
366 if (method_nr < 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
367 return NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
368
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
369 header_len = crypt_get_header_len(method_nr);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
370 if ((buffer = alloc(header_len)) == NULL)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
371 return NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
372 mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
373 if (header_len > CRYPT_MAGIC_LEN
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
374 && fread(buffer + CRYPT_MAGIC_LEN,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
375 header_len - CRYPT_MAGIC_LEN, 1, fp) != 1)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
376 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
377 vim_free(buffer);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
378 return NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
379 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
380
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
381 state = crypt_create_from_header(method_nr, key, buffer);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
382 vim_free(buffer);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
383 return state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
384 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
385
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
386 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
387 * Allocate a cryptstate_T for writing and initialize it with "key".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
388 * Allocates and fills in the header and stores it in "header", setting
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
389 * "header_len". The header may include salt and seed, depending on
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
390 * cryptmethod. Caller must free header.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
391 * Returns the state or NULL on failure.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
392 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
393 cryptstate_T *
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
394 crypt_create_for_writing(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
395 int method_nr,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
396 char_u *key,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
397 char_u **header,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
398 int *header_len)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
399 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
400 int len = crypt_get_header_len(method_nr);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
401 char_u *salt = NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
402 char_u *seed = NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
403 int salt_len = cryptmethods[method_nr].salt_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
404 int seed_len = cryptmethods[method_nr].seed_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
405 cryptstate_T *state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
406
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
407 *header_len = len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
408 *header = alloc(len);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
409 if (*header == NULL)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
410 return NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
411
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
412 mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
413 if (salt_len > 0 || seed_len > 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
414 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
415 if (salt_len > 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
416 salt = *header + CRYPT_MAGIC_LEN;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
417 if (seed_len > 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
418 seed = *header + CRYPT_MAGIC_LEN + salt_len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
419
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
420 // TODO: Should this be crypt method specific? (Probably not worth
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
421 // it). sha2_seed is pretty bad for large amounts of entropy, so make
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
422 // that into something which is suitable for anything.
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
423 #ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
424 if (sodium_init() >= 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
425 {
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
426 if (salt_len > 0)
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
427 randombytes_buf(salt, salt_len);
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
428 if (seed_len > 0)
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
429 randombytes_buf(seed, seed_len);
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
430 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
431 else
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
432 #endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
433 sha2_seed(salt, salt_len, seed, seed_len);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
434 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
435 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
436 if (state == NULL)
13244
ac42c4b11dbc patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents: 10042
diff changeset
437 VIM_CLEAR(*header);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
438 return state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
439 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
440
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
441 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
442 * Free the crypt state.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
443 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
444 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
445 crypt_free_state(cryptstate_T *state)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
446 {
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
447 #ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
448 if (state->method_nr == CRYPT_M_SOD)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
449 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
450 sodium_memzero(state->method_state, sizeof(sodium_state_T));
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
451 sodium_free(state->method_state);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
452 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
453 else
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
454 #endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
455 vim_free(state->method_state);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
456 vim_free(state);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
457 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
458
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
459 #ifdef CRYPT_NOT_INPLACE
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
460 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
461 * Encode "from[len]" and store the result in a newly allocated buffer, which
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
462 * is stored in "newptr".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
463 * Return number of bytes in "newptr", 0 for need more or -1 on error.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
464 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
465 long
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
466 crypt_encode_alloc(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
467 cryptstate_T *state,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
468 char_u *from,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
469 size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
470 char_u **newptr,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
471 int last)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
472 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
473 cryptmethod_T *method = &cryptmethods[state->method_nr];
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
474
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
475 if (method->encode_buffer_fn != NULL)
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
476 // Has buffer function, pass through.
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
477 return method->encode_buffer_fn(state, from, len, newptr, last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
478 if (len == 0)
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
479 // Not buffering, just return EOF.
6132
0242c27e40e1 updated for version 7.4.404
Bram Moolenaar <bram@vim.org>
parents: 6122
diff changeset
480 return (long)len;
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
481
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
482 *newptr = alloc(len + 50);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
483 if (*newptr == NULL)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
484 return -1;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
485 method->encode_fn(state, from, len, *newptr, last);
6132
0242c27e40e1 updated for version 7.4.404
Bram Moolenaar <bram@vim.org>
parents: 6122
diff changeset
486 return (long)len;
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
487 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
488
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
489 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
490 * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
491 * is stored in "newptr".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
492 * Return number of bytes in "newptr", 0 for need more or -1 on error.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
493 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
494 long
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
495 crypt_decode_alloc(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
496 cryptstate_T *state,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
497 char_u *ptr,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
498 long len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
499 char_u **newptr,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
500 int last)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
501 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
502 cryptmethod_T *method = &cryptmethods[state->method_nr];
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
503
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
504 if (method->decode_buffer_fn != NULL)
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
505 // Has buffer function, pass through.
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
506 return method->decode_buffer_fn(state, ptr, len, newptr, last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
507
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
508 if (len == 0)
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
509 // Not buffering, just return EOF.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
510 return len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
511
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
512 *newptr = alloc(len);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
513 if (*newptr == NULL)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
514 return -1;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
515 method->decode_fn(state, ptr, len, *newptr, last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
516 return len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
517 }
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
518 #endif
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
519
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
520 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
521 * Encrypting "from[len]" into "to[len]".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
522 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
523 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
524 crypt_encode(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
525 cryptstate_T *state,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
526 char_u *from,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
527 size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
528 char_u *to,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
529 int last)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
530 {
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
531 cryptmethods[state->method_nr].encode_fn(state, from, len, to, last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
532 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
533
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
534 #if 0 // unused
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
535 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
536 * decrypting "from[len]" into "to[len]".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
537 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
538 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
539 crypt_decode(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
540 cryptstate_T *state,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
541 char_u *from,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
542 size_t len,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
543 char_u *to,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
544 int last)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
545 {
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
546 cryptmethods[state->method_nr].decode_fn(state, from, len, to, last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
547 }
15531
959cf4c63b18 patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
548 #endif
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
549
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
550 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
551 * Simple inplace encryption, modifies "buf[len]" in place.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
552 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
553 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
554 crypt_encode_inplace(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
555 cryptstate_T *state,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
556 char_u *buf,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
557 size_t len,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
558 int last)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
559 {
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
560 cryptmethods[state->method_nr].encode_inplace_fn(state, buf, len,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
561 buf, last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
562 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
563
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
564 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
565 * Simple inplace decryption, modifies "buf[len]" in place.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
566 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
567 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
568 crypt_decode_inplace(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
569 cryptstate_T *state,
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
570 char_u *buf,
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
571 size_t len,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
572 int last)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
573 {
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
574 cryptmethods[state->method_nr].decode_inplace_fn(state, buf, len,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
575 buf, last);
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
576 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
577
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
578 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
579 * Free an allocated crypt key. Clear the text to make sure it doesn't stay
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
580 * in memory anywhere.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
581 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
582 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
583 crypt_free_key(char_u *key)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
584 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
585 char_u *p;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
586
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
587 if (key != NULL)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
588 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
589 for (p = key; *p != NUL; ++p)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
590 *p = 0;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
591 vim_free(key);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
592 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
593 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
594
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
595 /*
6353
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
596 * Check the crypt method and give a warning if it's outdated.
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
597 */
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
598 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
599 crypt_check_method(int method)
6353
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
600 {
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
601 if (method < CRYPT_M_BF2)
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
602 {
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
603 msg_scroll = TRUE;
15543
dd725a8ab112 patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 15531
diff changeset
604 msg(_("Warning: Using a weak encryption method; see :help 'cm'"));
6353
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
605 }
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
606 }
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
607
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
608 #ifdef FEAT_SODIUM
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
609 static void
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
610 crypt_check_swapfile_curbuf(void)
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
611 {
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
612 int method = crypt_get_method_nr(curbuf);
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
613 if (method == CRYPT_M_SOD)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
614 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
615 // encryption uses padding and MAC, that does not work very well with
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
616 // swap and undo files, so disable them
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
617 mf_close_file(curbuf, TRUE); // remove the swap file
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
618 set_option_value((char_u *)"swf", 0, NULL, OPT_LOCAL);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
619 #ifdef FEAT_PERSISTENT_UNDO
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
620 set_option_value((char_u *)"udf", 0, NULL, OPT_LOCAL);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
621 #endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
622 msg_scroll = TRUE;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
623 msg(_("Note: Encryption of swapfile not supported, disabling swap- and undofile"));
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
624 }
6353
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
625 }
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
626 #endif
6353
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
627
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
628 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
629 crypt_check_current_method(void)
6353
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
630 {
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
631 crypt_check_method(crypt_get_method_nr(curbuf));
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
632 }
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
633
60659773c73b updated for version 7.4.509
Bram Moolenaar <bram@vim.org>
parents: 6132
diff changeset
634 /*
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
635 * Ask the user for a crypt key.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
636 * When "store" is TRUE, the new key is stored in the 'key' option, and the
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
637 * 'key' option value is returned: Don't free it.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
638 * When "store" is FALSE, the typed key is returned in allocated memory.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
639 * Returns NULL on failure.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
640 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
641 char_u *
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
642 crypt_get_key(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
643 int store,
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
644 int twice) // Ask for the key twice.
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
645 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
646 char_u *p1, *p2 = NULL;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
647 int round;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
648
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
649 for (round = 0; ; ++round)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
650 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
651 cmdline_star = TRUE;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
652 cmdline_row = msg_row;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
653 p1 = getcmdline_prompt(NUL, round == 0
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
654 ? (char_u *)_("Enter encryption key: ")
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
655 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING,
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
656 NULL);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
657 cmdline_star = FALSE;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
658
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
659 if (p1 == NULL)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
660 break;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
661
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
662 if (round == twice)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
663 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
664 if (p2 != NULL && STRCMP(p1, p2) != 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
665 {
15543
dd725a8ab112 patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 15531
diff changeset
666 msg(_("Keys don't match!"));
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
667 crypt_free_key(p1);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
668 crypt_free_key(p2);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
669 p2 = NULL;
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
670 round = -1; // do it again
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
671 continue;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
672 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
673
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
674 if (store)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
675 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
676 set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
677 crypt_free_key(p1);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
678 p1 = curbuf->b_p_key;
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
679 #ifdef FEAT_SODIUM
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
680 crypt_check_swapfile_curbuf();
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
681 #endif
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
682 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
683 break;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
684 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
685 p2 = p1;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
686 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
687
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
688 // since the user typed this, no need to wait for return
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
689 if (crypt_get_method_nr(curbuf) != CRYPT_M_SOD)
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
690 {
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
691 if (msg_didout)
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
692 msg_putchar('\n');
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
693 need_wait_return = FALSE;
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
694 msg_didout = FALSE;
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
695 }
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
696
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
697 crypt_free_key(p2);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
698 return p1;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
699 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
700
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
701
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
702 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
703 * Append a message to IObuff for the encryption/decryption method being used.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
704 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
705 void
7817
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
706 crypt_append_msg(
83861277e6a3 commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents: 6353
diff changeset
707 buf_T *buf)
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
708 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
709 if (crypt_get_method_nr(buf) == 0)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
710 STRCAT(IObuff, _("[crypted]"));
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
711 else
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
712 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
713 STRCAT(IObuff, "[");
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
714 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
715 STRCAT(IObuff, "]");
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
716 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
717 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
718
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
719 int
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
720 crypt_sodium_init(
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
721 cryptstate_T *state UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
722 char_u *key UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
723 char_u *salt UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
724 int salt_len UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
725 char_u *seed UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
726 int seed_len UNUSED)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
727 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
728 # ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
729 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
730 unsigned char dkey[crypto_box_SEEDBYTES]; // 32
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
731 sodium_state_T *sd_state;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
732
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
733 if (sodium_init() < 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
734 return FAIL;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
735
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
736 sd_state = (sodium_state_T *)sodium_malloc(sizeof(sodium_state_T));
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
737 sodium_memzero(sd_state, sizeof(sodium_state_T));
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
738
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
739 // derive a key from the password
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
740 if (crypto_pwhash(dkey, sizeof(dkey), (const char *)key, STRLEN(key), salt,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
741 crypto_pwhash_OPSLIMIT_INTERACTIVE, crypto_pwhash_MEMLIMIT_INTERACTIVE,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
742 crypto_pwhash_ALG_DEFAULT) != 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
743 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
744 // out of memory
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
745 sodium_free(sd_state);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
746 return FAIL;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
747 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
748 memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
749 sd_state->count = 0;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
750 state->method_state = sd_state;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
751
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
752 return OK;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
753 # else
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
754 emsg(e_libsodium_not_built_in);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
755 return FAIL;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
756 # endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
757 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
758
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
759 /*
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
760 * Encrypt "from[len]" into "to[len]".
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
761 * "from" and "to" can be equal to encrypt in place.
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
762 * Call needs to ensure that there is enough space in to (for the header)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
763 */
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
764 #if 0 // Currently unused
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
765 void
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
766 crypt_sodium_encode(
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
767 cryptstate_T *state UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
768 char_u *from UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
769 size_t len UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
770 char_u *to UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
771 int last UNUSED)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
772 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
773 # ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
774 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
775 sodium_state_T *sod_st = state->method_state;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
776 unsigned char tag = last
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
777 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
778
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
779 if (sod_st->count == 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
780 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
781 if (len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
782 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
783 emsg(e_libsodium_cannot_encrypt_header);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
784 return;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
785 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
786 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
787 to, sod_st->key);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
788 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
789 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
790
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
791 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
792 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
793 emsg(e_libsodium_cannot_encrypt_buffer);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
794 return;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
795 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
796
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
797 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, to, NULL,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
798 from, len, NULL, 0, tag);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
799
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
800 sod_st->count++;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
801 # endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
802 }
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
803 #endif
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
804
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
805 /*
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
806 * Decrypt "from[len]" into "to[len]".
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
807 * "from" and "to" can be equal to encrypt in place.
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
808 */
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
809 #if 0 // Currently unused
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
810 void
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
811 crypt_sodium_decode(
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
812 cryptstate_T *state UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
813 char_u *from UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
814 size_t len UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
815 char_u *to UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
816 int last UNUSED)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
817 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
818 # ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
819 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
820 sodium_state_T *sod_st = state->method_state;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
821 unsigned char tag;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
822 unsigned long long buf_len;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
823 char_u *p1 = from;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
824 char_u *p2 = to;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
825 char_u *buf_out;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
826
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
827 if (sod_st->count == 0
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
828 && len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
829 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
830 emsg(e_libsodium_cannot_decrypt_header);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
831 return;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
832 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
833
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
834 buf_out = (char_u *)alloc(len);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
835
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
836 if (buf_out == NULL)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
837 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
838 emsg(e_libsodium_cannot_allocate_buffer);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
839 return;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
840 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
841 if (sod_st->count == 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
842 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
843 if (crypto_secretstream_xchacha20poly1305_init_pull(
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
844 &sod_st->state, from, sod_st->key) != 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
845 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
846 emsg(e_libsodium_decryption_failed_header_incomplete);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
847 goto fail;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
848 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
849
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
850 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
851 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
852
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
853 if (p1 == p2)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
854 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
855 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
856
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
857 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
858 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
859 emsg(e_libsodium_cannot_decrypt_buffer);
24986
fa31a0ea09e1 patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
860 goto fail;
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
861 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
862 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
863 buf_out, &buf_len, &tag, from, len, NULL, 0) != 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
864 {
24986
fa31a0ea09e1 patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
865 emsg(e_libsodium_decryption_failed);
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
866 goto fail;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
867 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
868 sod_st->count++;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
869
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
870 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
871 {
24986
fa31a0ea09e1 patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
872 emsg(e_libsodium_decryption_failed_premature);
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
873 goto fail;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
874 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
875 if (p1 == p2)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
876 mch_memmove(p2, buf_out, buf_len);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
877
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
878 fail:
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
879 vim_free(buf_out);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
880 # endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
881 }
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
882 #endif
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
883
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
884 /*
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
885 * Encrypt "from[len]" into "to[len]".
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
886 * "from" and "to" can be equal to encrypt in place.
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
887 */
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
888 long
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
889 crypt_sodium_buffer_encode(
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
890 cryptstate_T *state UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
891 char_u *from UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
892 size_t len UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
893 char_u **buf_out UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
894 int last UNUSED)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
895 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
896 # ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
897 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
898 unsigned long long out_len;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
899 char_u *ptr;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
900 unsigned char tag = last
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
901 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
902 int length;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
903 sodium_state_T *sod_st = state->method_state;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
904 int first = (sod_st->count == 0);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
905
24990
85d1e82ed134 patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents: 24986
diff changeset
906 length = (int)len + crypto_secretstream_xchacha20poly1305_ABYTES
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
907 + (first ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
908 *buf_out = alloc_clear(length);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
909 if (*buf_out == NULL)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
910 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
911 emsg(e_libsodium_cannot_allocate_buffer);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
912 return -1;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
913 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
914 ptr = *buf_out;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
915
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
916 if (first)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
917 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
918 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
919 ptr, sod_st->key);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
920 ptr += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
921 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
922
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
923 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, ptr,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
924 &out_len, from, len, NULL, 0, tag);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
925
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
926 sod_st->count++;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
927 return out_len + (first
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
928 ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
929 # else
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
930 return -1;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
931 # endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
932 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
933
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
934 /*
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
935 * Decrypt "from[len]" into "to[len]".
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
936 * "from" and "to" can be equal to encrypt in place.
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
937 */
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
938 long
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
939 crypt_sodium_buffer_decode(
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
940 cryptstate_T *state UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
941 char_u *from UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
942 size_t len UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
943 char_u **buf_out UNUSED,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
944 int last UNUSED)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
945 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
946 # ifdef FEAT_SODIUM
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
947 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
948 sodium_state_T *sod_st = state->method_state;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
949 unsigned char tag;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
950 unsigned long long out_len;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
951 *buf_out = alloc_clear(len);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
952 if (*buf_out == NULL)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
953 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
954 emsg(e_libsodium_cannot_allocate_buffer);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
955 return -1;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
956 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
957
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
958 if (sod_st->count == 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
959 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
960 if (crypto_secretstream_xchacha20poly1305_init_pull(&sod_st->state,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
961 from, sod_st->key) != 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
962 {
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
963 emsg(e_libsodium_decryption_failed_header_incomplete);
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
964 return -1;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
965 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
966 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
967 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
968 sod_st->count++;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
969 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
970 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state,
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
971 *buf_out, &out_len, &tag, from, len, NULL, 0) != 0)
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
972 {
24986
fa31a0ea09e1 patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
973 emsg(e_libsodium_decryption_failed);
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
974 return -1;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
975 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
976
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
977 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last)
24986
fa31a0ea09e1 patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents: 24970
diff changeset
978 emsg(e_libsodium_decryption_failed_premature);
24970
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
979 return (long) out_len;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
980 # else
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
981 return -1;
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
982 # endif
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
983 }
7e9e53a0368f patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents: 18757
diff changeset
984
18757
c469e1930456 patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 18498
diff changeset
985 #endif // FEAT_CRYPT