00001 /* twofish.hpp 00002 * 00003 * Copyright (C) 2003 Sawtooth Consulting Ltd. 00004 * 00005 * This file is part of yaSSL. 00006 * 00007 * yaSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * There are special exceptions to the terms and conditions of the GPL as it 00013 * is applied to yaSSL. View the full text of the exception in the file 00014 * FLOSS-EXCEPTIONS in the directory of this software distribution. 00015 * 00016 * yaSSL is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00024 */ 00025 00026 /* twofish.hpp defines Twofish 00027 */ 00028 00029 00030 #ifndef TAO_CRYPT_TWOFISH_HPP 00031 #define TAO_CRYPT_TWOFISH_HPP 00032 00033 #include "misc.hpp" 00034 #include "modes.hpp" 00035 #include "algorithm.hpp" 00036 00037 namespace TaoCrypt { 00038 00039 enum { TWOFISH_BLOCK_SIZE = 16 }; 00040 00041 00042 // Twofish encryption and decryption, see 00043 class Twofish : public Mode_BASE { 00044 public: 00045 enum { BLOCK_SIZE = TWOFISH_BLOCK_SIZE }; 00046 00047 Twofish(CipherDir DIR, Mode MODE) 00048 : Mode_BASE(BLOCK_SIZE), dir_(DIR), mode_(MODE) {} 00049 00050 void Process(byte*, const byte*, word32); 00051 void SetKey(const byte* key, word32 sz, CipherDir fake = ENCRYPTION); 00052 void SetIV(const byte* iv) { memcpy(r_, iv, BLOCK_SIZE); } 00053 private: 00054 CipherDir dir_; 00055 Mode mode_; 00056 00057 static const byte q_[2][256]; 00058 static const word32 mds_[4][256]; 00059 00060 word32 k_[40]; 00061 word32 s_[4][256]; 00062 00063 static word32 h0(word32 x, const word32 *key, unsigned int kLen); 00064 static word32 h(word32 x, const word32 *key, unsigned int kLen); 00065 00066 void ProcessAndXorBlock(const byte*, const byte*, byte*) const; 00067 00068 void encrypt(const byte*, const byte*, byte*) const; 00069 void decrypt(const byte*, const byte*, byte*) const; 00070 00071 void AsmEncrypt(const byte* inBlock, byte* outBlock) const; 00072 void AsmDecrypt(const byte* inBlock, byte* outBlock) const; 00073 00074 Twofish(const Twofish&); // hide copy 00075 Twofish& operator=(const Twofish&); // and assign 00076 }; 00077 00078 00079 typedef BlockCipher<ENCRYPTION, Twofish, ECB> Twofish_ECB_Encryption; 00080 typedef BlockCipher<DECRYPTION, Twofish, ECB> Twofish_ECB_Decryption; 00081 00082 typedef BlockCipher<ENCRYPTION, Twofish, CBC> Twofish_CBC_Encryption; 00083 typedef BlockCipher<DECRYPTION, Twofish, CBC> Twofish_CBC_Decryption; 00084 00085 00086 00087 } // naemspace 00088 00089 #endif // TAO_CRYPT_TWOFISH_HPP 00090
1.4.7

