comparison src/crypt.c @ 27231:e1cedf009920 v8.2.4144

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 patch 8.2.4144: cannot load libsodium dynamically Problem: Cannot load libsodium dynamically. Solution: Support dynamic loading on MS-Windows. (Ken Takata, closes https://github.com/vim/vim/issues/9554)
author Bram Moolenaar <Bram@vim.org>
date Wed, 19 Jan 2022 14:45:05 +0100
parents 268f6a3511df
children a077948be0f4
comparison
equal deleted inserted replaced
27230:c405d50702b1 27231:e1cedf009920
157 unsigned char key[crypto_box_SEEDBYTES]; 157 unsigned char key[crypto_box_SEEDBYTES];
158 // 32, same as crypto_secretstream_xchacha20poly1305_KEYBYTES 158 // 32, same as crypto_secretstream_xchacha20poly1305_KEYBYTES
159 crypto_secretstream_xchacha20poly1305_state 159 crypto_secretstream_xchacha20poly1305_state
160 state; 160 state;
161 } sodium_state_T; 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 #endif 264 #endif
163 265
164 #define CRYPT_MAGIC_LEN 12 // cannot change 266 #define CRYPT_MAGIC_LEN 12 // cannot change
165 static char crypt_magic_head[] = "VimCrypt~"; 267 static char crypt_magic_head[] = "VimCrypt~";
166 268
988 # else 1090 # else
989 return -1; 1091 return -1;
990 # endif 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 #endif // FEAT_CRYPT 1109 #endif // FEAT_CRYPT