Mirror of the Vim source from https://github.com/vim/vim
リビジョン | e1cedf00992028931e579ebb50be8b8ddb95bd1f (tree) |
---|---|
日時 | 2022-01-19 22:45:05 |
作者 | Bram Moolenaar <Bram@vim....> |
コミッター | Bram Moolenaar |
patch 8.2.4144: cannot load libsodium dynamically
Commit: https://github.com/vim/vim/commit/1a8825d7a3484d76ca16ea2aa9769cadca7758a4
Author: K.Takata <kentkt@csc.jp>
Date: Wed Jan 19 13:32:57 2022 +0000
@@ -668,7 +668,14 @@ | ||
668 | 668 | endif |
669 | 669 | |
670 | 670 | ifeq ($(SODIUM),yes) |
671 | + ifndef DYNAMIC_SODIUM | |
672 | +DYNAMIC_SODIUM=yes | |
673 | + endif | |
674 | + ifeq ($(DYNAMIC_SODIUM),yes) | |
675 | +DEFINES += -DDYNAMIC_SODIUM | |
676 | + else | |
671 | 677 | SODIUMLIB = -lsodium |
678 | + endif | |
672 | 679 | endif |
673 | 680 | |
674 | 681 | # Only allow XPM for a GUI build. |
@@ -42,10 +42,10 @@ | ||
42 | 42 | # Sound support: SOUND=yes (default is yes) |
43 | 43 | # |
44 | 44 | # Sodium support: SODIUM=[Path to Sodium directory] |
45 | -# Dynamic built with libsodium | |
46 | -# You need to install the msvc package from | |
47 | -# https://download.libsodium.org/libsodium/releases/ | |
48 | -# and package the libsodium.dll with Vim | |
45 | +# DYNAMIC_SODIUM=yes (to load the Sodium DLL dynamically) | |
46 | +# You need to install the msvc package from | |
47 | +# https://download.libsodium.org/libsodium/releases/ | |
48 | +# and package the libsodium.dll with Vim | |
49 | 49 | # |
50 | 50 | # |
51 | 51 | # DLL support (EXPERIMENTAL): VIMDLL=yes (default is no) |
@@ -384,6 +384,9 @@ | ||
384 | 384 | !ifndef SODIUM |
385 | 385 | SODIUM = no |
386 | 386 | !endif |
387 | +!ifndef DYNAMIC_SODIUM | |
388 | +DYNAMIC_SODIUM = yes | |
389 | +!endif | |
387 | 390 | |
388 | 391 | !if "$(SODIUM)" != "no" |
389 | 392 | ! if "$(CPU)" == "AMD64" |
@@ -397,8 +400,13 @@ | ||
397 | 400 | |
398 | 401 | !if "$(SODIUM)" != "no" |
399 | 402 | SOD_INC = /I "$(SODIUM)\include" |
403 | +! if "$(DYNAMIC_SODIUM)" == "yes" | |
404 | +SOD_DEFS = -DHAVE_SODIUM -DDYNAMIC_SODIUM | |
405 | +SOD_LIB = | |
406 | +! else | |
400 | 407 | SOD_DEFS = -DHAVE_SODIUM |
401 | 408 | SOD_LIB = $(SOD_LIB)\libsodium.lib |
409 | +! endif | |
402 | 410 | !endif |
403 | 411 | |
404 | 412 | !ifndef NETBEANS |
@@ -2269,8 +2269,9 @@ | ||
2269 | 2269 | #endif |
2270 | 2270 | #ifdef FEAT_CRYPT |
2271 | 2271 | # ifdef FEAT_SODIUM |
2272 | - if (buf->b_p_key != NULL && (crypt_get_method_nr(buf) == CRYPT_M_SOD)) | |
2273 | - sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key)); | |
2272 | + if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) && | |
2273 | + (crypt_get_method_nr(buf) == CRYPT_M_SOD)) | |
2274 | + crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key)); | |
2274 | 2275 | # endif |
2275 | 2276 | clear_string_option(&buf->b_p_key); |
2276 | 2277 | #endif |
@@ -159,6 +159,108 @@ | ||
159 | 159 | crypto_secretstream_xchacha20poly1305_state |
160 | 160 | state; |
161 | 161 | } sodium_state_T; |
162 | + | |
163 | + | |
164 | +# ifdef DYNAMIC_SODIUM | |
165 | +# define sodium_init load_sodium | |
166 | +# define sodium_free dll_sodium_free | |
167 | +# define sodium_malloc dll_sodium_malloc | |
168 | +# define sodium_memzero dll_sodium_memzero | |
169 | +# define sodium_mlock dll_sodium_mlock | |
170 | +# define sodium_munlock dll_sodium_munlock | |
171 | +# define crypto_secretstream_xchacha20poly1305_init_push \ | |
172 | + dll_crypto_secretstream_xchacha20poly1305_init_push | |
173 | +# define crypto_secretstream_xchacha20poly1305_push \ | |
174 | + dll_crypto_secretstream_xchacha20poly1305_push | |
175 | +# define crypto_secretstream_xchacha20poly1305_init_pull \ | |
176 | + dll_crypto_secretstream_xchacha20poly1305_init_pull | |
177 | +# define crypto_secretstream_xchacha20poly1305_pull \ | |
178 | + dll_crypto_secretstream_xchacha20poly1305_pull | |
179 | +# define crypto_pwhash dll_crypto_pwhash | |
180 | +# define randombytes_buf dll_randombytes_buf | |
181 | + | |
182 | +static int (*dll_sodium_init)(void) = NULL; | |
183 | +static void (*dll_sodium_free)(void *) = NULL; | |
184 | +static void *(*dll_sodium_malloc)(const size_t) = NULL; | |
185 | +static void (*dll_sodium_memzero)(void * const, const size_t) = NULL; | |
186 | +static int (*dll_sodium_mlock)(void * const, const size_t) = NULL; | |
187 | +static int (*dll_sodium_munlock)(void * const, const size_t) = NULL; | |
188 | +static int (*dll_crypto_secretstream_xchacha20poly1305_init_push) | |
189 | + (crypto_secretstream_xchacha20poly1305_state *state, | |
190 | + unsigned char [], | |
191 | + const unsigned char []) = NULL; | |
192 | +static int (*dll_crypto_secretstream_xchacha20poly1305_push) | |
193 | + (crypto_secretstream_xchacha20poly1305_state *state, | |
194 | + unsigned char *c, unsigned long long *clen_p, | |
195 | + const unsigned char *m, unsigned long long mlen, | |
196 | + const unsigned char *ad, unsigned long long adlen, unsigned char tag) | |
197 | + = NULL; | |
198 | +static int (*dll_crypto_secretstream_xchacha20poly1305_init_pull) | |
199 | + (crypto_secretstream_xchacha20poly1305_state *state, | |
200 | + const unsigned char [], | |
201 | + const unsigned char []) = NULL; | |
202 | +static int (*dll_crypto_secretstream_xchacha20poly1305_pull) | |
203 | + (crypto_secretstream_xchacha20poly1305_state *state, | |
204 | + unsigned char *m, unsigned long long *mlen_p, unsigned char *tag_p, | |
205 | + const unsigned char *c, unsigned long long clen, | |
206 | + const unsigned char *ad, unsigned long long adlen) = NULL; | |
207 | +static int (*dll_crypto_pwhash)(unsigned char * const out, | |
208 | + unsigned long long outlen, | |
209 | + const char * const passwd, unsigned long long passwdlen, | |
210 | + const unsigned char * const salt, | |
211 | + unsigned long long opslimit, size_t memlimit, int alg) | |
212 | + = NULL; | |
213 | +static void (*dll_randombytes_buf)(void * const buf, const size_t size); | |
214 | + | |
215 | +static struct { | |
216 | + const char *name; | |
217 | + FARPROC *ptr; | |
218 | +} sodium_funcname_table[] = { | |
219 | + {"sodium_init", (FARPROC*)&dll_sodium_init}, | |
220 | + {"sodium_free", (FARPROC*)&dll_sodium_free}, | |
221 | + {"sodium_malloc", (FARPROC*)&dll_sodium_malloc}, | |
222 | + {"sodium_memzero", (FARPROC*)&dll_sodium_memzero}, | |
223 | + {"sodium_mlock", (FARPROC*)&dll_sodium_mlock}, | |
224 | + {"sodium_munlock", (FARPROC*)&dll_sodium_munlock}, | |
225 | + {"crypto_secretstream_xchacha20poly1305_init_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, | |
226 | + {"crypto_secretstream_xchacha20poly1305_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, | |
227 | + {"crypto_secretstream_xchacha20poly1305_init_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, | |
228 | + {"crypto_secretstream_xchacha20poly1305_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, | |
229 | + {"crypto_pwhash", (FARPROC*)&dll_crypto_pwhash}, | |
230 | + {"randombytes_buf", (FARPROC*)&dll_randombytes_buf}, | |
231 | + {NULL, NULL} | |
232 | +}; | |
233 | + | |
234 | + static int | |
235 | +load_sodium(void) | |
236 | +{ | |
237 | + static HANDLE hsodium = NULL; | |
238 | + int i; | |
239 | + | |
240 | + if (hsodium != NULL) | |
241 | + return 0; | |
242 | + | |
243 | + hsodium = vimLoadLib("libsodium.dll"); | |
244 | + if (hsodium == NULL) | |
245 | + { | |
246 | + // TODO: Show error message. | |
247 | + return -1; | |
248 | + } | |
249 | + | |
250 | + for (i = 0; sodium_funcname_table[i].ptr; ++i) | |
251 | + { | |
252 | + if ((*sodium_funcname_table[i].ptr = GetProcAddress(hsodium, | |
253 | + sodium_funcname_table[i].name)) == NULL) | |
254 | + { | |
255 | + FreeLibrary(hsodium); | |
256 | + hsodium = NULL; | |
257 | + // TODO: Show error message. | |
258 | + return -1; | |
259 | + } | |
260 | + } | |
261 | + return dll_sodium_init(); | |
262 | +} | |
263 | +# endif | |
162 | 264 | #endif |
163 | 265 | |
164 | 266 | #define CRYPT_MAGIC_LEN 12 // cannot change |
@@ -990,4 +1092,18 @@ | ||
990 | 1092 | # endif |
991 | 1093 | } |
992 | 1094 | |
1095 | +# if defined(FEAT_SODIUM) || defined(PROTO) | |
1096 | + int | |
1097 | +crypt_sodium_munlock(void *const addr, const size_t len) | |
1098 | +{ | |
1099 | + return sodium_munlock(addr, len); | |
1100 | +} | |
1101 | + | |
1102 | + void | |
1103 | +crypt_sodium_randombytes_buf(void *const buf, const size_t size) | |
1104 | +{ | |
1105 | + randombytes_buf(buf, size); | |
1106 | +} | |
1107 | +# endif | |
1108 | + | |
993 | 1109 | #endif // FEAT_CRYPT |
@@ -436,7 +436,8 @@ | ||
436 | 436 | } |
437 | 437 | #ifdef FEAT_SODIUM |
438 | 438 | else if (method_nr == CRYPT_M_SOD) |
439 | - randombytes_buf(buf->b_ml.ml_mfp->mf_seed, MF_SEED_LEN); | |
439 | + crypt_sodium_randombytes_buf(buf->b_ml.ml_mfp->mf_seed, | |
440 | + MF_SEED_LEN); | |
440 | 441 | #endif |
441 | 442 | } |
442 | 443 | } |
@@ -26,4 +26,6 @@ | ||
26 | 26 | int crypt_sodium_init(cryptstate_T *state, char_u *key, char_u *salt, int salt_len, char_u *seed, int seed_len); |
27 | 27 | long crypt_sodium_buffer_encode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); |
28 | 28 | long crypt_sodium_buffer_decode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); |
29 | +int crypt_sodium_munlock(void *const addr, const size_t len); | |
30 | +void crypt_sodium_randombytes_buf(void *const buf, const size_t size); | |
29 | 31 | /* vim: set ft=c : */ |
@@ -751,6 +751,8 @@ | ||
751 | 751 | static int included_patches[] = |
752 | 752 | { /* Add new patch number below this line */ |
753 | 753 | /**/ |
754 | + 4144, | |
755 | +/**/ | |
754 | 756 | 4143, |
755 | 757 | /**/ |
756 | 758 | 4142, |