i_cfb64.c revision 63249
1187423Sgonzo/* crypto/idea/i_cfb64.c */
2187423Sgonzo/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3187423Sgonzo * All rights reserved.
4187423Sgonzo *
5187423Sgonzo * This package is an SSL implementation written
6187423Sgonzo * by Eric Young (eay@cryptsoft.com).
7187423Sgonzo * The implementation was written so as to conform with Netscapes SSL.
8187423Sgonzo *
9187423Sgonzo * This library is free for commercial and non-commercial use as long as
10187423Sgonzo * the following conditions are aheared to.  The following conditions
11187423Sgonzo * apply to all code found in this distribution, be it the RC4, RSA,
12187423Sgonzo * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13187423Sgonzo * included with this distribution is covered by the same copyright terms
14187423Sgonzo * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15187423Sgonzo *
16187423Sgonzo * Copyright remains Eric Young's, and as such any Copyright notices in
17187423Sgonzo * the code are not to be removed.
18187423Sgonzo * If this package is used in a product, Eric Young should be given attribution
19187423Sgonzo * as the author of the parts of the library used.
20187423Sgonzo * This can be in the form of a textual message at program startup or
21187423Sgonzo * in documentation (online or textual) provided with the package.
22187423Sgonzo *
23187423Sgonzo * Redistribution and use in source and binary forms, with or without
24187423Sgonzo * modification, are permitted provided that the following conditions
25187423Sgonzo * are met:
26187423Sgonzo * 1. Redistributions of source code must retain the copyright
27187423Sgonzo *    notice, this list of conditions and the following disclaimer.
28187423Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
29187423Sgonzo *    notice, this list of conditions and the following disclaimer in the
30187423Sgonzo *    documentation and/or other materials provided with the distribution.
31230195Sadrian * 3. All advertising materials mentioning features or use of this software
32187423Sgonzo *    must display the following acknowledgement:
33187423Sgonzo *    "This product includes cryptographic software written by
34187423Sgonzo *     Eric Young (eay@cryptsoft.com)"
35187423Sgonzo *    The word 'cryptographic' can be left out if the rouines from the library
36187423Sgonzo *    being used are not cryptographic related :-).
37187423Sgonzo * 4. If you include any Windows specific code (or a derivative thereof) from
38187423Sgonzo *    the apps directory (application code) you must include an acknowledgement:
39187423Sgonzo *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40198562Sthompsa *
41187423Sgonzo * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42187423Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43187423Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44187423Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45192178Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46192178Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47187423Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48187423Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49223562Skevlo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50187423Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51187423Sgonzo * SUCH DAMAGE.
52187423Sgonzo *
53187423Sgonzo * The licence and distribution terms for any publically available version or
54187423Sgonzo * derivative of this code cannot be changed.  i.e. this code cannot simply be
55187456Sgonzo * copied and put under another distribution licence
56187423Sgonzo * [including the GNU Public Licence.]
57211476Sadrian * $FreeBSD: head/crypto/openssl/crypto/idea/i_cfb64.c 61828 2000-06-19 21:09:27Z markm $
58211476Sadrian */
59211476Sadrian
60223562Skevlo#include <openssl/idea.h>
61223562Skevlo#include "idea_lcl.h"
62202954Sgonzo
63202954Sgonzo/* The input and output encrypted as though 64bit cfb mode is being
64192178Sgonzo * used.  The extra state information to record how much of the
65198562Sthompsa * 64bit block we have used is contained in *num;
66198562Sthompsa */
67198562Sthompsa
68187423Sgonzovoid idea_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
69198562Sthompsa	     IDEA_KEY_SCHEDULE *schedule, unsigned char *ivec, int *num,
70198562Sthompsa	     int encrypt)
71198562Sthompsa	{
72198562Sthompsa	register unsigned long v0,v1,t;
73198562Sthompsa	register int n= *num;
74198562Sthompsa	register long l=length;
75198562Sthompsa	unsigned long ti[2];
76198562Sthompsa	unsigned char *iv,c,cc;
77198562Sthompsa
78198562Sthompsa	iv=(unsigned char *)ivec;
79198562Sthompsa	if (encrypt)
80198562Sthompsa		{
81198562Sthompsa		while (l--)
82198562Sthompsa			{
83198562Sthompsa			if (n == 0)
84198562Sthompsa				{
85198562Sthompsa				n2l(iv,v0); ti[0]=v0;
86198562Sthompsa				n2l(iv,v1); ti[1]=v1;
87198562Sthompsa				idea_encrypt((unsigned long *)ti,schedule);
88198562Sthompsa				iv=(unsigned char *)ivec;
89198562Sthompsa				t=ti[0]; l2n(t,iv);
90198562Sthompsa				t=ti[1]; l2n(t,iv);
91198562Sthompsa				iv=(unsigned char *)ivec;
92198562Sthompsa				}
93198562Sthompsa			c= *(in++)^iv[n];
94198562Sthompsa			*(out++)=c;
95198562Sthompsa			iv[n]=c;
96198562Sthompsa			n=(n+1)&0x07;
97198562Sthompsa			}
98198562Sthompsa		}
99198562Sthompsa	else
100198562Sthompsa		{
101198562Sthompsa		while (l--)
102187423Sgonzo			{
103198669Srrs			if (n == 0)
104198669Srrs				{
105198669Srrs				n2l(iv,v0); ti[0]=v0;
106198669Srrs				n2l(iv,v1); ti[1]=v1;
107198669Srrs				idea_encrypt((unsigned long *)ti,schedule);
108198669Srrs				iv=(unsigned char *)ivec;
109187423Sgonzo				t=ti[0]; l2n(t,iv);
110187423Sgonzo				t=ti[1]; l2n(t,iv);
111211480Sadrian				iv=(unsigned char *)ivec;
112187463Sgonzo				}
113187463Sgonzo			cc= *(in++);
114187463Sgonzo			c=iv[n];
115187423Sgonzo			iv[n]=cc;
116187423Sgonzo			*(out++)=c^cc;
117220056Sadrian			n=(n+1)&0x07;
118220056Sadrian			}
119220056Sadrian		}
120220056Sadrian	v0=v1=ti[0]=ti[1]=t=c=cc=0;
121220056Sadrian	*num=n;
122220056Sadrian	}
123220056Sadrian
124220056Sadrian