1 /* crypto/des/ecb_enc.c */ 2 /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au) 3 * All rights reserved. 4 * 5 * This file is part of an SSL implementation written 6 * by Eric Young (eay@mincom.oz.au). 7 * The implementation was written so as to conform with Netscapes SSL 8 * specification. This library and applications are 9 * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE 10 * as long as the following conditions are aheared to. 11 * 12 * Copyright remains Eric Young's, and as such any Copyright notices in 13 * the code are not to be removed. If this code is used in a product, 14 * Eric Young should be given attribution as the author of the parts used. 15 * This can be in the form of a textual message at program startup or 16 * in documentation (online or textual) provided with the package. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 3. All advertising materials mentioning features or use of this software 27 * must display the following acknowledgement: 28 * This product includes software developed by Eric Young (eay@mincom.oz.au) 29 * 30 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * The licence and distribution terms for any publically available version or 43 * derivative of this code cannot be changed. i.e. this code cannot simply be 44 * copied and put under another distribution licence 45 * [including the GNU Public Licence.] 46 * 47 * $FreeBSD$ 48 */ 49 50 #include <crypto/des/des_locl.h> 51 #include <crypto/des/spr.h> 52 53 char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay"; 54 char *DES_version="DES part of SSLeay 0.6.4 30-Aug-1996"; 55 56 char *des_options() 57 { 58 #ifdef DES_PTR 59 if (sizeof(DES_LONG) != sizeof(long)) 60 return("des(ptr,int)"); 61 else 62 return("des(ptr,long)"); 63 #else 64 if (sizeof(DES_LONG) != sizeof(long)) 65 return("des(idx,int)"); 66 else 67 return("des(idx,long)"); 68 #endif 69 } 70 71 72 void des_ecb_encrypt(input, output, ks, encrypt) 73 des_cblock (*input); 74 des_cblock (*output); 75 des_key_schedule ks; 76 int encrypt; 77 { 78 register DES_LONG l; 79 register unsigned char *in,*out; 80 DES_LONG ll[2]; 81 82 in=(unsigned char *)input; 83 out=(unsigned char *)output; 84 c2l(in,l); ll[0]=l; 85 c2l(in,l); ll[1]=l; 86 des_encrypt(ll,ks,encrypt); 87 l=ll[0]; l2c(l,out); 88 l=ll[1]; l2c(l,out); 89 l=ll[0]=ll[1]=0; 90 } 91 92 void des_encrypt(data, ks, encrypt) 93 DES_LONG *data; 94 des_key_schedule ks; 95 int encrypt; 96 { 97 register DES_LONG l,r,t,u; 98 #ifdef DES_PTR 99 register unsigned char *des_SP=(unsigned char *)des_SPtrans; 100 #endif 101 #ifdef undef 102 union fudge { 103 DES_LONG l; 104 unsigned short s[2]; 105 unsigned char c[4]; 106 } U,T; 107 #endif 108 register int i; 109 register DES_LONG *s; 110 111 u=data[0]; 112 r=data[1]; 113 114 IP(u,r); 115 /* Things have been modified so that the initial rotate is 116 * done outside the loop. This required the 117 * des_SPtrans values in sp.h to be rotated 1 bit to the right. 118 * One perl script later and things have a 5% speed up on a sparc2. 119 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> 120 * for pointing this out. */ 121 l=(r<<1)|(r>>31); 122 r=(u<<1)|(u>>31); 123 124 /* clear the top bits on machines with 8byte longs */ 125 l&=0xffffffffL; 126 r&=0xffffffffL; 127 128 s=(DES_LONG *)ks; 129 /* I don't know if it is worth the effort of loop unrolling the 130 * inner loop 131 */ 132 if (encrypt) 133 { 134 for (i=0; i<32; i+=8) 135 { 136 D_ENCRYPT(l,r,i+0); /* 1 */ 137 D_ENCRYPT(r,l,i+2); /* 2 */ 138 D_ENCRYPT(l,r,i+4); /* 3 */ 139 D_ENCRYPT(r,l,i+6); /* 4 */ 140 } 141 } 142 else 143 { 144 for (i=30; i>0; i-=8) 145 { 146 D_ENCRYPT(l,r,i-0); /* 16 */ 147 D_ENCRYPT(r,l,i-2); /* 15 */ 148 D_ENCRYPT(l,r,i-4); /* 14 */ 149 D_ENCRYPT(r,l,i-6); /* 13 */ 150 } 151 } 152 l=(l>>1)|(l<<31); 153 r=(r>>1)|(r<<31); 154 /* clear the top bits on machines with 8byte longs */ 155 l&=0xffffffffL; 156 r&=0xffffffffL; 157 158 FP(r,l); 159 data[0]=l; 160 data[1]=r; 161 l=r=t=u=0; 162 } 163 164 void des_encrypt2(data, ks, encrypt) 165 DES_LONG *data; 166 des_key_schedule ks; 167 int encrypt; 168 { 169 register DES_LONG l,r,t,u; 170 #ifdef DES_PTR 171 register unsigned char *des_SP=(unsigned char *)des_SPtrans; 172 #endif 173 #ifdef undef 174 union fudge { 175 DES_LONG l; 176 unsigned short s[2]; 177 unsigned char c[4]; 178 } U,T; 179 #endif 180 register int i; 181 register DES_LONG *s; 182 183 u=data[0]; 184 r=data[1]; 185 186 /* Things have been modified so that the initial rotate is 187 * done outside the loop. This required the 188 * des_SPtrans values in sp.h to be rotated 1 bit to the right. 189 * One perl script later and things have a 5% speed up on a sparc2. 190 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> 191 * for pointing this out. */ 192 l=(r<<1)|(r>>31); 193 r=(u<<1)|(u>>31); 194 195 /* clear the top bits on machines with 8byte longs */ 196 l&=0xffffffffL; 197 r&=0xffffffffL; 198 199 s=(DES_LONG *)ks; 200 /* I don't know if it is worth the effort of loop unrolling the 201 * inner loop */ 202 if (encrypt) 203 { 204 for (i=0; i<32; i+=8) 205 { 206 D_ENCRYPT(l,r,i+0); /* 1 */ 207 D_ENCRYPT(r,l,i+2); /* 2 */ 208 D_ENCRYPT(l,r,i+4); /* 3 */ 209 D_ENCRYPT(r,l,i+6); /* 4 */ 210 } 211 } 212 else 213 { 214 for (i=30; i>0; i-=8) 215 { 216 D_ENCRYPT(l,r,i-0); /* 16 */ 217 D_ENCRYPT(r,l,i-2); /* 15 */ 218 D_ENCRYPT(l,r,i-4); /* 14 */ 219 D_ENCRYPT(r,l,i-6); /* 13 */ 220 } 221 } 222 l=(l>>1)|(l<<31); 223 r=(r>>1)|(r<<31); 224 /* clear the top bits on machines with 8byte longs */ 225 l&=0xffffffffL; 226 r&=0xffffffffL; 227 228 data[0]=l; 229 data[1]=r; 230 l=r=t=u=0; 231 } 232