147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* crypto/bf/bf_cfb64.c */
247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * All rights reserved.
447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *
547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This package is an SSL implementation written
647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * by Eric Young (eay@cryptsoft.com).
747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The implementation was written so as to conform with Netscapes SSL.
847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *
947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This library is free for commercial and non-commercial use as long as
1047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the following conditions are aheared to.  The following conditions
1147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * apply to all code found in this distribution, be it the RC4, RSA,
1247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * included with this distribution is covered by the same copyright terms
1447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *
1647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Copyright remains Eric Young's, and as such any Copyright notices in
1747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the code are not to be removed.
1847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * If this package is used in a product, Eric Young should be given attribution
1947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * as the author of the parts of the library used.
2047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This can be in the form of a textual message at program startup or
2147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * in documentation (online or textual) provided with the package.
2247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *
2347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Redistribution and use in source and binary forms, with or without
2447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * modification, are permitted provided that the following conditions
2547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * are met:
2647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 1. Redistributions of source code must retain the copyright
2747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    notice, this list of conditions and the following disclaimer.
2847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright
2947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    notice, this list of conditions and the following disclaimer in the
3047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    documentation and/or other materials provided with the distribution.
3147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 3. All advertising materials mentioning features or use of this software
3247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    must display the following acknowledgement:
3347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    "This product includes cryptographic software written by
3447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *     Eric Young (eay@cryptsoft.com)"
3547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    The word 'cryptographic' can be left out if the rouines from the library
3647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    being used are not cryptographic related :-).
3747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 4. If you include any Windows specific code (or a derivative thereof) from
3847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    the apps directory (application code) you must include an acknowledgement:
3947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *
4147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * SUCH DAMAGE.
5247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom *
5347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The licence and distribution terms for any publically available version or
5447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * derivative of this code cannot be changed.  i.e. this code cannot simply be
5547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * copied and put under another distribution licence
5647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * [including the GNU Public Licence.]
5747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom */
5847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
5947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include <openssl/blowfish.h>
6047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include "bf_locl.h"
6147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
6247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* The input and output encrypted as though 64bit cfb mode is being
6347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * used.  The extra state information to record how much of the
6447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 64bit block we have used is contained in *num;
6547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom */
6647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
6747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromvoid BF_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length,
6847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	     const BF_KEY *schedule, unsigned char *ivec, int *num, int encrypt)
6947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	{
7047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	register BF_LONG v0,v1,t;
7147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	register int n= *num;
7247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	register long l=length;
7347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	BF_LONG ti[2];
7447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	unsigned char *iv,c,cc;
7547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
7647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	iv=(unsigned char *)ivec;
7747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	if (encrypt)
7847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		{
7947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		while (l--)
8047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			{
8147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			if (n == 0)
8247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				{
8347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				n2l(iv,v0); ti[0]=v0;
8447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				n2l(iv,v1); ti[1]=v1;
8547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				BF_encrypt((BF_LONG *)ti,schedule);
8647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				iv=(unsigned char *)ivec;
8747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				t=ti[0]; l2n(t,iv);
8847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				t=ti[1]; l2n(t,iv);
8947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				iv=(unsigned char *)ivec;
9047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				}
9147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			c= *(in++)^iv[n];
9247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			*(out++)=c;
9347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			iv[n]=c;
9447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			n=(n+1)&0x07;
9547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			}
9647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		}
9747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	else
9847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		{
9947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		while (l--)
10047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			{
10147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			if (n == 0)
10247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				{
10347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				n2l(iv,v0); ti[0]=v0;
10447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				n2l(iv,v1); ti[1]=v1;
10547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				BF_encrypt((BF_LONG *)ti,schedule);
10647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				iv=(unsigned char *)ivec;
10747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				t=ti[0]; l2n(t,iv);
10847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				t=ti[1]; l2n(t,iv);
10947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				iv=(unsigned char *)ivec;
11047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom				}
11147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			cc= *(in++);
11247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			c=iv[n];
11347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			iv[n]=cc;
11447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			*(out++)=c^cc;
11547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			n=(n+1)&0x07;
11647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom			}
11747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom		}
11847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	v0=v1=ti[0]=ti[1]=t=c=cc=0;
11947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	*num=n;
12047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom	}
12147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom
122