📄 mydes.lst
字号:
C51 COMPILER V7.02a MYDES 07/16/2003 14:05:28 PAGE 1
C51 COMPILER V7.02a, COMPILATION OF MODULE MYDES
OBJECT MODULE PLACED IN myDES.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE source\myDES.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\myDES.lst) OBJECT(my
-DES.obj)
stmt level source
1 /*--------------------------------------------------------------------------------
2 DES.C: DES Algorithm Program from the Book Appliced Cryptography, Bruce Schneier
3 --------------------------------------------------------------------------------*/
4 #include <reg52.h>
5 #include "DES.h"
6 #include "string.h"
7 #include "posutils.h"
8
9 void deskey(unsigned char *, short);
10 /* hexkey[8] MODE
11 * Sets the internal key register according to the hexadecimal
12 * key contained in the 8 bytes of hexkey, according to the DES,
13 * for encryption or decryption according to MODE.
14 */
15
16 static void usekey(unsigned long *);
17 /* cookedkey[32]
18 * Loads the internal key register with the data in cookedkey.
19 */
20
21 static void cpkey(unsigned long *);
22 /* cookedkey[32]
23 * Copies the contents of the internal key register into the storage
24 * located at &cookedkey[0].
25 */
26 static void scrunch(unsigned char *, unsigned long *);
27 static void unscrun(unsigned long *, unsigned char *);
28 static void desfunc(unsigned long *, unsigned long *);
29 static void cookey(unsigned long *);
30
31
32 static unsigned long KnL[32] = { 0L };
33 static unsigned long KnR[32] = { 0L };
34 static unsigned long Kn3[32] = { 0L };
35 static unsigned char Df_Key[24] = {
36 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
37 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
38 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 };
39
40 static unsigned short code bytebit[8] = {
41 0200, 0100, 040, 020, 010, 04, 02, 01 };
42
43 static unsigned long code bigbyte[24] = {
44 0x800000L, 0x400000L, 0x200000L, 0x100000L,
45 0x80000L, 0x40000L, 0x20000L, 0x10000L,
46 0x8000L, 0x4000L, 0x2000L, 0x1000L,
47 0x800L, 0x400L, 0x200L, 0x100L,
48 0x80L, 0x40L, 0x20L, 0x10L,
49 0x8L, 0x4L, 0x2L, 0x1L };
50
51 /* Use the key schedule specified in the Standard (ANSI X3.92-1981). */
52
53 static unsigned char code pc1[56] = {
54 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,
C51 COMPILER V7.02a MYDES 07/16/2003 14:05:28 PAGE 2
55 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,
56 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
57 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 };
58
59 static unsigned char code totrot[16] = {
60 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };
61
62 static unsigned char code pc2[48] = {
63 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
64 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
65 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
66 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
67
68 void deskey(unsigned char *key, short edf) {
69 1 /* Thanks to James Gillogly & Phil Karn! */
70 1 register unsigned char i, j, l, m, n;
71 1 unsigned char pc1m[56], pcr[56];
72 1 unsigned long kn[32];
73 1
74 1 for ( j = 0; j < 56; j++ ) {
75 2 l = pc1[j];
76 2 m = l & 07;
77 2 pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
78 2 }
79 1 for( i = 0; i < 16; i++ ) {
80 2 if( edf == DE1 ) m = (15 - i) << 1;
81 2 else m = i << 1;
82 2 n = m + 1;
83 2 kn[m] = kn[n] = 0L;
84 2 for( j = 0; j < 28; j++ ) {
85 3 l = j + totrot[i];
86 3 if( l < 28 ) pcr[j] = pc1m[l];
87 3 else pcr[j] = pc1m[l - 28];
88 3 }
89 2 for( j = 28; j < 56; j++ ) {
90 3 l = j + totrot[i];
91 3 if( l < 56 ) pcr[j] = pc1m[l];
92 3 else pcr[j] = pc1m[l - 28];
93 3 }
94 2 for( j = 0; j < 24; j++ ) {
95 3 if( pcr[pc2[j]] ) kn[m] |= bigbyte[j];
96 3 if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j];
97 3 }
98 2 }
99 1 cookey(kn);
100 1 }
101
102 static void cookey(unsigned long *raw1) {
103 1 register unsigned long *cook, *raw0;
104 1 unsigned long dough[32];
105 1 register unsigned char i;
106 1
107 1 cook = dough;
108 1 for( i = 0; i < 16; i++, raw1++ ) {
109 2 raw0 = raw1++;
110 2 *cook = (*raw0 & 0x00fc0000L) << 6;
111 2 *cook |= (*raw0 & 0x00000fc0L) << 10;
112 2 *cook |= (*raw1 & 0x00fc0000L) >> 10;
113 2 *cook++|= (*raw1 & 0x00000fc0L) >> 6;
114 2 *cook = (*raw0 & 0x0003f000L) << 12;
115 2 *cook |= (*raw0 & 0x0000003fL) << 16;
116 2 *cook |= (*raw1 & 0x0003f000L) >> 4;
C51 COMPILER V7.02a MYDES 07/16/2003 14:05:28 PAGE 3
117 2 *cook++ |= (*raw1 & 0x0000003fL);
118 2 }
119 1 usekey(dough);
120 1 }
121
122 void cpkey(unsigned long *into) {
123 1 register unsigned long *from, *endp;
124 1
125 1 from = KnL, endp = &KnL[32];
126 1 while( from < endp ) *into++ = *from++;
127 1 }
128
129 void usekey(unsigned long *from) {
130 1 register unsigned long *to, *endp;
131 1
132 1 to = KnL, endp = &KnL[32];
133 1 while( to < endp ) *to++ = *from++;
134 1 }
135
136 static void scrunch(unsigned char *outof, unsigned long *into) {
137 1 *into = (*outof++ & 0xffL) << 24;
138 1 *into |= (*outof++ & 0xffL) << 16;
139 1 *into |= (*outof++ & 0xffL) << 8;
140 1 *into++ |= (*outof++ & 0xffL);
141 1 *into = (*outof++ & 0xffL) << 24;
142 1 *into |= (*outof++ & 0xffL) << 16;
143 1 *into |= (*outof++ & 0xffL) << 8;
144 1 *into |= (*outof & 0xffL);
145 1 }
146
147 static void unscrun(unsigned long *outof, unsigned char *into) {
148 1 *into++ = (*outof >> 24) & 0xffL;
149 1 *into++ = (*outof >> 16) & 0xffL;
150 1 *into++ = (*outof >> 8) & 0xffL;
151 1 *into++ = *outof++ & 0xffL;
152 1 *into++ = (*outof >> 24) & 0xffL;
153 1 *into++ = (*outof >> 16) & 0xffL;
154 1 *into++ = (*outof >> 8) & 0xffL;
155 1 *into = *outof & 0xffL;
156 1 }
157
158 static unsigned long code SP1[64] = {
159 0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
160 0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
161 0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
162 0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
163 0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
164 0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
165 0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
166 0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
167 0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
168 0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
169 0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
170 0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
171 0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
172 0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
173 0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
174 0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
175
176 static unsigned long code SP2[64] = {
177 0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
178 0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
C51 COMPILER V7.02a MYDES 07/16/2003 14:05:28 PAGE 4
179 0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
180 0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
181 0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
182 0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
183 0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
184 0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
185 0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
186 0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
187 0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
188 0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
189 0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
190 0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
191 0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
192 0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
193
194 static unsigned long code SP3[64] = {
195 0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
196 0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
197 0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
198 0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
199 0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
200 0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
201 0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
202 0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
203 0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
204 0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
205 0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
206 0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
207 0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
208 0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
209 0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
210 0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
211
212 static unsigned long code SP4[64] = {
213 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
214 0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
215 0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
216 0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
217 0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
218 0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
219 0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
220 0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
221 0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
222 0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
223 0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
224 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
225 0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
226 0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
227 0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
228 0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
229
230 static unsigned long code SP5[64] = {
231 0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
232 0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
233 0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
234 0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
235 0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
236 0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
237 0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
238 0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
239 0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -