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           delete m_pGCMCtxEnc;
00102           m_pGCMCtxEnc = NULL;
00103 
00104           delete m_pGCMCtxDec;
00105           m_pGCMCtxDec = NULL;
00106 
00107           delete m_pcsEnc;
00108           m_pcsEnc = NULL;
00109           delete m_pcsDec;
00110           m_pcsDec = NULL;
00111         }
00112       bool isKeyValid()
00113         {
00114           return m_bKeySet;
00115         }
00116 
00118       SINT32 setKey(const UINT8* key);  
00119       
00122       SINT32 setKeys(const UINT8* key,UINT32 keysize);  
00123       
00124       SINT32 setKey(const UINT8* key,bool bEncrypt);  
00125 
00130       SINT32 setIVs(const UINT8* p_iv)
00131         {
00132           memcpy(m_iv1,p_iv,16);
00133           memcpy(m_iv2,p_iv,16);
00134           return E_SUCCESS;
00135         }
00136 
00141       SINT32 setIV2(const UINT8* p_iv)
00142         {
00143           memcpy(m_iv2,p_iv,16);
00144           return E_SUCCESS;
00145         }
00146 
00147       SINT32 crypt1(const UINT8* in,UINT8* out,UINT32 len);
00148       SINT32 crypt2(const UINT8* in,UINT8* out,UINT32 len);
00149       SINT32 decrypt1CBCwithPKCS7(const UINT8* in,UINT8* out,UINT32* len);
00150       SINT32 encrypt1CBCwithPKCS7(const UINT8* in,UINT32 inlen,UINT8* out,UINT32* len);
00151 
00152       void setGCMKeys(UINT8* keyRecv, UINT8* keySend);
00153       SINT32 encryptMessage(const UINT8* in, UINT32 inlen, UINT8* out);
00154       SINT32 decryptMessage(const UINT8* in, UINT32 inlen, UINT8* out, bool integrityCheck);
00155 
00156       static SINT32 testSpeed();
00157 
00158     private:
00159       CAMutex* m_pcsEnc;
00160       CAMutex* m_pcsDec;
00161       gcm_ctx_64k* m_pGCMCtxEnc;
00162       gcm_ctx_64k* m_pGCMCtxDec;
00163       UINT32 m_nEncMsgCounter;
00164       UINT32* m_pEncMsgIV;
00165       UINT32 m_nDecMsgCounter;
00166       UINT32* m_pDecMsgIV;
00167 
00168     protected:
00169 
00170 #ifdef INTEL_IPP_CRYPTO
00171       IppsRijndael128Spec* m_keyAES1;
00172       IppsRijndael128Spec* m_keyAES2;
00173 #else
00174       AES_KEY* m_keyAES1;
00175       AES_KEY* m_keyAES2;
00176 #endif
00177 
00178       UINT8* m_iv1;
00179       UINT8* m_iv2;
00180       bool m_bKeySet;
00181   };
00182 
00183 #endif