1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup CryptoRandApi 18 * @{ 19 * 20 * @brief Describes the random number generation interface provided to applications. 21 * 22 * @since 20 23 */ 24 /** 25 * @file crypto_rand.h 26 * 27 * @brief Defines the random number generator APIs. 28 * 29 * @library libohcrypto.so 30 * @kit CryptoArchitectureKit 31 * @syscap SystemCapability.Security.CryptoFramework 32 * @since 20 33 */ 34 #ifndef CRYPTO_RAND_H 35 #define CRYPTO_RAND_H 36 37 #include "crypto_common.h" 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif 42 43 /** 44 * @brief Defines the random number generator structure. 45 * 46 * @since 20 47 */ 48 typedef struct OH_CryptoRand OH_CryptoRand; 49 50 /** 51 * @brief Creates a random number generator context. 52 * 53 * @param ctx Indicates the random number generator context. 54 * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. 55 * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. 56 * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. 57 * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. 58 * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. 59 * @since 20 60 */ 61 OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx); 62 63 /** 64 * @brief Generates random numbers. 65 * 66 * @param ctx Indicates the random number generator context. 67 * @param len Indicates the byte length of the random number. 68 * @param out Indicates the output data. 69 * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. 70 * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. 71 * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. 72 * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. 73 * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. 74 * @since 20 75 */ 76 OH_Crypto_ErrCode OH_CryptoRand_GenerateRandom(OH_CryptoRand *ctx, int len, Crypto_DataBlob *out); 77 78 /** 79 * @brief Gets the algorithm name of the random number generator context. 80 * 81 * @param ctx Indicates the pointer to the random number generator context. 82 * @return Return the algorithm name of the random number generator context. 83 * @since 20 84 */ 85 const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx); 86 87 /** 88 * @brief Sets the seed to the random number generator context. 89 * 90 * @param ctx Indicates the random number generator context. 91 * @param seed Indicates the seed. 92 * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. 93 * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. 94 * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. 95 * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. 96 * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. 97 * @since 20 98 */ 99 OH_Crypto_ErrCode OH_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *seed); 100 101 /** 102 * @brief Destroys the random number generator context. 103 * 104 * @param ctx Indicates the random number generator context. 105 * @since 20 106 */ 107 void OH_CryptoRand_Destroy(OH_CryptoRand *ctx); 108 109 #ifdef __cplusplus 110 } 111 #endif 112 113 #endif /* CRYPTO_RAND_H */ 114 /** @} */ 115