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.

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

mercurial