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 "CryptoAlgorithmAesCbcCfbParams.h"
32 #include "CryptoKeyAES.h"
33 #include "ScriptExecutionContext.h"
34 #include <CommonCrypto/CommonCrypto.h>
38 static ExceptionOr<Vector<uint8_t>> transformAES_CBC(CCOperation operation, const Vector<uint8_t>& iv, const Vector<uint8_t>& key, const Vector<uint8_t>& data)
41 CCCryptorStatus status = CCCryptorCreate(operation, kCCAlgorithmAES, kCCOptionPKCS7Padding, key.data(), key.size(), iv.data(), &cryptor);
43 return Exception { OperationError };
45 Vector<uint8_t> result(CCCryptorGetOutputLength(cryptor, data.size(), true));
48 status = CCCryptorUpdate(cryptor, data.data(), data.size(), result.data(), result.size(), &bytesWritten);
50 return Exception { OperationError };
52 uint8_t* p = result.data() + bytesWritten;
53 status = CCCryptorFinal(cryptor, p, result.end() - p, &bytesWritten);
56 return Exception { OperationError };
58 ASSERT(p <= result.end());
59 result.shrink(p - result.begin());
61 CCCryptorRelease(cryptor);
63 return WTFMove(result);
66 void CryptoAlgorithmAES_CBC::platformEncrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
69 workQueue.dispatch([parameters = WTFMove(parameters), key = WTFMove(key), plainText = WTFMove(plainText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
70 auto& aesParameters = downcast<CryptoAlgorithmAesCbcCfbParams>(*parameters);
71 auto& aesKey = downcast<CryptoKeyAES>(key.get());
72 ASSERT(aesParameters.ivVector().size() == kCCBlockSizeAES128);
73 auto result = transformAES_CBC(kCCEncrypt, aesParameters.ivVector(), aesKey.key(), plainText);
74 if (result.hasException()) {
75 // We should only dereference callbacks after being back to the Document/Worker threads.
76 context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
77 exceptionCallback(ec);
82 // We should only dereference callbacks after being back to the Document/Worker threads.
83 context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
90 void CryptoAlgorithmAES_CBC::platformDecrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
93 workQueue.dispatch([parameters = WTFMove(parameters), key = WTFMove(key), cipherText = WTFMove(cipherText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
94 auto& aesParameters = downcast<CryptoAlgorithmAesCbcCfbParams>(*parameters);
95 auto& aesKey = downcast<CryptoKeyAES>(key.get());
96 assert(aesParameters.ivVector().size() == kCCBlockSizeAES128);
97 auto result = transformAES_CBC(kCCDecrypt, aesParameters.ivVector(), aesKey.key(), cipherText);
98 if (result.hasException()) {
99 // We should only dereference callbacks after being back to the Document/Worker threads.
100 context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
101 exceptionCallback(ec);
106 // We should only dereference callbacks after being back to the Document/Worker threads.
107 context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
114 } // namespace WebCore
116 #endif // ENABLE(SUBTLE_CRYPTO)