comparison src/blowfish.c @ 5651:391e10afccf6 v7.4.172

updated for version 7.4.172 Problem: The blowfish code mentions output feedback, but the code is actually doing cipher feedback. Solution: Adjust names and comments.
author Bram Moolenaar <bram@vim.org>
date Tue, 11 Feb 2014 15:23:32 +0100
parents 2d9eebf2c48f
children 18ac55444b37
comparison
equal deleted inserted replaced
5650:5b8873427318 5651:391e10afccf6
4 * 4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions. 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. 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. 7 * See README.txt for an overview of the Vim source code.
8 * 8 *
9 * Blowfish encryption for Vim; in Blowfish output feedback mode. 9 * Blowfish encryption for Vim; in Blowfish cipher feedback mode.
10 * Contributed by Mohsin Ahmed, http://www.cs.albany.edu/~mosh 10 * Contributed by Mohsin Ahmed, http://www.cs.albany.edu/~mosh
11 * Based on http://www.schneier.com/blowfish.html by Bruce Schneier. 11 * Based on http://www.schneier.com/blowfish.html by Bruce Schneier.
12 */ 12 */
13 13
14 #include "vim.h" 14 #include "vim.h"
17 17
18 #define ARRAY_LENGTH(A) (sizeof(A)/sizeof(A[0])) 18 #define ARRAY_LENGTH(A) (sizeof(A)/sizeof(A[0]))
19 19
20 #define BF_BLOCK 8 20 #define BF_BLOCK 8
21 #define BF_BLOCK_MASK 7 21 #define BF_BLOCK_MASK 7
22 #define BF_OFB_LEN (8*(BF_BLOCK)) 22 #define BF_CFB_LEN (8*(BF_BLOCK))
23 23
24 typedef union { 24 typedef union {
25 UINT32_T ul[2]; 25 UINT32_T ul[2];
26 char_u uc[8]; 26 char_u uc[8];
27 } block8; 27 } block8;
552 } 552 }
553 553
554 return err > 0 ? FAIL : OK; 554 return err > 0 ? FAIL : OK;
555 } 555 }
556 556
557 /* Output feedback mode. */ 557 /* Cipher feedback mode. */
558 static int randbyte_offset = 0; 558 static int randbyte_offset = 0;
559 static int update_offset = 0; 559 static int update_offset = 0;
560 static char_u ofb_buffer[BF_OFB_LEN]; /* 64 bytes */ 560 static char_u cfb_buffer[BF_CFB_LEN]; /* 64 bytes */
561 561
562 /* 562 /*
563 * Initialize with seed "iv[iv_len]". 563 * Initialize with seed "iv[iv_len]".
564 */ 564 */
565 void 565 void
566 bf_ofb_init(iv, iv_len) 566 bf_cfb_init(iv, iv_len)
567 char_u *iv; 567 char_u *iv;
568 int iv_len; 568 int iv_len;
569 { 569 {
570 int i, mi; 570 int i, mi;
571 571
572 randbyte_offset = update_offset = 0; 572 randbyte_offset = update_offset = 0;
573 vim_memset(ofb_buffer, 0, BF_OFB_LEN); 573 vim_memset(cfb_buffer, 0, BF_CFB_LEN);
574 if (iv_len > 0) 574 if (iv_len > 0)
575 { 575 {
576 mi = iv_len > BF_OFB_LEN ? iv_len : BF_OFB_LEN; 576 mi = iv_len > BF_CFB_LEN ? iv_len : BF_CFB_LEN;
577 for (i = 0; i < mi; i++) 577 for (i = 0; i < mi; i++)
578 ofb_buffer[i % BF_OFB_LEN] ^= iv[i % iv_len]; 578 cfb_buffer[i % BF_CFB_LEN] ^= iv[i % iv_len];
579 } 579 }
580 } 580 }
581 581
582 #define BF_OFB_UPDATE(c) { \ 582 #define BF_CFB_UPDATE(c) { \
583 ofb_buffer[update_offset] ^= (char_u)c; \ 583 cfb_buffer[update_offset] ^= (char_u)c; \
584 if (++update_offset == BF_OFB_LEN) \ 584 if (++update_offset == BF_CFB_LEN) \
585 update_offset = 0; \ 585 update_offset = 0; \
586 } 586 }
587 587
588 #define BF_RANBYTE(t) { \ 588 #define BF_RANBYTE(t) { \
589 if ((randbyte_offset & BF_BLOCK_MASK) == 0) \ 589 if ((randbyte_offset & BF_BLOCK_MASK) == 0) \
590 bf_e_cblock(&ofb_buffer[randbyte_offset]); \ 590 bf_e_cblock(&cfb_buffer[randbyte_offset]); \
591 t = ofb_buffer[randbyte_offset]; \ 591 t = cfb_buffer[randbyte_offset]; \
592 if (++randbyte_offset == BF_OFB_LEN) \ 592 if (++randbyte_offset == BF_CFB_LEN) \
593 randbyte_offset = 0; \ 593 randbyte_offset = 0; \
594 } 594 }
595 595
596 /* 596 /*
597 * Encrypt "from[len]" into "to[len]". 597 * Encrypt "from[len]" into "to[len]".
608 608
609 for (i = 0; i < len; ++i) 609 for (i = 0; i < len; ++i)
610 { 610 {
611 ztemp = from[i]; 611 ztemp = from[i];
612 BF_RANBYTE(t); 612 BF_RANBYTE(t);
613 BF_OFB_UPDATE(ztemp); 613 BF_CFB_UPDATE(ztemp);
614 to[i] = t ^ ztemp; 614 to[i] = t ^ ztemp;
615 } 615 }
616 } 616 }
617 617
618 /* 618 /*
628 628
629 for (p = ptr; p < ptr + len; ++p) 629 for (p = ptr; p < ptr + len; ++p)
630 { 630 {
631 BF_RANBYTE(t); 631 BF_RANBYTE(t);
632 *p ^= t; 632 *p ^= t;
633 BF_OFB_UPDATE(*p); 633 BF_CFB_UPDATE(*p);
634 } 634 }
635 } 635 }
636 636
637 /* 637 /*
638 * Initialize the encryption keys and the random header according to 638 * Initialize the encryption keys and the random header according to
644 { 644 {
645 char_u *p; 645 char_u *p;
646 646
647 for (p = passwd; *p != NUL; ++p) 647 for (p = passwd; *p != NUL; ++p)
648 { 648 {
649 BF_OFB_UPDATE(*p); 649 BF_CFB_UPDATE(*p);
650 } 650 }
651 } 651 }
652 652
653 static int save_randbyte_offset; 653 static int save_randbyte_offset;
654 static int save_update_offset; 654 static int save_update_offset;
655 static char_u save_ofb_buffer[BF_OFB_LEN]; 655 static char_u save_cfb_buffer[BF_CFB_LEN];
656 static UINT32_T save_pax[18]; 656 static UINT32_T save_pax[18];
657 static UINT32_T save_sbx[4][256]; 657 static UINT32_T save_sbx[4][256];
658 658
659 /* 659 /*
660 * Save the current crypt state. Can only be used once before 660 * Save the current crypt state. Can only be used once before
663 void 663 void
664 bf_crypt_save() 664 bf_crypt_save()
665 { 665 {
666 save_randbyte_offset = randbyte_offset; 666 save_randbyte_offset = randbyte_offset;
667 save_update_offset = update_offset; 667 save_update_offset = update_offset;
668 mch_memmove(save_ofb_buffer, ofb_buffer, BF_OFB_LEN); 668 mch_memmove(save_cfb_buffer, cfb_buffer, BF_CFB_LEN);
669 mch_memmove(save_pax, pax, 4 * 18); 669 mch_memmove(save_pax, pax, 4 * 18);
670 mch_memmove(save_sbx, sbx, 4 * 4 * 256); 670 mch_memmove(save_sbx, sbx, 4 * 4 * 256);
671 } 671 }
672 672
673 /* 673 /*
677 void 677 void
678 bf_crypt_restore() 678 bf_crypt_restore()
679 { 679 {
680 randbyte_offset = save_randbyte_offset; 680 randbyte_offset = save_randbyte_offset;
681 update_offset = save_update_offset; 681 update_offset = save_update_offset;
682 mch_memmove(ofb_buffer, save_ofb_buffer, BF_OFB_LEN); 682 mch_memmove(cfb_buffer, save_cfb_buffer, BF_CFB_LEN);
683 mch_memmove(pax, save_pax, 4 * 18); 683 mch_memmove(pax, save_pax, 4 * 18);
684 mch_memmove(sbx, save_sbx, 4 * 4 * 256); 684 mch_memmove(sbx, save_sbx, 4 * 4 * 256);
685 } 685 }
686 686
687 /* 687 /*