Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/AES.d @ 14:5ce3012f1def
Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Tue, 18 Nov 2008 18:03:40 -0500 |
parents | 8c7f8fecdd75 |
children | 4589f8c5eb3c |
comparison
equal
deleted
inserted
replaced
13:7ea528b61802 | 14:5ce3012f1def |
---|---|
638 | 638 |
639 uint ROUNDS, // Number of rounds depends on keysize | 639 uint ROUNDS, // Number of rounds depends on keysize |
640 s0, s1, s2, s3; // State | 640 s0, s1, s2, s3; // State |
641 uint[] w; // Expanded key | 641 uint[] w; // Expanded key |
642 ubyte[] workingKey; | 642 ubyte[] workingKey; |
643 | 643 |
644 bool initialized, | |
645 encrypt; | |
646 } // end private | 644 } // end private |
647 | 645 |
648 char[] name() { | 646 char[] name() { |
649 return "AES"; | 647 return "AES"; |
650 } | 648 } |
651 | 649 |
652 uint rounds() { | 650 uint rounds() { |
653 if (!initialized) | 651 if (!_initialized) |
654 throw new NotInitializedError(name()~": Cipher not initialized."); | 652 throw new NotInitializedError(name()~": Cipher not initialized."); |
655 return ROUNDS; | 653 return ROUNDS; |
656 } | 654 } |
657 | 655 |
658 uint blockSize() { | 656 uint blockSize() { |
662 void init(bool encrypt, CipherParameters params) { | 660 void init(bool encrypt, CipherParameters params) { |
663 SymmetricKey keyParams = cast(SymmetricKey)params; | 661 SymmetricKey keyParams = cast(SymmetricKey)params; |
664 if (!keyParams) | 662 if (!keyParams) |
665 throw new InvalidParameterError( | 663 throw new InvalidParameterError( |
666 name()~": Invalid parameter passed to init"); | 664 name()~": Invalid parameter passed to init"); |
667 this.encrypt = encrypt; | 665 _encrypt = encrypt; |
668 | 666 |
669 uint len = keyParams.key.length; | 667 uint len = keyParams.key.length; |
670 if (len != 16 && len != 24 && len != 32) | 668 if (len != 16 && len != 24 && len != 32) |
671 throw new InvalidKeyError( | 669 throw new InvalidKeyError( |
672 name()~": Invalid key length (requires 16, 24 or 32 bytes)"); | 670 name()~": Invalid key length (requires 16, 24 or 32 bytes)"); |
673 workingKey = keyParams.key; | 671 workingKey = keyParams.key; |
674 | 672 |
675 setup(workingKey); | 673 setup(workingKey); |
676 | 674 |
677 initialized = true; | 675 _initialized = true; |
678 } | 676 } |
679 | 677 |
680 private void encryptBlock() { | 678 private void encryptBlock() { |
681 uint i = 4, | 679 uint i = 4, |
682 r = ROUNDS >> 1, | 680 r = ROUNDS >> 1, |
799 (RS[cast(ubyte)(t1 >> 8)] << 8) ^ | 797 (RS[cast(ubyte)(t1 >> 8)] << 8) ^ |
800 RS[cast(ubyte) t0]; | 798 RS[cast(ubyte) t0]; |
801 } | 799 } |
802 | 800 |
803 uint update(void[] input_, void[] output_) { | 801 uint update(void[] input_, void[] output_) { |
804 if (!initialized) | 802 if (!_initialized) |
805 throw new NotInitializedError(name()~": Cipher not initialized."); | 803 throw new NotInitializedError(name()~": Cipher not initialized."); |
806 | 804 |
807 ubyte[] input = cast(ubyte[]) input_, | 805 ubyte[] input = cast(ubyte[]) input_, |
808 output = cast(ubyte[]) output_; | 806 output = cast(ubyte[]) output_; |
809 | 807 |
816 s0 = w[0] ^ Util.ubytesToUintBig(input, 0); | 814 s0 = w[0] ^ Util.ubytesToUintBig(input, 0); |
817 s1 = w[1] ^ Util.ubytesToUintBig(input, 4); | 815 s1 = w[1] ^ Util.ubytesToUintBig(input, 4); |
818 s2 = w[2] ^ Util.ubytesToUintBig(input, 8); | 816 s2 = w[2] ^ Util.ubytesToUintBig(input, 8); |
819 s3 = w[3] ^ Util.ubytesToUintBig(input, 12); | 817 s3 = w[3] ^ Util.ubytesToUintBig(input, 12); |
820 | 818 |
821 if (encrypt) encryptBlock(); else decryptBlock(); | 819 if (_encrypt) encryptBlock(); else decryptBlock(); |
822 | 820 |
823 Util.uintToUbytesBig(s0, output, 0); | 821 Util.uintToUbytesBig(s0, output, 0); |
824 Util.uintToUbytesBig(s1, output, 4); | 822 Util.uintToUbytesBig(s1, output, 4); |
825 Util.uintToUbytesBig(s2, output, 8); | 823 Util.uintToUbytesBig(s2, output, 8); |
826 Util.uintToUbytesBig(s3, output, 12); | 824 Util.uintToUbytesBig(s3, output, 12); |
853 else if (nk > 6 && (i % nk == 4)) | 851 else if (nk > 6 && (i % nk == 4)) |
854 t = subWord(t); | 852 t = subWord(t); |
855 w[i] = w[i-nk] ^ t; | 853 w[i] = w[i-nk] ^ t; |
856 } | 854 } |
857 | 855 |
858 if (!encrypt) { | 856 if (!_encrypt) { |
859 for (uint i = 0; i <= 4*ROUNDS; i+=4) | 857 for (uint i = 0; i <= 4*ROUNDS; i+=4) |
860 w[i..i+4].reverse; | 858 w[i..i+4].reverse; |
861 w.reverse; | 859 w.reverse; |
862 | 860 |
863 for (uint i = 4; i < w.length-4; i++) { | 861 for (uint i = 4; i < w.length-4; i++) { |