Diff for /loncom/auth/londes.js between versions 1.1 and 1.4

version 1.1, 2000/06/28 22:49:24 version 1.4, 2002/03/23 11:27:41
Line 2 Line 2
 // The LearningOnline Network with CAPA  // The LearningOnline Network with CAPA
 // Encryption Routines according to Data Encryption Standard DES  // Encryption Routines according to Data Encryption Standard DES
 // Federal Information Processing Standards Publication 46-2 (1993 Dec 30)  // Federal Information Processing Standards Publication 46-2 (1993 Dec 30)
   //
   // $Id$
   //
   // Copyright Michigan State University Board of Trustees
   //
   // This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   //
   // LON-CAPA is free software; you can redistribute it and/or modify
   // it under the terms of the GNU General Public License as published by
   // the Free Software Foundation; either version 2 of the License, or
   // (at your option) any later version.
   //
   // LON-CAPA is distributed in the hope that it will be useful,
   // but WITHOUT ANY WARRANTY; without even the implied warranty of
   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   // GNU General Public License for more details.
   //
   // You should have received a copy of the GNU General Public License
   // along with LON-CAPA; if not, write to the Free Software
   // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   //
   // /home/httpd/html/adm/gpl.txt
   //
   // http://www.lon-capa.org/
   //
 // JavaScript Implementation by Gerd Kortemeyer  // JavaScript Implementation by Gerd Kortemeyer
 // 1997,06/27/00,06/28 Gerd Kortemeyer  // 1997,06/27/00,06/28,03/23/02 Gerd Kortemeyer
   
 // Global variables  // Global variables
   
Line 43  var b3=0; Line 68  var b3=0;
 // Text variable  // Text variable
 var text="";  var text="";
   
   
   // Function to AND with bit i
   
   function andbit(value,i) {
      return value&bit[i];
   }
   
   // Function to OR with bit i
   
   function orbit(value,i) {
      return value|bit[i];
   }
   
 // Initialize table arrays and perform self test  // Initialize table arrays and perform self test
   
 function init() {  function init() {
Line 103  function init() { Line 141  function init() {
         etarr[i]=parseInt(et.substring(i*2,i*2+2));          etarr[i]=parseInt(et.substring(i*2,i*2+2));
      }       }
   
   // Selftest
        window.status="Self test";
   
   // Primitive test, "Cryptography: A New Dimension in Computer Security"
   // C.H. Meyer, S.M. Matyas, John Wiley and Sons, 1982, pg. 160
   
        uextkey=0x1234567;
        lextkey=bit[31]|0x9abcdef;
        ublock=uextkey;
        lblock=lextkey;
        ip();
        if (hexstring(lv)!='cc00ccff') { alert("IP failed: lv."); }
        if (hexstring(rv)!='f0aaf0aa') { alert("IP failed: rv."); }
        initkeys();
        if (hexstring(uk[1])!='000b0267') { alert("Key generation failed: uk"); }
        if (hexstring(lk[1])!='009b49a5') { alert("Key generation failed: lk"); }
        stage(1);
        if (hexstring(uexpand)!='007a1555') { alert("E failed: u"); }
        if (hexstring(lexpand)!='007a1555') { alert("E failed: l"); }
        if (hexstring(rv)!='921c209c') { alert("Stage 1 failed"); }
   
   // According to National Bureau of Standards, Special Publication 500-20,
   // "Validating the Correctness of Hardware Implementations of the
   //  NBS Data Encryption Standard", Rev. Sep. 1980.
   
   // PC-1 and PC-2 test
        ublock=0;
        lblock=0;     
        uextkey=0x1010101;
        lextkey=0x1010102;
        initkeys();
        encrypt();    
        var st=hexstring(ublock);
        st+=hexstring(lblock);
        if (st!="869efd7f9f265a09") {
           st+=": failed PC-1 and PC-2 test!";     
           alert(st);
        }
        window.status="Self test .";
   
   // According to NBS 500-20 IP and E test
        ublock=bit[28]|0x66b40b4;
        lblock=bit[30]|0xaba4bd6;
        uextkey=0x1010101;
        lextkey=0x1010101;
        initkeys();
        encrypt();    
        var st=hexstring(ublock);
        st+=hexstring(lblock);
        if (st!="0000000000000001") {
           st+=": failed IP and E test!";     
           alert(st);
        }
        window.status="Self test ..";
       
   // According to NBS 500-20 ptest
        ublock=0;
        lblock=0;
        uextkey=bit[28]|0x29116;
        lextkey=bit[31]|bit[28]|0x8100101;
        initkeys();
        encrypt();    
        var st=hexstring(ublock);
        st+=hexstring(lblock);
        if (st!="1aeac39a61f0a464") {
           st+=": failed ptest!";     
           alert(st);
        }
        window.status="Self test ...";
   
   // According to NBS 500-20 S-box test
        ublock=bit[28]|bit[29]|0x553228;
        lblock=bit[29]|bit[30]|0xd6f295a;
        var orgu=ublock;
        var orgl=lblock;
        uextkey=bit[28]|0xc587f1c;
        lextkey=bit[28]|0x3924fef;
        initkeys();
        encrypt();
        var st=hexstring(ublock);
        st+=hexstring(lblock);
        if (st!="63fac0d034d9f793") {
           st+=": failed S-box test!";     
           alert(st);
        }
        window.status="Self test ....";
   
   // Decryption test
        decrypt();
        if (ublock!=orgu) { alert("Decryption failed for ublock."); }
        if (lblock!=orgl) { alert("Decryption failed for lblock."); }
        window.status="Self test .....";
   
   // Reset keys
   
        uextkey=0;
        lextkey=0;
   
      window.status="Ready.";       window.status="Ready.";
 }  }
   
Line 113  function hexstring(i) { Line 249  function hexstring(i) {
      var hchr="0123456789abcdef";       var hchr="0123456789abcdef";
      for (var k=28; k>=0; k=k-4) {       for (var k=28; k>=0; k=k-4) {
          j=0;           j=0;
          for (var m=0; m<=3; m++) { if ((i&bit[k+m])!=0) { j=j|bit[m]; } }           for (var m=0; m<=3; m++) { if (andbit(i,k+m)!=0) { j=orbit(j,m); } }
          hstr+=hchr.substring(j,j+1);           hstr+=hchr.substring(j,j+1);
      }          }   
      return(hstr);       return(hstr);
Line 122  function hexstring(i) { Line 258  function hexstring(i) {
 // Shift a 28-bit register one time  // Shift a 28-bit register one time
   
 function shift(sreg) {  function shift(sreg) {
      var bit27=(sreg&bit[0])<<27;       var bit27=andbit(sreg,0)<<27;
      return (sreg>>1)|bit27;       return (sreg>>1)|bit27;
 }  }
                   
Line 149  function initkeys() { Line 285  function initkeys() {
      for (var i=0; i<=27;i++) {       for (var i=0; i<=27;i++) {
           bt=parseInt(celements.substring(i*2,i*2+2));            bt=parseInt(celements.substring(i*2,i*2+2));
           if (bt<=32) {            if (bt<=32) {
               if ((uextkey & bit[31-(bt-1)]) != 0) { c=c|bit[i]; }                if (andbit(uextkey,31-(bt-1)) != 0) { c=orbit(c,i); }
           } else {            } else {
               if ((lextkey & bit[31-(bt-33)]) != 0) { c=c|bit[i]; }                if (andbit(lextkey,31-(bt-33)) != 0) { c=orbit(c,i); }
           }            }
           bt=parseInt(delements.substring(i*2,i*2+2));            bt=parseInt(delements.substring(i*2,i*2+2));
           if (bt<=32) {            if (bt<=32) {
               if ((uextkey & bit[31-(bt-1)]) != 0) { d=d|bit[i]; }                if (andbit(uextkey,31-(bt-1)) != 0) { d=orbit(d,i); }
           } else {            } else {
               if ((lextkey & bit[31-(bt-33)]) != 0) { d=d|bit[i]; }                if (andbit(lextkey,31-(bt-33)) != 0) { d=orbit(d,i); }
           }            }
       }        }
 // Initialize the k vectors  // Initialize the k vectors
Line 173  function initkeys() { Line 309  function initkeys() {
           uk[i]=0;            uk[i]=0;
           lk[i]=0;            lk[i]=0;
           for (j=0; j<=23;j++) {                        for (j=0; j<=23;j++) {            
             if ((d&bit[ukarr[j]-29])!=0) { lk[i]=lk[i]|bit[23-j]; }              if (andbit(d,ukarr[j]-29)!=0) { lk[i]=orbit(lk[i],23-j); }
             if ((c&bit[lkarr[j]-1])!=0) { uk[i]=uk[i]|bit[23-j]; }              if (andbit(c,lkarr[j]-1)!=0)  { uk[i]=orbit(uk[i],23-j); }
           }            }
       }        }
 }  }
Line 188  function ip() { Line 324  function ip() {
      for (var i=0; i<=31;i++) {       for (var i=0; i<=31;i++) {
           bt=riparr[i];            bt=riparr[i];
           if (bt<=32) {            if (bt<=32) {
               if ((lblock & bit[bt-1]) != 0) { rv=rv|bit[i]; }                if (andbit(lblock,bt-1) != 0) { rv=orbit(rv,i); }
           } else {            } else {
               if ((ublock & bit[bt-33]) != 0) { rv=rv|bit[i]; }                if (andbit(ublock,bt-33) != 0) { rv=orbit(rv,i); }
           }            }
           if (bt<=33) {            if (bt<=33) {
               if ((lblock & bit[bt-2]) != 0) { lv=lv|bit[i]; }                if (andbit(lblock,bt-2) != 0) { lv=orbit(lv,i); }
           } else {            } else {
               if ((ublock & bit[bt-34]) != 0) { lv=lv|bit[i]; }                if (andbit(ublock,bt-34) != 0) { lv=orbit(lv,i); }
           }                 }     
      }       }
 }  }
Line 208  function invip() { Line 344  function invip() {
      lblock=0;       lblock=0;
      for (var i=0; i<=15;i++) {       for (var i=0; i<=15;i++) {
           bt=liiparr[i];            bt=liiparr[i];
           if ((rv & bit[bt-1]) != 0) { lblock=lblock|bit[i*2]; }            if (andbit(rv,bt-1) != 0) { lblock=orbit(lblock,i*2); }
           if ((lv & bit[bt-1]) != 0) { lblock=lblock|bit[i*2+1]; }            if (andbit(lv,bt-1) != 0) { lblock=orbit(lblock,i*2+1); }
           bt=uiiparr[i];            bt=uiiparr[i];
           if ((rv & bit[bt-1]) != 0) { ublock=ublock|bit[i*2]; }            if (andbit(rv,bt-1) != 0) { ublock=orbit(ublock,i*2); }
           if ((lv & bit[bt-1]) != 0) { ublock=ublock|bit[i*2+1]; }            if (andbit(lv,bt-1) != 0) { ublock=orbit(ublock,i*2+1); }
      }       }
 }  }
   
Line 224  function expand() { Line 360  function expand() {
     lexpand=0;      lexpand=0;
     for (var i=0; i<=23; i++) {      for (var i=0; i<=23; i++) {
        bt=etarr[i];         bt=etarr[i];
        if (rv & bit[bt-1]) { lexpand=lexpand|bit[i]; }          if (andbit(rv,bt-1)!=0) { lexpand=orbit(lexpand,i); } 
     }      }
     for (i=24; i<=47; i++) {      for (i=24; i<=47; i++) {
        bt=etarr[i];         bt=etarr[i];
        if (rv & bit[bt-1]) { uexpand=uexpand|bit[i-24]; }         if (andbit(rv,bt-1)!=0) { uexpand=orbit(uexpand,i-24); }
     }      }
 }  }
   
Line 238  function permute() { Line 374  function permute() {
     var prv=rv;      var prv=rv;
     rv=0;      rv=0;
     for (var i=0; i<=31; i++) {      for (var i=0; i<=31; i++) {
        if ((prv&bit[31-(pparr[i]-1)])!=0) { rv=rv|bit[31-i]; }         if (andbit(prv,31-(pparr[i]-1))!=0) { rv=orbit(rv,31-i); }
     }      }
 }  }
   
Line 253  function stage(si) { Line 389  function stage(si) {
     for (var ki=42;ki>=24;ki=ki-6) {      for (var ki=42;ki>=24;ki=ki-6) {
        rv=rv<<4;         rv=rv<<4;
        var i=0;         var i=0;
        if ((uadd&bit[ki-24])!=0) { i=i|bit[0]; }         if (andbit(uadd,ki-24)!=0) { i=orbit(i,0); }
        if ((uadd&bit[ki-19])!=0) { i=i|bit[1]; }         if (andbit(uadd,ki-19)!=0) { i=orbit(i,1); }
        var j=0;         var j=0;
        for (var kj=0; kj<=3; kj++) {         for (var kj=0; kj<=3; kj++) {
           if ((uadd&bit[ki-23+kj])!=0) { j=j|bit[kj]; }            if (andbit(uadd,ki-23+kj)!=0) { j=orbit(j,kj); }
        }         }
        rv=rv|sbarr[ks*64+i*16+j];         rv=rv|sbarr[ks*64+i*16+j];
        ks++;         ks++;
Line 265  function stage(si) { Line 401  function stage(si) {
     for (var ki=18;ki>=0;ki=ki-6) {      for (var ki=18;ki>=0;ki=ki-6) {
        rv=rv<<4;         rv=rv<<4;
        var i=0;         var i=0;
        if ((ladd&bit[ki]  )!=0) { i=i|bit[0]; }         if (andbit(ladd,ki)  !=0) { i=orbit(i,0); }
        if ((ladd&bit[ki+5])!=0) { i=i|bit[1]; }         if (andbit(ladd,ki+5)!=0) { i=orbit(i,1); }
        var j=0;         var j=0;
        for (var kj=0; kj<=3; kj++) {         for (var kj=0; kj<=3; kj++) {
           if ((ladd&bit[ki+1+kj])!=0) { j=j|bit[kj]; }            if (andbit(ladd,ki+1+kj)!=0) { j=orbit(j,kj); }
        }         }
        rv=rv|sbarr[ks*64+i*16+j];         rv=rv|sbarr[ks*64+i*16+j];
        ks++;         ks++;

Removed from v.1.1  
changed lines
  Added in v.1.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>