10SN/A/* crypto/cast/c_cfb64.c */
29330SN/A/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30SN/A * All rights reserved.
40SN/A *
50SN/A * This package is an SSL implementation written
60SN/A * by Eric Young (eay@cryptsoft.com).
72362SN/A * The implementation was written so as to conform with Netscapes SSL.
80SN/A *
92362SN/A * This library is free for commercial and non-commercial use as long as
100SN/A * the following conditions are aheared to.  The following conditions
110SN/A * apply to all code found in this distribution, be it the RC4, RSA,
120SN/A * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130SN/A * included with this distribution is covered by the same copyright terms
140SN/A * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150SN/A *
160SN/A * Copyright remains Eric Young's, and as such any Copyright notices in
170SN/A * the code are not to be removed.
180SN/A * If this package is used in a product, Eric Young should be given attribution
190SN/A * as the author of the parts of the library used.
200SN/A * This can be in the form of a textual message at program startup or
212362SN/A * in documentation (online or textual) provided with the package.
222362SN/A *
232362SN/A * Redistribution and use in source and binary forms, with or without
240SN/A * modification, are permitted provided that the following conditions
250SN/A * are met:
260SN/A * 1. Redistributions of source code must retain the copyright
270SN/A *    notice, this list of conditions and the following disclaimer.
280SN/A * 2. Redistributions in binary form must reproduce the above copyright
290SN/A *    notice, this list of conditions and the following disclaimer in the
3011299Sjnimeh *    documentation and/or other materials provided with the distribution.
310SN/A * 3. All advertising materials mentioning features or use of this software
320SN/A *    must display the following acknowledgement:
330SN/A *    "This product includes cryptographic software written by
340SN/A *     Eric Young (eay@cryptsoft.com)"
350SN/A *    The word 'cryptographic' can be left out if the rouines from the library
360SN/A *    being used are not cryptographic related :-).
370SN/A * 4. If you include any Windows specific code (or a derivative thereof) from
380SN/A *    the apps directory (application code) you must include an acknowledgement:
390SN/A *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400SN/A *
410SN/A * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4511299Sjnimeh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4612318Sigerasim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510SN/A * SUCH DAMAGE.
520SN/A *
530SN/A * The licence and distribution terms for any publically available version or
540SN/A * derivative of this code cannot be changed.  i.e. this code cannot simply be
550SN/A * copied and put under another distribution licence
560SN/A * [including the GNU Public Licence.]
570SN/A */
580SN/A
5912318Sigerasim#include <openssl/cast.h>
600SN/A#include "cast_lcl.h"
610SN/A
620SN/A/*
630SN/A * The input and output encrypted as though 64bit cfb mode is being used.
640SN/A * The extra state information to record how much of the 64bit block we have
650SN/A * used is contained in *num;
660SN/A */
6712318Sigerasim
680SN/Avoid CAST_cfb64_encrypt(const unsigned char *in, unsigned char *out,
6928SN/A                        long length, const CAST_KEY *schedule,
700SN/A                        unsigned char *ivec, int *num, int enc)
710SN/A{
720SN/A    register CAST_LONG v0, v1, t;
736171SN/A    register int n = *num;
740SN/A    register long l = length;
750SN/A    CAST_LONG ti[2];
760SN/A    unsigned char *iv, c, cc;
7710089SN/A
780SN/A    iv = ivec;
7910089SN/A    if (enc) {
800SN/A        while (l--) {
8110089SN/A            if (n == 0) {
820SN/A                n2l(iv, v0);
8310089SN/A                ti[0] = v0;
840SN/A                n2l(iv, v1);
8510089SN/A                ti[1] = v1;
8610089SN/A                CAST_encrypt((CAST_LONG *)ti, schedule);
870SN/A                iv = ivec;
880SN/A                t = ti[0];
890SN/A                l2n(t, iv);
900SN/A                t = ti[1];
916171SN/A                l2n(t, iv);
920SN/A                iv = ivec;
930SN/A            }
940SN/A            c = *(in++) ^ iv[n];
950SN/A            *(out++) = c;
960SN/A            iv[n] = c;
970SN/A            n = (n + 1) & 0x07;
980SN/A        }
990SN/A    } else {
1000SN/A        while (l--) {
1010SN/A            if (n == 0) {
1026171SN/A                n2l(iv, v0);
1030SN/A                ti[0] = v0;
1040SN/A                n2l(iv, v1);
1050SN/A                ti[1] = v1;
1060SN/A                CAST_encrypt((CAST_LONG *)ti, schedule);
1070SN/A                iv = ivec;
1080SN/A                t = ti[0];
10912318Sigerasim                l2n(t, iv);
1100SN/A                t = ti[1];
1110SN/A                l2n(t, iv);
1120SN/A                iv = ivec;
1130SN/A            }
1140SN/A            cc = *(in++);
1150SN/A            c = iv[n];
1160SN/A            iv[n] = cc;
1170SN/A            *(out++) = c ^ cc;
1180SN/A            n = (n + 1) & 0x07;
11911299Sjnimeh        }
12011299Sjnimeh    }
12111299Sjnimeh    v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
12211299Sjnimeh    *num = n;
12311299Sjnimeh}
12411299Sjnimeh