Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/AES.d @ 12:8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 30 Aug 2008 14:38:23 -0400 |
parents | 23c62e28b3a4 |
children | 5ce3012f1def |
comparison
equal
deleted
inserted
replaced
11:02970e63257d | 12:8c7f8fecdd75 |
---|---|
798 (RS[cast(ubyte)(t2 >> 16)] << 16) ^ | 798 (RS[cast(ubyte)(t2 >> 16)] << 16) ^ |
799 (RS[cast(ubyte)(t1 >> 8)] << 8) ^ | 799 (RS[cast(ubyte)(t1 >> 8)] << 8) ^ |
800 RS[cast(ubyte) t0]; | 800 RS[cast(ubyte) t0]; |
801 } | 801 } |
802 | 802 |
803 ubyte[] process(void[] input_) { | 803 uint update(void[] input_, void[] output_) { |
804 if (!initialized) | 804 if (!initialized) |
805 throw new NotInitializedError(name()~": Cipher not initialized."); | 805 throw new NotInitializedError(name()~": Cipher not initialized."); |
806 | 806 |
807 ubyte[] input = cast(ubyte[]) input_; | 807 ubyte[] input = cast(ubyte[]) input_, |
808 output = cast(ubyte[]) output_; | |
808 | 809 |
809 if (input.length < blockSize) | 810 if (input.length < BLOCK_SIZE) |
810 throw new ShortBufferError(name()~": Input buffer too short"); | 811 throw new ShortBufferError(name()~": Input buffer too short"); |
812 | |
813 if (output.length < BLOCK_SIZE) | |
814 throw new ShortBufferError(name()~": Output buffer too short"); | |
811 | 815 |
812 s0 = w[0] ^ Util.ubytesToUintBig(input, 0); | 816 s0 = w[0] ^ Util.ubytesToUintBig(input, 0); |
813 s1 = w[1] ^ Util.ubytesToUintBig(input, 4); | 817 s1 = w[1] ^ Util.ubytesToUintBig(input, 4); |
814 s2 = w[2] ^ Util.ubytesToUintBig(input, 8); | 818 s2 = w[2] ^ Util.ubytesToUintBig(input, 8); |
815 s3 = w[3] ^ Util.ubytesToUintBig(input, 12); | 819 s3 = w[3] ^ Util.ubytesToUintBig(input, 12); |
816 | 820 |
817 if (encrypt) encryptBlock(); else decryptBlock(); | 821 if (encrypt) encryptBlock(); else decryptBlock(); |
818 | 822 |
819 ubyte[] output = new ubyte[blockSize]; | |
820 Util.uintToUbytesBig(s0, output, 0); | 823 Util.uintToUbytesBig(s0, output, 0); |
821 Util.uintToUbytesBig(s1, output, 4); | 824 Util.uintToUbytesBig(s1, output, 4); |
822 Util.uintToUbytesBig(s2, output, 8); | 825 Util.uintToUbytesBig(s2, output, 8); |
823 Util.uintToUbytesBig(s3, output, 12); | 826 Util.uintToUbytesBig(s3, output, 12); |
824 | 827 |
825 return output; | 828 return BLOCK_SIZE; |
826 } | 829 } |
827 | 830 |
828 void reset() {} | 831 void reset() {} |
829 | 832 |
830 private uint subWord(uint x) { | 833 private uint subWord(uint x) { |
903 char[] result; | 906 char[] result; |
904 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); | 907 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); |
905 | 908 |
906 // Encryption | 909 // Encryption |
907 t.init(true, key); | 910 t.init(true, key); |
908 buffer = t.process(Util.hexToUbytes(test_plaintexts[i])); | 911 t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); |
909 result = Util.ubytesToHex(buffer); | 912 result = Util.ubytesToHex(buffer); |
910 assert(result == test_ciphertexts[i], | 913 assert(result == test_ciphertexts[i], |
911 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); | 914 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); |
912 | 915 |
913 // Decryption | 916 // Decryption |
914 t.init(false, key); | 917 t.init(false, key); |
915 buffer = t.process(Util.hexToUbytes(test_ciphertexts[i])); | 918 t.update(Util.hexToUbytes(test_ciphertexts[i]), buffer); |
916 result = Util.ubytesToHex(buffer); | 919 result = Util.ubytesToHex(buffer); |
917 assert(result == test_plaintexts[i], | 920 assert(result == test_plaintexts[i], |
918 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); | 921 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); |
919 } | 922 } |
920 } | 923 } |