Mixe for Privacy and Anonymity in the Internet
CASymCipherOFB.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 "CASymCipherOFB.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  }
42 #endif
43 
49 {
50  return setKey(key,true);
51 }
52 
58 SINT32 CASymCipherOFB::setKey(const UINT8* key,bool bEncrypt)
59 {
60  memset(m_iv1,0,16);
61  memset(m_iv2,0,16);
62 
63 #ifdef INTEL_IPP_CRYPTO
64  ippsRijndael128Init(key, IppsRijndaelKey128,m_keyAES1);
65  ippsRijndael128Init(key, IppsRijndaelKey128,m_keyAES2);
66 #else
67  if(bEncrypt)
68  {
69 #ifdef AES_NI
70  aesni_set_encrypt_key(key,128,m_keyAES1);
71  aesni_set_encrypt_key(key,128,m_keyAES2);
72 
73 #else
74  AES_set_encrypt_key(key,128,m_keyAES1);
75  AES_set_encrypt_key(key,128,m_keyAES2);
76 #endif
77  }
78  else
79  {
80 #ifdef AES_NI
81  aesni_set_decrypt_key(key,128,m_keyAES1);
82  aesni_set_decrypt_key(key,128,m_keyAES2);
83 #else
84  AES_set_decrypt_key(key,128,m_keyAES1);
85  AES_set_decrypt_key(key,128,m_keyAES2);
86 #endif
87  }
88 #endif
89  m_bKeySet=true;
90  return E_SUCCESS;
91 }
92 
94 {
95 #ifdef _DEBUG
96  CAMsg::printMsg(LOG_DEBUG, "CASymCipherOFB::setKeys() - keysize %u\n",keysize);
97 #endif
98  if(keysize==KEY_SIZE)
99  {
100  return setKey(key);
101  }
102  else if(keysize==2*KEY_SIZE)
103  {
104 #ifdef INTEL_IPP_CRYPTO
105  ippsRijndael128Init(key, IppsRijndaelKey128,m_keyAES1);
106  ippsRijndael128Init(key+KEY_SIZE, IppsRijndaelKey128,m_keyAES2);
107 #else
108 #ifdef AES_NI
109  aesni_set_encrypt_key(key,128,m_keyAES1);
110  aesni_set_encrypt_key(key+KEY_SIZE,128,m_keyAES2);
111 #else
112  AES_set_encrypt_key(key,128,m_keyAES1);
113  AES_set_encrypt_key(key+KEY_SIZE,128,m_keyAES2);
114 #endif
115 #endif
116  memset(m_iv1,0,16);
117  memset(m_iv2,0,16);
118  m_bKeySet=true;
119  return E_SUCCESS;
120  }
121  return E_UNKNOWN;
122 }
123 
137 {
138 #ifdef INTEL_IPP_CRYPTO
139  UINT32 k=len&0xFFFFFFF0;
140  ippsRijndael128EncryptOFB(in,out,k,16, m_keyAES1,m_iv1);
141 // if((len%16)!=0)
142 // {
143  ippsRijndael128EncryptOFB(in+k,out+k,len%16,len%16, m_keyAES1,m_iv1);
144 // }
145  return E_SUCCESS;
146 #endif
147  UINT32 i=0;
148  while(i+15<len)
149  {
150 #ifdef INTEL_IPP_CRYPTO
151  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES1, IppsCPPaddingNONE);
152 #else
153 #ifdef AES_NI
154 // int outlen = 16;
155 // EVP_EncryptUpdate(m_ctxAES1, m_iv1, &outlen, m_iv1, 16);
156  aesni_encrypt(m_iv1,m_iv1,m_keyAES1);
157 #else
158  AES_encrypt(m_iv1,m_iv1,m_keyAES1);
159 #endif
160 #endif
161  out[i]=in[i]^m_iv1[0];
162  i++;
163  out[i]=in[i]^m_iv1[1];
164  i++;
165  out[i]=in[i]^m_iv1[2];
166  i++;
167  out[i]=in[i]^m_iv1[3];
168  i++;
169  out[i]=in[i]^m_iv1[4];
170  i++;
171  out[i]=in[i]^m_iv1[5];
172  i++;
173  out[i]=in[i]^m_iv1[6];
174  i++;
175  out[i]=in[i]^m_iv1[7];
176  i++;
177  out[i]=in[i]^m_iv1[8];
178  i++;
179  out[i]=in[i]^m_iv1[9];
180  i++;
181  out[i]=in[i]^m_iv1[10];
182  i++;
183  out[i]=in[i]^m_iv1[11];
184  i++;
185  out[i]=in[i]^m_iv1[12];
186  i++;
187  out[i]=in[i]^m_iv1[13];
188  i++;
189  out[i]=in[i]^m_iv1[14];
190  i++;
191  out[i]=in[i]^m_iv1[15];
192  i++;
193  }
194  if(i<len) //In this case len-i<16 !
195  {
196 #ifdef INTEL_IPP_CRYPTO
197  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES1, IppsCPPaddingNONE);
198 #else
199 #ifdef AES_NI
200  //int outlen = 16;
201  //EVP_EncryptUpdate(m_ctxAES1, m_iv1, &outlen, m_iv1, 16);
202  aesni_encrypt(m_iv1,m_iv1,m_keyAES1);
203 
204 #else
205  AES_encrypt(m_iv1,m_iv1,m_keyAES1);
206 #endif
207 #endif
208  len-=i;
209  for(UINT32 k=0; k<len; k++)
210  {
211  out[i]=in[i]^m_iv1[k];
212  i++;
213  }
214  }
215  return E_SUCCESS;
216 }
217 
226 {
227 
228  UINT32 i=0;
229  while(i+15<len)
230  {
231 #ifdef INTEL_IPP_CRYPTO
232  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES2, IppsCPPaddingNONE);
233 #else
234 #ifdef AES_NI
235  aesni_encrypt(m_iv2,m_iv2,m_keyAES2);
236 #else
237  AES_encrypt(m_iv2,m_iv2,m_keyAES2);
238 #endif
239 #endif
240  out[i]=in[i]^m_iv2[0];
241  i++;
242  out[i]=in[i]^m_iv2[1];
243  i++;
244  out[i]=in[i]^m_iv2[2];
245  i++;
246  out[i]=in[i]^m_iv2[3];
247  i++;
248  out[i]=in[i]^m_iv2[4];
249  i++;
250  out[i]=in[i]^m_iv2[5];
251  i++;
252  out[i]=in[i]^m_iv2[6];
253  i++;
254  out[i]=in[i]^m_iv2[7];
255  i++;
256  out[i]=in[i]^m_iv2[8];
257  i++;
258  out[i]=in[i]^m_iv2[9];
259  i++;
260  out[i]=in[i]^m_iv2[10];
261  i++;
262  out[i]=in[i]^m_iv2[11];
263  i++;
264  out[i]=in[i]^m_iv2[12];
265  i++;
266  out[i]=in[i]^m_iv2[13];
267  i++;
268  out[i]=in[i]^m_iv2[14];
269  i++;
270  out[i]=in[i]^m_iv2[15];
271  i++;
272  }
273  if(i<len)
274  {
275 #ifdef INTEL_IPP_CRYPTO
276  ippsRijndael128EncryptECB(m_iv1,m_iv1,KEY_SIZE, m_keyAES2, IppsCPPaddingNONE);
277 #else
278 #ifdef AES_NI
279  aesni_encrypt(m_iv2,m_iv2,m_keyAES2);
280 #else
281  AES_encrypt(m_iv2,m_iv2,m_keyAES2);
282 #endif
283 #endif
284  len-=i;
285  for(UINT32 k=0; k<len; k++)
286  {
287  out[i]=in[i]^m_iv2[k];
288  i++;
289  }
290  }
291  return E_SUCCESS;
292 }
293 
#define KEY_SIZE
Definition: CASymCipher.hpp:31
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
SINT32 setKeys(const UINT8 *key, UINT32 keysize)
Sets the keys for crypt1() and crypt2() either to the same key (if keysize==KEY_SIZE) or to different...
SINT32 setKey(const UINT8 *key)
Sets the keys for crypt1() and crypt2() to the same key.
SINT32 crypt1(const UINT8 *in, UINT8 *out, UINT32 len)
Encryptes/Decrpytes in to out using iv1 and key1.
SINT32 crypt2(const UINT8 *in, UINT8 *out, UINT32 len)
Decryptes in to out using iv2 and key2.
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT16 len
Definition: typedefs.hpp:0