/* * Copyright (C) 2023 Apple Inc. All rights reserved. * * 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 "CryptoAlgorithmEd25519.h" #if ENABLE(WEB_CRYPTO) #include "CryptoKeyOKP.h" #include #include // -- BUN -- #include namespace WebCore { static ExceptionOr> signEd25519(const Vector& sk, size_t len, const Vector& data) { uint8_t newSignature[64]; ED25519_sign(newSignature, data.data(), data.size(), sk.data()); return Vector(newSignature, 64); } ExceptionOr> CryptoAlgorithmEd25519::platformSign(const CryptoKeyOKP& key, const Vector& data) { return signEd25519(key.platformKey(), key.keySizeInBytes(), data); } static ExceptionOr verifyEd25519(const Vector& key, size_t keyLengthInBytes, const Vector& signature, const Vector data) { if (signature.size() != keyLengthInBytes * 2) return false; return ED25519_verify(data.data(), data.size(), signature.data(), key.data()) == 1; } ExceptionOr CryptoAlgorithmEd25519::platformVerify(const CryptoKeyOKP& key, const Vector& signature, const Vector& data) { return verifyEd25519(key.platformKey(), key.keySizeInBytes(), signature, data); } Ref CryptoAlgorithmEd25519::create() { return adoptRef(*new CryptoAlgorithmEd25519); } void CryptoAlgorithmEd25519::generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&) { if (usages & (CryptoKeyUsageEncrypt | CryptoKeyUsageDecrypt | CryptoKeyUsageDeriveKey | CryptoKeyUsageDeriveBits | CryptoKeyUsageWrapKey | CryptoKeyUsageUnwrapKey)) { exceptionCallback(SyntaxError); return; } auto result = CryptoKeyOKP::generatePair(CryptoAlgorithmIdentifier::Ed25519, CryptoKeyOKP::NamedCurve::Ed25519, extractable, usages); if (result.hasException()) { exceptionCallback(result.releaseException().code()); return; } auto pair = result.releaseReturnValue(); pair.publicKey->setUsagesBitmap(pair.publicKey->usagesBitmap() & CryptoKeyUsageVerify); pair.privateKey->setUsagesBitmap(pair.privateKey->usagesBitmap() & CryptoKeyUsageSign); callback(WTFMove(pair)); } void CryptoAlgorithmEd25519::sign(const CryptoAlgorithmParameters&, Ref&& key, Vector&& data, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) { if (key->type() != CryptoKeyType::Private) { exceptionCallback(InvalidAccessError); return; } dispatchOperationInWorkQueue(workQueue, context, WTFMove(callback), WTFMove(exceptionCallback), [key = WTFMove(key), data = WTFMove(data)] { return platformSign(downcast(key.get()), data); }); } void CryptoAlgorithmEd25519::verify(const CryptoAlgorithmParameters&, Ref&& key, Vector&& signature, Vector&& data, BoolCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) { if (key->type() != CryptoKeyType::Public) { exceptionCallback(InvalidAccessError); return; } dispatchOperationInWorkQueue(workQueue, context, WTFMove(callback), WTFMove(exceptionCallback), [key = WTFMove(key), signature = WTFMove(signature), data = WTFMove(data)] { return platformVerify(downcast(key.get()), signature, data); }); } void CryptoAlgorithmEd25519::importKey(CryptoKeyFormat format, KeyData&& data, const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap usages, KeyCallback&& callback, ExceptionCallback&& exceptionCallback) { RefPtr result; switch (format) { case CryptoKeyFormat::Jwk: { JsonWebKey key = WTFMove(std::get(data)); if (usages && ((!key.d.isNull() && (usages ^ CryptoKeyUsageSign)) || (key.d.isNull() && (usages ^ CryptoKeyUsageVerify)))) { exceptionCallback(SyntaxError); return; } if (usages && !key.use.isNull() && key.use != "sig"_s) { exceptionCallback(DataError); return; } result = CryptoKeyOKP::importJwk(CryptoAlgorithmIdentifier::Ed25519, CryptoKeyOKP::NamedCurve::Ed25519, WTFMove(key), extractable, usages); break; } case CryptoKeyFormat::Raw: if (usages && (usages ^ CryptoKeyUsageVerify)) { exceptionCallback(SyntaxError); return; } result = CryptoKeyOKP::importRaw(CryptoAlgorithmIdentifier::Ed25519, CryptoKeyOKP::NamedCurve::Ed25519, WTFMove(std::get>(data)), extractable, usages); break; case CryptoKeyFormat::Spki: if (usages && (usages ^ CryptoKeyUsageVerify)) { exceptionCallback(SyntaxError); return; } result = CryptoKeyOKP::importSpki(CryptoAlgorithmIdentifier::Ed25519, CryptoKeyOKP::NamedCurve::Ed25519, WTFMove(std::get>(data)), extractable, usages); break; case CryptoKeyFormat::Pkcs8: if (usages && (usages ^ CryptoKeyUsageSign)) { exceptionCallback(SyntaxError); return; } result = CryptoKeyOKP::importPkcs8(CryptoAlgorithmIdentifier::Ed25519, CryptoKeyOKP::NamedCurve::Ed25519, WTFMove(std::get>(data)), extractable, usages); break; } if (!result) { exceptionCallback(DataError); return; } callback(*result); } void CryptoAlgorithmEd25519::exportKey(CryptoKeyFormat format, Ref&& key, KeyDataCallback&& callback, ExceptionCallback&& exceptionCallback) { const auto& okpKey = downcast(key.get()); if (!okpKey.keySizeInBits()) { exceptionCallback(OperationError); return; } KeyData result; switch (format) { case CryptoKeyFormat::Jwk: { auto jwk = okpKey.exportJwk(); if (jwk.hasException()) { exceptionCallback(jwk.releaseException().code()); return; } result = jwk.releaseReturnValue(); break; } case CryptoKeyFormat::Raw: { auto raw = okpKey.exportRaw(); if (raw.hasException()) { exceptionCallback(raw.releaseException().code()); return; } result = raw.releaseReturnValue(); break; } case CryptoKeyFormat::Spki: { auto spki = okpKey.exportSpki(); if (spki.hasException()) { exceptionCallback(spki.releaseException().code()); return; } result = spki.releaseReturnValue(); break; } case CryptoKeyFormat::Pkcs8: { auto pkcs8 = okpKey.exportPkcs8(); if (pkcs8.hasException()) { exceptionCallback(pkcs8.releaseException().code()); return; } result = pkcs8.releaseReturnValue(); break; } } callback(format, WTFMove(result)); } } // namespace WebCore #endif // ENABLE(WEB_CRYPTO)