概述
產(chǎn)品和生態(tài)系統(tǒng)安全性的需求比以往任何時(shí)候都更加重要。真隨機(jī)數(shù)是所有安全系統(tǒng)的核心,其質(zhì)量會(huì)影響設(shè)計(jì)的安全性。因此在沒有內(nèi)置硬件TRNG的AT32的微控制器系列中,如何提高隨機(jī)數(shù)的有效,來符合應(yīng)用的需求。底下提供兩種方法:提高亂度的方法之一,使用ADC的誤差。AT32的微控制器內(nèi)置最多三個(gè)高級(jí)12位片上SAR模擬數(shù)字轉(zhuǎn)換器(ADC)并提供自校準(zhǔn)功能,保證12位ADC靜態(tài)準(zhǔn)確度(accuracy)可達(dá)10位以上。這誤差可以拿來計(jì)算隨機(jī)數(shù)的來源。提高亂度的方法之二,使用上電時(shí)SRAM內(nèi)容的不確定性。SRAM不保證上電時(shí)的內(nèi)容值,每次上電后,內(nèi)容也是不容易預(yù)測(cè)的。我們可以利用這個(gè)特性,拿來增加隨機(jī)數(shù)的亂度。
利用ADC的誤差來源產(chǎn)生隨機(jī)數(shù)的方法
本章介紹了使用軟件觸發(fā)方式觸發(fā)ADC,配置普通信道和DMA。根據(jù)隨機(jī)數(shù)需要的位數(shù)來配置信道數(shù),一次轉(zhuǎn)換最多到16信道,將16信道轉(zhuǎn)換的數(shù)值可組合計(jì)算成一個(gè)32位的隨機(jī)數(shù)。底下是DMA和ADC的配置代碼。
DMA配置函數(shù)代碼
ADC配置函數(shù)代碼
可以看到,代碼中并沒有對(duì)ADC做自校準(zhǔn),轉(zhuǎn)換時(shí)間也使用最短的,這種情況下,ADC的準(zhǔn)確度會(huì)是最差的,有助于亂度的提升。
ADC隨機(jī)數(shù)取得代碼
利用上電時(shí)SRAM的內(nèi)容來計(jì)算隨機(jī)數(shù)的方式
這范例只是簡(jiǎn)單的利用累加來獲得一個(gè)隨機(jī)數(shù)
SRAM配置函數(shù)代碼
隨機(jī)生成應(yīng)用指南
以上兩種方式建議應(yīng)用在上電后執(zhí)行,因?yàn)镾RAM內(nèi)容在運(yùn)行后會(huì)初始化,ADC也會(huì)有其他應(yīng)用上的需求,上電后執(zhí)行并獲得一個(gè)隨機(jī)數(shù),將這個(gè)隨機(jī)數(shù)當(dāng)成Seed,之后可以利用標(biāo)準(zhǔn)C函式庫中提供的隨機(jī)數(shù)生成器,產(chǎn)生后續(xù)的隨機(jī)數(shù)。
SRAM的方式限定在POR后使用。如果只是一般的reset,SRAM會(huì)維持內(nèi)容,造成產(chǎn)生的隨機(jī)數(shù)都是相同。ADC的方式則沒有限制,但是因?yàn)槭褂肁DC外設(shè)的資源,推薦放在開機(jī)時(shí)執(zhí)行,不會(huì)影響后續(xù)的ADC應(yīng)用。
范例運(yùn)行和分析
本篇應(yīng)用筆記適用于AT32各系列MCU,只要有ADC外設(shè)皆可適用。范例固件AN0175_SourceCode_V2.0.0運(yùn)行在AT32403A AT-START版上,透過 PuTTY(免費(fèi)開源終端仿真器)等終端仿真應(yīng)用程序,將數(shù)據(jù)存儲(chǔ)在工作站上。在工作站上編譯NIST統(tǒng)計(jì)測(cè)試集程序包,以生成可執(zhí)行程序。接下來運(yùn)行NIST統(tǒng)計(jì)測(cè)試集程序分析數(shù)據(jù)以及統(tǒng)計(jì)測(cè)試。以下是使用范例固件AN0175_SourceCode_V2.0.0在上電后會(huì)產(chǎn)生的一個(gè)隨機(jī)數(shù),在收集約319萬筆隨機(jī)數(shù)后,進(jìn)行NIST統(tǒng)計(jì)測(cè)試。圖1. 環(huán)境配置
硬件資源
AT32403A AT-START 版
1) 串口(PA9)
具有串口的計(jì)算機(jī),運(yùn)行 Linux 系統(tǒng)
軟件資源
下載到AT32403A AT-START版運(yùn)行
1) AN0175_SourceCode_V2.0.0
計(jì)算機(jī)端運(yùn)行
1) 終端仿真器如PuTTY
2) 統(tǒng)計(jì)測(cè)試集源程序
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統(tǒng)計(jì)測(cè)試集
基于NIST統(tǒng)計(jì)測(cè)試集: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.統(tǒng)計(jì)測(cè)試集源程序下載:https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/documents/sts-2_1_2.zip統(tǒng)計(jì)測(cè)試集結(jié)果:
NIST SP800-90b統(tǒng)計(jì)測(cè)試集
基于NIST統(tǒng)計(jì)測(cè)試集: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.統(tǒng)計(jì)測(cè)試集源程序下載:https://github.com/usnistgov/SP800-90B_EntropyAssessment統(tǒng)計(jì)測(cè)試集結(jié)果:需先轉(zhuǎn)換成符合2-bit-wide symbols數(shù)據(jù)輸入格式。./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
+關(guān)注
關(guān)注
146文章
17317瀏覽量
352620 -
雅特力
+關(guān)注
關(guān)注
0文章
168瀏覽量
8140 -
AT32
+關(guān)注
關(guān)注
1文章
118瀏覽量
2164
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論