概述
產品和生態系統安全性的需求比以往任何時候都更加重要。真隨機數是所有安全系統的核心,其質量會影響設計的安全性。因此在沒有內置硬件TRNG的AT32的微控制器系列中,如何提高隨機數的有效,來符合應用的需求。底下提供兩種方法:提高亂度的方法之一,使用ADC的誤差。AT32的微控制器內置最多三個高級12位片上SAR模擬數字轉換器(ADC)并提供自校準功能,保證12位ADC靜態準確度(accuracy)可達10位以上。這誤差可以拿來計算隨機數的來源。提高亂度的方法之二,使用上電時SRAM內容的不確定性。SRAM不保證上電時的內容值,每次上電后,內容也是不容易預測的。我們可以利用這個特性,拿來增加隨機數的亂度。
利用ADC的誤差來源產生隨機數的方法
本章介紹了使用軟件觸發方式觸發ADC,配置普通信道和DMA。根據隨機數需要的位數來配置信道數,一次轉換最多到16信道,將16信道轉換的數值可組合計算成一個32位的隨機數。底下是DMA和ADC的配置代碼。
DMA配置函數代碼
ADC配置函數代碼
可以看到,代碼中并沒有對ADC做自校準,轉換時間也使用最短的,這種情況下,ADC的準確度會是最差的,有助于亂度的提升。
ADC隨機數取得代碼
利用上電時SRAM的內容來計算隨機數的方式
這范例只是簡單的利用累加來獲得一個隨機數
SRAM配置函數代碼
隨機生成應用指南
以上兩種方式建議應用在上電后執行,因為SRAM內容在運行后會初始化,ADC也會有其他應用上的需求,上電后執行并獲得一個隨機數,將這個隨機數當成Seed,之后可以利用標準C函式庫中提供的隨機數生成器,產生后續的隨機數。
SRAM的方式限定在POR后使用。如果只是一般的reset,SRAM會維持內容,造成產生的隨機數都是相同。ADC的方式則沒有限制,但是因為使用ADC外設的資源,推薦放在開機時執行,不會影響后續的ADC應用。
范例運行和分析
本篇應用筆記適用于AT32各系列MCU,只要有ADC外設皆可適用。范例固件AN0175_SourceCode_V2.0.0運行在AT32403A AT-START版上,透過 PuTTY(免費開源終端仿真器)等終端仿真應用程序,將數據存儲在工作站上。在工作站上編譯NIST統計測試集程序包,以生成可執行程序。接下來運行NIST統計測試集程序分析數據以及統計測試。以下是使用范例固件AN0175_SourceCode_V2.0.0在上電后會產生的一個隨機數,在收集約319萬筆隨機數后,進行NIST統計測試。圖1. 環境配置
硬件資源
AT32403A AT-START 版
1) 串口(PA9)
具有串口的計算機,運行 Linux 系統
軟件資源
下載到AT32403A AT-START版運行
1) AN0175_SourceCode_V2.0.0
計算機端運行
1) 終端仿真器如PuTTY
2) 統計測試集源程序
https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/documents/sts-2_1_2.zip
https://github.com/usnistgov/SP800-90B_EntropyAssessment
NIST SP800-22b統計測試集
基于NIST統計測試集:April 27, 2010: NIST SP 800-22rev1a (dated April 2010), A Statistical Test Suite for the Validation of Random Number Generators and Pseudo Random Number Generators for Cryptographic Applications, that describes the test suite.統計測試集源程序下載:https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/documents/sts-2_1_2.zip統計測試集結果:
NIST SP800-90b統計測試集
基于NIST統計測試集:November 21, 2014: NIST requests comments on the latest revision of NIST SP 800-90A, Recommendation for Random Number Generation Using Deterministic Random Bit Generators, which is datedNovember 2014.統計測試集源程序下載:https://github.com/usnistgov/SP800-90B_EntropyAssessment統計測試集結果:需先轉換成符合2-bit-wide symbols數據輸入格式。./ea_non_iid 0421_2.bin 2 -i -a -vOpening file: '0421_2.bin'Loaded 50888144 samples of 4 distinct 2-bit-wide symbolsNumber of Binary Symbols: 101776288Running non-IID tests...Running Most Common Value Estimate...Bitstring MCV Estimate: mode = 50891714, p-hat = 0.50003507693265448, p_u = 0.50016273956095891Most Common Value Estimate (bit string) = 0.999531 / 1 bit(s)Literal MCV Estimate: mode = 12725005, p-hat = 0.25005834364876817, p_u = 0.25021470996034195Most Common Value Estimate = 1.998761 / 2 bit(s)Running Entropic Statistic Estimates (bit strings only)...Bitstring Collision Estimate: X-bar = 2.5000060058338387, sigma-hat = 0.50000000610486417, p = 0.50989562404154842Collision Test Estimate (bit string) = 0.971726 / 1 bit(s)Bitstring Markov Estimate: P_0 = 0.49996492306734552, P_1 = 0.50003507693265448, P_0,0 = 0.4999425562646943, P_0,1 = 0.5000574437353057, P_1,0 = 0.49998729655651403, P_1,1 = 0.50001270344348603, p_max = 2.9554800761609014e-39Markov Test Estimate (bit string) = 0.999936 / 1 bit(s)Bitstring Compression Estimate: X-bar = 5.2176714331187366, sigma-hat = 1.0152961906603262, p = 0.019654761320726077Compression Test Estimate (bit string) = 0.944830 / 1 bit(s)Running Tuple Estimates...Bitstring t-Tuple Estimate: t = 23, p-hat_max = 0.52357011476148263, p_u = 0.52369763546518522Bitstring LRS Estimate: u = 24, v = 50, p-hat = 0.50053161737274598, p_u = 0.50065927992920534T-Tuple Test Estimate (bit string) = 0.933194 / 1 bit(s)Literal t-Tuple Estimate: t = 11, p-hat_max = 0.27527598152543398, p_u = 0.27543726106146299Literal LRS Estimate: u = 12, v = 24, p-hat = 0.25086994374062016, p_u = 0.25102647882990431T-Tuple Test Estimate = 1.860204 / 2 bit(s)LRS Test Estimate (bit string) = 0.998099 / 1 bit(s)LRS Test Estimate = 1.994089 / 2 bit(s)Running Predictor Estimates...Bitstring MultiMCW Prediction Estimate: N = 101776225, Pglobal' = 0.50008960368099831 (C = 50884239) Plocal can't affect result (r = 26)Multi Most Common in Window (MultiMCW) Prediction Test Estimate (bit string) = 0.999741 / 1 bit(s)Literal MultiMCW Prediction Estimate: N = 50888081, Pglobal' = 0.25014573559900838 (C = 12721480) Plocal can't affect result (r = 12)Multi Most Common in Window (MultiMCW) Prediction Test Estimate = 1.999159 / 2 bit(s)Bitstring Lag Prediction Estimate: N = 101776287, Pglobal' = 0.50019269251081444 (C = 50894762) Plocal can't affect result (r = 25)Lag Prediction Test Estimate (bit string) = 0.999444 / 1 bit(s)Literal Lag Prediction Estimate: N = 50888143, Pglobal' = 0.25015172047634626 (C = 12721800) Plocal can't affect result (r = 13)Lag Prediction Test Estimate = 1.999125 / 2 bit(s)Bitstring MultiMMC Prediction Estimate: N = 101776286, Pglobal' = 0.50008456811129076 (C = 50883757) Plocal can't affect result (r = 27)Multi Markov Model with Counting (MultiMMC) Prediction Test Estimate (bit string) = 0.999756 / 1 bit(s)Literal MultiMMC Prediction Estimate: N = 50888142, Pglobal' = 0.2502104743048289 (C = 12724789) Plocal can't affect result (r = 13)Multi Markov Model with Counting (MultiMMC) Prediction Test Estimate = 1.998786 / 2 bit(s)Bitstring LZ78Y Prediction Estimate: N = 101776271, Pglobal' = 0.50008006313488451 (C = 50883291) Plocal can't affect result (r = 26)LZ78Y Prediction Test Estimate (bit string) = 0.999769 / 1 bit(s)Literal LZ78Y Prediction Estimate: N = 50888127, Pglobal' = 0.25021764352136133 (C = 12725150) Plocal can't affect result (r = 13)LZ78Y Prediction Test Estimate = 1.998745 / 2 bit(s)H_original: 1.860204H_bitstring: 0.933194min(H_original, 2 X H_bitstring): 1.860204
-
mcu
+關注
關注
146文章
17824瀏覽量
360182 -
雅特力
+關注
關注
0文章
178瀏覽量
8443 -
AT32
+關注
關注
1文章
121瀏覽量
2566
發布評論請先 登錄
如何使用MSP430內部時鐘生成隨機數
神經網絡的偽隨機數生成方法
如何利用SystemVerilog仿真生成隨機數

技術分享 | 隨機數生成過慢導致系統阻塞怎么辦?

評論