xref: /openbsd-src/lib/libcrypto/des/des_cksum.c (revision cc54cb09a623db51faa8fe667d693654f766166c)
1*cc54cb09Sjsing /* $OpenBSD: des_cksum.c,v 1.1 2024/08/31 15:56:09 jsing Exp $ */
2*cc54cb09Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*cc54cb09Sjsing  * All rights reserved.
4*cc54cb09Sjsing  *
5*cc54cb09Sjsing  * This package is an SSL implementation written
6*cc54cb09Sjsing  * by Eric Young (eay@cryptsoft.com).
7*cc54cb09Sjsing  * The implementation was written so as to conform with Netscapes SSL.
8*cc54cb09Sjsing  *
9*cc54cb09Sjsing  * This library is free for commercial and non-commercial use as long as
10*cc54cb09Sjsing  * the following conditions are aheared to.  The following conditions
11*cc54cb09Sjsing  * apply to all code found in this distribution, be it the RC4, RSA,
12*cc54cb09Sjsing  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*cc54cb09Sjsing  * included with this distribution is covered by the same copyright terms
14*cc54cb09Sjsing  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*cc54cb09Sjsing  *
16*cc54cb09Sjsing  * Copyright remains Eric Young's, and as such any Copyright notices in
17*cc54cb09Sjsing  * the code are not to be removed.
18*cc54cb09Sjsing  * If this package is used in a product, Eric Young should be given attribution
19*cc54cb09Sjsing  * as the author of the parts of the library used.
20*cc54cb09Sjsing  * This can be in the form of a textual message at program startup or
21*cc54cb09Sjsing  * in documentation (online or textual) provided with the package.
22*cc54cb09Sjsing  *
23*cc54cb09Sjsing  * Redistribution and use in source and binary forms, with or without
24*cc54cb09Sjsing  * modification, are permitted provided that the following conditions
25*cc54cb09Sjsing  * are met:
26*cc54cb09Sjsing  * 1. Redistributions of source code must retain the copyright
27*cc54cb09Sjsing  *    notice, this list of conditions and the following disclaimer.
28*cc54cb09Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
29*cc54cb09Sjsing  *    notice, this list of conditions and the following disclaimer in the
30*cc54cb09Sjsing  *    documentation and/or other materials provided with the distribution.
31*cc54cb09Sjsing  * 3. All advertising materials mentioning features or use of this software
32*cc54cb09Sjsing  *    must display the following acknowledgement:
33*cc54cb09Sjsing  *    "This product includes cryptographic software written by
34*cc54cb09Sjsing  *     Eric Young (eay@cryptsoft.com)"
35*cc54cb09Sjsing  *    The word 'cryptographic' can be left out if the rouines from the library
36*cc54cb09Sjsing  *    being used are not cryptographic related :-).
37*cc54cb09Sjsing  * 4. If you include any Windows specific code (or a derivative thereof) from
38*cc54cb09Sjsing  *    the apps directory (application code) you must include an acknowledgement:
39*cc54cb09Sjsing  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*cc54cb09Sjsing  *
41*cc54cb09Sjsing  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*cc54cb09Sjsing  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*cc54cb09Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*cc54cb09Sjsing  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*cc54cb09Sjsing  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*cc54cb09Sjsing  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*cc54cb09Sjsing  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*cc54cb09Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*cc54cb09Sjsing  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*cc54cb09Sjsing  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*cc54cb09Sjsing  * SUCH DAMAGE.
52*cc54cb09Sjsing  *
53*cc54cb09Sjsing  * The licence and distribution terms for any publically available version or
54*cc54cb09Sjsing  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*cc54cb09Sjsing  * copied and put under another distribution licence
56*cc54cb09Sjsing  * [including the GNU Public Licence.]
57*cc54cb09Sjsing  */
58*cc54cb09Sjsing 
59*cc54cb09Sjsing /* From "Message Authentication"  R.R. Jueneman, S.M. Matyas, C.H. Meyer
60*cc54cb09Sjsing  * IEEE Communications Magazine Sept 1985 Vol. 23 No. 9 p 29-40
61*cc54cb09Sjsing  * This module in only based on the code in this paper and is
62*cc54cb09Sjsing  * almost definitely not the same as the MIT implementation.
63*cc54cb09Sjsing  */
64*cc54cb09Sjsing #include "des_local.h"
65*cc54cb09Sjsing 
66*cc54cb09Sjsing /* bug fix for dos - 7/6/91 - Larry hughes@logos.ucs.indiana.edu */
67*cc54cb09Sjsing #define Q_B0(a)	(((DES_LONG)(a)))
68*cc54cb09Sjsing #define Q_B1(a)	(((DES_LONG)(a))<<8)
69*cc54cb09Sjsing #define Q_B2(a)	(((DES_LONG)(a))<<16)
70*cc54cb09Sjsing #define Q_B3(a)	(((DES_LONG)(a))<<24)
71*cc54cb09Sjsing 
72*cc54cb09Sjsing /* used to scramble things a bit */
73*cc54cb09Sjsing /* Got the value MIT uses via brute force :-) 2/10/90 eay */
74*cc54cb09Sjsing #define NOISE	((DES_LONG)83653421L)
75*cc54cb09Sjsing 
76*cc54cb09Sjsing DES_LONG
77*cc54cb09Sjsing DES_cbc_cksum(const unsigned char *in, DES_cblock *output,
78*cc54cb09Sjsing     long length, DES_key_schedule *schedule,
79*cc54cb09Sjsing     const_DES_cblock *ivec)
80*cc54cb09Sjsing {
81*cc54cb09Sjsing 	DES_LONG tout0, tout1, tin0, tin1;
82*cc54cb09Sjsing 	long l = length;
83*cc54cb09Sjsing 	DES_LONG tin[2];
84*cc54cb09Sjsing 	unsigned char *out = &(*output)[0];
85*cc54cb09Sjsing 	const unsigned char *iv = &(*ivec)[0];
86*cc54cb09Sjsing 
87*cc54cb09Sjsing 	c2l(iv, tout0);
88*cc54cb09Sjsing 	c2l(iv, tout1);
89*cc54cb09Sjsing 	for (; l > 0; l -= 8) {
90*cc54cb09Sjsing 		if (l >= 8) {
91*cc54cb09Sjsing 			c2l(in, tin0);
92*cc54cb09Sjsing 			c2l(in, tin1);
93*cc54cb09Sjsing 		} else
94*cc54cb09Sjsing 			c2ln(in, tin0, tin1, l);
95*cc54cb09Sjsing 
96*cc54cb09Sjsing 		tin0 ^= tout0;
97*cc54cb09Sjsing 		tin[0] = tin0;
98*cc54cb09Sjsing 		tin1 ^= tout1;
99*cc54cb09Sjsing 		tin[1] = tin1;
100*cc54cb09Sjsing 		DES_encrypt1((DES_LONG *)tin, schedule, DES_ENCRYPT);
101*cc54cb09Sjsing 		/* fix 15/10/91 eay - thanks to keithr@sco.COM */
102*cc54cb09Sjsing 		tout0 = tin[0];
103*cc54cb09Sjsing 		tout1 = tin[1];
104*cc54cb09Sjsing 	}
105*cc54cb09Sjsing 	if (out != NULL) {
106*cc54cb09Sjsing 		l2c(tout0, out);
107*cc54cb09Sjsing 		l2c(tout1, out);
108*cc54cb09Sjsing 	}
109*cc54cb09Sjsing 	tout0 = tin0 = tin1 = tin[0] = tin[1] = 0;
110*cc54cb09Sjsing 	/*
111*cc54cb09Sjsing 	  Transform the data in tout1 so that it will
112*cc54cb09Sjsing 	  match the return value that the MIT Kerberos
113*cc54cb09Sjsing 	  mit_des_cbc_cksum API returns.
114*cc54cb09Sjsing 	*/
115*cc54cb09Sjsing 	tout1 = ((tout1 >> 24L) & 0x000000FF) |
116*cc54cb09Sjsing 	    ((tout1 >> 8L) & 0x0000FF00) |
117*cc54cb09Sjsing 	    ((tout1 << 8L) & 0x00FF0000) |
118*cc54cb09Sjsing 	    ((tout1 << 24L) & 0xFF000000);
119*cc54cb09Sjsing 	return (tout1);
120*cc54cb09Sjsing }
121*cc54cb09Sjsing LCRYPTO_ALIAS(DES_cbc_cksum);
122*cc54cb09Sjsing 
123*cc54cb09Sjsing DES_LONG
124*cc54cb09Sjsing DES_quad_cksum(const unsigned char *input, DES_cblock output[],
125*cc54cb09Sjsing     long length, int out_count, DES_cblock *seed)
126*cc54cb09Sjsing {
127*cc54cb09Sjsing 	DES_LONG z0, z1, t0, t1;
128*cc54cb09Sjsing 	int i;
129*cc54cb09Sjsing 	long l;
130*cc54cb09Sjsing 	const unsigned char *cp;
131*cc54cb09Sjsing 	DES_LONG *lp;
132*cc54cb09Sjsing 
133*cc54cb09Sjsing 	if (out_count < 1)
134*cc54cb09Sjsing 		out_count = 1;
135*cc54cb09Sjsing 	lp = (DES_LONG *)&(output[0])[0];
136*cc54cb09Sjsing 
137*cc54cb09Sjsing 	z0 = Q_B0((*seed)[0])|Q_B1((*seed)[1])|Q_B2((*seed)[2])|Q_B3(
138*cc54cb09Sjsing 	    (*seed)[3]);
139*cc54cb09Sjsing 	z1 = Q_B0((*seed)[4])|Q_B1((*seed)[5])|Q_B2((*seed)[6])|Q_B3(
140*cc54cb09Sjsing 	    (*seed)[7]);
141*cc54cb09Sjsing 
142*cc54cb09Sjsing 	for (i = 0; ((i < 4) && (i < out_count)); i++) {
143*cc54cb09Sjsing 		cp = input;
144*cc54cb09Sjsing 		l = length;
145*cc54cb09Sjsing 		while (l > 0) {
146*cc54cb09Sjsing 			if (l > 1) {
147*cc54cb09Sjsing 				t0 = (DES_LONG)(*(cp++));
148*cc54cb09Sjsing 				t0 |= (DES_LONG)Q_B1(*(cp++));
149*cc54cb09Sjsing 				l--;
150*cc54cb09Sjsing 			} else
151*cc54cb09Sjsing 				t0 = (DES_LONG)(*(cp++));
152*cc54cb09Sjsing 			l--;
153*cc54cb09Sjsing 			/* add */
154*cc54cb09Sjsing 			t0 += z0;
155*cc54cb09Sjsing 			t0 &= 0xffffffffL;
156*cc54cb09Sjsing 			t1 = z1;
157*cc54cb09Sjsing 			/* square, well sort of square */
158*cc54cb09Sjsing 			z0 = ((((t0*t0) & 0xffffffffL) +
159*cc54cb09Sjsing 			    ((t1*t1) & 0xffffffffL)) & 0xffffffffL) %
160*cc54cb09Sjsing 			    0x7fffffffL;
161*cc54cb09Sjsing 			z1 = ((t0*((t1 + NOISE) & 0xffffffffL)) & 0xffffffffL) %
162*cc54cb09Sjsing 			    0x7fffffffL;
163*cc54cb09Sjsing 		}
164*cc54cb09Sjsing 		if (lp != NULL) {
165*cc54cb09Sjsing 			/* The MIT library assumes that the checksum is
166*cc54cb09Sjsing 			 * composed of 2*out_count 32 bit ints */
167*cc54cb09Sjsing 			*lp++ = z0;
168*cc54cb09Sjsing 			*lp++ = z1;
169*cc54cb09Sjsing 		}
170*cc54cb09Sjsing 	}
171*cc54cb09Sjsing 	return (z0);
172*cc54cb09Sjsing }
173*cc54cb09Sjsing LCRYPTO_ALIAS(DES_quad_cksum);
174