00001 /* des.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 /* des.hpp defines DES, DES_EDE2, and DES_EDE3 00027 see FIPS 46-2 and FIPS 81 00028 */ 00029 00030 00031 #ifndef TAO_CRYPT_DES_HPP 00032 #define TAO_CRYPT_DES_HPP 00033 00034 #include "misc.hpp" 00035 #include "modes.hpp" 00036 00037 namespace TaoCrypt { 00038 00039 00040 enum { DES_BLOCK_SIZE = 8, DES_KEY_SIZE = 32 }; 00041 00042 00043 class BasicDES { 00044 public: 00045 void SetKey(const byte*, word32, CipherDir dir); 00046 void RawProcessBlock(word32&, word32&) const; 00047 protected: 00048 word32 k_[DES_KEY_SIZE]; 00049 }; 00050 00051 00052 // DES 00053 class DES : public Mode_BASE, public BasicDES { 00054 public: 00055 DES(CipherDir DIR, Mode MODE) 00056 : Mode_BASE(DES_BLOCK_SIZE), dir_(DIR), mode_(MODE) {} 00057 00058 void Process(byte*, const byte*, word32); 00059 private: 00060 CipherDir dir_; 00061 Mode mode_; 00062 00063 void ProcessAndXorBlock(const byte*, const byte*, byte*) const; 00064 00065 DES(const DES&); // hide copy 00066 DES& operator=(const DES&); // and assign 00067 }; 00068 00069 00070 // DES_EDE2 00071 class DES_EDE2 : public Mode_BASE { 00072 public: 00073 DES_EDE2(CipherDir DIR, Mode MODE) 00074 : Mode_BASE(DES_BLOCK_SIZE), dir_(DIR), mode_(MODE) {} 00075 00076 void SetKey(const byte*, word32, CipherDir dir); 00077 void Process(byte*, const byte*, word32); 00078 private: 00079 CipherDir dir_; 00080 Mode mode_; 00081 00082 BasicDES des1_; 00083 BasicDES des2_; 00084 00085 void ProcessAndXorBlock(const byte*, const byte*, byte*) const; 00086 00087 DES_EDE2(const DES_EDE2&); // hide copy 00088 DES_EDE2& operator=(const DES_EDE2&); // and assign 00089 }; 00090 00091 00092 00093 // DES_EDE3 00094 class DES_EDE3 : public Mode_BASE { 00095 public: 00096 DES_EDE3(CipherDir DIR, Mode MODE) 00097 : Mode_BASE(DES_BLOCK_SIZE), dir_(DIR), mode_(MODE) {} 00098 00099 void SetKey(const byte*, word32, CipherDir dir); 00100 void SetIV(const byte* iv) { memcpy(r_, iv, DES_BLOCK_SIZE); } 00101 void Process(byte*, const byte*, word32); 00102 private: 00103 CipherDir dir_; 00104 Mode mode_; 00105 00106 BasicDES des1_; 00107 BasicDES des2_; 00108 BasicDES des3_; 00109 00110 void AsmProcess(const byte* in, byte* out, void* box) const; 00111 void ProcessAndXorBlock(const byte*, const byte*, byte*) const; 00112 00113 DES_EDE3(const DES_EDE3&); // hide copy 00114 DES_EDE3& operator=(const DES_EDE3&); // and assign 00115 }; 00116 00117 00118 typedef BlockCipher<ENCRYPTION, DES, ECB> DES_ECB_Encryption; 00119 typedef BlockCipher<DECRYPTION, DES, ECB> DES_ECB_Decryption; 00120 00121 typedef BlockCipher<ENCRYPTION, DES, CBC> DES_CBC_Encryption; 00122 typedef BlockCipher<DECRYPTION, DES, CBC> DES_CBC_Decryption; 00123 00124 typedef BlockCipher<ENCRYPTION, DES_EDE2, ECB> DES_EDE2_ECB_Encryption; 00125 typedef BlockCipher<DECRYPTION, DES_EDE2, ECB> DES_EDE2_ECB_Decryption; 00126 00127 typedef BlockCipher<ENCRYPTION, DES_EDE2, CBC> DES_EDE2_CBC_Encryption; 00128 typedef BlockCipher<DECRYPTION, DES_EDE2, CBC> DES_EDE2_CBC_Decryption; 00129 00130 typedef BlockCipher<ENCRYPTION, DES_EDE3, ECB> DES_EDE3_ECB_Encryption; 00131 typedef BlockCipher<DECRYPTION, DES_EDE3, ECB> DES_EDE3_ECB_Decryption; 00132 00133 typedef BlockCipher<ENCRYPTION, DES_EDE3, CBC> DES_EDE3_CBC_Encryption; 00134 typedef BlockCipher<DECRYPTION, DES_EDE3, CBC> DES_EDE3_CBC_Decryption; 00135 00136 00137 } // namespace 00138 00139 00140 #endif // TAO_CRYPT_DES_HPP
1.4.7

