-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathocra.js
399 lines (338 loc) · 12.9 KB
/
ocra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* This an implementation of OCRA - OATH Challenge-Response Algorithm
* based on ocra java reference implementation
* from https://tools.ietf.org/html/rfc6287
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
// to test
// https://su12147fct0:12002/ocrajs/index.html
var OCRA = {
// Convert a hex string to a byte array
hexStr2Bytes : function(hex)
{
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
},
// Convert a byte array to a hex string
bytesToHexStr : function(bytes)
{
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
},
// convert ArrayBuffer to String
ab2str : function (buf)
{
return String.fromCharCode.apply(null, new Uint8Array(buf));
},
// convert String to Uint8 ArrayBuffer View
convertStringToArrayBufferView : function(str)
{
if (typeof str === 'string' || str instanceof String)
{
var bytes = new Uint8Array(str.length);
for (var iii = 0; iii < str.length; iii++)
{
bytes[iii] = str.charCodeAt(iii);
}
return bytes;
}
else
return str;
},
// convert String to ArrayBuffer
str2ab : function(str) {
if (typeof str === 'string' || str instanceof String)
{
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return bufView;
}
else
return null;
},
// append ArrayBuffer to existing ArrayBuffer
ArrayConcat : function(buffer1, buffer2)
{
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
},
// hmac_hash with Web Cryptography API
// https://www.w3.org/TR/WebCryptoAPI/
//
// this api are supported on chrome 49+, firefox 47+, edge, and ms prefix old version on ie11
// http://caniuse.com/#feat=cryptography
//
// based on sample from https://jswebcrypto.azurewebsites.net/demo.html#/hmac
// and http://qnimate.com/digital-signature-using-web-cryptography-api/
hmacAsyncWeb_hash : function(hashAlgo, hashKey, hashText,codeDigits)
{
var hmacSha = {name: 'HMAC', hash: {name: hashAlgo.toUpperCase()}};
if (typeof hashKey === 'string' || hashKey instanceof String)
var hmacKeyBuf = this.convertStringToArrayBufferView(hashKey);
else
var hmacKeyBuf = hashKey;
if (typeof hashText === 'string' || hashText instanceof String)
var hmacTextBuf = this.convertStringToArrayBufferView(hashText);
else
var hmacTextBuf = hashText;
var crypto = window.crypto || window.msCrypto;
return crypto.subtle.importKey("raw", hmacKeyBuf, hmacSha, false, ["sign", "verify"])
.then(function(cryptokey){
return crypto.subtle.sign({name: 'HMAC'}, cryptokey, hmacKeyBuf);
},
function(err){
console.log(err);
})
.then(function(hash) {
// put selected bytes into result int
var offset = hash[hash.byteLength - 1] & 0xf;
var binary =
((hash[offset + 0] & 0x7f) << 24) |
((hash[offset + 1] & 0xff) << 16) |
((hash[offset + 2] & 0xff) << 8) |
(hash[offset + 3] & 0xff);
var otp = binary % DIGITS_POWER[codeDigits];
var result = otp.toString();
while (result.length < codeDigits) {
result = "0" + result;
}
return Promise.resolve(result);
});
},
// hmac using standford crypto lib
// https://github.com/bitwiseshiftleft/sjcl
hmacsjcl_hash : function(hashAlgo, hashKey, hashText)
{
if (typeof hashKey === 'string' || hashKey instanceof String)
var hmacKeyBuf = sjcl.codec.utf8String.toBits(hashKey);
else
var hmacKeyBuf = sjcl.codec.arrayBuffer.toBits(hashKey.buffer);
if (typeof hashText === 'string' || hashText instanceof String)
var hashText = sjcl.codec.utf8String.toBits(hashText);
else
var hashText = sjcl.codec.arrayBuffer.toBits(hashText.buffer);
var hashfunction = null;
if(hashAlgo.toUpperCase() == "SHA-1") hashfunction = sjcl.hash.sha1;
else if(hashAlgo.toUpperCase() == "SHA-256") hashfunction = sjcl.hash.sha256;
else if(hashAlgo.toUpperCase() == "SHA-512") hashfunction = sjcl.hash.sha512;
var out = (new sjcl.misc.hmac(hmacKeyBuf, hashfunction)).mac(hashText);
var hmac = new Uint8Array(sjcl.codec.arrayBuffer.fromBits(out));
return hmac;
},
hmacjssha_hash : function(hashAlgo, hashKey, hashText)
{
if (typeof hashKey === 'string' || hashKey instanceof String)
var hmacKeyBuf = this.convertStringToArrayBufferView(hashKey);
else
var hmacKeyBuf = hashKey;
if (typeof hashText === 'string' || hashText instanceof String)
var hmacTextBuf = this.convertStringToArrayBufferView(hashText);
else
var hmacTextBuf = hashText;
var shaObj = new jsSHA(hashAlgo, "ARRAYBUFFER");
shaObj.setHMACKey(hmacKeyBuf, "ARRAYBUFFER");
shaObj.update(hmacTextBuf);
return shaObj.getHMAC("ARRAYBUFFER");
},
// OCRA method
getOCRAmsg : function(ocraSuite, counter, question, password, sessionInformation, timeStamp)
{
var codeDigits = 0;
var crypto = "";
var result = null;
var ocraSuiteLength = ocraSuite.length;
var counterLength = 0;
var questionLength = 0;
var passwordLength = 0;
var sessionInformationLength = 0;
var timeStampLength = 0;
// The OCRASuites components
var CryptoFunction = ocraSuite.split(":")[1];
var DataInput = ocraSuite.split(":")[2];
if(CryptoFunction.toLowerCase().indexOf("sha1") > 1) crypto = "SHA-1";
else if(CryptoFunction.toLowerCase().indexOf("sha256") > 1) crypto = "SHA-256";
else if(CryptoFunction.toLowerCase().indexOf("sha512") > 1) crypto = "SHA-512";
else {
console.log("SHA algorithme unknow \"" + CryptoFunction + "\"");
return null;
}
// How many digits should we return
codeDigits = parseInt(CryptoFunction.substring(CryptoFunction.lastIndexOf("-")+1));
// The size of the byte array message to be encrypted
// Counter
if(DataInput.toLowerCase().startsWith("c")) {
// Fix the length of the HEX string
while(counter.length < 16)
counter = "0" + counter;
counterLength=8;
}
// Question - always 128 bytes
if(DataInput.toLowerCase().startsWith("q") ||
(DataInput.toLowerCase().indexOf("-q") >= 0)) {
while(question.length < 256)
question = question + "0";
questionLength=128;
}
// Password - sha1
if(DataInput.toLowerCase().indexOf("psha1") > 1){
passwordLength=20;
}
// Password - sha256
if(DataInput.toLowerCase().indexOf("psha256") > 1){
passwordLength=32;
}
// Password - sha512
if(DataInput.toLowerCase().indexOf("psha512") > 1){
passwordLength=64;
}
// sessionInformation - s064
if(DataInput.toLowerCase().indexOf("s064") > 1){
while(sessionInformation.length < 128)
sessionInformation = "0" + sessionInformation;
sessionInformationLength=64;
}
// sessionInformation - s128
if(DataInput.toLowerCase().indexOf("s128") > 1){
while(sessionInformation.length < 256)
sessionInformation = "0" + sessionInformation;
sessionInformationLength=128;
}
// sessionInformation - s256
if(DataInput.toLowerCase().indexOf("s256") > 1){
while(sessionInformation.length < 512)
sessionInformation = "0" + sessionInformation;
sessionInformationLength=256;
}
// sessionInformation - s512
if(DataInput.toLowerCase().indexOf("s512") > 1){
while(sessionInformation.length < 1024)
sessionInformation = "0" + sessionInformation;
sessionInformationLength=512;
}
// TimeStamp
if(DataInput.toLowerCase().startsWith("t") ||
(DataInput.toLowerCase().indexOf("-t") > 1)){
while(timeStamp.length < 16)
timeStamp = "0" + timeStamp;
timeStampLength=8;
}
// create a new array of Uint8Array with lenght of all zone
// Remember to add "1" for the "00" byte delimiter
var msgArrayBuffer = new ArrayBuffer(ocraSuiteLength +
counterLength +
questionLength +
passwordLength +
sessionInformationLength +
timeStampLength +
1);
// creat view of ab
var msg = new Uint8Array(msgArrayBuffer);
// Put the bytes of "ocraSuite" parameters into the message
var bArray = this.str2ab(ocraSuite);
for (var i=0;i<bArray.length;i++)
msg [i] = bArray[i];
// Delimiter
msg[bArray.length] = 0x00;
// Put the bytes of "Counter" to the message
// Input is HEX encoded
if(counterLength > 0 ){
bArray = this.hexStr2Bytes(counter);
for (var i=0;i<bArray.length;i++)
msg [i + ocraSuiteLength + 1] = bArray[i];
}
// Put the bytes of "question" to the message
// Input is text encoded
if(questionLength > 0 ){
bArray = this.hexStr2Bytes(question);
for (var i=0;i<bArray.length;i++)
msg [i + ocraSuiteLength + 1 + counterLength] = bArray[i];
}
// Put the bytes of "password" to the message
// Input is HEX encoded
if(passwordLength > 0){
bArray = this.hexStr2Bytes(password);
for (var i=0;i<bArray.length;i++)
msg [i + ocraSuiteLength + 1 + counterLength + questionLength] = bArray[i];
}
// Put the bytes of "sessionInformation" to the message
// Input is text encoded
if(sessionInformationLength > 0 ){
bArray = this.hexStr2Bytes(sessionInformation);
for (var i=0;i<bArray.length;i++)
msg [i + ocraSuiteLength + 1 + counterLength + questionLength + passwordLength] = bArray[i];
}
// Put the bytes of "time" to the message
// Input is text value of minutes
if(timeStampLength > 0){
bArray = this.hexStr2Bytes(timeStamp);
for (var i=0;i<bArray.length;i++)
msg [i + ocraSuiteLength + 1 + counterLength + questionLength + passwordLength + sessionInformationLength] = bArray[i];
}
return { msg: new Uint8Array(msg), hashmethod: crypto, codedigits: codeDigits};
},
genOCRAResult: function(hash,codeDigits) {
// OCRA size modulo : 0 1 2 3 4 5 6 7 8
var DIGITS_POWER = [1,10,100,1000,10000,100000,1000000,10000000,100000000];
// put selected bytes into result int
var offset = hash[hash.byteLength - 1] & 0xf;
var binary =
((hash[offset + 0] & 0x7f) << 24) |
((hash[offset + 1] & 0xff) << 16) |
((hash[offset + 2] & 0xff) << 8) |
(hash[offset + 3] & 0xff);
var otp = binary % DIGITS_POWER[codeDigits];
var result = otp.toString();
while (result.length < codeDigits) {
result = "0" + result;
}
return result;
},
generateOCRA : function(ocraSuite, key, counter, question, password, sessionInformation, timeStamp, cryptoEngine = "jsSHA")
{
var result=this.getOCRAmsg(ocraSuite, counter, question, password, sessionInformation, timeStamp);
var crypto = result.hashmethod;
var msgBuff = result.msg;
var codeDigits = result.codedigits;
var keyBuff = new Uint8Array(this.hexStr2Bytes(key));
if (cryptoEngine==="jsSHA")
var hash = this.hmacjssha_hash(crypto, keyBuff, msgBuff);
else if (cryptoEngine==="sjcl")
var hash = this.hmacsjcl_hash(crypto, keyBuff, msgBuff);
if (hash==null) return null;
return this.genOCRAResult(hash,codeDigits);
},
// return promise
// use then function to get result
generateOCRAasync : function(ocraSuite, key, counter, question, password, sessionInformation, timeStamp)
{
var result=this.getOCRAmsg(ocraSuite, counter, question, password, sessionInformation, timeStamp);
var crypto = result.hashmethod;
var msgBuff = result.msg;
var codeDigits = result.codedigits;
var keyBuff = new Uint8Array(this.hexStr2Bytes(key));
return this.hmacAsyncWeb_hash(crypto, keyBuff, msgBuff,codeDigits);
}
}