1 1.1 tls /* crypto/des/des_enc.c */ 2 1.1 tls /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 3 1.1 tls * All rights reserved. 4 1.1 tls * 5 1.1 tls * This package is an SSL implementation written 6 1.1 tls * by Eric Young (eay (at) cryptsoft.com). 7 1.1 tls * The implementation was written so as to conform with Netscapes SSL. 8 1.3 perry * 9 1.1 tls * This library is free for commercial and non-commercial use as long as 10 1.1 tls * the following conditions are aheared to. The following conditions 11 1.1 tls * apply to all code found in this distribution, be it the RC4, RSA, 12 1.1 tls * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 1.1 tls * included with this distribution is covered by the same copyright terms 14 1.1 tls * except that the holder is Tim Hudson (tjh (at) cryptsoft.com). 15 1.3 perry * 16 1.1 tls * Copyright remains Eric Young's, and as such any Copyright notices in 17 1.1 tls * the code are not to be removed. 18 1.1 tls * If this package is used in a product, Eric Young should be given attribution 19 1.1 tls * as the author of the parts of the library used. 20 1.1 tls * This can be in the form of a textual message at program startup or 21 1.1 tls * in documentation (online or textual) provided with the package. 22 1.3 perry * 23 1.1 tls * Redistribution and use in source and binary forms, with or without 24 1.1 tls * modification, are permitted provided that the following conditions 25 1.1 tls * are met: 26 1.1 tls * 1. Redistributions of source code must retain the copyright 27 1.1 tls * notice, this list of conditions and the following disclaimer. 28 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright 29 1.1 tls * notice, this list of conditions and the following disclaimer in the 30 1.1 tls * documentation and/or other materials provided with the distribution. 31 1.1 tls * 3. All advertising materials mentioning features or use of this software 32 1.1 tls * must display the following acknowledgement: 33 1.1 tls * "This product includes cryptographic software written by 34 1.1 tls * Eric Young (eay (at) cryptsoft.com)" 35 1.1 tls * The word 'cryptographic' can be left out if the rouines from the library 36 1.1 tls * being used are not cryptographic related :-). 37 1.3 perry * 4. If you include any Windows specific code (or a derivative thereof) from 38 1.1 tls * the apps directory (application code) you must include an acknowledgement: 39 1.1 tls * "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)" 40 1.3 perry * 41 1.1 tls * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 1.1 tls * SUCH DAMAGE. 52 1.3 perry * 53 1.1 tls * The licence and distribution terms for any publically available version or 54 1.1 tls * derivative of this code cannot be changed. i.e. this code cannot simply be 55 1.1 tls * copied and put under another distribution licence 56 1.1 tls * [including the GNU Public Licence.] 57 1.1 tls */ 58 1.1 tls 59 1.2 lukem #include <sys/cdefs.h> 60 1.4 christos __KERNEL_RCSID(0, "$NetBSD: des_enc.c,v 1.4 2005/12/11 12:20:52 christos Exp $"); 61 1.2 lukem 62 1.1 tls #include <sys/types.h> 63 1.1 tls #include <crypto/des/des_locl.h> 64 1.1 tls 65 1.1 tls extern const DES_LONG des_SPtrans[8][64]; 66 1.1 tls 67 1.1 tls void des_encrypt1(DES_LONG *data, des_key_schedule ks, int enc) 68 1.1 tls { 69 1.1 tls register DES_LONG l,r,t,u; 70 1.1 tls #ifdef DES_PTR 71 1.1 tls register const unsigned char *des_SP=(const unsigned char *)des_SPtrans; 72 1.1 tls #endif 73 1.1 tls #ifndef DES_UNROLL 74 1.1 tls register int i; 75 1.1 tls #endif 76 1.1 tls register DES_LONG *s; 77 1.1 tls 78 1.1 tls r=data[0]; 79 1.1 tls l=data[1]; 80 1.1 tls 81 1.1 tls IP(r,l); 82 1.1 tls /* Things have been modified so that the initial rotate is 83 1.1 tls * done outside the loop. This required the 84 1.1 tls * des_SPtrans values in sp.h to be rotated 1 bit to the right. 85 1.1 tls * One perl script later and things have a 5% speed up on a sparc2. 86 1.1 tls * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM> 87 1.1 tls * for pointing this out. */ 88 1.1 tls /* clear the top bits on machines with 8byte longs */ 89 1.1 tls /* shift left by 2 */ 90 1.1 tls r=ROTATE(r,29)&0xffffffffL; 91 1.1 tls l=ROTATE(l,29)&0xffffffffL; 92 1.1 tls 93 1.1 tls s=ks->ks.deslong; 94 1.1 tls /* I don't know if it is worth the effort of loop unrolling the 95 1.1 tls * inner loop */ 96 1.1 tls if (enc) 97 1.1 tls { 98 1.1 tls #ifdef DES_UNROLL 99 1.1 tls D_ENCRYPT(l,r, 0); /* 1 */ 100 1.1 tls D_ENCRYPT(r,l, 2); /* 2 */ 101 1.1 tls D_ENCRYPT(l,r, 4); /* 3 */ 102 1.1 tls D_ENCRYPT(r,l, 6); /* 4 */ 103 1.1 tls D_ENCRYPT(l,r, 8); /* 5 */ 104 1.1 tls D_ENCRYPT(r,l,10); /* 6 */ 105 1.1 tls D_ENCRYPT(l,r,12); /* 7 */ 106 1.1 tls D_ENCRYPT(r,l,14); /* 8 */ 107 1.1 tls D_ENCRYPT(l,r,16); /* 9 */ 108 1.1 tls D_ENCRYPT(r,l,18); /* 10 */ 109 1.1 tls D_ENCRYPT(l,r,20); /* 11 */ 110 1.1 tls D_ENCRYPT(r,l,22); /* 12 */ 111 1.1 tls D_ENCRYPT(l,r,24); /* 13 */ 112 1.1 tls D_ENCRYPT(r,l,26); /* 14 */ 113 1.1 tls D_ENCRYPT(l,r,28); /* 15 */ 114 1.1 tls D_ENCRYPT(r,l,30); /* 16 */ 115 1.1 tls #else 116 1.1 tls for (i=0; i<32; i+=8) 117 1.1 tls { 118 1.1 tls D_ENCRYPT(l,r,i+0); /* 1 */ 119 1.1 tls D_ENCRYPT(r,l,i+2); /* 2 */ 120 1.1 tls D_ENCRYPT(l,r,i+4); /* 3 */ 121 1.1 tls D_ENCRYPT(r,l,i+6); /* 4 */ 122 1.1 tls } 123 1.1 tls #endif 124 1.1 tls } 125 1.1 tls else 126 1.1 tls { 127 1.1 tls #ifdef DES_UNROLL 128 1.1 tls D_ENCRYPT(l,r,30); /* 16 */ 129 1.1 tls D_ENCRYPT(r,l,28); /* 15 */ 130 1.1 tls D_ENCRYPT(l,r,26); /* 14 */ 131 1.1 tls D_ENCRYPT(r,l,24); /* 13 */ 132 1.1 tls D_ENCRYPT(l,r,22); /* 12 */ 133 1.1 tls D_ENCRYPT(r,l,20); /* 11 */ 134 1.1 tls D_ENCRYPT(l,r,18); /* 10 */ 135 1.1 tls D_ENCRYPT(r,l,16); /* 9 */ 136 1.1 tls D_ENCRYPT(l,r,14); /* 8 */ 137 1.1 tls D_ENCRYPT(r,l,12); /* 7 */ 138 1.1 tls D_ENCRYPT(l,r,10); /* 6 */ 139 1.1 tls D_ENCRYPT(r,l, 8); /* 5 */ 140 1.1 tls D_ENCRYPT(l,r, 6); /* 4 */ 141 1.1 tls D_ENCRYPT(r,l, 4); /* 3 */ 142 1.1 tls D_ENCRYPT(l,r, 2); /* 2 */ 143 1.1 tls D_ENCRYPT(r,l, 0); /* 1 */ 144 1.1 tls #else 145 1.1 tls for (i=30; i>0; i-=8) 146 1.1 tls { 147 1.1 tls D_ENCRYPT(l,r,i-0); /* 16 */ 148 1.1 tls D_ENCRYPT(r,l,i-2); /* 15 */ 149 1.1 tls D_ENCRYPT(l,r,i-4); /* 14 */ 150 1.1 tls D_ENCRYPT(r,l,i-6); /* 13 */ 151 1.1 tls } 152 1.1 tls #endif 153 1.1 tls } 154 1.1 tls 155 1.1 tls /* rotate and clear the top bits on machines with 8byte longs */ 156 1.1 tls l=ROTATE(l,3)&0xffffffffL; 157 1.1 tls r=ROTATE(r,3)&0xffffffffL; 158 1.1 tls 159 1.1 tls FP(r,l); 160 1.1 tls data[0]=l; 161 1.1 tls data[1]=r; 162 1.1 tls l=r=t=u=0; 163 1.1 tls } 164 1.1 tls 165 1.1 tls void des_encrypt2(DES_LONG *data, des_key_schedule ks, int enc) 166 1.1 tls { 167 1.1 tls register DES_LONG l,r,t,u; 168 1.1 tls #ifdef DES_PTR 169 1.1 tls register const unsigned char *des_SP=(const unsigned char *)des_SPtrans; 170 1.1 tls #endif 171 1.1 tls #ifndef DES_UNROLL 172 1.1 tls register int i; 173 1.1 tls #endif 174 1.1 tls register DES_LONG *s; 175 1.1 tls 176 1.1 tls r=data[0]; 177 1.1 tls l=data[1]; 178 1.1 tls 179 1.1 tls /* Things have been modified so that the initial rotate is 180 1.1 tls * done outside the loop. This required the 181 1.1 tls * des_SPtrans values in sp.h to be rotated 1 bit to the right. 182 1.1 tls * One perl script later and things have a 5% speed up on a sparc2. 183 1.1 tls * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM> 184 1.1 tls * for pointing this out. */ 185 1.1 tls /* clear the top bits on machines with 8byte longs */ 186 1.1 tls r=ROTATE(r,29)&0xffffffffL; 187 1.1 tls l=ROTATE(l,29)&0xffffffffL; 188 1.1 tls 189 1.1 tls s=ks->ks.deslong; 190 1.1 tls /* I don't know if it is worth the effort of loop unrolling the 191 1.1 tls * inner loop */ 192 1.1 tls if (enc) 193 1.1 tls { 194 1.1 tls #ifdef DES_UNROLL 195 1.1 tls D_ENCRYPT(l,r, 0); /* 1 */ 196 1.1 tls D_ENCRYPT(r,l, 2); /* 2 */ 197 1.1 tls D_ENCRYPT(l,r, 4); /* 3 */ 198 1.1 tls D_ENCRYPT(r,l, 6); /* 4 */ 199 1.1 tls D_ENCRYPT(l,r, 8); /* 5 */ 200 1.1 tls D_ENCRYPT(r,l,10); /* 6 */ 201 1.1 tls D_ENCRYPT(l,r,12); /* 7 */ 202 1.1 tls D_ENCRYPT(r,l,14); /* 8 */ 203 1.1 tls D_ENCRYPT(l,r,16); /* 9 */ 204 1.1 tls D_ENCRYPT(r,l,18); /* 10 */ 205 1.1 tls D_ENCRYPT(l,r,20); /* 11 */ 206 1.1 tls D_ENCRYPT(r,l,22); /* 12 */ 207 1.1 tls D_ENCRYPT(l,r,24); /* 13 */ 208 1.1 tls D_ENCRYPT(r,l,26); /* 14 */ 209 1.1 tls D_ENCRYPT(l,r,28); /* 15 */ 210 1.1 tls D_ENCRYPT(r,l,30); /* 16 */ 211 1.1 tls #else 212 1.1 tls for (i=0; i<32; i+=8) 213 1.1 tls { 214 1.1 tls D_ENCRYPT(l,r,i+0); /* 1 */ 215 1.1 tls D_ENCRYPT(r,l,i+2); /* 2 */ 216 1.1 tls D_ENCRYPT(l,r,i+4); /* 3 */ 217 1.1 tls D_ENCRYPT(r,l,i+6); /* 4 */ 218 1.1 tls } 219 1.1 tls #endif 220 1.1 tls } 221 1.1 tls else 222 1.1 tls { 223 1.1 tls #ifdef DES_UNROLL 224 1.1 tls D_ENCRYPT(l,r,30); /* 16 */ 225 1.1 tls D_ENCRYPT(r,l,28); /* 15 */ 226 1.1 tls D_ENCRYPT(l,r,26); /* 14 */ 227 1.1 tls D_ENCRYPT(r,l,24); /* 13 */ 228 1.1 tls D_ENCRYPT(l,r,22); /* 12 */ 229 1.1 tls D_ENCRYPT(r,l,20); /* 11 */ 230 1.1 tls D_ENCRYPT(l,r,18); /* 10 */ 231 1.1 tls D_ENCRYPT(r,l,16); /* 9 */ 232 1.1 tls D_ENCRYPT(l,r,14); /* 8 */ 233 1.1 tls D_ENCRYPT(r,l,12); /* 7 */ 234 1.1 tls D_ENCRYPT(l,r,10); /* 6 */ 235 1.1 tls D_ENCRYPT(r,l, 8); /* 5 */ 236 1.1 tls D_ENCRYPT(l,r, 6); /* 4 */ 237 1.1 tls D_ENCRYPT(r,l, 4); /* 3 */ 238 1.1 tls D_ENCRYPT(l,r, 2); /* 2 */ 239 1.1 tls D_ENCRYPT(r,l, 0); /* 1 */ 240 1.1 tls #else 241 1.1 tls for (i=30; i>0; i-=8) 242 1.1 tls { 243 1.1 tls D_ENCRYPT(l,r,i-0); /* 16 */ 244 1.1 tls D_ENCRYPT(r,l,i-2); /* 15 */ 245 1.1 tls D_ENCRYPT(l,r,i-4); /* 14 */ 246 1.1 tls D_ENCRYPT(r,l,i-6); /* 13 */ 247 1.1 tls } 248 1.1 tls #endif 249 1.1 tls } 250 1.1 tls /* rotate and clear the top bits on machines with 8byte longs */ 251 1.1 tls data[0]=ROTATE(l,3)&0xffffffffL; 252 1.1 tls data[1]=ROTATE(r,3)&0xffffffffL; 253 1.1 tls l=r=t=u=0; 254 1.1 tls } 255 1.1 tls 256 1.1 tls void des_encrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2, 257 1.1 tls des_key_schedule ks3) 258 1.1 tls { 259 1.1 tls register DES_LONG l,r; 260 1.1 tls 261 1.1 tls l=data[0]; 262 1.1 tls r=data[1]; 263 1.1 tls IP(l,r); 264 1.1 tls data[0]=l; 265 1.1 tls data[1]=r; 266 1.1 tls des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT); 267 1.1 tls des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT); 268 1.1 tls des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT); 269 1.1 tls l=data[0]; 270 1.1 tls r=data[1]; 271 1.1 tls FP(r,l); 272 1.1 tls data[0]=l; 273 1.1 tls data[1]=r; 274 1.1 tls } 275 1.1 tls 276 1.1 tls void des_decrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2, 277 1.1 tls des_key_schedule ks3) 278 1.1 tls { 279 1.1 tls register DES_LONG l,r; 280 1.1 tls 281 1.1 tls l=data[0]; 282 1.1 tls r=data[1]; 283 1.1 tls IP(l,r); 284 1.1 tls data[0]=l; 285 1.1 tls data[1]=r; 286 1.1 tls des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT); 287 1.1 tls des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT); 288 1.1 tls des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT); 289 1.1 tls l=data[0]; 290 1.1 tls r=data[1]; 291 1.1 tls FP(r,l); 292 1.1 tls data[0]=l; 293 1.1 tls data[1]=r; 294 1.1 tls } 295