155714Skris/* crypto/des/ncbc_enc.c */ 2280304Sjkim/*- 359191Skris * #included by: 4109998Smarkm * cbc_enc.c (DES_cbc_encrypt) 5109998Smarkm * des_enc.c (DES_ncbc_encrypt) 659191Skris */ 755714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 855714Skris * All rights reserved. 955714Skris * 1055714Skris * This package is an SSL implementation written 1155714Skris * by Eric Young (eay@cryptsoft.com). 1255714Skris * The implementation was written so as to conform with Netscapes SSL. 13280304Sjkim * 1455714Skris * This library is free for commercial and non-commercial use as long as 1555714Skris * the following conditions are aheared to. The following conditions 1655714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1755714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1855714Skris * included with this distribution is covered by the same copyright terms 1955714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 20280304Sjkim * 2155714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 2255714Skris * the code are not to be removed. 2355714Skris * If this package is used in a product, Eric Young should be given attribution 2455714Skris * as the author of the parts of the library used. 2555714Skris * This can be in the form of a textual message at program startup or 2655714Skris * in documentation (online or textual) provided with the package. 27280304Sjkim * 2855714Skris * Redistribution and use in source and binary forms, with or without 2955714Skris * modification, are permitted provided that the following conditions 3055714Skris * are met: 3155714Skris * 1. Redistributions of source code must retain the copyright 3255714Skris * notice, this list of conditions and the following disclaimer. 3355714Skris * 2. Redistributions in binary form must reproduce the above copyright 3455714Skris * notice, this list of conditions and the following disclaimer in the 3555714Skris * documentation and/or other materials provided with the distribution. 3655714Skris * 3. All advertising materials mentioning features or use of this software 3755714Skris * must display the following acknowledgement: 3855714Skris * "This product includes cryptographic software written by 3955714Skris * Eric Young (eay@cryptsoft.com)" 4055714Skris * The word 'cryptographic' can be left out if the rouines from the library 4155714Skris * being used are not cryptographic related :-). 42280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 4355714Skris * the apps directory (application code) you must include an acknowledgement: 4455714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 45280304Sjkim * 4655714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4755714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4855714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4955714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 5055714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5155714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5255714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5355714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5455714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5555714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5655714Skris * SUCH DAMAGE. 57280304Sjkim * 5855714Skris * The licence and distribution terms for any publically available version or 5955714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 6055714Skris * copied and put under another distribution licence 6155714Skris * [including the GNU Public Licence.] 6255714Skris */ 6355714Skris 6455714Skris#include "des_locl.h" 6555714Skris 6655714Skris#ifdef CBC_ENC_C__DONT_UPDATE_IV 67109998Smarkmvoid DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 68280304Sjkim DES_key_schedule *_schedule, DES_cblock *ivec, int enc) 6955714Skris#else 70280304Sjkimvoid DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, 71280304Sjkim long length, DES_key_schedule *_schedule, 72280304Sjkim DES_cblock *ivec, int enc) 7355714Skris#endif 74280304Sjkim{ 75280304Sjkim register DES_LONG tin0, tin1; 76280304Sjkim register DES_LONG tout0, tout1, xor0, xor1; 77280304Sjkim register long l = length; 78280304Sjkim DES_LONG tin[2]; 79280304Sjkim unsigned char *iv; 8055714Skris 81280304Sjkim iv = &(*ivec)[0]; 8255714Skris 83280304Sjkim if (enc) { 84280304Sjkim c2l(iv, tout0); 85280304Sjkim c2l(iv, tout1); 86280304Sjkim for (l -= 8; l >= 0; l -= 8) { 87280304Sjkim c2l(in, tin0); 88280304Sjkim c2l(in, tin1); 89280304Sjkim tin0 ^= tout0; 90280304Sjkim tin[0] = tin0; 91280304Sjkim tin1 ^= tout1; 92280304Sjkim tin[1] = tin1; 93280304Sjkim DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT); 94280304Sjkim tout0 = tin[0]; 95280304Sjkim l2c(tout0, out); 96280304Sjkim tout1 = tin[1]; 97280304Sjkim l2c(tout1, out); 98280304Sjkim } 99280304Sjkim if (l != -8) { 100280304Sjkim c2ln(in, tin0, tin1, l + 8); 101280304Sjkim tin0 ^= tout0; 102280304Sjkim tin[0] = tin0; 103280304Sjkim tin1 ^= tout1; 104280304Sjkim tin[1] = tin1; 105280304Sjkim DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT); 106280304Sjkim tout0 = tin[0]; 107280304Sjkim l2c(tout0, out); 108280304Sjkim tout1 = tin[1]; 109280304Sjkim l2c(tout1, out); 110280304Sjkim } 11155714Skris#ifndef CBC_ENC_C__DONT_UPDATE_IV 112280304Sjkim iv = &(*ivec)[0]; 113280304Sjkim l2c(tout0, iv); 114280304Sjkim l2c(tout1, iv); 11555714Skris#endif 116280304Sjkim } else { 117280304Sjkim c2l(iv, xor0); 118280304Sjkim c2l(iv, xor1); 119280304Sjkim for (l -= 8; l >= 0; l -= 8) { 120280304Sjkim c2l(in, tin0); 121280304Sjkim tin[0] = tin0; 122280304Sjkim c2l(in, tin1); 123280304Sjkim tin[1] = tin1; 124280304Sjkim DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT); 125280304Sjkim tout0 = tin[0] ^ xor0; 126280304Sjkim tout1 = tin[1] ^ xor1; 127280304Sjkim l2c(tout0, out); 128280304Sjkim l2c(tout1, out); 129280304Sjkim xor0 = tin0; 130280304Sjkim xor1 = tin1; 131280304Sjkim } 132280304Sjkim if (l != -8) { 133280304Sjkim c2l(in, tin0); 134280304Sjkim tin[0] = tin0; 135280304Sjkim c2l(in, tin1); 136280304Sjkim tin[1] = tin1; 137280304Sjkim DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT); 138280304Sjkim tout0 = tin[0] ^ xor0; 139280304Sjkim tout1 = tin[1] ^ xor1; 140280304Sjkim l2cn(tout0, tout1, out, l + 8); 14155714Skris#ifndef CBC_ENC_C__DONT_UPDATE_IV 142280304Sjkim xor0 = tin0; 143280304Sjkim xor1 = tin1; 14455714Skris#endif 145280304Sjkim } 146280304Sjkim#ifndef CBC_ENC_C__DONT_UPDATE_IV 147280304Sjkim iv = &(*ivec)[0]; 148280304Sjkim l2c(xor0, iv); 149280304Sjkim l2c(xor1, iv); 15055714Skris#endif 151280304Sjkim } 152280304Sjkim tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; 153280304Sjkim tin[0] = tin[1] = 0; 154280304Sjkim} 155