Skip to content

Commit 7792511

Browse files
brentfulghamBrent Fulgham
authored andcommitted
Complete transition to std::span in CryptoDigest-related code
https://bugs.webkit.org/show_bug.cgi?id=271488 <rdar://problem/125256518> Reviewed by Chris Dumez and Sihui Liu. Following the changes in Bug 249414 and Bug 271383, complete the work of moving to std::span in CryptoDigest-related code. * Source/JavaScriptCore/API/JSScript.mm: (-[JSScript readCache]): (-[JSScript writeCache:]): * Source/JavaScriptCore/bytecode/CodeBlockHash.cpp: (JSC::CodeBlockHash::CodeBlockHash): * Source/JavaScriptCore/inspector/remote/glib/RemoteInspectorUtils.cpp: (Inspector::backendCommandsHash): * Source/JavaScriptCore/runtime/CachedBytecode.h: (JSC::CachedBytecode::span const): * Source/WTF/wtf/SHA1.h: * Source/WTF/wtf/persistence/PersistentEncoder.cpp: (WTF::Persistence::Encoder::updateChecksumForData): * Source/WTF/wtf/persistence/PersistentEncoder.h: (WTF::Persistence::Encoder::updateChecksumForNumber): * Source/WebCore/Modules/webauthn/WebAuthenticationUtils.cpp: (WebCore::buildClientDataJsonHash): * Source/WebCore/Modules/websockets/WebSocketHandshake.cpp: (WebCore::WebSocketHandshake::getExpectedWebSocketAccept): * Source/WebCore/PAL/pal/crypto/CryptoDigest.h: (PAL::CryptoDigest::addBytes): Deleted. * Source/WebCore/PAL/pal/crypto/commoncrypto/CryptoDigestCommonCrypto.mm: (PAL::CryptoDigest::addBytes): * Source/WebCore/PAL/pal/crypto/gcrypt/CryptoDigestGCrypt.cpp: (PAL::CryptoDigest::addBytes): * Source/WebCore/PAL/pal/crypto/openssl/CryptoDigestOpenSSL.cpp: (PAL::CryptoDigest::addBytes): * Source/WebCore/PAL/pal/crypto/win/CryptoDigestWin.cpp: (PAL::CryptoDigest::addBytes): * Source/WebCore/css/FontFace.cpp: (WebCore::FontFace::create): * Source/WebCore/crypto/gcrypt/CryptoAlgorithmECDSAGCrypt.cpp: (WebCore::gcryptSign): (WebCore::gcryptVerify): * Source/WebCore/crypto/gcrypt/CryptoAlgorithmRSASSA_PKCS1_v1_5GCrypt.cpp: (WebCore::gcryptSign): (WebCore::gcryptVerify): * Source/WebCore/crypto/gcrypt/CryptoAlgorithmRSA_PSSGCrypt.cpp: (WebCore::gcryptSign): (WebCore::gcryptVerify): * Source/WebCore/dom/Node.cpp: (WebCore::hashPointer): * Source/WebCore/inspector/DOMPatchSupport.cpp: (WebCore::DOMPatchSupport::createDigest): * Source/WebCore/loader/ResourceCryptographicDigest.cpp: (WebCore::cryptographicDigestForBytes): * Source/WebCore/loader/ResourceCryptographicDigest.h: * Source/WebCore/loader/cache/TrustedFonts.cpp: (WebCore::hashForFontData): (WebCore::fontBinaryParsingPolicy): * Source/WebCore/loader/cache/TrustedFonts.h: * Source/WebCore/page/csp/ContentSecurityPolicy.cpp: (WebCore::generateHashesForContent): * Source/WebCore/platform/mediastream/RealtimeMediaSourceCenter.cpp: (WebCore::addStringToSHA1): * Source/WebCore/platform/network/soup/SoupNetworkSession.cpp: (WebCore::HostTLSCertificateSet::computeCertificateHash): * Source/WebCore/storage/StorageUtilities.cpp: (WebCore::StorageUtilities::encodeSecurityOriginForFileName): * Source/WebCore/workers/service/server/SWScriptStorage.cpp: (WebCore::SWScriptStorage::sha2Hash const): * Source/WebKit/NetworkProcess/cache/NetworkCacheData.cpp: (WebKit::NetworkCache::computeSHA1): * Source/WebKit/NetworkProcess/cache/NetworkCacheKey.cpp: (WebKit::NetworkCache::hashString): (WebKit::NetworkCache::Key::computeHash const): (WebKit::NetworkCache::Key::partitionToPartitionHash): * Source/WebKit/NetworkProcess/storage/CacheStorageDiskStore.cpp: (WebKit::computeSHA1): * Source/WebKit/NetworkProcess/storage/NetworkStorageManager.cpp: (WebKit::encode): * Source/WebKit/UIProcess/API/Cocoa/_WKWebAuthenticationPanel.mm: (produceClientDataJsonHash): (+[_WKWebAuthenticationPanel importLocalAuthenticatorWithAccessGroup:credential:error:]): * Source/WebKit/UIProcess/WebAuthentication/Cocoa/LocalAuthenticator.mm: (WebKit::LocalAuthenticator::continueMakeCredentialAfterUserVerification): * Source/WebKit/UIProcess/WebAuthentication/Virtual/VirtualAuthenticatorUtils.mm: (WebKit::credentialIdAndCosePubKeyForPrivateKey): * Tools/DumpRenderTree/cg/PixelDumpSupportCG.cpp: (computeSHA1HashStringForBitmapContext): * Tools/TestWebKitAPI/Tests/WTF/BloomFilter.cpp: (TestWebKitAPI::generateRandomDigests): * Tools/TestWebKitAPI/Tests/WebCore/CryptoDigest.cpp: (TestWebKitAPI::expect): * Tools/TestWebKitAPI/cocoa/HTTPServer.mm: (TestWebKitAPI::Connection::webSocketHandshake): * Tools/WebKitTestRunner/cairo/TestInvocationCairo.cpp: (WTR::computeSHA1HashStringForCairoSurface): * Tools/WebKitTestRunner/skia/TestInvocationSkia.cpp: (WTR::computeSHA1HashStringForPixmap): * Tools/WebKitTestRunner/cg/TestInvocationCG.cpp: (WTR::computeSHA1HashStringForContext): Canonical link: https://commits.webkit.org/276673@main
1 parent 20f9756 commit 7792511

43 files changed

Lines changed: 82 additions & 100 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Source/JavaScriptCore/API/JSScript.mm‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,24 @@ - (void)readCache
171171
if (!success)
172172
return;
173173

174-
const uint8_t* fileData = reinterpret_cast<const uint8_t*>(mappedFile.data());
175-
unsigned fileTotalSize = mappedFile.size();
174+
auto fileData = mappedFile.span();
176175

177176
// Ensure we at least have a SHA1::Digest to read.
178-
if (fileTotalSize < sizeof(SHA1::Digest)) {
177+
if (fileData.size() < sizeof(SHA1::Digest)) {
179178
FileSystem::deleteFile(cacheFilename);
180179
return;
181180
}
182181

183-
unsigned fileDataSize = fileTotalSize - sizeof(SHA1::Digest);
182+
unsigned fileDataSize = fileData.size() - sizeof(SHA1::Digest);
184183

185184
SHA1::Digest computedHash;
186185
SHA1 sha1;
187-
sha1.addBytes(fileData, fileDataSize);
186+
sha1.addBytes(fileData.subspan(0, fileDataSize));
188187
sha1.computeHash(computedHash);
189188

190189
SHA1::Digest fileHash;
191-
memcpy(&fileHash, fileData + fileDataSize, sizeof(SHA1::Digest));
190+
auto hashSpan = fileData.subspan(fileDataSize, sizeof(SHA1::Digest));
191+
memcpy(&fileHash, hashSpan.data(), hashSpan.size());
192192

193193
if (computedHash != fileHash) {
194194
FileSystem::deleteFile(cacheFilename);
@@ -346,7 +346,7 @@ - (BOOL)writeCache:(String&)error
346346

347347
SHA1::Digest computedHash;
348348
SHA1 sha1;
349-
sha1.addBytes(m_cachedBytecode->data(), m_cachedBytecode->size());
349+
sha1.addBytes(m_cachedBytecode->span());
350350
sha1.computeHash(computedHash);
351351
FileSystem::writeToFile(tempFD, computedHash.data(), sizeof(computedHash));
352352

‎Source/JavaScriptCore/bytecode/CodeBlockHash.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ CodeBlockHash::CodeBlockHash(const SourceCode& sourceCode, CodeSpecializationKin
6262
unsigned length = str.length();
6363
unsigned step = (length >> 10) + 1;
6464

65-
sha1.addBytes(bitwise_cast<uint8_t*>(&length), sizeof(length));
65+
sha1.addBytes(std::span { bitwise_cast<uint8_t*>(&length), sizeof(length) });
6666
do {
6767
UChar character = str[index];
68-
sha1.addBytes(bitwise_cast<uint8_t*>(&character), sizeof(character));
68+
sha1.addBytes(std::span { bitwise_cast<uint8_t*>(&character), sizeof(character) });
6969
oldIndex = index;
7070
index += step;
7171
} while (index > oldIndex && index < length);

‎Source/JavaScriptCore/inspector/remote/glib/RemoteInspectorUtils.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const CString& backendCommandsHash()
7474
gconstpointer data = g_bytes_get_data(bytes.get(), &dataSize);
7575
ASSERT(dataSize);
7676
SHA1 sha1;
77-
sha1.addBytes(static_cast<const uint8_t*>(data), dataSize);
77+
sha1.addBytes(std::span { static_cast<const uint8_t*>(data), dataSize });
7878
hexDigest = sha1.computeHexDigest();
7979
}
8080
return hexDigest;

‎Source/JavaScriptCore/runtime/CachedBytecode.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class CachedBytecode : public RefCounted<CachedBytecode> {
6464
using ForEachUpdateCallback = Function<void(off_t, const void*, size_t)>;
6565
JS_EXPORT_PRIVATE void commitUpdates(const ForEachUpdateCallback&) const;
6666

67+
std::span<const uint8_t> span() const { return { data(), size() }; }
6768
const uint8_t* data() const { return m_payload.data(); }
6869
size_t size() const { return m_payload.size(); }
6970
bool hasUpdates() const { return !m_updates.isEmpty(); }

‎Source/WTF/wtf/SHA1.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ class SHA1 {
6262
addBytes(input.span());
6363
}
6464

65-
void addBytes(const uint8_t* input, size_t length)
66-
{
67-
addBytes(std::span(input, length));
68-
}
69-
7065
// Size of the SHA1 hash
7166
WTF_EXPORT_PRIVATE static constexpr size_t hashSize = 20;
7267

‎Source/WTF/wtf/persistence/PersistentEncoder.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ uint8_t* Encoder::grow(size_t size)
4444
void Encoder::updateChecksumForData(SHA1& sha1, std::span<const uint8_t> span)
4545
{
4646
auto typeSalt = Salt<uint8_t*>::value;
47-
sha1.addBytes(reinterpret_cast<uint8_t*>(&typeSalt), sizeof(typeSalt));
47+
sha1.addBytes(std::span { reinterpret_cast<uint8_t*>(&typeSalt), sizeof(typeSalt) });
4848
sha1.addBytes(span);
4949
}
5050

‎Source/WTF/wtf/persistence/PersistentEncoder.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ template <typename Type>
106106
void Encoder::updateChecksumForNumber(SHA1& sha1, Type value)
107107
{
108108
auto typeSalt = Salt<Type>::value;
109-
sha1.addBytes(reinterpret_cast<uint8_t*>(&typeSalt), sizeof(typeSalt));
110-
sha1.addBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
109+
sha1.addBytes(std::span { reinterpret_cast<uint8_t*>(&typeSalt), sizeof(typeSalt) });
110+
sha1.addBytes(std::span { reinterpret_cast<uint8_t*>(&value), sizeof(value) });
111111
}
112112

113113
}

‎Source/WebCore/Modules/webauthn/WebAuthenticationUtils.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Ref<ArrayBuffer> buildClientDataJson(ClientDataType type, const BufferSource& ch
186186
Vector<uint8_t> buildClientDataJsonHash(const ArrayBuffer& clientDataJson)
187187
{
188188
auto crypto = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256);
189-
crypto->addBytes(clientDataJson.data(), clientDataJson.byteLength());
189+
crypto->addBytes(clientDataJson.span());
190190
return crypto->computeHash();
191191
}
192192

‎Source/WebCore/Modules/websockets/WebSocketHandshake.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ static String generateSecWebSocketKey()
100100

101101
String WebSocketHandshake::getExpectedWebSocketAccept(const String& secWebSocketKey)
102102
{
103-
constexpr uint8_t webSocketKeyGUID[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
103+
const auto webSocketKeyGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"_span;
104104
SHA1 sha1;
105105
CString keyData = secWebSocketKey.ascii();
106106
sha1.addBytes(keyData.span());
107-
sha1.addBytes(webSocketKeyGUID, std::size(webSocketKeyGUID) - 1);
107+
sha1.addBytes(webSocketKeyGUID);
108108
SHA1::Digest hash;
109109
sha1.computeHash(hash);
110110
return base64EncodeToString(hash.data(), SHA1::hashSize);

‎Source/WebCore/PAL/pal/crypto/CryptoDigest.h‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class CryptoDigest {
4747
PAL_EXPORT static std::unique_ptr<CryptoDigest> create(Algorithm);
4848
PAL_EXPORT ~CryptoDigest();
4949

50-
void addBytes(std::span<const uint8_t>);
51-
PAL_EXPORT void addBytes(const void* input, size_t length);
50+
PAL_EXPORT void addBytes(std::span<const uint8_t>);
5251
PAL_EXPORT Vector<uint8_t> computeHash();
5352
PAL_EXPORT String toHexString();
5453
PAL_EXPORT static std::optional<Vector<uint8_t>> computeHash(Algorithm, const Vector<uint8_t>&, bool);
@@ -58,9 +57,4 @@ class CryptoDigest {
5857
std::unique_ptr<CryptoDigestContext> m_context;
5958
};
6059

61-
inline void CryptoDigest::addBytes(std::span<const uint8_t> input)
62-
{
63-
return addBytes(input.data(), input.size());
64-
}
65-
6660
} // namespace PAL

0 commit comments

Comments
 (0)