annotate src/crypt.c @ 15622:f01d2f68a488

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