打印

全志R128应用开发案例——获取真随机数

[复制链接]
560|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
神棍地海棠|  楼主 | 2023-11-13 16:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[color=rgba(0, 0, 0, 0.87)]
本文案例代码
下载地址

获取真随机数案例代码https://www.aw-ol.com/downloads?cat=24

[color=rgba(0, 0, 0, 0.87)]R128 内置了TRNG,一个真随机数发生器,随机源是 8 路独立的环形振荡器,由模拟器件电源噪声产生频率抖动,用低频始终重采样,然后进行弹性抽取和熵提取处理,最终输出128bit真随机数。
载入方案
[color=rgba(0, 0, 0, 0.87)]我们使用的开发板是 R128-Devkit,需要开发 C906 核心的应用程序,所以载入方案选择r128s2_module_c906
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">$ source envsetup.sh $ lunch_rtos 1
[color=rgba(0, 0, 0, 0.87)][color=var(--md-typeset-a-color)]
设置 TRNG驱动
[color=rgba(0, 0, 0, 0.87)]运行 mrtos_menuconfig 进入配置页面。前往下列地址找到 TRNG Devices
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">Drivers Options  --->    soc related device drivers  --->            TRNG Devices --->            -*- enable trng driver
[color=rgba(0, 0, 0, 0.87)][color=var(--md-typeset-a-color)]
编写程序
[color=rgba(0, 0, 0, 0.87)]打开你喜欢的编辑器,修改文件:lichee/rtos/projects/r128s2/module_c906/src/main.c
[color=rgba(0, 0, 0, 0.87)][color=var(--md-typeset-a-color)]
引入头文件 code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">#include <sunxi_hal_trng.h>
[color=rgba(0, 0, 0, 0.87)][color=var(--md-typeset-a-color)]
初始化 TRNG 读取数据模块
[color=rgba(0, 0, 0, 0.87)][color=var(--md-typeset-a-color)]
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">uint32_t random[4 = {0};HAL_TRNG_Extract(0, random); // 读取 CRC 模式printf("trng CRC result: 0x%08x 0x%08x 0x%08x 0x%08x\n", random[0, random[1, random[2, random[3);HAL_TRNG_Extract(1, random); // 读取 XOR 模式printf("trng XOR result: 0x%08x 0x%08x 0x%08x 0x%08x\n", random[0, random[1, random[2, random[3);结果
[color=rgba(0, 0, 0, 0.87)]编译固件后烧录,可以看到随机数输出。
[color=rgba(0, 0, 0, 0.87)][color=var(--md-accent-fg-color)]

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

201

主题

209

帖子

0

粉丝