Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 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 { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 33 | char *name; // encryption name as used in 'cryptmethod' |
| 34 | char *magic; // magic bytes stored in file header |
| 35 | int salt_len; // length of salt, or 0 when not using salt |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 36 | int seed_len; // length of seed, or 0 when not using seed |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 37 | int add_len; // additional length in the header needed for storing |
| 38 | // custom data |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 39 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 40 | int works_inplace; // encryption/decryption can be done in-place |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 41 | #endif |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 42 | int whole_undofile; // whole undo file is encrypted |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 43 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 44 | // Optional function pointer for a self-test. |
Michael Jarvis | be9624e | 2023-04-19 20:28:48 +0100 | [diff] [blame] | 45 | int (* self_test_fn)(void); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 46 | |
Bram Moolenaar | ad3ec76 | 2019-04-21 00:00:13 +0200 | [diff] [blame] | 47 | // Function pointer for initializing encryption/decryption. |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 48 | int (* init_fn)(cryptstate_T *state, char_u *key, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 49 | crypt_arg_T *arg); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 50 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 51 | // Function pointers for encoding/decoding from one buffer into another. |
| 52 | // Optional, however, these or the _buffer ones should be configured. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 53 | void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 54 | char_u *to, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 55 | void (*decode_fn)(cryptstate_T *state, char_u *from, size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 56 | char_u *to, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 57 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 58 | // Function pointers for encoding and decoding, can buffer data if needed. |
| 59 | // Optional (however, these or the above should be configured). |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 60 | long (*encode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 61 | char_u **newptr, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 62 | long (*decode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 63 | char_u **newptr, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 64 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 65 | // Function pointers for in-place encoding and decoding, used for |
| 66 | // crypt_*_inplace(). "from" and "to" arguments will be equal. |
| 67 | // These may be the same as decode_fn and encode_fn above, however an |
| 68 | // algorithm may implement them in a way that is not interchangeable with |
| 69 | // the crypt_(en|de)code() interface (for example because it wishes to add |
| 70 | // padding to files). |
| 71 | // This method is used for swap and undo files which have a rigid format. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 72 | void (*encode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 73 | char_u *p2, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 74 | void (*decode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 75 | char_u *p2, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 76 | } cryptmethod_T; |
| 77 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 78 | static int crypt_sodium_init_(cryptstate_T *state, char_u *key, crypt_arg_T *arg); |
Yegappan Lakshmanan | ee47eac | 2022-06-29 12:55:36 +0100 | [diff] [blame] | 79 | static long crypt_sodium_buffer_decode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); |
| 80 | static long crypt_sodium_buffer_encode(cryptstate_T *state, char_u *from, size_t len, char_u **buf_out, int last); |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 81 | #if defined(FEAT_EVAL) && defined(FEAT_SODIUM) |
| 82 | static void crypt_sodium_report_hash_params( unsigned long long opslimit, unsigned long long ops_def, size_t memlimit, size_t mem_def, int alg, int alg_def); |
| 83 | #endif |
Yegappan Lakshmanan | ee47eac | 2022-06-29 12:55:36 +0100 | [diff] [blame] | 84 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 85 | // index is method_nr of cryptstate_T, CRYPT_M_* |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 86 | static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 87 | // PK_Zip; very weak |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 88 | { |
| 89 | "zip", |
| 90 | "VimCrypt~01!", |
| 91 | 0, |
| 92 | 0, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 93 | 0, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 94 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 95 | TRUE, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 96 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 97 | FALSE, |
| 98 | NULL, |
| 99 | crypt_zip_init, |
| 100 | crypt_zip_encode, crypt_zip_decode, |
| 101 | NULL, NULL, |
| 102 | crypt_zip_encode, crypt_zip_decode, |
| 103 | }, |
| 104 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 105 | // Blowfish/CFB + SHA-256 custom key derivation; implementation issues. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 106 | { |
| 107 | "blowfish", |
| 108 | "VimCrypt~02!", |
| 109 | 8, |
| 110 | 8, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 111 | 0, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 112 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 113 | TRUE, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 114 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 115 | FALSE, |
| 116 | blowfish_self_test, |
| 117 | crypt_blowfish_init, |
| 118 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 119 | NULL, NULL, |
| 120 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 121 | }, |
| 122 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 123 | // Blowfish/CFB + SHA-256 custom key derivation; fixed. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 124 | { |
| 125 | "blowfish2", |
| 126 | "VimCrypt~03!", |
| 127 | 8, |
| 128 | 8, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 129 | 0, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 130 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 131 | TRUE, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 132 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 133 | TRUE, |
| 134 | blowfish_self_test, |
| 135 | crypt_blowfish_init, |
| 136 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 137 | NULL, NULL, |
| 138 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 139 | }, |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 140 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 141 | // XChaCha20 using libsodium; implementation issues |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 142 | { |
| 143 | "xchacha20", |
| 144 | "VimCrypt~04!", |
| 145 | #ifdef FEAT_SODIUM |
| 146 | crypto_pwhash_argon2id_SALTBYTES, // 16 |
| 147 | #else |
| 148 | 16, |
| 149 | #endif |
| 150 | 8, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 151 | 0, |
| 152 | #ifdef CRYPT_NOT_INPLACE |
| 153 | FALSE, |
| 154 | #endif |
| 155 | FALSE, |
| 156 | NULL, |
| 157 | crypt_sodium_init_, |
| 158 | NULL, NULL, |
| 159 | crypt_sodium_buffer_encode, crypt_sodium_buffer_decode, |
| 160 | NULL, NULL, |
| 161 | }, |
| 162 | // XChaCha20 using libsodium; stores parameters in header |
| 163 | { |
| 164 | "xchacha20v2", |
| 165 | "VimCrypt~05!", |
| 166 | #ifdef FEAT_SODIUM |
| 167 | crypto_pwhash_argon2id_SALTBYTES, // 16 |
| 168 | #else |
| 169 | 16, |
| 170 | #endif |
| 171 | 8, |
| 172 | // sizeof(crypto_pwhash_OPSLIMIT_INTERACTIVE + crypto_pwhash_MEMLIMIT_INTERACTIVE + crypto_pwhash_ALG_DEFAULT) |
| 173 | 20, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 174 | #ifdef CRYPT_NOT_INPLACE |
| 175 | FALSE, |
| 176 | #endif |
| 177 | FALSE, |
| 178 | NULL, |
K.Takata | a8cdb4e | 2022-12-06 16:17:01 +0000 | [diff] [blame] | 179 | crypt_sodium_init_, |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 180 | NULL, NULL, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 181 | crypt_sodium_buffer_encode, crypt_sodium_buffer_decode, |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 182 | NULL, NULL, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 183 | }, |
| 184 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 185 | // NOTE: when adding a new method, use some random bytes for the magic key, |
| 186 | // to avoid that a text file is recognized as encrypted. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 187 | }; |
| 188 | |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 189 | #if defined(FEAT_SODIUM) || defined(PROTO) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 190 | typedef struct { |
| 191 | size_t count; |
| 192 | unsigned char key[crypto_box_SEEDBYTES]; |
| 193 | // 32, same as crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 194 | crypto_secretstream_xchacha20poly1305_state |
| 195 | state; |
| 196 | } sodium_state_T; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 197 | |
| 198 | |
| 199 | # ifdef DYNAMIC_SODIUM |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 200 | # ifdef MSWIN |
| 201 | # define SODIUM_PROC FARPROC |
| 202 | # define load_dll vimLoadLib |
| 203 | # define symbol_from_dll GetProcAddress |
| 204 | # define close_dll FreeLibrary |
| 205 | # define load_dll_error GetWin32Error |
| 206 | # else |
| 207 | # error Dynamic loading of libsodium is not supported for now. |
| 208 | //# define HINSTANCE void* |
| 209 | //# define SODIUM_PROC void* |
| 210 | //# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
| 211 | //# define symbol_from_dll dlsym |
| 212 | //# define close_dll dlclose |
| 213 | //# define load_dll_error dlerror |
| 214 | # endif |
| 215 | |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 216 | # define sodium_init load_sodium |
| 217 | # define sodium_free dll_sodium_free |
| 218 | # define sodium_malloc dll_sodium_malloc |
| 219 | # define sodium_memzero dll_sodium_memzero |
| 220 | # define sodium_mlock dll_sodium_mlock |
| 221 | # define sodium_munlock dll_sodium_munlock |
| 222 | # define crypto_secretstream_xchacha20poly1305_init_push \ |
| 223 | dll_crypto_secretstream_xchacha20poly1305_init_push |
| 224 | # define crypto_secretstream_xchacha20poly1305_push \ |
| 225 | dll_crypto_secretstream_xchacha20poly1305_push |
| 226 | # define crypto_secretstream_xchacha20poly1305_init_pull \ |
| 227 | dll_crypto_secretstream_xchacha20poly1305_init_pull |
| 228 | # define crypto_secretstream_xchacha20poly1305_pull \ |
| 229 | dll_crypto_secretstream_xchacha20poly1305_pull |
| 230 | # define crypto_pwhash dll_crypto_pwhash |
| 231 | # define randombytes_buf dll_randombytes_buf |
K.Takata | a8cdb4e | 2022-12-06 16:17:01 +0000 | [diff] [blame] | 232 | # define randombytes_random dll_randombytes_random |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 233 | |
| 234 | static int (*dll_sodium_init)(void) = NULL; |
| 235 | static void (*dll_sodium_free)(void *) = NULL; |
| 236 | static void *(*dll_sodium_malloc)(const size_t) = NULL; |
| 237 | static void (*dll_sodium_memzero)(void * const, const size_t) = NULL; |
| 238 | static int (*dll_sodium_mlock)(void * const, const size_t) = NULL; |
| 239 | static int (*dll_sodium_munlock)(void * const, const size_t) = NULL; |
| 240 | static int (*dll_crypto_secretstream_xchacha20poly1305_init_push) |
| 241 | (crypto_secretstream_xchacha20poly1305_state *state, |
| 242 | unsigned char [], |
| 243 | const unsigned char []) = NULL; |
| 244 | static int (*dll_crypto_secretstream_xchacha20poly1305_push) |
| 245 | (crypto_secretstream_xchacha20poly1305_state *state, |
| 246 | unsigned char *c, unsigned long long *clen_p, |
| 247 | const unsigned char *m, unsigned long long mlen, |
| 248 | const unsigned char *ad, unsigned long long adlen, unsigned char tag) |
| 249 | = NULL; |
| 250 | static int (*dll_crypto_secretstream_xchacha20poly1305_init_pull) |
| 251 | (crypto_secretstream_xchacha20poly1305_state *state, |
| 252 | const unsigned char [], |
| 253 | const unsigned char []) = NULL; |
| 254 | static int (*dll_crypto_secretstream_xchacha20poly1305_pull) |
| 255 | (crypto_secretstream_xchacha20poly1305_state *state, |
| 256 | unsigned char *m, unsigned long long *mlen_p, unsigned char *tag_p, |
| 257 | const unsigned char *c, unsigned long long clen, |
| 258 | const unsigned char *ad, unsigned long long adlen) = NULL; |
| 259 | static int (*dll_crypto_pwhash)(unsigned char * const out, |
| 260 | unsigned long long outlen, |
| 261 | const char * const passwd, unsigned long long passwdlen, |
| 262 | const unsigned char * const salt, |
| 263 | unsigned long long opslimit, size_t memlimit, int alg) |
| 264 | = NULL; |
| 265 | static void (*dll_randombytes_buf)(void * const buf, const size_t size); |
K.Takata | a8cdb4e | 2022-12-06 16:17:01 +0000 | [diff] [blame] | 266 | static uint32_t (*dll_randombytes_random)(void); |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 267 | |
| 268 | static struct { |
| 269 | const char *name; |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 270 | SODIUM_PROC *ptr; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 271 | } sodium_funcname_table[] = { |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 272 | {"sodium_init", (SODIUM_PROC*)&dll_sodium_init}, |
| 273 | {"sodium_free", (SODIUM_PROC*)&dll_sodium_free}, |
| 274 | {"sodium_malloc", (SODIUM_PROC*)&dll_sodium_malloc}, |
| 275 | {"sodium_memzero", (SODIUM_PROC*)&dll_sodium_memzero}, |
| 276 | {"sodium_mlock", (SODIUM_PROC*)&dll_sodium_mlock}, |
| 277 | {"sodium_munlock", (SODIUM_PROC*)&dll_sodium_munlock}, |
| 278 | {"crypto_secretstream_xchacha20poly1305_init_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_push}, |
| 279 | {"crypto_secretstream_xchacha20poly1305_push", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_push}, |
| 280 | {"crypto_secretstream_xchacha20poly1305_init_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_init_pull}, |
| 281 | {"crypto_secretstream_xchacha20poly1305_pull", (SODIUM_PROC*)&dll_crypto_secretstream_xchacha20poly1305_pull}, |
| 282 | {"crypto_pwhash", (SODIUM_PROC*)&dll_crypto_pwhash}, |
| 283 | {"randombytes_buf", (SODIUM_PROC*)&dll_randombytes_buf}, |
K.Takata | a8cdb4e | 2022-12-06 16:17:01 +0000 | [diff] [blame] | 284 | {"randombytes_random", (SODIUM_PROC*)&dll_randombytes_random}, |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 285 | {NULL, NULL} |
| 286 | }; |
| 287 | |
| 288 | static int |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 289 | sodium_runtime_link_init(int verbose) |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 290 | { |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 291 | static HINSTANCE hsodium = NULL; |
K.Takata | d8f8629 | 2022-03-07 15:16:15 +0000 | [diff] [blame] | 292 | const char *libname = DYNAMIC_SODIUM_DLL; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 293 | int i; |
| 294 | |
| 295 | if (hsodium != NULL) |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 296 | return OK; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 297 | |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 298 | hsodium = load_dll(libname); |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 299 | if (hsodium == NULL) |
| 300 | { |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 301 | if (verbose) |
| 302 | semsg(_(e_could_not_load_library_str_str), libname, load_dll_error()); |
| 303 | return FAIL; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | for (i = 0; sodium_funcname_table[i].ptr; ++i) |
| 307 | { |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 308 | if ((*sodium_funcname_table[i].ptr = symbol_from_dll(hsodium, |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 309 | sodium_funcname_table[i].name)) == NULL) |
| 310 | { |
K.Takata | d8f8629 | 2022-03-07 15:16:15 +0000 | [diff] [blame] | 311 | close_dll(hsodium); |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 312 | hsodium = NULL; |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 313 | if (verbose) |
| 314 | semsg(_(e_could_not_load_library_function_str), sodium_funcname_table[i].name); |
| 315 | return FAIL; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 318 | return OK; |
| 319 | } |
| 320 | |
| 321 | static int |
| 322 | load_sodium(void) |
| 323 | { |
| 324 | if (sodium_runtime_link_init(TRUE) == FAIL) |
| 325 | return -1; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 326 | return dll_sodium_init(); |
| 327 | } |
| 328 | # endif |
K.Takata | d68b2fc | 2022-02-12 11:18:37 +0000 | [diff] [blame] | 329 | |
| 330 | # if defined(DYNAMIC_SODIUM) || defined(PROTO) |
| 331 | int |
| 332 | sodium_enabled(int verbose) |
| 333 | { |
| 334 | return sodium_runtime_link_init(verbose) == OK; |
| 335 | } |
| 336 | # endif |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 337 | #endif |
| 338 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 339 | #define CRYPT_MAGIC_LEN 12 // cannot change |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 340 | static char crypt_magic_head[] = "VimCrypt~"; |
| 341 | |
| 342 | /* |
| 343 | * Return int value for crypt method name. |
| 344 | * 0 for "zip", the old method. Also for any non-valid value. |
| 345 | * 1 for "blowfish". |
| 346 | * 2 for "blowfish2". |
| 347 | */ |
| 348 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 349 | crypt_method_nr_from_name(char_u *name) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 350 | { |
| 351 | int i; |
| 352 | |
| 353 | for (i = 0; i < CRYPT_M_COUNT; ++i) |
| 354 | if (STRCMP(name, cryptmethods[i].name) == 0) |
| 355 | return i; |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Get the crypt method used for a file from "ptr[len]", the magic text at the |
| 361 | * start of the file. |
| 362 | * Returns -1 when no encryption used. |
| 363 | */ |
| 364 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 365 | crypt_method_nr_from_magic(char *ptr, int len) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 366 | { |
| 367 | int i; |
| 368 | |
| 369 | if (len < CRYPT_MAGIC_LEN) |
| 370 | return -1; |
| 371 | |
| 372 | for (i = 0; i < CRYPT_M_COUNT; i++) |
| 373 | if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0) |
| 374 | return i; |
| 375 | |
| 376 | i = (int)STRLEN(crypt_magic_head); |
| 377 | if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0) |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 378 | emsg(_(e_file_is_encrypted_with_unknown_method)); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 379 | |
| 380 | return -1; |
| 381 | } |
| 382 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 383 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 384 | /* |
| 385 | * Return TRUE if the crypt method for "method_nr" can be done in-place. |
| 386 | */ |
| 387 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 388 | crypt_works_inplace(cryptstate_T *state) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 389 | { |
| 390 | return cryptmethods[state->method_nr].works_inplace; |
| 391 | } |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 392 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * Get the crypt method for buffer "buf" as a number. |
| 396 | */ |
| 397 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 398 | crypt_get_method_nr(buf_T *buf) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 399 | { |
| 400 | return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); |
| 401 | } |
| 402 | |
| 403 | /* |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 404 | * Returns True for Sodium Encryption. |
| 405 | */ |
| 406 | int |
| 407 | crypt_method_is_sodium(int method) |
| 408 | { |
| 409 | return method == CRYPT_M_SOD || method == CRYPT_M_SOD2; |
| 410 | } |
| 411 | |
| 412 | /* |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 413 | * Return TRUE when the buffer uses an encryption method that encrypts the |
| 414 | * whole undo file, not only the text. |
| 415 | */ |
| 416 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 417 | crypt_whole_undofile(int method_nr) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 418 | { |
| 419 | return cryptmethods[method_nr].whole_undofile; |
| 420 | } |
| 421 | |
| 422 | /* |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 423 | * Get crypt method specific length of the file header in bytes. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 424 | */ |
| 425 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 426 | crypt_get_header_len(int method_nr) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 427 | { |
| 428 | return CRYPT_MAGIC_LEN |
| 429 | + cryptmethods[method_nr].salt_len |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 430 | + cryptmethods[method_nr].seed_len |
| 431 | + cryptmethods[method_nr].add_len; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 434 | |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 435 | #if defined(FEAT_SODIUM) || defined(PROTO) |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 436 | /* |
| 437 | * Get maximum crypt method specific length of the file header in bytes. |
| 438 | */ |
| 439 | int |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 440 | crypt_get_max_header_len(void) |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 441 | { |
| 442 | int i; |
| 443 | int max = 0; |
| 444 | int temp = 0; |
| 445 | |
| 446 | for (i = 0; i < CRYPT_M_COUNT; ++i) |
| 447 | { |
| 448 | temp = crypt_get_header_len(i); |
| 449 | if (temp > max) |
| 450 | max = temp; |
| 451 | } |
| 452 | return max; |
| 453 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 454 | #endif |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 455 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 456 | /* |
| 457 | * Set the crypt method for buffer "buf" to "method_nr" using the int value as |
| 458 | * returned by crypt_method_nr_from_name(). |
| 459 | */ |
| 460 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 461 | crypt_set_cm_option(buf_T *buf, int method_nr) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 462 | { |
| 463 | free_string_option(buf->b_p_cm); |
| 464 | buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * If the crypt method for the current buffer has a self-test, run it and |
| 469 | * return OK/FAIL. |
| 470 | */ |
| 471 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 472 | crypt_self_test(void) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 473 | { |
| 474 | int method_nr = crypt_get_method_nr(curbuf); |
| 475 | |
| 476 | if (cryptmethods[method_nr].self_test_fn == NULL) |
| 477 | return OK; |
| 478 | return cryptmethods[method_nr].self_test_fn(); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Allocate a crypt state and initialize it. |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 483 | * Return NULL for failure. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 484 | */ |
| 485 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 486 | crypt_create( |
| 487 | int method_nr, |
| 488 | char_u *key, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 489 | crypt_arg_T *crypt_arg) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 490 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 491 | cryptstate_T *state = ALLOC_ONE(cryptstate_T); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 492 | |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 493 | if (state == NULL) |
| 494 | return state; |
| 495 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 496 | state->method_nr = method_nr; |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 497 | if (cryptmethods[method_nr].init_fn(state, key, crypt_arg) == FAIL) |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 498 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 499 | vim_free(state); |
| 500 | return NULL; |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 501 | } |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 502 | return state; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Allocate a crypt state from a file header and initialize it. |
| 507 | * Assumes that header contains at least the number of bytes that |
| 508 | * crypt_get_header_len() returns for "method_nr". |
| 509 | */ |
| 510 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 511 | crypt_create_from_header( |
| 512 | int method_nr, |
| 513 | char_u *key, |
| 514 | char_u *header) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 515 | { |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 516 | crypt_arg_T arg; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 517 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 518 | CLEAR_FIELD(arg); |
| 519 | arg.cat_init_from_file = TRUE; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 520 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 521 | arg.cat_salt_len = cryptmethods[method_nr].salt_len; |
| 522 | arg.cat_seed_len = cryptmethods[method_nr].seed_len; |
| 523 | arg.cat_add_len = cryptmethods[method_nr].add_len; |
| 524 | if (arg.cat_salt_len > 0) |
| 525 | arg.cat_salt = header + CRYPT_MAGIC_LEN; |
| 526 | if (arg.cat_seed_len > 0) |
| 527 | arg.cat_seed = header + CRYPT_MAGIC_LEN + arg.cat_salt_len; |
| 528 | if (arg.cat_add_len > 0) |
| 529 | arg.cat_add = header + CRYPT_MAGIC_LEN + arg.cat_salt_len + arg.cat_seed_len; |
| 530 | |
| 531 | return crypt_create(method_nr, key, &arg); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Read the crypt method specific header data from "fp". |
| 536 | * Return an allocated cryptstate_T or NULL on error. |
| 537 | */ |
| 538 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 539 | crypt_create_from_file(FILE *fp, char_u *key) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 540 | { |
| 541 | int method_nr; |
| 542 | int header_len; |
| 543 | char magic_buffer[CRYPT_MAGIC_LEN]; |
| 544 | char_u *buffer; |
| 545 | cryptstate_T *state; |
| 546 | |
| 547 | if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) |
| 548 | return NULL; |
| 549 | method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN); |
| 550 | if (method_nr < 0) |
| 551 | return NULL; |
| 552 | |
| 553 | header_len = crypt_get_header_len(method_nr); |
| 554 | if ((buffer = alloc(header_len)) == NULL) |
| 555 | return NULL; |
| 556 | mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN); |
| 557 | if (header_len > CRYPT_MAGIC_LEN |
| 558 | && fread(buffer + CRYPT_MAGIC_LEN, |
| 559 | header_len - CRYPT_MAGIC_LEN, 1, fp) != 1) |
| 560 | { |
| 561 | vim_free(buffer); |
| 562 | return NULL; |
| 563 | } |
| 564 | |
| 565 | state = crypt_create_from_header(method_nr, key, buffer); |
| 566 | vim_free(buffer); |
| 567 | return state; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Allocate a cryptstate_T for writing and initialize it with "key". |
| 572 | * Allocates and fills in the header and stores it in "header", setting |
| 573 | * "header_len". The header may include salt and seed, depending on |
| 574 | * cryptmethod. Caller must free header. |
| 575 | * Returns the state or NULL on failure. |
| 576 | */ |
| 577 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 578 | crypt_create_for_writing( |
| 579 | int method_nr, |
| 580 | char_u *key, |
| 581 | char_u **header, |
| 582 | int *header_len) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 583 | { |
| 584 | int len = crypt_get_header_len(method_nr); |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 585 | crypt_arg_T arg; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 586 | cryptstate_T *state; |
| 587 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 588 | CLEAR_FIELD(arg); |
| 589 | arg.cat_salt_len = cryptmethods[method_nr].salt_len; |
| 590 | arg.cat_seed_len = cryptmethods[method_nr].seed_len; |
| 591 | arg.cat_add_len = cryptmethods[method_nr].add_len; |
| 592 | arg.cat_init_from_file = FALSE; |
| 593 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 594 | *header_len = len; |
| 595 | *header = alloc(len); |
| 596 | if (*header == NULL) |
| 597 | return NULL; |
| 598 | |
| 599 | mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN); |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 600 | if (arg.cat_salt_len > 0 || arg.cat_seed_len > 0 || arg.cat_add_len > 0) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 601 | { |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 602 | if (arg.cat_salt_len > 0) |
| 603 | arg.cat_salt = *header + CRYPT_MAGIC_LEN; |
| 604 | if (arg.cat_seed_len > 0) |
| 605 | arg.cat_seed = *header + CRYPT_MAGIC_LEN + arg.cat_salt_len; |
| 606 | if (arg.cat_add_len > 0) |
| 607 | arg.cat_add = *header + CRYPT_MAGIC_LEN + arg.cat_salt_len + arg.cat_seed_len; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 608 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 609 | // TODO: Should this be crypt method specific? (Probably not worth |
| 610 | // it). sha2_seed is pretty bad for large amounts of entropy, so make |
| 611 | // that into something which is suitable for anything. |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 612 | #ifdef FEAT_SODIUM |
| 613 | if (sodium_init() >= 0) |
| 614 | { |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 615 | if (arg.cat_salt_len > 0) |
| 616 | randombytes_buf(arg.cat_salt, arg.cat_salt_len); |
| 617 | if (arg.cat_seed_len > 0) |
| 618 | randombytes_buf(arg.cat_seed, arg.cat_seed_len); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 619 | } |
| 620 | else |
| 621 | #endif |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 622 | sha2_seed(arg.cat_salt, arg.cat_salt_len, arg.cat_seed, arg.cat_seed_len); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 623 | } |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 624 | state = crypt_create(method_nr, key, &arg); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 625 | if (state == NULL) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 626 | VIM_CLEAR(*header); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 627 | return state; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Free the crypt state. |
| 632 | */ |
| 633 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 634 | crypt_free_state(cryptstate_T *state) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 635 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 636 | #ifdef FEAT_SODIUM |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 637 | if (crypt_method_is_sodium(state->method_nr)) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 638 | { |
Bram Moolenaar | 131530a | 2021-07-29 20:37:49 +0200 | [diff] [blame] | 639 | sodium_munlock(((sodium_state_T *)state->method_state)->key, |
| 640 | crypto_box_SEEDBYTES); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 641 | sodium_memzero(state->method_state, sizeof(sodium_state_T)); |
| 642 | sodium_free(state->method_state); |
| 643 | } |
| 644 | else |
| 645 | #endif |
| 646 | vim_free(state->method_state); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 647 | vim_free(state); |
| 648 | } |
| 649 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 650 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 651 | /* |
| 652 | * Encode "from[len]" and store the result in a newly allocated buffer, which |
| 653 | * is stored in "newptr". |
| 654 | * Return number of bytes in "newptr", 0 for need more or -1 on error. |
| 655 | */ |
| 656 | long |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 657 | crypt_encode_alloc( |
| 658 | cryptstate_T *state, |
| 659 | char_u *from, |
| 660 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 661 | char_u **newptr, |
| 662 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 663 | { |
| 664 | cryptmethod_T *method = &cryptmethods[state->method_nr]; |
| 665 | |
| 666 | if (method->encode_buffer_fn != NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 667 | // Has buffer function, pass through. |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 668 | return method->encode_buffer_fn(state, from, len, newptr, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 669 | if (len == 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 670 | // Not buffering, just return EOF. |
Bram Moolenaar | 9b8f021 | 2014-08-13 22:05:53 +0200 | [diff] [blame] | 671 | return (long)len; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 672 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 673 | *newptr = alloc(len + 50); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 674 | if (*newptr == NULL) |
| 675 | return -1; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 676 | method->encode_fn(state, from, len, *newptr, last); |
Bram Moolenaar | 9b8f021 | 2014-08-13 22:05:53 +0200 | [diff] [blame] | 677 | return (long)len; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which |
| 682 | * is stored in "newptr". |
| 683 | * Return number of bytes in "newptr", 0 for need more or -1 on error. |
| 684 | */ |
| 685 | long |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 686 | crypt_decode_alloc( |
| 687 | cryptstate_T *state, |
| 688 | char_u *ptr, |
| 689 | long len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 690 | char_u **newptr, |
| 691 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 692 | { |
| 693 | cryptmethod_T *method = &cryptmethods[state->method_nr]; |
| 694 | |
| 695 | if (method->decode_buffer_fn != NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 696 | // Has buffer function, pass through. |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 697 | return method->decode_buffer_fn(state, ptr, len, newptr, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 698 | |
| 699 | if (len == 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 700 | // Not buffering, just return EOF. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 701 | return len; |
| 702 | |
| 703 | *newptr = alloc(len); |
| 704 | if (*newptr == NULL) |
| 705 | return -1; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 706 | method->decode_fn(state, ptr, len, *newptr, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 707 | return len; |
| 708 | } |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 709 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 710 | |
| 711 | /* |
| 712 | * Encrypting "from[len]" into "to[len]". |
| 713 | */ |
| 714 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 715 | crypt_encode( |
| 716 | cryptstate_T *state, |
| 717 | char_u *from, |
| 718 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 719 | char_u *to, |
| 720 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 721 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 722 | cryptmethods[state->method_nr].encode_fn(state, from, len, to, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 723 | } |
| 724 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 725 | #if 0 // unused |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 726 | /* |
| 727 | * decrypting "from[len]" into "to[len]". |
| 728 | */ |
| 729 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 730 | crypt_decode( |
| 731 | cryptstate_T *state, |
| 732 | char_u *from, |
| 733 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 734 | char_u *to, |
| 735 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 736 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 737 | cryptmethods[state->method_nr].decode_fn(state, from, len, to, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 738 | } |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 739 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * Simple inplace encryption, modifies "buf[len]" in place. |
| 743 | */ |
| 744 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 745 | crypt_encode_inplace( |
| 746 | cryptstate_T *state, |
| 747 | char_u *buf, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 748 | size_t len, |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 749 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 750 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 751 | cryptmethods[state->method_nr].encode_inplace_fn(state, buf, len, |
| 752 | buf, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Simple inplace decryption, modifies "buf[len]" in place. |
| 757 | */ |
| 758 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 759 | crypt_decode_inplace( |
| 760 | cryptstate_T *state, |
| 761 | char_u *buf, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 762 | size_t len, |
| 763 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 764 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 765 | cryptmethods[state->method_nr].decode_inplace_fn(state, buf, len, |
| 766 | buf, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Free an allocated crypt key. Clear the text to make sure it doesn't stay |
| 771 | * in memory anywhere. |
| 772 | */ |
| 773 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 774 | crypt_free_key(char_u *key) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 775 | { |
| 776 | char_u *p; |
| 777 | |
| 778 | if (key != NULL) |
| 779 | { |
| 780 | for (p = key; *p != NUL; ++p) |
| 781 | *p = 0; |
| 782 | vim_free(key); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | /* |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 787 | * Check the crypt method and give a warning if it's outdated. |
| 788 | */ |
| 789 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 790 | crypt_check_method(int method) |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 791 | { |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 792 | if (method < CRYPT_M_BF2 || method == CRYPT_M_SOD) |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 793 | { |
| 794 | msg_scroll = TRUE; |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 795 | msg(_("Warning: Using a weak encryption method; see :help 'cm'")); |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 796 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | #ifdef FEAT_SODIUM |
| 800 | static void |
| 801 | crypt_check_swapfile_curbuf(void) |
| 802 | { |
| 803 | int method = crypt_get_method_nr(curbuf); |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 804 | if (crypt_method_is_sodium(method)) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 805 | { |
| 806 | // encryption uses padding and MAC, that does not work very well with |
| 807 | // swap and undo files, so disable them |
| 808 | mf_close_file(curbuf, TRUE); // remove the swap file |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 809 | set_option_value_give_err((char_u *)"swf", 0, NULL, OPT_LOCAL); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 810 | msg_scroll = TRUE; |
Christian Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 811 | msg(_("Note: Encryption of swapfile not supported, disabling swap file")); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 812 | } |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 813 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 814 | #endif |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 815 | |
| 816 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 817 | crypt_check_current_method(void) |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 818 | { |
| 819 | crypt_check_method(crypt_get_method_nr(curbuf)); |
| 820 | } |
| 821 | |
| 822 | /* |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 823 | * Ask the user for a crypt key. |
| 824 | * When "store" is TRUE, the new key is stored in the 'key' option, and the |
| 825 | * 'key' option value is returned: Don't free it. |
| 826 | * When "store" is FALSE, the typed key is returned in allocated memory. |
| 827 | * Returns NULL on failure. |
| 828 | */ |
| 829 | char_u * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 830 | crypt_get_key( |
| 831 | int store, |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 832 | int twice) // Ask for the key twice. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 833 | { |
| 834 | char_u *p1, *p2 = NULL; |
| 835 | int round; |
| 836 | |
| 837 | for (round = 0; ; ++round) |
| 838 | { |
| 839 | cmdline_star = TRUE; |
| 840 | cmdline_row = msg_row; |
| 841 | p1 = getcmdline_prompt(NUL, round == 0 |
| 842 | ? (char_u *)_("Enter encryption key: ") |
| 843 | : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING, |
| 844 | NULL); |
| 845 | cmdline_star = FALSE; |
| 846 | |
| 847 | if (p1 == NULL) |
| 848 | break; |
| 849 | |
| 850 | if (round == twice) |
| 851 | { |
| 852 | if (p2 != NULL && STRCMP(p1, p2) != 0) |
| 853 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 854 | msg(_("Keys don't match!")); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 855 | crypt_free_key(p1); |
| 856 | crypt_free_key(p2); |
| 857 | p2 = NULL; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 858 | round = -1; // do it again |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 859 | continue; |
| 860 | } |
| 861 | |
| 862 | if (store) |
| 863 | { |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 864 | set_option_value_give_err((char_u *)"key", 0L, p1, OPT_LOCAL); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 865 | crypt_free_key(p1); |
| 866 | p1 = curbuf->b_p_key; |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 867 | #ifdef FEAT_SODIUM |
| 868 | crypt_check_swapfile_curbuf(); |
| 869 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 870 | } |
| 871 | break; |
| 872 | } |
| 873 | p2 = p1; |
| 874 | } |
| 875 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 876 | // since the user typed this, no need to wait for return |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 877 | if (!crypt_method_is_sodium(crypt_get_method_nr(curbuf))) |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 878 | { |
| 879 | if (msg_didout) |
| 880 | msg_putchar('\n'); |
| 881 | need_wait_return = FALSE; |
| 882 | msg_didout = FALSE; |
| 883 | } |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 884 | |
| 885 | crypt_free_key(p2); |
| 886 | return p1; |
| 887 | } |
| 888 | |
| 889 | |
| 890 | /* |
| 891 | * Append a message to IObuff for the encryption/decryption method being used. |
| 892 | */ |
| 893 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 894 | crypt_append_msg( |
| 895 | buf_T *buf) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 896 | { |
| 897 | if (crypt_get_method_nr(buf) == 0) |
| 898 | STRCAT(IObuff, _("[crypted]")); |
| 899 | else |
| 900 | { |
| 901 | STRCAT(IObuff, "["); |
| 902 | STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); |
| 903 | STRCAT(IObuff, "]"); |
| 904 | } |
| 905 | } |
| 906 | |
Yegappan Lakshmanan | ee47eac | 2022-06-29 12:55:36 +0100 | [diff] [blame] | 907 | static int |
K.Takata | a8cdb4e | 2022-12-06 16:17:01 +0000 | [diff] [blame] | 908 | crypt_sodium_init_( |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 909 | cryptstate_T *state UNUSED, |
| 910 | char_u *key UNUSED, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 911 | crypt_arg_T *arg UNUSED) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 912 | { |
| 913 | # ifdef FEAT_SODIUM |
| 914 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 915 | unsigned char dkey[crypto_box_SEEDBYTES]; // 32 |
| 916 | sodium_state_T *sd_state; |
Bram Moolenaar | 131530a | 2021-07-29 20:37:49 +0200 | [diff] [blame] | 917 | int retval = 0; |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 918 | unsigned long long opslimit; |
| 919 | size_t memlimit; |
| 920 | int alg; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 921 | |
| 922 | if (sodium_init() < 0) |
| 923 | return FAIL; |
| 924 | |
| 925 | sd_state = (sodium_state_T *)sodium_malloc(sizeof(sodium_state_T)); |
| 926 | sodium_memzero(sd_state, sizeof(sodium_state_T)); |
| 927 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 928 | if ((state->method_nr == CRYPT_M_SOD2 && !arg->cat_init_from_file) |
| 929 | || state->method_nr == CRYPT_M_SOD) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 930 | { |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 931 | opslimit = crypto_pwhash_OPSLIMIT_INTERACTIVE; |
| 932 | memlimit = crypto_pwhash_MEMLIMIT_INTERACTIVE; |
| 933 | alg = crypto_pwhash_ALG_DEFAULT; |
| 934 | |
| 935 | #if 0 |
| 936 | // For testing |
| 937 | if (state->method_nr == CRYPT_M_SOD2) |
| 938 | { |
| 939 | opslimit = crypto_pwhash_OPSLIMIT_MODERATE; |
| 940 | memlimit = crypto_pwhash_MEMLIMIT_MODERATE; |
| 941 | } |
| 942 | #endif |
| 943 | |
| 944 | // derive a key from the password |
| 945 | if (crypto_pwhash(dkey, sizeof(dkey), (const char *)key, STRLEN(key), |
| 946 | arg->cat_salt, opslimit, memlimit, alg) != 0) |
| 947 | { |
| 948 | // out of memory |
| 949 | sodium_free(sd_state); |
| 950 | return FAIL; |
| 951 | } |
| 952 | memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES); |
| 953 | |
| 954 | retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES); |
| 955 | retval += sodium_mlock(key, STRLEN(key)); |
| 956 | |
| 957 | if (retval < 0) |
| 958 | { |
| 959 | emsg(_(e_encryption_sodium_mlock_failed)); |
| 960 | sodium_free(sd_state); |
| 961 | return FAIL; |
| 962 | } |
| 963 | if (state->method_nr == CRYPT_M_SOD2) |
| 964 | { |
| 965 | memcpy(arg->cat_add, &opslimit, sizeof(opslimit)); |
| 966 | arg->cat_add += sizeof(opslimit); |
| 967 | |
| 968 | memcpy(arg->cat_add, &memlimit, sizeof(memlimit)); |
| 969 | arg->cat_add += sizeof(memlimit); |
| 970 | |
| 971 | memcpy(arg->cat_add, &alg, sizeof(alg)); |
| 972 | arg->cat_add += sizeof(alg); |
| 973 | } |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 974 | } |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 975 | else |
Bram Moolenaar | 131530a | 2021-07-29 20:37:49 +0200 | [diff] [blame] | 976 | { |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 977 | // Reading parameters from file |
| 978 | if (arg->cat_add_len |
| 979 | < (int)(sizeof(opslimit) + sizeof(memlimit) + sizeof(alg))) |
| 980 | { |
| 981 | sodium_free(sd_state); |
| 982 | return FAIL; |
| 983 | } |
| 984 | |
| 985 | // derive the key from the file header |
| 986 | memcpy(&opslimit, arg->cat_add, sizeof(opslimit)); |
| 987 | arg->cat_add += sizeof(opslimit); |
| 988 | |
| 989 | memcpy(&memlimit, arg->cat_add, sizeof(memlimit)); |
| 990 | arg->cat_add += sizeof(memlimit); |
| 991 | |
| 992 | memcpy(&alg, arg->cat_add, sizeof(alg)); |
| 993 | arg->cat_add += sizeof(alg); |
| 994 | |
| 995 | #ifdef FEAT_EVAL |
| 996 | crypt_sodium_report_hash_params(opslimit, |
| 997 | crypto_pwhash_OPSLIMIT_INTERACTIVE, |
| 998 | memlimit, crypto_pwhash_MEMLIMIT_INTERACTIVE, |
| 999 | alg, crypto_pwhash_ALG_DEFAULT); |
| 1000 | #endif |
| 1001 | |
| 1002 | if (crypto_pwhash(dkey, sizeof(dkey), (const char *)key, STRLEN(key), |
| 1003 | arg->cat_salt, opslimit, memlimit, alg) != 0) |
| 1004 | { |
| 1005 | // out of memory |
| 1006 | sodium_free(sd_state); |
| 1007 | return FAIL; |
| 1008 | } |
| 1009 | memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES); |
| 1010 | |
| 1011 | retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES); |
| 1012 | retval += sodium_mlock(key, STRLEN(key)); |
| 1013 | |
| 1014 | if (retval < 0) |
| 1015 | { |
| 1016 | emsg(_(e_encryption_sodium_mlock_failed)); |
| 1017 | sodium_free(sd_state); |
| 1018 | return FAIL; |
| 1019 | } |
Bram Moolenaar | 131530a | 2021-07-29 20:37:49 +0200 | [diff] [blame] | 1020 | } |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1021 | sd_state->count = 0; |
| 1022 | state->method_state = sd_state; |
| 1023 | |
| 1024 | return OK; |
| 1025 | # else |
| 1026 | emsg(e_libsodium_not_built_in); |
| 1027 | return FAIL; |
| 1028 | # endif |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * Encrypt "from[len]" into "to[len]". |
| 1033 | * "from" and "to" can be equal to encrypt in place. |
| 1034 | * Call needs to ensure that there is enough space in to (for the header) |
| 1035 | */ |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 1036 | #if 0 // Currently unused |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1037 | void |
| 1038 | crypt_sodium_encode( |
| 1039 | cryptstate_T *state UNUSED, |
| 1040 | char_u *from UNUSED, |
| 1041 | size_t len UNUSED, |
| 1042 | char_u *to UNUSED, |
| 1043 | int last UNUSED) |
| 1044 | { |
| 1045 | # ifdef FEAT_SODIUM |
| 1046 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 1047 | sodium_state_T *sod_st = state->method_state; |
| 1048 | unsigned char tag = last |
| 1049 | ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; |
| 1050 | |
| 1051 | if (sod_st->count == 0) |
| 1052 | { |
| 1053 | if (len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
| 1054 | { |
| 1055 | emsg(e_libsodium_cannot_encrypt_header); |
| 1056 | return; |
| 1057 | } |
| 1058 | crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state, |
| 1059 | to, sod_st->key); |
| 1060 | to += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1061 | } |
| 1062 | |
| 1063 | if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES) |
| 1064 | { |
| 1065 | emsg(e_libsodium_cannot_encrypt_buffer); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | crypto_secretstream_xchacha20poly1305_push(&sod_st->state, to, NULL, |
| 1070 | from, len, NULL, 0, tag); |
| 1071 | |
| 1072 | sod_st->count++; |
| 1073 | # endif |
| 1074 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 1075 | #endif |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1076 | |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 1077 | /* |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1078 | * Decrypt "from[len]" into "to[len]". |
| 1079 | * "from" and "to" can be equal to encrypt in place. |
| 1080 | */ |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 1081 | #if 0 // Currently unused |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1082 | void |
| 1083 | crypt_sodium_decode( |
| 1084 | cryptstate_T *state UNUSED, |
| 1085 | char_u *from UNUSED, |
| 1086 | size_t len UNUSED, |
| 1087 | char_u *to UNUSED, |
| 1088 | int last UNUSED) |
| 1089 | { |
| 1090 | # ifdef FEAT_SODIUM |
| 1091 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 1092 | sodium_state_T *sod_st = state->method_state; |
| 1093 | unsigned char tag; |
| 1094 | unsigned long long buf_len; |
| 1095 | char_u *p1 = from; |
| 1096 | char_u *p2 = to; |
| 1097 | char_u *buf_out; |
| 1098 | |
| 1099 | if (sod_st->count == 0 |
| 1100 | && len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
| 1101 | { |
| 1102 | emsg(e_libsodium_cannot_decrypt_header); |
| 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | buf_out = (char_u *)alloc(len); |
| 1107 | |
| 1108 | if (buf_out == NULL) |
| 1109 | { |
| 1110 | emsg(e_libsodium_cannot_allocate_buffer); |
| 1111 | return; |
| 1112 | } |
| 1113 | if (sod_st->count == 0) |
| 1114 | { |
| 1115 | if (crypto_secretstream_xchacha20poly1305_init_pull( |
| 1116 | &sod_st->state, from, sod_st->key) != 0) |
| 1117 | { |
| 1118 | emsg(e_libsodium_decryption_failed_header_incomplete); |
| 1119 | goto fail; |
| 1120 | } |
| 1121 | |
| 1122 | from += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1123 | len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1124 | |
| 1125 | if (p1 == p2) |
| 1126 | to += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1127 | } |
| 1128 | |
| 1129 | if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES) |
| 1130 | { |
| 1131 | emsg(e_libsodium_cannot_decrypt_buffer); |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 1132 | goto fail; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1133 | } |
| 1134 | if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state, |
| 1135 | buf_out, &buf_len, &tag, from, len, NULL, 0) != 0) |
| 1136 | { |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 1137 | emsg(e_libsodium_decryption_failed); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1138 | goto fail; |
| 1139 | } |
| 1140 | sod_st->count++; |
| 1141 | |
| 1142 | if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last) |
| 1143 | { |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 1144 | emsg(e_libsodium_decryption_failed_premature); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1145 | goto fail; |
| 1146 | } |
| 1147 | if (p1 == p2) |
| 1148 | mch_memmove(p2, buf_out, buf_len); |
| 1149 | |
| 1150 | fail: |
| 1151 | vim_free(buf_out); |
| 1152 | # endif |
| 1153 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 1154 | #endif |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1155 | |
| 1156 | /* |
| 1157 | * Encrypt "from[len]" into "to[len]". |
| 1158 | * "from" and "to" can be equal to encrypt in place. |
| 1159 | */ |
Yegappan Lakshmanan | ee47eac | 2022-06-29 12:55:36 +0100 | [diff] [blame] | 1160 | static long |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1161 | crypt_sodium_buffer_encode( |
| 1162 | cryptstate_T *state UNUSED, |
| 1163 | char_u *from UNUSED, |
| 1164 | size_t len UNUSED, |
| 1165 | char_u **buf_out UNUSED, |
| 1166 | int last UNUSED) |
| 1167 | { |
| 1168 | # ifdef FEAT_SODIUM |
| 1169 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 1170 | unsigned long long out_len; |
| 1171 | char_u *ptr; |
| 1172 | unsigned char tag = last |
| 1173 | ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; |
| 1174 | int length; |
| 1175 | sodium_state_T *sod_st = state->method_state; |
| 1176 | int first = (sod_st->count == 0); |
| 1177 | |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 1178 | length = (int)len + crypto_secretstream_xchacha20poly1305_ABYTES |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1179 | + (first ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0); |
| 1180 | *buf_out = alloc_clear(length); |
| 1181 | if (*buf_out == NULL) |
| 1182 | { |
| 1183 | emsg(e_libsodium_cannot_allocate_buffer); |
| 1184 | return -1; |
| 1185 | } |
| 1186 | ptr = *buf_out; |
| 1187 | |
| 1188 | if (first) |
| 1189 | { |
| 1190 | crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state, |
| 1191 | ptr, sod_st->key); |
| 1192 | ptr += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1193 | } |
| 1194 | |
| 1195 | crypto_secretstream_xchacha20poly1305_push(&sod_st->state, ptr, |
| 1196 | &out_len, from, len, NULL, 0, tag); |
| 1197 | |
| 1198 | sod_st->count++; |
| 1199 | return out_len + (first |
| 1200 | ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0); |
| 1201 | # else |
| 1202 | return -1; |
| 1203 | # endif |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Decrypt "from[len]" into "to[len]". |
| 1208 | * "from" and "to" can be equal to encrypt in place. |
| 1209 | */ |
Yegappan Lakshmanan | ee47eac | 2022-06-29 12:55:36 +0100 | [diff] [blame] | 1210 | static long |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1211 | crypt_sodium_buffer_decode( |
| 1212 | cryptstate_T *state UNUSED, |
| 1213 | char_u *from UNUSED, |
| 1214 | size_t len UNUSED, |
| 1215 | char_u **buf_out UNUSED, |
| 1216 | int last UNUSED) |
| 1217 | { |
| 1218 | # ifdef FEAT_SODIUM |
| 1219 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 1220 | sodium_state_T *sod_st = state->method_state; |
| 1221 | unsigned char tag; |
| 1222 | unsigned long long out_len; |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 1223 | |
| 1224 | if (sod_st->count == 0 |
| 1225 | && state->method_nr == CRYPT_M_SOD |
| 1226 | && len > WRITEBUFSIZE |
| 1227 | + crypto_secretstream_xchacha20poly1305_HEADERBYTES |
| 1228 | + crypto_secretstream_xchacha20poly1305_ABYTES) |
| 1229 | len -= cryptmethods[CRYPT_M_SOD2].add_len; |
| 1230 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1231 | *buf_out = alloc_clear(len); |
| 1232 | if (*buf_out == NULL) |
| 1233 | { |
| 1234 | emsg(e_libsodium_cannot_allocate_buffer); |
| 1235 | return -1; |
| 1236 | } |
| 1237 | |
| 1238 | if (sod_st->count == 0) |
| 1239 | { |
| 1240 | if (crypto_secretstream_xchacha20poly1305_init_pull(&sod_st->state, |
| 1241 | from, sod_st->key) != 0) |
| 1242 | { |
| 1243 | emsg(e_libsodium_decryption_failed_header_incomplete); |
| 1244 | return -1; |
| 1245 | } |
| 1246 | from += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1247 | len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1248 | sod_st->count++; |
| 1249 | } |
| 1250 | if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state, |
| 1251 | *buf_out, &out_len, &tag, from, len, NULL, 0) != 0) |
| 1252 | { |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 1253 | emsg(e_libsodium_decryption_failed); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1254 | return -1; |
| 1255 | } |
| 1256 | |
| 1257 | if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last) |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 1258 | emsg(e_libsodium_decryption_failed_premature); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1259 | return (long) out_len; |
| 1260 | # else |
| 1261 | return -1; |
| 1262 | # endif |
| 1263 | } |
| 1264 | |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 1265 | # if defined(FEAT_SODIUM) || defined(PROTO) |
| 1266 | int |
| 1267 | crypt_sodium_munlock(void *const addr, const size_t len) |
| 1268 | { |
| 1269 | return sodium_munlock(addr, len); |
| 1270 | } |
| 1271 | |
| 1272 | void |
| 1273 | crypt_sodium_randombytes_buf(void *const buf, const size_t size) |
| 1274 | { |
| 1275 | randombytes_buf(buf, size); |
| 1276 | } |
K.Takata | a8cdb4e | 2022-12-06 16:17:01 +0000 | [diff] [blame] | 1277 | |
| 1278 | int |
| 1279 | crypt_sodium_init(void) |
| 1280 | { |
| 1281 | return sodium_init(); |
| 1282 | } |
| 1283 | |
| 1284 | uint32_t |
| 1285 | crypt_sodium_randombytes_random(void) |
| 1286 | { |
| 1287 | return randombytes_random(); |
| 1288 | } |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 1289 | |
| 1290 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 1291 | static void |
| 1292 | crypt_sodium_report_hash_params( |
| 1293 | unsigned long long opslimit, |
| 1294 | unsigned long long ops_def, |
| 1295 | size_t memlimit, |
| 1296 | size_t mem_def, |
| 1297 | int alg, |
| 1298 | int alg_def) |
| 1299 | { |
| 1300 | if (p_verbose > 0) |
| 1301 | { |
| 1302 | verbose_enter(); |
| 1303 | if (opslimit != ops_def) |
| 1304 | smsg(_("xchacha20v2: using custom opslimit \"%llu\" for Key derivation."), opslimit); |
| 1305 | else |
| 1306 | smsg(_("xchacha20v2: using default opslimit \"%llu\" for Key derivation."), opslimit); |
| 1307 | if (memlimit != mem_def) |
| 1308 | smsg(_("xchacha20v2: using custom memlimit \"%lu\" for Key derivation."), (unsigned long)memlimit); |
| 1309 | else |
| 1310 | smsg(_("xchacha20v2: using default memlimit \"%lu\" for Key derivation."), (unsigned long)memlimit); |
| 1311 | if (alg != alg_def) |
| 1312 | smsg(_("xchacha20v2: using custom algorithm \"%d\" for Key derivation."), alg); |
| 1313 | else |
| 1314 | smsg(_("xchacha20v2: using default algorithm \"%d\" for Key derivation."), alg); |
| 1315 | verbose_leave(); |
| 1316 | } |
| 1317 | } |
| 1318 | #endif |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 1319 | # endif |
| 1320 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1321 | #endif // FEAT_CRYPT |