From 007e3170251fa8ec700003447f0f2e2d6b35dae7 Mon Sep 17 00:00:00 2001 From: Giulio Cesare Solaroli Date: Mon, 28 Nov 2011 01:00:32 +0000 Subject: [PATCH 1/4] Added a simple html page with a basic example on how to use the core Crypto functions Signed-off-by: Giulio Cesare Solaroli --- examples/example.css | 0 examples/example.js | 78 ++++++++++++++ examples/index.html | 237 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 examples/example.css create mode 100644 examples/example.js create mode 100644 examples/index.html diff --git a/examples/example.css b/examples/example.css new file mode 100644 index 0000000..e69de29 diff --git a/examples/example.js b/examples/example.js new file mode 100644 index 0000000..383f829 --- /dev/null +++ b/examples/example.js @@ -0,0 +1,78 @@ + +function init() { + console.log("INIT"); + + MochiKit.Signal.connect("doEncrypt", 'onclick', doEncrypt); + MochiKit.Signal.connect("doDecrypt", 'onclick', doDecrypt); + + Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose(); +} + + +function doEncrypt() { + var key; + var value; + + key = MochiKit.DOM.getElement('password').value; + value = MochiKit.DOM.getElement('plaintext').value; + + encrypt(key, value, MochiKit.DOM.getElement('encryptedtext')); +} + +function doDecrypt() { + var key; + var value; + + key = MochiKit.DOM.getElement('password').value; + value = MochiKit.DOM.getElement('encryptedtext').value; + + decrypt(key, value, MochiKit.DOM.getElement('plaintext')); +} + +function encrypt(aKey, aValue, aTextArea) { + var key, value; + var prng; + var deferredResult; + + key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); + value = new Clipperz.ByteArray(aValue); + prng = Clipperz.Crypto.PRNG.defaultRandomGenerator(); + + deferredResult = new Clipperz.Async.Deferred("encrypt", {trace: true}); + deferredResult.addCallback(MochiKit.Base.method(prng, 'deferredEntropyCollection')); + deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncrypt, key, value); + deferredResult.addCallback(MochiKit.Async.wait, 0.1); + deferredResult.addCallback(function(aResult) { + aTextArea.value = aResult.toBase64String(); + }); + deferredResult.addErrback(function(anError) { + aTextArea.value = "ERROR"; + }) + + deferredResult.callback(); +} + +function decrypt(aKey, aValue, aTextArea) { + var key, value; + var prng; + var deferredResult; + + key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); + value = new Clipperz.ByteArray().appendBase64String(aValue); + prng = Clipperz.Crypto.PRNG.defaultRandomGenerator(); + + deferredResult = new Clipperz.Async.Deferred("encrypt", {trace: true}); + deferredResult.addCallback(MochiKit.Base.method(prng, 'deferredEntropyCollection')); + deferredResult.addCallback(Clipperz.Crypto.AES.deferredDecrypt, key, value); + deferredResult.addCallback(MochiKit.Async.wait, 0.1); + deferredResult.addCallback(function(aResult) { + aTextArea.value = aResult.asString(); + }); + deferredResult.addErrback(function(anError) { + aTextArea.value = "ERROR"; + }) + + deferredResult.callback(); +} + +MochiKit.DOM.addLoadEvent(init); diff --git a/examples/index.html b/examples/index.html new file mode 100644 index 0000000..b6ede87 --- /dev/null +++ b/examples/index.html @@ -0,0 +1,237 @@ + + + Clipperz Javascript Crypto Library - Examples + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
+ + + + + + + + + \ No newline at end of file From 919b433355cb77985bd75773979b14aac85c9a31 Mon Sep 17 00:00:00 2001 From: Giulio Cesare Solaroli Date: Mon, 28 Nov 2011 01:14:00 +0000 Subject: [PATCH 2/4] Fixed html 'label' tags Signed-off-by: Giulio Cesare Solaroli --- examples/index.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/index.html b/examples/index.html index b6ede87..cb4462a 100644 --- a/examples/index.html +++ b/examples/index.html @@ -19,11 +19,11 @@ - +
- + From 0ee4a2bd9ca672cfd38abc6d5e7aa7f54007f519 Mon Sep 17 00:00:00 2001 From: Giulio Cesare Solaroli Date: Thu, 8 Dec 2011 23:33:47 +0000 Subject: [PATCH 3/4] Fixed keyboard entropy collector The keyboard entropy collector had an empty implementation; this has been fixed, and some stale code also been removed. Signed-off-by: Giulio Cesare Solaroli --- js/Clipperz/Crypto/PRNG.js | 79 +++++++------------------------------- 1 file changed, 13 insertions(+), 66 deletions(-) diff --git a/js/Clipperz/Crypto/PRNG.js b/js/Clipperz/Crypto/PRNG.js index 6af9031..70ddd95 100644 --- a/js/Clipperz/Crypto/PRNG.js +++ b/js/Clipperz/Crypto/PRNG.js @@ -195,17 +195,17 @@ Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new C }, //------------------------------------------------------------------------- - +/* 'numberOfRandomBits': function() { return 5; }, - +*/ //------------------------------------------------------------------------- - +/* 'pollingFrequency': function() { return 10; }, - +*/ //------------------------------------------------------------------------- __syntaxFix__: "syntax fix" }); @@ -287,17 +287,17 @@ Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new }, //------------------------------------------------------------------------- - +/* 'numberOfRandomBits': function() { return 1; }, - +*/ //------------------------------------------------------------------------- - +/* 'pollingFrequency': function() { return 10; }, - +*/ //------------------------------------------------------------------------- __syntaxFix__: "syntax fix" }); @@ -320,68 +320,15 @@ Clipperz.Crypto.PRNG.KeyboardRandomnessSource.prototype = MochiKit.Base.update(n //------------------------------------------------------------------------- - 'randomBitsCollector': function() { - return this._randomBitsCollector; - }, - - 'setRandomBitsCollector': function(aValue) { - this._randomBitsCollector = aValue; - }, - - 'appendRandomBitToRandomBitsCollector': function(aValue) { - var collectedBits; - var numberOfRandomBitsCollected; - - numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); - collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); - this.setRandomBitsCollector(collectetBits); - numberOfRandomBitsCollected ++; - - if (numberOfRandomBitsCollected == 8) { - this.updateGeneratorWithValue(collectetBits); - numberOfRandomBitsCollected = 0; - this.setRandomBitsCollector(0); - } - - this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) - }, - - //------------------------------------------------------------------------- - - 'numberOfRandomBitsCollected': function() { - return this._numberOfRandomBitsCollected; - }, - - 'setNumberOfRandomBitsCollected': function(aValue) { - this._numberOfRandomBitsCollected = aValue; - }, - - //------------------------------------------------------------------------- - 'collectEntropy': function(anEvent) { -/* - var mouseLocation; - var randomBit; + var now; + var entropyByte; + now = new Date(); + entropyByte = (now.getTime() & 0xff); - mouseLocation = anEvent.mouse().client; - - randomBit = ((mouseLocation.x ^ mouseLocation.y) & 0x1); - this.appendRandomBitToRandomBitsCollector(randomBit); -*/ - }, - - //------------------------------------------------------------------------- - - 'numberOfRandomBits': function() { - return 1; + this.updateGeneratorWithValue(entropyByte); }, - //------------------------------------------------------------------------- - - 'pollingFrequency': function() { - return 10; - }, - //------------------------------------------------------------------------- __syntaxFix__: "syntax fix" }); From 44b09eaffb4fb760b2c6970a9a392786411032fc Mon Sep 17 00:00:00 2001 From: Giulio Cesare Solaroli Date: Mon, 20 Feb 2012 00:25:30 +0000 Subject: [PATCH 4/4] ByteArray implementation using JavaScript native ArrayBuffer and Uint8Array classes Implemented a new version of ByteArray leveraging the new native classes ArrayBuffer and Uint8Array. Some changes have been applied also on SHA.js in order to bake it compatible with data passed around as Uint8Array instead of plain old Arrays. Some type checking and some fixes have been applied also to AES.js, even if in this case it should be fully backward compatible. Signed-off-by: Giulio Cesare Solaroli --- js/Clipperz/Async.js | 2 +- js/Clipperz/Base.js | 2 +- js/Clipperz/ByteArray.js | 355 +++++++++++++++++- js/Clipperz/Crypto/AES.js | 11 +- js/Clipperz/Crypto/Base.js | 2 +- js/Clipperz/Crypto/BigInt.js | 2 +- js/Clipperz/Crypto/BigInt_scoped.js | 2 +- js/Clipperz/Crypto/ECC/BinaryField/Curve.js | 2 +- .../Crypto/ECC/BinaryField/FiniteField.js | 2 +- js/Clipperz/Crypto/ECC/BinaryField/Point.js | 2 +- js/Clipperz/Crypto/ECC/BinaryField/Value.js | 2 +- js/Clipperz/Crypto/ECC/StandardCurves.js | 2 +- js/Clipperz/Crypto/PRNG.js | 8 +- js/Clipperz/Crypto/RSA.js | 2 +- js/Clipperz/Crypto/SHA.js | 196 +++++++++- js/Clipperz/Crypto/SRP.js | 2 +- js/Clipperz/Logging.js | 2 +- js/Clipperz/YUI/Utils.js | 2 +- tests/SimpleTest/SimpleTest.Async.js | 2 +- tests/index.html | 2 +- tests/tests/Clipperz/Async.html | 2 +- tests/tests/Clipperz/Async.test.js | 2 +- tests/tests/Clipperz/Base.html | 2 +- tests/tests/Clipperz/Base.test.js | 2 +- tests/tests/Clipperz/ByteArray.html | 10 +- tests/tests/Clipperz/ByteArray.test.js | 76 ++-- tests/tests/Clipperz/Crypto/AES.html | 10 +- tests/tests/Clipperz/Crypto/Base.html | 2 +- tests/tests/Clipperz/Crypto/BigInt.html | 4 +- .../Clipperz/Crypto/ECC.B283.deferred.html | 2 +- .../ECC.BinaryField.FiniteField.B283.html | 4 +- .../Crypto/ECC.BinaryField.FiniteField.html | 4 +- .../Crypto/ECC.BinaryField.Value.html | 4 +- .../Clipperz/Crypto/ECC.K283.deferred.html | 2 +- .../Clipperz/Crypto/JSCrypto_vs_Clipperz.html | 2 +- .../Crypto/JSCrypto_vs_Clipperz.test.js | 2 +- tests/tests/Clipperz/Crypto/PRNG.html | 4 +- tests/tests/Clipperz/Crypto/RSA.html | 2 +- tests/tests/Clipperz/Crypto/SHA.html | 4 +- tests/tests/Clipperz/Crypto/SHA.test.js | 2 +- tests/tests/Clipperz/Crypto/SRP.html | 2 +- tests/tests/Clipperz/Crypto/Usage.html | 8 +- tests/tests/Clipperz/Crypto/index.html | 2 +- tests/tests/Clipperz/Crypto/jscrypto.js | 2 +- tests/tests/Clipperz/index.html | 2 +- tests/tests/index.html | 2 +- 46 files changed, 652 insertions(+), 110 deletions(-) diff --git a/js/Clipperz/Async.js b/js/Clipperz/Async.js index bf62ebc..ea9a38f 100644 --- a/js/Clipperz/Async.js +++ b/js/Clipperz/Async.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. diff --git a/js/Clipperz/Base.js b/js/Clipperz/Base.js index 429c988..e369384 100644 --- a/js/Clipperz/Base.js +++ b/js/Clipperz/Base.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. diff --git a/js/Clipperz/ByteArray.js b/js/Clipperz/ByteArray.js index b7d33b3..cfc4ab1 100644 --- a/js/Clipperz/ByteArray.js +++ b/js/Clipperz/ByteArray.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. @@ -131,7 +131,6 @@ Clipperz.ByteArray_abstract.prototype = MochiKit.Base.update(null, { var ii, cc; var padding; -// padding = new Clipperz.ByteArray(); padding = this.newInstance(); cc = aLength - bLength; for (ii=0; ii> 4); if (value3 != -1) { byte2 = ((value2 & 0x0f) << 4) | ((value3 & 0x3c) >> 2); @@ -791,7 +793,7 @@ Clipperz.ByteArray_hex.prototype = MochiKit.Base.update(new Clipperz.ByteArray_a //------------------------------------------------------------------------- - 'appendBlock': function(aBlock) { + 'appendByteArray': function(aBlock) { this._value = this._value += aBlock.toHexString().substring(2); return this; @@ -1006,7 +1008,7 @@ Clipperz.ByteArray_array.prototype = MochiKit.Base.update(new Clipperz.ByteArray //------------------------------------------------------------------------- - 'appendBlock': function(aBlock) { + 'appendByteArray': function(aBlock) { MochiKit.Base.extend(this._value, aBlock._value); return this; @@ -1089,6 +1091,332 @@ Clipperz.ByteArray_array.prototype = MochiKit.Base.update(new Clipperz.ByteArray }); +//============================================================================= +// +// Clipperz.ByteArray_typedArray +// +//============================================================================= + +Clipperz.ByteArray_typedArray = function (args) { + this._usedBytes = 0; + + if (typeof(args) != 'undefined') { + if (args.constructor == Array) { + var i, c; + c = args.length; + result_value = this._allocateBuffer(c); + + for (i=0; i>> HEX String", args); + value = args.substring(2).toLowerCase(); + if (/[0123456789abcdef]*/.test(value)) { + if ((value.length % 2) != 0) { + value = "0" + value; + } + } else { +MochiKit.Logging.logError("Clipperz.ByteArray should be inizialized with an hex string."); + throw Clipperz.ByteArray.exception.InvalidValue; + } + + c = value.length / 2 +// result_value = this._allocateBuffer(c); + this._allocateBuffer(c); + + for (i=0; i 0xxxxxxx + byteLength += 1; + } else if (unicode <= 0x7ff) { // 0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx + byteLength += 2; + } else if (unicode <= 0xffff) { // 0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx + byteLength += 3; + } else { // 0x00010000 - 0x001fffff -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + byteLength += 4; + } + } + +// result_value = this._allocateBuffer(byteLength); +// bufferIndex = 0; + this._allocateBuffer(byteLength); + for (i=0; i 0xxxxxxx +// result_value[bufferIndex] = unicode; bufferIndex += 1; + this.appendByte(unicode); + } else if (unicode <= 0x7ff) { // 0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx +// result_value[bufferIndex] = ((unicode >> 6) | 0xc0); bufferIndex += 1; +// result_value[bufferIndex] = ((unicode & 0x3F) | 0x80); bufferIndex += 1; + this.appendByte((unicode >> 6) | 0xc0); + this.appendByte((unicode & 0x3F) | 0x80); + } else if (unicode <= 0xffff) { // 0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx +// result_value[bufferIndex] = ((unicode >> 12) | 0xe0); bufferIndex += 1; +// result_value[bufferIndex] = (((unicode >> 6) & 0x3f) | 0x80); bufferIndex += 1; +// result_value[bufferIndex] = ((unicode & 0x3f) | 0x80); bufferIndex += 1; + this.appendByte(((unicode >> 12) | 0xe0)); + this.appendByte(((unicode >> 6) & 0x3f) | 0x80); + this.appendByte((unicode & 0x3f) | 0x80); + } else { // 0x00010000 - 0x001fffff -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx +// result_value[bufferIndex] = ((unicode >> 18) | 0xf0); bufferIndex += 1; +// result_value[bufferIndex] = (((unicode >> 12) & 0x3f) | 0x80); bufferIndex += 1; +// result_value[bufferIndex] = (((unicode >> 6) & 0x3f) | 0x80); bufferIndex += 1; +// result_value[bufferIndex] = ((unicode & 0x3f) | 0x80); bufferIndex += 1; + this.appendByte((unicode >> 18) | 0xf0); + this.appendByte(((unicode >> 12) & 0x3f) | 0x80); + this.appendByte(((unicode >> 6) & 0x3f) | 0x80); + this.appendByte((unicode & 0x3f) | 0x80); + } + } +// this._usedBytes = bufferIndex; + } + +// this._value = result_value; + } else if ((args.constructor == Object) && (args.length == 0)) { + this._allocateBuffer(0) + } else if ((args.constructor == Object) && (args[0].toString() == 'Clipperz.ByteArray_typedArray')) { + var byteArray; + + byteArray = args[0]; +// this._buffer = byteArray._buffer.slice(0); +// this._value = new Uint8Array(this._buffer); + this._value = new Uint8Array(byteArray._value); // NEW + this._usedBytes = byteArray._usedBytes; + + } else if ((args.constructor == Object) && (args[0].toString() == '[object ArrayBuffer]')) { +// this._buffer = args[0]; +// this._value = new Uint8Array(this._buffer); +// this._usedBytes = this._buffer.byteLength; + this._value = new Uint8Array(args[0]); // NEW + this._usedBytes = this._value.length; // NEW + } else if ((args.constructor == Object) && (args[0].toString() == '[object Uint8Array]')) { + } else if (args.toString() == '[object Uint8Array]') { +//console.log("~~~>>> Uint8Array"); +// this._buffer = args.buffer; +// this._value = args; +// this._usedBytes = this._buffer.byteLength; + this._value = new Uint8Array(args); // NEW + this._usedBytes = args.length; // NEW + } else if (args.toString() == '[object ArrayBuffer]') { +//console.log("~~~>>> ArrayBuffer"); + } else { + var bytes; + + bytes = MochiKit.Base.extend(null, arguments); + this._allocateBuffer(bytes.length) + this.appendBytes(bytes); + } + } else { + this._allocateBuffer(0) + } + + return this; +} + +Clipperz.ByteArray_typedArray.prototype = MochiKit.Base.update(new Clipperz.ByteArray_abstract(), { + + //------------------------------------------------------------------------- + + '_allocateBuffer': function (byteLength) { + this._usedBytes = 0; + +// this._buffer = new ArrayBuffer(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(byteLength)); +// this._value = new Uint8Array(this._buffer); + this._value = new Uint8Array(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(byteLength)); + + return this._value; + }, + + //......................................................................... + + '_extendAllocatedBuffer': function (aValue) { +// var newBuffer; + var newValue; + var extension; + var i, c; + + extension = aValue; + if (extension == null) { + extension = 1; + } + +// newBuffer = new ArrayBuffer(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(this._buffer.byteLength + extension)); +// newValue = new Uint8Array(newBuffer); + newValue = new Uint8Array(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(this._value.length + extension)); // NEW + + c = this._usedBytes; + for (i=0; i>> ByteArray.appendByte", aValue); + if (aValue != null) { + this.checkByteValue(aValue); + +// if (this._usedBytes == this._buffer.byteLength) { + if (this._usedBytes == this._value.length) { // NEW + this._extendAllocatedBuffer(); + } + + this._value[this._usedBytes] = aValue; + this._usedBytes ++; + } + + return this; + }, + + //------------------------------------------------------------------------- + + 'byteAtIndex': function(anIndex) { + return this._value[anIndex]; + }, + + 'setByteAtIndex': function(aValue, anIndex) { + var missingBytes; + + this.checkByteValue(aValue); + + missingBytes = anIndex - this.length(); + + if (missingBytes < 0) { + this._value[anIndex] = aValue; + } else if (missingBytes == 0) { + this.appendByte(aValue); + } else { + var i,c; + + c = missingBytes; + this._extendAllocatedBuffer(c); + for (i=0; i>> (32 - c); for (i=0; i>> (c - i))) == 0)) { - newKeySeed.appendBlock(this.accumulators()[i].stack()); + newKeySeed.appendByteArray(this.accumulators()[i].stack()); this.accumulators()[i].resetStack(); } } @@ -499,14 +499,14 @@ Clipperz.log("### PRNG.readyToGenerateRandomBytes"); c = Math.ceil(aSize / (128 / 8)); for (i=0; i> 24) & 0xff); + message[message.length - 4] = ((messageLengthInBits >> 24) & 0xff); +// message.push((messageLengthInBits >> 16) & 0xff); + message[message.length - 3] = ((messageLengthInBits >> 16) & 0xff); +// message.push((messageLengthInBits >> 8) & 0xff); + message[message.length - 2] = ((messageLengthInBits >> 8) & 0xff); +// message.push( messageLengthInBits & 0xff); + message[message.length - 1] = ( messageLengthInBits & 0xff); + + currentMessageIndex = 0; + while(currentMessageIndex < message.length) { + var w; + var a, b, c, d, e, f, g, h; + + w = Array(64); + + _c = 16; + for (_i=0; _i<_c; _i++) { + var _j; + + _j = currentMessageIndex + _i*4; + w[_i] = (message[_j] << 24) | (message[_j + 1] << 16) | (message[_j + 2] << 8) | (message[_j + 3] << 0); + } + + _c = 64; + for (_i=16; _i<_c; _i++) { + var s0, s1; + + s0 = (rotateRight(w[_i-15], 7)) ^ (rotateRight(w[_i-15], 18)) ^ (shiftRight(w[_i-15], 3)); + s1 = (rotateRight(w[_i-2], 17)) ^ (rotateRight(w[_i-2], 19)) ^ (shiftRight(w[_i-2], 10)); + w[_i] = safeAdd(w[_i-16], s0, w[_i-7], s1); + } + + a=h0; b=h1; c=h2; d=h3; e=h4; f=h5; g=h6; h=h7; + + _c = 64; + for (_i=0; _i<_c; _i++) { + var s0, s1, ch, maj, t1, t2; + + s0 = (rotateRight(a, 2)) ^ (rotateRight(a, 13)) ^ (rotateRight(a, 22)); + maj = (a & b) ^ (a & c) ^ (b & c); + t2 = safeAdd(s0, maj); + s1 = (rotateRight(e, 6)) ^ (rotateRight(e, 11)) ^ (rotateRight(e, 25)); + ch = (e & f) ^ ((~e) & g); + t1 = safeAdd(h, s1, ch, k[_i], w[_i]); + + h = g; + g = f; + f = e; + e = safeAdd(d, t1); + d = c; + c = b; + b = a; + a = safeAdd(t1, t2); + } + + h0 = safeAdd(h0, a); + h1 = safeAdd(h1, b); + h2 = safeAdd(h2, c); + h3 = safeAdd(h3, d); + h4 = safeAdd(h4, e); + h5 = safeAdd(h5, f); + h6 = safeAdd(h6, g); + h7 = safeAdd(h7, h); + + currentMessageIndex += bytesPerBlock; + } + +// result = new Array(256/8); + result = new Uint8Array(256/8); + result[0] = (h0 >> 24) & 0xff; + result[1] = (h0 >> 16) & 0xff; + result[2] = (h0 >> 8) & 0xff; + result[3] = h0 & 0xff; + + result[4] = (h1 >> 24) & 0xff; + result[5] = (h1 >> 16) & 0xff; + result[6] = (h1 >> 8) & 0xff; + result[7] = h1 & 0xff; + + result[8] = (h2 >> 24) & 0xff; + result[9] = (h2 >> 16) & 0xff; + result[10] = (h2 >> 8) & 0xff; + result[11] = h2 & 0xff; + + result[12] = (h3 >> 24) & 0xff; + result[13] = (h3 >> 16) & 0xff; + result[14] = (h3 >> 8) & 0xff; + result[15] = h3 & 0xff; + + result[16] = (h4 >> 24) & 0xff; + result[17] = (h4 >> 16) & 0xff; + result[18] = (h4 >> 8) & 0xff; + result[19] = h4 & 0xff; + + result[20] = (h5 >> 24) & 0xff; + result[21] = (h5 >> 16) & 0xff; + result[22] = (h5 >> 8) & 0xff; + result[23] = h5 & 0xff; + + result[24] = (h6 >> 24) & 0xff; + result[25] = (h6 >> 16) & 0xff; + result[26] = (h6 >> 8) & 0xff; + result[27] = h6 & 0xff; + + result[28] = (h7 >> 24) & 0xff; + result[29] = (h7 >> 16) & 0xff; + result[30] = (h7 >> 8) & 0xff; + result[31] = h7 & 0xff; + +//Clipperz.Profile.stop("Clipperz.Crypto.SHA.sha256_arrayBuffer"); + return result; + }, + //----------------------------------------------------------------------------- 'sha256': function(aValue) { @@ -268,7 +453,8 @@ MochiKit.Base.update(Clipperz.Crypto.SHA, { var valueArray; valueArray = aValue.arrayValues(); - resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); +// resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); + resultArray = Clipperz.Crypto.SHA.sha256_arrayBuffer(valueArray); result = new Clipperz.ByteArray(resultArray); @@ -285,8 +471,10 @@ MochiKit.Base.update(Clipperz.Crypto.SHA, { var valueArray; valueArray = aValue.arrayValues(); - resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); - resultArray = Clipperz.Crypto.SHA.sha256_array(resultArray); +// resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); + resultArray = Clipperz.Crypto.SHA.sha256_arrayBuffer(valueArray); +// resultArray = Clipperz.Crypto.SHA.sha256_array(resultArray); + resultArray = Clipperz.Crypto.SHA.sha256_arrayBuffer(resultArray); result = new Clipperz.ByteArray(resultArray); diff --git a/js/Clipperz/Crypto/SRP.js b/js/Clipperz/Crypto/SRP.js index 9101b13..49a31af 100644 --- a/js/Clipperz/Crypto/SRP.js +++ b/js/Clipperz/Crypto/SRP.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. diff --git a/js/Clipperz/Logging.js b/js/Clipperz/Logging.js index de6b776..7103106 100644 --- a/js/Clipperz/Logging.js +++ b/js/Clipperz/Logging.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. diff --git a/js/Clipperz/YUI/Utils.js b/js/Clipperz/YUI/Utils.js index d12b9c1..cf7198c 100644 --- a/js/Clipperz/YUI/Utils.js +++ b/js/Clipperz/YUI/Utils.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. diff --git a/tests/SimpleTest/SimpleTest.Async.js b/tests/SimpleTest/SimpleTest.Async.js index 2c44b60..6376382 100644 --- a/tests/SimpleTest/SimpleTest.Async.js +++ b/tests/SimpleTest/SimpleTest.Async.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. diff --git a/tests/index.html b/tests/index.html index 125e7d3..d75e4d4 100644 --- a/tests/index.html +++ b/tests/index.html @@ -1,6 +1,6 @@ - + +--> diff --git a/tests/tests/Clipperz/ByteArray.test.js b/tests/tests/Clipperz/ByteArray.test.js index 6243585..e793cfe 100644 --- a/tests/tests/Clipperz/ByteArray.test.js +++ b/tests/tests/Clipperz/ByteArray.test.js @@ -1,6 +1,6 @@ /* -Copyright 2008-2011 Clipperz Srl +Copyright 2008-2012 Clipperz Srl This file is part of Clipperz's Javascript Crypto Library. @@ -25,17 +25,17 @@ refer to http://www.clipperz.com */ -Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose(); +//Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose(); var tests = { //------------------------------------------------------------------------- 'core_tests': function (someTestArgs) { -// var deferredResult; + var deferredResult; -// deferredResult = new Clipperz.Async.Deferred("core_tests", someTestArgs); -// deferredResult.addCallback(function() { + deferredResult = new Clipperz.Async.Deferred("core_tests", someTestArgs); + deferredResult.addCallback(function() { var byteArray; byteArray = new Clipperz.ByteArray(); @@ -44,32 +44,47 @@ var tests = { byteArray.checkByteValue(512); is(false, true, "a value greater that a byte (0x200) should have raised an exception - NO Exception"); } catch(e) { -// is( e.name, -// "Clipperz.ByteArray.exception.InvalidValue", -// "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") ok( /Clipperz\.ByteArray\.exception\.InvalidValue.*/.test(e.name), "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") }; - -// }); -// deferredResult.callback(); + }); + deferredResult.callback(); -// return deferredResult; + return deferredResult; + }, + + //------------------------------------------------------------------------- + + 'typedArray_core_tests': function (someTestArgs) { + var deferredResult; + + deferredResult = new Clipperz.Async.Deferred("typedArray_core_tests", someTestArgs); + deferredResult.addCallback(function() { + is(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(0), 256, "Allocated bytes for given size"); + is(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(100), 256, "Allocated bytes for given size"); + is(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(256), 256, "Allocated bytes for given size"); + is(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(257), 256 * 2, "Allocated bytes for given size"); + is(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(500), 256 * 2, "Allocated bytes for given size"); + is(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(512), 256 * 2, "Allocated bytes for given size"); + is(Clipperz.ByteArray_typedArray.bytesToAllocateForByteCount(513), 256 * 3, "Allocated bytes for given size"); + }); + deferredResult.callback(); + return deferredResult; }, - + //------------------------------------------------------------------------- 'basic_tests': function (someTestArgs) { var deferredResult; - deferredResult = new Clipperz.Async.Deferred("simple_tests", someTestArgs); + deferredResult = new Clipperz.Async.Deferred("basic_tests", someTestArgs); deferredResult.addCallback(function() { var byteArray; var byteArray2; var byteArrayIterator; var nextBlock; - + byteArray = new Clipperz.ByteArray(); is(byteArray.length(), 0, "before adding any element the length is 0"); byteArray.appendByte(10); @@ -169,9 +184,6 @@ var tests = { byteArray.appendByte(512); is(false, true, "appending a value greater that a byte (0x200) should have raised an exception - NO Exception"); } catch(e) { -// is( e.name, -// "Clipperz.ByteArray.exception.InvalidValue", -// "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") ok( /Clipperz\.ByteArray\.exception\.InvalidValue.*/.test(e.name), "appending a value greater that a byte (0x200) should have raised an exception - EXCEPTION HANDLER") }; @@ -180,9 +192,6 @@ var tests = { byteArray.appendByte(256); is(false, true, "appending a value greater that a byte (0x100) should have raised an exception - NO Exception"); } catch(e) { -// is( e.name, -// "Clipperz.ByteArray.exception.InvalidValue", -// "appending a value greater that a byte (0x100) should have raised an exception - EXCEPTION HANDLER") ok( /Clipperz\.ByteArray\.exception\.InvalidValue.*/.test(e.name), "appending a value greater that a byte (0x100) should have raised an exception - EXCEPTION HANDLER") }; @@ -353,17 +362,17 @@ var tests = { //------------------------------------------------------------------------- - 'appendBlock_tests': function (someTestArgs) { + 'appendByteArray_tests': function (someTestArgs) { var deferredResult; - deferredResult = new Clipperz.Async.Deferred("appendBlock_tests", someTestArgs); + deferredResult = new Clipperz.Async.Deferred("appendByteArray_tests", someTestArgs); deferredResult.addCallback(function() { var byteArray; var byteArray2; byteArray = new Clipperz.ByteArray("0xa1b2c3d4"); byteArray2 = new Clipperz.ByteArray("0x1a2b3c4d"); - is(byteArray.appendBlock(byteArray2).toHexString(), "0xa1b2c3d41a2b3c4d", "the appendBlock method works fine"); + is(byteArray.appendByteArray(byteArray2).toHexString(), "0xa1b2c3d41a2b3c4d", "the appendByteArray method works fine"); }); deferredResult.callback(); @@ -472,16 +481,15 @@ var tests = { is(new Clipperz.ByteArray("0xe4b899").asString().charCodeAt(0), 19993, "String decoding - Three bytes char: CJK ideograph - 丙"); is(new Clipperz.ByteArray("0xefbfbf").asString().charCodeAt(0), 65535, "String decoding - Three bytes character - top"); -/* - is(new Clipperz.ByteArray("0xf0908080").asString().charCodeAt(0), 65536, "String decoding - Four bytes character - bottom"); - is(new Clipperz.ByteArray("0xf0a08294").asString().charCodeAt(0), 131220, "String decoding - Four bytes char: CJK extended ideograph - ”"); - is(new Clipperz.ByteArray("0xf3b48980").asString().charCodeAt(0), 1000000, "String decoding - Four bytes character - middle"); - is(new Clipperz.ByteArray("0xf7bfbfbf").asString().charCodeAt(0), 2097151, "String decoding - Four bytes character - top"); - - is(String.fromCharCode(65535).charCodeAt(0), 65535, "test fromCharCode - charCodeAt (65535)"); - is(String.fromCharCode(65536).charCodeAt(0), 65536, "test fromCharCode - charCodeAt (65536)"); - is(String.fromCharCode(1000000).charCodeAt(0), 1000000, "test fromCharCode - charCodeAt (1000000)"); -*/ +// is(new Clipperz.ByteArray("0xf0908080").asString().charCodeAt(0), 65536, "String decoding - Four bytes character - bottom"); +// is(new Clipperz.ByteArray("0xf0a08294").asString().charCodeAt(0), 131220, "String decoding - Four bytes char: CJK extended ideograph - ”"); +// is(new Clipperz.ByteArray("0xf3b48980").asString().charCodeAt(0), 1000000, "String decoding - Four bytes character - middle"); +// is(new Clipperz.ByteArray("0xf7bfbfbf").asString().charCodeAt(0), 2097151, "String decoding - Four bytes character - top"); +// +// is(String.fromCharCode(65535).charCodeAt(0), 65535, "test fromCharCode - charCodeAt (65535)"); +// is(String.fromCharCode(65536).charCodeAt(0), 65536, "test fromCharCode - charCodeAt (65536)"); +// is(String.fromCharCode(1000000).charCodeAt(0), 1000000, "test fromCharCode - charCodeAt (1000000)"); + //---------------------------------------------------------- byteArray = new Clipperz.ByteArray("ABCD"); diff --git a/tests/tests/Clipperz/Crypto/AES.html b/tests/tests/Clipperz/Crypto/AES.html index e1d84a2..50299b6 100644 --- a/tests/tests/Clipperz/Crypto/AES.html +++ b/tests/tests/Clipperz/Crypto/AES.html @@ -1,6 +1,6 @@
diff --git a/tests/tests/Clipperz/Crypto/ECC.B283.deferred.html b/tests/tests/Clipperz/Crypto/ECC.B283.deferred.html
index 8b5f8de..d80a4ec 100644
--- a/tests/tests/Clipperz/Crypto/ECC.B283.deferred.html
+++ b/tests/tests/Clipperz/Crypto/ECC.B283.deferred.html
@@ -1,6 +1,6 @@
 
 
 
 
diff --git a/tests/tests/Clipperz/Crypto/ECC.BinaryField.FiniteField.html b/tests/tests/Clipperz/Crypto/ECC.BinaryField.FiniteField.html
index d4ee899..040828d 100644
--- a/tests/tests/Clipperz/Crypto/ECC.BinaryField.FiniteField.html
+++ b/tests/tests/Clipperz/Crypto/ECC.BinaryField.FiniteField.html
@@ -1,6 +1,6 @@
 
 
 
 
diff --git a/tests/tests/Clipperz/Crypto/ECC.BinaryField.Value.html b/tests/tests/Clipperz/Crypto/ECC.BinaryField.Value.html
index 708a81d..003b7d5 100644
--- a/tests/tests/Clipperz/Crypto/ECC.BinaryField.Value.html
+++ b/tests/tests/Clipperz/Crypto/ECC.BinaryField.Value.html
@@ -1,6 +1,6 @@
 
 
 
 
diff --git a/tests/tests/Clipperz/Crypto/ECC.K283.deferred.html b/tests/tests/Clipperz/Crypto/ECC.K283.deferred.html
index 1e8c2e3..359d223 100644
--- a/tests/tests/Clipperz/Crypto/ECC.K283.deferred.html
+++ b/tests/tests/Clipperz/Crypto/ECC.K283.deferred.html
@@ -1,6 +1,6 @@
 
 
 
 	
diff --git a/tests/tests/Clipperz/Crypto/RSA.html b/tests/tests/Clipperz/Crypto/RSA.html
index 599b3e0..cd1c48f 100644
--- a/tests/tests/Clipperz/Crypto/RSA.html
+++ b/tests/tests/Clipperz/Crypto/RSA.html
@@ -1,6 +1,6 @@