---
title: RNG随机数获取接口
description: "```"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/252RC1/accel/kunpengaccel_17_0047.html
sourcePath: /source/zh/kunpengboostkithistory/252RC1/accel/kunpengaccel_17_0047.html
indexId: c86a68f5a7bbd8770feaa1bdad546ba1e289f313b139ef440ceae74a967f73f772
---
# RNG随机数获取接口

```
struct wcrypto_rng_msg {
__u8 alg_type; /* Denoted by enum wcrypto_type */
__u8 op_type;/* Denoted by enum wcrypto_rng_op_type */
__u8 data_fmt; /* Data format, denoted by enum wd_buff_type */
__u8 result;/* Data format, denoted by WD error code */
__u8 *out; /* Result address */
__u8 *in; /* Input address */
__u32 out_bytes; /* output bytes */
__u32 in_bytes; /* input bytes */
__u64 usr_tag; /* user identifier */
};

enum wcrypto_rng_op_type {
WCRYPTO_RNG_INVALID,/* Invalid RNG operational type */
WCRYPTO_DRBG_RESEED,/* seed operation */
WCRYPTO_DRBG_GEN,/* deterministic random number generation */
WCRYPTO_TRNG_GEN, /* true random number generation */
};

struct wcrypto_rng_ctx_setup {
wcrypto_cb cb;
__u16 data_fmt; /* Data format, denoted by enum wd_buff_type */
enum wcrypto_type type;/* Please refer to the definition of enum */
enum wcrypto_cipher_alg calg;/* DRBG cipher algorithm */
enum wcrypto_cipher_mode cmode; /* DRBG cipher mode */
enum wcrypto_digest_alg dalg; /* DRBG digest algorithm */
enum wcrypto_digest_mode dmode; /* DRBG digest mode */
};

struct wcrypto_rng_op_data {
enum wcrypto_rng_op_type op_type;
__u32 status; /* Operation result status */
void *in;/* input */
void *out; /* output */
__u32 in_bytes; /* input bytes */
__u32 out_bytes; /* output bytes */
};
```


| 函数原型 | void \* wcrypto\_create\_rng\_ctx(struct wd\_queue \*q, struct wcrypto\_rng\_ctx\_setup \*setup); |
| --- | --- |
| 函数功能 | 基于Warpdrive queue创建RNG算法context |
| 输入说明 | q：Warpdrive queue setup：用户对于context的创建输入参数 |
| 输出说明 | 无 |
| 返回值说明 | Context地址 NULL：失败 非NULL：成功 |
| 使用说明 | 支持多线程 |
| 注意事项 | 无 |


| 函数原型 | int wcrypto\_do\_rng(void \*ctx, struct wcrypto\_rng\_op\_data \*opdata, void \*tag); |
| --- | --- |
| 函数功能 | 基于RNG context的RNG算法操作 |
| 输入说明 | ctx：RNG context opdata：RNG操作参数，包括输入输出以及操作类型等 tag：用户回调标识，为NULL时，为同步接口，否则为异步接口 |
| 输出说明 | 无。 |
| 返回值说明 | 0：成功 否则失败 |
| 使用说明 | 同一个ctx仅支持单线程 |
| 注意事项 | 无 |


| 函数原型 | int wcrypto\_rng\_poll(struct wd\_queue \*q, unsigned int num); |
| --- | --- |
| 函数功能 | 基于warpdrive queue的RNG算法poll操作 |
| 输入说明 | q：Warpdrive RNG queue num：0表示将队列q中所有回应消息全部收上来，非0表示将队列中num个回应消息收上来 |
| 输出说明 | 无。 |
| 返回值说明 | 大于0：成功收到对应个数的回应消息 0：没有收到任何回应 负值：poll发生错误 |
| 使用说明 | 同一个q仅支持单线程 |
| 注意事项 | 无 |


| 函数原型 | void wcrypto\_del\_rng\_ctx(void \*ctx); |
| --- | --- |
| 函数功能 | 删除一个RNG的context |
| 输入说明 | ctx：需要删除的RNG context |
| 输出说明 | 无 |
| 返回值说明 | 无 |
| 使用说明 | 无 |
| 注意事项 | 无 |
