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 |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 37 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 38 | int works_inplace; // encryption/decryption can be done in-place |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 39 | #endif |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 40 | int whole_undofile; // whole undo file is encrypted |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 41 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 42 | // Optional function pointer for a self-test. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 43 | int (* self_test_fn)(); |
| 44 | |
Bram Moolenaar | ad3ec76 | 2019-04-21 00:00:13 +0200 | [diff] [blame] | 45 | // Function pointer for initializing encryption/decryption. |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 46 | int (* init_fn)(cryptstate_T *state, char_u *key, |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 47 | char_u *salt, int salt_len, char_u *seed, int seed_len); |
| 48 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 49 | // Function pointers for encoding/decoding from one buffer into another. |
| 50 | // Optional, however, these or the _buffer ones should be configured. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 51 | void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 52 | char_u *to, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 53 | void (*decode_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 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 56 | // Function pointers for encoding and decoding, can buffer data if needed. |
| 57 | // Optional (however, these or the above should be configured). |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 58 | 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] | 59 | char_u **newptr, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 60 | 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] | 61 | char_u **newptr, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 62 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 63 | // Function pointers for in-place encoding and decoding, used for |
| 64 | // crypt_*_inplace(). "from" and "to" arguments will be equal. |
| 65 | // These may be the same as decode_fn and encode_fn above, however an |
| 66 | // algorithm may implement them in a way that is not interchangeable with |
| 67 | // the crypt_(en|de)code() interface (for example because it wishes to add |
| 68 | // padding to files). |
| 69 | // 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] | 70 | 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] | 71 | char_u *p2, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 72 | 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] | 73 | char_u *p2, int last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 74 | } cryptmethod_T; |
| 75 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 76 | // index is method_nr of cryptstate_T, CRYPT_M_* |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 77 | static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 78 | // PK_Zip; very weak |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 79 | { |
| 80 | "zip", |
| 81 | "VimCrypt~01!", |
| 82 | 0, |
| 83 | 0, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 84 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 85 | TRUE, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 86 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 87 | FALSE, |
| 88 | NULL, |
| 89 | crypt_zip_init, |
| 90 | crypt_zip_encode, crypt_zip_decode, |
| 91 | NULL, NULL, |
| 92 | crypt_zip_encode, crypt_zip_decode, |
| 93 | }, |
| 94 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 95 | // Blowfish/CFB + SHA-256 custom key derivation; implementation issues. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 96 | { |
| 97 | "blowfish", |
| 98 | "VimCrypt~02!", |
| 99 | 8, |
| 100 | 8, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 101 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 102 | TRUE, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 103 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 104 | FALSE, |
| 105 | blowfish_self_test, |
| 106 | crypt_blowfish_init, |
| 107 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 108 | NULL, NULL, |
| 109 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 110 | }, |
| 111 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 112 | // Blowfish/CFB + SHA-256 custom key derivation; fixed. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 113 | { |
| 114 | "blowfish2", |
| 115 | "VimCrypt~03!", |
| 116 | 8, |
| 117 | 8, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 118 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 119 | TRUE, |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 120 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 121 | TRUE, |
| 122 | blowfish_self_test, |
| 123 | crypt_blowfish_init, |
| 124 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 125 | NULL, NULL, |
| 126 | crypt_blowfish_encode, crypt_blowfish_decode, |
| 127 | }, |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 128 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 129 | // XChaCha20 using libsodium |
| 130 | { |
| 131 | "xchacha20", |
| 132 | "VimCrypt~04!", |
| 133 | #ifdef FEAT_SODIUM |
| 134 | crypto_pwhash_argon2id_SALTBYTES, // 16 |
| 135 | #else |
| 136 | 16, |
| 137 | #endif |
| 138 | 8, |
| 139 | #ifdef CRYPT_NOT_INPLACE |
| 140 | FALSE, |
| 141 | #endif |
| 142 | FALSE, |
| 143 | NULL, |
| 144 | crypt_sodium_init, |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 145 | NULL, NULL, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 146 | crypt_sodium_buffer_encode, crypt_sodium_buffer_decode, |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 147 | NULL, NULL, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 148 | }, |
| 149 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 150 | // NOTE: when adding a new method, use some random bytes for the magic key, |
| 151 | // to avoid that a text file is recognized as encrypted. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 154 | #ifdef FEAT_SODIUM |
| 155 | typedef struct { |
| 156 | size_t count; |
| 157 | unsigned char key[crypto_box_SEEDBYTES]; |
| 158 | // 32, same as crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 159 | crypto_secretstream_xchacha20poly1305_state |
| 160 | state; |
| 161 | } sodium_state_T; |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 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 |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 264 | #endif |
| 265 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 266 | #define CRYPT_MAGIC_LEN 12 // cannot change |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 267 | static char crypt_magic_head[] = "VimCrypt~"; |
| 268 | |
| 269 | /* |
| 270 | * Return int value for crypt method name. |
| 271 | * 0 for "zip", the old method. Also for any non-valid value. |
| 272 | * 1 for "blowfish". |
| 273 | * 2 for "blowfish2". |
| 274 | */ |
| 275 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 276 | crypt_method_nr_from_name(char_u *name) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 277 | { |
| 278 | int i; |
| 279 | |
| 280 | for (i = 0; i < CRYPT_M_COUNT; ++i) |
| 281 | if (STRCMP(name, cryptmethods[i].name) == 0) |
| 282 | return i; |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Get the crypt method used for a file from "ptr[len]", the magic text at the |
| 288 | * start of the file. |
| 289 | * Returns -1 when no encryption used. |
| 290 | */ |
| 291 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 292 | crypt_method_nr_from_magic(char *ptr, int len) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 293 | { |
| 294 | int i; |
| 295 | |
| 296 | if (len < CRYPT_MAGIC_LEN) |
| 297 | return -1; |
| 298 | |
| 299 | for (i = 0; i < CRYPT_M_COUNT; i++) |
| 300 | if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0) |
| 301 | return i; |
| 302 | |
| 303 | i = (int)STRLEN(crypt_magic_head); |
| 304 | if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0) |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 305 | emsg(_(e_file_is_encrypted_with_unknown_method)); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 306 | |
| 307 | return -1; |
| 308 | } |
| 309 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 310 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 311 | /* |
| 312 | * Return TRUE if the crypt method for "method_nr" can be done in-place. |
| 313 | */ |
| 314 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 315 | crypt_works_inplace(cryptstate_T *state) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 316 | { |
| 317 | return cryptmethods[state->method_nr].works_inplace; |
| 318 | } |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 319 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * Get the crypt method for buffer "buf" as a number. |
| 323 | */ |
| 324 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 325 | crypt_get_method_nr(buf_T *buf) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 326 | { |
| 327 | return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Return TRUE when the buffer uses an encryption method that encrypts the |
| 332 | * whole undo file, not only the text. |
| 333 | */ |
| 334 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 335 | crypt_whole_undofile(int method_nr) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 336 | { |
| 337 | return cryptmethods[method_nr].whole_undofile; |
| 338 | } |
| 339 | |
| 340 | /* |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 341 | * Get crypt method specific length of the file header in bytes. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 342 | */ |
| 343 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 344 | crypt_get_header_len(int method_nr) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 345 | { |
| 346 | return CRYPT_MAGIC_LEN |
| 347 | + cryptmethods[method_nr].salt_len |
| 348 | + cryptmethods[method_nr].seed_len; |
| 349 | } |
| 350 | |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 351 | |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 352 | #if defined(FEAT_SODIUM) || defined(PROTO) |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 353 | /* |
| 354 | * Get maximum crypt method specific length of the file header in bytes. |
| 355 | */ |
| 356 | int |
| 357 | crypt_get_max_header_len() |
| 358 | { |
| 359 | int i; |
| 360 | int max = 0; |
| 361 | int temp = 0; |
| 362 | |
| 363 | for (i = 0; i < CRYPT_M_COUNT; ++i) |
| 364 | { |
| 365 | temp = crypt_get_header_len(i); |
| 366 | if (temp > max) |
| 367 | max = temp; |
| 368 | } |
| 369 | return max; |
| 370 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 371 | #endif |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 372 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 373 | /* |
| 374 | * Set the crypt method for buffer "buf" to "method_nr" using the int value as |
| 375 | * returned by crypt_method_nr_from_name(). |
| 376 | */ |
| 377 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 378 | crypt_set_cm_option(buf_T *buf, int method_nr) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 379 | { |
| 380 | free_string_option(buf->b_p_cm); |
| 381 | buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * If the crypt method for the current buffer has a self-test, run it and |
| 386 | * return OK/FAIL. |
| 387 | */ |
| 388 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 389 | crypt_self_test(void) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 390 | { |
| 391 | int method_nr = crypt_get_method_nr(curbuf); |
| 392 | |
| 393 | if (cryptmethods[method_nr].self_test_fn == NULL) |
| 394 | return OK; |
| 395 | return cryptmethods[method_nr].self_test_fn(); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Allocate a crypt state and initialize it. |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 400 | * Return NULL for failure. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 401 | */ |
| 402 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 403 | crypt_create( |
| 404 | int method_nr, |
| 405 | char_u *key, |
| 406 | char_u *salt, |
| 407 | int salt_len, |
| 408 | char_u *seed, |
| 409 | int seed_len) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 410 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 411 | cryptstate_T *state = ALLOC_ONE(cryptstate_T); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 412 | |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 413 | if (state == NULL) |
| 414 | return state; |
| 415 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 416 | state->method_nr = method_nr; |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 417 | if (cryptmethods[method_nr].init_fn( |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 418 | state, key, salt, salt_len, seed, seed_len) == FAIL) |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 419 | { |
| 420 | vim_free(state); |
| 421 | return NULL; |
| 422 | } |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 423 | return state; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Allocate a crypt state from a file header and initialize it. |
| 428 | * Assumes that header contains at least the number of bytes that |
| 429 | * crypt_get_header_len() returns for "method_nr". |
| 430 | */ |
| 431 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 432 | crypt_create_from_header( |
| 433 | int method_nr, |
| 434 | char_u *key, |
| 435 | char_u *header) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 436 | { |
| 437 | char_u *salt = NULL; |
| 438 | char_u *seed = NULL; |
| 439 | int salt_len = cryptmethods[method_nr].salt_len; |
| 440 | int seed_len = cryptmethods[method_nr].seed_len; |
| 441 | |
| 442 | if (salt_len > 0) |
| 443 | salt = header + CRYPT_MAGIC_LEN; |
| 444 | if (seed_len > 0) |
| 445 | seed = header + CRYPT_MAGIC_LEN + salt_len; |
| 446 | |
| 447 | return crypt_create(method_nr, key, salt, salt_len, seed, seed_len); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Read the crypt method specific header data from "fp". |
| 452 | * Return an allocated cryptstate_T or NULL on error. |
| 453 | */ |
| 454 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 455 | crypt_create_from_file(FILE *fp, char_u *key) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 456 | { |
| 457 | int method_nr; |
| 458 | int header_len; |
| 459 | char magic_buffer[CRYPT_MAGIC_LEN]; |
| 460 | char_u *buffer; |
| 461 | cryptstate_T *state; |
| 462 | |
| 463 | if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) |
| 464 | return NULL; |
| 465 | method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN); |
| 466 | if (method_nr < 0) |
| 467 | return NULL; |
| 468 | |
| 469 | header_len = crypt_get_header_len(method_nr); |
| 470 | if ((buffer = alloc(header_len)) == NULL) |
| 471 | return NULL; |
| 472 | mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN); |
| 473 | if (header_len > CRYPT_MAGIC_LEN |
| 474 | && fread(buffer + CRYPT_MAGIC_LEN, |
| 475 | header_len - CRYPT_MAGIC_LEN, 1, fp) != 1) |
| 476 | { |
| 477 | vim_free(buffer); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | state = crypt_create_from_header(method_nr, key, buffer); |
| 482 | vim_free(buffer); |
| 483 | return state; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Allocate a cryptstate_T for writing and initialize it with "key". |
| 488 | * Allocates and fills in the header and stores it in "header", setting |
| 489 | * "header_len". The header may include salt and seed, depending on |
| 490 | * cryptmethod. Caller must free header. |
| 491 | * Returns the state or NULL on failure. |
| 492 | */ |
| 493 | cryptstate_T * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 494 | crypt_create_for_writing( |
| 495 | int method_nr, |
| 496 | char_u *key, |
| 497 | char_u **header, |
| 498 | int *header_len) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 499 | { |
| 500 | int len = crypt_get_header_len(method_nr); |
| 501 | char_u *salt = NULL; |
| 502 | char_u *seed = NULL; |
| 503 | int salt_len = cryptmethods[method_nr].salt_len; |
| 504 | int seed_len = cryptmethods[method_nr].seed_len; |
| 505 | cryptstate_T *state; |
| 506 | |
| 507 | *header_len = len; |
| 508 | *header = alloc(len); |
| 509 | if (*header == NULL) |
| 510 | return NULL; |
| 511 | |
| 512 | mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN); |
| 513 | if (salt_len > 0 || seed_len > 0) |
| 514 | { |
| 515 | if (salt_len > 0) |
| 516 | salt = *header + CRYPT_MAGIC_LEN; |
| 517 | if (seed_len > 0) |
| 518 | seed = *header + CRYPT_MAGIC_LEN + salt_len; |
| 519 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 520 | // TODO: Should this be crypt method specific? (Probably not worth |
| 521 | // it). sha2_seed is pretty bad for large amounts of entropy, so make |
| 522 | // that into something which is suitable for anything. |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 523 | #ifdef FEAT_SODIUM |
| 524 | if (sodium_init() >= 0) |
| 525 | { |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 526 | if (salt_len > 0) |
| 527 | randombytes_buf(salt, salt_len); |
| 528 | if (seed_len > 0) |
| 529 | randombytes_buf(seed, seed_len); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 530 | } |
| 531 | else |
| 532 | #endif |
| 533 | sha2_seed(salt, salt_len, seed, seed_len); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 534 | } |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 535 | state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len); |
| 536 | if (state == NULL) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 537 | VIM_CLEAR(*header); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 538 | return state; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Free the crypt state. |
| 543 | */ |
| 544 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 545 | crypt_free_state(cryptstate_T *state) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 546 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 547 | #ifdef FEAT_SODIUM |
| 548 | if (state->method_nr == CRYPT_M_SOD) |
| 549 | { |
Bram Moolenaar | 131530a | 2021-07-29 20:37:49 +0200 | [diff] [blame] | 550 | sodium_munlock(((sodium_state_T *)state->method_state)->key, |
| 551 | crypto_box_SEEDBYTES); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 552 | sodium_memzero(state->method_state, sizeof(sodium_state_T)); |
| 553 | sodium_free(state->method_state); |
| 554 | } |
| 555 | else |
| 556 | #endif |
| 557 | vim_free(state->method_state); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 558 | vim_free(state); |
| 559 | } |
| 560 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 561 | #ifdef CRYPT_NOT_INPLACE |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 562 | /* |
| 563 | * Encode "from[len]" and store the result in a newly allocated buffer, which |
| 564 | * is stored in "newptr". |
| 565 | * Return number of bytes in "newptr", 0 for need more or -1 on error. |
| 566 | */ |
| 567 | long |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 568 | crypt_encode_alloc( |
| 569 | cryptstate_T *state, |
| 570 | char_u *from, |
| 571 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 572 | char_u **newptr, |
| 573 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 574 | { |
| 575 | cryptmethod_T *method = &cryptmethods[state->method_nr]; |
| 576 | |
| 577 | if (method->encode_buffer_fn != NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 578 | // Has buffer function, pass through. |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 579 | return method->encode_buffer_fn(state, from, len, newptr, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 580 | if (len == 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 581 | // Not buffering, just return EOF. |
Bram Moolenaar | 9b8f021 | 2014-08-13 22:05:53 +0200 | [diff] [blame] | 582 | return (long)len; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 583 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 584 | *newptr = alloc(len + 50); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 585 | if (*newptr == NULL) |
| 586 | return -1; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 587 | method->encode_fn(state, from, len, *newptr, last); |
Bram Moolenaar | 9b8f021 | 2014-08-13 22:05:53 +0200 | [diff] [blame] | 588 | return (long)len; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which |
| 593 | * is stored in "newptr". |
| 594 | * Return number of bytes in "newptr", 0 for need more or -1 on error. |
| 595 | */ |
| 596 | long |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 597 | crypt_decode_alloc( |
| 598 | cryptstate_T *state, |
| 599 | char_u *ptr, |
| 600 | long len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 601 | char_u **newptr, |
| 602 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 603 | { |
| 604 | cryptmethod_T *method = &cryptmethods[state->method_nr]; |
| 605 | |
| 606 | if (method->decode_buffer_fn != NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 607 | // Has buffer function, pass through. |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 608 | return method->decode_buffer_fn(state, ptr, len, newptr, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 609 | |
| 610 | if (len == 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 611 | // Not buffering, just return EOF. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 612 | return len; |
| 613 | |
| 614 | *newptr = alloc(len); |
| 615 | if (*newptr == NULL) |
| 616 | return -1; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 617 | method->decode_fn(state, ptr, len, *newptr, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 618 | return len; |
| 619 | } |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 620 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Encrypting "from[len]" into "to[len]". |
| 624 | */ |
| 625 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 626 | crypt_encode( |
| 627 | cryptstate_T *state, |
| 628 | char_u *from, |
| 629 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 630 | char_u *to, |
| 631 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 632 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 633 | cryptmethods[state->method_nr].encode_fn(state, from, len, to, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 636 | #if 0 // unused |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 637 | /* |
| 638 | * decrypting "from[len]" into "to[len]". |
| 639 | */ |
| 640 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 641 | crypt_decode( |
| 642 | cryptstate_T *state, |
| 643 | char_u *from, |
| 644 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 645 | char_u *to, |
| 646 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 647 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 648 | cryptmethods[state->method_nr].decode_fn(state, from, len, to, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 649 | } |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 650 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 651 | |
| 652 | /* |
| 653 | * Simple inplace encryption, modifies "buf[len]" in place. |
| 654 | */ |
| 655 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 656 | crypt_encode_inplace( |
| 657 | cryptstate_T *state, |
| 658 | char_u *buf, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 659 | size_t len, |
| 660 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 661 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 662 | cryptmethods[state->method_nr].encode_inplace_fn(state, buf, len, |
| 663 | buf, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Simple inplace decryption, modifies "buf[len]" in place. |
| 668 | */ |
| 669 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 670 | crypt_decode_inplace( |
| 671 | cryptstate_T *state, |
| 672 | char_u *buf, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 673 | size_t len, |
| 674 | int last) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 675 | { |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 676 | cryptmethods[state->method_nr].decode_inplace_fn(state, buf, len, |
| 677 | buf, last); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Free an allocated crypt key. Clear the text to make sure it doesn't stay |
| 682 | * in memory anywhere. |
| 683 | */ |
| 684 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 685 | crypt_free_key(char_u *key) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 686 | { |
| 687 | char_u *p; |
| 688 | |
| 689 | if (key != NULL) |
| 690 | { |
| 691 | for (p = key; *p != NUL; ++p) |
| 692 | *p = 0; |
| 693 | vim_free(key); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 698 | * Check the crypt method and give a warning if it's outdated. |
| 699 | */ |
| 700 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 701 | crypt_check_method(int method) |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 702 | { |
| 703 | if (method < CRYPT_M_BF2) |
| 704 | { |
| 705 | msg_scroll = TRUE; |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 706 | msg(_("Warning: Using a weak encryption method; see :help 'cm'")); |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 707 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | #ifdef FEAT_SODIUM |
| 711 | static void |
| 712 | crypt_check_swapfile_curbuf(void) |
| 713 | { |
| 714 | int method = crypt_get_method_nr(curbuf); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 715 | if (method == CRYPT_M_SOD) |
| 716 | { |
| 717 | // encryption uses padding and MAC, that does not work very well with |
| 718 | // swap and undo files, so disable them |
| 719 | mf_close_file(curbuf, TRUE); // remove the swap file |
| 720 | set_option_value((char_u *)"swf", 0, NULL, OPT_LOCAL); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 721 | msg_scroll = TRUE; |
Christian Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 722 | msg(_("Note: Encryption of swapfile not supported, disabling swap file")); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 723 | } |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 724 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 725 | #endif |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 726 | |
| 727 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 728 | crypt_check_current_method(void) |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 729 | { |
| 730 | crypt_check_method(crypt_get_method_nr(curbuf)); |
| 731 | } |
| 732 | |
| 733 | /* |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 734 | * Ask the user for a crypt key. |
| 735 | * When "store" is TRUE, the new key is stored in the 'key' option, and the |
| 736 | * 'key' option value is returned: Don't free it. |
| 737 | * When "store" is FALSE, the typed key is returned in allocated memory. |
| 738 | * Returns NULL on failure. |
| 739 | */ |
| 740 | char_u * |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 741 | crypt_get_key( |
| 742 | int store, |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 743 | int twice) // Ask for the key twice. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 744 | { |
| 745 | char_u *p1, *p2 = NULL; |
| 746 | int round; |
| 747 | |
| 748 | for (round = 0; ; ++round) |
| 749 | { |
| 750 | cmdline_star = TRUE; |
| 751 | cmdline_row = msg_row; |
| 752 | p1 = getcmdline_prompt(NUL, round == 0 |
| 753 | ? (char_u *)_("Enter encryption key: ") |
| 754 | : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING, |
| 755 | NULL); |
| 756 | cmdline_star = FALSE; |
| 757 | |
| 758 | if (p1 == NULL) |
| 759 | break; |
| 760 | |
| 761 | if (round == twice) |
| 762 | { |
| 763 | if (p2 != NULL && STRCMP(p1, p2) != 0) |
| 764 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 765 | msg(_("Keys don't match!")); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 766 | crypt_free_key(p1); |
| 767 | crypt_free_key(p2); |
| 768 | p2 = NULL; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 769 | round = -1; // do it again |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 770 | continue; |
| 771 | } |
| 772 | |
| 773 | if (store) |
| 774 | { |
| 775 | set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL); |
| 776 | crypt_free_key(p1); |
| 777 | p1 = curbuf->b_p_key; |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 778 | #ifdef FEAT_SODIUM |
| 779 | crypt_check_swapfile_curbuf(); |
| 780 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 781 | } |
| 782 | break; |
| 783 | } |
| 784 | p2 = p1; |
| 785 | } |
| 786 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 787 | // since the user typed this, no need to wait for return |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 788 | if (crypt_get_method_nr(curbuf) != CRYPT_M_SOD) |
| 789 | { |
| 790 | if (msg_didout) |
| 791 | msg_putchar('\n'); |
| 792 | need_wait_return = FALSE; |
| 793 | msg_didout = FALSE; |
| 794 | } |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 795 | |
| 796 | crypt_free_key(p2); |
| 797 | return p1; |
| 798 | } |
| 799 | |
| 800 | |
| 801 | /* |
| 802 | * Append a message to IObuff for the encryption/decryption method being used. |
| 803 | */ |
| 804 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 805 | crypt_append_msg( |
| 806 | buf_T *buf) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 807 | { |
| 808 | if (crypt_get_method_nr(buf) == 0) |
| 809 | STRCAT(IObuff, _("[crypted]")); |
| 810 | else |
| 811 | { |
| 812 | STRCAT(IObuff, "["); |
| 813 | STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); |
| 814 | STRCAT(IObuff, "]"); |
| 815 | } |
| 816 | } |
| 817 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 818 | int |
| 819 | crypt_sodium_init( |
| 820 | cryptstate_T *state UNUSED, |
| 821 | char_u *key UNUSED, |
| 822 | char_u *salt UNUSED, |
| 823 | int salt_len UNUSED, |
| 824 | char_u *seed UNUSED, |
| 825 | int seed_len UNUSED) |
| 826 | { |
| 827 | # ifdef FEAT_SODIUM |
| 828 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 829 | unsigned char dkey[crypto_box_SEEDBYTES]; // 32 |
| 830 | sodium_state_T *sd_state; |
Bram Moolenaar | 131530a | 2021-07-29 20:37:49 +0200 | [diff] [blame] | 831 | int retval = 0; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 832 | |
| 833 | if (sodium_init() < 0) |
| 834 | return FAIL; |
| 835 | |
| 836 | sd_state = (sodium_state_T *)sodium_malloc(sizeof(sodium_state_T)); |
| 837 | sodium_memzero(sd_state, sizeof(sodium_state_T)); |
| 838 | |
| 839 | // derive a key from the password |
| 840 | if (crypto_pwhash(dkey, sizeof(dkey), (const char *)key, STRLEN(key), salt, |
| 841 | crypto_pwhash_OPSLIMIT_INTERACTIVE, crypto_pwhash_MEMLIMIT_INTERACTIVE, |
| 842 | crypto_pwhash_ALG_DEFAULT) != 0) |
| 843 | { |
| 844 | // out of memory |
| 845 | sodium_free(sd_state); |
| 846 | return FAIL; |
| 847 | } |
| 848 | memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES); |
Bram Moolenaar | 131530a | 2021-07-29 20:37:49 +0200 | [diff] [blame] | 849 | |
| 850 | retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES); |
| 851 | retval += sodium_mlock(key, STRLEN(key)); |
| 852 | |
| 853 | if (retval < 0) |
| 854 | { |
| 855 | emsg(_(e_encryption_sodium_mlock_failed)); |
| 856 | sodium_free(sd_state); |
| 857 | return FAIL; |
| 858 | } |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 859 | sd_state->count = 0; |
| 860 | state->method_state = sd_state; |
| 861 | |
| 862 | return OK; |
| 863 | # else |
| 864 | emsg(e_libsodium_not_built_in); |
| 865 | return FAIL; |
| 866 | # endif |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Encrypt "from[len]" into "to[len]". |
| 871 | * "from" and "to" can be equal to encrypt in place. |
| 872 | * Call needs to ensure that there is enough space in to (for the header) |
| 873 | */ |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 874 | #if 0 // Currently unused |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 875 | void |
| 876 | crypt_sodium_encode( |
| 877 | cryptstate_T *state UNUSED, |
| 878 | char_u *from UNUSED, |
| 879 | size_t len UNUSED, |
| 880 | char_u *to UNUSED, |
| 881 | int last UNUSED) |
| 882 | { |
| 883 | # ifdef FEAT_SODIUM |
| 884 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 885 | sodium_state_T *sod_st = state->method_state; |
| 886 | unsigned char tag = last |
| 887 | ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; |
| 888 | |
| 889 | if (sod_st->count == 0) |
| 890 | { |
| 891 | if (len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
| 892 | { |
| 893 | emsg(e_libsodium_cannot_encrypt_header); |
| 894 | return; |
| 895 | } |
| 896 | crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state, |
| 897 | to, sod_st->key); |
| 898 | to += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 899 | } |
| 900 | |
| 901 | if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES) |
| 902 | { |
| 903 | emsg(e_libsodium_cannot_encrypt_buffer); |
| 904 | return; |
| 905 | } |
| 906 | |
| 907 | crypto_secretstream_xchacha20poly1305_push(&sod_st->state, to, NULL, |
| 908 | from, len, NULL, 0, tag); |
| 909 | |
| 910 | sod_st->count++; |
| 911 | # endif |
| 912 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 913 | #endif |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 914 | |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 915 | /* |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 916 | * Decrypt "from[len]" into "to[len]". |
| 917 | * "from" and "to" can be equal to encrypt in place. |
| 918 | */ |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 919 | #if 0 // Currently unused |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 920 | void |
| 921 | crypt_sodium_decode( |
| 922 | cryptstate_T *state UNUSED, |
| 923 | char_u *from UNUSED, |
| 924 | size_t len UNUSED, |
| 925 | char_u *to UNUSED, |
| 926 | int last UNUSED) |
| 927 | { |
| 928 | # ifdef FEAT_SODIUM |
| 929 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 930 | sodium_state_T *sod_st = state->method_state; |
| 931 | unsigned char tag; |
| 932 | unsigned long long buf_len; |
| 933 | char_u *p1 = from; |
| 934 | char_u *p2 = to; |
| 935 | char_u *buf_out; |
| 936 | |
| 937 | if (sod_st->count == 0 |
| 938 | && len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
| 939 | { |
| 940 | emsg(e_libsodium_cannot_decrypt_header); |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | buf_out = (char_u *)alloc(len); |
| 945 | |
| 946 | if (buf_out == NULL) |
| 947 | { |
| 948 | emsg(e_libsodium_cannot_allocate_buffer); |
| 949 | return; |
| 950 | } |
| 951 | if (sod_st->count == 0) |
| 952 | { |
| 953 | if (crypto_secretstream_xchacha20poly1305_init_pull( |
| 954 | &sod_st->state, from, sod_st->key) != 0) |
| 955 | { |
| 956 | emsg(e_libsodium_decryption_failed_header_incomplete); |
| 957 | goto fail; |
| 958 | } |
| 959 | |
| 960 | from += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 961 | len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 962 | |
| 963 | if (p1 == p2) |
| 964 | to += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 965 | } |
| 966 | |
| 967 | if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES) |
| 968 | { |
| 969 | emsg(e_libsodium_cannot_decrypt_buffer); |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 970 | goto fail; |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 971 | } |
| 972 | if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state, |
| 973 | buf_out, &buf_len, &tag, from, len, NULL, 0) != 0) |
| 974 | { |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 975 | emsg(e_libsodium_decryption_failed); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 976 | goto fail; |
| 977 | } |
| 978 | sod_st->count++; |
| 979 | |
| 980 | if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last) |
| 981 | { |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 982 | emsg(e_libsodium_decryption_failed_premature); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 983 | goto fail; |
| 984 | } |
| 985 | if (p1 == p2) |
| 986 | mch_memmove(p2, buf_out, buf_len); |
| 987 | |
| 988 | fail: |
| 989 | vim_free(buf_out); |
| 990 | # endif |
| 991 | } |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 992 | #endif |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 993 | |
| 994 | /* |
| 995 | * Encrypt "from[len]" into "to[len]". |
| 996 | * "from" and "to" can be equal to encrypt in place. |
| 997 | */ |
| 998 | long |
| 999 | crypt_sodium_buffer_encode( |
| 1000 | cryptstate_T *state UNUSED, |
| 1001 | char_u *from UNUSED, |
| 1002 | size_t len UNUSED, |
| 1003 | char_u **buf_out UNUSED, |
| 1004 | int last UNUSED) |
| 1005 | { |
| 1006 | # ifdef FEAT_SODIUM |
| 1007 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 1008 | unsigned long long out_len; |
| 1009 | char_u *ptr; |
| 1010 | unsigned char tag = last |
| 1011 | ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; |
| 1012 | int length; |
| 1013 | sodium_state_T *sod_st = state->method_state; |
| 1014 | int first = (sod_st->count == 0); |
| 1015 | |
Christian Brabandt | 226b28b | 2021-06-21 21:08:08 +0200 | [diff] [blame] | 1016 | length = (int)len + crypto_secretstream_xchacha20poly1305_ABYTES |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1017 | + (first ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0); |
| 1018 | *buf_out = alloc_clear(length); |
| 1019 | if (*buf_out == NULL) |
| 1020 | { |
| 1021 | emsg(e_libsodium_cannot_allocate_buffer); |
| 1022 | return -1; |
| 1023 | } |
| 1024 | ptr = *buf_out; |
| 1025 | |
| 1026 | if (first) |
| 1027 | { |
| 1028 | crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state, |
| 1029 | ptr, sod_st->key); |
| 1030 | ptr += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1031 | } |
| 1032 | |
| 1033 | crypto_secretstream_xchacha20poly1305_push(&sod_st->state, ptr, |
| 1034 | &out_len, from, len, NULL, 0, tag); |
| 1035 | |
| 1036 | sod_st->count++; |
| 1037 | return out_len + (first |
| 1038 | ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0); |
| 1039 | # else |
| 1040 | return -1; |
| 1041 | # endif |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * Decrypt "from[len]" into "to[len]". |
| 1046 | * "from" and "to" can be equal to encrypt in place. |
| 1047 | */ |
| 1048 | long |
| 1049 | crypt_sodium_buffer_decode( |
| 1050 | cryptstate_T *state UNUSED, |
| 1051 | char_u *from UNUSED, |
| 1052 | size_t len UNUSED, |
| 1053 | char_u **buf_out UNUSED, |
| 1054 | int last UNUSED) |
| 1055 | { |
| 1056 | # ifdef FEAT_SODIUM |
| 1057 | // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
| 1058 | sodium_state_T *sod_st = state->method_state; |
| 1059 | unsigned char tag; |
| 1060 | unsigned long long out_len; |
| 1061 | *buf_out = alloc_clear(len); |
| 1062 | if (*buf_out == NULL) |
| 1063 | { |
| 1064 | emsg(e_libsodium_cannot_allocate_buffer); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | |
| 1068 | if (sod_st->count == 0) |
| 1069 | { |
| 1070 | if (crypto_secretstream_xchacha20poly1305_init_pull(&sod_st->state, |
| 1071 | from, sod_st->key) != 0) |
| 1072 | { |
| 1073 | emsg(e_libsodium_decryption_failed_header_incomplete); |
| 1074 | return -1; |
| 1075 | } |
| 1076 | from += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1077 | len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
| 1078 | sod_st->count++; |
| 1079 | } |
| 1080 | if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state, |
| 1081 | *buf_out, &out_len, &tag, from, len, NULL, 0) != 0) |
| 1082 | { |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 1083 | emsg(e_libsodium_decryption_failed); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1084 | return -1; |
| 1085 | } |
| 1086 | |
| 1087 | if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last) |
Dominique Pelle | cb54bc6 | 2021-06-21 20:15:37 +0200 | [diff] [blame] | 1088 | emsg(e_libsodium_decryption_failed_premature); |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 1089 | return (long) out_len; |
| 1090 | # else |
| 1091 | return -1; |
| 1092 | # endif |
| 1093 | } |
| 1094 | |
K.Takata | 1a8825d | 2022-01-19 13:32:57 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1109 | #endif // FEAT_CRYPT |