Mercurial > vim
annotate src/crypt.c @ 31922:440256d03990 v9.0.1293
patch 9.0.1293: the set_num_option() is too long
Commit: https://github.com/vim/vim/commit/0caaf1e46511f7a92e036f05e6aa9d5992540117
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Thu Feb 9 12:23:17 2023 +0000
patch 9.0.1293: the set_num_option() is too long
Problem: The set_num_option() is too long.
Solution: Move code to separate functions. (Yegappan Lakshmanan,
closes #11954)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 09 Feb 2023 13:30:04 +0100 |
parents | 540e85ac14c9 |
children | 4545f58c8490 |
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 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * crypt.c: Generic encryption support. | |
12 */ | |
13 #include "vim.h" | |
14 | |
15 #if defined(FEAT_CRYPT) || defined(PROTO) | |
16 /* | |
17 * Optional encryption support. | |
18 * Mohsin Ahmed, mosh@sasi.com, 1998-09-24 | |
19 * Based on zip/crypt sources. | |
20 * Refactored by David Leadbeater, 2014. | |
21 * | |
22 * NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to | |
23 * most countries. There are a few exceptions, but that still should not be a | |
24 * problem since this code was originally created in Europe and India. | |
25 * | |
26 * Blowfish addition originally made by Mohsin Ahmed, | |
27 * http://www.cs.albany.edu/~mosh 2010-03-14 | |
28 * Based on blowfish by Bruce Schneier (http://www.schneier.com/blowfish.html) | |
29 * and sha256 by Christophe Devine. | |
30 */ | |
31 | |
32 typedef struct { | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 #ifdef CRYPT_NOT_INPLACE |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
38 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
|
39 #endif |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
40 int whole_undofile; // whole undo file is encrypted |
6122 | 41 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
42 // Optional function pointer for a self-test. |
6122 | 43 int (* self_test_fn)(); |
44 | |
16378
3d6b282e2d6e
patch 8.1.1194: typos and small problems in source files
Bram Moolenaar <Bram@vim.org>
parents:
15967
diff
changeset
|
45 // 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
|
46 int (* init_fn)(cryptstate_T *state, char_u *key, |
6122 | 47 char_u *salt, int salt_len, char_u *seed, int seed_len); |
48 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
49 // 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
|
50 // Optional, however, these or the _buffer ones should be configured. |
6122 | 51 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
|
52 char_u *to, int last); |
6122 | 53 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
|
54 char_u *to, int last); |
6122 | 55 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
56 // 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
|
57 // Optional (however, these or the above should be configured). |
6122 | 58 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
|
59 char_u **newptr, int last); |
6122 | 60 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
|
61 char_u **newptr, int last); |
6122 | 62 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
63 // 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
|
64 // 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
|
65 // 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
|
66 // 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
|
67 // 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
|
68 // padding to files). |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
69 // This method is used for swap and undo files which have a rigid format. |
6122 | 70 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
|
71 char_u *p2, int last); |
6122 | 72 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
|
73 char_u *p2, int last); |
6122 | 74 } cryptmethod_T; |
75 | |
31379
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
76 static int crypt_sodium_init_(cryptstate_T *state, char_u *key, char_u *salt, int salt_len, char_u *seed, int seed_len); |
29320
a74398c432a4
patch 9.0.0003: functions are global while they could be local
Bram Moolenaar <Bram@vim.org>
parents:
28809
diff
changeset
|
77 static long crypt_sodium_buffer_decode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); |
a74398c432a4
patch 9.0.0003: functions are global while they could be local
Bram Moolenaar <Bram@vim.org>
parents:
28809
diff
changeset
|
78 static long crypt_sodium_buffer_encode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); |
a74398c432a4
patch 9.0.0003: functions are global while they could be local
Bram Moolenaar <Bram@vim.org>
parents:
28809
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 | 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 | 83 { |
84 "zip", | |
85 "VimCrypt~01!", | |
86 0, | |
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 | 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 | 91 FALSE, |
92 NULL, | |
93 crypt_zip_init, | |
94 crypt_zip_encode, crypt_zip_decode, | |
95 NULL, NULL, | |
96 crypt_zip_encode, crypt_zip_decode, | |
97 }, | |
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 | 100 { |
101 "blowfish", | |
102 "VimCrypt~02!", | |
103 8, | |
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 | 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 | 108 FALSE, |
109 blowfish_self_test, | |
110 crypt_blowfish_init, | |
111 crypt_blowfish_encode, crypt_blowfish_decode, | |
112 NULL, NULL, | |
113 crypt_blowfish_encode, crypt_blowfish_decode, | |
114 }, | |
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 | 117 { |
118 "blowfish2", | |
119 "VimCrypt~03!", | |
120 8, | |
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 | 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 | 125 TRUE, |
126 blowfish_self_test, | |
127 crypt_blowfish_init, | |
128 crypt_blowfish_encode, crypt_blowfish_decode, | |
129 NULL, NULL, | |
130 crypt_blowfish_encode, crypt_blowfish_decode, | |
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, |
31379
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
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 | 156 }; |
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; |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
166 |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
167 |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
168 # ifdef DYNAMIC_SODIUM |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
169 # ifdef MSWIN |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
170 # define SODIUM_PROC FARPROC |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
171 # define load_dll vimLoadLib |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
172 # define symbol_from_dll GetProcAddress |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
173 # define close_dll FreeLibrary |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
174 # define load_dll_error GetWin32Error |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
175 # else |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
176 # error Dynamic loading of libsodium is not supported for now. |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
177 //# define HINSTANCE void* |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
178 //# define SODIUM_PROC void* |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
179 //# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
180 //# define symbol_from_dll dlsym |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
181 //# define close_dll dlclose |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
182 //# define load_dll_error dlerror |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
183 # endif |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
184 |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
185 # define sodium_init load_sodium |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
186 # define sodium_free dll_sodium_free |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
187 # define sodium_malloc dll_sodium_malloc |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
188 # define sodium_memzero dll_sodium_memzero |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
189 # define sodium_mlock dll_sodium_mlock |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
190 # define sodium_munlock dll_sodium_munlock |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
191 # define crypto_secretstream_xchacha20poly1305_init_push \ |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
192 dll_crypto_secretstream_xchacha20poly1305_init_push |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
193 # define crypto_secretstream_xchacha20poly1305_push \ |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
194 dll_crypto_secretstream_xchacha20poly1305_push |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
195 # define crypto_secretstream_xchacha20poly1305_init_pull \ |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
196 dll_crypto_secretstream_xchacha20poly1305_init_pull |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
197 # define crypto_secretstream_xchacha20poly1305_pull \ |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
198 dll_crypto_secretstream_xchacha20poly1305_pull |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
199 # define crypto_pwhash dll_crypto_pwhash |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
200 # define randombytes_buf dll_randombytes_buf |
31379
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
201 # define randombytes_random dll_randombytes_random |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
202 |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
203 static int (*dll_sodium_init)(void) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
204 static void (*dll_sodium_free)(void *) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
205 static void *(*dll_sodium_malloc)(const size_t) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
206 static void (*dll_sodium_memzero)(void * const, const size_t) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
207 static int (*dll_sodium_mlock)(void * const, const size_t) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
208 static int (*dll_sodium_munlock)(void * const, const size_t) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
209 static int (*dll_crypto_secretstream_xchacha20poly1305_init_push) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
210 (crypto_secretstream_xchacha20poly1305_state *state, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
211 unsigned char [], |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
212 const unsigned char []) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
213 static int (*dll_crypto_secretstream_xchacha20poly1305_push) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
214 (crypto_secretstream_xchacha20poly1305_state *state, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
215 unsigned char *c, unsigned long long *clen_p, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
216 const unsigned char *m, unsigned long long mlen, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
217 const unsigned char *ad, unsigned long long adlen, unsigned char tag) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
218 = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
219 static int (*dll_crypto_secretstream_xchacha20poly1305_init_pull) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
220 (crypto_secretstream_xchacha20poly1305_state *state, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
221 const unsigned char [], |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
222 const unsigned char []) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
223 static int (*dll_crypto_secretstream_xchacha20poly1305_pull) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
224 (crypto_secretstream_xchacha20poly1305_state *state, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
225 unsigned char *m, unsigned long long *mlen_p, unsigned char *tag_p, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
226 const unsigned char *c, unsigned long long clen, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
227 const unsigned char *ad, unsigned long long adlen) = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
228 static int (*dll_crypto_pwhash)(unsigned char * const out, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
229 unsigned long long outlen, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
230 const char * const passwd, unsigned long long passwdlen, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
231 const unsigned char * const salt, |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
232 unsigned long long opslimit, size_t memlimit, int alg) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
233 = NULL; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
234 static void (*dll_randombytes_buf)(void * const buf, const size_t size); |
31379
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
235 static uint32_t (*dll_randombytes_random)(void); |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
236 |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
237 static struct { |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
238 const char *name; |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
239 SODIUM_PROC *ptr; |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
240 } sodium_funcname_table[] = { |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
241 {"sodium_init", (SODIUM_PROC*)&dll_sodium_init}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
242 {"sodium_free", (SODIUM_PROC*)&dll_sodium_free}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
243 {"sodium_malloc", (SODIUM_PROC*)&dll_sodium_malloc}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
244 {"sodium_memzero", (SODIUM_PROC*)&dll_sodium_memzero}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
245 {"sodium_mlock", (SODIUM_PROC*)&dll_sodium_mlock}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
246 {"sodium_munlock", (SODIUM_PROC*)&dll_sodium_munlock}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
247 {"crypto_secretstream_xchacha20poly1305_init_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
248 {"crypto_secretstream_xchacha20poly1305_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
249 {"crypto_secretstream_xchacha20poly1305_init_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
250 {"crypto_secretstream_xchacha20poly1305_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
251 {"crypto_pwhash", (SODIUM_PROC*)&dll_crypto_pwhash}, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
252 {"randombytes_buf", (SODIUM_PROC*)&dll_randombytes_buf}, |
31379
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
253 {"randombytes_random", (SODIUM_PROC*)&dll_randombytes_random}, |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
254 {NULL, NULL} |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
255 }; |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
256 |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
257 static int |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
258 sodium_runtime_link_init(int verbose) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
259 { |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
260 static HINSTANCE hsodium = NULL; |
27998
ef7d9789919d
patch 8.2.4524: MS-Windows: cannot build with some sodium libraries
Bram Moolenaar <Bram@vim.org>
parents:
27657
diff
changeset
|
261 const char *libname = DYNAMIC_SODIUM_DLL; |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
262 int i; |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
263 |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
264 if (hsodium != NULL) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
265 return OK; |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
266 |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
267 hsodium = load_dll(libname); |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
268 if (hsodium == NULL) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
269 { |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
270 if (verbose) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
271 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error()); |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
272 return FAIL; |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
273 } |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
274 |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
275 for (i = 0; sodium_funcname_table[i].ptr; ++i) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
276 { |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
277 if ((*sodium_funcname_table[i].ptr = symbol_from_dll(hsodium, |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
278 sodium_funcname_table[i].name)) == NULL) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
279 { |
27998
ef7d9789919d
patch 8.2.4524: MS-Windows: cannot build with some sodium libraries
Bram Moolenaar <Bram@vim.org>
parents:
27657
diff
changeset
|
280 close_dll(hsodium); |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
281 hsodium = NULL; |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
282 if (verbose) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
283 semsg(_(e_could_not_load_library_function_str), sodium_funcname_table[i].name); |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
284 return FAIL; |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
285 } |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
286 } |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
287 return OK; |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
288 } |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
289 |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
290 static int |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
291 load_sodium(void) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
292 { |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
293 if (sodium_runtime_link_init(TRUE) == FAIL) |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
294 return -1; |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
295 return dll_sodium_init(); |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
296 } |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
297 # endif |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
298 |
27657
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
299 # if defined(DYNAMIC_SODIUM) || defined(PROTO) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
300 int |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
301 sodium_enabled(int verbose) |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
302 { |
a077948be0f4
patch 8.2.4354: dynamic loading of libsodium not handled properly
Bram Moolenaar <Bram@vim.org>
parents:
27231
diff
changeset
|
303 return sodium_runtime_link_init(verbose) == OK; |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
304 } |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
305 # endif |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
306 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
307 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
308 #define CRYPT_MAGIC_LEN 12 // cannot change |
6122 | 309 static char crypt_magic_head[] = "VimCrypt~"; |
310 | |
311 /* | |
312 * Return int value for crypt method name. | |
313 * 0 for "zip", the old method. Also for any non-valid value. | |
314 * 1 for "blowfish". | |
315 * 2 for "blowfish2". | |
316 */ | |
317 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
318 crypt_method_nr_from_name(char_u *name) |
6122 | 319 { |
320 int i; | |
321 | |
322 for (i = 0; i < CRYPT_M_COUNT; ++i) | |
323 if (STRCMP(name, cryptmethods[i].name) == 0) | |
324 return i; | |
325 return 0; | |
326 } | |
327 | |
328 /* | |
329 * Get the crypt method used for a file from "ptr[len]", the magic text at the | |
330 * start of the file. | |
331 * Returns -1 when no encryption used. | |
332 */ | |
333 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
334 crypt_method_nr_from_magic(char *ptr, int len) |
6122 | 335 { |
336 int i; | |
337 | |
338 if (len < CRYPT_MAGIC_LEN) | |
339 return -1; | |
340 | |
341 for (i = 0; i < CRYPT_M_COUNT; i++) | |
342 if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0) | |
343 return i; | |
344 | |
345 i = (int)STRLEN(crypt_magic_head); | |
346 if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0) | |
26962
85866e069c24
patch 8.2.4010: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents:
25417
diff
changeset
|
347 emsg(_(e_file_is_encrypted_with_unknown_method)); |
6122 | 348 |
349 return -1; | |
350 } | |
351 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
352 #ifdef CRYPT_NOT_INPLACE |
6122 | 353 /* |
354 * Return TRUE if the crypt method for "method_nr" can be done in-place. | |
355 */ | |
356 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
357 crypt_works_inplace(cryptstate_T *state) |
6122 | 358 { |
359 return cryptmethods[state->method_nr].works_inplace; | |
360 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
361 #endif |
6122 | 362 |
363 /* | |
364 * Get the crypt method for buffer "buf" as a number. | |
365 */ | |
366 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
367 crypt_get_method_nr(buf_T *buf) |
6122 | 368 { |
369 return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); | |
370 } | |
371 | |
372 /* | |
373 * Return TRUE when the buffer uses an encryption method that encrypts the | |
374 * whole undo file, not only the text. | |
375 */ | |
376 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
377 crypt_whole_undofile(int method_nr) |
6122 | 378 { |
379 return cryptmethods[method_nr].whole_undofile; | |
380 } | |
381 | |
382 /* | |
18498
9e6d5a4abb1c
patch 8.1.2243: typos in comments
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
383 * Get crypt method specific length of the file header in bytes. |
6122 | 384 */ |
385 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
386 crypt_get_header_len(int method_nr) |
6122 | 387 { |
388 return CRYPT_MAGIC_LEN | |
389 + cryptmethods[method_nr].salt_len | |
390 + cryptmethods[method_nr].seed_len; | |
391 } | |
392 | |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
393 |
27018
268f6a3511df
patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents:
26962
diff
changeset
|
394 #if defined(FEAT_SODIUM) || defined(PROTO) |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
395 /* |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
396 * 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
|
397 */ |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
398 int |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
399 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
|
400 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
401 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
|
402 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
|
403 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
|
404 |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
405 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
|
406 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
407 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
|
408 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
|
409 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
|
410 } |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
411 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
|
412 } |
27018
268f6a3511df
patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents:
26962
diff
changeset
|
413 #endif |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
414 |
6122 | 415 /* |
416 * Set the crypt method for buffer "buf" to "method_nr" using the int value as | |
417 * returned by crypt_method_nr_from_name(). | |
418 */ | |
419 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
420 crypt_set_cm_option(buf_T *buf, int method_nr) |
6122 | 421 { |
422 free_string_option(buf->b_p_cm); | |
423 buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name); | |
424 } | |
425 | |
426 /* | |
427 * If the crypt method for the current buffer has a self-test, run it and | |
428 * return OK/FAIL. | |
429 */ | |
430 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
431 crypt_self_test(void) |
6122 | 432 { |
433 int method_nr = crypt_get_method_nr(curbuf); | |
434 | |
435 if (cryptmethods[method_nr].self_test_fn == NULL) | |
436 return OK; | |
437 return cryptmethods[method_nr].self_test_fn(); | |
438 } | |
439 | |
440 /* | |
441 * 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
|
442 * Return NULL for failure. |
6122 | 443 */ |
444 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
445 crypt_create( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
446 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
447 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
448 char_u *salt, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
449 int salt_len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
450 char_u *seed, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
451 int seed_len) |
6122 | 452 { |
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
|
453 cryptstate_T *state = ALLOC_ONE(cryptstate_T); |
6122 | 454 |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
455 if (state == NULL) |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
456 return state; |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
457 |
6122 | 458 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
|
459 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
|
460 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
|
461 { |
28809
d0241e74bfdb
patch 8.2.4928: various white space and cosmetic mistakes
Bram Moolenaar <Bram@vim.org>
parents:
28457
diff
changeset
|
462 vim_free(state); |
d0241e74bfdb
patch 8.2.4928: various white space and cosmetic mistakes
Bram Moolenaar <Bram@vim.org>
parents:
28457
diff
changeset
|
463 return NULL; |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
464 } |
6122 | 465 return state; |
466 } | |
467 | |
468 /* | |
469 * Allocate a crypt state from a file header and initialize it. | |
470 * Assumes that header contains at least the number of bytes that | |
471 * crypt_get_header_len() returns for "method_nr". | |
472 */ | |
473 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
474 crypt_create_from_header( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
475 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
476 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
477 char_u *header) |
6122 | 478 { |
479 char_u *salt = NULL; | |
480 char_u *seed = NULL; | |
481 int salt_len = cryptmethods[method_nr].salt_len; | |
482 int seed_len = cryptmethods[method_nr].seed_len; | |
483 | |
484 if (salt_len > 0) | |
485 salt = header + CRYPT_MAGIC_LEN; | |
486 if (seed_len > 0) | |
487 seed = header + CRYPT_MAGIC_LEN + salt_len; | |
488 | |
489 return crypt_create(method_nr, key, salt, salt_len, seed, seed_len); | |
490 } | |
491 | |
492 /* | |
493 * Read the crypt method specific header data from "fp". | |
494 * Return an allocated cryptstate_T or NULL on error. | |
495 */ | |
496 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
497 crypt_create_from_file(FILE *fp, char_u *key) |
6122 | 498 { |
499 int method_nr; | |
500 int header_len; | |
501 char magic_buffer[CRYPT_MAGIC_LEN]; | |
502 char_u *buffer; | |
503 cryptstate_T *state; | |
504 | |
505 if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) | |
506 return NULL; | |
507 method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN); | |
508 if (method_nr < 0) | |
509 return NULL; | |
510 | |
511 header_len = crypt_get_header_len(method_nr); | |
512 if ((buffer = alloc(header_len)) == NULL) | |
513 return NULL; | |
514 mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN); | |
515 if (header_len > CRYPT_MAGIC_LEN | |
516 && fread(buffer + CRYPT_MAGIC_LEN, | |
517 header_len - CRYPT_MAGIC_LEN, 1, fp) != 1) | |
518 { | |
519 vim_free(buffer); | |
520 return NULL; | |
521 } | |
522 | |
523 state = crypt_create_from_header(method_nr, key, buffer); | |
524 vim_free(buffer); | |
525 return state; | |
526 } | |
527 | |
528 /* | |
529 * Allocate a cryptstate_T for writing and initialize it with "key". | |
530 * Allocates and fills in the header and stores it in "header", setting | |
531 * "header_len". The header may include salt and seed, depending on | |
532 * cryptmethod. Caller must free header. | |
533 * Returns the state or NULL on failure. | |
534 */ | |
535 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
536 crypt_create_for_writing( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
537 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
538 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
539 char_u **header, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
540 int *header_len) |
6122 | 541 { |
542 int len = crypt_get_header_len(method_nr); | |
543 char_u *salt = NULL; | |
544 char_u *seed = NULL; | |
545 int salt_len = cryptmethods[method_nr].salt_len; | |
546 int seed_len = cryptmethods[method_nr].seed_len; | |
547 cryptstate_T *state; | |
548 | |
549 *header_len = len; | |
550 *header = alloc(len); | |
551 if (*header == NULL) | |
552 return NULL; | |
553 | |
554 mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN); | |
555 if (salt_len > 0 || seed_len > 0) | |
556 { | |
557 if (salt_len > 0) | |
558 salt = *header + CRYPT_MAGIC_LEN; | |
559 if (seed_len > 0) | |
560 seed = *header + CRYPT_MAGIC_LEN + salt_len; | |
561 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
562 // 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
|
563 // 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
|
564 // 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
|
565 #ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
566 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
|
567 { |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
568 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
|
569 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
|
570 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
|
571 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
|
572 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
573 else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
574 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
575 sha2_seed(salt, salt_len, seed, seed_len); |
6122 | 576 } |
577 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len); | |
578 if (state == NULL) | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
579 VIM_CLEAR(*header); |
6122 | 580 return state; |
581 } | |
582 | |
583 /* | |
584 * Free the crypt state. | |
585 */ | |
586 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
587 crypt_free_state(cryptstate_T *state) |
6122 | 588 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
589 #ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
590 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
|
591 { |
25417
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
592 sodium_munlock(((sodium_state_T *)state->method_state)->key, |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
593 crypto_box_SEEDBYTES); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
594 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
|
595 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
|
596 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
597 else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
598 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
599 vim_free(state->method_state); |
6122 | 600 vim_free(state); |
601 } | |
602 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
603 #ifdef CRYPT_NOT_INPLACE |
6122 | 604 /* |
605 * Encode "from[len]" and store the result in a newly allocated buffer, which | |
606 * is stored in "newptr". | |
607 * Return number of bytes in "newptr", 0 for need more or -1 on error. | |
608 */ | |
609 long | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
610 crypt_encode_alloc( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
611 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
612 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
613 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
|
614 char_u **newptr, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
615 int last) |
6122 | 616 { |
617 cryptmethod_T *method = &cryptmethods[state->method_nr]; | |
618 | |
619 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
|
620 // 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
|
621 return method->encode_buffer_fn(state, from, len, newptr, last); |
6122 | 622 if (len == 0) |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
623 // Not buffering, just return EOF. |
6132 | 624 return (long)len; |
6122 | 625 |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
626 *newptr = alloc(len + 50); |
6122 | 627 if (*newptr == NULL) |
628 return -1; | |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
629 method->encode_fn(state, from, len, *newptr, last); |
6132 | 630 return (long)len; |
6122 | 631 } |
632 | |
633 /* | |
634 * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which | |
635 * is stored in "newptr". | |
636 * Return number of bytes in "newptr", 0 for need more or -1 on error. | |
637 */ | |
638 long | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
639 crypt_decode_alloc( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
640 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
641 char_u *ptr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
642 long len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
643 char_u **newptr, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
644 int last) |
6122 | 645 { |
646 cryptmethod_T *method = &cryptmethods[state->method_nr]; | |
647 | |
648 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
|
649 // 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
|
650 return method->decode_buffer_fn(state, ptr, len, newptr, last); |
6122 | 651 |
652 if (len == 0) | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
653 // Not buffering, just return EOF. |
6122 | 654 return len; |
655 | |
656 *newptr = alloc(len); | |
657 if (*newptr == NULL) | |
658 return -1; | |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
659 method->decode_fn(state, ptr, len, *newptr, last); |
6122 | 660 return len; |
661 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
662 #endif |
6122 | 663 |
664 /* | |
665 * Encrypting "from[len]" into "to[len]". | |
666 */ | |
667 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
668 crypt_encode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
669 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
670 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
671 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
|
672 char_u *to, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
673 int last) |
6122 | 674 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
675 cryptmethods[state->method_nr].encode_fn(state, from, len, to, last); |
6122 | 676 } |
677 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
678 #if 0 // unused |
6122 | 679 /* |
680 * decrypting "from[len]" into "to[len]". | |
681 */ | |
682 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
683 crypt_decode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
684 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
685 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
686 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
|
687 char_u *to, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
688 int last) |
6122 | 689 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
690 cryptmethods[state->method_nr].decode_fn(state, from, len, to, last); |
6122 | 691 } |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
692 #endif |
6122 | 693 |
694 /* | |
695 * Simple inplace encryption, modifies "buf[len]" in place. | |
696 */ | |
697 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
698 crypt_encode_inplace( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
699 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
700 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
|
701 size_t len, |
28809
d0241e74bfdb
patch 8.2.4928: various white space and cosmetic mistakes
Bram Moolenaar <Bram@vim.org>
parents:
28457
diff
changeset
|
702 int last) |
6122 | 703 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
704 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
|
705 buf, last); |
6122 | 706 } |
707 | |
708 /* | |
709 * Simple inplace decryption, modifies "buf[len]" in place. | |
710 */ | |
711 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
712 crypt_decode_inplace( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
713 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
714 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
|
715 size_t len, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
716 int last) |
6122 | 717 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
718 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
|
719 buf, last); |
6122 | 720 } |
721 | |
722 /* | |
723 * Free an allocated crypt key. Clear the text to make sure it doesn't stay | |
724 * in memory anywhere. | |
725 */ | |
726 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
727 crypt_free_key(char_u *key) |
6122 | 728 { |
729 char_u *p; | |
730 | |
731 if (key != NULL) | |
732 { | |
733 for (p = key; *p != NUL; ++p) | |
734 *p = 0; | |
735 vim_free(key); | |
736 } | |
737 } | |
738 | |
739 /* | |
6353 | 740 * Check the crypt method and give a warning if it's outdated. |
741 */ | |
742 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
743 crypt_check_method(int method) |
6353 | 744 { |
745 if (method < CRYPT_M_BF2) | |
746 { | |
747 msg_scroll = TRUE; | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
748 msg(_("Warning: Using a weak encryption method; see :help 'cm'")); |
6353 | 749 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
750 } |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
751 |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
752 #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
|
753 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
|
754 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
|
755 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
756 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
|
757 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
|
758 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
759 // 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
|
760 // 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
|
761 mf_close_file(curbuf, TRUE); // remove the swap file |
28457
4dcccb2673fe
patch 8.2.4753: error from setting an option is silently ignored
Bram Moolenaar <Bram@vim.org>
parents:
27998
diff
changeset
|
762 set_option_value_give_err((char_u *)"swf", 0, NULL, OPT_LOCAL); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
763 msg_scroll = TRUE; |
25362
68a7e6d70a5e
patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents:
24990
diff
changeset
|
764 msg(_("Note: Encryption of swapfile not supported, disabling swap file")); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
765 } |
6353 | 766 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
767 #endif |
6353 | 768 |
769 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
770 crypt_check_current_method(void) |
6353 | 771 { |
772 crypt_check_method(crypt_get_method_nr(curbuf)); | |
773 } | |
774 | |
775 /* | |
6122 | 776 * Ask the user for a crypt key. |
777 * When "store" is TRUE, the new key is stored in the 'key' option, and the | |
778 * 'key' option value is returned: Don't free it. | |
779 * When "store" is FALSE, the typed key is returned in allocated memory. | |
780 * Returns NULL on failure. | |
781 */ | |
782 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
783 crypt_get_key( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
784 int store, |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
785 int twice) // Ask for the key twice. |
6122 | 786 { |
787 char_u *p1, *p2 = NULL; | |
788 int round; | |
789 | |
790 for (round = 0; ; ++round) | |
791 { | |
792 cmdline_star = TRUE; | |
793 cmdline_row = msg_row; | |
794 p1 = getcmdline_prompt(NUL, round == 0 | |
795 ? (char_u *)_("Enter encryption key: ") | |
796 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING, | |
797 NULL); | |
798 cmdline_star = FALSE; | |
799 | |
800 if (p1 == NULL) | |
801 break; | |
802 | |
803 if (round == twice) | |
804 { | |
805 if (p2 != NULL && STRCMP(p1, p2) != 0) | |
806 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
807 msg(_("Keys don't match!")); |
6122 | 808 crypt_free_key(p1); |
809 crypt_free_key(p2); | |
810 p2 = NULL; | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
811 round = -1; // do it again |
6122 | 812 continue; |
813 } | |
814 | |
815 if (store) | |
816 { | |
28457
4dcccb2673fe
patch 8.2.4753: error from setting an option is silently ignored
Bram Moolenaar <Bram@vim.org>
parents:
27998
diff
changeset
|
817 set_option_value_give_err((char_u *)"key", 0L, p1, OPT_LOCAL); |
6122 | 818 crypt_free_key(p1); |
819 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
|
820 #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
|
821 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
|
822 #endif |
6122 | 823 } |
824 break; | |
825 } | |
826 p2 = p1; | |
827 } | |
828 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
829 // 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
|
830 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
|
831 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
832 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
|
833 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
|
834 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
|
835 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
|
836 } |
6122 | 837 |
838 crypt_free_key(p2); | |
839 return p1; | |
840 } | |
841 | |
842 | |
843 /* | |
844 * Append a message to IObuff for the encryption/decryption method being used. | |
845 */ | |
846 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
847 crypt_append_msg( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
848 buf_T *buf) |
6122 | 849 { |
850 if (crypt_get_method_nr(buf) == 0) | |
851 STRCAT(IObuff, _("[crypted]")); | |
852 else | |
853 { | |
854 STRCAT(IObuff, "["); | |
855 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); | |
856 STRCAT(IObuff, "]"); | |
857 } | |
858 } | |
859 | |
29320
a74398c432a4
patch 9.0.0003: functions are global while they could be local
Bram Moolenaar <Bram@vim.org>
parents:
28809
diff
changeset
|
860 static int |
31379
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
861 crypt_sodium_init_( |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
862 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
|
863 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
|
864 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
|
865 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
|
866 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
|
867 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
|
868 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
869 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
870 // 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
|
871 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
|
872 sodium_state_T *sd_state; |
25417
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
873 int retval = 0; |
24970
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 (sodium_init() < 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
876 return FAIL; |
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 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
|
879 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
|
880 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
881 // 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
|
882 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
|
883 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
|
884 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
|
885 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
886 // out of memory |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
887 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
|
888 return FAIL; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
889 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
890 memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES); |
25417
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
891 |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
892 retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
893 retval += sodium_mlock(key, STRLEN(key)); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
894 |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
895 if (retval < 0) |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
896 { |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
897 emsg(_(e_encryption_sodium_mlock_failed)); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
898 sodium_free(sd_state); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
899 return FAIL; |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
900 } |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
901 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
|
902 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
|
903 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
904 return OK; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
905 # else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
906 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
|
907 return FAIL; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
908 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
909 } |
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 /* |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
912 * 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
|
913 * "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
|
914 * 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
|
915 */ |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
916 #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
|
917 void |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
918 crypt_sodium_encode( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
919 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
|
920 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
|
921 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
|
922 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
|
923 int last UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
924 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
925 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
926 // 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
|
927 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
|
928 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
|
929 ? 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
|
930 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
931 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
|
932 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
933 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
|
934 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
935 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
|
936 return; |
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 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
|
939 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
|
940 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
|
941 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
942 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
943 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
|
944 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
945 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
|
946 return; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
947 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
948 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
949 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
|
950 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
|
951 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
952 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
953 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
954 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
955 #endif |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
956 |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
957 /* |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
958 * 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
|
959 * "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
|
960 */ |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
961 #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
|
962 void |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
963 crypt_sodium_decode( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
964 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
|
965 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
|
966 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
|
967 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
|
968 int last UNUSED) |
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 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
971 // 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
|
972 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
|
973 unsigned char tag; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
974 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
|
975 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
|
976 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
|
977 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
|
978 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
979 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
|
980 && 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
|
981 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
982 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
|
983 return; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
984 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
985 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
986 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
|
987 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
988 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
|
989 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
990 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
|
991 return; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
992 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
993 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
|
994 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
995 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
|
996 &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
|
997 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
998 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
|
999 goto fail; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1000 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1001 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1002 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
|
1003 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
|
1004 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1005 if (p1 == p2) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1006 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
|
1007 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1008 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1009 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
|
1010 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1011 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
|
1012 goto fail; |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1013 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1014 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
|
1015 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
|
1016 { |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
1017 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
|
1018 goto fail; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1019 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1020 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1021 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1022 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
|
1023 { |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
1024 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
|
1025 goto fail; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1026 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1027 if (p1 == p2) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1028 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
|
1029 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1030 fail: |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1031 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
|
1032 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1033 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
1034 #endif |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1035 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1036 /* |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1037 * 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
|
1038 * "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
|
1039 */ |
29320
a74398c432a4
patch 9.0.0003: functions are global while they could be local
Bram Moolenaar <Bram@vim.org>
parents:
28809
diff
changeset
|
1040 static long |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1041 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
|
1042 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
|
1043 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
|
1044 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
|
1045 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
|
1046 int last UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1047 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1048 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1049 // 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
|
1050 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
|
1051 char_u *ptr; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1052 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
|
1053 ? 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
|
1054 int length; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1055 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
|
1056 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
|
1057 |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
1058 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
|
1059 + (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
|
1060 *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
|
1061 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
|
1062 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1063 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
|
1064 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1065 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1066 ptr = *buf_out; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1067 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1068 if (first) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1069 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1070 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
|
1071 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
|
1072 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
|
1073 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1074 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1075 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
|
1076 &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
|
1077 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1078 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1079 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
|
1080 ? 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
|
1081 # else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1082 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1083 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1084 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1085 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1086 /* |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1087 * 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
|
1088 * "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
|
1089 */ |
29320
a74398c432a4
patch 9.0.0003: functions are global while they could be local
Bram Moolenaar <Bram@vim.org>
parents:
28809
diff
changeset
|
1090 static long |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1091 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
|
1092 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
|
1093 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
|
1094 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
|
1095 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
|
1096 int last UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1097 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1098 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1099 // 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
|
1100 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
|
1101 unsigned char tag; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1102 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
|
1103 *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
|
1104 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
|
1105 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1106 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
|
1107 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1108 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1109 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1110 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
|
1111 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1112 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
|
1113 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
|
1114 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1115 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
|
1116 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1117 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1118 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
|
1119 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
|
1120 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1121 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1122 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
|
1123 *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
|
1124 { |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
1125 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
|
1126 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1127 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1128 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1129 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
|
1130 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
|
1131 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
|
1132 # else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1133 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1134 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1135 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
1136 |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1137 # if defined(FEAT_SODIUM) || defined(PROTO) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1138 int |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1139 crypt_sodium_munlock(void *const addr, const size_t len) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1140 { |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1141 return sodium_munlock(addr, len); |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1142 } |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1143 |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1144 void |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1145 crypt_sodium_randombytes_buf(void *const buf, const size_t size) |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1146 { |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1147 randombytes_buf(buf, size); |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1148 } |
31379
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1149 |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1150 int |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1151 crypt_sodium_init(void) |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1152 { |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1153 return sodium_init(); |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1154 } |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1155 |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1156 uint32_t |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1157 crypt_sodium_randombytes_random(void) |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1158 { |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1159 return randombytes_random(); |
540e85ac14c9
patch 9.0.1023: MS-Windows: dynamic loading of libsodium doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
29320
diff
changeset
|
1160 } |
27231
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1161 # endif |
e1cedf009920
patch 8.2.4144: cannot load libsodium dynamically
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
1162 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
1163 #endif // FEAT_CRYPT |