Mercurial > vim
annotate src/crypt_zip.c @ 15809:dd5e97262ca3
Added tag v8.1.0911 for changeset 37d31fc37a5acd7b4834913c199ef231021b10f2
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 13 Feb 2019 21:30:06 +0100 |
parents | 2dcaa860e3fc |
children | cd5c83115ec6 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
7817
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
6122 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * crypt_zip.c: Zip encryption support. | |
12 */ | |
13 #include "vim.h" | |
14 | |
15 #if defined(FEAT_CRYPT) || defined(PROTO) | |
16 /* | |
17 * Optional encryption support. | |
18 * Mohsin Ahmed, mosh@sasi.com, 98-09-24 | |
19 * Based on zip/crypt sources. | |
20 * | |
21 * NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to | |
22 * most countries. There are a few exceptions, but that still should not be a | |
23 * problem since this code was originally created in Europe and India. | |
24 */ | |
25 | |
26 /* Need a type that should be 32 bits. 64 also works but wastes space. */ | |
27 typedef unsigned int u32_T; /* int is at least 32 bits */ | |
28 | |
29 /* The state of encryption, referenced by cryptstate_T. */ | |
30 typedef struct { | |
31 u32_T keys[3]; | |
32 } zip_state_T; | |
33 | |
34 | |
35 static u32_T crc_32_table[256]; | |
36 | |
37 /* | |
38 * Fill the CRC table, if not done already. | |
39 */ | |
40 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
41 make_crc_tab(void) |
6122 | 42 { |
43 u32_T s, t, v; | |
44 static int done = FALSE; | |
45 | |
46 if (done) | |
47 return; | |
48 for (t = 0; t < 256; t++) | |
49 { | |
50 v = t; | |
51 for (s = 0; s < 8; s++) | |
52 v = (v >> 1) ^ ((v & 1) * (u32_T)0xedb88320L); | |
53 crc_32_table[t] = v; | |
54 } | |
55 done = TRUE; | |
56 } | |
57 | |
58 #define CRC32(c, b) (crc_32_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8)) | |
59 | |
60 /* | |
61 * Return the next byte in the pseudo-random sequence. | |
62 */ | |
63 #define DECRYPT_BYTE_ZIP(keys, t) { \ | |
64 short_u temp = (short_u)keys[2] | 2; \ | |
65 t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \ | |
66 } | |
67 | |
68 /* | |
69 * Update the encryption keys with the next byte of plain text. | |
70 */ | |
71 #define UPDATE_KEYS_ZIP(keys, c) { \ | |
72 keys[0] = CRC32(keys[0], (c)); \ | |
73 keys[1] += keys[0] & 0xff; \ | |
74 keys[1] = keys[1] * 134775813L + 1; \ | |
75 keys[2] = CRC32(keys[2], (int)(keys[1] >> 24)); \ | |
76 } | |
77 | |
78 /* | |
79 * Initialize for encryption/decryption. | |
80 */ | |
81 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
82 crypt_zip_init( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
83 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
84 char_u *key, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
85 char_u *salt UNUSED, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
86 int salt_len UNUSED, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
87 char_u *seed UNUSED, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
88 int seed_len UNUSED) |
6122 | 89 { |
90 char_u *p; | |
91 zip_state_T *zs; | |
92 | |
93 zs = (zip_state_T *)alloc(sizeof(zip_state_T)); | |
94 state->method_state = zs; | |
95 | |
96 make_crc_tab(); | |
97 zs->keys[0] = 305419896L; | |
98 zs->keys[1] = 591751049L; | |
99 zs->keys[2] = 878082192L; | |
100 for (p = key; *p != NUL; ++p) | |
101 { | |
102 UPDATE_KEYS_ZIP(zs->keys, (int)*p); | |
103 } | |
104 } | |
105 | |
106 /* | |
107 * Encrypt "from[len]" into "to[len]". | |
108 * "from" and "to" can be equal to encrypt in place. | |
109 */ | |
110 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
111 crypt_zip_encode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
112 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
113 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
114 size_t len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
115 char_u *to) |
6122 | 116 { |
117 zip_state_T *zs = state->method_state; | |
118 size_t i; | |
119 int ztemp, t; | |
120 | |
121 for (i = 0; i < len; ++i) | |
122 { | |
123 ztemp = from[i]; | |
124 DECRYPT_BYTE_ZIP(zs->keys, t); | |
125 UPDATE_KEYS_ZIP(zs->keys, ztemp); | |
126 to[i] = t ^ ztemp; | |
127 } | |
128 } | |
129 | |
130 /* | |
131 * Decrypt "from[len]" into "to[len]". | |
132 */ | |
133 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
134 crypt_zip_decode( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
135 cryptstate_T *state, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
136 char_u *from, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
137 size_t len, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
138 char_u *to) |
6122 | 139 { |
140 zip_state_T *zs = state->method_state; | |
141 size_t i; | |
142 short_u temp; | |
143 | |
144 for (i = 0; i < len; ++i) | |
145 { | |
146 temp = (short_u)zs->keys[2] | 2; | |
147 temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); | |
148 UPDATE_KEYS_ZIP(zs->keys, to[i] = from[i] ^ temp); | |
149 } | |
150 } | |
151 | |
152 #endif /* FEAT_CRYPT */ |