Move CryptoDigest to WebCore/platform
authordbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Mar 2016 19:26:23 +0000 (19:26 +0000)
committerdbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Mar 2016 19:26:23 +0000 (19:26 +0000)
https://bugs.webkit.org/show_bug.cgi?id=155008
<rdar://problem/24969787>

Reviewed by Brent Fulgham.

CryptoDigest provides a platform-independent interface for interacting with platform-
specific cryptographic hashing services. We currently make use of this code as part
of the implementation of Web Crypto. This code will also be beneficial as part of
implementing support for Content Security Policy inline script and inline stylesheet
hashes. We should move CryptoDigest to WebCore/platform to convey that it a general
purpose platform abstraction.

* CMakeLists.txt: Add include directory WebCore/platform/crypto.
* PlatformEfl.cmake: Add file platform/crypto/gnutls/CryptoDigestGnuTLS.cpp and
remove file crypto/gnutls/CryptoDigestGnuTLS.cpp.
* PlatformGTK.cmake: Ditto.
* PlatformMac.cmake: Add file platform/crypto/mac/CryptoDigestMac.cpp and
remove file crypto/mac/CryptoDigestMac.cpp.
* WebCore.xcodeproj/project.pbxproj:
* crypto/algorithms/CryptoAlgorithmSHA1.cpp:
(WebCore::CryptoAlgorithmSHA1::digest): Substitute "CryptoDigest::Algorithm" for "CryptoAlgorithmIdentifier".
* crypto/algorithms/CryptoAlgorithmSHA224.cpp:
(WebCore::CryptoAlgorithmSHA224::digest): Ditto.
* crypto/algorithms/CryptoAlgorithmSHA256.cpp:
(WebCore::CryptoAlgorithmSHA256::digest): Ditto.
* crypto/algorithms/CryptoAlgorithmSHA384.cpp:
(WebCore::CryptoAlgorithmSHA384::digest): Ditto.
* crypto/algorithms/CryptoAlgorithmSHA512.cpp:
(WebCore::CryptoAlgorithmSHA512::digest): Ditto.
* crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp:
(WebCore::getCryptoDigestAlgorithm): Converts a CryptoAlgorithmIdentifier enumerator to a
CryptoDigest::Algorithm enumerator, if applicable.
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign): Write in terms of WebCore::getCryptoDigestAlgorithm().
(WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify): Ditto.
* platform/crypto/CryptoDigest.h: Renamed from Source/WebCore/crypto/CryptoDigest.h. Also added enum CryptoDigest::Algorithm
and changed constructor to take this enum.
* platform/crypto/gnutls/CryptoDigestGnuTLS.cpp: Renamed from Source/WebCore/crypto/gnutls/CryptoDigestGnuTLS.cpp.
(WebCore::CryptoDigest::CryptoDigest): Substitute "CryptoDigest::Algorithm" for "CryptoAlgorithmIdentifier".
(WebCore::CryptoDigest::~CryptoDigest): Ditto.
(WebCore::CryptoDigest::create): Ditto.
(WebCore::CryptoDigest::addBytes): Ditto.
(WebCore::CryptoDigest::computeHash): Ditto.
* platform/crypto/mac/CryptoDigestMac.cpp: Renamed from Source/WebCore/crypto/mac/CryptoDigestMac.cpp.
(WebCore::toSHA1Context): Ditto.
(WebCore::toSHA224Context): Ditto.
(WebCore::toSHA256Context): Ditto.
(WebCore::toSHA384Context): Ditto.
(WebCore::toSHA512Context): Ditto.
(WebCore::CryptoDigest::CryptoDigest): Ditto.
(WebCore::CryptoDigest::~CryptoDigest): Ditto.
(WebCore::CryptoDigest::create): Ditto.
(WebCore::CryptoDigest::addBytes): Ditto.
(WebCore::CryptoDigest::computeHash): Ditto.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@197575 268f45cc-cd09-0410-ab3c-d52691b4dbfc

15 files changed:
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/PlatformEfl.cmake
Source/WebCore/PlatformGTK.cmake
Source/WebCore/PlatformMac.cmake
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA1.cpp
Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA224.cpp
Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA256.cpp
Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA384.cpp
Source/WebCore/crypto/algorithms/CryptoAlgorithmSHA512.cpp
Source/WebCore/crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp
Source/WebCore/platform/crypto/CryptoDigest.h [moved from Source/WebCore/crypto/CryptoDigest.h with 88% similarity]
Source/WebCore/platform/crypto/gnutls/CryptoDigestGnuTLS.cpp [moved from Source/WebCore/crypto/gnutls/CryptoDigestGnuTLS.cpp with 87% similarity]
Source/WebCore/platform/crypto/mac/CryptoDigestMac.cpp [moved from Source/WebCore/crypto/mac/CryptoDigestMac.cpp with 75% similarity]

index c029dd7..75dd1df 100644 (file)
@@ -69,6 +69,7 @@ set(WebCore_INCLUDE_DIRECTORIES
     "${WEBCORE_DIR}/platform"
     "${WEBCORE_DIR}/platform/animation"
     "${WEBCORE_DIR}/platform/audio"
+    "${WEBCORE_DIR}/platform/crypto"
     "${WEBCORE_DIR}/platform/graphics"
     "${WEBCORE_DIR}/platform/graphics/cpu/arm"
     "${WEBCORE_DIR}/platform/graphics/cpu/arm/filters"
index c0a7e63..47ed05d 100644 (file)
@@ -1,3 +1,60 @@
+2016-03-04  Daniel Bates  <dabates@apple.com>
+
+        Move CryptoDigest to WebCore/platform
+        https://bugs.webkit.org/show_bug.cgi?id=155008
+        <rdar://problem/24969787>
+
+        Reviewed by Brent Fulgham.
+
+        CryptoDigest provides a platform-independent interface for interacting with platform-
+        specific cryptographic hashing services. We currently make use of this code as part
+        of the implementation of Web Crypto. This code will also be beneficial as part of
+        implementing support for Content Security Policy inline script and inline stylesheet
+        hashes. We should move CryptoDigest to WebCore/platform to convey that it a general
+        purpose platform abstraction.
+
+        * CMakeLists.txt: Add include directory WebCore/platform/crypto.
+        * PlatformEfl.cmake: Add file platform/crypto/gnutls/CryptoDigestGnuTLS.cpp and
+        remove file crypto/gnutls/CryptoDigestGnuTLS.cpp.
+        * PlatformGTK.cmake: Ditto.
+        * PlatformMac.cmake: Add file platform/crypto/mac/CryptoDigestMac.cpp and
+        remove file crypto/mac/CryptoDigestMac.cpp.
+        * WebCore.xcodeproj/project.pbxproj:
+        * crypto/algorithms/CryptoAlgorithmSHA1.cpp:
+        (WebCore::CryptoAlgorithmSHA1::digest): Substitute "CryptoDigest::Algorithm" for "CryptoAlgorithmIdentifier".
+        * crypto/algorithms/CryptoAlgorithmSHA224.cpp:
+        (WebCore::CryptoAlgorithmSHA224::digest): Ditto.
+        * crypto/algorithms/CryptoAlgorithmSHA256.cpp:
+        (WebCore::CryptoAlgorithmSHA256::digest): Ditto.
+        * crypto/algorithms/CryptoAlgorithmSHA384.cpp:
+        (WebCore::CryptoAlgorithmSHA384::digest): Ditto.
+        * crypto/algorithms/CryptoAlgorithmSHA512.cpp:
+        (WebCore::CryptoAlgorithmSHA512::digest): Ditto.
+        * crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp:
+        (WebCore::getCryptoDigestAlgorithm): Converts a CryptoAlgorithmIdentifier enumerator to a
+        CryptoDigest::Algorithm enumerator, if applicable.
+        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign): Write in terms of WebCore::getCryptoDigestAlgorithm().
+        (WebCore::CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify): Ditto.
+        * platform/crypto/CryptoDigest.h: Renamed from Source/WebCore/crypto/CryptoDigest.h. Also added enum CryptoDigest::Algorithm
+        and changed constructor to take this enum.
+        * platform/crypto/gnutls/CryptoDigestGnuTLS.cpp: Renamed from Source/WebCore/crypto/gnutls/CryptoDigestGnuTLS.cpp.
+        (WebCore::CryptoDigest::CryptoDigest): Substitute "CryptoDigest::Algorithm" for "CryptoAlgorithmIdentifier".
+        (WebCore::CryptoDigest::~CryptoDigest): Ditto.
+        (WebCore::CryptoDigest::create): Ditto.
+        (WebCore::CryptoDigest::addBytes): Ditto.
+        (WebCore::CryptoDigest::computeHash): Ditto.
+        * platform/crypto/mac/CryptoDigestMac.cpp: Renamed from Source/WebCore/crypto/mac/CryptoDigestMac.cpp.
+        (WebCore::toSHA1Context): Ditto.
+        (WebCore::toSHA224Context): Ditto.
+        (WebCore::toSHA256Context): Ditto.
+        (WebCore::toSHA384Context): Ditto.
+        (WebCore::toSHA512Context): Ditto.
+        (WebCore::CryptoDigest::CryptoDigest): Ditto.
+        (WebCore::CryptoDigest::~CryptoDigest): Ditto.
+        (WebCore::CryptoDigest::create): Ditto.
+        (WebCore::CryptoDigest::addBytes): Ditto.
+        (WebCore::CryptoDigest::computeHash): Ditto.
+
 2016-03-04  Myles C. Maxfield  <mmaxfield@apple.com>
 
         Whitespace causes font-variant: all-small-caps to synthesize
index e8323c7..69d1c5c 100644 (file)
@@ -88,6 +88,8 @@ list(APPEND WebCore_SOURCES
 
     platform/audio/efl/AudioBusEfl.cpp
 
+    platform/crypto/gnutls/CryptoDigestGnuTLS.cpp
+
     platform/efl/BatteryProviderEfl.cpp
     platform/efl/CursorEfl.cpp
     platform/efl/DragDataEfl.cpp
@@ -432,7 +434,6 @@ if (ENABLE_SUBTLE_CRYPTO)
         crypto/gnutls/CryptoAlgorithmRSASSA_PKCS1_v1_5GnuTLS.cpp
         crypto/gnutls/CryptoAlgorithmRSA_OAEPGnuTLS.cpp
         crypto/gnutls/CryptoAlgorithmRegistryGnuTLS.cpp
-        crypto/gnutls/CryptoDigestGnuTLS.cpp
         crypto/gnutls/CryptoKeyRSAGnuTLS.cpp
         crypto/gnutls/SerializedCryptoKeyWrapGnuTLS.cpp
 
index 03addc6..7726063 100644 (file)
@@ -82,6 +82,8 @@ list(APPEND WebCore_SOURCES
 
     platform/audio/glib/AudioBusGLib.cpp
 
+    platform/crypto/gnutls/CryptoDigestGnuTLS.cpp
+
     platform/geoclue/GeolocationProviderGeoclue1.cpp
     platform/geoclue/GeolocationProviderGeoclue2.cpp
 
@@ -807,7 +809,6 @@ if (ENABLE_SUBTLE_CRYPTO)
         crypto/gnutls/CryptoAlgorithmRSASSA_PKCS1_v1_5GnuTLS.cpp
         crypto/gnutls/CryptoAlgorithmRSA_OAEPGnuTLS.cpp
         crypto/gnutls/CryptoAlgorithmRegistryGnuTLS.cpp
-        crypto/gnutls/CryptoDigestGnuTLS.cpp
         crypto/gnutls/CryptoKeyRSAGnuTLS.cpp
         crypto/gnutls/SerializedCryptoKeyWrapGnuTLS.cpp
 
index e4ab0c7..f82cf09 100644 (file)
@@ -218,7 +218,6 @@ list(APPEND WebCore_SOURCES
     crypto/mac/CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp
     crypto/mac/CryptoAlgorithmRSA_OAEPMac.cpp
     crypto/mac/CryptoAlgorithmRegistryMac.cpp
-    crypto/mac/CryptoDigestMac.cpp
     crypto/mac/CryptoKeyMac.cpp
     crypto/mac/CryptoKeyRSAMac.cpp
     crypto/mac/SerializedCryptoKeyWrapMac.mm
@@ -340,6 +339,8 @@ list(APPEND WebCore_SOURCES
     platform/cocoa/VNodeTrackerCocoa.cpp
     platform/cocoa/WebCoreNSErrorExtras.mm
 
+    platform/crypto/mac/CryptoDigestMac.cpp
+
     platform/graphics/DisplayRefreshMonitor.cpp
     platform/graphics/DisplayRefreshMonitorManager.cpp
     platform/graphics/FontPlatformData.cpp
index ef38df0..d43efeb 100644 (file)
                E1F80B89183172A2007885C3 /* JSCryptoKeyPairCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSCryptoKeyPairCustom.cpp; sourceTree = "<group>"; };
                E1F80B8B183172B5007885C3 /* JSCryptoKeyPair.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSCryptoKeyPair.cpp; sourceTree = "<group>"; };
                E1F80B8C183172B5007885C3 /* JSCryptoKeyPair.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSCryptoKeyPair.h; sourceTree = "<group>"; };
-               E1FE13621834351100892F13 /* CryptoDigestMac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoDigestMac.cpp; sourceTree = "<group>"; };
-               E1FE136618343A1000892F13 /* CryptoDigest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoDigest.h; sourceTree = "<group>"; };
+               E1FE13621834351100892F13 /* CryptoDigestMac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CryptoDigestMac.cpp; path = crypto/mac/CryptoDigestMac.cpp; sourceTree = "<group>"; };
+               E1FE136618343A1000892F13 /* CryptoDigest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CryptoDigest.h; path = crypto/CryptoDigest.h; sourceTree = "<group>"; };
                E1FE1368183FE1AB00892F13 /* CryptoAlgorithmRSA_OAEP.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoAlgorithmRSA_OAEP.cpp; sourceTree = "<group>"; };
                E1FE1369183FE1AB00892F13 /* CryptoAlgorithmRSA_OAEP.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoAlgorithmRSA_OAEP.h; sourceTree = "<group>"; };
                E1FE136E183FECF000892F13 /* CryptoAlgorithmRSA_OAEPMac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptoAlgorithmRSA_OAEPMac.cpp; sourceTree = "<group>"; };
                                FD31604012B026A300C1A359 /* audio */,
                                1AE42F670AA4B8CB00C8612D /* cf */,
                                A5C974CE11485FDA0066F2AB /* cocoa */,
+                               CE50D8CE1C8932ED0072EA5A /* crypto */,
                                B2A015910AF6CD53006BCE0E /* graphics */,
                                A59E3C1B11580F340072928E /* ios */,
                                6582A14809999D6C00BEEB6D /* mac */,
                        path = ios;
                        sourceTree = "<group>";
                };
+               CE50D8CE1C8932ED0072EA5A /* crypto */ = {
+                       isa = PBXGroup;
+                       children = (
+                               CE50D8CF1C8932FB0072EA5A /* mac */,
+                               E1FE136618343A1000892F13 /* CryptoDigest.h */,
+                       );
+                       name = crypto;
+                       sourceTree = "<group>";
+               };
+               CE50D8CF1C8932FB0072EA5A /* mac */ = {
+                       isa = PBXGroup;
+                       children = (
+                               E1FE13621834351100892F13 /* CryptoDigestMac.cpp */,
+                       );
+                       name = mac;
+                       sourceTree = "<group>";
+               };
                DF9AFD6F13FC31B00015FEB7 /* objc */ = {
                        isa = PBXGroup;
                        children = (
                                E1FE136E183FECF000892F13 /* CryptoAlgorithmRSA_OAEPMac.cpp */,
                                E1233F0E185A4130008DFAF5 /* CryptoAlgorithmRSAES_PKCS1_v1_5Mac.cpp */,
                                E1C266D618317AB4003F8B33 /* CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp */,
-                               E1FE13621834351100892F13 /* CryptoDigestMac.cpp */,
                                E19AC3F8182566F700349426 /* CryptoKeyMac.cpp */,
                                E164FAA418315E1A00DB4E61 /* CryptoKeyRSAMac.cpp */,
                                E18DF33618AAF14D00773E59 /* SerializedCryptoKeyWrapMac.mm */,
                                E1C657261816F9FE00256CDD /* CryptoAlgorithmParameters.h */,
                                E1FF8F6A180DB5BE00132674 /* CryptoAlgorithmRegistry.cpp */,
                                E1FF8F6B180DB5BE00132674 /* CryptoAlgorithmRegistry.h */,
-                               E1FE136618343A1000892F13 /* CryptoDigest.h */,
                                E157A8E218173A3A009F821D /* CryptoKey.cpp */,
                                E157A8E318173A3A009F821D /* CryptoKey.h */,
                                E157A8DC18172C2C009F821D /* CryptoKey.idl */,
index bc94aa3..54dcd03 100644 (file)
@@ -54,7 +54,7 @@ CryptoAlgorithmIdentifier CryptoAlgorithmSHA1::identifier() const
 
 void CryptoAlgorithmSHA1::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&)
 {
-    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_1);
+    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoDigest::Algorithm::SHA_1);
     if (!digest) {
         failureCallback();
         return;
index 62cc5a8..3878227 100644 (file)
@@ -54,7 +54,7 @@ CryptoAlgorithmIdentifier CryptoAlgorithmSHA224::identifier() const
 
 void CryptoAlgorithmSHA224::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&)
 {
-    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_224);
+    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoDigest::Algorithm::SHA_224);
     if (!digest) {
         failureCallback();
         return;
index 27fe8aa..0acaf09 100644 (file)
@@ -54,7 +54,7 @@ CryptoAlgorithmIdentifier CryptoAlgorithmSHA256::identifier() const
 
 void CryptoAlgorithmSHA256::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&)
 {
-    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_256);
+    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoDigest::Algorithm::SHA_256);
     if (!digest) {
         failureCallback();
         return;
index 6eba234..b1bfbe1 100644 (file)
@@ -54,7 +54,7 @@ CryptoAlgorithmIdentifier CryptoAlgorithmSHA384::identifier() const
 
 void CryptoAlgorithmSHA384::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&)
 {
-    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_384);
+    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoDigest::Algorithm::SHA_384);
     if (!digest) {
         failureCallback();
         return;
index 87d62d4..7ddf1d4 100644 (file)
@@ -54,7 +54,7 @@ CryptoAlgorithmIdentifier CryptoAlgorithmSHA512::identifier() const
 
 void CryptoAlgorithmSHA512::digest(const CryptoAlgorithmParameters&, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&)
 {
-    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoAlgorithmIdentifier::SHA_512);
+    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(CryptoDigest::Algorithm::SHA_512);
     if (!digest) {
         failureCallback();
         return;
index 0cde1b4..39ac402 100644 (file)
 
 namespace WebCore {
 
+inline bool getCryptoDigestAlgorithm(CryptoAlgorithmIdentifier hashFunction, CryptoDigest::Algorithm& algorithm)
+{
+    switch (hashFunction) {
+    case CryptoAlgorithmIdentifier::SHA_1:
+        algorithm = CryptoDigest::Algorithm::SHA_1;
+        return true;
+    case CryptoAlgorithmIdentifier::SHA_224:
+        algorithm = CryptoDigest::Algorithm::SHA_224;
+        return true;
+    case CryptoAlgorithmIdentifier::SHA_256:
+        algorithm = CryptoDigest::Algorithm::SHA_256;
+        return true;
+    case CryptoAlgorithmIdentifier::SHA_384:
+        algorithm = CryptoDigest::Algorithm::SHA_384;
+        return true;
+    case CryptoAlgorithmIdentifier::SHA_512:
+        algorithm = CryptoDigest::Algorithm::SHA_512;
+        return true;
+    default:
+        return false;
+    }
+}
+
 void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaParams& parameters, const CryptoKeyRSA& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode& ec)
 {
     CCDigestAlgorithm digestAlgorithm;
@@ -44,7 +67,13 @@ void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoAlgorithmRsaSsaP
         return;
     }
 
-    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(parameters.hash);
+    CryptoDigest::Algorithm cryptoDigestAlgorithm;
+    if (!getCryptoDigestAlgorithm(parameters.hash, cryptoDigestAlgorithm)) {
+        ec = NOT_SUPPORTED_ERR;
+        return;
+    }
+
+    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(cryptoDigestAlgorithm);
     if (!digest) {
         ec = NOT_SUPPORTED_ERR;
         return;
@@ -75,7 +104,13 @@ void CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(const CryptoAlgorithmRsaSs
         return;
     }
 
-    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(parameters.hash);
+    CryptoDigest::Algorithm cryptoDigestAlgorithm;
+    if (!getCryptoDigestAlgorithm(parameters.hash, cryptoDigestAlgorithm)) {
+        ec = NOT_SUPPORTED_ERR;
+        return;
+    }
+
+    std::unique_ptr<CryptoDigest> digest = CryptoDigest::create(cryptoDigestAlgorithm);
     if (!digest) {
         ec = NOT_SUPPORTED_ERR;
         return;
similarity index 88%
rename from Source/WebCore/crypto/CryptoDigest.h
rename to Source/WebCore/platform/crypto/CryptoDigest.h
index 353b13e..31f075a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013, 2016 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #ifndef CryptoDigest_h
 #define CryptoDigest_h
 
-#include "CryptoAlgorithmIdentifier.h"
 #include <wtf/Noncopyable.h>
 #include <wtf/Vector.h>
 
-#if ENABLE(SUBTLE_CRYPTO)
-
 namespace WebCore {
 
 struct CryptoDigestContext;
@@ -39,7 +36,14 @@ struct CryptoDigestContext;
 class CryptoDigest {
     WTF_MAKE_NONCOPYABLE(CryptoDigest);
 public:
-    static std::unique_ptr<CryptoDigest> create(CryptoAlgorithmIdentifier);
+    enum class Algorithm {
+        SHA_1,
+        SHA_224,
+        SHA_256,
+        SHA_384,
+        SHA_512,
+    };
+    static std::unique_ptr<CryptoDigest> create(Algorithm);
     ~CryptoDigest();
 
     void addBytes(const void* input, size_t length);
@@ -53,5 +57,4 @@ private:
 
 } // namespace WebCore
 
-#endif // ENABLE(SUBTLE_CRYPTO)
 #endif // CryptoDigest_h
@@ -26,8 +26,6 @@
 #include "config.h"
 #include "CryptoDigest.h"
 
-#if ENABLE(SUBTLE_CRYPTO)
-
 #include <gnutls/gnutls.h>
 #include <gnutls/crypto.h>
 
@@ -48,33 +46,31 @@ CryptoDigest::~CryptoDigest()
     gnutls_hash_deinit(m_context->hash, 0);
 }
 
-std::unique_ptr<CryptoDigest> CryptoDigest::create(CryptoAlgorithmIdentifier algorithm)
+std::unique_ptr<CryptoDigest> CryptoDigest::create(CryptoDigest::Algorithm algorithm)
 {
     gnutls_digest_algorithm_t gnutlsAlgorithm;
 
     switch (algorithm) {
-    case CryptoAlgorithmIdentifier::SHA_1: {
+    case CryptoDigest::Algorithm::SHA_1: {
         gnutlsAlgorithm = GNUTLS_DIG_SHA1;
         break;
     }
-    case CryptoAlgorithmIdentifier::SHA_224: {
+    case CryptoDigest::Algorithm::SHA_224: {
         gnutlsAlgorithm = GNUTLS_DIG_SHA224;
         break;
     }
-    case CryptoAlgorithmIdentifier::SHA_256: {
+    case CryptoDigest::Algorithm::SHA_256: {
         gnutlsAlgorithm = GNUTLS_DIG_SHA256;
         break;
     }
-    case CryptoAlgorithmIdentifier::SHA_384: {
+    case CryptoDigest::Algorithm::SHA_384: {
         gnutlsAlgorithm = GNUTLS_DIG_SHA384;
         break;
     }
-    case CryptoAlgorithmIdentifier::SHA_512: {
+    case CryptoDigest::Algorithm::SHA_512: {
         gnutlsAlgorithm = GNUTLS_DIG_SHA512;
         break;
     }
-    default:
-        return nullptr;
     }
 
     std::unique_ptr<CryptoDigest> digest(new CryptoDigest);
@@ -104,5 +100,3 @@ Vector<uint8_t> CryptoDigest::computeHash()
 }
 
 } // namespace WebCore
-
-#endif // ENABLE(SUBTLE_CRYPTO)
 #include "config.h"
 #include "CryptoDigest.h"
 
-#if ENABLE(SUBTLE_CRYPTO)
-
 #include <CommonCrypto/CommonCrypto.h>
 
 namespace WebCore {
 
 struct CryptoDigestContext {
-    CryptoAlgorithmIdentifier algorithm;
+    CryptoDigest::Algorithm algorithm;
     void* ccContext;
 };
 
 inline CC_SHA1_CTX* toSHA1Context(CryptoDigestContext* context)
 {
-    ASSERT(context->algorithm == CryptoAlgorithmIdentifier::SHA_1);
+    ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_1);
     return static_cast<CC_SHA1_CTX*>(context->ccContext);
 }
 inline CC_SHA256_CTX* toSHA224Context(CryptoDigestContext* context)
 {
-    ASSERT(context->algorithm == CryptoAlgorithmIdentifier::SHA_224);
+    ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_224);
     return static_cast<CC_SHA256_CTX*>(context->ccContext);
 }
 inline CC_SHA256_CTX* toSHA256Context(CryptoDigestContext* context)
 {
-    ASSERT(context->algorithm == CryptoAlgorithmIdentifier::SHA_256);
+    ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_256);
     return static_cast<CC_SHA256_CTX*>(context->ccContext);
 }
 inline CC_SHA512_CTX* toSHA384Context(CryptoDigestContext* context)
 {
-    ASSERT(context->algorithm == CryptoAlgorithmIdentifier::SHA_384);
+    ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_384);
     return static_cast<CC_SHA512_CTX*>(context->ccContext);
 }
 inline CC_SHA512_CTX* toSHA512Context(CryptoDigestContext* context)
 {
-    ASSERT(context->algorithm == CryptoAlgorithmIdentifier::SHA_512);
+    ASSERT(context->algorithm == CryptoDigest::Algorithm::SHA_512);
     return static_cast<CC_SHA512_CTX*>(context->ccContext);
 }
 
@@ -71,88 +69,82 @@ CryptoDigest::CryptoDigest()
 CryptoDigest::~CryptoDigest()
 {
     switch (m_context->algorithm) {
-    case CryptoAlgorithmIdentifier::SHA_1:
+    case CryptoDigest::Algorithm::SHA_1:
         delete toSHA1Context(m_context.get());
         return;
-    case CryptoAlgorithmIdentifier::SHA_224:
+    case CryptoDigest::Algorithm::SHA_224:
         delete toSHA224Context(m_context.get());
         return;
-    case CryptoAlgorithmIdentifier::SHA_256:
+    case CryptoDigest::Algorithm::SHA_256:
         delete toSHA256Context(m_context.get());
         return;
-    case CryptoAlgorithmIdentifier::SHA_384:
+    case CryptoDigest::Algorithm::SHA_384:
         delete toSHA384Context(m_context.get());
         return;
-    case CryptoAlgorithmIdentifier::SHA_512:
+    case CryptoDigest::Algorithm::SHA_512:
         delete toSHA512Context(m_context.get());
         return;
-    default:
-        ASSERT_NOT_REACHED();
     }
 }
 
 
-std::unique_ptr<CryptoDigest> CryptoDigest::create(CryptoAlgorithmIdentifier algorithm)
+std::unique_ptr<CryptoDigest> CryptoDigest::create(CryptoDigest::Algorithm algorithm)
 {
     std::unique_ptr<CryptoDigest> digest(new CryptoDigest);
     digest->m_context->algorithm = algorithm;
 
     switch (algorithm) {
-    case CryptoAlgorithmIdentifier::SHA_1: {
+    case CryptoDigest::Algorithm::SHA_1: {
         CC_SHA1_CTX* context = new CC_SHA1_CTX;
         digest->m_context->ccContext = context;
         CC_SHA1_Init(context);
         return digest;
     }
-    case CryptoAlgorithmIdentifier::SHA_224: {
+    case CryptoDigest::Algorithm::SHA_224: {
         CC_SHA256_CTX* context = new CC_SHA256_CTX;
         digest->m_context->ccContext = context;
         CC_SHA224_Init(context);
         return digest;
     }
-    case CryptoAlgorithmIdentifier::SHA_256: {
+    case CryptoDigest::Algorithm::SHA_256: {
         CC_SHA256_CTX* context = new CC_SHA256_CTX;
         digest->m_context->ccContext = context;
         CC_SHA256_Init(context);
         return digest;
     }
-    case CryptoAlgorithmIdentifier::SHA_384: {
+    case CryptoDigest::Algorithm::SHA_384: {
         CC_SHA512_CTX* context = new CC_SHA512_CTX;
         digest->m_context->ccContext = context;
         CC_SHA384_Init(context);
         return digest;
     }
-    case CryptoAlgorithmIdentifier::SHA_512: {
+    case CryptoDigest::Algorithm::SHA_512: {
         CC_SHA512_CTX* context = new CC_SHA512_CTX;
         digest->m_context->ccContext = context;
         CC_SHA512_Init(context);
         return digest;
     }
-    default:
-        return nullptr;
     }
 }
 
 void CryptoDigest::addBytes(const void* input, size_t length)
 {
     switch (m_context->algorithm) {
-    case CryptoAlgorithmIdentifier::SHA_1:
+    case CryptoDigest::Algorithm::SHA_1:
         CC_SHA1_Update(toSHA1Context(m_context.get()), input, length);
         return;
-    case CryptoAlgorithmIdentifier::SHA_224:
+    case CryptoDigest::Algorithm::SHA_224:
         CC_SHA224_Update(toSHA224Context(m_context.get()), input, length);
         return;
-    case CryptoAlgorithmIdentifier::SHA_256:
+    case CryptoDigest::Algorithm::SHA_256:
         CC_SHA256_Update(toSHA256Context(m_context.get()), input, length);
         return;
-    case CryptoAlgorithmIdentifier::SHA_384:
+    case CryptoDigest::Algorithm::SHA_384:
         CC_SHA384_Update(toSHA384Context(m_context.get()), input, length);
         return;
-    case CryptoAlgorithmIdentifier::SHA_512:
+    case CryptoDigest::Algorithm::SHA_512:
         CC_SHA512_Update(toSHA512Context(m_context.get()), input, length);
         return;
-    default:
-        ASSERT_NOT_REACHED();
     }
 }
 
@@ -160,32 +152,28 @@ Vector<uint8_t> CryptoDigest::computeHash()
 {
     Vector<uint8_t> result;
     switch (m_context->algorithm) {
-    case CryptoAlgorithmIdentifier::SHA_1:
+    case CryptoDigest::Algorithm::SHA_1:
         result.resize(CC_SHA1_DIGEST_LENGTH);
         CC_SHA1_Final(result.data(), toSHA1Context(m_context.get()));
         break;
-    case CryptoAlgorithmIdentifier::SHA_224:
+    case CryptoDigest::Algorithm::SHA_224:
         result.resize(CC_SHA224_DIGEST_LENGTH);
         CC_SHA224_Final(result.data(), toSHA224Context(m_context.get()));
         break;
-    case CryptoAlgorithmIdentifier::SHA_256:
+    case CryptoDigest::Algorithm::SHA_256:
         result.resize(CC_SHA256_DIGEST_LENGTH);
         CC_SHA256_Final(result.data(), toSHA256Context(m_context.get()));
         break;
-    case CryptoAlgorithmIdentifier::SHA_384:
+    case CryptoDigest::Algorithm::SHA_384:
         result.resize(CC_SHA384_DIGEST_LENGTH);
         CC_SHA384_Final(result.data(), toSHA384Context(m_context.get()));
         break;
-    case CryptoAlgorithmIdentifier::SHA_512:
+    case CryptoDigest::Algorithm::SHA_512:
         result.resize(CC_SHA512_DIGEST_LENGTH);
         CC_SHA512_Final(result.data(), toSHA512Context(m_context.get()));
         break;
-    default:
-        ASSERT_NOT_REACHED();
     }
     return result;
 }
 
 } // namespace WebCore
-
-#endif // ENABLE(SUBTLE_CRYPTO)