POK
|
00001 /* 00002 * POK header 00003 * 00004 * The following file is a part of the POK project. Any modification should 00005 * made according to the POK licence. You CANNOT use this file or a part of 00006 * this file is this part of a file for your own project 00007 * 00008 * For more information on the POK licence, please see our LICENCE FILE 00009 * 00010 * Please follow the coding guidelines described in doc/CODING_GUIDELINES 00011 * 00012 * Copyright (c) 2007-2009 POK team 00013 * 00014 * Created by julien on Tue Dec 8 15:53:28 2009 00015 */ 00016 00017 /* crypto/des/ncbc_enc.c */ 00018 /* 00019 * #included by: 00020 * cbc_enc.c (DES_cbc_encrypt) 00021 * des_enc.c (DES_ncbc_encrypt) 00022 */ 00023 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 00024 * All rights reserved. 00025 * 00026 * This package is an SSL implementation written 00027 * by Eric Young (eay@cryptsoft.com). 00028 * The implementation was written so as to conform with Netscapes SSL. 00029 * 00030 * This library is free for commercial and non-commercial use as long as 00031 * the following conditions are aheared to. The following conditions 00032 * apply to all code found in this distribution, be it the RC4, RSA, 00033 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 00034 * included with this distribution is covered by the same copyright terms 00035 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 00036 * 00037 * Copyright remains Eric Young's, and as such any Copyright notices in 00038 * the code are not to be removed. 00039 * If this package is used in a product, Eric Young should be given attribution 00040 * as the author of the parts of the library used. 00041 * This can be in the form of a textual message at program startup or 00042 * in documentation (online or textual) provided with the package. 00043 * 00044 * Redistribution and use in source and binary forms, with or without 00045 * modification, are permitted provided that the following conditions 00046 * are met: 00047 * 1. Redistributions of source code must retain the copyright 00048 * notice, this list of conditions and the following disclaimer. 00049 * 2. Redistributions in binary form must reproduce the above copyright 00050 * notice, this list of conditions and the following disclaimer in the 00051 * documentation and/or other materials provided with the distribution. 00052 * 3. All advertising materials mentioning features or use of this software 00053 * must display the following acknowledgement: 00054 * "This product includes cryptographic software written by 00055 * Eric Young (eay@cryptsoft.com)" 00056 * The word 'cryptographic' can be left out if the rouines from the library 00057 * being used are not cryptographic related :-). 00058 * 4. If you include any Windows specific code (or a derivative thereof) from 00059 * the apps directory (application code) you must include an acknowledgement: 00060 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 00061 * 00062 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 00063 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00064 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00065 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00066 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00067 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00068 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00069 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00070 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00071 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00072 * SUCH DAMAGE. 00073 * 00074 * The licence and distribution terms for any publically available version or 00075 * derivative of this code cannot be changed. i.e. this code cannot simply be 00076 * copied and put under another distribution licence 00077 * [including the GNU Public Licence.] 00078 */ 00079 00080 #ifdef POK_NEEDS_PROTOCOLS_DES 00081 00082 #include "des_locl.h" 00083 00084 #ifdef CBC_ENC_C__DONT_UPDATE_IV 00085 void DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 00086 DES_key_schedule *_schedule, DES_cblock *ivec, int enc) 00087 #else 00088 void DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length, 00089 DES_key_schedule *_schedule, DES_cblock *ivec, int enc) 00090 #endif 00091 { 00092 register DES_LONG tin0,tin1; 00093 register DES_LONG tout0,tout1,xor0,xor1; 00094 register long l=length; 00095 DES_LONG tin[2]; 00096 unsigned char *iv; 00097 00098 iv = &(*ivec)[0]; 00099 00100 if (enc) 00101 { 00102 c2l(iv,tout0); 00103 c2l(iv,tout1); 00104 for (l-=8; l>=0; l-=8) 00105 { 00106 c2l(in,tin0); 00107 c2l(in,tin1); 00108 tin0^=tout0; tin[0]=tin0; 00109 tin1^=tout1; tin[1]=tin1; 00110 DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT); 00111 tout0=tin[0]; l2c(tout0,out); 00112 tout1=tin[1]; l2c(tout1,out); 00113 } 00114 if (l != -8) 00115 { 00116 c2ln(in,tin0,tin1,l+8); 00117 tin0^=tout0; tin[0]=tin0; 00118 tin1^=tout1; tin[1]=tin1; 00119 DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT); 00120 tout0=tin[0]; l2c(tout0,out); 00121 tout1=tin[1]; l2c(tout1,out); 00122 } 00123 #ifndef CBC_ENC_C__DONT_UPDATE_IV 00124 iv = &(*ivec)[0]; 00125 l2c(tout0,iv); 00126 l2c(tout1,iv); 00127 #endif 00128 } 00129 else 00130 { 00131 c2l(iv,xor0); 00132 c2l(iv,xor1); 00133 for (l-=8; l>=0; l-=8) 00134 { 00135 c2l(in,tin0); tin[0]=tin0; 00136 c2l(in,tin1); tin[1]=tin1; 00137 DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT); 00138 tout0=tin[0]^xor0; 00139 tout1=tin[1]^xor1; 00140 l2c(tout0,out); 00141 l2c(tout1,out); 00142 xor0=tin0; 00143 xor1=tin1; 00144 } 00145 if (l != -8) 00146 { 00147 c2l(in,tin0); tin[0]=tin0; 00148 c2l(in,tin1); tin[1]=tin1; 00149 DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT); 00150 tout0=tin[0]^xor0; 00151 tout1=tin[1]^xor1; 00152 l2cn(tout0,tout1,out,l+8); 00153 #ifndef CBC_ENC_C__DONT_UPDATE_IV 00154 xor0=tin0; 00155 xor1=tin1; 00156 #endif 00157 } 00158 #ifndef CBC_ENC_C__DONT_UPDATE_IV 00159 iv = &(*ivec)[0]; 00160 l2c(xor0,iv); 00161 l2c(xor1,iv); 00162 #endif 00163 } 00164 tin0=tin1=tout0=tout1=xor0=xor1=0; 00165 tin[0]=tin[1]=0; 00166 } 00167 00168 00169 #endif /* POK_NEEDS_ ...*/