fcrypt_b.c revision 109998
1258945Sroberto/* crypto/des/fcrypt_b.c */
2258945Sroberto/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3258945Sroberto * All rights reserved.
4258945Sroberto *
5258945Sroberto * This package is an SSL implementation written
6258945Sroberto * by Eric Young (eay@cryptsoft.com).
7258945Sroberto * The implementation was written so as to conform with Netscapes SSL.
8258945Sroberto *
9258945Sroberto * This library is free for commercial and non-commercial use as long as
10258945Sroberto * the following conditions are aheared to.  The following conditions
11258945Sroberto * apply to all code found in this distribution, be it the RC4, RSA,
12258945Sroberto * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13258945Sroberto * included with this distribution is covered by the same copyright terms
14258945Sroberto * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15258945Sroberto *
16258945Sroberto * Copyright remains Eric Young's, and as such any Copyright notices in
17258945Sroberto * the code are not to be removed.
18258945Sroberto * If this package is used in a product, Eric Young should be given attribution
19258945Sroberto * as the author of the parts of the library used.
20258945Sroberto * This can be in the form of a textual message at program startup or
21258945Sroberto * in documentation (online or textual) provided with the package.
22258945Sroberto *
23258945Sroberto * Redistribution and use in source and binary forms, with or without
24258945Sroberto * modification, are permitted provided that the following conditions
25258945Sroberto * are met:
26258945Sroberto * 1. Redistributions of source code must retain the copyright
27258945Sroberto *    notice, this list of conditions and the following disclaimer.
28258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright
29258945Sroberto *    notice, this list of conditions and the following disclaimer in the
30258945Sroberto *    documentation and/or other materials provided with the distribution.
31258945Sroberto * 3. All advertising materials mentioning features or use of this software
32258945Sroberto *    must display the following acknowledgement:
33258945Sroberto *    "This product includes cryptographic software written by
34258945Sroberto *     Eric Young (eay@cryptsoft.com)"
35258945Sroberto *    The word 'cryptographic' can be left out if the rouines from the library
36258945Sroberto *    being used are not cryptographic related :-).
37258945Sroberto * 4. If you include any Windows specific code (or a derivative thereof) from
38258945Sroberto *    the apps directory (application code) you must include an acknowledgement:
39258945Sroberto *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40258945Sroberto *
41258945Sroberto * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42258945Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43258945Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44258945Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45258945Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46258945Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47258945Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48258945Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49258945Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50258945Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51258945Sroberto * SUCH DAMAGE.
52258945Sroberto *
53258945Sroberto * The licence and distribution terms for any publically available version or
54258945Sroberto * derivative of this code cannot be changed.  i.e. this code cannot simply be
55258945Sroberto * copied and put under another distribution licence
56258945Sroberto * [including the GNU Public Licence.]
57258945Sroberto */
58258945Sroberto
59258945Sroberto#include <stdio.h>
60258945Sroberto
61258945Sroberto/* This version of crypt has been developed from my MIT compatible
62258945Sroberto * DES library.
63258945Sroberto * The library is available at pub/Crypto/DES at ftp.psy.uq.oz.au
64258945Sroberto * Eric Young (eay@cryptsoft.com)
65258945Sroberto */
66258945Sroberto
67258945Sroberto#define DES_FCRYPT
68258945Sroberto#include "des_locl.h"
69258945Sroberto#undef DES_FCRYPT
70258945Sroberto
71258945Sroberto#undef PERM_OP
72258945Sroberto#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
73258945Sroberto	(b)^=(t),\
74258945Sroberto	(a)^=((t)<<(n)))
75258945Sroberto
76258945Sroberto#undef HPERM_OP
77258945Sroberto#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
78258945Sroberto	(a)=(a)^(t)^(t>>(16-(n))))\
79258945Sroberto
80258945Srobertovoid fcrypt_body(DES_LONG *out, DES_key_schedule *ks, DES_LONG Eswap0,
81258945Sroberto		 DES_LONG Eswap1)
82258945Sroberto	{
83258945Sroberto	register DES_LONG l,r,t,u;
84258945Sroberto#ifdef DES_PTR
85258945Sroberto	register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
86258945Sroberto#endif
87258945Sroberto	register DES_LONG *s;
88258945Sroberto	register int j;
89258945Sroberto	register DES_LONG E0,E1;
90258945Sroberto
91258945Sroberto	l=0;
92258945Sroberto	r=0;
93258945Sroberto
94258945Sroberto	s=(DES_LONG *)ks;
95258945Sroberto	E0=Eswap0;
96258945Sroberto	E1=Eswap1;
97258945Sroberto
98258945Sroberto	for (j=0; j<25; j++)
99258945Sroberto		{
100258945Sroberto#ifndef DES_UNROLL
101258945Sroberto		register int i;
102258945Sroberto
103258945Sroberto		for (i=0; i<32; i+=8)
104258945Sroberto			{
105258945Sroberto			D_ENCRYPT(l,r,i+0); /*  1 */
106258945Sroberto			D_ENCRYPT(r,l,i+2); /*  2 */
107258945Sroberto			D_ENCRYPT(l,r,i+4); /*  1 */
108258945Sroberto			D_ENCRYPT(r,l,i+6); /*  2 */
109258945Sroberto			}
110258945Sroberto#else
111258945Sroberto		D_ENCRYPT(l,r, 0); /*  1 */
112258945Sroberto		D_ENCRYPT(r,l, 2); /*  2 */
113258945Sroberto		D_ENCRYPT(l,r, 4); /*  3 */
114258945Sroberto		D_ENCRYPT(r,l, 6); /*  4 */
115258945Sroberto		D_ENCRYPT(l,r, 8); /*  5 */
116258945Sroberto		D_ENCRYPT(r,l,10); /*  6 */
117258945Sroberto		D_ENCRYPT(l,r,12); /*  7 */
118258945Sroberto		D_ENCRYPT(r,l,14); /*  8 */
119258945Sroberto		D_ENCRYPT(l,r,16); /*  9 */
120258945Sroberto		D_ENCRYPT(r,l,18); /*  10 */
121258945Sroberto		D_ENCRYPT(l,r,20); /*  11 */
122258945Sroberto		D_ENCRYPT(r,l,22); /*  12 */
123258945Sroberto		D_ENCRYPT(l,r,24); /*  13 */
124258945Sroberto		D_ENCRYPT(r,l,26); /*  14 */
125258945Sroberto		D_ENCRYPT(l,r,28); /*  15 */
126258945Sroberto		D_ENCRYPT(r,l,30); /*  16 */
127258945Sroberto#endif
128258945Sroberto
129258945Sroberto		t=l;
130258945Sroberto		l=r;
131258945Sroberto		r=t;
132258945Sroberto		}
133258945Sroberto	l=ROTATE(l,3)&0xffffffffL;
134258945Sroberto	r=ROTATE(r,3)&0xffffffffL;
135258945Sroberto
136258945Sroberto	PERM_OP(l,r,t, 1,0x55555555L);
137258945Sroberto	PERM_OP(r,l,t, 8,0x00ff00ffL);
138258945Sroberto	PERM_OP(l,r,t, 2,0x33333333L);
139258945Sroberto	PERM_OP(r,l,t,16,0x0000ffffL);
140258945Sroberto	PERM_OP(l,r,t, 4,0x0f0f0f0fL);
141258945Sroberto
142258945Sroberto	out[0]=r;
143258945Sroberto	out[1]=l;
144258945Sroberto	}
145258945Sroberto
146258945Sroberto