src/firefoxos/otpalg/otpalg.js

Mon, 22 Apr 2013 22:00:43 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 22 Apr 2013 22:00:43 +0200
changeset 0
6a0957738c54
permissions
-rw-r--r--

Import pristine sources of new project OTPWCalc.

     1 /*
     2  * OTPWCalc - One time password challenge response calculator client
     3  * Copyright © 2013 Michael Schloh von Bennewitz <michael@schloh.com>
     4  * 
     5  * OTPWCalc is free software: you can redistribute it and/or modify
     6  * it under the terms of the European Union Public Licence, either
     7  * version 1.1 of the license, or (at your option) any later version.
     8  * 
     9  * OTPWCalc is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty
    11  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
    12  * the European Union Public License for more details.
    13  * 
    14  * You should have received a copy of the European Union Public
    15  * Licence along with OTPWCalc. If not, please refer to
    16  * <http://joinup.ec.europa.eu/software/page/eupl/>.
    17  * 
    18  * This file is part of project OTWPCalc, a one time password challenge
    19  * response calculator client and is found at http://otpwcalc.europalab.com/
    20  * 
    21  * otpalg.js: ECMA JavaScript implementation
    22  */
    24 // <![CDATA[
    25 function genotpmd4(secret, seed, n) {
    26     var t = seed.toString().toLowerCase() + secret;
    27     t = mdxfold(coremd4(str2binl(t), t.length * 8));
    28     for (var i = n; i > 0; --i) { t = mdxfold(coremd4(t, 64)); }
    29     return t;
    30 }
    32 function genotpmd5(secret, seed, n) {
    33     var t = seed.toString().toLowerCase() + secret;
    34     t = mdxfold(coremd5(str2binl(t), t.length * 8));
    35     for (var i = n; i > 0; --i) { t = mdxfold(coremd5(t, 64)); }
    36     return t;
    37 }
    39 function genotpsha1(secret, seed, n) {
    40     var t = seed.toString().toLowerCase() + secret;
    41     t = sha1fold(coresha1(str2binb(t), t.length * 8));
    42     for (var i = n; i > 0; --i) { t = sha1fold(coresha1(t, 64)); }
    43     t = invertendian(t, true);
    44     return t;
    45 }
    47 function genotprmd160(secret, seed, n) {
    48     var t = seed.toString().toLowerCase() + secret;
    49     t = rmd160fold(corermd160(str2binl(t), t.length * 8));
    50     for (var i = n; i > 0; --i) { t = rmd160fold(corermd160(t, 64)); }
    51     return t;
    52 }
    54 function genotpmultmd4(secret, seed, n, m) {
    55     var res = Array(); var lim = n - m + 1;
    56     var t = seed.toString().toLowerCase() + secret;
    57     t = mdxfold(coremd4(str2binl(t), t.length * 8));
    58     if (lim == 0) res[0] = t;
    59     for (var i = 1; i <= n; ++i) {
    60 	t = mdxfold(coremd4(t, 64));
    61 	if (i >= lim) res[i-lim] = t;
    62     }
    63     return res;
    64 }
    66 function genotpmultmd5(secret, seed, n, m) {
    67     var res = Array(); var lim = n - m + 1;
    68     var t = seed.toString().toLowerCase() + secret;
    69     t = mdxfold(coremd5(str2binl(t), t.length * 8));
    70     if (lim == 0) res[0] = t;
    71     for (var i = 1; i <= n; ++i) {
    72 	t = mdxfold(coremd5(t, 64));
    73 	if (i >= lim) res[i-lim] = t;
    74     }
    75     return res;
    76 }
    78 function genotpmultsha1(secret, seed, n, m) {
    79     var res = Array(); var lim = n - m + 1;
    80     var t = seed.toString().toLowerCase() + secret;
    81     t = sha1fold(coresha1(str2binb(t), t.length * 8));
    82     if (lim == 0) res[0] = invertendian(t, false);
    83     for (var i = 1; i <= n; ++i) {
    84 	t = sha1fold(coresha1(t, 64));
    85 	if (i >= lim) res[i-lim] = invertendian(t, false);
    86     }
    87     return res;
    88 }
    90 function genotpmultrmd160(secret, seed, n, m) {
    91     var res = Array(); var lim = n - m + 1;
    92     var t = seed.toString().toLowerCase() + secret;
    93     t = rmd160fold(corermd160(str2binl(t), t.length * 8));
    94     if (lim == 0) res[0] = t;
    95     for (var i = 1; i <= n; ++i) {
    96 	t = rmd160fold(corermd160(t, 64));
    97 	if (i >= lim) res[i-lim] = t;
    98     }
    99     return res;
   100 }
   102 function mdxfold(h) { return Array(h[0] ^ h[2], h[1] ^ h[3]); }
   104 function sha1fold(h) {
   105     h = invertendian(h, true);
   106     return Array(h[0] ^ h[2] ^ h[4], h[1] ^ h[3]);
   107 }
   109 function rmd160fold(h) { return Array(h[0] ^ h[2] ^ h[4], h[1] ^ h[3]); }
   111 function invertendian(a, inpl) {
   112     var t = inpl ? a : Array(a.length);
   113     for (var i = 0; i < a.length; ++i) {
   114 	var t1 = (a[i] & 0xff) << 24;
   115 	var t2 = ((a[i] >> 8) & 0xff) << 16;
   116 	var t3 = ((a[i] >> 16) & 0xff) << 8;
   117 	var t4 = (a[i] >> 24) & 0xff;
   118 	t[i] = t1 | t2 | t3 | t4;
   119     }
   120     return t;
   121 }
   123 function arrtoboth(a) { return arrtosix(a) + " (" + arrtohex(a) + ")"; }
   125 function arrtohex(a) {
   126     var s = "";
   127     for (var i = 0; i < 2; ++i) {
   128 	for (var j = 0; j < 4; ++j) {
   129 	    var t = (a[i] >> (8*j)) & 0xff;
   130 	    t = t.toString(16).toUpperCase();
   131 	    s += (t.length == 1) ? ('0' + t) : t; // 1 octet = 2 hex digits
   132 	    if (j % 2 == 1) s += ' ';
   133 	}
   134     }
   135     return s.substr(0, s.length-1); // drop the last space
   136 }
   138 function arrtosix(h) {
   139     var s = "";
   140     var parity = 0;
   141     for (var i = 0; i < 2; ++i) {
   142 	for (var j = 0; j < 32; j += 2) {
   143 	    parity += (h[i] >> j) & 0x3;
   144 	}
   145     }
   146     var ind;
   147     ind = (h[0] & 0xff) << 3;
   148     ind |= (h[0] >> 13) & 0x7;
   149     s += words[ind] + " ";
   150     ind = ((h[0] >> 8) & 0x1f) << 6;
   151     ind |= (h[0] >> 18) & 0x3f;
   152     s += words[ind] + " ";
   153     ind = ((h[0] >> 16) & 0x3) << 9;
   154     ind |= ((h[0] >> 24) & 0xff) << 1;
   155     ind |= (h[1] >> 7) & 0x1;
   156     s += words[ind] + " ";
   157     ind = (h[1] & 0x7f) << 4;
   158     ind |= (h[1] >> 12) & 0xf;
   159     s += words[ind] + " ";
   160     ind = ((h[1] >> 8) & 0xf) << 7;
   161     ind |= (h[1] >> 17) & 0x7f;
   162     s += words[ind] + " ";
   163     ind = ((h[1] >> 16) & 0x1) << 10;
   164     ind |= ((h[1] >> 24) & 0xff) << 2;
   165     ind |= (parity & 0x03);
   166     s += words[ind];
   167     return s;
   168 }
   169 // ]]>

mercurial