Changeset 158582 in webkit


Ignore:
Timestamp:
Nov 4, 2013 10:45:03 AM (10 years ago)
Author:
ap@apple.com
Message:

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.

Location:
trunk
Files:
17 edited
6 copied

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r158578 r158582  
     12013-11-04  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Implement generateKey for HMAC and AES-CBC
     4        https://bugs.webkit.org/show_bug.cgi?id=123669
     5
     6        Reviewed by Dan Bernstein.
     7
     8        * crypto/subtle/aes-cbc-generate-key-expected.txt: Added.
     9        * crypto/subtle/aes-cbc-generate-key.html: Added.
     10        * crypto/subtle/hmac-generate-key-expected.txt: Added.
     11        * crypto/subtle/hmac-generate-key.html: Added.
     12
     13        * crypto/subtle/sha-1-expected.txt: Now that crypto.webkitSubtle.generateKey exists,
     14        a different exception is raised.
     15
    1162013-11-04  Commit Queue  <commit-queue@webkit.org>
    217
  • trunk/LayoutTests/crypto/subtle/sha-1-expected.txt

    r158578 r158582  
    1212SHA1 of [new Uint8Array([0, 1, 2, 3, 4]), new Uint8Array(5, 6, 7, 8, 9, 10])]
    1313    = [2c 7e 7c 38 4f 78 29 69 42 82 b1 e3 a6 21 6d ef 80 82 d0 55]
    14 PASS crypto.subtle.generateKey('sha-1') threw exception TypeError: undefined is not a function (evaluating 'crypto.subtle.generateKey('sha-1')').
     14PASS crypto.subtle.generateKey('sha-1') threw exception Error: NotSupportedError: DOM Exception 9.
    1515PASS successfullyParsed is true
    1616
  • trunk/Source/WebCore/ChangeLog

    r158581 r158582  
     12013-11-04  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Implement generateKey for HMAC and AES-CBC
     4        https://bugs.webkit.org/show_bug.cgi?id=123669
     5
     6        Reviewed by Dan Bernstein.
     7
     8        Tests: crypto/subtle/aes-cbc-generate-key.html
     9               crypto/subtle/hmac-generate-key.html
     10
     11        * WebCore.xcodeproj/project.pbxproj: Added new files.
     12
     13        * bindings/js/JSCryptoAlgorithmDictionary.cpp:
     14        (WebCore::createAesKeyGenParams): Added bindings for AesKeyGenParams.
     15        (WebCore::JSCryptoAlgorithmDictionary::createParametersForGenerateKey): Handle
     16        algorithms that generate AES and HMAC keys.
     17
     18        * bindings/js/JSSubtleCryptoCustom.cpp: (WebCore::JSSubtleCrypto::generateKey): Added.
     19
     20        * crypto/CryptoAlgorithmAesKeyGenParams.h: Added.
     21
     22        * crypto/CryptoKey.cpp: (WebCore::CryptoKey::randomData):
     23        * crypto/CryptoKey.h:
     24        * crypto/CryptoKeyMac.cpp: Added
     25        Expose a function that produces random data for symmetric crypto keys. Cross-platform
     26        implementation uses ARC4 code from WTF, while Mac uses a system function that
     27        provides a FIPS validated random number generator.
     28
     29        * crypto/CryptoKeyAES.cpp: (WebCore::CryptoKeyAES::generate):
     30        * crypto/CryptoKeyAES.h:
     31        Added a function that creates AES keys.
     32
     33        * crypto/SubtleCrypto.idl: Added generateKey.
     34
     35        * crypto/algorithms/CryptoAlgorithmAES_CBC.cpp:
     36        (WebCore::CryptoAlgorithmAES_CBC::generateKey): Added.
     37
     38        * crypto/algorithms/CryptoAlgorithmHMAC.cpp:
     39        (WebCore::CryptoAlgorithmHMAC::generateKey): Added.
     40
     41        * crypto/keys/CryptoKeyHMAC.cpp: (WebCore::CryptoKeyHMAC::generate):
     42        * crypto/keys/CryptoKeyHMAC.h:
     43        Added a function that creates HMAC keys.
     44
     45        * crypto/mac/CryptoAlgorithmAES_CBCMac.cpp: Removed generateKey stub, the implementation
     46        ended up in cross-platform file.
     47
     48        * crypto/mac/CryptoAlgorithmHMACMac.cpp: Ditto.
     49
    1502013-11-04  Daniel Bates  <dabates@apple.com>
    251
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r158578 r158582  
    55835583                E18772F1126E2629003DD586 /* Language.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E18772F0126E2629003DD586 /* Language.cpp */; };
    55845584                E19727161820549E00592D51 /* CryptoKeyType.h in Headers */ = {isa = PBXBuildFile; fileRef = E19727151820549E00592D51 /* CryptoKeyType.h */; };
     5585                E19AC3F71824E5D100349426 /* CryptoAlgorithmAesKeyGenParams.h in Headers */ = {isa = PBXBuildFile; fileRef = E19AC3F61824E5D100349426 /* CryptoAlgorithmAesKeyGenParams.h */; };
     5586                E19AC3F9182566F700349426 /* CryptoKeyMac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E19AC3F8182566F700349426 /* CryptoKeyMac.cpp */; };
    55855587                E19AC3E21824DC6900349426 /* CryptoAlgorithmSHA224Mac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E19AC3DE1824DC6900349426 /* CryptoAlgorithmSHA224Mac.cpp */; };
    55865588                E19AC3E31824DC6900349426 /* CryptoAlgorithmSHA256Mac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E19AC3DF1824DC6900349426 /* CryptoAlgorithmSHA256Mac.cpp */; };
     
    1261512617                E18772F0126E2629003DD586 /* Language.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Language.cpp; sourceTree = "<group>"; };
    1261612618                E19727151820549E00592D51 /* CryptoKeyType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoKeyType.h; sourceTree = "<group>"; };
     12619                E19AC3F61824E5D100349426 /* CryptoAlgorithmAesKeyGenParams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoAlgorithmAesKeyGenParams.h; sourceTree = "<group>"; };
     12620                E19AC3F8182566F700349426 /* CryptoKeyMac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoKeyMac.cpp; sourceTree = "<group>"; };
    1261712621                E19AC3DE1824DC6900349426 /* CryptoAlgorithmSHA224Mac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CryptoAlgorithmSHA224Mac.cpp; path = mac/CryptoAlgorithmSHA224Mac.cpp; sourceTree = "<group>"; };
    1261812622                E19AC3DF1824DC6900349426 /* CryptoAlgorithmSHA256Mac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CryptoAlgorithmSHA256Mac.cpp; path = mac/CryptoAlgorithmSHA256Mac.cpp; sourceTree = "<group>"; };
     
    2017720181                                E19AC3E01824DC6900349426 /* CryptoAlgorithmSHA384Mac.cpp */,
    2017820182                                E19AC3E11824DC6900349426 /* CryptoAlgorithmSHA512Mac.cpp */,
     20183                                E19AC3F8182566F700349426 /* CryptoKeyMac.cpp */,
    2017920184                        );
    2018020185                        name = mac;
     
    2021720222                        children = (
    2021820223                                E125F8391824104800D84CD9 /* CryptoAlgorithmAesCbcParams.h */,
     20224                                E19AC3F61824E5D100349426 /* CryptoAlgorithmAesKeyGenParams.h */,
    2021920225                                E19DA29B18189ADD00088BC8 /* CryptoAlgorithmHmacKeyParams.h */,
    2022020226                                E1C6571E1816E50300256CDD /* CryptoAlgorithmHmacParams.h */,
     
    2319223198                                76808B50159DADFA002B5233 /* JSHTMLDialogElement.h in Headers */,
    2319323199                                1A85B1E70A1B240500D8C87C /* JSHTMLDirectoryElement.h in Headers */,
     23200                                E19AC3F71824E5D100349426 /* CryptoAlgorithmAesKeyGenParams.h in Headers */,
    2319423201                                1A85B2B70A1B2AC700D8C87C /* JSHTMLDivElement.h in Headers */,
    2319523202                                1A85B1E90A1B240500D8C87C /* JSHTMLDListElement.h in Headers */,
     
    2701027017                                E19AC3E51824DC6900349426 /* CryptoAlgorithmSHA512Mac.cpp in Sources */,
    2701127018                                D6E528A3149A926D00EFE1F3 /* MutationObserverInterestGroup.cpp in Sources */,
     27019                                E19AC3F9182566F700349426 /* CryptoKeyMac.cpp in Sources */,
    2701227020                                D6E276AF14637455001D280A /* MutationObserverRegistration.cpp in Sources */,
    2701327021                                C6F08FBC1430FE8F00685849 /* MutationRecord.cpp in Sources */,
  • trunk/Source/WebCore/bindings/js/JSCryptoAlgorithmDictionary.cpp

    r158578 r158582  
    3030
    3131#include "CryptoAlgorithmAesCbcParams.h"
     32#include "CryptoAlgorithmAesKeyGenParams.h"
    3233#include "CryptoAlgorithmHmacKeyParams.h"
    3334#include "CryptoAlgorithmHmacParams.h"
     
    127128        return nullptr;
    128129
    129     std::unique_ptr<CryptoAlgorithmAesCbcParams> result = std::make_unique<CryptoAlgorithmAesCbcParams>();
     130    auto result = std::make_unique<CryptoAlgorithmAesCbcParams>();
    130131
    131132    CryptoOperationData ivData;
     
    145146}
    146147
    147 static std::unique_ptr<CryptoAlgorithmParameters> createHmacParams(JSC::ExecState* exec, JSC::JSValue value)
     148static std::unique_ptr<CryptoAlgorithmParameters> createAesKeyGenParams(JSC::ExecState* exec, JSC::JSValue value)
    148149{
    149150    if (!value.isObject()) {
     
    152153    }
    153154
     155    auto result = std::make_unique<CryptoAlgorithmAesKeyGenParams>();
     156
     157    JSValue lengthValue = getProperty(exec, value.getObject(), "length");
     158    if (exec->hadException())
     159        return nullptr;
     160
     161    result->length = toUInt16(exec, lengthValue, EnforceRange);
     162
     163    return std::move(result);
     164}
     165
     166static std::unique_ptr<CryptoAlgorithmParameters> createHmacParams(JSC::ExecState* exec, JSC::JSValue value)
     167{
     168    if (!value.isObject()) {
     169        throwTypeError(exec);
     170        return nullptr;
     171    }
     172
    154173    JSDictionary jsDictionary(exec, value.getObject());
    155     std::unique_ptr<CryptoAlgorithmHmacParams> result = std::make_unique<CryptoAlgorithmHmacParams>();
     174    auto result = std::make_unique<CryptoAlgorithmHmacParams>();
    156175
    157176    if (!getHashAlgorithm(jsDictionary, result->hash)) {
     
    171190
    172191    JSDictionary jsDictionary(exec, value.getObject());
    173     std::unique_ptr<CryptoAlgorithmHmacKeyParams> result = std::make_unique<CryptoAlgorithmHmacKeyParams>();
     192    auto result = std::make_unique<CryptoAlgorithmHmacKeyParams>();
    174193
    175194    if (!getHashAlgorithm(jsDictionary, result->hash)) {
     
    345364}
    346365
    347 std::unique_ptr<CryptoAlgorithmParameters> JSCryptoAlgorithmDictionary::createParametersForGenerateKey(JSC::ExecState* exec, CryptoAlgorithmIdentifier algorithm, JSC::JSValue)
    348 {
    349     switch (algorithm) {
    350     case CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5:
    351     case CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5:
    352     case CryptoAlgorithmIdentifier::RSA_PSS:
    353     case CryptoAlgorithmIdentifier::RSA_OAEP:
    354     case CryptoAlgorithmIdentifier::ECDSA:
    355     case CryptoAlgorithmIdentifier::ECDH:
    356     case CryptoAlgorithmIdentifier::AES_CTR:
    357     case CryptoAlgorithmIdentifier::AES_CBC:
    358     case CryptoAlgorithmIdentifier::AES_CMAC:
    359     case CryptoAlgorithmIdentifier::AES_GCM:
    360     case CryptoAlgorithmIdentifier::AES_CFB:
    361     case CryptoAlgorithmIdentifier::HMAC:
     366std::unique_ptr<CryptoAlgorithmParameters> JSCryptoAlgorithmDictionary::createParametersForGenerateKey(JSC::ExecState* exec, CryptoAlgorithmIdentifier algorithm, JSC::JSValue value)
     367{
     368    switch (algorithm) {
     369    case CryptoAlgorithmIdentifier::RSAES_PKCS1_v1_5:
     370    case CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5:
     371    case CryptoAlgorithmIdentifier::RSA_PSS:
     372    case CryptoAlgorithmIdentifier::RSA_OAEP:
     373    case CryptoAlgorithmIdentifier::ECDSA:
     374    case CryptoAlgorithmIdentifier::ECDH:
     375        setDOMException(exec, NOT_SUPPORTED_ERR);
     376        return nullptr;
     377    case CryptoAlgorithmIdentifier::AES_CTR:
     378    case CryptoAlgorithmIdentifier::AES_CBC:
     379    case CryptoAlgorithmIdentifier::AES_CMAC:
     380    case CryptoAlgorithmIdentifier::AES_GCM:
     381    case CryptoAlgorithmIdentifier::AES_CFB:
     382        return createAesKeyGenParams(exec, value);
     383    case CryptoAlgorithmIdentifier::HMAC:
     384        return createHmacKeyParams(exec, value);
    362385    case CryptoAlgorithmIdentifier::DH:
    363386    case CryptoAlgorithmIdentifier::SHA_1:
  • trunk/Source/WebCore/bindings/js/JSSubtleCryptoCustom.cpp

    r158578 r158582  
    338338}
    339339
     340JSValue JSSubtleCrypto::generateKey(JSC::ExecState* exec)
     341{
     342    if (exec->argumentCount() < 1)
     343        return exec->vm().throwException(exec, createNotEnoughArgumentsError(exec));
     344
     345    auto algorithm = createAlgorithmFromJSValue(exec, exec->uncheckedArgument(0));
     346    if (!algorithm) {
     347        ASSERT(exec->hadException());
     348        return jsUndefined();
     349    }
     350
     351    auto parameters = JSCryptoAlgorithmDictionary::createParametersForGenerateKey(exec, algorithm->identifier(), exec->uncheckedArgument(0));
     352    if (!parameters) {
     353        ASSERT(exec->hadException());
     354        return jsUndefined();
     355    }
     356
     357    bool extractable = false;
     358    if (exec->argumentCount() >= 2) {
     359        extractable = exec->uncheckedArgument(1).toBoolean(exec);
     360        if (exec->hadException())
     361            return jsUndefined();
     362    }
     363
     364    CryptoKeyUsage keyUsages = 0;
     365    if (exec->argumentCount() >= 3) {
     366        if (!cryptoKeyUsagesFromJSValue(exec, exec->argument(2), keyUsages)) {
     367            ASSERT(exec->hadException());
     368            return jsUndefined();
     369        }
     370    }
     371
     372    JSPromise* promise = JSPromise::createWithResolver(exec->vm(), globalObject());
     373    auto promiseWrapper = PromiseWrapper::create(globalObject(), promise);
     374
     375    ExceptionCode ec = 0;
     376    algorithm->generateKey(*parameters, extractable, keyUsages, std::move(promiseWrapper), ec);
     377    if (ec) {
     378        setDOMException(exec, ec);
     379        return jsUndefined();
     380    }
     381
     382    return promise;
     383}
     384
    340385JSValue JSSubtleCrypto::importKey(JSC::ExecState* exec)
    341386{
  • trunk/Source/WebCore/crypto/CryptoKey.cpp

    r158578 r158582  
    3131#include "CryptoAlgorithmDescriptionBuilder.h"
    3232#include "CryptoAlgorithmRegistry.h"
     33#include <wtf/CryptographicallyRandomNumber.h>
    3334#include <wtf/text/WTFString.h>
    3435
     
    8889}
    8990
     91#if !PLATFORM(MAC)
     92Vector<char> CryptoKey::randomData(size_t size)
     93{
     94    Vector<char> result(size);
     95    cryptographicallyRandomValues(result.data(), result.size());
     96    return result;
     97}
     98#endif
    9099} // namespace WebCore
    91100
  • trunk/Source/WebCore/crypto/CryptoKey.h

    r158578 r158582  
    5959    bool allows(CryptoKeyUsage usage) const { return usage == (m_usages & usage); }
    6060
     61    static Vector<char> randomData(size_t);
     62
    6163private:
    6264    CryptoAlgorithmIdentifier m_algorithm;
  • trunk/Source/WebCore/crypto/CryptoKeyAES.cpp

    r158578 r158582  
    5050}
    5151
     52PassRefPtr<CryptoKeyAES> CryptoKeyAES::generate(CryptoAlgorithmIdentifier algorithm, size_t lengthBits, bool extractable, CryptoKeyUsage usages)
     53{
     54    if (lengthBits % 8)
     55        return nullptr;
     56    return adoptRef(new CryptoKeyAES(algorithm, randomData(lengthBits / 8), extractable, usages));
     57}
     58
    5259void CryptoKeyAES::buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder& builder) const
    5360{
  • trunk/Source/WebCore/crypto/CryptoKeyAES.h

    r158578 r158582  
    4343    virtual ~CryptoKeyAES();
    4444
     45    static PassRefPtr<CryptoKeyAES> generate(CryptoAlgorithmIdentifier, size_t lengthBits, bool extractable, CryptoKeyUsage);
     46
    4547    virtual CryptoKeyClass keyClass() const OVERRIDE { return CryptoKeyClass::AES; }
    4648
  • trunk/Source/WebCore/crypto/CryptoKeyMac.cpp

    r158526 r158582  
    2929#if ENABLE(SUBTLE_CRYPTO)
    3030
    31 #ifdef __has_include
     31#if defined(__has_include) && (PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)
    3232#if __has_include(<CommonCrypto/CommonRandomSPI.h>)
    3333#include <CommonCrypto/CommonRandomSPI.h>
  • trunk/Source/WebCore/crypto/SubtleCrypto.idl

    r158578 r158582  
    3636    [Custom] Promise verify(AlgorithmIdentifier algorithm, Key key, CryptoOperationData signature, sequence<CryptoOperationData> data);
    3737    [Custom] Promise digest(AlgorithmIdentifier algorithm, sequence<CryptoOperationData> data);
     38    [Custom] Promise generateKey(AlgorithmIdentifier algorithm, optional boolean extractable, optional KeyUsage[] keyUsages);
    3839    [Custom] Promise importKey(KeyFormat format, CryptoOperationData keyData, AlgorithmIdentifier? algorithm, optional boolean extractable, optional KeyUsage[] keyUsages);
    3940};
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmAES_CBC.cpp

    r158578 r158582  
    2929#if ENABLE(SUBTLE_CRYPTO)
    3030
    31 #include "CryptoAlgorithmAesCbcParams.h"
     31#include "CryptoAlgorithmAesKeyGenParams.h"
    3232#include "CryptoKeyAES.h"
    3333#include "ExceptionCode.h"
     
    5656}
    5757
     58void CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     59{
     60    const CryptoAlgorithmAesKeyGenParams& aesParameters = static_cast<const CryptoAlgorithmAesKeyGenParams&>(parameters);
     61
     62    RefPtr<CryptoKeyAES> result = CryptoKeyAES::generate(CryptoAlgorithmIdentifier::AES_CBC, aesParameters.length, extractable, usages);
     63    if (!result) {
     64        promise->reject(nullptr);
     65        return;
     66    }
     67
     68    promise->fulfill(result.release());
     69}
     70
    5871void CryptoAlgorithmAES_CBC::importKey(const CryptoAlgorithmParameters&, CryptoKeyFormat format, const CryptoOperationData& data, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
    5972{
  • trunk/Source/WebCore/crypto/algorithms/CryptoAlgorithmHMAC.cpp

    r158578 r158582  
    2929#if ENABLE(SUBTLE_CRYPTO)
    3030
     31#include "CryptoAlgorithmHmacKeyParams.h"
    3132#include "CryptoAlgorithmHmacParams.h"
    3233#include "CryptoKeyHMAC.h"
     
    5657}
    5758
     59void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, std::unique_ptr<PromiseWrapper> promise, ExceptionCode&)
     60{
     61    const CryptoAlgorithmHmacKeyParams& hmacParameters = static_cast<const CryptoAlgorithmHmacKeyParams&>(parameters);
     62
     63    RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::generate(hmacParameters.hasLength ? hmacParameters.length : 0, hmacParameters.hash, extractable, usages);
     64    if (!result) {
     65        promise->reject(nullptr);
     66        return;
     67    }
     68
     69    promise->fulfill(result.release());
     70}
     71
    5872void CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParameters& parameters, CryptoKeyFormat format, const CryptoOperationData& data, bool extractable, CryptoKeyUsage usage, std::unique_ptr<PromiseWrapper> promise, ExceptionCode& ec)
    5973{
  • trunk/Source/WebCore/crypto/keys/CryptoKeyHMAC.cpp

    r158578 r158582  
    4646}
    4747
     48PassRefPtr<CryptoKeyHMAC> CryptoKeyHMAC::generate(size_t lengthBytes, CryptoAlgorithmIdentifier hash, bool extractable, CryptoKeyUsage usages)
     49{
     50    if (!lengthBytes) {
     51        switch (hash) {
     52        case CryptoAlgorithmIdentifier::SHA_1:
     53        case CryptoAlgorithmIdentifier::SHA_224:
     54        case CryptoAlgorithmIdentifier::SHA_256:
     55            lengthBytes = 64;
     56            break;
     57        case CryptoAlgorithmIdentifier::SHA_384:
     58        case CryptoAlgorithmIdentifier::SHA_512:
     59            lengthBytes = 128;
     60            break;
     61        default:
     62            return nullptr;
     63        }
     64    }
     65
     66    return adoptRef(new CryptoKeyHMAC(randomData(lengthBytes), hash, extractable, usages));
     67}
     68
    4869void CryptoKeyHMAC::buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder& builder) const
    4970{
  • trunk/Source/WebCore/crypto/keys/CryptoKeyHMAC.h

    r158578 r158582  
    4242    virtual ~CryptoKeyHMAC();
    4343
     44    // If lengthBytes is 0, a recommended length is used, which is the size of the associated hash function's block size.
     45    static PassRefPtr<CryptoKeyHMAC> generate(size_t lengthBytes, CryptoAlgorithmIdentifier hash, bool extractable, CryptoKeyUsage);
     46
    4447    virtual CryptoKeyClass keyClass() const OVERRIDE { return CryptoKeyClass::HMAC; }
    4548
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmAES_CBCMac.cpp

    r158578 r158582  
    116116}
    117117
    118 void CryptoAlgorithmAES_CBC::generateKey(const CryptoAlgorithmParameters&, bool /*extractable*/, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
    119 {
    120     // Not yet implemented.
    121     ec = NOT_SUPPORTED_ERR;
    122 }
    123 
    124118} // namespace WebCore
    125119
  • trunk/Source/WebCore/crypto/mac/CryptoAlgorithmHMACMac.cpp

    r158578 r158582  
    137137}
    138138
    139 void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters&, bool /*extractable*/, CryptoKeyUsage, std::unique_ptr<PromiseWrapper>, ExceptionCode& ec)
    140 {
    141     // Not yet implemented.
    142     ec = NOT_SUPPORTED_ERR;
    143 }
    144 
    145 
    146139}
    147140
Note: See TracChangeset for help on using the changeset viewer.