00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : DESCrypter 00006 * Package : Zeus.ZeusBase.Security 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 00033 /*****************************************************************************/ 00034 /* CODE INFORMATION: 00035 The main code of this class has been taken from the "NTLM authenticaion 00036 Library 0.3.10" published by Grant Edwards. 00037 It can be downloaded at: 00038 http://linux.wareseeker.com/download/ntlm-authentication-library-0.3.10.rar/330784 00039 00040 Copyright information from des.h: 00041 00042 des.h --- DES cipher implementation. 00043 Copyright (C) 2005 Free Software Foundation, Inc. 00044 00045 This file is free software; you can redistribute it and/or modify 00046 it under the terms of the GNU Lesser General Public License as published 00047 by the Free Software Foundation; either version 2.1, or (at your 00048 option) any later version. 00049 00050 This file is distributed in the hope that it will be useful, but 00051 WITHOUT ANY WARRANTY; without even the implied warranty of 00052 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00053 General Public License for more details. 00054 00055 You should have received a copy of the GNU Lesser General Public License 00056 along with this file; if not, write to the Free Software 00057 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00058 02110-1301, USA. 00059 00060 */ 00061 /*****************************************************************************/ 00062 //--------------------------------------------------------------------------- 00063 00064 //--------------------------------------------------------------------------- 00065 /* Adapted for gnulib by Simon Josefsson, based on Libgcrypt. 00066 Adapted for ZeusBase by Benjamin Hadorn. 00067 */ 00068 //--------------------------------------------------------------------------- 00069 00070 #ifndef DESCrypterH 00071 #define DESCrypterH 00072 //--------------------------------------------------------------------------- 00073 00074 #include <zeusbase/Security/AbstractCrypter.h> 00075 00076 /* 00077 * Handy macros for encryption and decryption of data 00078 */ 00079 #define encryptWithDES(ctx, from, to) cryptWithDES(ctx, from, to, 0) 00080 #define decryptWithDES(ctx, from, to) cryptWithDES(ctx, from, to, 1) 00081 #define encryptWithTripleDES(ctx, from, to) cryptWithTripleDES(ctx,from,to,0) 00082 #define decryptWithTripleDES(ctx, from, to) cryptWithTripleDES(ctx,from,to,1) 00083 00084 BEGIN_NAMESPACE_Zeus 00085 00086 /***************************************************************************/ 00090 /***************************************************************************/ 00091 zeusbase_class TDESCrypter : public TAbstractCrypter 00092 { 00093 public: 00094 /**********************************************************************/ 00097 /**********************************************************************/ 00098 enum EDESMode 00099 { 00100 etDES = 0, 00101 etTripleDES = 1 00102 }; 00103 00104 /**********************************************************************/ 00107 /**********************************************************************/ 00108 struct TypeDES_CTX 00109 { 00110 Uint32 aulEncryptSubkeys[32]; 00111 Uint32 aulDecryptSubkeys[32]; 00112 }; 00113 00114 /**********************************************************************/ 00117 /**********************************************************************/ 00118 struct TypeTripleDES_CTX 00119 { 00120 Uint32 aulEncryptSubkeys[96]; 00121 Uint32 aulDecryptSubkeys[96]; 00122 }; 00123 00124 TDESCrypter(EDESMode eMode); 00125 00126 //abstract methods 00127 virtual Retval encode(const IByteArray& rInput, IByteArray& rOutput); 00128 virtual Retval decode(const IByteArray& rInput, IByteArray& rOutput); 00129 virtual Int getBlockSizeForDecoding() const; 00130 virtual Int getBlockSizeForEncoding() const; 00131 virtual void setKey(Uint64 uldKey); 00132 virtual void setKey(const IByteArray& rKey); 00133 00134 //######################################################################## 00135 // METHODS FOR DIRECT USAGE OF DES AND TRIPLEDES 00136 static bool isWeakKey(const char* key); 00137 static void scheduleDESKey(const char* _rawkey, Uint32* subkey); 00138 00139 //DES methods 00140 static void setDESKey(TypeDES_CTX& rCtx, const char* key); 00141 static bool makeDESKey(TypeDES_CTX& rCtx, const char* key, size_t keylen); 00142 static void cryptWithDES(TypeDES_CTX& rCtx, const char* _from, char* _to, int iMode); 00143 00144 //TripleDES methods 00145 static void setTripleDESKeys(TypeTripleDES_CTX& rCtx, const char* key1, const char* key2); 00146 static void setTripleDESKeys(TypeTripleDES_CTX& rCtx, const char* key1, const char* key2, const char* key3); 00147 static bool makeTripleDESKey(TypeTripleDES_CTX& rCtx, const char* key, size_t keylen); 00148 static void cryptWithTripleDES(TypeTripleDES_CTX& rCtx, const char* _from, char* _to, int iMode); 00149 00150 protected: 00151 inline virtual ~TDESCrypter() {} 00152 00153 private: 00154 00156 EDESMode m_eMode; 00158 TypeDES_CTX m_DESKey; 00160 TypeTripleDES_CTX m_TripleDESKey; 00162 bool m_bKeySet; 00163 }; 00164 00165 /***************************************************************************/ 00168 /***************************************************************************/ 00169 inline Int TDESCrypter::getBlockSizeForDecoding() const 00170 { 00171 return 1; 00172 } 00173 00174 /***************************************************************************/ 00177 /***************************************************************************/ 00178 inline Int TDESCrypter::getBlockSizeForEncoding() const 00179 { 00180 return 1; 00181 } 00182 00183 END_NAMESPACE_Zeus 00184 00185 #endif