2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CryptoAlgorithmAES_CBC.h"
29 #if ENABLE(SUBTLE_CRYPTO)
31 #include "CryptoAlgorithmAesCbcParams.h"
32 #include "CryptoAlgorithmAesCbcParamsDeprecated.h"
33 #include "CryptoKeyAES.h"
34 #include "ExceptionCode.h"
35 #include "ScriptExecutionContext.h"
36 #include <CommonCrypto/CommonCrypto.h>
40 // FIXME: We should change iv and data to Vector<uint8_t> type once WebKitSubtleCrypto is deprecated.
41 // https://bugs.webkit.org/show_bug.cgi?id=164939
42 static ExceptionOr<Vector<uint8_t>> transformAES_CBC(CCOperation operation, const uint8_t* iv, const Vector<uint8_t>& key, const uint8_t* data, size_t dataLength)
44 size_t keyLengthInBytes = key.size();
47 CCAlgorithm aesAlgorithm = kCCAlgorithmAES;
49 CCAlgorithm aesAlgorithm = kCCAlgorithmAES128;
51 CCCryptorStatus status = CCCryptorCreate(operation, aesAlgorithm, kCCOptionPKCS7Padding, key.data(), keyLengthInBytes, iv, &cryptor);
53 return Exception { OperationError };
55 Vector<uint8_t> result(CCCryptorGetOutputLength(cryptor, dataLength, true));
58 status = CCCryptorUpdate(cryptor, data, dataLength, result.data(), result.size(), &bytesWritten);
60 return Exception { OperationError };
62 uint8_t* p = result.data() + bytesWritten;
63 status = CCCryptorFinal(cryptor, p, result.end() - p, &bytesWritten);
66 return Exception { OperationError };
68 ASSERT(p <= result.end());
69 result.shrink(p - result.begin());
71 CCCryptorRelease(cryptor);
73 return WTFMove(result);
76 void CryptoAlgorithmAES_CBC::platformEncrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
79 workQueue.dispatch([parameters = WTFMove(parameters), key = WTFMove(key), plainText = WTFMove(plainText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
80 auto& aesParameters = downcast<CryptoAlgorithmAesCbcParams>(*parameters);
81 auto& aesKey = downcast<CryptoKeyAES>(key.get());
82 ASSERT(aesParameters.ivVector().size() == kCCBlockSizeAES128);
83 auto result = transformAES_CBC(kCCEncrypt, aesParameters.ivVector().data(), aesKey.key(), plainText.data(), plainText.size());
84 if (result.hasException()) {
85 // We should only derenference callbacks after being back to the Document/Worker threads.
86 context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
87 exceptionCallback(ec);
92 // We should only derenference callbacks after being back to the Document/Worker threads.
93 context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
100 void CryptoAlgorithmAES_CBC::platformDecrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
103 workQueue.dispatch([parameters = WTFMove(parameters), key = WTFMove(key), cipherText = WTFMove(cipherText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
104 auto& aesParameters = downcast<CryptoAlgorithmAesCbcParams>(*parameters);
105 auto& aesKey = downcast<CryptoKeyAES>(key.get());
106 assert(aesParameters.ivVector().size() == kCCBlockSizeAES128);
107 auto result = transformAES_CBC(kCCDecrypt, aesParameters.ivVector().data(), aesKey.key(), cipherText.data(), cipherText.size());
108 if (result.hasException()) {
109 // We should only derenference callbacks after being back to the Document/Worker threads.
110 context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
111 exceptionCallback(ec);
116 // We should only derenference callbacks after being back to the Document/Worker threads.
117 context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
124 ExceptionOr<void> CryptoAlgorithmAES_CBC::platformEncrypt(const CryptoAlgorithmAesCbcParamsDeprecated& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback)
126 ASSERT(sizeof(parameters.iv) == kCCBlockSizeAES128);
127 auto result = transformAES_CBC(kCCEncrypt, parameters.iv.data(), key.key(), data.first, data.second);
128 if (result.hasException()) {
132 callback(result.releaseReturnValue());
136 ExceptionOr<void> CryptoAlgorithmAES_CBC::platformDecrypt(const CryptoAlgorithmAesCbcParamsDeprecated& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback)
138 ASSERT(sizeof(parameters.iv) == kCCBlockSizeAES128);
139 auto result = transformAES_CBC(kCCDecrypt, parameters.iv.data(), key.key(), data.first, data.second);
140 if (result.hasException()) {
144 callback(result.releaseReturnValue());
148 } // namespace WebCore
150 #endif // ENABLE(SUBTLE_CRYPTO)