1109998Smarkm/* crypto/rc2/rc2cfb64.c */
2109998Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3109998Smarkm * All rights reserved.
4109998Smarkm *
5109998Smarkm * This package is an SSL implementation written
6109998Smarkm * by Eric Young (eay@cryptsoft.com).
7109998Smarkm * The implementation was written so as to conform with Netscapes SSL.
8109998Smarkm *
9109998Smarkm * This library is free for commercial and non-commercial use as long as
10109998Smarkm * the following conditions are aheared to.  The following conditions
11109998Smarkm * apply to all code found in this distribution, be it the RC4, RSA,
12109998Smarkm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13109998Smarkm * included with this distribution is covered by the same copyright terms
14109998Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15109998Smarkm *
16109998Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in
17109998Smarkm * the code are not to be removed.
18109998Smarkm * If this package is used in a product, Eric Young should be given attribution
19109998Smarkm * as the author of the parts of the library used.
20109998Smarkm * This can be in the form of a textual message at program startup or
21109998Smarkm * in documentation (online or textual) provided with the package.
22109998Smarkm *
23109998Smarkm * Redistribution and use in source and binary forms, with or without
24109998Smarkm * modification, are permitted provided that the following conditions
25109998Smarkm * are met:
26109998Smarkm * 1. Redistributions of source code must retain the copyright
27109998Smarkm *    notice, this list of conditions and the following disclaimer.
28109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
29109998Smarkm *    notice, this list of conditions and the following disclaimer in the
30109998Smarkm *    documentation and/or other materials provided with the distribution.
31109998Smarkm * 3. All advertising materials mentioning features or use of this software
32109998Smarkm *    must display the following acknowledgement:
33109998Smarkm *    "This product includes cryptographic software written by
34109998Smarkm *     Eric Young (eay@cryptsoft.com)"
35109998Smarkm *    The word 'cryptographic' can be left out if the rouines from the library
36109998Smarkm *    being used are not cryptographic related :-).
37109998Smarkm * 4. If you include any Windows specific code (or a derivative thereof) from
38109998Smarkm *    the apps directory (application code) you must include an acknowledgement:
39109998Smarkm *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40109998Smarkm *
41109998Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42109998Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44109998Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45109998Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46109998Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47109998Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49109998Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50109998Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51109998Smarkm * SUCH DAMAGE.
52109998Smarkm *
53109998Smarkm * The licence and distribution terms for any publically available version or
54109998Smarkm * derivative of this code cannot be changed.  i.e. this code cannot simply be
55109998Smarkm * copied and put under another distribution licence
56109998Smarkm * [including the GNU Public Licence.]
57109998Smarkm */
58109998Smarkm
59109998Smarkm#include <openssl/rc2.h>
60109998Smarkm#include "rc2_locl.h"
61109998Smarkm
62109998Smarkm/*
63109998Smarkm * The input and output encrypted as though 64bit cfb mode is being used.
64109998Smarkm * The extra state information to record how much of the 64bit block we have
65109998Smarkm * used is contained in *num;
66109998Smarkm */
67109998Smarkm
68109998Smarkmvoid RC2_cfb64_encrypt(const unsigned char *in, unsigned char *out,
69109998Smarkm                       long length, RC2_KEY *schedule, unsigned char *ivec,
70109998Smarkm                       int *num, int encrypt)
71109998Smarkm{
72109998Smarkm    register unsigned long v0, v1, t;
73109998Smarkm    register int n = *num;
74109998Smarkm    register long l = length;
75109998Smarkm    unsigned long ti[2];
76109998Smarkm    unsigned char *iv, c, cc;
77109998Smarkm
78109998Smarkm    iv = (unsigned char *)ivec;
79109998Smarkm    if (encrypt) {
80109998Smarkm        while (l--) {
81109998Smarkm            if (n == 0) {
82109998Smarkm                c2l(iv, v0);
83109998Smarkm                ti[0] = v0;
84109998Smarkm                c2l(iv, v1);
85109998Smarkm                ti[1] = v1;
86109998Smarkm                RC2_encrypt((unsigned long *)ti, schedule);
87109998Smarkm                iv = (unsigned char *)ivec;
88109998Smarkm                t = ti[0];
89109998Smarkm                l2c(t, iv);
90109998Smarkm                t = ti[1];
91109998Smarkm                l2c(t, iv);
92109998Smarkm                iv = (unsigned char *)ivec;
93109998Smarkm            }
94109998Smarkm            c = *(in++) ^ iv[n];
95109998Smarkm            *(out++) = c;
96109998Smarkm            iv[n] = c;
97109998Smarkm            n = (n + 1) & 0x07;
98109998Smarkm        }
99109998Smarkm    } else {
100194206Ssimon        while (l--) {
101160814Ssimon            if (n == 0) {
102109998Smarkm                c2l(iv, v0);
103194206Ssimon                ti[0] = v0;
104194206Ssimon                c2l(iv, v1);
105109998Smarkm                ti[1] = v1;
106109998Smarkm                RC2_encrypt((unsigned long *)ti, schedule);
107160814Ssimon                iv = (unsigned char *)ivec;
108160814Ssimon                t = ti[0];
109160814Ssimon                l2c(t, iv);
110160814Ssimon                t = ti[1];
111160814Ssimon                l2c(t, iv);
112160814Ssimon                iv = (unsigned char *)ivec;
113160814Ssimon            }
114160814Ssimon            cc = *(in++);
115160814Ssimon            c = iv[n];
116160814Ssimon            iv[n] = cc;
117160814Ssimon            *(out++) = c ^ cc;
118160814Ssimon            n = (n + 1) & 0x07;
119109998Smarkm        }
120109998Smarkm    }
121109998Smarkm    v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
122109998Smarkm    *num = n;
123109998Smarkm}
124109998Smarkm