Commit ea43d19f authored by ap@apple.com's avatar ap@apple.com

Add WebCrypto AES-CBC

https://bugs.webkit.org/show_bug.cgi?id=123647

Reviewed by Anders Carlsson.

Source/WebCore:

Tests: crypto/subtle/aes-cbc-192-encrypt-decrypt.html
       crypto/subtle/aes-cbc-256-encrypt-decrypt.html
       crypto/subtle/aes-cbc-encrypt-decrypt-with-padding.html
       crypto/subtle/aes-cbc-encrypt-decrypt.html
       crypto/subtle/aes-cbc-invalid-length.html
       crypto/subtle/aes-cbc-wrong-key-class.html

* WebCore.xcodeproj/project.pbxproj: Added new files.
* bindings/js/JSCryptoAlgorithmDictionary.cpp:
(WebCore::getProperty): Factored out a function to get a property as JSValue.
(WebCore::getHashAlgorithm): Use it.
(WebCore::createAesCbcParams): Added converter for AesCbcParams.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForEncrypt): Support AES_CBC.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForDecrypt): Ditto.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForImportKey): Support all
algorithms, all the new ones just have trivial dictionaries.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForExportKey): Ditto.

* bindings/js/JSCryptoOperationData.cpp: Added.
(WebCore::sequenceOfCryptoOperationDataFromJSValue):
(WebCore::cryptoOperationDataFromJSValue):
* bindings/js/JSCryptoOperationData.h: Added.
Moved CryptoOperationData bindings out of JSSubtleCryptoCustom.cpp, so that we
could use them in JSCryptoAlgorithmDictionary.cpp.

* bindings/js/JSDOMPromise.h: (WebCore::PromiseWrapper::reject): Added a specialization
to reject with null result. The spec doesn't actually say how algorithms fail.

* bindings/js/JSSubtleCryptoCustom.cpp:
(WebCore::JSSubtleCrypto::encrypt): Added.
(WebCore::JSSubtleCrypto::decrypt): Ditto.
(WebCore::JSSubtleCrypto::sign): Style fix.

* crypto/CryptoAlgorithmAesCbcParams.h: Added.

* crypto/CryptoKey.h:
(WebCore::CryptoKeyClass):
* crypto/keys/CryptoKeyHMAC.h:
(WebCore::asCryptoKeyHMAC):
Added poor man's RTTI, so that we can safely upcast Keys passed fro JavaScript code.

* crypto/CryptoKeyAES.cpp: Added.
(WebCore::CryptoKeyAES::CryptoKeyAES):
(WebCore::CryptoKeyAES::~CryptoKeyAES):
(WebCore::CryptoKeyAES::buildAlgorithmDescription):
* crypto/CryptoKeyAES.h: Added.
(WebCore::asCryptoKeyAES):
AES keys are the same for all algorithms, but they still need to remember the algorithm.

* crypto/SubtleCrypto.idl: Added encrypt/decrypt.

* crypto/algorithms/CryptoAlgorithmAES_CBC.cpp: Added.
(WebCore::CryptoAlgorithmAES_CBC::CryptoAlgorithmAES_CBC):
(WebCore::CryptoAlgorithmAES_CBC::~CryptoAlgorithmAES_CBC):
(WebCore::CryptoAlgorithmAES_CBC::create):
(WebCore::CryptoAlgorithmAES_CBC::identifier):
(WebCore::CryptoAlgorithmAES_CBC::importKey):
(WebCore::CryptoAlgorithmAES_CBC::exportKey):
* crypto/algorithms/CryptoAlgorithmAES_CBC.h: Added.
* crypto/mac/CryptoAlgorithmAES_CBCMac.cpp: Added.
(WebCore::transformAES_CBC):
(WebCore::CryptoAlgorithmAES_CBC::encrypt):
(WebCore::CryptoAlgorithmAES_CBC::decrypt):
(WebCore::CryptoAlgorithmAES_CBC::generateKey):
Added.

* crypto/mac/CryptoAlgorithmHMACMac.cpp:
(WebCore::CryptoAlgorithmHMAC::sign):
(WebCore::CryptoAlgorithmHMAC::verify):
Check key class before casting it to CryptoKeyHMAC.

* crypto/mac/CryptoAlgorithmRegistryMac.cpp:
(WebCore::CryptoAlgorithmRegistry::platformRegisterAlgorithms): Register AES-CBC
on Mac, so that it can be used.

Source/WTF:

* wtf/FixedArray.h: (WTF::FixedArray::data): Added a const version of the function.

* wtf/Vector.h: Added a comment to Vector constructor about how it is different
from std::vector.

LayoutTests:

Part of aes-cbc-encrypt-decrypt.html test was taken from Blink.

* crypto/subtle/aes-cbc-192-encrypt-decrypt-expected.txt: Added.
* crypto/subtle/aes-cbc-192-encrypt-decrypt.html: Added.
* crypto/subtle/aes-cbc-256-encrypt-decrypt-expected.txt: Added.
* crypto/subtle/aes-cbc-256-encrypt-decrypt.html: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt-expected.txt: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt-with-padding-expected.txt: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt-with-padding.html: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt.html: Added.
* crypto/subtle/aes-cbc-invalid-length-expected.txt: Added.
* crypto/subtle/aes-cbc-invalid-length.html: Added.
* crypto/subtle/aes-cbc-wrong-key-class-expected.txt: Added.
* crypto/subtle/aes-cbc-wrong-key-class.html: Added.

* crypto/subtle/hmac-sign-verify-expected.txt:
* crypto/subtle/hmac-sign-verify.html:
Corrected a description.

* crypto/subtle/resources/common.js: (hexToArrayBuffer): Added a helper.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@158485 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 148c2879
2013-11-01 Alexey Proskuryakov <ap@apple.com>
Add WebCrypto AES-CBC
https://bugs.webkit.org/show_bug.cgi?id=123647
Reviewed by Anders Carlsson.
Part of aes-cbc-encrypt-decrypt.html test was taken from Blink.
* crypto/subtle/aes-cbc-192-encrypt-decrypt-expected.txt: Added.
* crypto/subtle/aes-cbc-192-encrypt-decrypt.html: Added.
* crypto/subtle/aes-cbc-256-encrypt-decrypt-expected.txt: Added.
* crypto/subtle/aes-cbc-256-encrypt-decrypt.html: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt-expected.txt: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt-with-padding-expected.txt: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt-with-padding.html: Added.
* crypto/subtle/aes-cbc-encrypt-decrypt.html: Added.
* crypto/subtle/aes-cbc-invalid-length-expected.txt: Added.
* crypto/subtle/aes-cbc-invalid-length.html: Added.
* crypto/subtle/aes-cbc-wrong-key-class-expected.txt: Added.
* crypto/subtle/aes-cbc-wrong-key-class.html: Added.
* crypto/subtle/hmac-sign-verify-expected.txt:
* crypto/subtle/hmac-sign-verify.html:
Corrected a description.
* crypto/subtle/resources/common.js: (hexToArrayBuffer): Added a helper.
2013-11-01 Jaehun Lim <ljaehun.lim@samsung.com>
Update layout test results after CSS calc() simplification
......
Test AES-CBC encrypt and decrypt functions with a 256 bit key.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
Importing a raw AES key from string literal...
PASS key.type is 'secret'
PASS key.extractable is true
PASS key.algorithm.name is 'aes-cbc'
PASS key.algorithm.length is 192
PASS key.usages is ['encrypt', 'decrypt']
Using the key to encrypt plaintext...
PASS byteArrayToHexString(new Uint8Array(encryptionResult)) is '[4f 02 1d b2 43 bc 63 3d 71 78 18 3a 9f a0 71 e8 b4 d9 ad a9 ad 7d ed f4 e5 e7 38 76 3f 69 14 5a 57 1b 24 20 12 fb 7a e0 7f a9 ba ac 3d f1 02 e0 08 b0 e2 79 88 59 88 81 d9 20 a9 e6 4f 56 15 cd 61 2c cd 79 22 4b 35 09 35 d4 5d d6 a9 8f 81 76]'
Decrypting it back...
PASS new Uint8Array(decryptionResult) is plaintext
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test AES-CBC encrypt and decrypt functions with a 256 bit key.");
jsTestIsAsync = true;
if (!window.subtle)
window.crypto.subtle = window.crypto.webkitSubtle;
var keyData = hexToArrayBuffer("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
var iv = hexToArrayBuffer("000102030405060708090a0b0c0d0e0f");
var plaintext = hexToArrayBuffer("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
var extractable = true;
debug("Importing a raw AES key from string literal...");
crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'aes-cbc'");
shouldBe("key.algorithm.length", "192");
shouldBe("key.usages", "['encrypt', 'decrypt']");
debug("Using the key to encrypt plaintext...");
return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext]);
}).then(function(result) {
encryptionResult = result;
shouldBe("byteArrayToHexString(new Uint8Array(encryptionResult))", "'[4f 02 1d b2 43 bc 63 3d 71 78 18 3a 9f a0 71 e8 b4 d9 ad a9 ad 7d ed f4 e5 e7 38 76 3f 69 14 5a 57 1b 24 20 12 fb 7a e0 7f a9 ba ac 3d f1 02 e0 08 b0 e2 79 88 59 88 81 d9 20 a9 e6 4f 56 15 cd 61 2c cd 79 22 4b 35 09 35 d4 5d d6 a9 8f 81 76]'");
debug("Decrypting it back...");
return crypto.subtle.decrypt({name: "aes-cbc", iv: iv}, key, [result]);
}).then(function(result) {
decryptionResult = result;
shouldBe("new Uint8Array(decryptionResult)", "plaintext");
finishJSTest();
});
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
Test AES-CBC encrypt and decrypt functions with a 256 bit key.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
Importing a raw AES key from string literal...
PASS key.type is 'secret'
PASS key.extractable is true
PASS key.algorithm.name is 'aes-cbc'
PASS key.algorithm.length is 256
PASS key.usages is ['encrypt', 'decrypt']
Using the key to encrypt plaintext...
PASS byteArrayToHexString(new Uint8Array(encryptionResult)) is '[f5 8c 4c 04 d6 e5 f1 ba 77 9e ab fb 5f 7b fb d6 9c fc 4e 96 7e db 80 8d 67 9f 77 7b c6 70 2c 7d 39 f2 33 69 a9 d9 ba cf a5 30 e2 63 04 23 14 61 b2 eb 05 e2 c3 9b e9 fc da 6c 19 07 8c 6a 9d 1b 3f 46 17 96 d6 b0 d6 b2 e0 c2 a7 2b 4d 80 e6 44]'
Decrypting it back...
PASS new Uint8Array(decryptionResult) is plaintext
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test AES-CBC encrypt and decrypt functions with a 256 bit key.");
jsTestIsAsync = true;
if (!window.subtle)
window.crypto.subtle = window.crypto.webkitSubtle;
var keyData = hexToArrayBuffer("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
var iv = hexToArrayBuffer("000102030405060708090a0b0c0d0e0f");
var plaintext = hexToArrayBuffer("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
var extractable = true;
debug("Importing a raw AES key from string literal...");
crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'aes-cbc'");
shouldBe("key.algorithm.length", "256");
shouldBe("key.usages", "['encrypt', 'decrypt']");
debug("Using the key to encrypt plaintext...");
return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext]);
}).then(function(result) {
encryptionResult = result;
shouldBe("byteArrayToHexString(new Uint8Array(encryptionResult))", "'[f5 8c 4c 04 d6 e5 f1 ba 77 9e ab fb 5f 7b fb d6 9c fc 4e 96 7e db 80 8d 67 9f 77 7b c6 70 2c 7d 39 f2 33 69 a9 d9 ba cf a5 30 e2 63 04 23 14 61 b2 eb 05 e2 c3 9b e9 fc da 6c 19 07 8c 6a 9d 1b 3f 46 17 96 d6 b0 d6 b2 e0 c2 a7 2b 4d 80 e6 44]'");
debug("Decrypting it back...");
return crypto.subtle.decrypt({name: "aes-cbc", iv: iv}, key, [result]);
}).then(function(result) {
decryptionResult = result;
shouldBe("new Uint8Array(decryptionResult)", "plaintext");
finishJSTest();
});
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
Test AES-CBC encrypt and decrypt functions.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
Importing a raw AES key from string literal...
PASS key.type is 'secret'
PASS key.extractable is true
PASS key.algorithm.name is 'aes-cbc'
PASS key.algorithm.length is 128
PASS key.usages is ['encrypt', 'decrypt']
Using the key to encrypt plaintext...
PASS byteArrayToHexString(new Uint8Array(encryptionResult)) is '[76 49 ab ac 81 19 b2 46 ce e9 8e 9b 12 e9 19 7d 50 86 cb 9b 50 72 19 ee 95 db 11 3a 91 76 78 b2 73 be d6 b8 e3 c1 74 3b 71 16 e6 9e 22 22 95 16 3f f1 ca a1 68 1f ac 09 12 0e ca 30 75 86 e1 a7 8c b8 28 07 23 0e 13 21 d3 fa e0 0d 18 cc 20 12]'
Decrypting it back...
PASS new Uint8Array(decryptionResult) is plaintext
Testing initialization vector bindings...
PASS crypto.subtle.encrypt({name: 'AES-CBC', iv: null}, key, [plaintext]) threw exception TypeError: Only ArrayBuffer and ArrayBufferView objects can be passed as CryptoOperationData.
PASS crypto.subtle.encrypt({name: 'AES-CBC'}, key, [plaintext]) threw exception TypeError: Only ArrayBuffer and ArrayBufferView objects can be passed as CryptoOperationData.
PASS crypto.subtle.encrypt({name: 'AES-CBC', iv: 3}, key, [plaintext]) threw exception TypeError: Only ArrayBuffer and ArrayBufferView objects can be passed as CryptoOperationData.
PASS crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array([0])}, key, [plaintext]) threw exception Error: AES-CBC initialization data must be 16 bytes.
PASS successfullyParsed is true
TEST COMPLETE
Test AES-CBC encrypt and decrypt functions on a plaintext that is not a multiple of block size in length.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
Importing a raw AES key from string literal...
PASS key.type is 'secret'
PASS key.extractable is true
PASS key.algorithm.name is 'aes-cbc'
PASS key.algorithm.length is 128
PASS key.usages is ['encrypt', 'decrypt']
Using the key to encrypt plaintext...
= [63 01 99 c5 f2 02 cc 71 67 bb 84 c6 c7 2b 34 9d]
Decrypting it back...
PASS new Uint8Array(decryptionResult) is plaintext
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test AES-CBC encrypt and decrypt functions on a plaintext that is not a multiple of block size in length.");
jsTestIsAsync = true;
if (!window.subtle)
window.crypto.subtle = window.crypto.webkitSubtle;
var keyData = hexToArrayBuffer("2b7e151628aed2a6abf7158809cf4f3c");
var iv = hexToArrayBuffer("000102030405060708090a0b0c0d0e0f");
var plaintext = asciiToArrayBuffer("test");
var extractable = true;
debug("Importing a raw AES key from string literal...");
crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'aes-cbc'");
shouldBe("key.algorithm.length", "128");
shouldBe("key.usages", "['encrypt', 'decrypt']");
debug("Using the key to encrypt plaintext...");
return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext]);
}).then(function(result) {
printAcceptedResult(result);
debug("Decrypting it back...");
return crypto.subtle.decrypt({name: "aes-cbc", iv: iv}, key, [result]);
}).then(function(result) {
decryptionResult = result;
shouldBe("new Uint8Array(decryptionResult)", "plaintext");
finishJSTest();
});
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test AES-CBC encrypt and decrypt functions.");
jsTestIsAsync = true;
if (!window.subtle)
window.crypto.subtle = window.crypto.webkitSubtle;
var keyData = hexToArrayBuffer("2b7e151628aed2a6abf7158809cf4f3c");
var iv = hexToArrayBuffer("000102030405060708090a0b0c0d0e0f");
var plaintext = hexToArrayBuffer("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
var extractable = true;
debug("Importing a raw AES key from string literal...");
crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'aes-cbc'");
shouldBe("key.algorithm.length", "128");
shouldBe("key.usages", "['encrypt', 'decrypt']");
debug("Using the key to encrypt plaintext...");
return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext]);
}).then(function(result) {
encryptionResult = result;
shouldBe("byteArrayToHexString(new Uint8Array(encryptionResult))", "'[76 49 ab ac 81 19 b2 46 ce e9 8e 9b 12 e9 19 7d 50 86 cb 9b 50 72 19 ee 95 db 11 3a 91 76 78 b2 73 be d6 b8 e3 c1 74 3b 71 16 e6 9e 22 22 95 16 3f f1 ca a1 68 1f ac 09 12 0e ca 30 75 86 e1 a7 8c b8 28 07 23 0e 13 21 d3 fa e0 0d 18 cc 20 12]'");
debug("Decrypting it back...");
return crypto.subtle.decrypt({name: "aes-cbc", iv: iv}, key, [result]);
}).then(function(result) {
decryptionResult = result;
shouldBe("new Uint8Array(decryptionResult)", "plaintext");
debug("Testing initialization vector bindings...");
shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: null}, key, [plaintext])");
shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC'}, key, [plaintext])");
shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: 3}, key, [plaintext])");
shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array([0])}, key, [plaintext])");
finishJSTest();
});
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
CONSOLE MESSAGE: line 38: SyntaxError: Unexpected identifier 'finishJSTest'. Expected ')' to end a argument list.
FAIL successfullyParsed should be true (of type boolean). Was undefined (of type undefined).
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test an unsupported AES key length.");
jsTestIsAsync = true;
if (!window.subtle)
window.crypto.subtle = window.crypto.webkitSubtle;
var keyData = hexToArrayBuffer("8e73b0f7da0e6452c810f32b809079e562f8ead2522c");
var iv = hexToArrayBuffer("000102030405060708090a0b0c0d0e0f");
var plaintext = hexToArrayBuffer("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
var extractable = true;
debug("Importing a raw AES key from string literal...");
crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
key = result;
shouldBe("key.type", "'secret'");
shouldBe("key.extractable", "true");
shouldBe("key.algorithm.name", "'aes-cbc'");
shouldBe("key.algorithm.length", "176");
shouldBe("key.usages", "['encrypt', 'decrypt']");
debug("Using the key to encrypt plaintext...");
return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext]);
}).then(undefined, function(result) {
debug("Failed, as expected. Note that the spec doesn't appear to clearly define which step should fail."
finishJSTest();
});
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
Test calling AES-CBC encrypt with a HMAC key.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
Importing a raw HMAC key from string literal...
Using the key to encrypt plaintext...
PASS crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext]) threw exception Error: NotSupportedError: DOM Exception 9.
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test calling AES-CBC encrypt with a HMAC key.");
jsTestIsAsync = true;
if (!window.subtle)
window.crypto.subtle = window.crypto.webkitSubtle;
var iv = hexToArrayBuffer("000102030405060708090a0b0c0d0e0f");
var plaintext = hexToArrayBuffer("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
var hmacKey = asciiToArrayBuffer('a');
var extractable = true;
debug("Importing a raw HMAC key from string literal...");
crypto.subtle.importKey("raw", hmacKey, {name: 'hmac', hash: {name: 'sha-1'}}, extractable, ["sign", "verify", "encrypt", "decrypt"]).then(function(result) {
key = result;
debug("Using the key to encrypt plaintext...");
shouldThrow('crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext])')
finishJSTest();
});
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
Test crypto.subtle.digest.
Test HMAC sign and verify functions.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
......
......@@ -9,7 +9,7 @@
<div id="console"></div>
<script>
description("Test crypto.subtle.digest.");
description("Test HMAC sign and verify functions.");
jsTestIsAsync = true;
......
......@@ -45,6 +45,16 @@ function asciiToArrayBuffer(str)
return new Uint8Array(chars);
}
function hexToArrayBuffer(str)
{
if (str.length % 2)
throw "Hex string lenght must be even";
var chars = [];
for (var i = 0; i < str.length; i += 2)
chars.push(parseInt(str.substr(i, 2), 16));
return new Uint8Array(chars);
}
function printRejectedResult(value)
{
debug(" rejected with value of " + value);
......
2013-11-01 Alexey Proskuryakov <ap@apple.com>
Add WebCrypto AES-CBC
https://bugs.webkit.org/show_bug.cgi?id=123647
Reviewed by Anders Carlsson.
* wtf/FixedArray.h: (WTF::FixedArray::data): Added a const version of the function.
* wtf/Vector.h: Added a comment to Vector constructor about how it is different
from std::vector.
2013-11-01 Joseph Pecoraro <pecoraro@apple.com>
Unreviewed extended attempt at Windows build fix after r158471.
......
......@@ -45,6 +45,7 @@ public:
}
T* data() { return m_data; }
const T* data() const { return m_data; }
size_t size() const { return Size; }
private:
......
......@@ -532,7 +532,8 @@ public:
Vector()
{
}
// Unlike in std::vector, this constructor does not initialize POD types.
explicit Vector(size_t size)
: Base(size, size)
{
......
2013-11-01 Alexey Proskuryakov <ap@apple.com>
Add WebCrypto AES-CBC
https://bugs.webkit.org/show_bug.cgi?id=123647
Reviewed by Anders Carlsson.
Tests: crypto/subtle/aes-cbc-192-encrypt-decrypt.html
crypto/subtle/aes-cbc-256-encrypt-decrypt.html
crypto/subtle/aes-cbc-encrypt-decrypt-with-padding.html
crypto/subtle/aes-cbc-encrypt-decrypt.html
crypto/subtle/aes-cbc-invalid-length.html
crypto/subtle/aes-cbc-wrong-key-class.html
* WebCore.xcodeproj/project.pbxproj: Added new files.
* bindings/js/JSCryptoAlgorithmDictionary.cpp:
(WebCore::getProperty): Factored out a function to get a property as JSValue.
(WebCore::getHashAlgorithm): Use it.
(WebCore::createAesCbcParams): Added converter for AesCbcParams.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForEncrypt): Support AES_CBC.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForDecrypt): Ditto.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForImportKey): Support all
algorithms, all the new ones just have trivial dictionaries.
(WebCore::JSCryptoAlgorithmDictionary::createParametersForExportKey): Ditto.
* bindings/js/JSCryptoOperationData.cpp: Added.
(WebCore::sequenceOfCryptoOperationDataFromJSValue):
(WebCore::cryptoOperationDataFromJSValue):
* bindings/js/JSCryptoOperationData.h: Added.
Moved CryptoOperationData bindings out of JSSubtleCryptoCustom.cpp, so that we
could use them in JSCryptoAlgorithmDictionary.cpp.
* bindings/js/JSDOMPromise.h: (WebCore::PromiseWrapper::reject): Added a specialization
to reject with null result. The spec doesn't actually say how algorithms fail.
* bindings/js/JSSubtleCryptoCustom.cpp:
(WebCore::JSSubtleCrypto::encrypt): Added.
(WebCore::JSSubtleCrypto::decrypt): Ditto.
(WebCore::JSSubtleCrypto::sign): Style fix.
* crypto/CryptoAlgorithmAesCbcParams.h: Added.
* crypto/CryptoKey.h:
(WebCore::CryptoKeyClass):
* crypto/keys/CryptoKeyHMAC.h:
(WebCore::asCryptoKeyHMAC):
Added poor man's RTTI, so that we can safely upcast Keys passed fro JavaScript code.
* crypto/CryptoKeyAES.cpp: Added.
(WebCore::CryptoKeyAES::CryptoKeyAES):
(WebCore::CryptoKeyAES::~CryptoKeyAES):
(WebCore::CryptoKeyAES::buildAlgorithmDescription):
* crypto/CryptoKeyAES.h: Added.
(WebCore::asCryptoKeyAES):
AES keys are the same for all algorithms, but they still need to remember the algorithm.
* crypto/SubtleCrypto.idl: Added encrypt/decrypt.
* crypto/algorithms/CryptoAlgorithmAES_CBC.cpp: Added.
(WebCore::CryptoAlgorithmAES_CBC::CryptoAlgorithmAES_CBC):
(WebCore::CryptoAlgorithmAES_CBC::~CryptoAlgorithmAES_CBC):
(WebCore::CryptoAlgorithmAES_CBC::create):
(WebCore::CryptoAlgorithmAES_CBC::identifier):
(WebCore::CryptoAlgorithmAES_CBC::importKey):
(WebCore::CryptoAlgorithmAES_CBC::exportKey):
* crypto/algorithms/CryptoAlgorithmAES_CBC.h: Added.
* crypto/mac/CryptoAlgorithmAES_CBCMac.cpp: Added.
(WebCore::transformAES_CBC):
(WebCore::CryptoAlgorithmAES_CBC::encrypt):
(WebCore::CryptoAlgorithmAES_CBC::decrypt):
(WebCore::CryptoAlgorithmAES_CBC::generateKey):
Added.
* crypto/mac/CryptoAlgorithmHMACMac.cpp:
(WebCore::CryptoAlgorithmHMAC::sign):
(WebCore::CryptoAlgorithmHMAC::verify):
Check key class before casting it to CryptoKeyHMAC.
* crypto/mac/CryptoAlgorithmRegistryMac.cpp:
(WebCore::CryptoAlgorithmRegistry::platformRegisterAlgorithms): Register AES-CBC
on Mac, so that it can be used.
2013-11-01 Andreas Kling <akling@apple.com>
SVGRenderStyle accessors should return references.
......
......@@ -5500,6 +5500,14 @@
E125F8351822F18A00D84CD9 /* CryptoKeyHMAC.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E125F8331822F18A00D84CD9 /* CryptoKeyHMAC.cpp */; };
E125F8361822F18A00D84CD9 /* CryptoKeyHMAC.h in Headers */ = {isa = PBXBuildFile; fileRef = E125F8341822F18A00D84CD9 /* CryptoKeyHMAC.h */; };
E125F8381822F1EB00D84CD9 /* CryptoAlgorithmHMACMac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E125F8371822F1EB00D84CD9 /* CryptoAlgorithmHMACMac.cpp */; };
E125F83A1824104800D84CD9 /* CryptoAlgorithmAesCbcParams.h in Headers */ = {isa = PBXBuildFile; fileRef = E125F8391824104800D84CD9 /* CryptoAlgorithmAesCbcParams.h */; };
E125F83D182411E700D84CD9 /* JSCryptoOperationData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E125F83B182411E700D84CD9 /* JSCryptoOperationData.cpp */; };
E125F83E182411E700D84CD9 /* JSCryptoOperationData.h in Headers */ = {isa = PBXBuildFile; fileRef = E125F83C182411E700D84CD9 /* JSCryptoOperationData.h */; };
E125F8411824253A00D84CD9 /* CryptoAlgorithmAES_CBC.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E125F83F1824253A00D84CD9 /* CryptoAlgorithmAES_CBC.cpp */; };
E125F8421824253A00D84CD9 /* CryptoAlgorithmAES_CBC.h in Headers */ = {isa = PBXBuildFile; fileRef = E125F8401824253A00D84CD9 /* CryptoAlgorithmAES_CBC.h */; };