зеркало из https://github.com/mozilla/gecko-dev.git
Back out Bug 610914 as it causes Bug 618068.
This commit is contained in:
Родитель
a602bc20ee
Коммит
56ed182429
|
@ -499,22 +499,11 @@ WeaveCrypto.prototype = {
|
|||
return "" + outputBuffer.readString() + "";
|
||||
},
|
||||
|
||||
_importSymKey: function _importSymKey(slot, mechanism, origin, op, keyItem) {
|
||||
let symKey = this.nss.PK11_ImportSymKey(slot, mechanism, origin, op, keyItem.address(), null);
|
||||
if (symKey.isNull())
|
||||
throw Components.Exception("symkey import failed", Cr.NS_ERROR_FAILURE);
|
||||
return symKey;
|
||||
},
|
||||
|
||||
_freeSymKey: function _freeSymKey(symKey) {
|
||||
if (symKey && !symKey.isNull())
|
||||
this.nss.PK11_FreeSymKey(symKey);
|
||||
},
|
||||
|
||||
_commonCrypt : function (input, output, symmetricKey, iv, operation) {
|
||||
this.log("_commonCrypt() called");
|
||||
// Get rid of the base64 encoding and convert to SECItems.
|
||||
let keyItem = this.makeSECItem(symmetricKey, true, true);
|
||||
let keyItem = this.makeSECItem(symmetricKey, true);
|
||||
let ivItem = this.makeSECItem(iv, true);
|
||||
|
||||
// Determine which (padded) PKCS#11 mechanism to use.
|
||||
|
@ -534,7 +523,10 @@ WeaveCrypto.prototype = {
|
|||
if (slot.isNull())
|
||||
throw Components.Exception("can't get internal key slot", Cr.NS_ERROR_FAILURE);
|
||||
|
||||
symKey = this._importSymKey(slot, mechanism, this.nss.PK11_OriginUnwrap, operation, keyItem);
|
||||
symKey = this.nss.PK11_ImportSymKey(slot, mechanism, this.nss.PK11_OriginUnwrap, operation, keyItem.address(), null);
|
||||
if (symKey.isNull())
|
||||
throw Components.Exception("symkey import failed", Cr.NS_ERROR_FAILURE);
|
||||
|
||||
ctx = this.nss.PK11_CreateContextBySymKey(mechanism, operation, symKey, ivParam);
|
||||
if (ctx.isNull())
|
||||
throw Components.Exception("couldn't create context for symkey", Cr.NS_ERROR_FAILURE);
|
||||
|
@ -565,7 +557,8 @@ WeaveCrypto.prototype = {
|
|||
} finally {
|
||||
if (ctx && !ctx.isNull())
|
||||
this.nss.PK11_DestroyContext(ctx, true);
|
||||
this._freeSymKey(symKey);
|
||||
if (symKey && !symKey.isNull())
|
||||
this.nss.PK11_FreeSymKey(symKey);
|
||||
if (slot && !slot.isNull())
|
||||
this.nss.PK11_FreeSlot(slot);
|
||||
if (ivParam && !ivParam.isNull())
|
||||
|
@ -700,6 +693,7 @@ WeaveCrypto.prototype = {
|
|||
return new this.nss_t.SECItem(this.nss.SIBUFFER, outputData, outputData.length);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Returns the expanded data string for the derived key.
|
||||
*/
|
||||
|
@ -757,45 +751,3 @@ WeaveCrypto.prototype = {
|
|||
}
|
||||
},
|
||||
};
|
||||
|
||||
// Memoize makeSECItem for symmetric keys.
|
||||
WeaveCrypto.prototype.makeSECItem =
|
||||
(function (orig) {
|
||||
let memo = {};
|
||||
|
||||
return function(input, isEncoded, memoize) {
|
||||
if (memoize) {
|
||||
let memoKey = "" + input + !!isEncoded;
|
||||
let val = memo[memoKey];
|
||||
if (!val) {
|
||||
val = orig.apply(this, arguments);
|
||||
memo[memoKey] = val;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
return orig.apply(this, arguments);
|
||||
};
|
||||
}(WeaveCrypto.prototype.makeSECItem));
|
||||
|
||||
WeaveCrypto.prototype._importSymKey =
|
||||
(function (orig) {
|
||||
let memo = {}
|
||||
|
||||
return function(slot, mechanism, origin, op, keyItem) {
|
||||
// keyItem lookup is already memoized, so we can directly use the address.
|
||||
// Slot changes each time. Don't use it as memo key input.
|
||||
let memoKey = "" + "-" + mechanism +
|
||||
origin + op + "-" + keyItem.address();
|
||||
let val = memo[memoKey];
|
||||
if (!val) {
|
||||
val = orig.apply(this, arguments);
|
||||
memo[memoKey] = val;
|
||||
}
|
||||
return val;
|
||||
};
|
||||
}(WeaveCrypto.prototype._importSymKey));
|
||||
|
||||
// Yes, this leaks. However, _importSymKey is now memoized, so the average user
|
||||
// will have only a single key, which we persist for the lifetime of the
|
||||
// session...
|
||||
WeaveCrypto.prototype._freeSymKey = function(symKey) {};
|
||||
|
|
|
@ -8,69 +8,7 @@ try {
|
|||
.getService(Ci.IWeaveCrypto);
|
||||
}
|
||||
|
||||
function weavecrypto_memo() {
|
||||
if (!cryptoSvc._importSymKey)
|
||||
return
|
||||
|
||||
let w = new WeaveCrypto();
|
||||
let key = w.generateRandomKey();
|
||||
let keyItem1 = w.makeSECItem(key, true, true);
|
||||
let keyItem2 = w.makeSECItem(key, true, true);
|
||||
do_check_eq(keyItem1, keyItem2);
|
||||
|
||||
do_check_eq("" + w.nss.PK11_AlgtagToMechanism(w.algorithm),
|
||||
"" + w.nss.PK11_AlgtagToMechanism(w.algorithm));
|
||||
|
||||
let symKey1 =
|
||||
w._importSymKey(w.nss.PK11_GetInternalKeySlot(),
|
||||
w.nss.PK11_AlgtagToMechanism(w.algorithm),
|
||||
w.nss.PK11_OriginUnwrap,
|
||||
w.nss.CKA_DECRYPT,
|
||||
keyItem1);
|
||||
let symKey2 =
|
||||
w._importSymKey(w.nss.PK11_GetInternalKeySlot(),
|
||||
w.nss.PK11_AlgtagToMechanism(w.algorithm),
|
||||
w.nss.PK11_OriginUnwrap,
|
||||
w.nss.CKA_DECRYPT,
|
||||
keyItem1);
|
||||
do_check_eq(symKey1, symKey2);
|
||||
}
|
||||
|
||||
/*
|
||||
With memoization:
|
||||
make check-one 10.39s user 0.75s system 100% cpu 11.041 total
|
||||
nsStringStats
|
||||
=> mAllocCount: 1923
|
||||
=> mReallocCount: 306
|
||||
=> mFreeCount: 1923
|
||||
=> mShareCount: 6764
|
||||
=> mAdoptCount: 101
|
||||
=> mAdoptFreeCount: 101
|
||||
<<<<<<<
|
||||
|
||||
Without memoization, it crashes after a few thousand iterations... and 5610 take
|
||||
make check-one 7.57s user 0.67s system 101% cpu 8.105 total
|
||||
nsStringStats
|
||||
=> mAllocCount: 1923
|
||||
=> mReallocCount: 306
|
||||
=> mFreeCount: 1923
|
||||
=> mShareCount: 6764
|
||||
=> mAdoptCount: 101
|
||||
=> mAdoptFreeCount: 101
|
||||
<<<<<<<
|
||||
*/
|
||||
function multiple_decrypts(iterations) {
|
||||
let iv = cryptoSvc.generateRandomIV();
|
||||
let key = cryptoSvc.generateRandomKey();
|
||||
let cipherText = cryptoSvc.encrypt("Hello, world.", key, iv);
|
||||
|
||||
for (let i = 0; i < iterations; ++i) {
|
||||
let clearText = cryptoSvc.decrypt(cipherText, key, iv);
|
||||
do_check_eq(clearText + " " + i, "Hello, world. " + i);
|
||||
}
|
||||
}
|
||||
|
||||
function test_encryption() {
|
||||
function run_test() {
|
||||
// First, do a normal run with expected usage... Generate a random key and
|
||||
// iv, encrypt and decrypt a string.
|
||||
var iv = cryptoSvc.generateRandomIV();
|
||||
|
@ -224,9 +162,3 @@ function test_encryption() {
|
|||
do_check_true(failure);
|
||||
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
weavecrypto_memo();
|
||||
multiple_decrypts(6000);
|
||||
test_encryption();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче