---
title: 创建Context并设定Cipher key
description: "创建Cipher context的时候需要提供内存及相关管理函数填充setup.ops，可以提供第三方内存，也可以使用WD提供的BLK内存池接口创建。此处以wd_queue申请的test_mempool为例，介绍Cipher context的创建过程。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_17_0075.html
sourcePath: /source/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_17_0075.html
indexId: 2ee2617e80c4d46f290899bb7bf54ecf18df8bf4bce4039be5f9404f8a97b85072
---
# 创建Context并设定Cipher key

创建Cipher context的时候需要提供内存及相关管理函数填充setup.ops，可以提供第三方内存，也可以使用WD提供的BLK内存池接口创建。此处以wd_queue申请的test_mempool为例，介绍Cipher context的创建过程。

```
struct wcrypto_cipher_ctx_setup setup;
void *ctx = NULL, *pool = NULL;
int block_size = TEST_BLOCK_SIZE;
int block_num = TEST_BLOCK_NUM;

pool = test_mempool_create(&q, block_size, block_num);
memset((void *)&setup, 0, sizeof(setup));
setup.ops.alloc = test_alloc_buf;
setup.ops.free = test_free_buf;
setup.ops.dma_map = test_dma_map;
setup.ops.dma_unmap = test_dma_unmap;
setup.ops.usr = pool;
setup.alg = WCRYPTO_CIPHER_SM4;
setup.mode = WCRYPTO_CIPHER_CBC;
ctx = wcrypto_create_cipher_ctx(&q, &setup);
wcrypto_set_cipher_key(ctx, key, key_len);
```
