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