Changeset 214821 in webkit


Ignore:
Timestamp:
Apr 3, 2017 11:10:29 AM (7 years ago)
Author:
zandobersek@gmail.com
Message:

[GCrypt] Implement PBKDF2 support
https://bugs.webkit.org/show_bug.cgi?id=170270

Reviewed by Michael Catanzaro.

Implement the CryptoAlgorithmPBKDF2::platformDeriveBits() functionality
for configurations that use libgcrypt. This is done by leveraging the
gcry_kdf_derive() API, using GCRY_KDF_PBKDF2 as the preferred KDF
along with the properly deducted SHA algorithm.

No new tests -- current ones cover this sufficiently, but are not yet
enabled due to other missing platform-specific SUBTLE_CRYPTO
implementations.

  • crypto/gcrypt/CryptoAlgorithmPBKDF2GCrypt.cpp:

(WebCore::gcryptDeriveBits):
(WebCore::CryptoAlgorithmPBKDF2::platformDeriveBits):

Location:
trunk/Source/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r214819 r214821  
     12017-04-03  Zan Dobersek  <zdobersek@igalia.com>
     2
     3        [GCrypt] Implement PBKDF2 support
     4        https://bugs.webkit.org/show_bug.cgi?id=170270
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        Implement the CryptoAlgorithmPBKDF2::platformDeriveBits() functionality
     9        for configurations that use libgcrypt. This is done by leveraging the
     10        gcry_kdf_derive() API, using GCRY_KDF_PBKDF2 as the preferred KDF
     11        along with the properly deducted SHA algorithm.
     12
     13        No new tests -- current ones cover this sufficiently, but are not yet
     14        enabled due to other missing platform-specific SUBTLE_CRYPTO
     15        implementations.
     16
     17        * crypto/gcrypt/CryptoAlgorithmPBKDF2GCrypt.cpp:
     18        (WebCore::gcryptDeriveBits):
     19        (WebCore::CryptoAlgorithmPBKDF2::platformDeriveBits):
     20
    1212017-04-01  Simon Fraser  <simon.fraser@apple.com>
    222
  • trunk/Source/WebCore/crypto/gcrypt/CryptoAlgorithmPBKDF2GCrypt.cpp

    r214538 r214821  
    11/*
    22 * Copyright (C) 2017 Apple Inc. All rights reserved.
     3 * Copyright (C) 2017 Metrological Group B.V.
     4 * Copyright (C) 2017 Igalia S.L.
    35 *
    46 * Redistribution and use in source and binary forms, with or without
     
    2931#if ENABLE(SUBTLE_CRYPTO)
    3032
    31 #include "NotImplemented.h"
     33#include "CryptoAlgorithmPbkdf2Params.h"
     34#include "CryptoKeyRaw.h"
     35#include "ExceptionCode.h"
     36#include "ScriptExecutionContext.h"
     37#include <pal/crypto/gcrypt/Utilities.h>
    3238
    3339namespace WebCore {
    3440
    35 void CryptoAlgorithmPBKDF2::platformDeriveBits(std::unique_ptr<CryptoAlgorithmParameters>&&, Ref<CryptoKey>&&, size_t, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&)
     41static std::optional<Vector<uint8_t>> gcryptDeriveBits(const Vector<uint8_t>& keyData, const Vector<uint8_t>& saltData, CryptoAlgorithmIdentifier hashIdentifier, size_t iterations, size_t length)
    3642{
    37     notImplemented();
     43    int hashAlgorithm;
     44    switch (hashIdentifier) {
     45    case CryptoAlgorithmIdentifier::SHA_1:
     46        hashAlgorithm = GCRY_MD_SHA1;
     47        break;
     48    case CryptoAlgorithmIdentifier::SHA_224:
     49        hashAlgorithm = GCRY_MD_SHA224;
     50        break;
     51    case CryptoAlgorithmIdentifier::SHA_256:
     52        hashAlgorithm = GCRY_MD_SHA256;
     53        break;
     54    case CryptoAlgorithmIdentifier::SHA_384:
     55        hashAlgorithm = GCRY_MD_SHA384;
     56        break;
     57    case CryptoAlgorithmIdentifier::SHA_512:
     58        hashAlgorithm = GCRY_MD_SHA512;
     59        break;
     60    default:
     61        return std::nullopt;
     62    }
     63
     64    // Length, in bits, is a multiple of 8, as guaranteed by CryptoAlgorithmPBKDF2::deriveBits().
     65    ASSERT(!(length % 8));
     66
     67    Vector<uint8_t> result(length / 8);
     68    gcry_error_t error = gcry_kdf_derive(keyData.data(), keyData.size(), GCRY_KDF_PBKDF2, hashAlgorithm, saltData.data(), saltData.size(), iterations, result.size(), result.data());
     69    if (error != GPG_ERR_NO_ERROR) {
     70        PAL::GCrypt::logError(error);
     71        return std::nullopt;
     72    }
     73
     74    return result;
     75}
     76
     77void CryptoAlgorithmPBKDF2::platformDeriveBits(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, size_t length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
     78{
     79    context.ref();
     80    workQueue.dispatch(
     81        [parameters = WTFMove(parameters), key = WTFMove(key), length, callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
     82            auto& pbkdf2Parameters = downcast<CryptoAlgorithmPbkdf2Params>(*parameters);
     83            auto& rawKey = downcast<CryptoKeyRaw>(key.get());
     84
     85            auto output = gcryptDeriveBits(rawKey.key(), pbkdf2Parameters.saltVector(), pbkdf2Parameters.hashIdentifier, pbkdf2Parameters.iterations, length);
     86            if (!output) {
     87                // We should only dereference callbacks after being back to the Document/Worker threads.
     88                context.postTask(
     89                    [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
     90                        exceptionCallback(OperationError);
     91                        context.deref();
     92                    });
     93                return;
     94            }
     95
     96            // We should only dereference callbacks after being back to the Document/Worker threads.
     97            context.postTask(
     98                [output = WTFMove(*output), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
     99                    callback(output);
     100                    context.deref();
     101                });
     102        });
    38103}
    39104
Note: See TracChangeset for help on using the changeset viewer.