Mercurial > vim
annotate src/crypt.c @ 17616:83ad219cc133 v8.1.1805
patch 8.1.1805: au_did_filetype is declared twice
commit https://github.com/vim/vim/commit/6cd57d44669c02af9195f5601b882edd435b47e8
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Aug 3 23:08:14 2019 +0200
patch 8.1.1805: au_did_filetype is declared twice
Problem: Au_did_filetype is declared twice.
Solution: Remove it from autocmd.c. (closes https://github.com/vim/vim/issues/4767)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 03 Aug 2019 23:15:06 +0200 |
parents | ce04ebdf26b8 |
children | 9e6d5a4abb1c |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
7817
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
6122 | 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 { | |
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 */ | |
36 int seed_len; /* length of seed, or 0 when not using salt */ | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
37 #ifdef CRYPT_NOT_INPLACE |
6122 | 38 int works_inplace; /* encryption/decryption can be done in-place */ |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
39 #endif |
6122 | 40 int whole_undofile; /* whole undo file is encrypted */ |
41 | |
42 /* Optional function pointer for a self-test. */ | |
43 int (* self_test_fn)(); | |
44 | |
16378
3d6b282e2d6e
patch 8.1.1194: typos and small problems in source files
Bram Moolenaar <Bram@vim.org>
parents:
15967
diff
changeset
|
45 // Function pointer for initializing encryption/decryption. |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
46 int (* init_fn)(cryptstate_T *state, char_u *key, |
6122 | 47 char_u *salt, int salt_len, char_u *seed, int seed_len); |
48 | |
49 /* Function pointers for encoding/decoding from one buffer into another. | |
50 * Optional, however, these or the _buffer ones should be configured. */ | |
51 void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len, | |
52 char_u *to); | |
53 void (*decode_fn)(cryptstate_T *state, char_u *from, size_t len, | |
54 char_u *to); | |
55 | |
56 /* Function pointers for encoding and decoding, can buffer data if needed. | |
57 * Optional (however, these or the above should be configured). */ | |
58 long (*encode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, | |
59 char_u **newptr); | |
60 long (*decode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, | |
61 char_u **newptr); | |
62 | |
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. | |
70 */ | |
71 void (*encode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, | |
72 char_u *p2); | |
73 void (*decode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, | |
74 char_u *p2); | |
75 } cryptmethod_T; | |
76 | |
77 /* index is method_nr of cryptstate_T, CRYPT_M_* */ | |
78 static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = { | |
79 /* PK_Zip; very weak */ | |
80 { | |
81 "zip", | |
82 "VimCrypt~01!", | |
83 0, | |
84 0, | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
85 #ifdef CRYPT_NOT_INPLACE |
6122 | 86 TRUE, |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
87 #endif |
6122 | 88 FALSE, |
89 NULL, | |
90 crypt_zip_init, | |
91 crypt_zip_encode, crypt_zip_decode, | |
92 NULL, NULL, | |
93 crypt_zip_encode, crypt_zip_decode, | |
94 }, | |
95 | |
96 /* Blowfish/CFB + SHA-256 custom key derivation; implementation issues. */ | |
97 { | |
98 "blowfish", | |
99 "VimCrypt~02!", | |
100 8, | |
101 8, | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
102 #ifdef CRYPT_NOT_INPLACE |
6122 | 103 TRUE, |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
104 #endif |
6122 | 105 FALSE, |
106 blowfish_self_test, | |
107 crypt_blowfish_init, | |
108 crypt_blowfish_encode, crypt_blowfish_decode, | |
109 NULL, NULL, | |
110 crypt_blowfish_encode, crypt_blowfish_decode, | |
111 }, | |
112 | |
113 /* Blowfish/CFB + SHA-256 custom key derivation; fixed. */ | |
114 { | |
115 "blowfish2", | |
116 "VimCrypt~03!", | |
117 8, | |
118 8, | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
119 #ifdef CRYPT_NOT_INPLACE |
6122 | 120 TRUE, |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
121 #endif |
6122 | 122 TRUE, |
123 blowfish_self_test, | |
124 crypt_blowfish_init, | |
125 crypt_blowfish_encode, crypt_blowfish_decode, | |
126 NULL, NULL, | |
127 crypt_blowfish_encode, crypt_blowfish_decode, | |
128 }, | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
129 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
130 /* NOTE: when adding a new method, use some random bytes for the magic key, |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
131 * to avoid that a text file is recognized as encrypted. */ |
6122 | 132 }; |
133 | |
134 #define CRYPT_MAGIC_LEN 12 /* cannot change */ | |
135 static char crypt_magic_head[] = "VimCrypt~"; | |
136 | |
137 /* | |
138 * Return int value for crypt method name. | |
139 * 0 for "zip", the old method. Also for any non-valid value. | |
140 * 1 for "blowfish". | |
141 * 2 for "blowfish2". | |
142 */ | |
143 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
144 crypt_method_nr_from_name(char_u *name) |
6122 | 145 { |
146 int i; | |
147 | |
148 for (i = 0; i < CRYPT_M_COUNT; ++i) | |
149 if (STRCMP(name, cryptmethods[i].name) == 0) | |
150 return i; | |
151 return 0; | |
152 } | |
153 | |
154 /* | |
155 * Get the crypt method used for a file from "ptr[len]", the magic text at the | |
156 * start of the file. | |
157 * Returns -1 when no encryption used. | |
158 */ | |
159 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
160 crypt_method_nr_from_magic(char *ptr, int len) |
6122 | 161 { |
162 int i; | |
163 | |
164 if (len < CRYPT_MAGIC_LEN) | |
165 return -1; | |
166 | |
167 for (i = 0; i < CRYPT_M_COUNT; i++) | |
168 if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0) | |
169 return i; | |
170 | |
171 i = (int)STRLEN(crypt_magic_head); | |
172 if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
13244
diff
changeset
|
173 emsg(_("E821: File is encrypted with unknown method")); |
6122 | 174 |
175 return -1; | |
176 } | |
177 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
178 #ifdef CRYPT_NOT_INPLACE |
6122 | 179 /* |
180 * Return TRUE if the crypt method for "method_nr" can be done in-place. | |
181 */ | |
182 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
183 crypt_works_inplace(cryptstate_T *state) |
6122 | 184 { |
185 return cryptmethods[state->method_nr].works_inplace; | |
186 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
187 #endif |
6122 | 188 |
189 /* | |
190 * Get the crypt method for buffer "buf" as a number. | |
191 */ | |
192 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
193 crypt_get_method_nr(buf_T *buf) |
6122 | 194 { |
195 return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); | |
196 } | |
197 | |
198 /* | |
199 * Return TRUE when the buffer uses an encryption method that encrypts the | |
200 * whole undo file, not only the text. | |
201 */ | |
202 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
203 crypt_whole_undofile(int method_nr) |
6122 | 204 { |
205 return cryptmethods[method_nr].whole_undofile; | |
206 } | |
207 | |
208 /* | |
209 * Get crypt method specifc length of the file header in bytes. | |
210 */ | |
211 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
212 crypt_get_header_len(int method_nr) |
6122 | 213 { |
214 return CRYPT_MAGIC_LEN | |
215 + cryptmethods[method_nr].salt_len | |
216 + cryptmethods[method_nr].seed_len; | |
217 } | |
218 | |
219 /* | |
220 * Set the crypt method for buffer "buf" to "method_nr" using the int value as | |
221 * returned by crypt_method_nr_from_name(). | |
222 */ | |
223 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
224 crypt_set_cm_option(buf_T *buf, int method_nr) |
6122 | 225 { |
226 free_string_option(buf->b_p_cm); | |
227 buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name); | |
228 } | |
229 | |
230 /* | |
231 * If the crypt method for the current buffer has a self-test, run it and | |
232 * return OK/FAIL. | |
233 */ | |
234 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
235 crypt_self_test(void) |
6122 | 236 { |
237 int method_nr = crypt_get_method_nr(curbuf); | |
238 | |
239 if (cryptmethods[method_nr].self_test_fn == NULL) | |
240 return OK; | |
241 return cryptmethods[method_nr].self_test_fn(); | |
242 } | |
243 | |
244 /* | |
245 * Allocate a crypt state and initialize it. | |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
246 * Return NULL for failure. |
6122 | 247 */ |
248 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
249 crypt_create( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
250 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
251 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
252 char_u *salt, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
253 int salt_len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
254 char_u *seed, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
255 int seed_len) |
6122 | 256 { |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16782
diff
changeset
|
257 cryptstate_T *state = ALLOC_ONE(cryptstate_T); |
6122 | 258 |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
259 if (state == NULL) |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
260 return state; |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
261 |
6122 | 262 state->method_nr = method_nr; |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
263 if (cryptmethods[method_nr].init_fn( |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
264 state, key, salt, salt_len, seed, seed_len) == FAIL) |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
265 { |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
266 vim_free(state); |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
267 return NULL; |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
268 } |
6122 | 269 return state; |
270 } | |
271 | |
272 /* | |
273 * Allocate a crypt state from a file header and initialize it. | |
274 * Assumes that header contains at least the number of bytes that | |
275 * crypt_get_header_len() returns for "method_nr". | |
276 */ | |
277 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
278 crypt_create_from_header( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
279 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
280 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
281 char_u *header) |
6122 | 282 { |
283 char_u *salt = NULL; | |
284 char_u *seed = NULL; | |
285 int salt_len = cryptmethods[method_nr].salt_len; | |
286 int seed_len = cryptmethods[method_nr].seed_len; | |
287 | |
288 if (salt_len > 0) | |
289 salt = header + CRYPT_MAGIC_LEN; | |
290 if (seed_len > 0) | |
291 seed = header + CRYPT_MAGIC_LEN + salt_len; | |
292 | |
293 return crypt_create(method_nr, key, salt, salt_len, seed, seed_len); | |
294 } | |
295 | |
296 /* | |
297 * Read the crypt method specific header data from "fp". | |
298 * Return an allocated cryptstate_T or NULL on error. | |
299 */ | |
300 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
301 crypt_create_from_file(FILE *fp, char_u *key) |
6122 | 302 { |
303 int method_nr; | |
304 int header_len; | |
305 char magic_buffer[CRYPT_MAGIC_LEN]; | |
306 char_u *buffer; | |
307 cryptstate_T *state; | |
308 | |
309 if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) | |
310 return NULL; | |
311 method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN); | |
312 if (method_nr < 0) | |
313 return NULL; | |
314 | |
315 header_len = crypt_get_header_len(method_nr); | |
316 if ((buffer = alloc(header_len)) == NULL) | |
317 return NULL; | |
318 mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN); | |
319 if (header_len > CRYPT_MAGIC_LEN | |
320 && fread(buffer + CRYPT_MAGIC_LEN, | |
321 header_len - CRYPT_MAGIC_LEN, 1, fp) != 1) | |
322 { | |
323 vim_free(buffer); | |
324 return NULL; | |
325 } | |
326 | |
327 state = crypt_create_from_header(method_nr, key, buffer); | |
328 vim_free(buffer); | |
329 return state; | |
330 } | |
331 | |
332 /* | |
333 * Allocate a cryptstate_T for writing and initialize it with "key". | |
334 * Allocates and fills in the header and stores it in "header", setting | |
335 * "header_len". The header may include salt and seed, depending on | |
336 * cryptmethod. Caller must free header. | |
337 * Returns the state or NULL on failure. | |
338 */ | |
339 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
340 crypt_create_for_writing( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
341 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
342 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
343 char_u **header, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
344 int *header_len) |
6122 | 345 { |
346 int len = crypt_get_header_len(method_nr); | |
347 char_u *salt = NULL; | |
348 char_u *seed = NULL; | |
349 int salt_len = cryptmethods[method_nr].salt_len; | |
350 int seed_len = cryptmethods[method_nr].seed_len; | |
351 cryptstate_T *state; | |
352 | |
353 *header_len = len; | |
354 *header = alloc(len); | |
355 if (*header == NULL) | |
356 return NULL; | |
357 | |
358 mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN); | |
359 if (salt_len > 0 || seed_len > 0) | |
360 { | |
361 if (salt_len > 0) | |
362 salt = *header + CRYPT_MAGIC_LEN; | |
363 if (seed_len > 0) | |
364 seed = *header + CRYPT_MAGIC_LEN + salt_len; | |
365 | |
366 /* TODO: Should this be crypt method specific? (Probably not worth | |
367 * it). sha2_seed is pretty bad for large amounts of entropy, so make | |
368 * that into something which is suitable for anything. */ | |
369 sha2_seed(salt, salt_len, seed, seed_len); | |
370 } | |
371 | |
372 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len); | |
373 if (state == NULL) | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
374 VIM_CLEAR(*header); |
6122 | 375 return state; |
376 } | |
377 | |
378 /* | |
379 * Free the crypt state. | |
380 */ | |
381 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
382 crypt_free_state(cryptstate_T *state) |
6122 | 383 { |
384 vim_free(state->method_state); | |
385 vim_free(state); | |
386 } | |
387 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
388 #ifdef CRYPT_NOT_INPLACE |
6122 | 389 /* |
390 * Encode "from[len]" and store the result in a newly allocated buffer, which | |
391 * is stored in "newptr". | |
392 * Return number of bytes in "newptr", 0 for need more or -1 on error. | |
393 */ | |
394 long | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
395 crypt_encode_alloc( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
396 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
397 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
398 size_t len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
399 char_u **newptr) |
6122 | 400 { |
401 cryptmethod_T *method = &cryptmethods[state->method_nr]; | |
402 | |
403 if (method->encode_buffer_fn != NULL) | |
404 /* Has buffer function, pass through. */ | |
405 return method->encode_buffer_fn(state, from, len, newptr); | |
406 if (len == 0) | |
407 /* Not buffering, just return EOF. */ | |
6132 | 408 return (long)len; |
6122 | 409 |
16782
fc58fee685e2
patch 8.1.1393: unnecessary type casts
Bram Moolenaar <Bram@vim.org>
parents:
16429
diff
changeset
|
410 *newptr = alloc(len); |
6122 | 411 if (*newptr == NULL) |
412 return -1; | |
413 method->encode_fn(state, from, len, *newptr); | |
6132 | 414 return (long)len; |
6122 | 415 } |
416 | |
417 /* | |
418 * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which | |
419 * is stored in "newptr". | |
420 * Return number of bytes in "newptr", 0 for need more or -1 on error. | |
421 */ | |
422 long | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
423 crypt_decode_alloc( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
424 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
425 char_u *ptr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
426 long len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
427 char_u **newptr) |
6122 | 428 { |
429 cryptmethod_T *method = &cryptmethods[state->method_nr]; | |
430 | |
431 if (method->decode_buffer_fn != NULL) | |
432 /* Has buffer function, pass through. */ | |
433 return method->decode_buffer_fn(state, ptr, len, newptr); | |
434 | |
435 if (len == 0) | |
436 /* Not buffering, just return EOF. */ | |
437 return len; | |
438 | |
439 *newptr = alloc(len); | |
440 if (*newptr == NULL) | |
441 return -1; | |
442 method->decode_fn(state, ptr, len, *newptr); | |
443 return len; | |
444 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
445 #endif |
6122 | 446 |
447 /* | |
448 * Encrypting "from[len]" into "to[len]". | |
449 */ | |
450 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
451 crypt_encode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
452 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
453 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
454 size_t len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
455 char_u *to) |
6122 | 456 { |
457 cryptmethods[state->method_nr].encode_fn(state, from, len, to); | |
458 } | |
459 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
460 #if 0 // unused |
6122 | 461 /* |
462 * decrypting "from[len]" into "to[len]". | |
463 */ | |
464 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
465 crypt_decode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
466 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
467 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
468 size_t len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
469 char_u *to) |
6122 | 470 { |
471 cryptmethods[state->method_nr].decode_fn(state, from, len, to); | |
472 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
473 #endif |
6122 | 474 |
475 /* | |
476 * Simple inplace encryption, modifies "buf[len]" in place. | |
477 */ | |
478 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
479 crypt_encode_inplace( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
480 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
481 char_u *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
482 size_t len) |
6122 | 483 { |
484 cryptmethods[state->method_nr].encode_inplace_fn(state, buf, len, buf); | |
485 } | |
486 | |
487 /* | |
488 * Simple inplace decryption, modifies "buf[len]" in place. | |
489 */ | |
490 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
491 crypt_decode_inplace( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
492 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
493 char_u *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
494 size_t len) |
6122 | 495 { |
496 cryptmethods[state->method_nr].decode_inplace_fn(state, buf, len, buf); | |
497 } | |
498 | |
499 /* | |
500 * Free an allocated crypt key. Clear the text to make sure it doesn't stay | |
501 * in memory anywhere. | |
502 */ | |
503 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
504 crypt_free_key(char_u *key) |
6122 | 505 { |
506 char_u *p; | |
507 | |
508 if (key != NULL) | |
509 { | |
510 for (p = key; *p != NUL; ++p) | |
511 *p = 0; | |
512 vim_free(key); | |
513 } | |
514 } | |
515 | |
516 /* | |
6353 | 517 * Check the crypt method and give a warning if it's outdated. |
518 */ | |
519 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
520 crypt_check_method(int method) |
6353 | 521 { |
522 if (method < CRYPT_M_BF2) | |
523 { | |
524 msg_scroll = TRUE; | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
525 msg(_("Warning: Using a weak encryption method; see :help 'cm'")); |
6353 | 526 } |
527 } | |
528 | |
529 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
530 crypt_check_current_method(void) |
6353 | 531 { |
532 crypt_check_method(crypt_get_method_nr(curbuf)); | |
533 } | |
534 | |
535 /* | |
6122 | 536 * Ask the user for a crypt key. |
537 * When "store" is TRUE, the new key is stored in the 'key' option, and the | |
538 * 'key' option value is returned: Don't free it. | |
539 * When "store" is FALSE, the typed key is returned in allocated memory. | |
540 * Returns NULL on failure. | |
541 */ | |
542 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
543 crypt_get_key( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
544 int store, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
545 int twice) /* Ask for the key twice. */ |
6122 | 546 { |
547 char_u *p1, *p2 = NULL; | |
548 int round; | |
549 | |
550 for (round = 0; ; ++round) | |
551 { | |
552 cmdline_star = TRUE; | |
553 cmdline_row = msg_row; | |
554 p1 = getcmdline_prompt(NUL, round == 0 | |
555 ? (char_u *)_("Enter encryption key: ") | |
556 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING, | |
557 NULL); | |
558 cmdline_star = FALSE; | |
559 | |
560 if (p1 == NULL) | |
561 break; | |
562 | |
563 if (round == twice) | |
564 { | |
565 if (p2 != NULL && STRCMP(p1, p2) != 0) | |
566 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
567 msg(_("Keys don't match!")); |
6122 | 568 crypt_free_key(p1); |
569 crypt_free_key(p2); | |
570 p2 = NULL; | |
571 round = -1; /* do it again */ | |
572 continue; | |
573 } | |
574 | |
575 if (store) | |
576 { | |
577 set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL); | |
578 crypt_free_key(p1); | |
579 p1 = curbuf->b_p_key; | |
580 } | |
581 break; | |
582 } | |
583 p2 = p1; | |
584 } | |
585 | |
586 /* since the user typed this, no need to wait for return */ | |
587 if (msg_didout) | |
588 msg_putchar('\n'); | |
589 need_wait_return = FALSE; | |
590 msg_didout = FALSE; | |
591 | |
592 crypt_free_key(p2); | |
593 return p1; | |
594 } | |
595 | |
596 | |
597 /* | |
598 * Append a message to IObuff for the encryption/decryption method being used. | |
599 */ | |
600 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
601 crypt_append_msg( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
602 buf_T *buf) |
6122 | 603 { |
604 if (crypt_get_method_nr(buf) == 0) | |
605 STRCAT(IObuff, _("[crypted]")); | |
606 else | |
607 { | |
608 STRCAT(IObuff, "["); | |
609 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); | |
610 STRCAT(IObuff, "]"); | |
611 } | |
612 } | |
613 | |
614 #endif /* FEAT_CRYPT */ |