blob: e50fa6b1c50819ac87d5c287e5f213b9e22aa32e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002 *
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# if VIM_SIZEOF_INT >= 4
28typedef unsigned int u32_T; /* int is at least 32 bits */
29# else
30typedef unsigned long u32_T; /* long should be 32 bits or more */
31# endif
32
33/* The state of encryption, referenced by cryptstate_T. */
34typedef struct {
35 u32_T keys[3];
36} zip_state_T;
37
38
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020039static u32_T crc_32_table[256];
40
41/*
42 * Fill the CRC table, if not done already.
43 */
44 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010045make_crc_tab(void)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020046{
47 u32_T s, t, v;
48 static int done = FALSE;
49
50 if (done)
51 return;
52 for (t = 0; t < 256; t++)
53 {
54 v = t;
55 for (s = 0; s < 8; s++)
56 v = (v >> 1) ^ ((v & 1) * (u32_T)0xedb88320L);
57 crc_32_table[t] = v;
58 }
59 done = TRUE;
60}
61
62#define CRC32(c, b) (crc_32_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
63
64/*
65 * Return the next byte in the pseudo-random sequence.
66 */
67#define DECRYPT_BYTE_ZIP(keys, t) { \
68 short_u temp = (short_u)keys[2] | 2; \
69 t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \
70}
71
72/*
73 * Update the encryption keys with the next byte of plain text.
74 */
75#define UPDATE_KEYS_ZIP(keys, c) { \
76 keys[0] = CRC32(keys[0], (c)); \
77 keys[1] += keys[0] & 0xff; \
78 keys[1] = keys[1] * 134775813L + 1; \
79 keys[2] = CRC32(keys[2], (int)(keys[1] >> 24)); \
80}
81
82/*
83 * Initialize for encryption/decryption.
84 */
85 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010086crypt_zip_init(
87 cryptstate_T *state,
88 char_u *key,
89 char_u *salt UNUSED,
90 int salt_len UNUSED,
91 char_u *seed UNUSED,
92 int seed_len UNUSED)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020093{
94 char_u *p;
95 zip_state_T *zs;
96
97 zs = (zip_state_T *)alloc(sizeof(zip_state_T));
98 state->method_state = zs;
99
100 make_crc_tab();
101 zs->keys[0] = 305419896L;
102 zs->keys[1] = 591751049L;
103 zs->keys[2] = 878082192L;
104 for (p = key; *p != NUL; ++p)
105 {
106 UPDATE_KEYS_ZIP(zs->keys, (int)*p);
107 }
108}
109
110/*
111 * Encrypt "from[len]" into "to[len]".
112 * "from" and "to" can be equal to encrypt in place.
113 */
114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100115crypt_zip_encode(
116 cryptstate_T *state,
117 char_u *from,
118 size_t len,
119 char_u *to)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200120{
121 zip_state_T *zs = state->method_state;
122 size_t i;
123 int ztemp, t;
124
125 for (i = 0; i < len; ++i)
126 {
127 ztemp = from[i];
128 DECRYPT_BYTE_ZIP(zs->keys, t);
129 UPDATE_KEYS_ZIP(zs->keys, ztemp);
130 to[i] = t ^ ztemp;
131 }
132}
133
134/*
135 * Decrypt "from[len]" into "to[len]".
136 */
137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138crypt_zip_decode(
139 cryptstate_T *state,
140 char_u *from,
141 size_t len,
142 char_u *to)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200143{
144 zip_state_T *zs = state->method_state;
145 size_t i;
146 short_u temp;
147
148 for (i = 0; i < len; ++i)
149 {
150 temp = (short_u)zs->keys[2] | 2;
151 temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff);
152 UPDATE_KEYS_ZIP(zs->keys, to[i] = from[i] ^ temp);
153 }
154}
155
156#endif /* FEAT_CRYPT */