annotate src/crypt.c @ 32669:448aef880252

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