comparison src/crypt.c @ 18757:c469e1930456 v8.1.2368

patch 8.1.2368: using old C style comments Commit: https://github.com/vim/vim/commit/c667da5185ce5dce914d2006d62da2be0cedb384 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Nov 30 20:52:27 2019 +0100 patch 8.1.2368: using old C style comments Problem: Using old C style comments. Solution: Use // comments where appropriate.
author Bram Moolenaar <Bram@vim.org>
date Sat, 30 Nov 2019 21:00:04 +0100
parents 9e6d5a4abb1c
children 7e9e53a0368f
comparison
equal deleted inserted replaced
18756:c58a9ff2b806 18757:c469e1930456
28 * Based on blowfish by Bruce Schneier (http://www.schneier.com/blowfish.html) 28 * Based on blowfish by Bruce Schneier (http://www.schneier.com/blowfish.html)
29 * and sha256 by Christophe Devine. 29 * and sha256 by Christophe Devine.
30 */ 30 */
31 31
32 typedef struct { 32 typedef struct {
33 char *name; /* encryption name as used in 'cryptmethod' */ 33 char *name; // encryption name as used in 'cryptmethod'
34 char *magic; /* magic bytes stored in file header */ 34 char *magic; // magic bytes stored in file header
35 int salt_len; /* length of salt, or 0 when not using salt */ 35 int salt_len; // length of salt, or 0 when not using salt
36 int seed_len; /* length of seed, or 0 when not using salt */ 36 int seed_len; // length of seed, or 0 when not using salt
37 #ifdef CRYPT_NOT_INPLACE 37 #ifdef CRYPT_NOT_INPLACE
38 int works_inplace; /* encryption/decryption can be done in-place */ 38 int works_inplace; // encryption/decryption can be done in-place
39 #endif 39 #endif
40 int whole_undofile; /* whole undo file is encrypted */ 40 int whole_undofile; // whole undo file is encrypted
41 41
42 /* Optional function pointer for a self-test. */ 42 // Optional function pointer for a self-test.
43 int (* self_test_fn)(); 43 int (* self_test_fn)();
44 44
45 // Function pointer for initializing encryption/decryption. 45 // Function pointer for initializing encryption/decryption.
46 int (* init_fn)(cryptstate_T *state, char_u *key, 46 int (* init_fn)(cryptstate_T *state, char_u *key,
47 char_u *salt, int salt_len, char_u *seed, int seed_len); 47 char_u *salt, int salt_len, char_u *seed, int seed_len);
48 48
49 /* Function pointers for encoding/decoding from one buffer into another. 49 // Function pointers for encoding/decoding from one buffer into another.
50 * Optional, however, these or the _buffer ones should be configured. */ 50 // Optional, however, these or the _buffer ones should be configured.
51 void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len, 51 void (*encode_fn)(cryptstate_T *state, char_u *from, size_t len,
52 char_u *to); 52 char_u *to);
53 void (*decode_fn)(cryptstate_T *state, char_u *from, size_t len, 53 void (*decode_fn)(cryptstate_T *state, char_u *from, size_t len,
54 char_u *to); 54 char_u *to);
55 55
56 /* Function pointers for encoding and decoding, can buffer data if needed. 56 // Function pointers for encoding and decoding, can buffer data if needed.
57 * Optional (however, these or the above should be configured). */ 57 // Optional (however, these or the above should be configured).
58 long (*encode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, 58 long (*encode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len,
59 char_u **newptr); 59 char_u **newptr);
60 long (*decode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len, 60 long (*decode_buffer_fn)(cryptstate_T *state, char_u *from, size_t len,
61 char_u **newptr); 61 char_u **newptr);
62 62
63 /* Function pointers for in-place encoding and decoding, used for 63 // Function pointers for in-place encoding and decoding, used for
64 * crypt_*_inplace(). "from" and "to" arguments will be equal. 64 // crypt_*_inplace(). "from" and "to" arguments will be equal.
65 * These may be the same as decode_fn and encode_fn above, however an 65 // These may be the same as decode_fn and encode_fn above, however an
66 * algorithm may implement them in a way that is not interchangeable with 66 // algorithm may implement them in a way that is not interchangeable with
67 * the crypt_(en|de)code() interface (for example because it wishes to add 67 // the crypt_(en|de)code() interface (for example because it wishes to add
68 * padding to files). 68 // padding to files).
69 * This method is used for swap and undo files which have a rigid format. 69 // This method is used for swap and undo files which have a rigid format.
70 */
71 void (*encode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, 70 void (*encode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len,
72 char_u *p2); 71 char_u *p2);
73 void (*decode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len, 72 void (*decode_inplace_fn)(cryptstate_T *state, char_u *p1, size_t len,
74 char_u *p2); 73 char_u *p2);
75 } cryptmethod_T; 74 } cryptmethod_T;
76 75
77 /* index is method_nr of cryptstate_T, CRYPT_M_* */ 76 // index is method_nr of cryptstate_T, CRYPT_M_*
78 static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = { 77 static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = {
79 /* PK_Zip; very weak */ 78 // PK_Zip; very weak
80 { 79 {
81 "zip", 80 "zip",
82 "VimCrypt~01!", 81 "VimCrypt~01!",
83 0, 82 0,
84 0, 83 0,
91 crypt_zip_encode, crypt_zip_decode, 90 crypt_zip_encode, crypt_zip_decode,
92 NULL, NULL, 91 NULL, NULL,
93 crypt_zip_encode, crypt_zip_decode, 92 crypt_zip_encode, crypt_zip_decode,
94 }, 93 },
95 94
96 /* Blowfish/CFB + SHA-256 custom key derivation; implementation issues. */ 95 // Blowfish/CFB + SHA-256 custom key derivation; implementation issues.
97 { 96 {
98 "blowfish", 97 "blowfish",
99 "VimCrypt~02!", 98 "VimCrypt~02!",
100 8, 99 8,
101 8, 100 8,
108 crypt_blowfish_encode, crypt_blowfish_decode, 107 crypt_blowfish_encode, crypt_blowfish_decode,
109 NULL, NULL, 108 NULL, NULL,
110 crypt_blowfish_encode, crypt_blowfish_decode, 109 crypt_blowfish_encode, crypt_blowfish_decode,
111 }, 110 },
112 111
113 /* Blowfish/CFB + SHA-256 custom key derivation; fixed. */ 112 // Blowfish/CFB + SHA-256 custom key derivation; fixed.
114 { 113 {
115 "blowfish2", 114 "blowfish2",
116 "VimCrypt~03!", 115 "VimCrypt~03!",
117 8, 116 8,
118 8, 117 8,
125 crypt_blowfish_encode, crypt_blowfish_decode, 124 crypt_blowfish_encode, crypt_blowfish_decode,
126 NULL, NULL, 125 NULL, NULL,
127 crypt_blowfish_encode, crypt_blowfish_decode, 126 crypt_blowfish_encode, crypt_blowfish_decode,
128 }, 127 },
129 128
130 /* NOTE: when adding a new method, use some random bytes for the magic key, 129 // NOTE: when adding a new method, use some random bytes for the magic key,
131 * to avoid that a text file is recognized as encrypted. */ 130 // to avoid that a text file is recognized as encrypted.
132 }; 131 };
133 132
134 #define CRYPT_MAGIC_LEN 12 /* cannot change */ 133 #define CRYPT_MAGIC_LEN 12 // cannot change
135 static char crypt_magic_head[] = "VimCrypt~"; 134 static char crypt_magic_head[] = "VimCrypt~";
136 135
137 /* 136 /*
138 * Return int value for crypt method name. 137 * Return int value for crypt method name.
139 * 0 for "zip", the old method. Also for any non-valid value. 138 * 0 for "zip", the old method. Also for any non-valid value.
361 if (salt_len > 0) 360 if (salt_len > 0)
362 salt = *header + CRYPT_MAGIC_LEN; 361 salt = *header + CRYPT_MAGIC_LEN;
363 if (seed_len > 0) 362 if (seed_len > 0)
364 seed = *header + CRYPT_MAGIC_LEN + salt_len; 363 seed = *header + CRYPT_MAGIC_LEN + salt_len;
365 364
366 /* TODO: Should this be crypt method specific? (Probably not worth 365 // TODO: Should this be crypt method specific? (Probably not worth
367 * it). sha2_seed is pretty bad for large amounts of entropy, so make 366 // it). sha2_seed is pretty bad for large amounts of entropy, so make
368 * that into something which is suitable for anything. */ 367 // that into something which is suitable for anything.
369 sha2_seed(salt, salt_len, seed, seed_len); 368 sha2_seed(salt, salt_len, seed, seed_len);
370 } 369 }
371 370
372 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len); 371 state = crypt_create(method_nr, key, salt, salt_len, seed, seed_len);
373 if (state == NULL) 372 if (state == NULL)
399 char_u **newptr) 398 char_u **newptr)
400 { 399 {
401 cryptmethod_T *method = &cryptmethods[state->method_nr]; 400 cryptmethod_T *method = &cryptmethods[state->method_nr];
402 401
403 if (method->encode_buffer_fn != NULL) 402 if (method->encode_buffer_fn != NULL)
404 /* Has buffer function, pass through. */ 403 // Has buffer function, pass through.
405 return method->encode_buffer_fn(state, from, len, newptr); 404 return method->encode_buffer_fn(state, from, len, newptr);
406 if (len == 0) 405 if (len == 0)
407 /* Not buffering, just return EOF. */ 406 // Not buffering, just return EOF.
408 return (long)len; 407 return (long)len;
409 408
410 *newptr = alloc(len); 409 *newptr = alloc(len);
411 if (*newptr == NULL) 410 if (*newptr == NULL)
412 return -1; 411 return -1;
427 char_u **newptr) 426 char_u **newptr)
428 { 427 {
429 cryptmethod_T *method = &cryptmethods[state->method_nr]; 428 cryptmethod_T *method = &cryptmethods[state->method_nr];
430 429
431 if (method->decode_buffer_fn != NULL) 430 if (method->decode_buffer_fn != NULL)
432 /* Has buffer function, pass through. */ 431 // Has buffer function, pass through.
433 return method->decode_buffer_fn(state, ptr, len, newptr); 432 return method->decode_buffer_fn(state, ptr, len, newptr);
434 433
435 if (len == 0) 434 if (len == 0)
436 /* Not buffering, just return EOF. */ 435 // Not buffering, just return EOF.
437 return len; 436 return len;
438 437
439 *newptr = alloc(len); 438 *newptr = alloc(len);
440 if (*newptr == NULL) 439 if (*newptr == NULL)
441 return -1; 440 return -1;
540 * Returns NULL on failure. 539 * Returns NULL on failure.
541 */ 540 */
542 char_u * 541 char_u *
543 crypt_get_key( 542 crypt_get_key(
544 int store, 543 int store,
545 int twice) /* Ask for the key twice. */ 544 int twice) // Ask for the key twice.
546 { 545 {
547 char_u *p1, *p2 = NULL; 546 char_u *p1, *p2 = NULL;
548 int round; 547 int round;
549 548
550 for (round = 0; ; ++round) 549 for (round = 0; ; ++round)
566 { 565 {
567 msg(_("Keys don't match!")); 566 msg(_("Keys don't match!"));
568 crypt_free_key(p1); 567 crypt_free_key(p1);
569 crypt_free_key(p2); 568 crypt_free_key(p2);
570 p2 = NULL; 569 p2 = NULL;
571 round = -1; /* do it again */ 570 round = -1; // do it again
572 continue; 571 continue;
573 } 572 }
574 573
575 if (store) 574 if (store)
576 { 575 {
581 break; 580 break;
582 } 581 }
583 p2 = p1; 582 p2 = p1;
584 } 583 }
585 584
586 /* since the user typed this, no need to wait for return */ 585 // since the user typed this, no need to wait for return
587 if (msg_didout) 586 if (msg_didout)
588 msg_putchar('\n'); 587 msg_putchar('\n');
589 need_wait_return = FALSE; 588 need_wait_return = FALSE;
590 msg_didout = FALSE; 589 msg_didout = FALSE;
591 590
609 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm); 608 STRCAT(IObuff, *buf->b_p_cm == NUL ? p_cm : buf->b_p_cm);
610 STRCAT(IObuff, "]"); 609 STRCAT(IObuff, "]");
611 } 610 }
612 } 611 }
613 612
614 #endif /* FEAT_CRYPT */ 613 #endif // FEAT_CRYPT