Mercurial > pidgin
comparison src/cipher.c @ 11335:59aa7080eb2d
[gaim-migrate @ 13548]
Added DES to ciphers. It is needed for NTLM
committer: Tailor Script <tailor@pidgin.im>
| author | Thomas Butter <tbutter> |
|---|---|
| date | Wed, 24 Aug 2005 19:02:59 +0000 |
| parents | b0185f9214d3 |
| children | 036bd21c5560 |
comparison
equal
deleted
inserted
replaced
| 11334:681870123ce3 | 11335:59aa7080eb2d |
|---|---|
| 20 * Modified by Steve French (sfrench@us.ibm.com) 2002 | 20 * Modified by Steve French (sfrench@us.ibm.com) 2002 |
| 21 * Copyright (c) Cryptoapi developers. | 21 * Copyright (c) Cryptoapi developers. |
| 22 * Copyright (c) 2002 David S. Miller (davem@redhat.com) | 22 * Copyright (c) 2002 David S. Miller (davem@redhat.com) |
| 23 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> | 23 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 24 * | 24 * |
| 25 * Original des taken from gpg | |
| 26 * | |
| 27 * des.c - DES and Triple-DES encryption/decryption Algorithm | |
| 28 * Copyright (C) 1998 Free Software Foundation, Inc. | |
| 29 * | |
| 30 * Please see below for more legal information! | |
| 31 * | |
| 32 * According to the definition of DES in FIPS PUB 46-2 from December 1993. | |
| 33 * For a description of triple encryption, see: | |
| 34 * Bruce Schneier: Applied Cryptography. Second Edition. | |
| 35 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff. | |
| 36 * | |
| 37 * This file is part of GnuPG. | |
| 38 * | |
| 25 * This program is free software; you can redistribute it and/or modify | 39 * This program is free software; you can redistribute it and/or modify |
| 26 * it under the terms of the GNU General Public License as published by | 40 * it under the terms of the GNU General Public License as published by |
| 27 * the Free Software Foundation; either version 2 of the License, or | 41 * the Free Software Foundation; either version 2 of the License, or |
| 28 * (at your option) any later version. | 42 * (at your option) any later version. |
| 29 * | 43 * |
| 573 NULL, /* set salt */ | 587 NULL, /* set salt */ |
| 574 NULL, /* get salt size */ | 588 NULL, /* get salt size */ |
| 575 NULL, /* set key */ | 589 NULL, /* set key */ |
| 576 NULL /* get key size */ | 590 NULL /* get key size */ |
| 577 }; | 591 }; |
| 592 | |
| 593 /****************************************************************************** | |
| 594 * DES | |
| 595 *****************************************************************************/ | |
| 596 | |
| 597 typedef struct _des_ctx | |
| 598 { | |
| 599 guint32 encrypt_subkeys[32]; | |
| 600 guint32 decrypt_subkeys[32]; | |
| 601 } des_ctx[1]; | |
| 602 | |
| 603 /* | |
| 604 * The s-box values are permuted according to the 'primitive function P' | |
| 605 */ | |
| 606 static guint32 sbox1[64] = | |
| 607 { | |
| 608 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000, | |
| 609 0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002, | |
| 610 0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202, | |
| 611 0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000, | |
| 612 0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200, | |
| 613 0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202, | |
| 614 0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200, | |
| 615 0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002 | |
| 616 }; | |
| 617 | |
| 618 static guint32 sbox2[64] = | |
| 619 { | |
| 620 0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010, | |
| 621 0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010, | |
| 622 0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000, | |
| 623 0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010, | |
| 624 0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000, | |
| 625 0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000, | |
| 626 0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010, | |
| 627 0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000 | |
| 628 }; | |
| 629 | |
| 630 static guint32 sbox3[64] = | |
| 631 { | |
| 632 0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100, | |
| 633 0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104, | |
| 634 0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104, | |
| 635 0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000, | |
| 636 0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000, | |
| 637 0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004, | |
| 638 0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004, | |
| 639 0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100 | |
| 640 }; | |
| 641 | |
| 642 static guint32 sbox4[64] = | |
| 643 { | |
| 644 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000, | |
| 645 0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000, | |
| 646 0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040, | |
| 647 0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040, | |
| 648 0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000, | |
| 649 0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040, | |
| 650 0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040, | |
| 651 0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040 | |
| 652 }; | |
| 653 | |
| 654 static guint32 sbox5[64] = | |
| 655 { | |
| 656 0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000, | |
| 657 0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000, | |
| 658 0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080, | |
| 659 0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080, | |
| 660 0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080, | |
| 661 0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000, | |
| 662 0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000, | |
| 663 0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080 | |
| 664 }; | |
| 665 | |
| 666 static guint32 sbox6[64] = | |
| 667 { | |
| 668 0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000, | |
| 669 0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008, | |
| 670 0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008, | |
| 671 0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000, | |
| 672 0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008, | |
| 673 0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000, | |
| 674 0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008, | |
| 675 0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008 | |
| 676 }; | |
| 677 | |
| 678 static guint32 sbox7[64] = | |
| 679 { | |
| 680 0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400, | |
| 681 0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401, | |
| 682 0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001, | |
| 683 0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400, | |
| 684 0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001, | |
| 685 0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400, | |
| 686 0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401, | |
| 687 0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001 | |
| 688 }; | |
| 689 | |
| 690 static guint32 sbox8[64] = | |
| 691 { | |
| 692 0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000, | |
| 693 0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x08020800, 0x00020820, 0x00000800, 0x00000020, | |
| 694 0x08020000, 0x08000020, 0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800, | |
| 695 0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800, 0x00020820, 0x00020000, | |
| 696 0x00020820, 0x00020000, 0x08020800, 0x00000800, 0x00000020, 0x08020020, 0x00000800, 0x00020820, | |
| 697 0x08000800, 0x00000020, 0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820, | |
| 698 0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800, 0x08000820, 0x00000000, | |
| 699 0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800 | |
| 700 }; | |
| 701 | |
| 702 | |
| 703 | |
| 704 /* | |
| 705 * * These two tables are part of the 'permuted choice 1' function. | |
| 706 * * In this implementation several speed improvements are done. | |
| 707 * */ | |
| 708 guint32 leftkey_swap[16] = | |
| 709 { | |
| 710 0x00000000, 0x00000001, 0x00000100, 0x00000101, | |
| 711 0x00010000, 0x00010001, 0x00010100, 0x00010101, | |
| 712 0x01000000, 0x01000001, 0x01000100, 0x01000101, | |
| 713 0x01010000, 0x01010001, 0x01010100, 0x01010101 | |
| 714 }; | |
| 715 | |
| 716 guint32 rightkey_swap[16] = | |
| 717 { | |
| 718 0x00000000, 0x01000000, 0x00010000, 0x01010000, | |
| 719 0x00000100, 0x01000100, 0x00010100, 0x01010100, | |
| 720 0x00000001, 0x01000001, 0x00010001, 0x01010001, | |
| 721 0x00000101, 0x01000101, 0x00010101, 0x01010101, | |
| 722 }; | |
| 723 | |
| 724 | |
| 725 | |
| 726 /* | |
| 727 * Numbers of left shifts per round for encryption subkey schedule | |
| 728 * To calculate the decryption key scheduling we just reverse the | |
| 729 * ordering of the subkeys so we can omit the table for decryption | |
| 730 * subkey schedule. | |
| 731 */ | |
| 732 static guint8 encrypt_rotate_tab[16] = | |
| 733 { | |
| 734 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 | |
| 735 }; | |
| 736 | |
| 737 /* | |
| 738 * Macro to swap bits across two words | |
| 739 **/ | |
| 740 #define DO_PERMUTATION(a, temp, b, offset, mask) \ | |
| 741 temp = ((a>>offset) ^ b) & mask; \ | |
| 742 b ^= temp; \ | |
| 743 a ^= temp<<offset; | |
| 744 | |
| 745 | |
| 746 /* | |
| 747 * This performs the 'initial permutation' for the data to be encrypted or decrypted | |
| 748 **/ | |
| 749 #define INITIAL_PERMUTATION(left, temp, right) \ | |
| 750 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) \ | |
| 751 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \ | |
| 752 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \ | |
| 753 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \ | |
| 754 DO_PERMUTATION(left, temp, right, 1, 0x55555555) | |
| 755 | |
| 756 | |
| 757 /* | |
| 758 * The 'inverse initial permutation' | |
| 759 **/ | |
| 760 #define FINAL_PERMUTATION(left, temp, right) \ | |
| 761 DO_PERMUTATION(left, temp, right, 1, 0x55555555) \ | |
| 762 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \ | |
| 763 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \ | |
| 764 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \ | |
| 765 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) | |
| 766 | |
| 767 | |
| 768 /* | |
| 769 * A full DES round including 'expansion function', 'sbox substitution' | |
| 770 * and 'primitive function P' but without swapping the left and right word. | |
| 771 **/ | |
| 772 #define DES_ROUND(from, to, work, subkey) \ | |
| 773 work = ((from<<1) | (from>>31)) ^ *subkey++; \ | |
| 774 to ^= sbox8[ work & 0x3f ]; \ | |
| 775 to ^= sbox6[ (work>>8) & 0x3f ]; \ | |
| 776 to ^= sbox4[ (work>>16) & 0x3f ]; \ | |
| 777 to ^= sbox2[ (work>>24) & 0x3f ]; \ | |
| 778 work = ((from>>3) | (from<<29)) ^ *subkey++; \ | |
| 779 to ^= sbox7[ work & 0x3f ]; \ | |
| 780 to ^= sbox5[ (work>>8) & 0x3f ]; \ | |
| 781 to ^= sbox3[ (work>>16) & 0x3f ]; \ | |
| 782 to ^= sbox1[ (work>>24) & 0x3f ]; | |
| 783 | |
| 784 | |
| 785 /* | |
| 786 * Macros to convert 8 bytes from/to 32bit words | |
| 787 **/ | |
| 788 #define READ_64BIT_DATA(data, left, right) \ | |
| 789 left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \ | |
| 790 right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7]; | |
| 791 | |
| 792 #define WRITE_64BIT_DATA(data, left, right) \ | |
| 793 data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \ | |
| 794 data[2] = (left >> 8) &0xff; data[3] = left &0xff; \ | |
| 795 data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \ | |
| 796 data[6] = (right >> 8) &0xff; data[7] = right &0xff; | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 /* | |
| 804 * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for | |
| 805 * 16 encryption rounds. | |
| 806 * To calculate subkeys for decryption the caller | |
| 807 * have to reorder the generated subkeys. | |
| 808 * | |
| 809 * rawkey: 8 Bytes of key data | |
| 810 * subkey: Array of at least 32 guint32s. Will be filled | |
| 811 * with calculated subkeys. | |
| 812 * | |
| 813 **/ | |
| 814 static void | |
| 815 des_key_schedule (const guint8 * rawkey, guint32 * subkey) | |
| 816 { | |
| 817 guint32 left, right, work; | |
| 818 int round; | |
| 819 | |
| 820 READ_64BIT_DATA (rawkey, left, right) | |
| 821 | |
| 822 DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f) | |
| 823 DO_PERMUTATION (right, work, left, 0, 0x10101010) | |
| 824 | |
| 825 left = (leftkey_swap[(left >> 0) & 0xf] << 3) | (leftkey_swap[(left >> 8) & 0xf] << 2) | |
| 826 | (leftkey_swap[(left >> 16) & 0xf] << 1) | (leftkey_swap[(left >> 24) & 0xf]) | |
| 827 | (leftkey_swap[(left >> 5) & 0xf] << 7) | (leftkey_swap[(left >> 13) & 0xf] << 6) | |
| 828 | (leftkey_swap[(left >> 21) & 0xf] << 5) | (leftkey_swap[(left >> 29) & 0xf] << 4); | |
| 829 | |
| 830 left &= 0x0fffffff; | |
| 831 | |
| 832 right = (rightkey_swap[(right >> 1) & 0xf] << 3) | (rightkey_swap[(right >> 9) & 0xf] << 2) | |
| 833 | (rightkey_swap[(right >> 17) & 0xf] << 1) | (rightkey_swap[(right >> 25) & 0xf]) | |
| 834 | (rightkey_swap[(right >> 4) & 0xf] << 7) | (rightkey_swap[(right >> 12) & 0xf] << 6) | |
| 835 | (rightkey_swap[(right >> 20) & 0xf] << 5) | (rightkey_swap[(right >> 28) & 0xf] << 4); | |
| 836 | |
| 837 right &= 0x0fffffff; | |
| 838 | |
| 839 for (round = 0; round < 16; ++round) | |
| 840 { | |
| 841 left = ((left << encrypt_rotate_tab[round]) | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff; | |
| 842 right = ((right << encrypt_rotate_tab[round]) | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff; | |
| 843 | |
| 844 *subkey++ = ((left << 4) & 0x24000000) | |
| 845 | ((left << 28) & 0x10000000) | |
| 846 | ((left << 14) & 0x08000000) | |
| 847 | ((left << 18) & 0x02080000) | |
| 848 | ((left << 6) & 0x01000000) | |
| 849 | ((left << 9) & 0x00200000) | |
| 850 | ((left >> 1) & 0x00100000) | |
| 851 | ((left << 10) & 0x00040000) | |
| 852 | ((left << 2) & 0x00020000) | |
| 853 | ((left >> 10) & 0x00010000) | |
| 854 | ((right >> 13) & 0x00002000) | |
| 855 | ((right >> 4) & 0x00001000) | |
| 856 | ((right << 6) & 0x00000800) | |
| 857 | ((right >> 1) & 0x00000400) | |
| 858 | ((right >> 14) & 0x00000200) | |
| 859 | (right & 0x00000100) | |
| 860 | ((right >> 5) & 0x00000020) | |
| 861 | ((right >> 10) & 0x00000010) | |
| 862 | ((right >> 3) & 0x00000008) | |
| 863 | ((right >> 18) & 0x00000004) | |
| 864 | ((right >> 26) & 0x00000002) | |
| 865 | ((right >> 24) & 0x00000001); | |
| 866 | |
| 867 *subkey++ = ((left << 15) & 0x20000000) | |
| 868 | ((left << 17) & 0x10000000) | |
| 869 | ((left << 10) & 0x08000000) | |
| 870 | ((left << 22) & 0x04000000) | |
| 871 | ((left >> 2) & 0x02000000) | |
| 872 | ((left << 1) & 0x01000000) | |
| 873 | ((left << 16) & 0x00200000) | |
| 874 | ((left << 11) & 0x00100000) | |
| 875 | ((left << 3) & 0x00080000) | |
| 876 | ((left >> 6) & 0x00040000) | |
| 877 | ((left << 15) & 0x00020000) | |
| 878 | ((left >> 4) & 0x00010000) | |
| 879 | ((right >> 2) & 0x00002000) | |
| 880 | ((right << 8) & 0x00001000) | |
| 881 | ((right >> 14) & 0x00000808) | |
| 882 | ((right >> 9) & 0x00000400) | |
| 883 | ((right) & 0x00000200) | |
| 884 | ((right << 7) & 0x00000100) | |
| 885 | ((right >> 7) & 0x00000020) | |
| 886 | ((right >> 3) & 0x00000011) | |
| 887 | ((right << 2) & 0x00000004) | |
| 888 | ((right >> 21) & 0x00000002); | |
| 889 } | |
| 890 } | |
| 891 | |
| 892 | |
| 893 | |
| 894 /* | |
| 895 * Fill a DES context with subkeys calculated from a 64bit key. | |
| 896 * Does not check parity bits, but simply ignore them. | |
| 897 * Does not check for weak keys. | |
| 898 **/ | |
| 899 void | |
| 900 des_setkey (GaimCipherContext *context, guchar * key) | |
| 901 { | |
| 902 struct _des_ctx *ctx = gaim_cipher_context_get_data(context); | |
| 903 int i; | |
| 904 | |
| 905 des_key_schedule (key, ctx->encrypt_subkeys); | |
| 906 | |
| 907 for(i=0; i<32; i+=2) | |
| 908 { | |
| 909 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30-i]; | |
| 910 ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[31-i]; | |
| 911 } | |
| 912 } | |
| 913 | |
| 914 | |
| 915 | |
| 916 /* | |
| 917 * Electronic Codebook Mode DES encryption/decryption of data according | |
| 918 * to 'mode'. | |
| 919 **/ | |
| 920 int | |
| 921 des_ecb_crypt (struct _des_ctx *ctx, const guint8 * from, guint8 * to, int mode) | |
| 922 { | |
| 923 guint32 left, right, work; | |
| 924 guint32 *keys; | |
| 925 | |
| 926 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys; | |
| 927 | |
| 928 READ_64BIT_DATA (from, left, right) | |
| 929 INITIAL_PERMUTATION (left, work, right) | |
| 930 | |
| 931 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 932 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 933 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 934 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 935 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 936 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 937 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 938 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys) | |
| 939 | |
| 940 FINAL_PERMUTATION (right, work, left) | |
| 941 WRITE_64BIT_DATA (to, right, left) | |
| 942 | |
| 943 return 0; | |
| 944 } | |
| 945 | |
| 946 gint des_encrypt(GaimCipherContext *context, const guchar data[], size_t len, guchar output[], size_t *outlen) { | |
| 947 int offset = 0; | |
| 948 int i = 0; | |
| 949 int tmp; | |
| 950 guint8 buf[8] = {0,0,0,0,0,0,0,0}; | |
| 951 while(offset+8<=len) { | |
| 952 des_ecb_crypt(gaim_cipher_context_get_data(context), | |
| 953 data+offset, | |
| 954 output+offset, | |
| 955 0); | |
| 956 offset+=8; | |
| 957 } | |
| 958 *outlen = len; | |
| 959 if(offset<len) { | |
| 960 *outlen += len - offset; | |
| 961 tmp = offset; | |
| 962 while(tmp<len) { | |
| 963 buf[i++] = data[tmp]; | |
| 964 tmp++; | |
| 965 } | |
| 966 des_ecb_crypt(gaim_cipher_context_get_data(context), | |
| 967 buf, | |
| 968 output+offset, | |
| 969 0); | |
| 970 } | |
| 971 return 0; | |
| 972 } | |
| 973 | |
| 974 static void | |
| 975 des_init(GaimCipherContext *context, gpointer extra) { | |
| 976 struct _des_ctx *mctx; | |
| 977 mctx = g_new0(struct _des_ctx, 1); | |
| 978 gaim_cipher_context_set_data(context, mctx); | |
| 979 } | |
| 980 | |
| 981 static void | |
| 982 des_uninit(GaimCipherContext *context) { | |
| 983 struct _des_ctx *des_context; | |
| 984 | |
| 985 des_context = gaim_cipher_context_get_data(context); | |
| 986 memset(des_context, 0, sizeof(des_context)); | |
| 987 | |
| 988 g_free(des_context); | |
| 989 des_context = NULL; | |
| 990 } | |
| 991 | |
| 992 static GaimCipherOps DESOps = { | |
| 993 NULL, /* Set option */ | |
| 994 NULL, /* Get option */ | |
| 995 des_init, /* init */ | |
| 996 NULL, /* reset */ | |
| 997 des_uninit, /* uninit */ | |
| 998 NULL, /* set iv */ | |
| 999 NULL, /* append */ | |
| 1000 NULL, /* digest */ | |
| 1001 des_encrypt, /* encrypt */ | |
| 1002 NULL, /* decrypt */ | |
| 1003 NULL, /* set salt */ | |
| 1004 NULL, /* get salt size */ | |
| 1005 des_setkey, /* set key */ | |
| 1006 NULL /* get key size */ | |
| 1007 }; | |
| 1008 | |
| 578 | 1009 |
| 579 /******************************************************************************* | 1010 /******************************************************************************* |
| 580 * SHA-1 | 1011 * SHA-1 |
| 581 ******************************************************************************/ | 1012 ******************************************************************************/ |
| 582 #define SHA1_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xFFFFFFFF) | 1013 #define SHA1_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xFFFFFFFF) |
| 1004 GAIM_SUBTYPE_CIPHER)); | 1435 GAIM_SUBTYPE_CIPHER)); |
| 1005 | 1436 |
| 1006 gaim_ciphers_register_cipher("md5", &MD5Ops); | 1437 gaim_ciphers_register_cipher("md5", &MD5Ops); |
| 1007 gaim_ciphers_register_cipher("sha1", &SHA1Ops); | 1438 gaim_ciphers_register_cipher("sha1", &SHA1Ops); |
| 1008 gaim_ciphers_register_cipher("md4", &MD4Ops); | 1439 gaim_ciphers_register_cipher("md4", &MD4Ops); |
| 1440 gaim_ciphers_register_cipher("des", &DESOps); | |
| 1009 } | 1441 } |
| 1010 | 1442 |
| 1011 void | 1443 void |
| 1012 gaim_ciphers_uninit() { | 1444 gaim_ciphers_uninit() { |
| 1013 GaimCipher *cipher; | 1445 GaimCipher *cipher; |
