Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Implement generateKey for HMAC and AES-CBC
https://bugs.webkit.org/show_bug.cgi?id=123669 Reviewed by Dan Bernstein. Source/WebCore: Tests: crypto/subtle/aes-cbc-generate-key.html crypto/subtle/hmac-generate-key.html * WebCore.xcodeproj/project.pbxproj: Added new files. * bindings/js/JSCryptoAlgorithmDictionary.cpp: (WebCore::createAesKeyGenParams): Added bindings for AesKeyGenParams. (WebCore::JSCryptoAlgorithmDictionary::createParametersForGenerateKey): Handle algorithms that generate AES and HMAC keys. * bindings/js/JSSubtleCryptoCustom.cpp: (WebCore::JSSubtleCrypto::generateKey): Added. * crypto/CryptoAlgorithmAesKeyGenParams.h: Added. * crypto/CryptoKey.cpp: (WebCore::CryptoKey::randomData): * crypto/CryptoKey.h: * crypto/CryptoKeyMac.cpp: Added Expose a function that produces random data for symmetric crypto keys. Cross-platform implementation uses ARC4 code from WTF, while Mac uses a system function that provides a FIPS validated random number generator. * crypto/CryptoKeyAES.cpp: (WebCore::CryptoKeyAES::generate): * crypto/CryptoKeyAES.h: Added a function that creates AES keys. * crypto/SubtleCrypto.idl: Added generateKey. * crypto/algorithms/CryptoAlgorithmAES_CBC.cpp: (WebCore::CryptoAlgorithmAES_CBC::generateKey): Added. * crypto/algorithms/CryptoAlgorithmHMAC.cpp: (WebCore::CryptoAlgorithmHMAC::generateKey): Added. * crypto/keys/CryptoKeyHMAC.cpp: (WebCore::CryptoKeyHMAC::generate): * crypto/keys/CryptoKeyHMAC.h: Added a function that creates HMAC keys. * crypto/mac/CryptoAlgorithmAES_CBCMac.cpp: Removed generateKey stub, the implementation ended up in cross-platform file. * crypto/mac/CryptoAlgorithmHMACMac.cpp: Ditto. LayoutTests: * crypto/subtle/aes-cbc-generate-key-expected.txt: Added. * crypto/subtle/aes-cbc-generate-key.html: Added. * crypto/subtle/hmac-generate-key-expected.txt: Added. * crypto/subtle/hmac-generate-key.html: Added. * crypto/subtle/sha-1-expected.txt: Now that crypto.webkitSubtle.generateKey exists, a different exception is raised. Canonical link: https://commits.webkit.org/141879@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@158526 268f45cc-cd09-0410-ab3c-d52691b4dbfc
- Loading branch information
1 parent
69644d2
commit f656051
Showing
23 changed files
with
454 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
LayoutTests/crypto/subtle/aes-cbc-generate-key-expected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Test generating an AES key using AES-CBC algorithm. | ||
|
||
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE". | ||
|
||
|
||
PASS crypto.subtle.generateKey("aes-cbc", extractable, ["encrypt", "decrypt"]) threw exception TypeError: Type error. | ||
PASS crypto.subtle.generateKey({name: "aes-cbc"}, extractable, ["encrypt", "decrypt"]) threw exception TypeError: Type error. | ||
PASS crypto.subtle.generateKey({name: "aes-cbc", length: undefined}, extractable, ["encrypt", "decrypt"]) threw exception TypeError: Type error. | ||
PASS crypto.subtle.generateKey({name: "aes-cbc", length: {}}, extractable, ["encrypt", "decrypt"]) threw exception TypeError: Type error. | ||
Generating a key... | ||
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'] | ||
PASS successfullyParsed is true | ||
|
||
TEST COMPLETE | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!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 generating an AES key using AES-CBC algorithm."); | ||
|
||
jsTestIsAsync = true; | ||
|
||
if (!window.subtle) | ||
window.crypto.subtle = window.crypto.webkitSubtle; | ||
|
||
var extractable = true; | ||
|
||
shouldThrow('crypto.subtle.generateKey("aes-cbc", extractable, ["encrypt", "decrypt"])'); | ||
shouldThrow('crypto.subtle.generateKey({name: "aes-cbc"}, extractable, ["encrypt", "decrypt"])'); | ||
shouldThrow('crypto.subtle.generateKey({name: "aes-cbc", length: undefined}, extractable, ["encrypt", "decrypt"])'); | ||
shouldThrow('crypto.subtle.generateKey({name: "aes-cbc", length: {}}, extractable, ["encrypt", "decrypt"])'); | ||
|
||
debug("Generating a key..."); | ||
crypto.subtle.generateKey({name: "aes-cbc", length: 128}, 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']"); | ||
|
||
finishJSTest(); | ||
}); | ||
</script> | ||
|
||
<script src="../../resources/js-test-post.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Test generating a HMAC key. | ||
|
||
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE". | ||
|
||
|
||
PASS crypto.subtle.generateKey("hmac", extractable, ["sign", "verify"]) threw exception TypeError: Type error. | ||
PASS crypto.subtle.generateKey({name: "hmac"}, extractable, ["sign", "verify"]) threw exception Error: NotSupportedError: DOM Exception 9. | ||
PASS crypto.subtle.generateKey({name: "hmac", length: undefined}, extractable, ["sign", "verify"]) threw exception Error: NotSupportedError: DOM Exception 9. | ||
PASS crypto.subtle.generateKey({name: "hmac", length: {}}, extractable, ["sign", "verify"]) threw exception Error: NotSupportedError: DOM Exception 9. | ||
|
||
Generating a key with default length... | ||
PASS key.type is 'secret' | ||
PASS key.extractable is true | ||
PASS key.algorithm.name is 'hmac' | ||
PASS key.algorithm.length is 64 | ||
PASS key.usages is ["sign", "verify"] | ||
|
||
Generating a key with custom length... | ||
PASS key.type is 'secret' | ||
PASS key.extractable is true | ||
PASS key.algorithm.name is 'hmac' | ||
PASS key.algorithm.length is 5 | ||
PASS key.usages is ["sign"] | ||
PASS successfullyParsed is true | ||
|
||
TEST COMPLETE | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!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 generating a HMAC key."); | ||
|
||
jsTestIsAsync = true; | ||
|
||
if (!window.subtle) | ||
window.crypto.subtle = window.crypto.webkitSubtle; | ||
|
||
var extractable = true; | ||
|
||
shouldThrow('crypto.subtle.generateKey("hmac", extractable, ["sign", "verify"])'); | ||
shouldThrow('crypto.subtle.generateKey({name: "hmac"}, extractable, ["sign", "verify"])'); | ||
shouldThrow('crypto.subtle.generateKey({name: "hmac", length: undefined}, extractable, ["sign", "verify"])'); | ||
shouldThrow('crypto.subtle.generateKey({name: "hmac", length: {}}, extractable, ["sign", "verify"])'); | ||
|
||
debug("\nGenerating a key with default length..."); | ||
crypto.subtle.generateKey({name: "hmac", hash: "sha-1"}, extractable, ["sign", "verify"]).then(function(result) { | ||
key = result; | ||
|
||
shouldBe("key.type", "'secret'"); | ||
shouldBe("key.extractable", "true"); | ||
shouldBe("key.algorithm.name", "'hmac'"); | ||
shouldBe("key.algorithm.length", "64"); | ||
shouldBe("key.usages", '["sign", "verify"]'); | ||
|
||
debug("\nGenerating a key with custom length..."); | ||
return crypto.subtle.generateKey({name: "hmac", hash: "sha-1", length: 5}, extractable, ["sign"]); | ||
}).then(function(result) { | ||
key = result; | ||
|
||
shouldBe("key.type", "'secret'"); | ||
shouldBe("key.extractable", "true"); | ||
shouldBe("key.algorithm.name", "'hmac'"); | ||
shouldBe("key.algorithm.length", "5"); | ||
shouldBe("key.usages", '["sign"]'); | ||
finishJSTest(); | ||
}); | ||
</script> | ||
|
||
<script src="../../resources/js-test-post.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.