虽然写的是RNG加密,但个人认为他的真实叫法应该是硬件随机数生成。
源码分析
代码路径
components\drivers\hwcrypto\hw_rng.c components\drivers\hwcrypto\hw_rng.h
数据结构
struct hwcrypto_rng_ops { rt_uint32_t (*update)(struct hwcrypto_rng *ctx); /**< 返回一个随机数 */ }; struct hwcrypto_rng { struct rt_hwcrypto_ctx parent; /**< 硬件加密框架设备 */ const struct hwcrypto_rng_ops *ops; /**< 初始化时需要提供的入口 */ };
获取RNG上下文
struct rt_hwcrypto_ctx *rt_hwcrypto_rng_create(struct rt_hwcrypto_device *device) { struct rt_hwcrypto_ctx *ctx; ctx = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_RNG, sizeof(struct hwcrypto_rng)); return ctx; } rt_err_t rt_hwcrypto_rng_default(struct rt_hwcrypto_device *device) { struct rt_hwcrypto_ctx *tmp_ctx; /* 如果传入设备为空,则销毁设备, 有些看不明白这操作,直接返回错误,让上层决定如何操作不更好嘛 */ if (device == RT_NULL) { if (ctx_default) { rt_hwcrypto_rng_destroy(ctx_default); ctx_default = RT_NULL; } return RT_EOK; } /* 尝试获取默认的加密设备 */ tmp_ctx = rt_hwcrypto_rng_create(device); if (tmp_ctx == RT_NULL) { return -RT_ERROR; } /* 销毁旧的上下文,并更新成新的上下文 */ rt_hwcrypto_rng_destroy(ctx_default); ctx_default = tmp_ctx; return RT_EOK; }
销毁RNG上下文
void rt_hwcrypto_rng_destroy(struct rt_hwcrypto_ctx *ctx) { if (ctx == ctx_default) { ctx_default = RT_NULL; } rt_hwcrypto_ctx_destroy(ctx); }
获取RNG值
rt_uint32_t rt_hwcrypto_rng_update_ctx(struct rt_hwcrypto_ctx *ctx) { if (ctx) { return ((struct hwcrypto_rng *)ctx)->ops->update((struct hwcrypto_rng *)ctx); } return 0; } rt_uint32_t rt_hwcrypto_rng_update(void) { // 没有则创建 if (ctx_default == RT_NULL) { rt_hwcrypto_rng_default(rt_hwcrypto_dev_default()); } // 获取值 return rt_hwcrypto_rng_update_ctx(ctx_default); }
总结
从代码也可以看出来,这个部分,本质上就是硬件随机数生成,个人并不认为这属于加密设备,但既然这部分在RTT框架里也加到了硬件加密框架中,那就也总结一下驱动适配时的默认模板 。
#include <rtconfig.h> #if defined(RT_USING_HWCRYPTO) #include <rtdevice.h> #include <rtdbg.h> #include <board.h> #if defined(BSP_USING_RNG) rt_uint32_t rng_update(struct hwcrypto_rng *ctx) { // TODO:rng生成实现 return length; } static const struct hwcrypto_rng_ops rng_ops= { .update = rng_update, }; #endif static rt_err_t hwcrypto_create(struct rt_hwcrypto_ctx *ctx) { rt_err_t res = RT_EOK; switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK) { #if defined(BSP_USING_RNG) case HWCRYPTO_TYPE_RNG: { ctx->contex = RT_NULL; //Setup rng operation ((struct hwcrypto_rng_ops *)ctx)->ops = &rng_ops; break; } #endif /* BSP_USING_BIGNUM */ default: res = -RT_ERROR; break; } return res; } static void hwcrypto_destroy(struct rt_hwcrypto_ctx *ctx) { if (ctx->contex) rt_free(ctx->contex); } static rt_err_t hwcrypto_clone(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src) { rt_err_t res = RT_EOK; if (des->contex && src->contex) { rt_memcpy(des->contex, src->contex, sizeof(struct rt_hwcrypto_ctx)); } else return -RT_EINVAL; return res; } static void hwcrypto_reset(struct rt_hwcrypto_ctx *ctx) { switch (ctx->type & HWCRYPTO_MAIN_TYPE_MASK) { #if defined(BSP_USING_RNG) case HWCRYPTO_TYPE_RNG: { // TODO: rng reset break; } #endif /* BSP_USING_RNG */ default: res = -RT_ERROR; break; } return; } static const struct rt_hwcrypto_ops hwcrypto_ops = { .create = hwcrypto_create, .destroy = hwcrypto_destroy, .copy = hwcrypto_clone, .reset = hwcrypto_reset, }; int hwcrypto_device_init(void) { static struct rt_hwcrypto_device hwcrypto_dev; hwcrypto_dev.ops = &hwcrypto_ops; hwcrypto_dev.id = 0; hwcrypto_dev.user_data = &hwcrypto_dev; // 硬件资源初始化 // 注册加密设备 if (rt_hwcrypto_register(&hwcrypto_dev, RT_HWCRYPTO_DEFAULT_NAME) != RT_EOK) { return -1; } return 0; } INIT_DEVICE_EXPORT(hwcrypto_device_init); #endif //#if defined(RT_USING_HWCRYPTO)