17777dab0Sopenharmony_ci/*
27777dab0Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
37777dab0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47777dab0Sopenharmony_ci * you may not use this file except in compliance with the License.
57777dab0Sopenharmony_ci * You may obtain a copy of the License at
67777dab0Sopenharmony_ci *
77777dab0Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
87777dab0Sopenharmony_ci *
97777dab0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107777dab0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117777dab0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127777dab0Sopenharmony_ci * See the License for the specific language governing permissions and
137777dab0Sopenharmony_ci * limitations under the License.
147777dab0Sopenharmony_ci */
157777dab0Sopenharmony_ci
167777dab0Sopenharmony_ci/**
177777dab0Sopenharmony_ci * @addtogroup CryptoAsymKeyApi
187777dab0Sopenharmony_ci * @{
197777dab0Sopenharmony_ci *
207777dab0Sopenharmony_ci * @brief Describe the features provided by the openHarmony asymmetric key related interface for applications.
217777dab0Sopenharmony_ci *
227777dab0Sopenharmony_ci * @since 12
237777dab0Sopenharmony_ci */
247777dab0Sopenharmony_ci
257777dab0Sopenharmony_ci/**
267777dab0Sopenharmony_ci * @file crypto_asym_key.h
277777dab0Sopenharmony_ci *
287777dab0Sopenharmony_ci * @brief Defines the AsymKey APIs.
297777dab0Sopenharmony_ci *
307777dab0Sopenharmony_ci * @library libohcrypto.so
317777dab0Sopenharmony_ci * @kit CryptoArchitectureKit
327777dab0Sopenharmony_ci * @syscap SystemCapability.Security.CryptoFramework
337777dab0Sopenharmony_ci * @since 12
347777dab0Sopenharmony_ci */
357777dab0Sopenharmony_ci
367777dab0Sopenharmony_ci#ifndef CRYPTO_ASYM_KEY_H
377777dab0Sopenharmony_ci#define CRYPTO_ASYM_KEY_H
387777dab0Sopenharmony_ci
397777dab0Sopenharmony_ci#include "crypto_common.h"
407777dab0Sopenharmony_ci
417777dab0Sopenharmony_ci#ifdef __cplusplus
427777dab0Sopenharmony_ciextern "C" {
437777dab0Sopenharmony_ci#endif
447777dab0Sopenharmony_ci
457777dab0Sopenharmony_ci/**
467777dab0Sopenharmony_ci * @brief Define the key pair structure.
477777dab0Sopenharmony_ci *
487777dab0Sopenharmony_ci * @since 12
497777dab0Sopenharmony_ci */
507777dab0Sopenharmony_citypedef struct OH_CryptoKeyPair OH_CryptoKeyPair;
517777dab0Sopenharmony_ci
527777dab0Sopenharmony_ci/**
537777dab0Sopenharmony_ci * @brief Define the public Key structure.
547777dab0Sopenharmony_ci *
557777dab0Sopenharmony_ci * @since 12
567777dab0Sopenharmony_ci */
577777dab0Sopenharmony_citypedef struct OH_CryptoPubKey OH_CryptoPubKey;
587777dab0Sopenharmony_ci
597777dab0Sopenharmony_ci/**
607777dab0Sopenharmony_ci * @brief Define the asymmetric key parameter types.
617777dab0Sopenharmony_ci *
627777dab0Sopenharmony_ci * @since 12
637777dab0Sopenharmony_ci */
647777dab0Sopenharmony_citypedef enum {
657777dab0Sopenharmony_ci    /** Indicates the DSA prime p. */
667777dab0Sopenharmony_ci    CRYPTO_DSA_P_DATABLOB = 101,
677777dab0Sopenharmony_ci    /** Indicates the DSA sub-prime q. */
687777dab0Sopenharmony_ci    CRYPTO_DSA_Q_DATABLOB = 102,
697777dab0Sopenharmony_ci    /** Indicates the DSA base g. */
707777dab0Sopenharmony_ci    CRYPTO_DSA_G_DATABLOB = 103,
717777dab0Sopenharmony_ci    /** Indicates the DSA private key. */
727777dab0Sopenharmony_ci    CRYPTO_DSA_SK_DATABLOB = 104,
737777dab0Sopenharmony_ci    /** Indicates the DSA public key. */
747777dab0Sopenharmony_ci    CRYPTO_DSA_PK_DATABLOB = 105,
757777dab0Sopenharmony_ci
767777dab0Sopenharmony_ci    /** Indicates the prime p of an elliptic curve (EC) prime finite field. */
777777dab0Sopenharmony_ci    CRYPTO_ECC_FP_P_DATABLOB = 201,
787777dab0Sopenharmony_ci    /** Indicates the first coefficient a of this elliptic curve. */
797777dab0Sopenharmony_ci    CRYPTO_ECC_A_DATABLOB = 202,
807777dab0Sopenharmony_ci    /** Indicates the second coefficient b of this elliptic curve. */
817777dab0Sopenharmony_ci    CRYPTO_ECC_B_DATABLOB = 203,
827777dab0Sopenharmony_ci    /** Indicates the affine x-coordinate of base point g. */
837777dab0Sopenharmony_ci    CRYPTO_ECC_G_X_DATABLOB = 204,
847777dab0Sopenharmony_ci    /** Indicates the affine y-coordinate of base point g. */
857777dab0Sopenharmony_ci    CRYPTO_ECC_G_Y_DATABLOB = 205,
867777dab0Sopenharmony_ci    /** Indicates the order of the base point g. */
877777dab0Sopenharmony_ci    CRYPTO_ECC_N_DATABLOB = 206,
887777dab0Sopenharmony_ci    /** Indicates the cofactor of the elliptic curve. */
897777dab0Sopenharmony_ci    CRYPTO_ECC_H_INT = 207,
907777dab0Sopenharmony_ci    /** Indicates the private value of the ECC private key. */
917777dab0Sopenharmony_ci    CRYPTO_ECC_SK_DATABLOB = 208,
927777dab0Sopenharmony_ci    /** Indicates the affine x-coordinate of a point, which is the public point of an ECC public key. */
937777dab0Sopenharmony_ci    CRYPTO_ECC_PK_X_DATABLOB = 209,
947777dab0Sopenharmony_ci    /** Indicates the affine y-coordinate of a point, which is the public point of an ECC public key. */
957777dab0Sopenharmony_ci    CRYPTO_ECC_PK_Y_DATABLOB = 210,
967777dab0Sopenharmony_ci    /** Indicates an elliptic curve finite field type. */
977777dab0Sopenharmony_ci    CRYPTO_ECC_FIELD_TYPE_STR = 211,
987777dab0Sopenharmony_ci    /** Indicates the field size in bits. */
997777dab0Sopenharmony_ci    CRYPTO_ECC_FIELD_SIZE_INT = 212,
1007777dab0Sopenharmony_ci    /** Indicates the curve name according to SECG (Standards for Efficient Cryptography Group). */
1017777dab0Sopenharmony_ci    CRYPTO_ECC_CURVE_NAME_STR = 213,
1027777dab0Sopenharmony_ci
1037777dab0Sopenharmony_ci    /** Indicates the modulus n of RSA algorithm. */
1047777dab0Sopenharmony_ci    CRYPTO_RSA_N_DATABLOB = 301,
1057777dab0Sopenharmony_ci    /** Indicates the private exponent d of RSA algorithm. */
1067777dab0Sopenharmony_ci    CRYPTO_RSA_D_DATABLOB = 302,
1077777dab0Sopenharmony_ci    /** Indicates the public exponent e of RSA algorithm. */
1087777dab0Sopenharmony_ci    CRYPTO_RSA_E_DATABLOB = 303,
1097777dab0Sopenharmony_ci
1107777dab0Sopenharmony_ci    /** Indicates the prime p of DH algorithm. */
1117777dab0Sopenharmony_ci    CRYPTO_DH_P_DATABLOB = 401,
1127777dab0Sopenharmony_ci    /** Indicates the generator g of DH algorithm. */
1137777dab0Sopenharmony_ci    CRYPTO_DH_G_DATABLOB = 402,
1147777dab0Sopenharmony_ci    /** Indicates the number of bits of the private key length used in the DH algorithm. */
1157777dab0Sopenharmony_ci    CRYPTO_DH_L_INT = 403,
1167777dab0Sopenharmony_ci    /** Indicates the private value of the DH private key. */
1177777dab0Sopenharmony_ci    CRYPTO_DH_SK_DATABLOB = 404,
1187777dab0Sopenharmony_ci    /** Indicates the public value of the DH public key. */
1197777dab0Sopenharmony_ci    CRYPTO_DH_PK_DATABLOB = 405,
1207777dab0Sopenharmony_ci
1217777dab0Sopenharmony_ci    /** Indicates the private value of the ED25519 private key. */
1227777dab0Sopenharmony_ci    CRYPTO_ED25519_SK_DATABLOB = 501,
1237777dab0Sopenharmony_ci    /** Indicates the public value of the ED25519 public key. */
1247777dab0Sopenharmony_ci    CRYPTO_ED25519_PK_DATABLOB = 502,
1257777dab0Sopenharmony_ci    /** Indicates the private value of the X25519 private key. */
1267777dab0Sopenharmony_ci    CRYPTO_X25519_SK_DATABLOB = 601,
1277777dab0Sopenharmony_ci    /** Indicates the public value of the X25519 public key. */
1287777dab0Sopenharmony_ci    CRYPTO_X25519_PK_DATABLOB = 602,
1297777dab0Sopenharmony_ci} CryptoAsymKey_ParamType;
1307777dab0Sopenharmony_ci
1317777dab0Sopenharmony_ci/**
1327777dab0Sopenharmony_ci * @brief Define the encoding type.
1337777dab0Sopenharmony_ci *
1347777dab0Sopenharmony_ci * @since 12
1357777dab0Sopenharmony_ci */
1367777dab0Sopenharmony_citypedef enum {
1377777dab0Sopenharmony_ci    /** PEM format */
1387777dab0Sopenharmony_ci    CRYPTO_PEM = 0,
1397777dab0Sopenharmony_ci    /** DER format */
1407777dab0Sopenharmony_ci    CRYPTO_DER = 1,
1417777dab0Sopenharmony_ci} Crypto_EncodingType;
1427777dab0Sopenharmony_ci
1437777dab0Sopenharmony_ci/**
1447777dab0Sopenharmony_ci * @brief Define the asymmetric key generator structure.
1457777dab0Sopenharmony_ci *
1467777dab0Sopenharmony_ci * @since 12
1477777dab0Sopenharmony_ci */
1487777dab0Sopenharmony_citypedef struct OH_CryptoAsymKeyGenerator OH_CryptoAsymKeyGenerator;
1497777dab0Sopenharmony_ci
1507777dab0Sopenharmony_ci/**
1517777dab0Sopenharmony_ci * @brief Create an asymmetric key generator according to the given algorithm name.
1527777dab0Sopenharmony_ci *
1537777dab0Sopenharmony_ci * @param algoName Indicates the algorithm name for generating the generator. Example RSA1024|PRIMES_2.
1547777dab0Sopenharmony_ci * @param ctx Indicates the pointer to asymmetric key generator context.
1557777dab0Sopenharmony_ci * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful.
1567777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid.
1577777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported.
1587777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed.
1597777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed.
1607777dab0Sopenharmony_ci * @since 12
1617777dab0Sopenharmony_ci */
1627777dab0Sopenharmony_ciOH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_CryptoAsymKeyGenerator **ctx);
1637777dab0Sopenharmony_ci
1647777dab0Sopenharmony_ci/**
1657777dab0Sopenharmony_ci * @brief Generate an asymmetric key(a key pair).
1667777dab0Sopenharmony_ci *
1677777dab0Sopenharmony_ci * @param ctx Indicates the asymmetric key generator context.
1687777dab0Sopenharmony_ci * @param keyCtx Indicates the pointer to the asyKey context.
1697777dab0Sopenharmony_ci * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful.
1707777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid.
1717777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported.
1727777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed.
1737777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed.
1747777dab0Sopenharmony_ci * @since 12
1757777dab0Sopenharmony_ci */
1767777dab0Sopenharmony_ciOH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Generate(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyCtx);
1777777dab0Sopenharmony_ci
1787777dab0Sopenharmony_ci/**
1797777dab0Sopenharmony_ci * @brief Convert the asymmetric key data to a key pair.
1807777dab0Sopenharmony_ci *
1817777dab0Sopenharmony_ci * @param ctx Indicates the asymmetric key generator context.
1827777dab0Sopenharmony_ci * @param type Indicates the encryption encoding type.
1837777dab0Sopenharmony_ci * @param pubKeyData Indicates the public key data.
1847777dab0Sopenharmony_ci * @param priKeyData Indicates the private key data.
1857777dab0Sopenharmony_ci * @param keyCtx Indicates the pointer to the keyPair instance.
1867777dab0Sopenharmony_ci * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful.
1877777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid.
1887777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported.
1897777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed.
1907777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed.
1917777dab0Sopenharmony_ci * @since 12
1927777dab0Sopenharmony_ci */
1937777dab0Sopenharmony_ciOH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_EncodingType type,
1947777dab0Sopenharmony_ci    Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx);
1957777dab0Sopenharmony_ci
1967777dab0Sopenharmony_ci/**
1977777dab0Sopenharmony_ci * @brief Get the algorithm name of the asymmetric key generator.
1987777dab0Sopenharmony_ci *
1997777dab0Sopenharmony_ci * @param ctx Indicates the asymmetric key generator context.
2007777dab0Sopenharmony_ci * @return Returns the asymmetric key algorithm name.
2017777dab0Sopenharmony_ci * @since 12
2027777dab0Sopenharmony_ci */
2037777dab0Sopenharmony_ciconst char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx);
2047777dab0Sopenharmony_ci
2057777dab0Sopenharmony_ci/**
2067777dab0Sopenharmony_ci * @brief Destroy the asymmetric key generator.
2077777dab0Sopenharmony_ci *
2087777dab0Sopenharmony_ci * @param ctx Indicates the asymmetric key generator context.
2097777dab0Sopenharmony_ci * @since 12
2107777dab0Sopenharmony_ci */
2117777dab0Sopenharmony_civoid OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx);
2127777dab0Sopenharmony_ci
2137777dab0Sopenharmony_ci/**
2147777dab0Sopenharmony_ci * @brief Destroy the key pair.
2157777dab0Sopenharmony_ci *
2167777dab0Sopenharmony_ci * @param keyCtx Indicates the keyPair context.
2177777dab0Sopenharmony_ci * @since 12
2187777dab0Sopenharmony_ci */
2197777dab0Sopenharmony_civoid OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx);
2207777dab0Sopenharmony_ci
2217777dab0Sopenharmony_ci/**
2227777dab0Sopenharmony_ci * @brief Get the public key of the key pair.
2237777dab0Sopenharmony_ci *
2247777dab0Sopenharmony_ci * @param keyCtx Indicates the keyPair context.
2257777dab0Sopenharmony_ci * @return Return the public key context from the key pair.
2267777dab0Sopenharmony_ci * @since 12
2277777dab0Sopenharmony_ci */
2287777dab0Sopenharmony_ciOH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx);
2297777dab0Sopenharmony_ci
2307777dab0Sopenharmony_ci/**
2317777dab0Sopenharmony_ci * @brief Encode the public key.
2327777dab0Sopenharmony_ci *
2337777dab0Sopenharmony_ci * @param key Indicates the public key.
2347777dab0Sopenharmony_ci * @param type Indicates the pubkey type.
2357777dab0Sopenharmony_ci * @param encodingStandard Indicates the encoding standard .
2367777dab0Sopenharmony_ci * @param out Indicates the encoded result.
2377777dab0Sopenharmony_ci * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful.
2387777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid.
2397777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported.
2407777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed.
2417777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed.
2427777dab0Sopenharmony_ci * @since 12
2437777dab0Sopenharmony_ci */
2447777dab0Sopenharmony_ciOH_Crypto_ErrCode OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_EncodingType type,
2457777dab0Sopenharmony_ci    const char *encodingStandard, Crypto_DataBlob *out);
2467777dab0Sopenharmony_ci
2477777dab0Sopenharmony_ci/**
2487777dab0Sopenharmony_ci * @brief Get the specified param of the public key.
2497777dab0Sopenharmony_ci *
2507777dab0Sopenharmony_ci * @param key Indicates the public key.
2517777dab0Sopenharmony_ci * @param item Indicates the asymmetric key param type.
2527777dab0Sopenharmony_ci * @param value Indicates the output data.
2537777dab0Sopenharmony_ci * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful.
2547777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid.
2557777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported.
2567777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed.
2577777dab0Sopenharmony_ci *         {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed.
2587777dab0Sopenharmony_ci * @since 12
2597777dab0Sopenharmony_ci */
2607777dab0Sopenharmony_ciOH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value);
2617777dab0Sopenharmony_ci
2627777dab0Sopenharmony_ci#ifdef __cplusplus
2637777dab0Sopenharmony_ci}
2647777dab0Sopenharmony_ci#endif
2657777dab0Sopenharmony_ci
2667777dab0Sopenharmony_ci/** @} */
2677777dab0Sopenharmony_ci#endif /* CRYPTO_ASYM_KEY_H */
268