Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 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 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 28 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 29 | // The state of encryption, referenced by cryptstate_T. |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 30 | typedef struct { |
| 31 | u32_T keys[3]; |
| 32 | } zip_state_T; |
| 33 | |
| 34 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 35 | static u32_T crc_32_table[256]; |
| 36 | |
| 37 | /* |
| 38 | * Fill the CRC table, if not done already. |
| 39 | */ |
| 40 | static void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 41 | make_crc_tab(void) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 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 | */ |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 63 | #define DECRYPT_BYTE_ZIP(keys, t) \ |
| 64 | { \ |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 65 | short_u temp = (short_u)keys[2] | 2; \ |
| 66 | t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \ |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Update the encryption keys with the next byte of plain text. |
| 71 | */ |
Bram Moolenaar | abab0b0 | 2019-03-30 18:47:01 +0100 | [diff] [blame] | 72 | #define UPDATE_KEYS_ZIP(keys, c) do { \ |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 73 | keys[0] = CRC32(keys[0], (c)); \ |
| 74 | keys[1] += keys[0] & 0xff; \ |
| 75 | keys[1] = keys[1] * 134775813L + 1; \ |
| 76 | keys[2] = CRC32(keys[2], (int)(keys[1] >> 24)); \ |
Bram Moolenaar | abab0b0 | 2019-03-30 18:47:01 +0100 | [diff] [blame] | 77 | } while (0) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Initialize for encryption/decryption. |
| 81 | */ |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 82 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 83 | crypt_zip_init( |
| 84 | cryptstate_T *state, |
| 85 | char_u *key, |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 86 | crypt_arg_T *arg UNUSED) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 87 | { |
| 88 | char_u *p; |
| 89 | zip_state_T *zs; |
| 90 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 91 | zs = ALLOC_ONE(zip_state_T); |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 92 | if (zs == NULL) |
| 93 | return FAIL; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 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) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 101 | UPDATE_KEYS_ZIP(zs->keys, (int)*p); |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 102 | |
| 103 | return OK; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Encrypt "from[len]" into "to[len]". |
| 108 | * "from" and "to" can be equal to encrypt in place. |
| 109 | */ |
| 110 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 111 | crypt_zip_encode( |
| 112 | cryptstate_T *state, |
| 113 | char_u *from, |
| 114 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 115 | char_u *to, |
| 116 | int last UNUSED) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 117 | { |
| 118 | zip_state_T *zs = state->method_state; |
| 119 | size_t i; |
| 120 | int ztemp, t; |
| 121 | |
| 122 | for (i = 0; i < len; ++i) |
| 123 | { |
| 124 | ztemp = from[i]; |
| 125 | DECRYPT_BYTE_ZIP(zs->keys, t); |
| 126 | UPDATE_KEYS_ZIP(zs->keys, ztemp); |
| 127 | to[i] = t ^ ztemp; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Decrypt "from[len]" into "to[len]". |
| 133 | */ |
| 134 | void |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 135 | crypt_zip_decode( |
| 136 | cryptstate_T *state, |
| 137 | char_u *from, |
| 138 | size_t len, |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 139 | char_u *to, |
| 140 | int last UNUSED) |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 141 | { |
| 142 | zip_state_T *zs = state->method_state; |
| 143 | size_t i; |
| 144 | short_u temp; |
| 145 | |
| 146 | for (i = 0; i < len; ++i) |
| 147 | { |
| 148 | temp = (short_u)zs->keys[2] | 2; |
| 149 | temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); |
| 150 | UPDATE_KEYS_ZIP(zs->keys, to[i] = from[i] ^ temp); |
| 151 | } |
| 152 | } |
| 153 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 154 | #endif // FEAT_CRYPT |