To: vim_dev@googlegroups.com Subject: Patch 8.2.4144 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.4144 Problem: Cannot load libsodium dynamically. Solution: Support dynamic loading on MS-Windows. (Ken Takata, closes #9554) Files: src/Make_cyg_ming.mak, src/Make_mvc.mak, src/buffer.c, src/crypt.c, src/memline.c, src/proto/crypt.pro *** ../vim-8.2.4143/src/Make_cyg_ming.mak 2022-01-13 18:09:08.241764850 +0000 --- src/Make_cyg_ming.mak 2022-01-19 13:30:04.695277499 +0000 *************** *** 668,674 **** --- 668,681 ---- endif ifeq ($(SODIUM),yes) + ifndef DYNAMIC_SODIUM + DYNAMIC_SODIUM=yes + endif + ifeq ($(DYNAMIC_SODIUM),yes) + DEFINES += -DDYNAMIC_SODIUM + else SODIUMLIB = -lsodium + endif endif # Only allow XPM for a GUI build. *** ../vim-8.2.4143/src/Make_mvc.mak 2022-01-18 11:34:52.889893556 +0000 --- src/Make_mvc.mak 2022-01-19 13:30:04.695277499 +0000 *************** *** 42,51 **** # Sound support: SOUND=yes (default is yes) # # Sodium support: SODIUM=[Path to Sodium directory] ! # Dynamic built with libsodium ! # You need to install the msvc package from ! # https://download.libsodium.org/libsodium/releases/ ! # and package the libsodium.dll with Vim # # # DLL support (EXPERIMENTAL): VIMDLL=yes (default is no) --- 42,51 ---- # Sound support: SOUND=yes (default is yes) # # Sodium support: SODIUM=[Path to Sodium directory] ! # DYNAMIC_SODIUM=yes (to load the Sodium DLL dynamically) ! # You need to install the msvc package from ! # https://download.libsodium.org/libsodium/releases/ ! # and package the libsodium.dll with Vim # # # DLL support (EXPERIMENTAL): VIMDLL=yes (default is no) *************** *** 384,389 **** --- 384,392 ---- !ifndef SODIUM SODIUM = no !endif + !ifndef DYNAMIC_SODIUM + DYNAMIC_SODIUM = yes + !endif !if "$(SODIUM)" != "no" ! if "$(CPU)" == "AMD64" *************** *** 397,404 **** --- 400,412 ---- !if "$(SODIUM)" != "no" SOD_INC = /I "$(SODIUM)\include" + ! if "$(DYNAMIC_SODIUM)" == "yes" + SOD_DEFS = -DHAVE_SODIUM -DDYNAMIC_SODIUM + SOD_LIB = + ! else SOD_DEFS = -DHAVE_SODIUM SOD_LIB = $(SOD_LIB)\libsodium.lib + ! endif !endif !ifndef NETBEANS *** ../vim-8.2.4143/src/buffer.c 2022-01-14 20:11:34.917647651 +0000 --- src/buffer.c 2022-01-19 13:30:04.695277499 +0000 *************** *** 2269,2276 **** #endif #ifdef FEAT_CRYPT # ifdef FEAT_SODIUM ! if (buf->b_p_key != NULL && (crypt_get_method_nr(buf) == CRYPT_M_SOD)) ! sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key)); # endif clear_string_option(&buf->b_p_key); #endif --- 2269,2277 ---- #endif #ifdef FEAT_CRYPT # ifdef FEAT_SODIUM ! if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) && ! (crypt_get_method_nr(buf) == CRYPT_M_SOD)) ! crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key)); # endif clear_string_option(&buf->b_p_key); #endif *** ../vim-8.2.4143/src/crypt.c 2022-01-08 12:41:12.200795557 +0000 --- src/crypt.c 2022-01-19 13:30:04.695277499 +0000 *************** *** 159,164 **** --- 159,266 ---- crypto_secretstream_xchacha20poly1305_state state; } sodium_state_T; + + + # ifdef DYNAMIC_SODIUM + # define sodium_init load_sodium + # define sodium_free dll_sodium_free + # define sodium_malloc dll_sodium_malloc + # define sodium_memzero dll_sodium_memzero + # define sodium_mlock dll_sodium_mlock + # define sodium_munlock dll_sodium_munlock + # define crypto_secretstream_xchacha20poly1305_init_push \ + dll_crypto_secretstream_xchacha20poly1305_init_push + # define crypto_secretstream_xchacha20poly1305_push \ + dll_crypto_secretstream_xchacha20poly1305_push + # define crypto_secretstream_xchacha20poly1305_init_pull \ + dll_crypto_secretstream_xchacha20poly1305_init_pull + # define crypto_secretstream_xchacha20poly1305_pull \ + dll_crypto_secretstream_xchacha20poly1305_pull + # define crypto_pwhash dll_crypto_pwhash + # define randombytes_buf dll_randombytes_buf + + static int (*dll_sodium_init)(void) = NULL; + static void (*dll_sodium_free)(void *) = NULL; + static void *(*dll_sodium_malloc)(const size_t) = NULL; + static void (*dll_sodium_memzero)(void * const, const size_t) = NULL; + static int (*dll_sodium_mlock)(void * const, const size_t) = NULL; + static int (*dll_sodium_munlock)(void * const, const size_t) = NULL; + static int (*dll_crypto_secretstream_xchacha20poly1305_init_push) + (crypto_secretstream_xchacha20poly1305_state *state, + unsigned char [], + const unsigned char []) = NULL; + static int (*dll_crypto_secretstream_xchacha20poly1305_push) + (crypto_secretstream_xchacha20poly1305_state *state, + unsigned char *c, unsigned long long *clen_p, + const unsigned char *m, unsigned long long mlen, + const unsigned char *ad, unsigned long long adlen, unsigned char tag) + = NULL; + static int (*dll_crypto_secretstream_xchacha20poly1305_init_pull) + (crypto_secretstream_xchacha20poly1305_state *state, + const unsigned char [], + const unsigned char []) = NULL; + static int (*dll_crypto_secretstream_xchacha20poly1305_pull) + (crypto_secretstream_xchacha20poly1305_state *state, + unsigned char *m, unsigned long long *mlen_p, unsigned char *tag_p, + const unsigned char *c, unsigned long long clen, + const unsigned char *ad, unsigned long long adlen) = NULL; + static int (*dll_crypto_pwhash)(unsigned char * const out, + unsigned long long outlen, + const char * const passwd, unsigned long long passwdlen, + const unsigned char * const salt, + unsigned long long opslimit, size_t memlimit, int alg) + = NULL; + static void (*dll_randombytes_buf)(void * const buf, const size_t size); + + static struct { + const char *name; + FARPROC *ptr; + } sodium_funcname_table[] = { + {"sodium_init", (FARPROC*)&dll_sodium_init}, + {"sodium_free", (FARPROC*)&dll_sodium_free}, + {"sodium_malloc", (FARPROC*)&dll_sodium_malloc}, + {"sodium_memzero", (FARPROC*)&dll_sodium_memzero}, + {"sodium_mlock", (FARPROC*)&dll_sodium_mlock}, + {"sodium_munlock", (FARPROC*)&dll_sodium_munlock}, + {"crypto_secretstream_xchacha20poly1305_init_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, + {"crypto_secretstream_xchacha20poly1305_push", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, + {"crypto_secretstream_xchacha20poly1305_init_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, + {"crypto_secretstream_xchacha20poly1305_pull", (FARPROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, + {"crypto_pwhash", (FARPROC*)&dll_crypto_pwhash}, + {"randombytes_buf", (FARPROC*)&dll_randombytes_buf}, + {NULL, NULL} + }; + + static int + load_sodium(void) + { + static HANDLE hsodium = NULL; + int i; + + if (hsodium != NULL) + return 0; + + hsodium = vimLoadLib("libsodium.dll"); + if (hsodium == NULL) + { + // TODO: Show error message. + return -1; + } + + for (i = 0; sodium_funcname_table[i].ptr; ++i) + { + if ((*sodium_funcname_table[i].ptr = GetProcAddress(hsodium, + sodium_funcname_table[i].name)) == NULL) + { + FreeLibrary(hsodium); + hsodium = NULL; + // TODO: Show error message. + return -1; + } + } + return dll_sodium_init(); + } + # endif #endif #define CRYPT_MAGIC_LEN 12 // cannot change *************** *** 990,993 **** --- 1092,1109 ---- # endif } + # if defined(FEAT_SODIUM) || defined(PROTO) + int + crypt_sodium_munlock(void *const addr, const size_t len) + { + return sodium_munlock(addr, len); + } + + void + crypt_sodium_randombytes_buf(void *const buf, const size_t size) + { + randombytes_buf(buf, size); + } + # endif + #endif // FEAT_CRYPT *** ../vim-8.2.4143/src/memline.c 2022-01-05 17:49:10.877225131 +0000 --- src/memline.c 2022-01-19 13:30:04.699277417 +0000 *************** *** 436,442 **** } #ifdef FEAT_SODIUM else if (method_nr == CRYPT_M_SOD) ! randombytes_buf(buf->b_ml.ml_mfp->mf_seed, MF_SEED_LEN); #endif } } --- 436,443 ---- } #ifdef FEAT_SODIUM else if (method_nr == CRYPT_M_SOD) ! crypt_sodium_randombytes_buf(buf->b_ml.ml_mfp->mf_seed, ! MF_SEED_LEN); #endif } } *** ../vim-8.2.4143/src/proto/crypt.pro 2021-06-21 20:08:04.928547486 +0100 --- src/proto/crypt.pro 2022-01-19 13:30:04.699277417 +0000 *************** *** 26,29 **** --- 26,31 ---- int crypt_sodium_init(cryptstate_T *state, char_u *key, char_u *salt, int salt_len, char_u *seed, int seed_len); long crypt_sodium_buffer_encode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); long crypt_sodium_buffer_decode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); + int crypt_sodium_munlock(void *const addr, const size_t len); + void crypt_sodium_randombytes_buf(void *const buf, const size_t size); /* vim: set ft=c : */ *** ../vim-8.2.4143/src/version.c 2022-01-19 12:59:18.198810793 +0000 --- src/version.c 2022-01-19 13:31:24.537666286 +0000 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 4144, /**/ -- Advice to worms: Sleep late. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///