---
title: CIPHER算法类相关接口
description: "```"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/252RC1/accel/kunpengaccel_17_0048.html
sourcePath: /source/zh/kunpengboostkithistory/252RC1/accel/kunpengaccel_17_0048.html
indexId: 091453e32ed9c4bcfe00e699a445356435318aa4eeb00aaaf1976d0def97bd4772
---
# CIPHER算法类相关接口

```
/* Cipher message format of Warpdrive */
struct wcrypto_cipher_msg {
__u8 alg_type:4;/* Denoted by enum wcrypto_alg_type */
__u8 alg:4;/* Denoted by enum wcrypto_cipher_alg*/
__u8 op_type:4;/* Denoted by enum wcrypto_cipher_op_type */
__u8 mode:4;/* Denoted by enum wcrypto_cipher_mode */
__u8 data_fmt;/* Data format, denoted by enum wd_buff_type */
__u8 result;/* Data format, denoted by WD error code */
__u16 key_bytes;/* Key bytes */
__u16 iv_bytes;/* IV bytes */
__u32 in_bytes;/* Input data bytes */
__u32 out_bytes;/* Output data bytes */
__u8 *key;/* Input key VA pointer, should be DMA buffer */
__u8 *iv;/* Input IV VA pointer, should be DMA buffer */
__u8 *in;/* Input data VA pointer, should be DMA buffer */
__u8 *out;/* Output data VA pointer, should be DMA buffer */
__u64 usr_data;/* user identifier: for example, can be struct wcrypto_cb_tag */
};

enum wcrypto_cipher_op_type {
WCRYPTO_CIPHER_ENCRYPTION,
WCRYPTO_CIPHER_DECRYPTION,
};

enum wcrypto_cipher_alg {
WCRYPTO_CIPHER_SM4,
WCRYPTO_CIPHER_AES,
WCRYPTO_CIPHER_DES,
WCRYPTO_CIPHER_3DES,
};

enum wcrypto_cipher_mode {
WCRYPTO_CIPHER_ECB,
WCRYPTO_CIPHER_CBC,
WCRYPTO_CIPHER_CTR,
WCRYPTO_CIPHER_XTS,
};

struct wcrypto_cipher_ctx_setup {
wcrypto_cb cb;/* call back function from user */
enum wcrypto_cipher_alg alg;/* cipher algorithm type */
enum wcrypto_cipher_mode mode;/* cipher algorithm mode */
struct wd_mm_br br;/* memory operations from user */
__u16 data_fmt;/* data format denoted by enum wd_buff_type */
};
struct wcrypto_cipher_op_data {
enum wcrypto_cipher_op_type op_type;/* cipher operation type */
int status;/* cipher operation status */
void *in;/* cipher operation input address */
void *out;/* cipher operation output address *
void *iv;/* cipher operation iv address *
__u32 in_bytes;/* cipher operation input bytes */
__u32 out_bytes;/* cipher operation output bytes */
__u32 iv_bytes;/* cipher operation iv bytes */
void *priv;/* private information */
};
```


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


| 函数原型 | int wcrypto\_set\_cipher\_key(void \*ctx, \_\_u8 \*key, \_\_u16 key\_len); |
| --- | --- |
| 函数功能 | 设置cipher算法需要的key信息到cipher算法context |
| 输入说明 | ctx：对称加解密算法context key：cipher算法输入key的指针地址 key\_len：cipher算法输入key的长度 |
| 输出说明 | 无。 |
| 返回值说明 | 0：成功 否则失败 |
| 使用说明 | 无 |
| 注意事项 | 无 |


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


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


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