Annotation of loncom/auth/londes.js, revision 1.8
1.8 ! matthew 1: <script type="text/javascript" language="JavaScript">
1.1 www 2: // The LearningOnline Network with CAPA
3: // Encryption Routines according to Data Encryption Standard DES
4: // Federal Information Processing Standards Publication 46-2 (1993 Dec 30)
1.2 www 5: //
1.8 ! matthew 6: // $Id: londes.js,v 1.7 2003/01/08 22:43:10 albertel Exp $
1.2 www 7: //
8: // Copyright Michigan State University Board of Trustees
9: //
10: // This file is part of the LearningOnline Network with CAPA (LON-CAPA).
11: //
12: // LON-CAPA is free software; you can redistribute it and/or modify
13: // it under the terms of the GNU General Public License as published by
14: // the Free Software Foundation; either version 2 of the License, or
15: // (at your option) any later version.
16: //
17: // LON-CAPA is distributed in the hope that it will be useful,
18: // but WITHOUT ANY WARRANTY; without even the implied warranty of
19: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20: // GNU General Public License for more details.
21: //
22: // You should have received a copy of the GNU General Public License
23: // along with LON-CAPA; if not, write to the Free Software
24: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25: //
26: // /home/httpd/html/adm/gpl.txt
27: //
28: // http://www.lon-capa.org/
29: //
1.1 www 30: // JavaScript Implementation by Gerd Kortemeyer
1.6 www 31: // 1997,06/27/00,06/28 Gerd Kortemeyer
1.1 www 32:
33: // Global variables
34:
35: // Array of integers with i-th bit set
36: var bit=new Array(31);
37: // Initial permutation IP for right part
38: var riparr=new Array(31);
39: // Inverse permutation IP-1
40: var liiparr=new Array(31);
41: var uiiparr=new Array(31);
42: // Key vectors
43: var uk=new Array(16);
44: var lk=new Array(16);
45: // Upper- and lower 32-bit of external key
46: var uextkey=0;
47: var lextkey=0;
48: // Upper- and lower 32-bit of block
49: var ublock=0;
50: var lblock=0;
51: // Upper and lower 24 bit of expanded right vector
52: var lexpand=0;
53: var uexpand=0;
54: // S-box array
55: var sbarr=new Array(511);
56: // E expansion
57: var etarr=new Array(47);
58: // P permutation
59: var pparr=new Array(31);
60: // r- and l-vector
61: var rv=0;
62: var lv=0;
63: // Blocks of data for text
64: var b0=0;
65: var b1=0;
66: var b2=0;
67: var b3=0;
68: // Text variable
69: var text="";
70:
71: // Initialize table arrays and perform self test
72:
73: function init() {
74: window.status="Initializing ...";
75: var rip="58504234261810 260524436282012 462544638302214 664564840322416 8";
76: var iip=" 8162432 7152331 6142230 5132129 4122028 3111927 2101826 1 91725";
77: var sb
78: sb ="14 413 1 21511 8 310 612 5 9 0 7";
79: sb+=" 015 7 414 213 110 61211 9 5 3 8";
80: sb+=" 4 114 813 6 2111512 9 7 310 5 0";
81: sb+="1512 8 2 4 9 1 7 511 31410 0 613";
82: sb+="15 1 814 611 3 4 9 7 21312 0 510";
83: sb+=" 313 4 715 2 81412 0 110 6 911 5";
84: sb+=" 014 71110 413 1 5 812 6 9 3 215";
85: sb+="13 810 1 315 4 211 6 712 0 514 9";
86: sb+="10 0 914 6 315 5 11312 711 4 2 8";
87: sb+="13 7 0 9 3 4 610 2 8 514121115 1";
88: sb+="13 6 4 9 815 3 011 1 212 51014 7";
89: sb+=" 11013 0 6 9 8 7 41514 311 5 212";
90: sb+=" 71314 3 0 6 910 1 2 8 51112 415";
91: sb+="13 811 5 615 0 3 4 7 212 11014 9";
92: sb+="10 6 9 01211 71315 1 314 5 2 8 4";
93: sb+=" 315 0 610 113 8 9 4 51112 7 214";
94: sb+=" 212 4 1 71011 6 8 5 31513 014 9";
95: sb+="1411 212 4 713 1 5 01510 3 9 8 6";
96: sb+=" 4 2 1111013 7 815 912 5 6 3 014";
97: sb+="11 812 7 114 213 615 0 910 4 5 3";
98: sb+="12 11015 9 2 6 8 013 3 414 7 511";
99: sb+="1015 4 2 712 9 5 6 11314 011 3 8";
100: sb+=" 91415 5 2 812 3 7 0 410 11311 6";
101: sb+=" 4 3 212 9 515101114 1 7 6 0 813";
102: sb+=" 411 21415 0 813 312 9 7 510 6 1";
103: sb+="13 011 7 4 9 11014 3 512 215 8 6";
104: sb+=" 1 4111312 3 7141015 6 8 0 5 9 2";
105: sb+=" 61113 8 1 410 7 9 5 01514 2 312";
106: sb+="13 2 8 4 61511 110 9 314 5 012 7";
107: sb+=" 11513 810 3 7 412 5 611 014 9 2";
108: sb+=" 711 4 1 91214 2 0 6101315 3 5 8";
109: sb+=" 2 114 7 410 8131512 9 0 3 5 611";
110: var et
111: et ="32 1 2 3 4 5 4 5 6 7 8 9 8 910111213121314151617";
112: et+="1617181920212021222324252425262728292829303132 1";
113: var pp="16 7202129122817 1152326 5183110 2 824143227 3 9191330 62211 425";
114: bit[0]=1;
115: for (var i=1; i<=31;i++) { bit[i]=bit[i-1]<<1; }
116: for (i=0; i<=31;i++) {
117: riparr[i]=parseInt(rip.substring(i*2,i*2+2));
118: pparr[i]=parseInt(pp.substring(i*2,i*2+2));
119: }
120: for (i=0;i<=15;i++) {
121: liiparr[i]=parseInt(iip.substring(i*2,i*2+2));
122: uiiparr[i]=parseInt(iip.substring(32+i*2,32+i*2+2));
123: }
124: for (i=0;i<=511;i++) {
125: sbarr[i]=parseInt(sb.substring(i*2,i*2+2));
126: }
127: for (i=0;i<=47;i++) {
128: etarr[i]=parseInt(et.substring(i*2,i*2+2));
129: }
130:
131: window.status="Ready.";
132: }
133:
134: // Return a string with hex-representation of integer
135:
136: function hexstring(i) {
137: var hstr="";
138: var hchr="0123456789abcdef";
139: for (var k=28; k>=0; k=k-4) {
140: j=0;
1.6 www 141: for (var m=0; m<=3; m++) { if ((i&bit[k+m])!=0) { j=j|bit[m]; } }
1.1 www 142: hstr+=hchr.substring(j,j+1);
143: }
144: return(hstr);
145: }
146:
147: // Shift a 28-bit register one time
148:
149: function shift(sreg) {
1.6 www 150: var bit27=(sreg&bit[0])<<27;
1.1 www 151: return (sreg>>1)|bit27;
152: }
153:
154: // Initialize key-arrays according to uextkey and lextkey
155:
156: function initkeys() {
1.7 albertel 157: init();
1.1 www 158: // Constants
159: var celements="574941332517 9 158504234261810 259514335271911 360524436";
160: var delements="63554739312315 762544638302214 661534537292113 5282012 4";
161: var lkelements="14171124 1 5 32815 62110231912 426 816 7272013 2";
162: var ukelements="415231374755304051453348444939563453464250362932";
163: var shifttimes="1122222212222221";
164: // PC-2 order
165: var ukarr=new Array(23);
166: var lkarr=new Array(23);
167: for (i=0;i<=23;i++) {
168: ukarr[i]=parseInt(ukelements.substring(i*2,i*2+2));
169: lkarr[i]=parseInt(lkelements.substring(i*2,i*2+2));
170: }
171: // Initialize c and d
172: var bt;
173: var c=0;
174: var d=0;
175: for (var i=0; i<=27;i++) {
176: bt=parseInt(celements.substring(i*2,i*2+2));
177: if (bt<=32) {
1.6 www 178: if ((uextkey & bit[31-(bt-1)]) != 0) { c=c|bit[i]; }
1.1 www 179: } else {
1.6 www 180: if ((lextkey & bit[31-(bt-33)]) != 0) { c=c|bit[i]; }
1.1 www 181: }
182: bt=parseInt(delements.substring(i*2,i*2+2));
183: if (bt<=32) {
1.6 www 184: if ((uextkey & bit[31-(bt-1)]) != 0) { d=d|bit[i]; }
1.1 www 185: } else {
1.6 www 186: if ((lextkey & bit[31-(bt-33)]) != 0) { d=d|bit[i]; }
1.1 www 187: }
188: }
189: // Initialize the k vectors
190: for (i=1;i<=16;i++) {
191: // Shift c and d
192: if (shifttimes.substring(i-1,i)=='1') {
193: c=shift(c);
194: d=shift(d);
195: } else {
196: c=shift(shift(c));
197: d=shift(shift(d));
198: }
199: uk[i]=0;
200: lk[i]=0;
201: for (j=0; j<=23;j++) {
1.6 www 202: if ((d&bit[ukarr[j]-29])!=0) { lk[i]=lk[i]|bit[23-j]; }
203: if ((c&bit[lkarr[j]-1])!=0) { uk[i]=uk[i]|bit[23-j]; }
1.1 www 204: }
205: }
206: }
207:
208: // Initial permutation of ublock and lblock into rv and lv
209:
210: function ip() {
211: var bt;
212: rv=0;
213: lv=0;
214: for (var i=0; i<=31;i++) {
215: bt=riparr[i];
216: if (bt<=32) {
1.6 www 217: if ((lblock & bit[bt-1]) != 0) { rv=rv|bit[i]; }
1.1 www 218: } else {
1.6 www 219: if ((ublock & bit[bt-33]) != 0) { rv=rv|bit[i]; }
1.1 www 220: }
221: if (bt<=33) {
1.6 www 222: if ((lblock & bit[bt-2]) != 0) { lv=lv|bit[i]; }
1.1 www 223: } else {
1.6 www 224: if ((ublock & bit[bt-34]) != 0) { lv=lv|bit[i]; }
1.1 www 225: }
226: }
227: }
228:
229: // Inverse Initial Permutation IP-1, input rv,lv, output lblock,ublock
230:
231: function invip() {
232: var bt;
233: ublock=0;
234: lblock=0;
235: for (var i=0; i<=15;i++) {
236: bt=liiparr[i];
1.6 www 237: if ((rv & bit[bt-1]) != 0) { lblock=lblock|bit[i*2]; }
238: if ((lv & bit[bt-1]) != 0) { lblock=lblock|bit[i*2+1]; }
1.1 www 239: bt=uiiparr[i];
1.6 www 240: if ((rv & bit[bt-1]) != 0) { ublock=ublock|bit[i*2]; }
241: if ((lv & bit[bt-1]) != 0) { ublock=ublock|bit[i*2+1]; }
1.1 www 242: }
243: }
244:
245: // Expansion of right vector according to E, input rv
246:
247: function expand() {
248: var bt;
249: uexpand=0;
250: lexpand=0;
251: for (var i=0; i<=23; i++) {
252: bt=etarr[i];
1.6 www 253: if (rv & bit[bt-1]) { lexpand=lexpand|bit[i]; }
1.1 www 254: }
255: for (i=24; i<=47; i++) {
256: bt=etarr[i];
1.6 www 257: if (rv & bit[bt-1]) { uexpand=uexpand|bit[i-24]; }
1.1 www 258: }
259: }
260:
261: // Permutes rv according to P, input/output rv
262:
263: function permute() {
264: var prv=rv;
265: rv=0;
266: for (var i=0; i<=31; i++) {
1.6 www 267: if ((prv&bit[31-(pparr[i]-1)])!=0) { rv=rv|bit[31-i]; }
1.1 www 268: }
269: }
270:
271: // Performs stage si of the encryption, input/output rv
272:
273: function stage(si) {
274: expand();
275: var uadd=uexpand^uk[si];
276: var ladd=lexpand^lk[si];
277: rv=0;
278: var ks=0;
279: for (var ki=42;ki>=24;ki=ki-6) {
280: rv=rv<<4;
281: var i=0;
1.6 www 282: if ((uadd&bit[ki-24])!=0) { i=i|bit[0]; }
283: if ((uadd&bit[ki-19])!=0) { i=i|bit[1]; }
1.1 www 284: var j=0;
285: for (var kj=0; kj<=3; kj++) {
1.6 www 286: if ((uadd&bit[ki-23+kj])!=0) { j=j|bit[kj]; }
1.1 www 287: }
1.6 www 288: rv=rv|sbarr[ks*64+i*16+j];
1.1 www 289: ks++;
290: }
291: for (var ki=18;ki>=0;ki=ki-6) {
292: rv=rv<<4;
293: var i=0;
1.6 www 294: if ((ladd&bit[ki] )!=0) { i=i|bit[0]; }
295: if ((ladd&bit[ki+5])!=0) { i=i|bit[1]; }
1.1 www 296: var j=0;
297: for (var kj=0; kj<=3; kj++) {
1.6 www 298: if ((ladd&bit[ki+1+kj])!=0) { j=j|bit[kj]; }
1.1 www 299: }
1.6 www 300: rv=rv|sbarr[ks*64+i*16+j];
1.1 www 301: ks++;
302: }
303: permute();
304: }
305:
306: // Encryption
307:
308: function encrypt() {
309: ip();
310: for (var i=1; i<=16; i++) {
311: var prv=rv;
312: stage(i);
313: rv=rv^lv;
314: lv=prv;
315: }
316: invip();
317: }
318:
319: // Decryption
320:
321: function decrypt() {
322: ip();
323: for (var i=16; i>=1; i--) {
324: var prv=rv;
325: stage(i);
326: rv=rv^lv;
327: lv=prv;
328: }
329: invip();
330: }
331:
332: // Put text into b0,b1,b2,b3
333:
334: function block(text) {;
335: var equiv= "*+ -./0123456789......@";
336: equiv+="ABCDEFGHIJKLMNOPQRSTUVWXYZ _ ";
337: equiv+="abcdefghijklmnopqrstuvwxyz";
338: var tl=text.length;
339: if (tl>=16) { tl=15; }
340: text+=" ";
341: text=escape(text);
342: var i;
343: var c;
344: var conv='';
345: var j=0;
346: for (i=0;i<15;i++) {
347: if (text.charAt(j)=='%') {
348: conv+=text.charAt(j+1)+text.charAt(j+2);
349: j=j+3;
350: } else {
351: c=hexstring(42+equiv.indexOf(text.charAt(j)));
352: conv+=c.substr(6,2);
353: j++;
354: }
355: }
356: b3=tl*bit[24]+parseInt("0x"+conv.substr(0,6));
357: b2=parseInt("0x"+conv.substr(6,8));
358: b1=parseInt("0x"+conv.substr(14,8));
359: b0=parseInt("0x"+conv.substr(22,8));
360:
361: }
362:
363: function crypted(text) {
364: block(text);
365: lblock=b0;
366: ublock=b1;
367: encrypt();
368: b0=lblock;
369: b1=ublock;
370: lblock=b2;
371: ublock=b3;
372: encrypt();
373: b2=lblock;
374: b3=ublock;
375: return(hexstring(b3)+hexstring(b2)+hexstring(b1)+hexstring(b0));
376: }
377:
378: </script>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>