Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/AES.d @ 8:23c62e28b3a4
Reworked symmetric cipher classes to have SymmetricCipher as their superclass, and follow the general interface of init(), process(), etc. Made sure everything still passed test vectors. Removed Cipher class. I'll worry about that shit when we support something other than symmetric ciphers.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Mon, 18 Aug 2008 01:14:37 -0400 |
parents | 23e6e80f8ee3 |
children | 8c7f8fecdd75 |
comparison
equal
deleted
inserted
replaced
7:23e6e80f8ee3 | 8:23c62e28b3a4 |
---|---|
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 uint processBlock(void[] input_, uint inOff, void[] output_, uint outOff) { | 803 ubyte[] process(void[] input_) { |
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 ubyte[] output = cast(ubyte[]) output_; | |
809 | 808 |
810 if ((inOff + BLOCK_SIZE) > input.length) | 809 if (input.length < blockSize) |
811 throw new ShortBufferError(name()~": Input buffer too short"); | 810 throw new ShortBufferError(name()~": Input buffer too short"); |
812 | 811 |
813 if ((outOff + BLOCK_SIZE) > output.length) | 812 s0 = w[0] ^ Util.ubytesToUintBig(input, 0); |
814 throw new ShortBufferError(name()~": Output buffer too short"); | 813 s1 = w[1] ^ Util.ubytesToUintBig(input, 4); |
815 | 814 s2 = w[2] ^ Util.ubytesToUintBig(input, 8); |
816 s0 = w[0] ^ Util.ubytesToUintBig(input, inOff); | 815 s3 = w[3] ^ Util.ubytesToUintBig(input, 12); |
817 s1 = w[1] ^ Util.ubytesToUintBig(input, inOff+4); | |
818 s2 = w[2] ^ Util.ubytesToUintBig(input, inOff+8); | |
819 s3 = w[3] ^ Util.ubytesToUintBig(input, inOff+12); | |
820 | 816 |
821 if (encrypt) encryptBlock(); else decryptBlock(); | 817 if (encrypt) encryptBlock(); else decryptBlock(); |
822 | 818 |
823 Util.uintToUbytesBig(s0, output, outOff); | 819 ubyte[] output = new ubyte[blockSize]; |
824 Util.uintToUbytesBig(s1, output, outOff+4); | 820 Util.uintToUbytesBig(s0, output, 0); |
825 Util.uintToUbytesBig(s2, output, outOff+8); | 821 Util.uintToUbytesBig(s1, output, 4); |
826 Util.uintToUbytesBig(s3, output, outOff+12); | 822 Util.uintToUbytesBig(s2, output, 8); |
827 | 823 Util.uintToUbytesBig(s3, output, 12); |
828 return BLOCK_SIZE; | 824 |
825 return output; | |
829 } | 826 } |
830 | 827 |
831 void reset() {} | 828 void reset() {} |
832 | 829 |
833 private uint subWord(uint x) { | 830 private uint subWord(uint x) { |
906 char[] result; | 903 char[] result; |
907 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); | 904 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); |
908 | 905 |
909 // Encryption | 906 // Encryption |
910 t.init(true, key); | 907 t.init(true, key); |
911 t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0); | 908 buffer = t.process(Util.hexToUbytes(test_plaintexts[i])); |
912 result = Util.ubytesToHex(buffer); | 909 result = Util.ubytesToHex(buffer); |
913 assert(result == test_ciphertexts[i], | 910 assert(result == test_ciphertexts[i], |
914 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); | 911 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); |
915 | 912 |
916 // Decryption | 913 // Decryption |
917 t.init(false, key); | 914 t.init(false, key); |
918 t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0); | 915 buffer = t.process(Util.hexToUbytes(test_ciphertexts[i])); |
919 result = Util.ubytesToHex(buffer); | 916 result = Util.ubytesToHex(buffer); |
920 assert(result == test_plaintexts[i], | 917 assert(result == test_plaintexts[i], |
921 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); | 918 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); |
922 } | 919 } |
923 } | 920 } |