Mixe for Privacy and Anonymity in the Internet
CASymCipher.hpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team 
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification, 
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice, 
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice, 
00011     this list of conditions and the following disclaimer in the documentation and/or 
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 
00015     may be used to endorse or promote products derived from this software without specific 
00016     prior written permission. 
00017 
00018   
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 #ifndef __CASYMCIPHER__
00029 #define __CASYMCIPHER__
00030 
00031 #define KEY_SIZE 16
00032 
00033 #include "CALockAble.hpp"
00034 
00042 class CASymCipher
00043 #ifndef ONLY_LOCAL_PROXY  
00044   :public CALockAble
00045 #endif
00046   {
00047     public:
00048       CASymCipher()
00049         {
00050           m_bKeySet=false;
00051 #ifdef INTEL_IPP_CRYPTO
00052           int size=0;
00053           ippsRijndael128GetSize(&size);
00054           m_keyAES1=(IppsRijndael128Spec*)new UINT8[size];
00055           m_keyAES2=(IppsRijndael128Spec*)new UINT8[size];
00056 #else
00057           m_keyAES1=new AES_KEY;
00058           m_keyAES2=new AES_KEY;
00059 #endif
00060           m_iv1=new UINT8[16];
00061           m_iv2=new UINT8[16];
00062 
00063           m_nEncMsgCounter = 0;
00064           m_pEncMsgIV = new UINT32[3];
00065           memset(m_pEncMsgIV, 0, 12);
00066           m_nDecMsgCounter = 0;
00067           m_pDecMsgIV = new UINT32[3];
00068           memset(m_pDecMsgIV, 0, 12);
00069 
00070           m_pGCMCtxEnc = NULL;
00071           m_pGCMCtxDec = NULL;
00072 
00073           m_pcsEnc = new CAMutex();
00074           m_pcsDec = new CAMutex();
00075         }
00076 
00077       ~CASymCipher()
00078         {
00079 #ifndef ONLY_LOCAL_PROXY
00080           waitForDestroy();
00081 #endif
00082 #ifdef INTEL_IPP_CRYPTO
00083           delete[] (UINT8*)m_keyAES1;
00084           delete[] (UINT8*)m_keyAES2;
00085 #else
00086           delete m_keyAES1;
00087           delete m_keyAES2;
00088 #endif
00089           m_keyAES1 = NULL;
00090           m_keyAES2 = NULL;
00091           delete[] m_iv1;
00092           m_iv1 = NULL;
00093           delete[] m_iv2;
00094           m_iv2 = NULL;
00095 
00096           delete [] m_pEncMsgIV;
00097           m_pEncMsgIV = NULL;
00098           delete [] m_pDecMsgIV;
00099           m_pDecMsgIV = NULL;
00100 
00101 #ifndef USE_OPENSSL_GCM
00102           delete m_pGCMCtxEnc;
00103           delete m_pGCMCtxDec;
00104 #else
00105           CRYPTO_gcm128_release(m_pGCMCtxEnc);
00106           CRYPTO_gcm128_release(m_pGCMCtxDec);
00107 #endif
00108 
00109           m_pGCMCtxEnc = NULL;
00110           m_pGCMCtxDec = NULL;
00111 
00112           delete m_pcsEnc;
00113           m_pcsEnc = NULL;
00114           delete m_pcsDec;
00115           m_pcsDec = NULL;
00116         }
00117       bool isKeyValid()
00118         {
00119           return m_bKeySet;
00120         }
00121 
00123       SINT32 setKey(const UINT8* key);  
00124       
00127       SINT32 setKeys(const UINT8* key,UINT32 keysize);  
00128       
00129       SINT32 setKey(const UINT8* key,bool bEncrypt);  
00130 
00135       SINT32 setIVs(const UINT8* p_iv)
00136         {
00137           memcpy(m_iv1,p_iv,16);
00138           memcpy(m_iv2,p_iv,16);
00139           return E_SUCCESS;
00140         }
00141 
00146       SINT32 setIV2(const UINT8* p_iv)
00147         {
00148           memcpy(m_iv2,p_iv,16);
00149           return E_SUCCESS;
00150         }
00151 
00152       SINT32 crypt1(const UINT8* in,UINT8* out,UINT32 len);
00153       SINT32 crypt2(const UINT8* in,UINT8* out,UINT32 len);
00154       SINT32 decrypt1CBCwithPKCS7(const UINT8* in,UINT8* out,UINT32* len);
00155       SINT32 encrypt1CBCwithPKCS7(const UINT8* in,UINT32 inlen,UINT8* out,UINT32* len);
00156 
00157       void setGCMKeys(UINT8* keyRecv, UINT8* keySend);
00158       SINT32 encryptMessage(const UINT8* in, UINT32 inlen, UINT8* out);
00159       SINT32 decryptMessage(const UINT8* in, UINT32 inlen, UINT8* out, bool integrityCheck);
00160 
00161       static SINT32 testSpeed();
00162 
00163     private:
00164       CAMutex* m_pcsEnc;
00165       CAMutex* m_pcsDec;
00166 #ifndef USE_OPENSSL_GCM
00167       gcm_ctx_64k* m_pGCMCtxEnc;
00168       gcm_ctx_64k* m_pGCMCtxDec;
00169 #else
00170       GCM128_CONTEXT* m_pGCMCtxEnc;
00171       GCM128_CONTEXT* m_pGCMCtxDec;
00172 #endif
00173       UINT32 m_nEncMsgCounter;
00174       UINT32* m_pEncMsgIV;
00175       UINT32 m_nDecMsgCounter;
00176       UINT32* m_pDecMsgIV;
00177 
00178     protected:
00179 
00180 #ifdef INTEL_IPP_CRYPTO
00181       IppsRijndael128Spec* m_keyAES1;
00182       IppsRijndael128Spec* m_keyAES2;
00183 #else
00184       AES_KEY* m_keyAES1;
00185       AES_KEY* m_keyAES2;
00186 #endif
00187 
00188       UINT8* m_iv1;
00189       UINT8* m_iv2;
00190       bool m_bKeySet;
00191   };
00192 
00193 #endif