mirror of
https://github.com/neovim/neovim.git
synced 2025-09-27 13:38:34 +00:00
Cleanup crypt.h/.c to follow the style guide.
This commit is contained in:

committed by
Thiago de Arruda

parent
05d29fb0b8
commit
8d14dd93b8
170
src/crypt.c
170
src/crypt.c
@@ -1,17 +1,16 @@
|
|||||||
/*
|
// Optional encryption support.
|
||||||
* Optional encryption support.
|
// Mohsin Ahmed, mosh@sasi.com, 98-09-24
|
||||||
* Mohsin Ahmed, mosh@sasi.com, 98-09-24
|
// Based on zip/crypt sources.
|
||||||
* Based on zip/crypt sources.
|
//
|
||||||
*
|
// NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to
|
||||||
* NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to
|
// most countries. There are a few exceptions, but that still should not be a
|
||||||
* most countries. There are a few exceptions, but that still should not be a
|
// problem since this code was originally created in Europe and India.
|
||||||
* problem since this code was originally created in Europe and India.
|
//
|
||||||
*
|
// Blowfish addition originally made by Mohsin Ahmed,
|
||||||
* Blowfish addition originally made by Mohsin Ahmed,
|
// http://www.cs.albany.edu/~mosh 2010-03-14
|
||||||
* http://www.cs.albany.edu/~mosh 2010-03-14
|
// Based on blowfish by Bruce Schneier (http://www.schneier.com/blowfish.html)
|
||||||
* Based on blowfish by Bruce Schneier (http://www.schneier.com/blowfish.html)
|
// and sha256 by Christophe Devine.
|
||||||
* and sha256 by Christophe Devine.
|
|
||||||
*/
|
|
||||||
#include "vim.h"
|
#include "vim.h"
|
||||||
#include "misc2.h"
|
#include "misc2.h"
|
||||||
#include "blowfish.h"
|
#include "blowfish.h"
|
||||||
@@ -19,22 +18,17 @@
|
|||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "option.h"
|
#include "option.h"
|
||||||
|
|
||||||
/* from zip.h */
|
|
||||||
|
|
||||||
typedef unsigned short ush; /* unsigned 16-bit value */
|
|
||||||
typedef unsigned long ulg; /* unsigned 32-bit value */
|
|
||||||
|
|
||||||
static void make_crc_tab(void);
|
static void make_crc_tab(void);
|
||||||
|
|
||||||
static ulg crc_32_tab[256];
|
static unsigned long crc_32_tab[256];
|
||||||
|
|
||||||
/*
|
// Fills the CRC table.
|
||||||
* Fill the CRC table.
|
|
||||||
*/
|
|
||||||
static void make_crc_tab(void)
|
static void make_crc_tab(void)
|
||||||
{
|
{
|
||||||
ulg s, t, v;
|
unsigned long s;
|
||||||
static int done = FALSE;
|
unsigned long t;
|
||||||
|
unsigned long v;
|
||||||
|
static bool done = false;
|
||||||
|
|
||||||
if (done) {
|
if (done) {
|
||||||
return;
|
return;
|
||||||
@@ -44,29 +38,25 @@ static void make_crc_tab(void)
|
|||||||
v = t;
|
v = t;
|
||||||
|
|
||||||
for (s = 0; s < 8; s++) {
|
for (s = 0; s < 8; s++) {
|
||||||
v = (v >> 1) ^ ((v & 1) * (ulg)0xedb88320L);
|
v = (v >> 1) ^ ((v & 1) * (unsigned long)0xedb88320L);
|
||||||
}
|
}
|
||||||
crc_32_tab[t] = v;
|
crc_32_tab[t] = v;
|
||||||
}
|
}
|
||||||
done = TRUE;
|
done = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CRC32(c, b) (crc_32_tab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
|
#define CRC32(c, b) (crc_32_tab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
|
||||||
|
|
||||||
static ulg keys[3]; /* keys defining the pseudo-random sequence */
|
static unsigned long keys[3]; // keys defining the pseudo-random sequence
|
||||||
|
|
||||||
/*
|
// Returns the next byte in the pseudo-random sequence.
|
||||||
* Return the next byte in the pseudo-random sequence.
|
|
||||||
*/
|
|
||||||
#define DECRYPT_BYTE_ZIP(t) { \
|
#define DECRYPT_BYTE_ZIP(t) { \
|
||||||
ush temp; \
|
unsigned short temp; \
|
||||||
temp = (ush)keys[2] | 2; \
|
temp = (unsigned short)keys[2] | 2; \
|
||||||
t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \
|
t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Updates the encryption keys with the next byte of plain text.
|
||||||
* Update the encryption keys with the next byte of plain text.
|
|
||||||
*/
|
|
||||||
#define UPDATE_KEYS_ZIP(c) { \
|
#define UPDATE_KEYS_ZIP(c) { \
|
||||||
keys[0] = CRC32(keys[0], (c)); \
|
keys[0] = CRC32(keys[0], (c)); \
|
||||||
keys[1] += keys[0] & 0xff; \
|
keys[1] += keys[0] & 0xff; \
|
||||||
@@ -75,46 +65,38 @@ static ulg keys[3]; /* keys defining the pseudo-random sequence */
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int crypt_busy = 0;
|
static int crypt_busy = 0;
|
||||||
static ulg saved_keys[3];
|
static unsigned long saved_keys[3];
|
||||||
static int saved_crypt_method;
|
static int saved_crypt_method;
|
||||||
|
|
||||||
/*
|
// Returns int value for crypt method string:
|
||||||
* Return int value for crypt method string:
|
// 0 for "zip", the old method. Also for any non-valid value.
|
||||||
* 0 for "zip", the old method. Also for any non-valid value.
|
// 1 for "blowfish".
|
||||||
* 1 for "blowfish".
|
|
||||||
*/
|
|
||||||
int crypt_method_from_string(char_u *s)
|
int crypt_method_from_string(char_u *s)
|
||||||
{
|
{
|
||||||
return *s == 'b' ? 1 : 0;
|
return *s == 'b' ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Returns the crypt method for buffer "buf" as a number.
|
||||||
* Get the crypt method for buffer "buf" as a number.
|
|
||||||
*/
|
|
||||||
int get_crypt_method(buf_T *buf)
|
int get_crypt_method(buf_T *buf)
|
||||||
{
|
{
|
||||||
return crypt_method_from_string(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
|
return crypt_method_from_string(*buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Sets the crypt method for buffer "buf" to "method" using the int value as
|
||||||
* Set the crypt method for buffer "buf" to "method" using the int value as
|
// returned by crypt_method_from_string().
|
||||||
* returned by crypt_method_from_string().
|
|
||||||
*/
|
|
||||||
void set_crypt_method(buf_T *buf, int method)
|
void set_crypt_method(buf_T *buf, int method)
|
||||||
{
|
{
|
||||||
free_string_option(buf->b_p_cm);
|
free_string_option(buf->b_p_cm);
|
||||||
buf->b_p_cm = vim_strsave((char_u *)(method == 0 ? "zip" : "blowfish"));
|
buf->b_p_cm = vim_strsave((char_u *)(method == 0 ? "zip" : "blowfish"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Prepares for initializing the encryption. If already doing encryption then
|
||||||
* Prepare for initializing encryption. If already doing encryption then save
|
// save the state.
|
||||||
* the state.
|
// Must always be called symmetrically with crypt_pop_state().
|
||||||
* Must always be called symmetrically with crypt_pop_state().
|
|
||||||
*/
|
|
||||||
void crypt_push_state(void)
|
void crypt_push_state(void)
|
||||||
{
|
{
|
||||||
if (crypt_busy == 1) {
|
if (crypt_busy == 1) {
|
||||||
/* save the state */
|
// Save the state
|
||||||
if (use_crypt_method == 0) {
|
if (use_crypt_method == 0) {
|
||||||
saved_keys[0] = keys[0];
|
saved_keys[0] = keys[0];
|
||||||
saved_keys[1] = keys[1];
|
saved_keys[1] = keys[1];
|
||||||
@@ -126,17 +108,15 @@ void crypt_push_state(void)
|
|||||||
} else if (crypt_busy > 1) {
|
} else if (crypt_busy > 1) {
|
||||||
EMSG2(_(e_intern2), "crypt_push_state()");
|
EMSG2(_(e_intern2), "crypt_push_state()");
|
||||||
}
|
}
|
||||||
++crypt_busy;
|
crypt_busy++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Ends encryption. If doing encryption before crypt_push_state() then restore
|
||||||
* End encryption. If doing encryption before crypt_push_state() then restore
|
// the saved state.
|
||||||
* the saved state.
|
// Must always be called symmetrically with crypt_push_state().
|
||||||
* Must always be called symmetrically with crypt_push_state().
|
|
||||||
*/
|
|
||||||
void crypt_pop_state(void)
|
void crypt_pop_state(void)
|
||||||
{
|
{
|
||||||
--crypt_busy;
|
crypt_busy--;
|
||||||
|
|
||||||
if (crypt_busy == 1) {
|
if (crypt_busy == 1) {
|
||||||
use_crypt_method = saved_crypt_method;
|
use_crypt_method = saved_crypt_method;
|
||||||
@@ -151,17 +131,16 @@ void crypt_pop_state(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Encrypts "from[len]" into "to[len]".
|
||||||
* Encrypt "from[len]" into "to[len]".
|
// "from" and "to" can be equal to encrypt in place.
|
||||||
* "from" and "to" can be equal to encrypt in place.
|
|
||||||
*/
|
|
||||||
void crypt_encode(char_u *from, size_t len, char_u *to)
|
void crypt_encode(char_u *from, size_t len, char_u *to)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
int ztemp, t;
|
int ztemp;
|
||||||
|
int t;
|
||||||
|
|
||||||
if (use_crypt_method == 0) {
|
if (use_crypt_method == 0) {
|
||||||
for (i = 0; i < len; ++i) {
|
for (i = 0; i < len; i++) {
|
||||||
ztemp = from[i];
|
ztemp = from[i];
|
||||||
DECRYPT_BYTE_ZIP(t);
|
DECRYPT_BYTE_ZIP(t);
|
||||||
UPDATE_KEYS_ZIP(ztemp);
|
UPDATE_KEYS_ZIP(ztemp);
|
||||||
@@ -172,18 +151,16 @@ void crypt_encode(char_u *from, size_t len, char_u *to)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Decrypts "ptr[len]" in place.
|
||||||
* Decrypt "ptr[len]" in place.
|
|
||||||
*/
|
|
||||||
void crypt_decode(char_u *ptr, long len)
|
void crypt_decode(char_u *ptr, long len)
|
||||||
{
|
{
|
||||||
char_u *p;
|
char_u *p;
|
||||||
|
|
||||||
if (use_crypt_method == 0) {
|
if (use_crypt_method == 0) {
|
||||||
for (p = ptr; p < ptr + len; ++p) {
|
for (p = ptr; p < ptr + len; p++) {
|
||||||
ush temp;
|
unsigned short temp;
|
||||||
|
|
||||||
temp = (ush)keys[2] | 2;
|
temp = (unsigned short)keys[2] | 2;
|
||||||
temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff);
|
temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff);
|
||||||
UPDATE_KEYS_ZIP(*p ^= temp);
|
UPDATE_KEYS_ZIP(*p ^= temp);
|
||||||
}
|
}
|
||||||
@@ -192,12 +169,10 @@ void crypt_decode(char_u *ptr, long len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Initializes the encryption keys and the random header according to
|
||||||
* Initialize the encryption keys and the random header according to
|
// the given password.
|
||||||
* the given password.
|
// If "passwd" is NULL or empty, don't do anything.
|
||||||
* If "passwd" is NULL or empty, don't do anything.
|
// in: "passwd" password string with which to modify keys
|
||||||
* in: "passwd" password string with which to modify keys
|
|
||||||
*/
|
|
||||||
void crypt_init_keys(char_u *passwd)
|
void crypt_init_keys(char_u *passwd)
|
||||||
{
|
{
|
||||||
if ((passwd != NULL) && (*passwd != NUL)) {
|
if ((passwd != NULL) && (*passwd != NUL)) {
|
||||||
@@ -209,7 +184,7 @@ void crypt_init_keys(char_u *passwd)
|
|||||||
keys[1] = 591751049L;
|
keys[1] = 591751049L;
|
||||||
keys[2] = 878082192L;
|
keys[2] = 878082192L;
|
||||||
|
|
||||||
for (p = passwd; *p != NUL; ++p) {
|
for (p = passwd; *p != NUL; p++) {
|
||||||
UPDATE_KEYS_ZIP((int)*p);
|
UPDATE_KEYS_ZIP((int)*p);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -218,36 +193,33 @@ void crypt_init_keys(char_u *passwd)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Frees an allocated crypt key. Clears the text to make sure it doesn't stay
|
||||||
* Free an allocated crypt key. Clear the text to make sure it doesn't stay
|
// in memory anywhere.
|
||||||
* in memory anywhere.
|
|
||||||
*/
|
|
||||||
void free_crypt_key(char_u *key)
|
void free_crypt_key(char_u *key)
|
||||||
{
|
{
|
||||||
char_u *p;
|
char_u *p;
|
||||||
|
|
||||||
if (key != NULL) {
|
if (key != NULL) {
|
||||||
for (p = key; *p != NUL; ++p) {
|
for (p = key; *p != NUL; p++) {
|
||||||
*p = 0;
|
*p = 0;
|
||||||
}
|
}
|
||||||
vim_free(key);
|
free(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Asks the user for a crypt key.
|
||||||
* Ask the user for a crypt key.
|
// When "store" is TRUE, the new key is stored in the 'key' option, and the
|
||||||
* When "store" is TRUE, the new key is stored in the 'key' option, and the
|
// 'key' option value is returned: Don't free it.
|
||||||
* 'key' option value is returned: Don't free it.
|
// When "store" is FALSE, the typed key is returned in allocated memory.
|
||||||
* When "store" is FALSE, the typed key is returned in allocated memory.
|
// in: "twice" Ask for the key twice.
|
||||||
* in: "twice" Ask for the key twice.
|
// Returns NULL on failure.
|
||||||
* Returns NULL on failure.
|
|
||||||
*/
|
|
||||||
char_u *get_crypt_key(int store, int twice)
|
char_u *get_crypt_key(int store, int twice)
|
||||||
{
|
{
|
||||||
char_u *p1, *p2 = NULL;
|
char_u *p1;
|
||||||
|
char_u *p2 = NULL;
|
||||||
int round;
|
int round;
|
||||||
|
|
||||||
for (round = 0;; ++round) {
|
for (round = 0;; round++) {
|
||||||
cmdline_star = TRUE;
|
cmdline_star = TRUE;
|
||||||
cmdline_row = msg_row;
|
cmdline_row = msg_row;
|
||||||
char_u *prompt = (round == 0)
|
char_u *prompt = (round == 0)
|
||||||
@@ -265,7 +237,7 @@ char_u *get_crypt_key(int store, int twice)
|
|||||||
free_crypt_key(p1);
|
free_crypt_key(p1);
|
||||||
free_crypt_key(p2);
|
free_crypt_key(p2);
|
||||||
p2 = NULL;
|
p2 = NULL;
|
||||||
round = -1; /* do it again */
|
round = -1; // Do it again
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,7 +251,7 @@ char_u *get_crypt_key(int store, int twice)
|
|||||||
p2 = p1;
|
p2 = p1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* since the user typed this, no need to wait for return */
|
// Since the user typed this, no need to wait for return.
|
||||||
if (msg_didout) {
|
if (msg_didout) {
|
||||||
msg_putchar('\n');
|
msg_putchar('\n');
|
||||||
}
|
}
|
||||||
|
@@ -12,4 +12,4 @@ void crypt_init_keys(char_u *passwd);
|
|||||||
void free_crypt_key(char_u *key);
|
void free_crypt_key(char_u *key);
|
||||||
char_u *get_crypt_key(int store, int twice);
|
char_u *get_crypt_key(int store, int twice);
|
||||||
|
|
||||||
#endif /* NEOVIM_CRYPT_H */
|
#endif // NEOVIM_CRYPT_H
|
||||||
|
Reference in New Issue
Block a user