1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/firefoxos/otpalg/otpalg.js Mon Apr 22 22:00:43 2013 +0200 1.3 @@ -0,0 +1,169 @@ 1.4 +/* 1.5 + * OTPWCalc - One time password challenge response calculator client 1.6 + * Copyright © 2013 Michael Schloh von Bennewitz <michael@schloh.com> 1.7 + * 1.8 + * OTPWCalc is free software: you can redistribute it and/or modify 1.9 + * it under the terms of the European Union Public Licence, either 1.10 + * version 1.1 of the license, or (at your option) any later version. 1.11 + * 1.12 + * OTPWCalc is distributed in the hope that it will be useful, 1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty 1.14 + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 1.15 + * the European Union Public License for more details. 1.16 + * 1.17 + * You should have received a copy of the European Union Public 1.18 + * Licence along with OTPWCalc. If not, please refer to 1.19 + * <http://joinup.ec.europa.eu/software/page/eupl/>. 1.20 + * 1.21 + * This file is part of project OTWPCalc, a one time password challenge 1.22 + * response calculator client and is found at http://otpwcalc.europalab.com/ 1.23 + * 1.24 + * otpalg.js: ECMA JavaScript implementation 1.25 + */ 1.26 + 1.27 +// <![CDATA[ 1.28 +function genotpmd4(secret, seed, n) { 1.29 + var t = seed.toString().toLowerCase() + secret; 1.30 + t = mdxfold(coremd4(str2binl(t), t.length * 8)); 1.31 + for (var i = n; i > 0; --i) { t = mdxfold(coremd4(t, 64)); } 1.32 + return t; 1.33 +} 1.34 + 1.35 +function genotpmd5(secret, seed, n) { 1.36 + var t = seed.toString().toLowerCase() + secret; 1.37 + t = mdxfold(coremd5(str2binl(t), t.length * 8)); 1.38 + for (var i = n; i > 0; --i) { t = mdxfold(coremd5(t, 64)); } 1.39 + return t; 1.40 +} 1.41 + 1.42 +function genotpsha1(secret, seed, n) { 1.43 + var t = seed.toString().toLowerCase() + secret; 1.44 + t = sha1fold(coresha1(str2binb(t), t.length * 8)); 1.45 + for (var i = n; i > 0; --i) { t = sha1fold(coresha1(t, 64)); } 1.46 + t = invertendian(t, true); 1.47 + return t; 1.48 +} 1.49 + 1.50 +function genotprmd160(secret, seed, n) { 1.51 + var t = seed.toString().toLowerCase() + secret; 1.52 + t = rmd160fold(corermd160(str2binl(t), t.length * 8)); 1.53 + for (var i = n; i > 0; --i) { t = rmd160fold(corermd160(t, 64)); } 1.54 + return t; 1.55 +} 1.56 + 1.57 +function genotpmultmd4(secret, seed, n, m) { 1.58 + var res = Array(); var lim = n - m + 1; 1.59 + var t = seed.toString().toLowerCase() + secret; 1.60 + t = mdxfold(coremd4(str2binl(t), t.length * 8)); 1.61 + if (lim == 0) res[0] = t; 1.62 + for (var i = 1; i <= n; ++i) { 1.63 + t = mdxfold(coremd4(t, 64)); 1.64 + if (i >= lim) res[i-lim] = t; 1.65 + } 1.66 + return res; 1.67 +} 1.68 + 1.69 +function genotpmultmd5(secret, seed, n, m) { 1.70 + var res = Array(); var lim = n - m + 1; 1.71 + var t = seed.toString().toLowerCase() + secret; 1.72 + t = mdxfold(coremd5(str2binl(t), t.length * 8)); 1.73 + if (lim == 0) res[0] = t; 1.74 + for (var i = 1; i <= n; ++i) { 1.75 + t = mdxfold(coremd5(t, 64)); 1.76 + if (i >= lim) res[i-lim] = t; 1.77 + } 1.78 + return res; 1.79 +} 1.80 + 1.81 +function genotpmultsha1(secret, seed, n, m) { 1.82 + var res = Array(); var lim = n - m + 1; 1.83 + var t = seed.toString().toLowerCase() + secret; 1.84 + t = sha1fold(coresha1(str2binb(t), t.length * 8)); 1.85 + if (lim == 0) res[0] = invertendian(t, false); 1.86 + for (var i = 1; i <= n; ++i) { 1.87 + t = sha1fold(coresha1(t, 64)); 1.88 + if (i >= lim) res[i-lim] = invertendian(t, false); 1.89 + } 1.90 + return res; 1.91 +} 1.92 + 1.93 +function genotpmultrmd160(secret, seed, n, m) { 1.94 + var res = Array(); var lim = n - m + 1; 1.95 + var t = seed.toString().toLowerCase() + secret; 1.96 + t = rmd160fold(corermd160(str2binl(t), t.length * 8)); 1.97 + if (lim == 0) res[0] = t; 1.98 + for (var i = 1; i <= n; ++i) { 1.99 + t = rmd160fold(corermd160(t, 64)); 1.100 + if (i >= lim) res[i-lim] = t; 1.101 + } 1.102 + return res; 1.103 +} 1.104 + 1.105 +function mdxfold(h) { return Array(h[0] ^ h[2], h[1] ^ h[3]); } 1.106 + 1.107 +function sha1fold(h) { 1.108 + h = invertendian(h, true); 1.109 + return Array(h[0] ^ h[2] ^ h[4], h[1] ^ h[3]); 1.110 +} 1.111 + 1.112 +function rmd160fold(h) { return Array(h[0] ^ h[2] ^ h[4], h[1] ^ h[3]); } 1.113 + 1.114 +function invertendian(a, inpl) { 1.115 + var t = inpl ? a : Array(a.length); 1.116 + for (var i = 0; i < a.length; ++i) { 1.117 + var t1 = (a[i] & 0xff) << 24; 1.118 + var t2 = ((a[i] >> 8) & 0xff) << 16; 1.119 + var t3 = ((a[i] >> 16) & 0xff) << 8; 1.120 + var t4 = (a[i] >> 24) & 0xff; 1.121 + t[i] = t1 | t2 | t3 | t4; 1.122 + } 1.123 + return t; 1.124 +} 1.125 + 1.126 +function arrtoboth(a) { return arrtosix(a) + " (" + arrtohex(a) + ")"; } 1.127 + 1.128 +function arrtohex(a) { 1.129 + var s = ""; 1.130 + for (var i = 0; i < 2; ++i) { 1.131 + for (var j = 0; j < 4; ++j) { 1.132 + var t = (a[i] >> (8*j)) & 0xff; 1.133 + t = t.toString(16).toUpperCase(); 1.134 + s += (t.length == 1) ? ('0' + t) : t; // 1 octet = 2 hex digits 1.135 + if (j % 2 == 1) s += ' '; 1.136 + } 1.137 + } 1.138 + return s.substr(0, s.length-1); // drop the last space 1.139 +} 1.140 + 1.141 +function arrtosix(h) { 1.142 + var s = ""; 1.143 + var parity = 0; 1.144 + for (var i = 0; i < 2; ++i) { 1.145 + for (var j = 0; j < 32; j += 2) { 1.146 + parity += (h[i] >> j) & 0x3; 1.147 + } 1.148 + } 1.149 + var ind; 1.150 + ind = (h[0] & 0xff) << 3; 1.151 + ind |= (h[0] >> 13) & 0x7; 1.152 + s += words[ind] + " "; 1.153 + ind = ((h[0] >> 8) & 0x1f) << 6; 1.154 + ind |= (h[0] >> 18) & 0x3f; 1.155 + s += words[ind] + " "; 1.156 + ind = ((h[0] >> 16) & 0x3) << 9; 1.157 + ind |= ((h[0] >> 24) & 0xff) << 1; 1.158 + ind |= (h[1] >> 7) & 0x1; 1.159 + s += words[ind] + " "; 1.160 + ind = (h[1] & 0x7f) << 4; 1.161 + ind |= (h[1] >> 12) & 0xf; 1.162 + s += words[ind] + " "; 1.163 + ind = ((h[1] >> 8) & 0xf) << 7; 1.164 + ind |= (h[1] >> 17) & 0x7f; 1.165 + s += words[ind] + " "; 1.166 + ind = ((h[1] >> 16) & 0x1) << 10; 1.167 + ind |= ((h[1] >> 24) & 0xff) << 2; 1.168 + ind |= (parity & 0x03); 1.169 + s += words[ind]; 1.170 + return s; 1.171 +} 1.172 +// ]]>