src/tizen/otpalg/otpalg.js

Wed, 31 Jul 2013 19:48:00 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Jul 2013 19:48:00 +0200
changeset 14
d1b294812560
permissions
-rw-r--r--

Introduce port to the Tizen OS.

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

mercurial