/* * Copyright (C) 2020 Sony Interactive Entertainment Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #include "CryptoAlgorithmAES_CBC.h" #if ENABLE(WEB_CRYPTO) #include "CryptoAlgorithmAesCbcCfbParams.h" #include "CryptoKeyAES.h" #include "OpenSSLCryptoUniquePtr.h" #include namespace WebCore { static const EVP_CIPHER* aesAlgorithm(size_t keySize) { if (keySize * 8 == 128) return EVP_aes_128_cbc(); if (keySize * 8 == 192) return EVP_aes_192_cbc(); if (keySize * 8 == 256) return EVP_aes_256_cbc(); return nullptr; } static std::optional> cryptEncrypt(const Vector& key, const Vector& iv, Vector&& plainText) { const EVP_CIPHER* algorithm = aesAlgorithm(key.size()); if (!algorithm) return std::nullopt; EvpCipherCtxPtr ctx; int len; // Create and initialize the context if (!(ctx = EvpCipherCtxPtr(EVP_CIPHER_CTX_new()))) return std::nullopt; size_t plainSize = plainText.size(); const size_t cipherTextLen = roundUpToMultipleOf(EVP_CIPHER_block_size(algorithm), plainSize + 1); Vector cipherText(cipherTextLen); // Initialize the encryption operation if (1 != EVP_EncryptInit_ex(ctx.get(), algorithm, nullptr, key.data(), iv.data())) return std::nullopt; // Provide the message to be encrypted, and obtain the encrypted output if (1 != EVP_EncryptUpdate(ctx.get(), cipherText.data(), &len, plainText.data(), plainSize)) return std::nullopt; // Finalize the encryption. Further ciphertext bytes may be written at this stage if (1 != EVP_EncryptFinal_ex(ctx.get(), cipherText.data() + len, &len)) return std::nullopt; return cipherText; } static std::optional> cryptDecrypt(const Vector& key, const Vector& iv, const Vector& cipherText) { const EVP_CIPHER* algorithm = aesAlgorithm(key.size()); if (!algorithm) return std::nullopt; EvpCipherCtxPtr ctx; size_t cipherSize = cipherText.size(); Vector plainText(cipherSize); int len; int plainTextLen; // Create and initialize the context if (!(ctx = EvpCipherCtxPtr(EVP_CIPHER_CTX_new()))) return std::nullopt; // Initialize the decryption operation if (1 != EVP_DecryptInit_ex(ctx.get(), algorithm, nullptr, key.data(), iv.data())) return std::nullopt; // Provide the message to be decrypted, and obtain the plaintext output if (1 != EVP_DecryptUpdate(ctx.get(), plainText.data(), &len, cipherText.data(), cipherSize)) return std::nullopt; plainTextLen = len; // Finalize the decryption. Further plaintext bytes may be written at this stage if (1 != EVP_DecryptFinal_ex(ctx.get(), plainText.data() + len, &len)) return std::nullopt; plainTextLen += len; plainText.shrink(plainTextLen); return plainText; } ExceptionOr> CryptoAlgorithmAES_CBC::platformEncrypt(const CryptoAlgorithmAesCbcCfbParams& parameters, const CryptoKeyAES& key, const Vector& plainText, Padding) { auto output = cryptEncrypt(key.key(), parameters.ivVector(), Vector(plainText)); if (!output) return Exception { OperationError }; return WTFMove(*output); } ExceptionOr> CryptoAlgorithmAES_CBC::platformDecrypt(const CryptoAlgorithmAesCbcCfbParams& parameters, const CryptoKeyAES& key, const Vector& cipherText, Padding) { auto output = cryptDecrypt(key.key(), parameters.ivVector(), cipherText); if (!output) return Exception { OperationError }; return WTFMove(*output); } } // namespace WebCore #endif // ENABLE(WEB_CRYPTO) vitest Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
AgeCommit message (Expand)AuthorFilesLines
2022-02-07[ci] update lockfile (#2543)Gravatar Fred K. Schott 1-171/+178
2022-02-07improve debug logs (#2537)Gravatar Fred K. Schott 3-4/+19
2022-02-07[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-06[ci] update lockfile (#2527)Gravatar Fred K. Schott 1-208/+238
2022-02-06[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-05[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-04[ci] yarn formatGravatar natemoo-re 1-2/+3
2022-02-04fix: HTML/SVG boolean attributes (#2538)Gravatar Nate Moore 2-3/+21
2022-02-04[ci] yarn formatGravatar matthewp 1-4/+2
2022-02-04fix: import local plugins into markdown (#2534)Gravatar Juan Martín Seery 9-22/+41
2022-02-04[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-03Append to list of HMR modules, don't override (#2532)Gravatar Matthew Phillips 2-1/+6
2022-02-03add back dev server host support (#2531)Gravatar Fred K. Schott 2-1/+14
2022-02-03simplify status code regexGravatar Fred K. Schott 2-87/+4
2022-02-03Adding StackUp Digital to the list of sponsors (#2521)Gravatar Astroalex 3-0/+10
2022-02-03[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-03[ci] yarn formatGravatar FredKSchott 2-3/+85
2022-02-02Handles all http error code file names the same as 404 files. (#2525)Gravatar Zade Viggers 2-4/+7
2022-02-02fix(sitemap): remove debug if sitemap disabled (#2514)Gravatar Mark Pinero 1-2/+2
2022-02-02[ci] update lockfile (#2515)Gravatar Fred K. Schott 1-276/+279
2022-02-02[ci] yarn formatGravatar matthewp 1-8/+8
2022-02-02[ci] release (next) (#2523)astro@0.23.0-next.1Gravatar github-actions[bot] 28-34/+41
2022-02-02[ci] yarn formatGravatar matthewp 2-17/+29
2022-02-02Fix support for scss in static build (#2522)Gravatar Matthew Phillips 6-20/+114
2022-02-02[ci] collect statsGravatar FredKSchott 1-0/+1
2022-02-01[ci] yarn formatGravatar matthewp 2-12/+12
2022-02-01[ci] release (next) (#2492)astro@0.23.0-next.0@astrojs/test-static-build-pkg@0.0.2@astrojs/markdown-remark@0.6.1-next.0Gravatar github-actions[bot] 31-43/+93
2022-02-01[ci] collect statsGravatar FredKSchott 1-0/+1
2022-01-31update congratsbot format againGravatar Fred K. Schott 1-1/+1
2022-01-31update congratsbot againGravatar Fred K. Schott 1-1/+1
2022-01-31Remove SVG animation on GitHub/NPM (#2512)Gravatar Nate Moore 1-21/+0
2022-01-31[ci] yarn formatGravatar natemoo-re 2-4/+6
2022-01-31Add Shiki as an alternative to Prism (#2497)Gravatar Juan Martín Seery 26-9/+356
2022-01-31Deprecate unescaped HTML inside of expressions (#2489)Gravatar Nate Moore 9-31/+74