1139749Simp/* crypto/des/qud_cksm.c */
2197404Sjoel/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3197404Sjoel * All rights reserved.
4197404Sjoel *
550724Scg * This package is an SSL implementation written
6197404Sjoel * by Eric Young (eay@cryptsoft.com).
7197404Sjoel * The implementation was written so as to conform with Netscapes SSL.
8197404Sjoel *
9197404Sjoel * This library is free for commercial and non-commercial use as long as
10197404Sjoel * the following conditions are aheared to.  The following conditions
11197404Sjoel * apply to all code found in this distribution, be it the RC4, RSA,
12197404Sjoel * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13197404Sjoel * included with this distribution is covered by the same copyright terms
14197404Sjoel * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15197404Sjoel *
16197404Sjoel * Copyright remains Eric Young's, and as such any Copyright notices in
17197404Sjoel * the code are not to be removed.
18197404Sjoel * If this package is used in a product, Eric Young should be given attribution
19197404Sjoel * as the author of the parts of the library used.
20197404Sjoel * This can be in the form of a textual message at program startup or
21197404Sjoel * in documentation (online or textual) provided with the package.
22197404Sjoel *
23197404Sjoel * Redistribution and use in source and binary forms, with or without
24197404Sjoel * modification, are permitted provided that the following conditions
25197404Sjoel * are met:
26197404Sjoel * 1. Redistributions of source code must retain the copyright
27197404Sjoel *    notice, this list of conditions and the following disclaimer.
28197404Sjoel * 2. Redistributions in binary form must reproduce the above copyright
29119853Scg *    notice, this list of conditions and the following disclaimer in the
30197404Sjoel *    documentation and/or other materials provided with the distribution.
3150724Scg * 3. All advertising materials mentioning features or use of this software
3250724Scg *    must display the following acknowledgement:
3350724Scg *    "This product includes cryptographic software written by
3450724Scg *     Eric Young (eay@cryptsoft.com)"
3550724Scg *    The word 'cryptographic' can be left out if the rouines from the library
3650724Scg *    being used are not cryptographic related :-).
3750724Scg * 4. If you include any Windows specific code (or a derivative thereof) from
3850724Scg *    the apps directory (application code) you must include an acknowledgement:
3950724Scg *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4050724Scg *
4150724Scg * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4250724Scg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4350724Scg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4450724Scg * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4550724Scg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4650724Scg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4750724Scg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4850724Scg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4950724Scg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5050724Scg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5150724Scg * SUCH DAMAGE.
5250724Scg *
5350724Scg * The licence and distribution terms for any publically available version or
5450724Scg * derivative of this code cannot be changed.  i.e. this code cannot simply be
5550724Scg * copied and put under another distribution licence
5650724Scg * [including the GNU Public Licence.]
5750724Scg */
5850724Scg
5950724Scg/*
6050724Scg * From "Message Authentication" R.R. Jueneman, S.M. Matyas, C.H. Meyer IEEE
6150724Scg * Communications Magazine Sept 1985 Vol. 23 No. 9 p 29-40 This module in
6250724Scg * only based on the code in this paper and is almost definitely not the same
6350724Scg * as the MIT implementation.
6450724Scg */
6553413Sroger#include "des_locl.h"
66197404Sjoel
67197404Sjoel/* bug fix for dos - 7/6/91 - Larry hughes@logos.ucs.indiana.edu */
68197404Sjoel#define Q_B0(a) (((DES_LONG)(a)))
6953413Sroger#define Q_B1(a) (((DES_LONG)(a))<<8)
7053413Sroger#define Q_B2(a) (((DES_LONG)(a))<<16)
7154831Scg#define Q_B3(a) (((DES_LONG)(a))<<24)
7254831Scg
7353413Sroger/* used to scramble things a bit */
7453413Sroger/* Got the value MIT uses via brute force :-) 2/10/90 eay */
7553413Sroger#define NOISE   ((DES_LONG)83653421L)
76193640Sariff
77193640SariffDES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[],
78193640Sariff                        long length, int out_count, DES_cblock *seed)
79193640Sariff{
8053465Scg    DES_LONG z0, z1, t0, t1;
8153465Scg    int i;
8253465Scg    long l;
8350724Scg    const unsigned char *cp;
84119287Simp#ifdef _CRAY
85119287Simp    struct lp_st {
8650724Scg        int a:32;
8753413Sroger        int b:32;
8853413Sroger    } *lp;
8970134Scg#else
9070134Scg    DES_LONG *lp;
9182180Scg#endif
9282180Scg
9350724Scg    if (out_count < 1)
9450724Scg        out_count = 1;
9550724Scg#ifdef _CRAY
9650724Scg    lp = (struct lp_st *)&(output[0])[0];
9753413Sroger#else
9856154Speter    lp = (DES_LONG *)&(output[0])[0];
9976086Scg#endif
100119548Sorion
10150724Scg    z0 = Q_B0((*seed)[0]) | Q_B1((*seed)[1]) | Q_B2((*seed)[2]) |
10278033Scg        Q_B3((*seed)[3]);
10376086Scg    z1 = Q_B0((*seed)[4]) | Q_B1((*seed)[5]) | Q_B2((*seed)[6]) |
10476086Scg        Q_B3((*seed)[7]);
10576086Scg
10676086Scg    for (i = 0; ((i < 4) && (i < out_count)); i++) {
10776086Scg        cp = input;
10876086Scg        l = length;
10976086Scg        while (l > 0) {
11076086Scg            if (l > 1) {
11176086Scg                t0 = (DES_LONG)(*(cp++));
11276086Scg                t0 |= (DES_LONG)Q_B1(*(cp++));
11395678Scg                l--;
11476086Scg            } else
115119548Sorion                t0 = (DES_LONG)(*(cp++));
116119548Sorion            l--;
11784658Scg            /* add */
11859019Scg            t0 += z0;
119152419Sariff            t0 &= 0xffffffffL;
120152419Sariff            t1 = z1;
121152419Sariff            /* square, well sort of square */
122152419Sariff            z0 = ((((t0 * t0) & 0xffffffffL) + ((t1 * t1) & 0xffffffffL))
123152419Sariff                  & 0xffffffffL) % 0x7fffffffL;
124152419Sariff            z1 = ((t0 * ((t1 + NOISE) & 0xffffffffL)) & 0xffffffffL) %
125167648Sariff                0x7fffffffL;
126167648Sariff        }
127167648Sariff        if (lp != NULL) {
128167648Sariff            /*
129167648Sariff             * The MIT library assumes that the checksum is composed of
130152419Sariff             * 2*out_count 32 bit ints
131152419Sariff             */
132152419Sariff#ifdef _CRAY
13350724Scg            (*lp).a = z0;
13450724Scg            (*lp).b = z1;
13550724Scg            lp++;
13655209Scg#else
13750724Scg            *lp++ = z0;
13874763Scg            *lp++ = z1;
13974763Scg#endif
140152419Sariff        }
141152419Sariff    }
142164614Sariff    return (z0);
143164614Sariff}
144164614Sariff