Changeset View
Changeset View
Standalone View
Standalone View
src/crypto/key-export.hpp
- This file was added.
| /* | |||||
| * This file is part of libkazv. | |||||
| * SPDX-FileCopyrightText: 2025 tusooa <tusooa@kazv.moe> | |||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | |||||
| */ | |||||
| #pragma once | |||||
| #include <libkazv-config.hpp> | |||||
| #include <vector> | |||||
| #include <nlohmann/json.hpp> | |||||
| #include <maybe.hpp> | |||||
| #include "crypto-util.hpp" | |||||
| namespace Kazv | |||||
| { | |||||
| /** | |||||
| * Derive the key-export key from the user-inputted password. | |||||
| * | |||||
| * @param password The user-inputted password. | |||||
| * @param salt The salt from the key export file, or randomly generated when | |||||
| * exporting. | |||||
| * @param iterations The number of iterations from the key export file, or | |||||
| * manually specified when exporting. | |||||
| * @return A pair of [K, K'] specified in the Key Export section in | |||||
| * the matrix spec: <https://spec.matrix.org/v1.15/client-server-api/#key-export-format>. | |||||
| */ | |||||
| std::pair<ByteArray, ByteArray> deriveKeyExportKey( | |||||
| std::string password, | |||||
| ByteArray salt, | |||||
| std::size_t iterations | |||||
| ); | |||||
| namespace DecryptKeyExportErrorCodes | |||||
| { | |||||
| /// The file content does not conform to the spec. | |||||
| static const std::string FILE_MALFORMED{"FILE_MALFORMED"}; | |||||
| /// The version of the export file is not supported. | |||||
| static const std::string VERSION_UNSUPPORTED{"VERSION_UNSUPPORTED"}; | |||||
| /// The decrypted content cannot be parsed as json. | |||||
| static const std::string NOT_JSON{"NOT_JSON"}; | |||||
| /// The HMAC verification failed. This usually means the password is not correct, or the backup file was corrupted. | |||||
| static const std::string HMAC_FAILED{"HMAC_FAILED"}; | |||||
| } | |||||
| /** | |||||
| * Decrypt the key-export file with the user-inputted password. | |||||
| * | |||||
| * @param exportContent The file content. | |||||
| * @param password The password inputted by the user. | |||||
| * @return A Maybe containing the json of the keys contained in the file, | |||||
| * if the operation is successful. Otherwise, it contains the error code | |||||
| * defined in the namespace Kazv::DecryptKeyExportErrorCodes. | |||||
| */ | |||||
| Maybe<nlohmann::json> decryptKeyExport(std::string exportContent, std::string password); | |||||
| } | |||||