#include "components/webcrypto/algorithms/test_helpers.h"
#include <stddef.h>
#include <algorithm>
#include "base/base64url.h"
#include "base/check.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "components/webcrypto/algorithm_dispatch.h"
#include "components/webcrypto/generate_key_result.h"
#include "components/webcrypto/jwk.h"
#include "components/webcrypto/status.h"
#include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
#include "third_party/blink/public/platform/web_crypto_key_algorithm.h"
#include "third_party/re2/src/re2/re2.h"
namespace webcrypto {
namespace {
bool Base64DecodeUrlSafe(std::string_view input, std::string* output) { … }
std::optional<base::Value> ReadJsonTestFile(const char* test_file_name) { … }
}
void WebCryptoTestBase::SetUpTestSuite() { … }
void PrintTo(const Status& status, ::std::ostream* os) { … }
bool operator==(const Status& a, const Status& b) { … }
bool operator!=(const Status& a, const Status& b) { … }
static std::string ErrorTypeToString(blink::WebCryptoErrorType type) { … }
std::string StatusToString(const Status& status) { … }
blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm(
blink::WebCryptoAlgorithmId algorithm_id,
const blink::WebCryptoAlgorithmId hash_id,
unsigned int modulus_length,
const std::vector<uint8_t>& public_exponent) { … }
std::vector<uint8_t> Corrupted(const std::vector<uint8_t>& input) { … }
std::vector<uint8_t> HexStringToBytes(std::string_view hex) { … }
std::vector<uint8_t> MakeJsonVector(const base::ValueView& value) { … }
base::Value::List ReadJsonTestFileAsList(const char* test_file_name) { … }
std::vector<uint8_t> GetBytesFromHexString(const base::Value::Dict& dict,
std::string_view property_name) { … }
blink::WebCryptoAlgorithm GetDigestAlgorithm(const base::Value::Dict& dict,
const char* property_name) { … }
class CompareUsingIndex { … };
bool CopiesExist(const std::vector<std::vector<uint8_t>>& bufs) { … }
blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
blink::WebCryptoAlgorithmId aes_alg_id,
uint16_t length) { … }
const unsigned int kModulusLengthBits = …;
const char* const kPublicKeySpkiDerHex = …;
const char* const kPrivateKeyPkcs8DerHex = …;
const char* const kPublicKeyModulusHex = …;
const char* const kPublicKeyExponentHex = …;
blink::WebCryptoKey ImportSecretKeyFromRaw(
const std::vector<uint8_t>& key_raw,
const blink::WebCryptoAlgorithm& algorithm,
blink::WebCryptoKeyUsageMask usage) { … }
void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der,
const std::vector<uint8_t>& pkcs8_der,
const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask public_key_usages,
blink::WebCryptoKeyUsageMask private_key_usages,
blink::WebCryptoKey* public_key,
blink::WebCryptoKey* private_key) { … }
Status ImportKeyJwkFromDict(const base::ValueView& dict,
const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoKey* key) { … }
std::optional<base::Value::Dict> GetJwkDictionary(
const std::vector<uint8_t>& json) { … }
::testing::AssertionResult VerifyJwk(
const base::Value::Dict& dict,
std::string_view kty_expected,
std::string_view alg_expected,
blink::WebCryptoKeyUsageMask use_mask_expected) { … }
::testing::AssertionResult VerifySecretJwk(
const std::vector<uint8_t>& json,
std::string_view alg_expected,
std::string_view k_expected_hex,
blink::WebCryptoKeyUsageMask use_mask_expected) { … }
::testing::AssertionResult VerifyPublicJwk(
const std::vector<uint8_t>& json,
std::string_view alg_expected,
std::string_view n_expected_hex,
std::string_view e_expected_hex,
blink::WebCryptoKeyUsageMask use_mask_expected) { … }
void ImportExportJwkSymmetricKey(
int key_len_bits,
const blink::WebCryptoAlgorithm& import_algorithm,
blink::WebCryptoKeyUsageMask usages,
std::string_view jwk_alg) { … }
Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoKey* key) { … }
Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoKey* public_key,
blink::WebCryptoKey* private_key) { … }
blink::WebCryptoKeyFormat GetKeyFormatFromJsonTestCase(
const base::Value::Dict& test) { … }
std::vector<uint8_t> GetKeyDataFromJsonTestCase(
const base::Value::Dict& test,
blink::WebCryptoKeyFormat key_format) { … }
blink::WebCryptoNamedCurve GetCurveNameFromDictionary(
const base::Value::Dict& dict) { … }
blink::WebCryptoNamedCurve CurveNameToCurve(std::string_view name) { … }
blink::WebCryptoAlgorithm CreateHmacImportAlgorithm(
blink::WebCryptoAlgorithmId hash_id,
unsigned int length_bits) { … }
blink::WebCryptoAlgorithm CreateHmacImportAlgorithmNoLength(
blink::WebCryptoAlgorithmId hash_id) { … }
blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { … }
blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm(
blink::WebCryptoAlgorithmId id,
blink::WebCryptoAlgorithmId hash_id) { … }
blink::WebCryptoAlgorithm CreateEcImportAlgorithm(
blink::WebCryptoAlgorithmId id,
blink::WebCryptoNamedCurve named_curve) { … }
}