Blame snmplib/openssl/openssl_ncbc_enc.c

Packit fcad23
/* crypto/des/ncbc_enc.c */
Packit fcad23
/*
Packit fcad23
 * #included by:
Packit fcad23
 *    cbc_enc.c  (DES_cbc_encrypt)
Packit fcad23
 *    des_enc.c  (DES_ncbc_encrypt)
Packit fcad23
 */
Packit fcad23
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
Packit fcad23
 * All rights reserved.
Packit fcad23
 *
Packit fcad23
 * This package is an SSL implementation written
Packit fcad23
 * by Eric Young (eay@cryptsoft.com).
Packit fcad23
 * The implementation was written so as to conform with Netscapes SSL.
Packit fcad23
 * 
Packit fcad23
 * This library is free for commercial and non-commercial use as long as
Packit fcad23
 * the following conditions are aheared to.  The following conditions
Packit fcad23
 * apply to all code found in this distribution, be it the RC4, RSA,
Packit fcad23
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
Packit fcad23
 * included with this distribution is covered by the same copyright terms
Packit fcad23
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
Packit fcad23
 * 
Packit fcad23
 * Copyright remains Eric Young's, and as such any Copyright notices in
Packit fcad23
 * the code are not to be removed.
Packit fcad23
 * If this package is used in a product, Eric Young should be given attribution
Packit fcad23
 * as the author of the parts of the library used.
Packit fcad23
 * This can be in the form of a textual message at program startup or
Packit fcad23
 * in documentation (online or textual) provided with the package.
Packit fcad23
 * 
Packit fcad23
 * Redistribution and use in source and binary forms, with or without
Packit fcad23
 * modification, are permitted provided that the following conditions
Packit fcad23
 * are met:
Packit fcad23
 * 1. Redistributions of source code must retain the copyright
Packit fcad23
 *    notice, this list of conditions and the following disclaimer.
Packit fcad23
 * 2. Redistributions in binary form must reproduce the above copyright
Packit fcad23
 *    notice, this list of conditions and the following disclaimer in the
Packit fcad23
 *    documentation and/or other materials provided with the distribution.
Packit fcad23
 * 3. All advertising materials mentioning features or use of this software
Packit fcad23
 *    must display the following acknowledgement:
Packit fcad23
 *    "This product includes cryptographic software written by
Packit fcad23
 *     Eric Young (eay@cryptsoft.com)"
Packit fcad23
 *    The word 'cryptographic' can be left out if the rouines from the library
Packit fcad23
 *    being used are not cryptographic related :-).
Packit fcad23
 * 4. If you include any Windows specific code (or a derivative thereof) from 
Packit fcad23
 *    the apps directory (application code) you must include an acknowledgement:
Packit fcad23
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Packit fcad23
 * 
Packit fcad23
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
Packit fcad23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit fcad23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit fcad23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
Packit fcad23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit fcad23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit fcad23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit fcad23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit fcad23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit fcad23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit fcad23
 * SUCH DAMAGE.
Packit fcad23
 * 
Packit fcad23
 * The licence and distribution terms for any publically available version or
Packit fcad23
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
Packit fcad23
 * copied and put under another distribution licence
Packit fcad23
 * [including the GNU Public Licence.]
Packit fcad23
 */
Packit fcad23
Packit fcad23
#include "openssl_des_local.h"
Packit fcad23
Packit fcad23
#ifdef CBC_ENC_C__DONT_UPDATE_IV
Packit fcad23
void DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
Packit fcad23
		     DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
Packit fcad23
#else
Packit fcad23
void DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length,
Packit fcad23
		     DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
Packit fcad23
#endif
Packit fcad23
	{
Packit fcad23
	register DES_LONG tin0,tin1;
Packit fcad23
	register DES_LONG tout0,tout1,xor0,xor1;
Packit fcad23
	register long l=length;
Packit fcad23
	DES_LONG tin[2];
Packit fcad23
	unsigned char *iv;
Packit fcad23
Packit fcad23
	iv = &(*ivec)[0];
Packit fcad23
Packit fcad23
	if (enc)
Packit fcad23
		{
Packit fcad23
		c2l(iv,tout0);
Packit fcad23
		c2l(iv,tout1);
Packit fcad23
		for (l-=8; l>=0; l-=8)
Packit fcad23
			{
Packit fcad23
			c2l(in,tin0);
Packit fcad23
			c2l(in,tin1);
Packit fcad23
			tin0^=tout0; tin[0]=tin0;
Packit fcad23
			tin1^=tout1; tin[1]=tin1;
Packit fcad23
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
Packit fcad23
			tout0=tin[0]; l2c(tout0,out);
Packit fcad23
			tout1=tin[1]; l2c(tout1,out);
Packit fcad23
			}
Packit fcad23
		if (l != -8)
Packit fcad23
			{
Packit fcad23
			c2ln(in,tin0,tin1,l+8);
Packit fcad23
			tin0^=tout0; tin[0]=tin0;
Packit fcad23
			tin1^=tout1; tin[1]=tin1;
Packit fcad23
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
Packit fcad23
			tout0=tin[0]; l2c(tout0,out);
Packit fcad23
			tout1=tin[1]; l2c(tout1,out);
Packit fcad23
			}
Packit fcad23
#ifndef CBC_ENC_C__DONT_UPDATE_IV
Packit fcad23
		iv = &(*ivec)[0];
Packit fcad23
		l2c(tout0,iv);
Packit fcad23
		l2c(tout1,iv);
Packit fcad23
#endif
Packit fcad23
		}
Packit fcad23
	else
Packit fcad23
		{
Packit fcad23
		c2l(iv,xor0);
Packit fcad23
		c2l(iv,xor1);
Packit fcad23
		for (l-=8; l>=0; l-=8)
Packit fcad23
			{
Packit fcad23
			c2l(in,tin0); tin[0]=tin0;
Packit fcad23
			c2l(in,tin1); tin[1]=tin1;
Packit fcad23
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
Packit fcad23
			tout0=tin[0]^xor0;
Packit fcad23
			tout1=tin[1]^xor1;
Packit fcad23
			l2c(tout0,out);
Packit fcad23
			l2c(tout1,out);
Packit fcad23
			xor0=tin0;
Packit fcad23
			xor1=tin1;
Packit fcad23
			}
Packit fcad23
		if (l != -8)
Packit fcad23
			{
Packit fcad23
			c2l(in,tin0); tin[0]=tin0;
Packit fcad23
			c2l(in,tin1); tin[1]=tin1;
Packit fcad23
			DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
Packit fcad23
			tout0=tin[0]^xor0;
Packit fcad23
			tout1=tin[1]^xor1;
Packit fcad23
			l2cn(tout0,tout1,out,l+8);
Packit fcad23
#ifndef CBC_ENC_C__DONT_UPDATE_IV
Packit fcad23
			xor0=tin0;
Packit fcad23
			xor1=tin1;
Packit fcad23
#endif
Packit fcad23
			}
Packit fcad23
#ifndef CBC_ENC_C__DONT_UPDATE_IV 
Packit fcad23
		iv = &(*ivec)[0];
Packit fcad23
		l2c(xor0,iv);
Packit fcad23
		l2c(xor1,iv);
Packit fcad23
#endif
Packit fcad23
		}
Packit fcad23
	tin0=tin1=tout0=tout1=xor0=xor1=0;
Packit fcad23
	tin[0]=tin[1]=0;
Packit fcad23
	}