Mercurial > vim
annotate src/crypt.c @ 25479:fbdfa533001c v8.2.3276
patch 8.2.3276: Vim9: exists() can only be evaluated at runtime
Commit: https://github.com/vim/vim/commit/bb7ee7abe1ea1e60b8a5dadd2bed8cdbe4f3e8fb
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Aug 2 20:06:50 2021 +0200
patch 8.2.3276: Vim9: exists() can only be evaluated at runtime
Problem: Vim9: exists() can only be evaluated at runtime.
Solution: Evaluate at compile time for option name literals. (closes https://github.com/vim/vim/issues/8437)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 02 Aug 2021 20:15:03 +0200 |
parents | 1919361a53da |
children | 85866e069c24 |
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 { | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
33 char *name; // encryption name as used in 'cryptmethod' |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
34 char *magic; // magic bytes stored in file header |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
35 int salt_len; // length of salt, or 0 when not using salt |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
36 int seed_len; // length of seed, or 0 when not using seed |
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 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
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 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
40 int whole_undofile; // whole undo file is encrypted |
6122 | 41 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
42 // Optional function pointer for a self-test. |
6122 | 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 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
49 // Function pointers for encoding/decoding from one buffer into another. |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
50 // Optional, however, these or the _buffer ones should be configured. |
6122 | 51 void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
52 char_u *to, int last); |
6122 | 53 void (*decode_fn)(cryptstate_T *state, char_u *from, size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
54 char_u *to, int last); |
6122 | 55 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
56 // Function pointers for encoding and decoding, can buffer data if needed. |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
57 // Optional (however, these or the above should be configured). |
6122 | 58 long (*encode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
59 char_u **newptr, int last); |
6122 | 60 long (*decode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
61 char_u **newptr, int last); |
6122 | 62 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
63 // Function pointers for in-place encoding and decoding, used for |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
64 // crypt_*_inplace(). "from" and "to" arguments will be equal. |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
65 // These may be the same as decode_fn and encode_fn above, however an |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
66 // algorithm may implement them in a way that is not interchangeable with |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
67 // the crypt_(en|de)code() interface (for example because it wishes to add |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
68 // padding to files). |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
69 // This method is used for swap and undo files which have a rigid format. |
6122 | 70 void (*encode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
71 char_u *p2, int last); |
6122 | 72 void (*decode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
73 char_u *p2, int last); |
6122 | 74 } cryptmethod_T; |
75 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
76 // index is method_nr of cryptstate_T, CRYPT_M_* |
6122 | 77 static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = { |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
78 // PK_Zip; very weak |
6122 | 79 { |
80 "zip", | |
81 "VimCrypt~01!", | |
82 0, | |
83 0, | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
84 #ifdef CRYPT_NOT_INPLACE |
6122 | 85 TRUE, |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
86 #endif |
6122 | 87 FALSE, |
88 NULL, | |
89 crypt_zip_init, | |
90 crypt_zip_encode, crypt_zip_decode, | |
91 NULL, NULL, | |
92 crypt_zip_encode, crypt_zip_decode, | |
93 }, | |
94 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
95 // Blowfish/CFB + SHA-256 custom key derivation; implementation issues. |
6122 | 96 { |
97 "blowfish", | |
98 "VimCrypt~02!", | |
99 8, | |
100 8, | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
101 #ifdef CRYPT_NOT_INPLACE |
6122 | 102 TRUE, |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
103 #endif |
6122 | 104 FALSE, |
105 blowfish_self_test, | |
106 crypt_blowfish_init, | |
107 crypt_blowfish_encode, crypt_blowfish_decode, | |
108 NULL, NULL, | |
109 crypt_blowfish_encode, crypt_blowfish_decode, | |
110 }, | |
111 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
112 // Blowfish/CFB + SHA-256 custom key derivation; fixed. |
6122 | 113 { |
114 "blowfish2", | |
115 "VimCrypt~03!", | |
116 8, | |
117 8, | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
118 #ifdef CRYPT_NOT_INPLACE |
6122 | 119 TRUE, |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
120 #endif |
6122 | 121 TRUE, |
122 blowfish_self_test, | |
123 crypt_blowfish_init, | |
124 crypt_blowfish_encode, crypt_blowfish_decode, | |
125 NULL, NULL, | |
126 crypt_blowfish_encode, crypt_blowfish_decode, | |
127 }, | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
128 |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
129 // XChaCha20 using libsodium |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
130 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
131 "xchacha20", |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
132 "VimCrypt~04!", |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
133 #ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
134 crypto_pwhash_argon2id_SALTBYTES, // 16 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
135 #else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
136 16, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
137 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
138 8, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
139 #ifdef CRYPT_NOT_INPLACE |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
140 FALSE, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
141 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
142 FALSE, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
143 NULL, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
144 crypt_sodium_init, |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
145 NULL, NULL, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
146 crypt_sodium_buffer_encode, crypt_sodium_buffer_decode, |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
147 NULL, NULL, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
148 }, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
149 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
150 // NOTE: when adding a new method, use some random bytes for the magic key, |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
151 // to avoid that a text file is recognized as encrypted. |
6122 | 152 }; |
153 | |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
154 #ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
155 typedef struct { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
156 size_t count; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
157 unsigned char key[crypto_box_SEEDBYTES]; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
158 // 32, same as crypto_secretstream_xchacha20poly1305_KEYBYTES |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
159 crypto_secretstream_xchacha20poly1305_state |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
160 state; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
161 } sodium_state_T; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
162 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
163 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
164 #define CRYPT_MAGIC_LEN 12 // cannot change |
6122 | 165 static char crypt_magic_head[] = "VimCrypt~"; |
166 | |
167 /* | |
168 * Return int value for crypt method name. | |
169 * 0 for "zip", the old method. Also for any non-valid value. | |
170 * 1 for "blowfish". | |
171 * 2 for "blowfish2". | |
172 */ | |
173 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
174 crypt_method_nr_from_name(char_u *name) |
6122 | 175 { |
176 int i; | |
177 | |
178 for (i = 0; i < CRYPT_M_COUNT; ++i) | |
179 if (STRCMP(name, cryptmethods[i].name) == 0) | |
180 return i; | |
181 return 0; | |
182 } | |
183 | |
184 /* | |
185 * Get the crypt method used for a file from "ptr[len]", the magic text at the | |
186 * start of the file. | |
187 * Returns -1 when no encryption used. | |
188 */ | |
189 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
190 crypt_method_nr_from_magic(char *ptr, int len) |
6122 | 191 { |
192 int i; | |
193 | |
194 if (len < CRYPT_MAGIC_LEN) | |
195 return -1; | |
196 | |
197 for (i = 0; i < CRYPT_M_COUNT; i++) | |
198 if (memcmp(ptr, cryptmethods[i].magic, CRYPT_MAGIC_LEN) == 0) | |
199 return i; | |
200 | |
201 i = (int)STRLEN(crypt_magic_head); | |
202 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
|
203 emsg(_("E821: File is encrypted with unknown method")); |
6122 | 204 |
205 return -1; | |
206 } | |
207 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
208 #ifdef CRYPT_NOT_INPLACE |
6122 | 209 /* |
210 * Return TRUE if the crypt method for "method_nr" can be done in-place. | |
211 */ | |
212 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
213 crypt_works_inplace(cryptstate_T *state) |
6122 | 214 { |
215 return cryptmethods[state->method_nr].works_inplace; | |
216 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
217 #endif |
6122 | 218 |
219 /* | |
220 * Get the crypt method for buffer "buf" as a number. | |
221 */ | |
222 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
223 crypt_get_method_nr(buf_T *buf) |
6122 | 224 { |
225 return crypt_method_nr_from_name(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); | |
226 } | |
227 | |
228 /* | |
229 * Return TRUE when the buffer uses an encryption method that encrypts the | |
230 * whole undo file, not only the text. | |
231 */ | |
232 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
233 crypt_whole_undofile(int method_nr) |
6122 | 234 { |
235 return cryptmethods[method_nr].whole_undofile; | |
236 } | |
237 | |
238 /* | |
18498
9e6d5a4abb1c
patch 8.1.2243: typos in comments
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
239 * Get crypt method specific length of the file header in bytes. |
6122 | 240 */ |
241 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
242 crypt_get_header_len(int method_nr) |
6122 | 243 { |
244 return CRYPT_MAGIC_LEN | |
245 + cryptmethods[method_nr].salt_len | |
246 + cryptmethods[method_nr].seed_len; | |
247 } | |
248 | |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
249 |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
250 /* |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
251 * Get maximum crypt method specific length of the file header in bytes. |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
252 */ |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
253 int |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
254 crypt_get_max_header_len() |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
255 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
256 int i; |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
257 int max = 0; |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
258 int temp = 0; |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
259 |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
260 for (i = 0; i < CRYPT_M_COUNT; ++i) |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
261 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
262 temp = crypt_get_header_len(i); |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
263 if (temp > max) |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
264 max = temp; |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
265 } |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
266 return max; |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
267 } |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
268 |
6122 | 269 /* |
270 * Set the crypt method for buffer "buf" to "method_nr" using the int value as | |
271 * returned by crypt_method_nr_from_name(). | |
272 */ | |
273 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
274 crypt_set_cm_option(buf_T *buf, int method_nr) |
6122 | 275 { |
276 free_string_option(buf->b_p_cm); | |
277 buf->b_p_cm = vim_strsave((char_u *)cryptmethods[method_nr].name); | |
278 } | |
279 | |
280 /* | |
281 * If the crypt method for the current buffer has a self-test, run it and | |
282 * return OK/FAIL. | |
283 */ | |
284 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
285 crypt_self_test(void) |
6122 | 286 { |
287 int method_nr = crypt_get_method_nr(curbuf); | |
288 | |
289 if (cryptmethods[method_nr].self_test_fn == NULL) | |
290 return OK; | |
291 return cryptmethods[method_nr].self_test_fn(); | |
292 } | |
293 | |
294 /* | |
295 * 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
|
296 * Return NULL for failure. |
6122 | 297 */ |
298 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
299 crypt_create( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
300 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
301 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
302 char_u *salt, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
303 int salt_len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
304 char_u *seed, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
305 int seed_len) |
6122 | 306 { |
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
|
307 cryptstate_T *state = ALLOC_ONE(cryptstate_T); |
6122 | 308 |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
309 if (state == NULL) |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
310 return state; |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
311 |
6122 | 312 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
|
313 if (cryptmethods[method_nr].init_fn( |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
314 state, key, salt, salt_len, seed, seed_len) == FAIL) |
16429
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
315 { |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
316 vim_free(state); |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
317 return NULL; |
a1229400434a
patch 8.1.1219: not checking for NULL return from alloc()
Bram Moolenaar <Bram@vim.org>
parents:
16378
diff
changeset
|
318 } |
6122 | 319 return state; |
320 } | |
321 | |
322 /* | |
323 * Allocate a crypt state from a file header and initialize it. | |
324 * Assumes that header contains at least the number of bytes that | |
325 * crypt_get_header_len() returns for "method_nr". | |
326 */ | |
327 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
328 crypt_create_from_header( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
329 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
330 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
331 char_u *header) |
6122 | 332 { |
333 char_u *salt = NULL; | |
334 char_u *seed = NULL; | |
335 int salt_len = cryptmethods[method_nr].salt_len; | |
336 int seed_len = cryptmethods[method_nr].seed_len; | |
337 | |
338 if (salt_len > 0) | |
339 salt = header + CRYPT_MAGIC_LEN; | |
340 if (seed_len > 0) | |
341 seed = header + CRYPT_MAGIC_LEN + salt_len; | |
342 | |
343 return crypt_create(method_nr, key, salt, salt_len, seed, seed_len); | |
344 } | |
345 | |
346 /* | |
347 * Read the crypt method specific header data from "fp". | |
348 * Return an allocated cryptstate_T or NULL on error. | |
349 */ | |
350 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
351 crypt_create_from_file(FILE *fp, char_u *key) |
6122 | 352 { |
353 int method_nr; | |
354 int header_len; | |
355 char magic_buffer[CRYPT_MAGIC_LEN]; | |
356 char_u *buffer; | |
357 cryptstate_T *state; | |
358 | |
359 if (fread(magic_buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) | |
360 return NULL; | |
361 method_nr = crypt_method_nr_from_magic(magic_buffer, CRYPT_MAGIC_LEN); | |
362 if (method_nr < 0) | |
363 return NULL; | |
364 | |
365 header_len = crypt_get_header_len(method_nr); | |
366 if ((buffer = alloc(header_len)) == NULL) | |
367 return NULL; | |
368 mch_memmove(buffer, magic_buffer, CRYPT_MAGIC_LEN); | |
369 if (header_len > CRYPT_MAGIC_LEN | |
370 && fread(buffer + CRYPT_MAGIC_LEN, | |
371 header_len - CRYPT_MAGIC_LEN, 1, fp) != 1) | |
372 { | |
373 vim_free(buffer); | |
374 return NULL; | |
375 } | |
376 | |
377 state = crypt_create_from_header(method_nr, key, buffer); | |
378 vim_free(buffer); | |
379 return state; | |
380 } | |
381 | |
382 /* | |
383 * Allocate a cryptstate_T for writing and initialize it with "key". | |
384 * Allocates and fills in the header and stores it in "header", setting | |
385 * "header_len". The header may include salt and seed, depending on | |
386 * cryptmethod. Caller must free header. | |
387 * Returns the state or NULL on failure. | |
388 */ | |
389 cryptstate_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
390 crypt_create_for_writing( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
391 int method_nr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
392 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
393 char_u **header, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
394 int *header_len) |
6122 | 395 { |
396 int len = crypt_get_header_len(method_nr); | |
397 char_u *salt = NULL; | |
398 char_u *seed = NULL; | |
399 int salt_len = cryptmethods[method_nr].salt_len; | |
400 int seed_len = cryptmethods[method_nr].seed_len; | |
401 cryptstate_T *state; | |
402 | |
403 *header_len = len; | |
404 *header = alloc(len); | |
405 if (*header == NULL) | |
406 return NULL; | |
407 | |
408 mch_memmove(*header, cryptmethods[method_nr].magic, CRYPT_MAGIC_LEN); | |
409 if (salt_len > 0 || seed_len > 0) | |
410 { | |
411 if (salt_len > 0) | |
412 salt = *header + CRYPT_MAGIC_LEN; | |
413 if (seed_len > 0) | |
414 seed = *header + CRYPT_MAGIC_LEN + salt_len; | |
415 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
416 // TODO: Should this be crypt method specific? (Probably not worth |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
417 // it). sha2_seed is pretty bad for large amounts of entropy, so make |
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
418 // that into something which is suitable for anything. |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
419 #ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
420 if (sodium_init() >= 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
421 { |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
422 if (salt_len > 0) |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
423 randombytes_buf(salt, salt_len); |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
424 if (seed_len > 0) |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
425 randombytes_buf(seed, seed_len); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
426 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
427 else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
428 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
429 sha2_seed(salt, salt_len, seed, seed_len); |
6122 | 430 } |
431 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len); | |
432 if (state == NULL) | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
433 VIM_CLEAR(*header); |
6122 | 434 return state; |
435 } | |
436 | |
437 /* | |
438 * Free the crypt state. | |
439 */ | |
440 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
441 crypt_free_state(cryptstate_T *state) |
6122 | 442 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
443 #ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
444 if (state->method_nr == CRYPT_M_SOD) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
445 { |
25417
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
446 sodium_munlock(((sodium_state_T *)state->method_state)->key, |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
447 crypto_box_SEEDBYTES); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
448 sodium_memzero(state->method_state, sizeof(sodium_state_T)); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
449 sodium_free(state->method_state); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
450 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
451 else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
452 #endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
453 vim_free(state->method_state); |
6122 | 454 vim_free(state); |
455 } | |
456 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
457 #ifdef CRYPT_NOT_INPLACE |
6122 | 458 /* |
459 * Encode "from[len]" and store the result in a newly allocated buffer, which | |
460 * is stored in "newptr". | |
461 * Return number of bytes in "newptr", 0 for need more or -1 on error. | |
462 */ | |
463 long | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
464 crypt_encode_alloc( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
465 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
466 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
467 size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
468 char_u **newptr, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
469 int last) |
6122 | 470 { |
471 cryptmethod_T *method = &cryptmethods[state->method_nr]; | |
472 | |
473 if (method->encode_buffer_fn != NULL) | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
474 // Has buffer function, pass through. |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
475 return method->encode_buffer_fn(state, from, len, newptr, last); |
6122 | 476 if (len == 0) |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
477 // Not buffering, just return EOF. |
6132 | 478 return (long)len; |
6122 | 479 |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
480 *newptr = alloc(len + 50); |
6122 | 481 if (*newptr == NULL) |
482 return -1; | |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
483 method->encode_fn(state, from, len, *newptr, last); |
6132 | 484 return (long)len; |
6122 | 485 } |
486 | |
487 /* | |
488 * Decrypt "ptr[len]" and store the result in a newly allocated buffer, which | |
489 * is stored in "newptr". | |
490 * Return number of bytes in "newptr", 0 for need more or -1 on error. | |
491 */ | |
492 long | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
493 crypt_decode_alloc( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
494 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
495 char_u *ptr, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
496 long len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
497 char_u **newptr, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
498 int last) |
6122 | 499 { |
500 cryptmethod_T *method = &cryptmethods[state->method_nr]; | |
501 | |
502 if (method->decode_buffer_fn != NULL) | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
503 // Has buffer function, pass through. |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
504 return method->decode_buffer_fn(state, ptr, len, newptr, last); |
6122 | 505 |
506 if (len == 0) | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
507 // Not buffering, just return EOF. |
6122 | 508 return len; |
509 | |
510 *newptr = alloc(len); | |
511 if (*newptr == NULL) | |
512 return -1; | |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
513 method->decode_fn(state, ptr, len, *newptr, last); |
6122 | 514 return len; |
515 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
516 #endif |
6122 | 517 |
518 /* | |
519 * Encrypting "from[len]" into "to[len]". | |
520 */ | |
521 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
522 crypt_encode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
523 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
524 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
525 size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
526 char_u *to, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
527 int last) |
6122 | 528 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
529 cryptmethods[state->method_nr].encode_fn(state, from, len, to, last); |
6122 | 530 } |
531 | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
532 #if 0 // unused |
6122 | 533 /* |
534 * decrypting "from[len]" into "to[len]". | |
535 */ | |
536 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
537 crypt_decode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
538 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
539 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
540 size_t len, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
541 char_u *to, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
542 int last) |
6122 | 543 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
544 cryptmethods[state->method_nr].decode_fn(state, from, len, to, last); |
6122 | 545 } |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
546 #endif |
6122 | 547 |
548 /* | |
549 * Simple inplace encryption, modifies "buf[len]" in place. | |
550 */ | |
551 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
552 crypt_encode_inplace( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
553 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
554 char_u *buf, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
555 size_t len, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
556 int last) |
6122 | 557 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
558 cryptmethods[state->method_nr].encode_inplace_fn(state, buf, len, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
559 buf, last); |
6122 | 560 } |
561 | |
562 /* | |
563 * Simple inplace decryption, modifies "buf[len]" in place. | |
564 */ | |
565 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
566 crypt_decode_inplace( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
567 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
568 char_u *buf, |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
569 size_t len, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
570 int last) |
6122 | 571 { |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
572 cryptmethods[state->method_nr].decode_inplace_fn(state, buf, len, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
573 buf, last); |
6122 | 574 } |
575 | |
576 /* | |
577 * Free an allocated crypt key. Clear the text to make sure it doesn't stay | |
578 * in memory anywhere. | |
579 */ | |
580 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
581 crypt_free_key(char_u *key) |
6122 | 582 { |
583 char_u *p; | |
584 | |
585 if (key != NULL) | |
586 { | |
587 for (p = key; *p != NUL; ++p) | |
588 *p = 0; | |
589 vim_free(key); | |
590 } | |
591 } | |
592 | |
593 /* | |
6353 | 594 * Check the crypt method and give a warning if it's outdated. |
595 */ | |
596 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
597 crypt_check_method(int method) |
6353 | 598 { |
599 if (method < CRYPT_M_BF2) | |
600 { | |
601 msg_scroll = TRUE; | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
602 msg(_("Warning: Using a weak encryption method; see :help 'cm'")); |
6353 | 603 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
604 } |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
605 |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
606 #ifdef FEAT_SODIUM |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
607 static void |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
608 crypt_check_swapfile_curbuf(void) |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
609 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
610 int method = crypt_get_method_nr(curbuf); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
611 if (method == CRYPT_M_SOD) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
612 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
613 // encryption uses padding and MAC, that does not work very well with |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
614 // swap and undo files, so disable them |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
615 mf_close_file(curbuf, TRUE); // remove the swap file |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
616 set_option_value((char_u *)"swf", 0, NULL, OPT_LOCAL); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
617 msg_scroll = TRUE; |
25362
68a7e6d70a5e
patch 8.2.3218: when using xchaha20 crypt undo file is not removed
Bram Moolenaar <Bram@vim.org>
parents:
24990
diff
changeset
|
618 msg(_("Note: Encryption of swapfile not supported, disabling swap file")); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
619 } |
6353 | 620 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
621 #endif |
6353 | 622 |
623 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
624 crypt_check_current_method(void) |
6353 | 625 { |
626 crypt_check_method(crypt_get_method_nr(curbuf)); | |
627 } | |
628 | |
629 /* | |
6122 | 630 * Ask the user for a crypt key. |
631 * When "store" is TRUE, the new key is stored in the 'key' option, and the | |
632 * 'key' option value is returned: Don't free it. | |
633 * When "store" is FALSE, the typed key is returned in allocated memory. | |
634 * Returns NULL on failure. | |
635 */ | |
636 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
637 crypt_get_key( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
638 int store, |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
639 int twice) // Ask for the key twice. |
6122 | 640 { |
641 char_u *p1, *p2 = NULL; | |
642 int round; | |
643 | |
644 for (round = 0; ; ++round) | |
645 { | |
646 cmdline_star = TRUE; | |
647 cmdline_row = msg_row; | |
648 p1 = getcmdline_prompt(NUL, round == 0 | |
649 ? (char_u *)_("Enter encryption key: ") | |
650 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING, | |
651 NULL); | |
652 cmdline_star = FALSE; | |
653 | |
654 if (p1 == NULL) | |
655 break; | |
656 | |
657 if (round == twice) | |
658 { | |
659 if (p2 != NULL && STRCMP(p1, p2) != 0) | |
660 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
661 msg(_("Keys don't match!")); |
6122 | 662 crypt_free_key(p1); |
663 crypt_free_key(p2); | |
664 p2 = NULL; | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
665 round = -1; // do it again |
6122 | 666 continue; |
667 } | |
668 | |
669 if (store) | |
670 { | |
671 set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL); | |
672 crypt_free_key(p1); | |
673 p1 = curbuf->b_p_key; | |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
674 #ifdef FEAT_SODIUM |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
675 crypt_check_swapfile_curbuf(); |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
676 #endif |
6122 | 677 } |
678 break; | |
679 } | |
680 p2 = p1; | |
681 } | |
682 | |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
683 // since the user typed this, no need to wait for return |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
684 if (crypt_get_method_nr(curbuf) != CRYPT_M_SOD) |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
685 { |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
686 if (msg_didout) |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
687 msg_putchar('\n'); |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
688 need_wait_return = FALSE; |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
689 msg_didout = FALSE; |
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
690 } |
6122 | 691 |
692 crypt_free_key(p2); | |
693 return p1; | |
694 } | |
695 | |
696 | |
697 /* | |
698 * Append a message to IObuff for the encryption/decryption method being used. | |
699 */ | |
700 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
701 crypt_append_msg( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
6353
diff
changeset
|
702 buf_T *buf) |
6122 | 703 { |
704 if (crypt_get_method_nr(buf) == 0) | |
705 STRCAT(IObuff, _("[crypted]")); | |
706 else | |
707 { | |
708 STRCAT(IObuff, "["); | |
709 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); | |
710 STRCAT(IObuff, "]"); | |
711 } | |
712 } | |
713 | |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
714 int |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
715 crypt_sodium_init( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
716 cryptstate_T *state UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
717 char_u *key UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
718 char_u *salt UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
719 int salt_len UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
720 char_u *seed UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
721 int seed_len UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
722 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
723 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
724 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
725 unsigned char dkey[crypto_box_SEEDBYTES]; // 32 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
726 sodium_state_T *sd_state; |
25417
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
727 int retval = 0; |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
728 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
729 if (sodium_init() < 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
730 return FAIL; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
731 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
732 sd_state = (sodium_state_T *)sodium_malloc(sizeof(sodium_state_T)); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
733 sodium_memzero(sd_state, sizeof(sodium_state_T)); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
734 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
735 // derive a key from the password |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
736 if (crypto_pwhash(dkey, sizeof(dkey), (const char *)key, STRLEN(key), salt, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
737 crypto_pwhash_OPSLIMIT_INTERACTIVE, crypto_pwhash_MEMLIMIT_INTERACTIVE, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
738 crypto_pwhash_ALG_DEFAULT) != 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
739 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
740 // out of memory |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
741 sodium_free(sd_state); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
742 return FAIL; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
743 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
744 memcpy(sd_state->key, dkey, crypto_box_SEEDBYTES); |
25417
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
745 |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
746 retval += sodium_mlock(sd_state->key, crypto_box_SEEDBYTES); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
747 retval += sodium_mlock(key, STRLEN(key)); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
748 |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
749 if (retval < 0) |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
750 { |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
751 emsg(_(e_encryption_sodium_mlock_failed)); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
752 sodium_free(sd_state); |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
753 return FAIL; |
1919361a53da
patch 8.2.3245: the crypt key may appear in a swap partition
Bram Moolenaar <Bram@vim.org>
parents:
25362
diff
changeset
|
754 } |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
755 sd_state->count = 0; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
756 state->method_state = sd_state; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
757 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
758 return OK; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
759 # else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
760 emsg(e_libsodium_not_built_in); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
761 return FAIL; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
762 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
763 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
764 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
765 /* |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
766 * Encrypt "from[len]" into "to[len]". |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
767 * "from" and "to" can be equal to encrypt in place. |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
768 * Call needs to ensure that there is enough space in to (for the header) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
769 */ |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
770 #if 0 // Currently unused |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
771 void |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
772 crypt_sodium_encode( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
773 cryptstate_T *state UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
774 char_u *from UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
775 size_t len UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
776 char_u *to UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
777 int last UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
778 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
779 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
780 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
781 sodium_state_T *sod_st = state->method_state; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
782 unsigned char tag = last |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
783 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
784 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
785 if (sod_st->count == 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
786 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
787 if (len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
788 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
789 emsg(e_libsodium_cannot_encrypt_header); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
790 return; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
791 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
792 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
793 to, sod_st->key); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
794 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
795 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
796 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
797 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
798 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
799 emsg(e_libsodium_cannot_encrypt_buffer); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
800 return; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
801 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
802 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
803 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, to, NULL, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
804 from, len, NULL, 0, tag); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
805 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
806 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
807 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
808 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
809 #endif |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
810 |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
811 /* |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
812 * Decrypt "from[len]" into "to[len]". |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
813 * "from" and "to" can be equal to encrypt in place. |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
814 */ |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
815 #if 0 // Currently unused |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
816 void |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
817 crypt_sodium_decode( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
818 cryptstate_T *state UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
819 char_u *from UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
820 size_t len UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
821 char_u *to UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
822 int last UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
823 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
824 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
825 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
826 sodium_state_T *sod_st = state->method_state; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
827 unsigned char tag; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
828 unsigned long long buf_len; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
829 char_u *p1 = from; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
830 char_u *p2 = to; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
831 char_u *buf_out; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
832 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
833 if (sod_st->count == 0 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
834 && len <= crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
835 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
836 emsg(e_libsodium_cannot_decrypt_header); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
837 return; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
838 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
839 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
840 buf_out = (char_u *)alloc(len); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
841 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
842 if (buf_out == NULL) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
843 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
844 emsg(e_libsodium_cannot_allocate_buffer); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
845 return; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
846 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
847 if (sod_st->count == 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
848 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
849 if (crypto_secretstream_xchacha20poly1305_init_pull( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
850 &sod_st->state, from, sod_st->key) != 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
851 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
852 emsg(e_libsodium_decryption_failed_header_incomplete); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
853 goto fail; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
854 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
855 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
856 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
857 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
858 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
859 if (p1 == p2) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
860 to += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
861 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
862 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
863 if (sod_st->count && len <= crypto_secretstream_xchacha20poly1305_ABYTES) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
864 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
865 emsg(e_libsodium_cannot_decrypt_buffer); |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
866 goto fail; |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
867 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
868 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
869 buf_out, &buf_len, &tag, from, len, NULL, 0) != 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
870 { |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
871 emsg(e_libsodium_decryption_failed); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
872 goto fail; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
873 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
874 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
875 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
876 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
877 { |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
878 emsg(e_libsodium_decryption_failed_premature); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
879 goto fail; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
880 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
881 if (p1 == p2) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
882 mch_memmove(p2, buf_out, buf_len); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
883 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
884 fail: |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
885 vim_free(buf_out); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
886 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
887 } |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
888 #endif |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
889 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
890 /* |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
891 * Encrypt "from[len]" into "to[len]". |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
892 * "from" and "to" can be equal to encrypt in place. |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
893 */ |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
894 long |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
895 crypt_sodium_buffer_encode( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
896 cryptstate_T *state UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
897 char_u *from UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
898 size_t len UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
899 char_u **buf_out UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
900 int last UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
901 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
902 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
903 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
904 unsigned long long out_len; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
905 char_u *ptr; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
906 unsigned char tag = last |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
907 ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
908 int length; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
909 sodium_state_T *sod_st = state->method_state; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
910 int first = (sod_st->count == 0); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
911 |
24990
85d1e82ed134
patch 8.2.3032: build problems with MSVC, other crypt issues with libsodium
Bram Moolenaar <Bram@vim.org>
parents:
24986
diff
changeset
|
912 length = (int)len + crypto_secretstream_xchacha20poly1305_ABYTES |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
913 + (first ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
914 *buf_out = alloc_clear(length); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
915 if (*buf_out == NULL) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
916 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
917 emsg(e_libsodium_cannot_allocate_buffer); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
918 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
919 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
920 ptr = *buf_out; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
921 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
922 if (first) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
923 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
924 crypto_secretstream_xchacha20poly1305_init_push(&sod_st->state, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
925 ptr, sod_st->key); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
926 ptr += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
927 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
928 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
929 crypto_secretstream_xchacha20poly1305_push(&sod_st->state, ptr, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
930 &out_len, from, len, NULL, 0, tag); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
931 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
932 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
933 return out_len + (first |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
934 ? crypto_secretstream_xchacha20poly1305_HEADERBYTES : 0); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
935 # else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
936 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
937 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
938 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
939 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
940 /* |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
941 * Decrypt "from[len]" into "to[len]". |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
942 * "from" and "to" can be equal to encrypt in place. |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
943 */ |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
944 long |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
945 crypt_sodium_buffer_decode( |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
946 cryptstate_T *state UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
947 char_u *from UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
948 size_t len UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
949 char_u **buf_out UNUSED, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
950 int last UNUSED) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
951 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
952 # ifdef FEAT_SODIUM |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
953 // crypto_box_SEEDBYTES == crypto_secretstream_xchacha20poly1305_KEYBYTES |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
954 sodium_state_T *sod_st = state->method_state; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
955 unsigned char tag; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
956 unsigned long long out_len; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
957 *buf_out = alloc_clear(len); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
958 if (*buf_out == NULL) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
959 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
960 emsg(e_libsodium_cannot_allocate_buffer); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
961 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
962 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
963 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
964 if (sod_st->count == 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
965 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
966 if (crypto_secretstream_xchacha20poly1305_init_pull(&sod_st->state, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
967 from, sod_st->key) != 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
968 { |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
969 emsg(e_libsodium_decryption_failed_header_incomplete); |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
970 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
971 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
972 from += crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
973 len -= crypto_secretstream_xchacha20poly1305_HEADERBYTES; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
974 sod_st->count++; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
975 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
976 if (crypto_secretstream_xchacha20poly1305_pull(&sod_st->state, |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
977 *buf_out, &out_len, &tag, from, len, NULL, 0) != 0) |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
978 { |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
979 emsg(e_libsodium_decryption_failed); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
980 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
981 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
982 |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
983 if (tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !last) |
24986
fa31a0ea09e1
patch 8.2.3030: Coverity reports a memory leak
Bram Moolenaar <Bram@vim.org>
parents:
24970
diff
changeset
|
984 emsg(e_libsodium_decryption_failed_premature); |
24970
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
985 return (long) out_len; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
986 # else |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
987 return -1; |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
988 # endif |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
989 } |
7e9e53a0368f
patch 8.2.3022: available encryption methods are not strong enough
Bram Moolenaar <Bram@vim.org>
parents:
18757
diff
changeset
|
990 |
18757
c469e1930456
patch 8.1.2368: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18498
diff
changeset
|
991 #endif // FEAT_CRYPT |