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