ada021688b500fcc23507e9d6b00ac00aed0720d
[openafs.git] / src / des / des.c
1 /*
2  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
3  * of Technology.
4  *
5  * For copying and distribution information, please see the file
6  * <mit-cpyright.h>.
7  *
8  * These routines perform encryption and decryption using the DES
9  * private key algorithm, or else a subset of it-- fewer inner loops.
10  * (AUTH_DES_ITER defaults to 16, may be less.)
11  *
12  * Under U.S. law, this software may not be exported outside the US
13  * without license from the U.S. Commerce department.
14  *
15  * The key schedule is passed as an arg, as well as the cleartext or
16  * ciphertext.
17  *
18  * All registers labeled imply Vax using the Ultrix or 4.2bsd
19  * compiler.
20  *
21  *
22  *      NOTE:  bit and byte numbering:
23  *                      DES algorithm is defined in terms of bits of L
24  *                      followed by bits of R.
25
26  *              bit 0  ==> lsb of L
27  *              bit 63 ==> msb of R
28  *
29  * Always work in register pairs, FROM L1,R1 TO L2,R2 to make
30  * bookkeeping easier.
31  *
32  * originally written by Steve Miller, MIT Project Athena
33  */
34 #include <mit-cpyright.h>
35
36 #include <afsconfig.h>
37 #include <afs/param.h>
38
39 RCSID
40     ("$Header$");
41
42 #ifndef KERNEL
43 #include <stdio.h>
44 #endif
45 #ifdef AFS_PTHREAD_ENV
46 #include <pthread.h>
47 #endif /* AFS_PTHREAD_ENV */
48 #include <des.h>
49 #include "des_internal.h"
50 #include "s_table.h"
51 #ifdef BIG
52 #include "p_table.h"
53 #endif
54 #include "stats.h"
55
56 #include "des_prototypes.h"
57
58 #define XPRT_DES
59
60 #ifdef DEBUG
61 #define DBG_PRINT(s) if (des_debug & 2) \
62     des_debug_print(s,i,L1&0xffff,(L1>>16)&0xffff, \
63                 R1&0xffff,(R1>>16)&0xffff)
64 #else
65 #define DBG_PRINT(s)
66 #endif
67
68 #ifdef AFS_PTHREAD_ENV
69 pthread_mutex_t rxkad_stats_mutex;
70 #endif /* AFS_PTHREAD_ENV */
71
72 /* encrypt == 0  ==> decrypt, else encrypt */
73
74 afs_int32
75 des_ecb_encrypt(void * clear, void * cipher,
76                 register des_key_schedule schedule, int encrypt)
77 {
78     /* better pass 8 bytes, length not checked here */
79
80     register afs_uint32 R1 = 0;
81     register afs_uint32 L1 = 0; /* R1 = r10, L1 = r9 */
82     register afs_uint32 R2 = 0, L2 = 0; /* R2 = r8, L2 = r7 */
83     afs_int32 i;
84     /* one more registers left on VAX, see below P_temp_p */
85 #ifdef BITS16
86     sbox_in_16_a S_in_16_a;
87     sbox_in_16_b S_in_16_b;
88     sbox_in_16_c S_in_16_c;
89     unsigned int *S_in_a_16_p = (unsigned int *)&S_in_16_a;
90     unsigned int *S_in_b_16_p = (unsigned int *)&S_in_16_b;
91     unsigned int *S_in_c_16_p = (unsigned int *)&S_in_16_c;
92 #endif
93 #ifndef BITS32
94 #ifndef BITS16
95 #error dunno how to do this machine type, you lose;
96 #endif
97 #endif
98     afs_uint32 P_temp;
99     register unsigned char *P_temp_p = (unsigned char *)&P_temp;
100 #ifdef BITS16
101     sbox_out S_out;
102     afs_uint32 *S_out_p = (afs_uint32 *) & S_out;
103 #endif
104     afs_uint32 R_save, L_save;
105 #ifdef DEBUG
106     afs_uint32 dbg_tmp[2];
107 #endif
108     LOCK_RXKAD_STATS;
109     if (encrypt)
110         rxkad_stats.des_encrypts[DES_ENCRYPT]++;
111     else
112         rxkad_stats.des_encrypts[DES_DECRYPT]++;
113     UNLOCK_RXKAD_STATS;
114     /*
115      * Use L1,R1 and L2,R2 as two sets of "64-bit" registers always
116      * work from L1,R1 input to L2,R2 output; initialize the cleartext
117      * into registers.
118      */
119 #ifdef MUSTALIGN
120 #ifdef DEBUG
121     /*
122      * If the alignment is wrong, the programmer really screwed up --
123      * we aren't even getting the right data type.  His problem.  Keep
124      * this code for debugging.
125      */
126     /* Make sure schedule is ok */
127     if ((afs_int32) schedule & 3) {
128         fprintf(stderr, "des.c schedule arg pointer not aligned\n");
129         abort();
130     }
131 #endif
132     if ((afs_int32) clear & 3) {
133         clear=((char*)clear)+1;
134         memcpy((char *)(&L_save), (char *)clear, sizeof(L_save));
135         memcpy((char *)(&R_save), (char *)clear, sizeof(R_save));
136         L1 = L_save;
137         R1 = R_save;
138     } else
139 #endif
140     {
141         if (clear)
142             L1 = (*((afs_int32 *)clear))++;
143         else
144             L1 = 0;
145         if (clear)
146             R1 = *((afs_int32 *)clear);
147         else
148             R1 = 0;
149     }
150
151 #ifdef DEBUG
152     if (des_debug & 2) {
153         printf("All values printed from low byte (bit 0)");
154         printf(" --> high byte (bit 63)\n");
155         i = 0;
156         dbg_tmp[0] = L1;
157         dbg_tmp[1] = R1;
158         printf("iter = %2d  before IP\n\t\tL1 R1 = ", i);
159         des_cblock_print_file(dbg_tmp, stdout);
160     }
161
162     DBG_PRINT("before IP");
163 #endif
164
165 /*   IP_start:*/
166
167     /* all the Initial Permutation code is in the include file */
168 #include "ip.c"
169     /* reset input to L1,R1 */
170     L1 = L2;
171     R1 = R2;
172
173     /* iterate through the inner loop */
174     for (i = 0; i <= (AUTH_DES_ITER - 1); i++) {
175
176 #ifdef DEBUG
177         if (des_debug & 2) {
178             dbg_tmp[0] = L1;
179             dbg_tmp[1] = R1;
180             printf("iter = %2d  start loop\n\t\tL1 R1 = ", i);
181             des_cblock_print_file(dbg_tmp, stdout);
182             DBG_PRINT("start loop");
183         }
184 #endif
185
186         R_save = R1;
187         L_save = L1;
188
189 /*   E_start:*/
190         /* apply the E permutation from R1 to L2, R2 */
191 #ifndef VAXASM
192 #ifdef SLOW_E
193 #include "e.c"
194 #else /* Bill's fast E */
195         L2 = (R1 << 1);
196         if (R1 & (1 << 31))
197             L2 |= 1 << 0;
198         L2 &= 077;
199         L2 |= (R1 << 3) & 07700;
200         L2 |= (R1 << 5) & 0770000;
201         L2 |= (R1 << 7) & 077000000;
202         L2 |= (R1 << 9) & 07700000000;
203         L2 |= (R1 << 11) & 030000000000;
204
205         /* now from right to right */
206
207         R2 = ((R1 >> 17) & 0176000);
208         if (R1 & (1 << 0))
209             R2 |= 1 << 15;
210
211         R2 |= ((R1 >> 21) & 017);
212         R2 |= ((R1 >> 19) & 01760);
213 #endif /* SLOW_E */
214 #else /* VAXASM */
215         /* E operations */
216         /* right to left */
217         asm("   rotl    $1,r10,r7");
218         L2 &= 077;
219         L2 |= (R1 << 3) & 07700;
220         L2 |= (R1 << 5) & 0770000;
221         L2 |= (R1 << 7) & 077000000;
222         L2 |= (R1 << 9) & 07700000000;
223         L2 |= (R1 << 11) & 030000000000;
224
225         asm("   rotl    $-17,r10,r8");
226         R2 &= 0176000;
227         asm("   rotl    $-21,r10,r0");
228         asm("   bicl2   $-16,r0");
229         asm("  bisl2    r0,r8");
230         asm("   rotl    $-19,r10,r0");
231         asm("   bicl2   $-1009,r0");
232         asm("  bisl2    r0,r8");
233
234 #endif
235
236         /* reset input to L1,R1 */
237         L1 = L2;
238         R1 = R2;
239
240 #ifdef DEBUG
241         if (des_debug & 2) {
242             dbg_tmp[0] = L1;
243             dbg_tmp[1] = R1;
244             DBG_PRINT("after e");
245             printf("iter = %2d  after e\n\t\tL1 R1 = ", i);
246             des_cblock_print_file(dbg_tmp, stdout);
247         }
248 #endif
249
250 /*   XOR_start:*/
251         /*
252          * XOR with the key schedule, "schedule"
253          *
254          * If this is an encryption operation, use schedule[i],
255          * otherwise use schedule [AUTH_DES_ITER-i-1]
256          *
257          * First XOR left half.
258          */
259         if (encrypt) {
260             L1 ^= *(((afs_uint32 *) & schedule[i]) + 0);
261             /* now right half */
262             R1 ^= *(((afs_uint32 *) & schedule[i]) + 1);
263         } else {
264             L1 ^= *(((afs_uint32 *) & schedule[AUTH_DES_ITER - i - 1]) + 0);
265             /* now right half */
266             R1 ^= *(((afs_uint32 *) & schedule[AUTH_DES_ITER - i - 1]) + 1);
267         }
268
269         /* dont have to reset input to L1, R1 */
270
271 #ifdef DEBUG
272         if (des_debug & 2) {
273             dbg_tmp[0] = L1;
274             dbg_tmp[1] = R1;
275             DBG_PRINT("after xor");
276             printf("iter = %2d  after xor\n\t\tL1 R1 =", i);
277             des_cblock_print_file(dbg_tmp, stdout);
278         }
279 #endif
280
281 /*   S_start:*/
282         /* apply the S selection from L1, R1 to R2 */
283
284 #ifdef notdef
285 #include "s.c"
286 #endif
287
288         /* S operations , cant use registers for bit field stuff */
289         /* from S_in to S_out */
290
291 #ifdef BITS16
292         *S_in_a_16_p = L1 & 0xffff;
293         *S_in_b_16_p = (L1 >> 16) & 0xffff;
294         *S_in_c_16_p = R1 & 0xffff;
295         (*(afs_uint32 *) & S_out) = (unsigned)S_adj[0][S_in_16_a.b0];
296         S_out.b1 = (unsigned)S_adj[1][S_in_16_a.b1];
297         /* b2 spans two words */
298         S_out.b2 = (unsigned)
299             S_adj[2][(unsigned)S_in_16_a.b2 +
300                      (((unsigned)S_in_16_b.b2) << 4)];
301         S_out.b3 = (unsigned)S_adj[3][S_in_16_b.b3];
302         S_out.b4 = (unsigned)S_adj[4][S_in_16_b.b4];
303         /* b5 spans both parts */
304         S_out.b5 = (unsigned)
305             S_adj[5][(unsigned)S_in_16_b.b5 +
306                      (((unsigned)S_in_16_c.b5) << 2)];
307         S_out.b6 = (unsigned)S_adj[6][S_in_16_c.b6];
308         S_out.b7 = (unsigned)S_adj[7][S_in_16_c.b7];
309         R1 = *S_out_p;
310 #else
311         /* is a 32 bit sys */
312 #ifndef VAXASM
313         R2 = (unsigned)S_adj[0][L1 & 077];
314         L2 = (unsigned)S_adj[1][(L1 >> 6) & 077];
315         R2 |= (L2 << 4);
316         L2 = (unsigned)S_adj[2][(L1 >> 12) & 077];
317         R2 |= (L2 << 8);
318         L2 = (unsigned)S_adj[3][(L1 >> 18) & 077];
319         R2 |= (L2 << 12);
320         L2 = (unsigned)S_adj[4][(L1 >> 24) & 077];
321         R2 |= (L2 << 16);
322         /* b5 spans both parts */
323         L2 = (unsigned)
324             S_adj[5][(unsigned)((L1 >> 30) & 03) + ((R1 & 017) << 2)];
325         R2 |= (L2 << 20);
326         L2 = (unsigned)S_adj[6][(R1 >> 4) & 077];
327         R2 |= (L2 << 24);
328         L2 = (unsigned)S_adj[7][(R1 >> 10) & 077];
329         R1 = R2 | (L2 << 28);
330         /* reset input to L1, R1 */
331 #else /* vaxasm */
332         /*
333          * this is the c code produced above, with
334          * extzv replaced by rotl
335          */
336         asm("bicl3      $-64,r9,r0");
337         asm("movzbl     _S_adj[r0],r8");
338         asm("rotl       $-6,r9,r0");
339         asm("bicl2      $-64,r0");
340         asm("movzbl     _S_adj+64[r0],r7");
341         asm("ashl       $4,r7,r0");
342         asm("bisl2      r0,r8");
343         asm("rotl       $-12,r9,r0");
344         asm("bicl2      $-64,r0");
345         asm("movzbl     _S_adj+128[r0],r7");
346         asm("ashl       $8,r7,r0");
347         asm("bisl2      r0,r8");
348         asm("rotl       $-18,r9,r0");
349         asm("bicl2      $-64,r0");
350         asm("movzbl     _S_adj+192[r0],r7");
351         asm("ashl       $12,r7,r0");
352         asm("bisl2      r0,r8");
353         asm("rotl       $-24,r9,r0");
354         asm("bicl2      $-64,r0");
355         asm("movzbl     _S_adj+256[r0],r7");
356         asm("ashl       $16,r7,r0");
357         asm("bisl2      r0,r8");
358         asm("rotl       $-30,r9,r0");
359         asm("bicl2      $-4,r0");
360         asm("bicl3      $-16,r10,r1");
361         asm("ashl       $2,r1,r1");
362         asm("addl2      r1,r0");
363         asm("movzbl     _S_adj+320[r0],r7");
364         asm("ashl       $20,r7,r0");
365         asm("bisl2      r0,r8");
366         asm("rotl       $-4,r10,r0");
367         asm("bicl2      $-64,r0");
368         asm("movzbl     _S_adj+384[r0],r7");
369         asm("ashl       $24,r7,r0");
370         asm("bisl2      r0,r8");
371         asm("rotl       $-10,r10,r0");
372         asm("bicl2      $-64,r0");
373         asm("movzbl     _S_adj+448[r0],r7");
374         asm("ashl       $28,r7,r0");
375         asm("bisl2      r8,r0");
376         asm("movl       r0,r10");
377
378 #endif /* vaxasm */
379 #endif
380
381 #ifdef DEBUG
382         if (des_debug & 2) {
383             dbg_tmp[0] = L1;
384             dbg_tmp[1] = R1;
385             DBG_PRINT("after s");
386             printf("iter = %2d  after s\n\t\tL1 R1 = ", i);
387             des_cblock_print_file(dbg_tmp, stdout);
388         }
389 #endif
390
391 /*   P_start:*/
392         /* and then the p permutation from R1 into R2 */
393 #include "p.c"
394         /* reset the input to L1, R1 */
395         R1 = R2;
396
397 #ifdef DEBUG
398         if (des_debug & 2) {
399             dbg_tmp[0] = L1;
400             dbg_tmp[1] = R1;
401             DBG_PRINT("after p");
402             printf("iter = %2d  after p\n\t\tL1 R1 = ", i);
403             des_cblock_print_file(dbg_tmp, stdout);
404         }
405 #endif
406
407         /* R1 is the output value from the f() */
408         /* move R[iter] to L[iter+1] */
409 /*   XOR_2_start:*/
410         L1 = R_save;
411         /* xor with left */
412         R1 = L_save ^ R1;
413         /* reset the input */
414     }
415
416     /* flip left and right before final permutation */
417     L2 = R1;                    /* flip */
418     R2 = L1;
419     /* reset the input */
420     L1 = L2;
421     R1 = R2;
422
423 #ifdef DEBUG
424     if (des_debug & 2) {
425         dbg_tmp[0] = L1;
426         dbg_tmp[1] = R1;
427         DBG_PRINT("before FP");
428         printf("iter = %2d  before FP\n\t\tL1 R1 = ", i);
429         des_cblock_print_file(dbg_tmp, stdout);
430     }
431 #endif
432
433 /*FP_start:*/
434     /* do the final permutation from L1R1 to L2R2 */
435     /* all the fp code is in the include file */
436 #include "fp.c"
437
438     /* copy the output to the ciphertext string;
439      * can be same as cleartext
440      */
441
442 #ifdef MUSTALIGN
443     if ((afs_int32) cipher & 3) {
444         L_save = L2;            /* cant bcopy a reg */
445         R_save = R2;
446         cipher=((char*)cipher)+1;
447         memcpy((char *)cipher, (char *)&L_save, sizeof(L_save));
448         memcpy((char *)cipher, (char *)&R_save, sizeof(R_save));
449     } else
450 #endif
451     {
452         (*((afs_int32 *)cipher))++;
453         *((afs_int32*)cipher)= L2;      
454         *((afs_int32 *)cipher) = R2;
455     }
456
457 #ifdef DEBUG
458     if (des_debug & 2) {
459         L1 = L2;
460         R1 = R2;
461         dbg_tmp[0] = L1;
462         dbg_tmp[1] = R1;
463         DBG_PRINT("done");
464         printf("iter = %2d  done\n\t\tL1 R1 = ", i);
465         des_cblock_print_file(dbg_tmp, stdout);
466     }
467 #endif
468
469     /* that's it, no errors can be returned */
470     return 0;
471 }