Mixe for Privacy and Anonymity in the Internet
CASymCipher.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #include "StdAfx.h"
29 #include "CASymCipher.hpp"
30 #include "CAMsg.hpp"
31 //AES
32 
33 
34 #ifdef AES_NI
35 extern "C"
36  {
37  int aesni_set_encrypt_key(const unsigned char *userKey, int bits,AES_KEY *key);
38  int aesni_set_decrypt_key(const unsigned char *userKey, int bits,AES_KEY *key);
39  void aesni_encrypt(const unsigned char *in, unsigned char *out,
40  const AES_KEY *key);
41  void aesni_cbc_encrypt(const unsigned char *in,
42  unsigned char *out,
43  size_t length,
44  const AES_KEY *key, unsigned char *ivec, int enc);
45  }
46 #endif
47 
53 {
54  return setKey(key,true);
55 }
56 
62 SINT32 CASymCipher::setKey(const UINT8* key,bool bEncrypt)
63 {
64  memset(m_iv1,0,16);
65 #ifdef INTEL_IPP_CRYPTO
66  ippsRijndael128Init(key, IppsRijndaelKey128,m_keyAES1);
67 #else
68  if(bEncrypt)
69  {
70 #ifdef AES_NI
71  aesni_set_encrypt_key(key,128,m_keyAES1);
72 
73 #else
74  AES_set_encrypt_key(key,128,m_keyAES1);
75 #endif
76  }
77  else
78  {
79 #ifdef AES_NI
80  aesni_set_decrypt_key(key,128,m_keyAES1);
81 #else
82  AES_set_decrypt_key(key,128,m_keyAES1);
83 #endif
84  }
85 #endif
86  m_bKeySet=true;
87  return E_SUCCESS;
88 }
89 
90 /*
91 SINT32 CASymCipher::setKeys(const UINT8* key,UINT32 keysize)
92 {
93  if(keysize==KEY_SIZE)
94  {
95  return setKey(key);
96  }
97  else if(keysize==2*KEY_SIZE)
98  {
99 #ifdef INTEL_IPP_CRYPTO
100  ippsRijndael128Init(key, IppsRijndaelKey128,m_keyAES1);
101  ippsRijndael128Init(key+KEY_SIZE, IppsRijndaelKey128,m_keyAES2);
102 #else
103 #ifdef AES_NI
104  aesni_set_encrypt_key(key,128,m_keyAES1);
105  aesni_set_encrypt_key(key+KEY_SIZE,128,m_keyAES2);
106 #else
107  AES_set_encrypt_key(key,128,m_keyAES1);
108 #endif
109 #endif
110  memset(m_iv1,0,16);
111 #ifdef SYM_CIPHER_CTR
112  EVP_DecryptInit_ex(m_ctxAES1,EVP_aes_128_ctr(), NULL, key, m_iv1);
113  EVP_EncryptInit_ex(m_ctxAES2, EVP_aes_128_ctr(), NULL, key+KEY_SIZE, m_iv2);
114  memcpy(key1, key, 16);
115  memcpy(key2, key + KEY_SIZE, 16);
116 #endif
117  m_bKeySet=true;
118  return E_SUCCESS;
119  }
120  return E_UNKNOWN;
121 }
122 */
123 
124 
135 {
136  if(in==NULL||out==NULL||len==NULL||*len==0)
137  return E_UNKNOWN;
138 #ifdef INTEL_IPP_CRYPTO
139 #else
140 #ifndef AES_NI
141  AES_cbc_encrypt(in,out,*len,m_keyAES1,m_iv1,AES_DECRYPT);
142 #else
143  aesni_cbc_encrypt(in,out,*len,m_keyAES1,m_iv1,AES_DECRYPT);
144 #endif
145  //Now remove padding
146  UINT32 pad=out[*len-1];
147  if(pad>16||pad>*len)
148  return E_UNKNOWN;
149  for(UINT32 i=*len-pad; i<*len-1; i++)
150  if(out[i]!=pad)
151  return E_UNKNOWN;
152  *len-=pad;
153 #endif
154  return E_SUCCESS;
155 }
156 
167 {
168 #ifdef INTEL_IPP_CRYPTO
169 #else
170  UINT32 padlen=16-(inlen%16);
171  if(inlen+padlen>(*len))
172  {
173  return E_SPACE;
174  }
175  UINT8* tmp=new UINT8[inlen+padlen];
176  memcpy(tmp,in,inlen);
177  for(UINT32 i=inlen; i<inlen+padlen; i++)
178  {
179  tmp[i]=(UINT8)padlen;
180  }
181 #ifndef AES_NI
182  AES_cbc_encrypt(tmp,out,inlen+padlen,m_keyAES1,m_iv1,AES_ENCRYPT);
183 #else
184  aesni_cbc_encrypt(tmp,out,inlen+padlen,m_keyAES1,m_iv1,AES_ENCRYPT);
185 #endif
186  delete[] tmp;
187  tmp = NULL;
188  *len=inlen+padlen;
189 #endif
190  return E_SUCCESS;
191 }
192 
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
UINT8 * m_iv1
AES_KEY * m_keyAES1
SINT32 encryptCBCwithPKCS7(const UINT8 *in, UINT32 inlen, UINT8 *out, UINT32 *len)
En-/Decryptes in to out using IV1 and key1.
SINT32 setKey(const UINT8 *key)
Sets the key for encryption.
Definition: CASymCipher.cpp:52
SINT32 decryptCBCwithPKCS7(const UINT8 *in, UINT8 *out, UINT32 *len)
En-/Decryptes in to out using iv1 and key1.
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_SPACE
Definition: errorcodes.hpp:7
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT16 len
Definition: typedefs.hpp:0