RdRand

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書

RDRAND(之前被稱為Bull Mountain[1])是一個計算機指令,用於從芯片上的硬件隨機數生成器中獲取隨機數。所用到的隨機數生成器由芯片上的池初始化。[2] RDRAND指令在Ivy Bridge架構處理器上可用[a],該指令也是X86-64IA-32指令集的一部分。AMD在2015年6月添加了對RdRand指令的支持。[4]

該隨機數生成器要遵守安全標準和加密標準,比如NIST SP 800-90A英語NIST SP 800-90A[5] FIPS 140-2英語FIPS 140-2ANSI X9.82[2] Intel也在1999年和2012年請密碼學研究 Cryptography Research 公司來審查這個隨機數發生器,並產生了兩篇論文:1999年的 The Intel Random Number Generator[6] 和2012年的 Analysis of Intel's Ivy Bridge Digital Random Number Generator[7]

RDSEEDRDRAND類似,也提供了訪問硬件熵池的高級方法。 Intel Broadwell 系列的CPU[8]AMD Zen 系列的CPU[9]都支持RDSEED生成器和rdseed指令。

概略[編輯]

AMDIntel的CPU上,CPUID指令都可以檢測中央處理器(CPU)是否支持RDRAND 指令。如果支持,調用CPUID的標準函數01H之後,ECX寄存器的第30位會被設置成1[10]。AMD處理器也可以使用同樣的方式檢測是否支持[11]。在Intel CPU上,也可以使用類似的方法檢測RDSEED是否支持。如果支持RDSEED,在調用完CPUID的標準函數07H後,EBX寄存器的第18位會被設置為1[12]

RDRAND的操作碼是0x0F 0xC7,後面跟一個ModRM字節,來指示目標寄存器。在64位模式下,還可以於REX前綴結合(這是可選的)[13]

Intel安全密鑰IntelRDRAND指令和底層的隨機數生成器(RNG)的硬件實現的統稱,它在開發期間的代號是"Bull Mountain"[14]。Intel稱自己的RNG"數字隨機數生成器"或DRNG。生成器採用有硬件產生的256位原始熵樣本對,並將其應用到一個高級加密標準(AES)(在CBC-MAC模式下)調節器,將其減少到256位條件熵樣本。

NIST SP 800-90A中定義了一個名叫CTR_DRBG的確定性隨機數生成器。它由調節器的輸出來初始化,為使用RDRAND指令的應用程序提供了密碼學安全的隨機數。[2][14] 在重新初始化之前,硬件將發出最多511個128位的樣本。使用RDSEED可以訪問來自AES-CBC-MAC的、條件化後的256位樣本。


為了初始化另一個為隨機數生成器,RDSEED指令被添加到了Intel安全密鑰[15],在Broadwell微架構的CPU上開始支持。RDSEED指令的熵來源自時序線路,並且使用硅片上的熱噪聲來以3GHz的速度輸出隨機比特流[16]。這比從RDRAND獲得的6.4Gbit/s速率要慢(這兩個速率都是所有核心、所有線程共享)[17]RDSEED用來初始化任意寬度的軟件 PRNG,而RDRAND指令適用於需要高質量隨機數的應用程序。如果不要求密碼學安全,軟件隨機數生成器比如Xorshift一般會比較快。[18]

性能[編輯]

在Intel 酷睿 i7-7700K,4500MHz(45 x 100MHz)的處理器(Kaby Lake-S架構)上,單個RDRANDRDSEED指令花費110納秒或463個時鐘周期,不論操作數大小(16位、32位、64位)。這個時鐘周期數適用於所有SkylakeKaby Lake架構的處理器。在Silvermont架構的處理器上,每個指令花費1472時鐘周期,不論操作數大小;在Ivy Bridge架構的處理器上,花費117時鐘周期[19]

在AMD Ryzen 處理器上,對於16位或32位操作數,每個指令約花費1200個時鐘周期;對於64位操作數,約花費2500個時鐘周期。

編譯器支持[編輯]

GCC 4.6+和Clang 3.2+提供了RdRand的內置支持英語intrinsic function——當在編譯參數中指定了-mrdrnd命令行參數、並且在條件編譯英語conditional compilation時設置__RDRND__巨集的情況下[20]。更新的版本額外提供了immintrin.h將這些內置函數封裝成與英特爾C編譯器版本12.1+兼容的功能中。這些函數將隨機數據寫入參數指定的位置,並在成功時返回1 [21]

用來檢測RDRAND指令的x86匯編語言例子[編輯]

; 使用 NASM 语法

section .data
	msg db "0x00000000",10

section .text
global _start
_start:
	mov eax,1
	cpuid
	bt ecx,30
	mov rdi,1 ; exit code: failure
	jnc .exit

	; 如果没有随机数可用,rdrand 设置 CF=0
        ; Intel 的文档建议循环重试10次
	mov ecx,11
.loop1:
	dec ecx
	jecxz .exit ; exit code 已经设置了
	rdrand eax
	jnc .loop1

	; 将数字转换成 ASCII 字符
	mov rdi,msg+9
	mov ecx,8
.loop2:
	mov edx,eax
	and edx,0Fh
	; add 7 to nibbles of 0xA and above
	; to align with ASCII code for 'A'
	; ('A' - '0') - 10 = 7
	mov r8d,7
	xor r9d,r9d
	cmp dl,9
	cmova r9,r8
	add edx,r9d
	add [rdi],dl
	shr eax,4
	dec rdi
	loop .loop2

	mov rax,1 ; SYS_WRITE
	mov rdi,1 ; stdout
	mov rsi,msg
	mov rdx,11
	syscall

	mov rdi,0 ; exit code: success
.exit:
	mov rax,60 ; SYS_EXIT
	syscall

Reception[編輯]

在2013年9月,曹子德(Theodore Ts'o)為回應紐約時報的文章Global surveillance disclosures (2013–present)英語Global surveillance disclosures (2013–present),公開發文表達對Linux內核/dev/random中使用RdRand的擔憂[22]

I am so glad I resisted pressure from Intel engineers to let /dev/random rely only on the RDRAND instruction. To quote from the article below: 'By this year, the Sigint Enabling Project had found ways inside some of the encryption chips that scramble information for businesses and governments, either by working with chipmakers to insert back doors....' Relying solely on the hardware random number generator which is using an implementation sealed inside a chip which is impossible to audit is a BAD idea.

林納斯·托瓦茲駁斥了在Linux內核中使用RdRand的擔憂,並指出RdRand不是/dev/random的唯一熵來源;從RdRand接受數據並和其他隨機數來源結合來改善熵。[23][24] 然而,Defuse Security的Taylor Hornby表明,如果將後門引入到專門針對使用代碼的RdRand指令中,Linux隨機數生成器可能會變得不安全。 泰勒的概念驗證實現在版本3.13之前的未修改的Linux內核上工作。[25][26][27]

開發者注釋掉了FreeBSD內核中直接使用RdRand威盛電子的代碼,並添加說明"對於 FreeBSD 10,我們將回溯並刪除RDRAND和Padlock後端,並將它們提供給Yarrow,而不是將其輸出直接傳遞到 /dev/random 。如果需要,還可以通過內聯匯編或使用OpenSSL直接訪問硬件隨機數生成器,即RDRAND,Padlock等,但是我們不能再信任他們。"[23][28]

參見[編輯]

註腳[編輯]

  1. ^ 在一些版本的Ivy Bridge結構處理器上,由於一個bug,RdRand指令會導致一個非法指令異常[3]

參考資料[編輯]

  1. ^ Hofemeier, Gael. Find out about Intel's new RdRand Instruction.. Intel Developer Zone Blogs. 2011-06-22 [December 2013]. (原始內容存檔於2017-09-26). 
  2. ^ 2.0 2.1 2.2 Intel Digital Random Number Generator (DRNG): Software Implementation Guide, Revision 1.1 (PDF). 英特爾. 2012-08-07 [2012-11-25]. (原始內容存檔 (PDF)於2013-05-18). 
  3. ^ Desktop 3rd Generation Intel Core Processor Family, Specification Update (PDF). Intel Corporation. January 2013 [2017-09-26]. (原始內容存檔 (PDF)於2018-01-01). 
  4. ^ AMD64 Architecture Programmer’s Manual Volume 3: General-Purpose and System Instructions (PDF). AMD Developer Guides, Manuals & ISA Documents. June 2015 [16 October 2015]. (原始內容存檔 (PDF)於2017-12-22). 
  5. ^ Barker, Elaine; Kelsey, John. Recommendation for Random Number Generation Using Deterministic Random Bit Generators (PDF). 國家標準技術研究所. January 2012 [September 16, 2013]. (原始內容存檔 (PDF)於2013-10-09). 
  6. ^ Jun, Benjamin; Kocher, Paul. The Intel Random Number Generator (PDF). Cryptography Research, Inc. 1999-04-22 [2015-08-21]. (原始內容 (PDF)存檔於2015-02-13). 
  7. ^ Hamburg, Mike; Kocher, Paul; Marson, Mark. Analysis of Intel's Ivy Bridge Digital Random Number Generator (PDF). Cryptography Research, Inc. 2012-03-12 [2015-08-21]. (原始內容 (PDF)存檔於2014-12-30). 
  8. ^ Hofemeier, Gael. Introduction to Intel AES-NI and Intel SecureKey Instructions. Intel Developer Zone. Intel. 2012-07-26 [2015-10-24]. (原始內容存檔於2015-11-04). 
  9. ^ AMD Starts Linux Enablement On Next-Gen "Zen" Architecture - Phoronix. www.phoronix.com. [2015-10-25]. (原始內容存檔於2017-03-08). 
  10. ^ Volume 1, Section 7.3.17, 'Random Number Generator Instruction' (PDF). Intel® 64 and IA-32 Architectures Software Developer’s Manual Combined Volumes: 1, 2A, 2B, 2C, 3A, 3B and 3C. Intel Corporation: 177. June 2013 [24 June 2013]. (原始內容存檔 (PDF)於2013-11-04). All Intel processors that support the RDRAND instruction indicate the availability of the RDRAND instruction via reporting CPUID.01H:ECX.RDRAND[bit 30] = 1 
  11. ^ AMD64 Architecture Programmer’s Manual Volume 3: General-Purpose and System Instructions (PDF). AMD: 278. June 2015 [15 October 2015]. (原始內容存檔 (PDF)於2017-12-22). Support for the RDRAND instruction is optional. On processors that support the instruction, CPUID Fn0000_0001_ECX[RDRAND] = 1 
  12. ^ Volume 1, Section 7.3.17, 'Random Number Generator Instruction' (PDF). Intel® 64 and IA-32 Architectures Software Developer’s Manual Combined Volumes: 1, 2A, 2B, 2C, 3A, 3B and 3C. Intel Corporation: 177. June 2013 [25 October 2015]. (原始內容存檔 (PDF)於2016-08-10). All Intel processors that support the RDSEED instruction indicate the availability of the RDSEED instruction via reporting CPUID.(EAX=07H, ECX=0H):EBX.RDSEED[bit 18] = 1 
  13. ^ Intel® Digital Random Number Generator (DRNG) Software Implementation Guide | Intel® Developer Zone. Software.intel.com. [2014-01-30]. (原始內容存檔於2014-01-12). 
  14. ^ 14.0 14.1 Taylor, Greg; Cox, George. Behind Intel's New Random-Number Generator. IEEE Spectrum. September 2011 [2017-10-02]. (原始內容存檔於2019-07-01). 
  15. ^ John Mechalas. The Difference Between RDRAND and RDSEED. software.intel.com. Intel Corporation. November 2012 [1 January 2014]. (原始內容存檔於2017-10-02). 
  16. ^ Mechalas, John. Intel Digital Random Number Generator (DRNG) Software Implementation Guide, Section 3.2.1 Entropy Source (ES). https://software.intel.com. Intel. [18 February 2015]. (原始內容存檔於2017-10-02).  外部連結存在於|website= (幫助)
  17. ^ https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide頁面存檔備份,存於網際網路檔案館) 說是 800 megabyte,即 6.4 gigabit每秒
  18. ^ 最簡單的64位Xorshift實現具有三次異或和三次邏輯移位;如果這在4核2GHz CPU上運行,吞吐量是80 Gb/s。在實踐中,由於存儲、加載等開銷,吞吐量會減少,但仍然會超過RDRAND的6.4 Gb/s。另一方面,RDRAND產生的隨機數質量會比軟件隨機數生成器高,比如Xorshift。
  19. ^ 存档副本 (PDF). [2017-10-02]. (原始內容 (PDF)存檔於2014-07-30). 
  20. ^ 存档副本. [2017-10-02]. (原始內容存檔於2018-05-20). 
  21. ^ 存档副本. [2017-10-02]. (原始內容存檔於2017-10-02). 
  22. ^ Ts'o, Theodore. I am so glad I resisted pressure from Intel engineers to let /dev/random rely.... September 6, 2013 [2017-10-02]. (原始內容存檔於2018-06-11). 
  23. ^ 23.0 23.1 Richard Chirgwin. FreeBSD abandoning hardware randomness. The Register. 2013-12-09 [2017-10-02]. (原始內容存檔於2017-10-02). 
  24. ^ Gavin Clarke. Torvalds shoots down call to yank 'backdoored' Intel RdRand in Linux crypto. theregister.co.uk. 10 September 2013 [12 March 2014]. (原始內容存檔於2019-11-09). 
  25. ^ Taylor Hornby. RDRAND backdoor proof of concept is working! Stock kernel (3.8.13), only the RDRAND instruction is modified.. 6 December 2013 [9 April 2015]. (原始內容存檔於2016-03-05). 
  26. ^ Taylor Hornby [@DefuseSec]. I wrote a short dialogue explaining why Linux's use of RDRAND is problematic. http://pastebin.com/A07q3nL3 /cc @kaepora @voodooKobra (推文). 10 September 2013 [11 January 2016] –透過Twitter. 
  27. ^ Daniel J. Bernstein; Tanja Lange. Randomness generation (PDF). 16 May 2014 [9 April 2015]. (原始內容存檔 (PDF)於2017-08-25). 
  28. ^ FreeBSD Quarterly Status Report. Freebsd.org. [2014-01-30]. (原始內容存檔於2014-01-22).