---
title: DH算法类相关接口
description: "```"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_17_0045.html
sourcePath: /source/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_17_0045.html
indexId: bd112889f0cf5d63936005ade99206ec866c8052d564416b2162830e673ae96d72
---
# DH算法类相关接口

```
struct wcrypto_dh_msg {
__u8 alg_type:3;/* Denoted by enum wcrypto_type */
__u8 op_type:2;/* Denoted by enum wcrypto_dh_op_type */
__u8 data_fmt:1; /* Data format, denoted by enum wd_buff_type */
__u8 is_g2:2;/* g2 mode of phase 1 */
__u8 result;/* Data format, denoted by WD error code */
__u16 key_bytes;/* Key size */
__u8 *x_p; /* This is Xa and p data in order. Should be DMA buffer */
__u8 *g; /* This is PV also at phase 2. Should be DMA buffer*/
__u8 *out; /* Result address, should be DMA buffer */
__u16 xbytes; /* parameter Xa size */
__u16 pbytes; /* parameter p size */
__u16 gbytes; /* parameter g size */
__u16 out_bytes; /* output parameter size */
__u64 usr_data; /* user identifier: struct wcrypto_cb_tag */
};

enum wcrypto_dh_op_type {
WCRYPTO_DH_INVALID, /* invalid DH operation */
WCRYPTO_DH_PHASE1,/* Phase1 DH key generate */
WCRYPTO_DH_PHASE2,/* Phase2 DH key compute */
};

struct wcrypto_dh_ctx_setup {
wcrypto_cb cb; /* call back function from user */
__u16 data_fmt; /* data format denoted by enum wd_buff_type */
__u16 key_bits;/* DH key bites */
bool is_g2; /* is g2 mode or not */
struct wd_mm_br br; /* memory operations from user */
};

struct wcrypto_dh_op_data {
void *x_p; /* x and p, should be DMA buffer */
void *pv;/* it is g, but it is PV at phase 2, should be DMA buffer */
void *pri;/* phase 1&&2 output, should be DMA buffer */
__u16 pri_bytes;/* output bytes */
__u16 pbytes; /* p bytes */
__u16 xbytes; /* x bytes */
__u16 pvbytes; /* pv bytes */
enum wcrypto_dh_op_type op_type; /* operational type */
__u32 status;/* output status  */
};
```

| 函数原型 | bool wcrypto\_dh\_is\_g2(void \*ctx); |
| --- | --- |
| 函数功能 | 该函数用于判断DH context是否处于G2模式 |
| 输入说明 | ctx：DH算法context |
| 输出说明 | 无 |
| 返回值说明 | True：G2模式 False：非G2模式 |
| 使用说明 | 无 |
| 注意事项 | 无 |


| 函数原型 | int wcrypto\_dh\_key\_bits(void \*ctx); |
| --- | --- |
| 函数功能 | DH context的密钥bits宽度 |
| 输入说明 | ctx：DH算法context |
| 输出说明 | 无 |
| 返回值说明 | DH算法密钥宽度bits(768,1024,1536,2048,3072,4096) |
| 使用说明 | 无 |
| 注意事项 | 无 |


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


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


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


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


| 函数原型 | int wcrypto\_set\_dh\_g(void \*ctx, struct wd\_dtb \*g); |
| --- | --- |
| 函数功能 | 设置DH算法context的g参数 |
| 输入说明 | ctx：DH算法context g：DH g参数 |
| 输出说明 | 无 |
| 返回值说明 | 0：设置成功 非0：设置失败（错误码含义请参考WD消息错误码接口定义） |
| 使用说明 | 无 |
| 注意事项 | 无 |


| 函数原型 | void wcrypto\_get\_dh\_g(void \*ctx, struct wd\_dtb \*\*g); |
| --- | --- |
| 函数功能 | 由DH算法context获取g参数 |
| 输入说明 | ctx：DH算法context g：DH g参数地址 |
| 输出说明 | 无 |
| 返回值说明 | 无 |
| 使用说明 | 无 |
| 注意事项 | 无 |
