Edit online

接口设计

18 Jul 2024
Read time: 5 minute(s)
1. aic_skcipher_alg_init
函数原型int aic_skcipher_alg_init(struct crypto_skcipher *tfm)
功能说明对称密钥算法的初始化函数
参数定义struct crypto_skcipher *tfm 算法实例指针
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
2. aic_skcipher_alg_exit
函数原型void aic_skcipher_alg_exit(struct crypto_skcipher *tfm)
功能说明对称密钥算法使用完毕,释放相关资源
参数定义struct crypto_skcipher *tfm 算法实例指针
返回值
注意事项-
3. aic_skcipher_alg_setkey
函数原型int aic_skcipher_alg_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
功能说明设置对称密钥算法的密钥
参数定义
  • struct crypto_skcipher *tfm 算法实例指针
  • const u8 *key 密钥的指针
  • keylen 密钥的长度
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
4. aic_skcipher_crypt
函数原型int aic_skcipher_crypt(struct skcipher_request *req, unsigned long flg)
功能说明发起一个对称密钥算法的加解密处理的请求
参数定义
  • struct skcipher_request *req 加解密请求的指针
  • flg 算法标记,用来标识算法类型和算法模式
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
5. aic_akcipher_rsa_alg_init
函数原型int aic_akcipher_rsa_alg_init(struct crypto_akcipher *tfm)
功能说明RSA 算法初始化
参数定义struct crypto_akcipher *tfm 算法实例的指针
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
6. aic_akcipher_rsa_alg_exit
函数原型void aic_akcipher_rsa_alg_exit(struct crypto_akcipher *tfm)
功能说明RSA 算法使用完毕,释放相关资源
参数定义struct crypto_akcipher *tfm 算法实例的指针
返回值
注意事项-
7. aic_akcipher_rsa_set_pub_key
函数原型int aic_akcipher_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,unsigned int keylen)
功能说明设置 RSA 算法的公钥
参数定义
  • struct crypto_akcipher *tfm 算法实例指针
  • const void *key 密钥数据的指针
  • keylen 密钥的长度
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
8. aic_akcipher_rsa_set_priv_key
函数原型int aic_akcipher_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,unsigned int keylen)
功能说明设置 RSA 算法的私钥
参数定义
  • struct crypto_akcipher *tfm 算法实例指针
  • const void *key 密钥数据的指针
  • keylen 密钥的长度
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
9. aic_akcipher_rsa_max_size
函数原型int aic_akcipher_rsa_max_size(struct crypto_akcipher *tfm)
功能说明获取当前 RSA 算法的密钥长度
参数定义struct crypto_akcipher *tfm 算法实例指针
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
10. aic_akcipher_rsa_crypt
函数原型int aic_akcipher_rsa_crypt(struct akcipher_request *req, unsigned long flag)
功能说明发起一个 RSA 算法的加解密请求
参数定义struct akcipher_request *req 请求的指针 flag 算法标记
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
11. aic_hash_alg_init
函数原型int aic_hash_alg_init(struct crypto_tfm *tfm)
功能说明HASH 算法的初始化
参数定义struct crypto_tfm *tfm 算法实例的指针
返回值
  • 0: 成功
  • 其他: 失败
注意事项-
12. aic_hash_alg_exit
函数原型void aic_hash_alg_exit(struct crypto_tfm *tfm)
功能说明HASH 算法使用完毕,释放相关资源
参数定义struct crypto_tfm *tfm 算法实例的指针
返回值
注意事项-
13. aic_hash_init
函数原型int aic_hash_init(struct ahash_request *req)
功能说明初始化一个 HASH 运算的操作
参数定义struct ahash_request *req

HASH 运算的请求,具体所使用的算法,根据消息摘要的大小决定

返回值
  • 0: 成功
  • 其他: 失败
注意事项-
14. aic_hash_update
函数原型int aic_hash_update(struct ahash_request *req)
功能说明更新一笔 HASH 运算的数据,用于数据分多次输入的场景
参数定义struct ahash_request *req

HASH 运算的请求,数据信息保存在该结构体中

返回值
  • 0: 成功
  • 其他: 失败
注意事项-
15. aic_hash_final
函数原型int aic_hash_final(struct ahash_request *req)
功能说明HASH 运算的数据输入结束,获取结果
参数定义struct ahash_request *req

HASH 运算的请求,本请求不带数据,只获取结果

返回值
  • 0: 成功
  • 其他: 失败
注意事项-
16. aic_hash_finup
函数原型int aic_hash_finup(struct ahash_request *req)
功能说明HASH 运算的最后一笔数据,并且获取结果
参数定义struct ahash_request *req

HASH 运算的请求,本请求带最后一笔数据,并获取结果

返回值
  • 0: 成功
  • 其他: 失败
注意事项-
17. aic_hash_digest
函数原型int aic_hash_digest(struct ahash_request *req)
功能说明针对一笔数据,完成 init 和 finup 的 HASH 运算操作,并获取结果
参数定义struct ahash_request *req

HASH 运算的请求,本请求带最后一笔数据,并获取结果

返回值
  • 0: 成功
  • 其他: 失败
注意事项-

Driver 层接口设计

18. drv_aes_init
函数原型rt_err_t drv_aes_init(struct rt_hwcrypto_ctx *ctx)
功能说明AES 算法初始化
参数定义struct rt_hwcrypto_ctx *ctx - 上下文实例指针
返回值0,初始化成功。其它,初始化失败
注意事项-
19. aes_ecb_crypto
函数原型s32 aes_ecb_crypto(u8 *key, u8 keylen, u8 dir, u8 *in, u8 *out, u32 len)
功能说明AES ECB 加解密实现
参数定义
  • u8 *key - 密钥的指针
  • u8 keylen - 密钥的长度
  • u8 dir - 运算方向
    • 0,加密
    • 1,解密
  • u8 *in - 输入数据
  • u8 *out - 输出数据
  • u32 len - 数据长度
返回值0,加解密成功。其它,加解密失败。
注意事项-
20. aes_cbc_crypto
函数原型s32 aes_cbc_crypto(u8 *key, u8 keylen, u8 dir, u8 *iv, u8 *in, u8 *out, u32 len)
功能说明AES CBC 加解密实现
参数定义
  • u8 *key - 密钥的指针
  • u8 keylen - 密钥的长度
  • u8 dir - 运算方向
    • 0,加密
    • 1,解密
  • u8 *iv - 输入 iv
  • u8 *in - 输入数据
  • u8 *out - 输出数据
  • u32 len - 数据长度
返回值0,加解密成功。其它,加解密失败。
注意事项-
21. drv_sha_init
函数原型rt_err_t drv_sha_init(struct rt_hwcrypto_ctx *ctx)
功能说明SHA 算法初始化
参数定义struct rt_hwcrypto_ctx *ctx - 上下文实例指针
返回值0,初始化成功。其它,初始化失败
注意事项-
22. drv_sha_start
函数原型rt_err_t drv_sha_start(struct rt_hwcrypto_ctx *ctx)
功能说明SHA 算法启动
参数定义struct rt_hwcrypto_ctx *ctx - 上下文实例指针
返回值0,初始化成功。其它,初始化失败
注意事项-
23. drv_sha_update
函数原型rt_err_t drv_sha_update(aic_sha_context_t *context, const void *input, uint32_t size)
功能说明更新一笔 SHA 算法数据
参数定义
  • struct rt_hwcrypto_ctx *ctx - 上下文实例指针
  • void *input - 输入数据
  • uint32_t size - 输入数据长度
返回值0,运算成功。其它,运算失败
注意事项-
24. drv_sha_finish
函数原型rt_err_t drv_sha_finish(struct hwcrypto_hash *ctx, void *output, rt_size_t *out_size)
功能说明SHA 算法运算结束,获取结果
参数定义
  • struct hwcrypto_hash *ctx - 上下文 HASH 实例指针
  • void *output - 输出数据
  • rt_size_t *out_size - 输出数据长度
返回值0,获取结果成功。其它,获取结果失败
注意事项-

HAL 层接口设计

HAL 层的函数接口声明存放在 hal_ce.h 中,主要接口有:
s32 hal_crypto_init(void);
s32 hal_crypto_deinit(void);
void hal_crypto_irq_handler();
s32 hal_crypto_start_symm(struct crypto_task *task);
s32 hal_crypto_start_asym(struct crypto_task *task);
s32 hal_crypto_start_hash(struct crypto_task *task);
u32 hal_crypto_poll_finish(u32 alg_unit);
void hal_crypto_pending_clear(u32 alg_unit);
u32 hal_crypto_get_err(u32 alg_unit);
s32 hal_crypto_bignum_byteswap(u8 *bn, u32 len);
s32 hal_crypto_bignum_le2be(u8 *src, u32 slen, u8 *dst, u32 dlen);
s32 hal_crypto_bignum_be2le(u8 *src, u32 slen, u8 *dst, u32 dlen);