跳转到内容

SHA家族:修订间差异

维基百科,自由的百科全书
删除的内容 添加的内容
无编辑摘要
InternetArchiveBot留言 | 贡献
补救12个来源,并将0个来源标记为失效。) #IABot (v2.0.8
 
(未显示19个用户的40个中间版本)
第1行: 第1行:
{{noteTA
{{noteTA
|G1 = IT
|G1 = IT
|1 = zh-hans:散列;zh-hant:雜湊;
}}
}}
'''安全散列演算法'''({{lang-en|Secure Hash Algorithm}},縮寫為SHA)是一個[[密碼雜湊函數]]家族,是[[联邦资料处理标准|FIPS]]所認證的安全[[雜湊函數|雜湊演算法]]。能計算出一個數位訊息所對應到的,長度固定的字串(又稱訊息摘要)的算法。且若輸入的訊息不同,它們對應到不同字串的機率很高。
{{no footnotes|time=2014-08-12T07:45:53+00:00}}
{{refimprove|time=2014-08-12T07:45:53+00:00}}


SHA家族的演算法,由[[美國國家安全局]](NSA)所設計,並由[[美國國家標準與技術研究院]](NIST)發佈,是美國的政府標準,其分別是:
'''安全散列演算法'''({{lang-en|'''Secure Hash Algorithm'''}})是一种能計算出一個數位訊息所對應到的,長度固定的字串(又稱訊息摘要)的算法。且若輸入的訊息不同,它們對應到不同字串的機率很高;而SHA是[[联邦资料处理标准|FIPS]]所認證的五種安全[[雜湊函數|雜湊演算法]]。這些演算法之所以稱作「安全」是基於以下兩點(根據官方標準的描述):
*'''[[SHA-0]]''':1993年发布,当时称做安全散列标准(Secure Hash Standard),发布之后很快就被NSA撤回,是SHA-1的前身。
# 由訊息摘要反推原輸入訊息,從計算理論上來說是很困難的。
# 想要找到兩組不同的訊息對應到相同的訊息摘要,從計算理論上來說也是很困難的。任何對輸入訊息的變動,都有很高的機率導致其產生的訊息摘要迥異。


*'''[[SHA-1]]''':1995年发布,SHA-1在許多安全協定中廣為使用,包括[[TLS]]、[[GnuPG]]、[[Secure Shell|SSH]]、[[S/MIME]]和[[IPsec]],是[[MD5]]的後繼者。但SHA-1的安全性在2010年以后已经不被大多数的加密场景所接受。2017年荷兰密码学研究小组CWI和Google正式宣布攻破了SHA-1<ref>{{Cite web|url=http://shattered.it/|title=SHAttered|accessdate=2017-02-24|work=shattered.it|language=en|archive-date=2017-02-23|archive-url=https://web.archive.org/web/20170223184831/https://shattered.it/|dead-url=no}}</ref>。
SHA家族的五個演算法,分別是SHA-1、SHA-224、SHA-256、SHA-384,和SHA-512,由[[美國國家安全局]](NSA)所設計,並由[[美國國家標準與技術研究院]](NIST)發佈;是美國的政府標準。後四者有時並稱為SHA-2。SHA-1在許多安全協定中廣為使用,包括[[TLS]]和[[SSL]]、[[PGP]]、[[SSH]]、[[S/MIME]]和[[IPsec]],曾被視為是[[MD5]](更早之前被廣為使用的雜湊函數)的後繼者。但SHA-1的安全性如今被密碼學家嚴重質疑;雖然至今尚未出現對SHA-2有效的攻擊,它的演算法跟SHA-1基本上仍然相似;因此有些人開始發展其他替代的雜湊演算法。緣於最近{{when}}對SHA-1的種種攻擊發表,「[[美國國家標準與技術研究院]](NIST)開始設法經由公開競爭管道(類似[[高級加密標準]]AES的發展經過),發展一個或多個新的雜湊演算法。」


*'''[[SHA-2]]''':2001年发布,包括SHA-224、SHA-256、SHA-384、SHA-512、SHA-512/224、SHA-512/256。SHA-2目前沒有出現明顯的弱點。雖然至今尚未出現對SHA-2有效的攻擊,但它的演算法跟SHA-1基本上仍然相似。
2012年10月2日,Keccak被選為NIST雜湊函式競賽的勝利者,<ref>{{cite web|url=http://www.nist.gov/itl/csd/sha-100212.cfm|title=NIST Selects Winner of Secure Hash Algorithm (SHA-3) Competition|first=2012-10-02|publisher=[[NIST]]|accessdate=2012-10-02}}</ref>成为[[SHA-3]]。 SHA-3並不是要取代SHA-2,因為SHA-2目前並沒有出現明顯的弱點。由於對MD5出現成功的破解,以及對SHA-0和SHA-1出現理論上破解的方法,NIST感覺需要一個與之前演算法不同的,可替換的加密雜湊演算法,也就是現在的SHA-3。設計者宣稱在Intel Core 2的CPU上面,此演算法的效能是12.5cpb(每位元組周期數,cycles per byte)。不過,在硬體實做上面,這個演算法比起其他演算法明顯的快上很多。<ref>{{Citation |title=Fair and Comprehensive Performance Evaluation of 14 Second Round SHA-3 ASIC Implementations |url=http://csrc.nist.gov/groups/ST/hash/sha-3/Round2/Aug2010/documents/papers/SCHAUMONT_SHA3.pdf |first1=Xu |last1=Guo |first2=Sinan |last2=Huang |first3=Leyla |last3=Nazhandali |first4=Patrick |last4=Schaumont |journal=NIST 2nd SHA-3 Candidate Conference |month=Aug. |year=2010 |accessdate=2011-02-18 |page=12}} Keccak is second only to Luffa, which did not advance to the final round.</ref>


*'''[[SHA-3]]''':2015年正式发布,由於對MD5出現成功的破解,以及對SHA-0和SHA-1出現理論上破解的方法,[[國家標準暨技術研究院|NIST]]感覺需要一個與之前演算法不同的,可替換的加密雜湊演算法,也就是現在的SHA-3。
== SHA-0和SHA-1 ==
[[File:SHA-1.svg|thumbnail|right|300px|SHA-1壓縮演算法中的一個迴圈。A, B, C, D和E是這個state中的32位元文字;''F''是會變化的非線性函數;<<<<sub>n</sub>代表bit向左循環移動n個位置。n因操作而異。田代表modulo 2<sup>32</sup>之下的加法,K<sub>t</sub>是一個常數。]]
最初載明的演算法於1993年發佈,稱做'''安全雜湊標準(Secure Hash Standard)''',[[联邦资料处理标准|FIPS]] PUB 180。這個版本現在常被稱為SHA-0。它在發佈之後很快就被NSA撤回,並且由1995年發佈的修訂版本FIPS PUB 180-1(通常稱為SHA-1)取代。SHA-1和SHA-0的演算法只在壓縮函數的訊息轉換部份差了一個位元的循環位移。根據NSA的說法,它修正了一個在原始演算法中會降低雜湊安全性的弱點。然而NSA並沒有提供任何進一步的解釋或證明該弱點已被修正。而後SHA-0和SHA-1的弱點相繼被攻破,SHA-1似乎是顯得比SHA-0有抵抗性,這多少證實了NSA當初修正演算法以增進安全性的聲明。


== SHA函数对比 ==
SHA-0和SHA-1可將一個最大2<sup>64</sup>位元的訊息,轉換成一串160位元的訊息摘要;其設計原理相似於MIT教授[[Ron Rivest|Ronald L. Rivest]]所設計的密碼學雜湊演算法[[MD4]]和[[MD5]]。
下表中的中繼雜湊值(internal state)表示對每個資料區塊壓縮雜湊過後的中繼值(internal hash sum)。

{{Further|Merkle–Damgård construction}}
=== SHA-0的破解 ===
{| class="wikitable" style="margin-top: 0px; width:100%"
在[[CRYPTO]] 98上,兩位法國研究者提出一種對SHA-0的攻擊方式<ref>Chabaud and Joux, 1998</ref>:在2<sup>61</sup>的計算複雜度之內,就可以發現一次碰撞(即兩個不同的訊息對應到相同的訊息摘要);這個數字小於[[生日攻擊|生日攻擊法]]所需的2<sup>80</sup>,也就是說,存在一種演算法,使其安全性不到一個理想的雜湊函數抵抗攻擊所應具備的計算複雜度。
|+ SHA函数对比

|- style="vertical-align:bottom;"
2004年時,[[Eli Biham|Biham]]和Chen也發現了SHA-0的近似碰撞,也就是兩個訊息可以雜湊出幾乎相同的數值;其中162位元中有142位元相同。他們也發現了SHA-0的完整碰撞(相對於近似碰撞),將本來需要80次方的複雜度降低到62次方。
! colspan="2" | 算法和变体

! 輸出雜湊值長度<br/>(bits)
2004年8月12日,Joux, Carribault, Lemuet和Jalby宣佈找到SHA-0演算法的完整碰撞的方法,這是歸納Chabaud和Joux的攻擊所完成的結果。發現一個完整碰撞只需要2<sup>51</sup>的計算複雜度。他們使用的是一台有256顆[[Itanium#Itanium2|Itanium2]]處理器的[[超級電腦]],約耗80,000 CPU工時。
! 中繼雜湊值長度<br/>(bits)

! 資料區塊長度<br/>(bits)
2004年8月17日,在[[CRYPTO]] 2004的Rump會議上,[[王小雲]],[[馮登國]](Feng)、[[來學嘉]](Lai),和[[于紅波]](Yu)宣佈了攻擊[[MD5]]、SHA-0和其他[[雜湊函數]]的初步結果。他們攻擊SHA-0的計算複雜度是2<sup>40</sup>,這意謂的他們的攻擊成果比Joux還有其他人所做的更好。請參見[[MD5#Security|MD5安全性]]。2005年二月,[[王小雲]]和[[殷益群]]、[[于紅波]]再度發表了對SHA-0破密的演算法,可在2<sup>39</sup>的計算複雜度內就找到碰撞。
! 最大輸入訊息長度<br/>(bits)

! 迴圈次數
=== SHA-1的破解 ===
! 使用到的運算子
鑒於SHA-0的破密成果,專家們建議那些計劃利用SHA-1實作密碼系統的人們也應重新考慮。在2004年[[CRYPTO]]會議結果公佈之後,NIST即宣布他們將逐漸減少使用SHA-1,改以SHA-2取而代之。
! 碰撞攻击<br/>(bits)

! 性能示例{{refn|Found on an [[AMD Opteron]] 8354 2.2 GHz processor running 64-bit Linux<ref>{{cite web
2005年,[[Rijmen]]和[[Oswald]]發表了對SHA-1較弱版本(53次的加密迴圈而非80次)的攻擊:在2<sup>80</sup>的計算複雜度之內找到碰撞。
| url=http://www.cryptopp.com/benchmarks-amd64.html

| title=Crypto++ 5.6.0 Benchmarks
2005年二月,[[王小雲]]、[[殷益群]]及[[于紅波]]發表了對完整版SHA-1的攻擊,只需少於2<sup>69</sup>的計算複雜度,就能找到一組碰撞。(利用[[生日攻擊|生日攻擊法]]找到碰撞需要2<sup>80</sup>的計算複雜度。)
| accessdate=2013-06-13

| archive-date=2016-10-14
這篇論文的作者們寫道;「我們的破密分析是以對付SHA-0的[[差分攻擊]]、近似碰撞、多區塊碰撞技術、以及從[[MD5]]演算法中尋找碰撞的訊息更改技術為基礎。沒有這些強力的分析工具,SHA-1就無法破解。」此外,作者還展示了一次對58次加密迴圈SHA-1的破密,在2<sup>33</sup>個單位操作內就找到一組碰撞。完整攻擊方法的論文發表在2005年八月的[[CRYPTO]]會議中。
| archive-url=https://web.archive.org/web/20161014211419/http://www.cryptopp.com/benchmarks-amd64.html

| dead-url=no
殷益群在一次面談中如此陳述:「大致上來說,我們找到了兩個弱點:其一是前置處理不夠複雜;其二是前20個迴圈中的某些數學運算會造成不可預期的安全性問題。」
}}</ref>}}<br/>([[Mebibyte|MiB]]/s)

|- style="text-align:center;vertical-align:top;"
2005年8月17日的[[CRYPTO]]會議尾聲中[[王小雲]]、[[姚期智]]、[[姚儲楓]]再度發表更有效率的SHA-1攻擊法,能在2<sup>63</sup>個計算複雜度內找到碰撞。
| colspan="2" | '''[[MD5]]'''(作为参考)|| 128 || 128<br>{{nowrap|(4 × 32)}} || 512 || 无限<ref>{{cite web |url=http://tools.ietf.org/html/rfc1321 |title=The MD5 Message-Digest Algorithm |date= |accessdate=2016-04-18 |archive-date=2017-04-26 |archive-url=https://web.archive.org/web/20170426215704/https://tools.ietf.org/html/rfc1321 |dead-url=no }}</ref> || 64 || And, Xor, Rot, {{nowrap|Add (mod&nbsp;2<sup>32</sup>),}} Or || {{Bad|≤18<br/>(发现碰撞)}} || 335

|- style="text-align:center;vertical-align:top;"
2006年的[[CRYPTO]]會議上,[[Christian Rechberger]]和[[Christophe De Cannière]]宣布他們能在容許攻擊者決定部分原訊息的條件之下,找到SHA-1的一個碰撞。
| colspan="2" | '''{{nowrap|[[SHA-0]]}}''' || 160 || 160<br>{{nowrap|(5 × 32)}} || 512 || 2<sup>64</sup> − 1|| 80 || rowspan="2" | And, Xor, Rot, {{nowrap|Add (mod&nbsp;2<sup>32</sup>),}} Or || {{Bad|<34<br/>(发现碰撞)}} || -

|- style="text-align:center;vertical-align:top;"
在密碼學的學術理論中,任何攻擊方式,其計算複雜度若少於暴力搜尋法所需要的計算複雜度,就能被視為針對該密碼系統的一種破密法;但這並不表示該破密法已經可以進入實際應用的階段。
| colspan="2" | '''{{nowrap|[[SHA-1]]}}''' || 160 || 160<br>{{nowrap|(5 × 32)}} || 512 || 2<sup>64</sup> − 1 || 80 || {{Bad|<63<ref>{{cite web |url=https://sites.google.com/site/itstheshappening/ |title=The SHAppening: freestart collisions for SHA-1 |date= |accessdate=2015-11-05 |archive-date=2017-04-19 |archive-url=https://web.archive.org/web/20170419214146/https://sites.google.com/site/itstheshappening/ |dead-url=no }}</ref><br/>(发现碰撞<ref>{{cite web |url=https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html |title=Announcing the first SHA1 collision |date=2017-02-23 |accessdate=2017-02-23 |archive-date=2017-04-24 |archive-url=https://web.archive.org/web/20170424012912/https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html |dead-url=no }}</ref>)}} || 192

|- style="text-align:center;vertical-align:top;"
就應用層面的考量而言,一種新的破密法出現,暗示着將來可能會出現更有效率、足以實用的改良版本。雖然這些實用的破密法版本根本還沒誕生,但確有必要發展更強的雜湊演算法來取代舊的演算法。在「碰撞」攻擊法之外,另有一種反譯攻擊法(Pre-image attack),就是由雜湊出的字串反推原本的訊息;反譯攻擊的嚴重性更在碰撞攻擊之上,但也更困難。在許多會應用到密碼雜湊的情境(如用戶密碼的存放、文件的[[數位簽章]]等)中,碰撞攻擊的影響並不是很大。舉例來說,一個攻擊者可能不會只想要偽造一份一模一樣的文件,而會想改造原來的文件,再附上合法的簽章,來愚弄持有公钥的驗證者。另一方面,如果可以從密文中反推未加密前的使用者密碼,攻擊者就能利用得到的密碼登入其他使用者的帳戶,而這種事在密碼系統中是不能被允許的。但若存在反譯攻擊,只要能得到指定使用者密碼雜湊過後的字串(通常存在影檔中,而且可能不會透露原密碼資訊),就有可能得到該使用者的密碼。
| rowspan="2" | '''{{nowrap|[[SHA-2]]}}''' || ''SHA-224''<br/>''SHA-256'' || 224<br/>256 || 256<br/>{{nowrap|(8 × 32)}} || 512 || 2<sup>64</sup> − 1 || 64 || And, Xor, Rot, {{nowrap|Add (mod&nbsp;2<sup>32</sup>),}} Or, Shr || {{Won|112 <br/> 128}} || 139

|- style="text-align:center;vertical-align:top;"
== SHA-2 ==
| ''SHA-384''<br/>''SHA-512''<br/>''{{nowrap|SHA-512/224}}''<br/>''{{nowrap|SHA-512/256}}'' || 384<br/>512<br/>224 <br/>256 || 512<br/>{{nowrap|(8 × 64)}} || 1024 || 2<sup>128</sup> − 1 || 80 || And, Xor, Rot, {{nowrap|Add (mod&nbsp;2<sup>64</sup>),}} Or, Shr || {{Won|192<br/>256<br/>112<br/>128}} || 154
[[File:SHA-2.svg|thumbnail|right|400px| SHA-2的第t個加密迴圈。圖中的深藍色方塊是事先定義好的非線性函數。ABCDEFGH一開始分別是八個初始值,K<sub>t</sub>是第t個金鑰,W<sub>t</sub>是本區塊產生第t個word。原訊息被切成固定長度的區塊,對每一個區塊,產生n個word(n視演算法而定),透過重複運作迴圈n次對ABCDEFGH這八個工作區段循環加密。最後一次迴圈所產生的八段字串合起來即是此區塊對應到的雜湊字串。若原訊息包含數個區塊,則最後還要將這些區塊產生的雜湊字串加以混合才能產生最後的雜湊字串。]]
|- style="text-align:center;vertical-align:top;"

| rowspan="2" | '''{{nowrap|[[SHA-3]]}}''' || ''SHA3-224''<br/>''SHA3-256''<br/>''SHA3-384''<br/>''SHA3-512'' || 224<br/>256<br/>384<br/>512 || rowspan="2" | 1600<br>{{nowrap|(5 × 5 × 64)}} || 1152<br/>1088<br/>832<br/>576 || rowspan="2" | 无限<ref>{{cite web |url=http://sponge.noekeon.org/ |title=The Sponge Functions Corner |date= |accessdate=2016-01-27 |archive-date=2012-09-04 |archive-url=https://www.webcitation.org/6AQLAQuz4?url=http://sponge.noekeon.org/ |dead-url=no }}</ref>|| rowspan="2" | 24<ref>{{cite web |url=http://keccak.noekeon.org/specs_summary.html |title=The Keccak sponge function family |date= |accessdate=2016-01-27 |archive-date=2016-08-06 |archive-url=https://web.archive.org/web/20160806192717/http://keccak.noekeon.org/specs_summary.html |dead-url=no }}</ref> || rowspan="2" | And, Xor, Rot, Not || {{Won|112<br/>128<br/>192<br/>256}} || -
NIST發佈了三個額外的SHA變體,這三個函數都將訊息對應到更長的訊息摘要。以它們的摘要長度(以位元計算)加在原名後面來命名:SHA-256,SHA-384和SHA-512。它們發佈於2001年的FIPS PUB 180-2草稿中,隨即通過審查和評論。包含SHA-1的FIPS PUB 180-2,於2002年以官方標準發佈。2004年2月,發佈了一次FIPS PUB 180-2的變更通知,加入了一個額外的變種SHA-224",這是為了符合雙金鑰[[3DES]]所需的金鑰長度而定義。
|- style="text-align:center;vertical-align:top;"

| ''SHAKE128''<br/>''SHAKE256'' || {{nowrap|''d'' (arbitrary)}}<br/>{{nowrap|''d'' (arbitrary)}} || 1344<br/>1088 || {{Won|min(''d''/2, 128)<br/>min(''d''/2, 256)}} || -
SHA-256和SHA-512是很新的雜湊函數,前者以定義一個word為32位元,後者則定義一個word為64位元。它們分別使用了不同的偏移量,或用不同的常數,然而,實際上二者結構是相同的,只在迴圈執行的次數上有所差異。SHA-224以及SHA-384則是前述二種雜湊函數的截短版,利用不同的初始值做計算。

這些新的雜湊函數並沒有接受像SHA-1一樣的公眾密碼社群做詳細的檢驗,所以它們的密碼安全性還不被大家廣泛的信任。Gilbert和Handschuh在2003年曾對這些新變種作過一些研究,聲稱他們沒有找到弱點。

== SHA所定義的長度 ==
下表中的中繼雜湊值(internal state)表示對每個資料區塊壓縮雜湊過後的中繼值(internal hash sum)。詳情請參見[[Merkle-Damgård hash function|Merkle-Damgård construction]]。

{| class="wikitable"
!演算法
!輸出雜湊值長度(bits)
!中繼雜湊值長度(bits)
!資料區塊長度(bits)
!最大輸入訊息長度(bits)
!一個Word長度(bits)
!迴圈次數
!使用到的運算子
![[雜湊碰撞攻擊|碰撞攻擊]]
|- align="center"
| '''SHA-0''' || 160 || 160 || 512 || 2<sup>64</sup> − 1 || 32 || 80 || +,and,or,xor,rotl || 是
|- align="center"
| '''SHA-1''' || 160 || 160 || 512 || 2<sup>64</sup> − 1 || 32 || 80 || +,and,or,xor,rotl || 存在2<sup>63</sup>的攻擊
|- align="center"
| '''SHA-256/224''' || 256/224 || 256 || 512 || 2<sup>64</sup> − 1 || 32 || 64 || +,and,or,xor,shr,rotr || 尚未出現
|- align="center"
| '''SHA-512/384''' || 512/384 || 512 || 1024 || 2<sup>128</sup> − 1 || 64 || 80 || +,and,or,xor,shr,rotr || 尚未出現
|}
|}


== SHA<sub>d</sub> ==
== SHA<sub>d</sub> ==
{{Unreferenced section|time=2016-12-15T16:17:38+00:00}}
SHA<sub>d</sub>函數是一個簡單的相同SHA函數的重述:
SHA<sub>d</sub>函數是一個簡單的相同SHA函數的重述:


SHA<sub>d</sub>-256(m)=SHA-256(SHA-256(m))。它會克服有關''延伸長度''攻擊的問題。
SHA<sub>d</sub>-256(m)=SHA-256(SHA-256(m))。它會克服有關''延伸長度''攻擊的問題。

== 應用 ==
SHA-1, SHA-224, SHA-256, SHA-384和SHA-512都被需要安全雜湊演算法的美國聯邦政府所應用,他們也使用其他的密碼演算法和協定來保護敏感的未保密資料。FIPS PUB 180-1也鼓勵私人或商業組織使用SHA-1加密。[[Fritz-chip]]將很可能使用SHA-1雜湊函數來實現個人電腦上的[[數位版權管理]]。

首先推動安全雜湊演算法出版的是已合併的[[數位簽章演算法|數位簽章標準]]。

SHA雜湊函數已被做為[[SHACAL]] [[分組密碼]]演算法的基礎。

== SHA-1演算法 ==
以下是SHA-1演算法的[[伪代码]]:
<span style="color: green;">''Note: All variables are unsigned 32 bits and wrap modulo 2<sup>32</sup> when calculating''</span>

<span style="color: green;">''Initialize variables:''</span>
h0 := 0x67452301
h1 := 0xEFCDAB89
h2 := 0x98BADCFE
h3 := 0x10325476
h4 := 0xC3D2E1F0

<span style="color: green;">''Pre-processing:''</span>
append the bit '1' to the message
append k bits '0', where k is the minimum number >= 0 such that the resulting message
length (in ''bits'') is [[modular arithmetic|congruent]] to 448(mod 512)
append length of message (before pre-processing), in ''bits'', as 64-bit big-endian integer

<span style="color: green;">''Process the message in successive 512-bit chunks:''</span>
break message into 512-bit chunks
'''for''' each chunk
break chunk into sixteen 32-bit big-endian words w[i], 0 ≤ i ≤ 15

<span style="color: green;">''Extend the sixteen 32-bit words into eighty 32-bit words:''</span>
'''for''' i '''from''' 16 to 79
w[i] := (w[i-3] '''xor''' w[i-8] '''xor''' w[i-14] '''xor''' w[i-16]) '''leftrotate''' 1

<span style="color: green;">''Initialize hash value for this chunk:''</span>
a := h0
b := h1
c := h2
d := h3
e := h4

<span style="color: green;">''Main loop:''</span>
'''for''' i '''from''' 0 to 79
'''if''' 0 ≤ i ≤ 19 '''then'''
f := (b '''and''' c) '''or''' (('''not''' b) '''and''' d)
k := 0x5A827999
'''else if''' 20 ≤ i ≤ 39
f := b '''xor''' c '''xor''' d
k := 0x6ED9EBA1
'''else if''' 40 ≤ i ≤ 59
f := (b '''and''' c) '''or''' (b '''and''' d) '''or'''(c '''and''' d)
k := 0x8F1BBCDC
'''else if''' 60 ≤ i ≤ 79
f := b '''xor''' c '''xor''' d
k := 0xCA62C1D6

temp := (a '''leftrotate''' 5) + f + e + k + w[i]
e := d
d := c
c := b '''leftrotate''' 30
b := a
a := temp

<span style="color: green;">''Add this chunk's hash to result so far:''</span>
h0 := h0 + a
h1 := h1 + b
h2 := h2 + c
h3 := h3 + d
h4 := h4 + e

<span style="color:green;">''Produce the final hash value (big-endian):''</span>
digest = hash = h0 '''append''' h1 '''append''' h2 '''append''' h3 '''append''' h4

上述關於<code>f</code>運算式列於FIPS PUB 180-1中,以下替代運算式也許也能在主要迴圈裡計算<code>f</code>:
(0 ≤ i ≤ 19): f := d '''xor''' (b '''and''' (c '''xor''' d)) <span style="color: green;">''(alternative)''</span>
(40 ≤ i ≤ 59): f := (b '''and''' c) '''or''' (d '''and''' (b '''or''' c)) <span style="color: green;">''(alternative 1)''</span>
(40 ≤ i ≤ 59): f := (b '''and''' c) '''or''' (d '''and''' (b '''xor''' c)) <span style="color: green;">''(alternative 2)''</span>
(40 ≤ i ≤ 59): f := (b '''and''' c) + (d '''and''' (b '''xor''' c)) <span style="color: green;">''(alternative 3)''</span>

== SHA-2演算法 ==
以下是SHA-256演算法的[[虛擬碼]]。注意,64個word <code>w[16..63]</code>中的位元比起SHA-1演算法,混合的程度大幅提升。

<span style="color: green;">''Note: All variables are unsigned 32 bits and wrap modulo 2<sup>32</sup> when calculating''</span>

<span style="color:green;">''Initialize variables''</span>
<span style="color:green;">(first 32 bits of the <em>fractional parts<em> of the square roots of the first 8 primes 2..19):</span>
h0 := 0x6a09e667
h1 := 0xbb67ae85
h2 := 0x3c6ef372
h3 := 0xa54ff53a
h4 := 0x510e527f
h5 := 0x9b05688c
h6 := 0x1f83d9ab
h7 := 0x5be0cd19

<span style="color:green;">''Initialize table of round constants''</span>
<span style="color:green;">(first 32 bits of the ''fractional parts'' of the cube roots of the first 64 primes 2..311):</span>
k[0..63] :=
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2

<span style="color:green;">''Pre-processing:''</span>
append the bit '1' to the message
append k bits '0', where k is the minimum number >= 0 such that the resulting message
length (in ''bits'') is [[modular arithmetic|congruent]] to 448(mod 512)
append length of message (before pre-processing), in ''bits'', as 64-bit big-endian integer

<span style="color:green;">''Process the message in successive 512-bit chunks:''</span>
break message into 512-bit chunks
'''for''' each chunk
break chunk into sixteen 32-bit big-endian words w[0..15]

<span style="color:green;">''Extend the sixteen 32-bit words into sixty-four 32-bit words:''</span>
'''for''' i '''from''' 16 to 63
s0 := (w[i-15] '''rightrotate''' 7) '''xor''' (w[i-15] '''rightrotate''' 18) '''xor'''(w[i-15] '''rightshift''' 3)
s1 := (w[i-2] '''rightrotate''' 17) '''xor''' (w[i-2] '''rightrotate''' 19) '''xor'''(w[i-2] '''rightshift''' 10)
w[i] := w[i-16] '''+''' s0 '''+''' w[i-7] '''+''' s1

<span style="color:green;">''Initialize hash value for this chunk:''</span>
a := h0
b := h1
c := h2
d := h3
e := h4
f := h5
g := h6
h := h7

<span style="color:green;">''Main loop:''</span>
'''for''' i '''from''' 0 to 63
s0 := (a '''rightrotate''' 2) '''xor''' (a '''rightrotate''' 13) '''xor'''(a '''rightrotate''' 22)
maj := (a '''and''' b) '''xor''' (a '''and''' c) '''xor'''(b '''and''' c)
t2 := s0 + maj
s1 := (e '''rightrotate''' 6) '''xor''' (e '''rightrotate''' 11) '''xor'''(e '''rightrotate''' 25)
ch := (e '''and''' f) '''xor''' (('''not''' e) '''and''' g)
t1 := h + s1 + ch + k[i] + w[i]

h := g
g := f
f := e
e := d + t1
d := c
c := b
b := a
a := t1 + t2

<span style="color:green;">''Add this chunk's hash to result so far:''</span>
h0 := h0 + a
h1 := h1 + b
h2 := h2 + c
h3 := h3 + d
h4 := h4 + e
h5 := h5 + f
h6 := h6 + g
h7 := h7 + h

<span style="color:green;">''Produce the final hash value (big-endian):''</span>
digest = hash = h0 '''append''' h1 '''append''' h2 '''append''' h3 '''append''' h4 '''append''' h5 '''append''' h6 '''append''' h7

其中<code>ch</code>函數及<code>maj</code>函數可利用前述SHA-1的優化方式改寫。

SHA-224和SHA-256基本上是相同的,除了:
* <code>h0</code>到<code>h7</code>的初始值不同,以及
* SHA-224輸出時截掉<code>h7</code>的函數值。

SHA-512和SHA-256的結構相同,但:
* SHA-512所有的數字都是64位元,
* SHA-512執行80次加密迴圈而非64次,
* SHA-512初始值和常數拉長成64位元,以及
* 二者位元的偏移量和循環位移量不同。

SHA-384和SHA-512基本上是相同的,除了:
* <code>h0</code>到<code>h7</code>的初始值不同,以及
* SHA-384輸出時截掉<code>h6</code>和<code>h7</code>的函數值。


== 参考文献 ==
== 参考文献 ==
=== 引用 ===
=== 引用 ===
{{Reflist|2}}
{{Reflist}}


=== 来源 ===
=== 来源 ===
* Eli Biham, Rafi Chen, Near-Collisions of SHA-0, Cryptology ePrint Archive, Report 2004/146, 2004 (to appear CRYPTO 2004) [http://eprint.iacr.org/2004/146/]
* Eli Biham, Rafi Chen, Near-Collisions of SHA-0, Cryptology ePrint Archive, Report 2004/146, 2004 (to appear CRYPTO 2004) [http://eprint.iacr.org/2004/146/]{{Wayback|url=http://eprint.iacr.org/2004/146/ |date=20040927021451 }}
* Florent Chabaud, Antoine Joux: Differential Collisions in SHA-0. [[CRYPTO]] 1998. pp. 56–71.
* Florent Chabaud, Antoine Joux: Differential Collisions in SHA-0. [[CRYPTO]] 1998. pp. 56–71.
* Henri Gilbert, Helena Handschuh: Security Analysis of SHA-256 and Sisters. Selected Areas in Cryptography 2003: pp. 175–193.
* Henri Gilbert, Helena Handschuh: Security Analysis of SHA-256 and Sisters. Selected Areas in Cryptography 2003: pp. 175–193.


== 外部链接 ==
== 外部链接 ==
* [http://csrc.nist.gov/encryption/tkhash.html FIPS PUB 180-2],''安全雜湊標準''
* [https://web.archive.org/web/20021212213605/http://csrc.nist.gov/encryption/tkhash.html FIPS PUB 180-2],''安全雜湊標準''
* [http://www.ietf.org/rfc/rfc3174.txt RFC 3174],''美國安全雜湊演算法1(SHA1)''
* [http://www.ietf.org/rfc/rfc3174.txt RFC 3174]{{Wayback|url=http://www.ietf.org/rfc/rfc3174.txt |date=20110824210522 }},''美國安全雜湊演算法1(SHA1)''
* [http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-SHA1.html 這個Javascript SHA-1計算機會展示計算過程中的中繼值]
* [http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-SHA1.html 這個Javascript SHA-1計算機會展示計算過程中的中繼值]{{Wayback|url=http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-SHA1.html |date=20040830062306 }}
* [https://web.archive.org/web/20180620001154/https://passwordsgenerator.pro/sha256 SHA-256在线格计算工具]


== 參見 ==
== 參見 ==
第286行: 第78行:
* [[加密技术]]
* [[加密技术]]
* [[演算法]]
* [[演算法]]
* [[图灵机]]
* [[图灵归约]]
* [[交互式证明系统]]
* [[隨機預言機]]
* [[MD5]]

{{密碼學|hash}}


[[Category:密散列函数]]
[[Category:密散列函数]]

2021年2月8日 (一) 20:57的最新版本

安全散列演算法(英語:Secure Hash Algorithm,縮寫為SHA)是一個密碼雜湊函數家族,是FIPS所認證的安全雜湊演算法。能計算出一個數位訊息所對應到的,長度固定的字串(又稱訊息摘要)的算法。且若輸入的訊息不同,它們對應到不同字串的機率很高。

SHA家族的演算法,由美國國家安全局(NSA)所設計,並由美國國家標準與技術研究院(NIST)發佈,是美國的政府標準,其分別是:

  • SHA-0:1993年发布,当时称做安全散列标准(Secure Hash Standard),发布之后很快就被NSA撤回,是SHA-1的前身。
  • SHA-1:1995年发布,SHA-1在許多安全協定中廣為使用,包括TLSGnuPGSSHS/MIMEIPsec,是MD5的後繼者。但SHA-1的安全性在2010年以后已经不被大多数的加密场景所接受。2017年荷兰密码学研究小组CWI和Google正式宣布攻破了SHA-1[1]
  • SHA-2:2001年发布,包括SHA-224、SHA-256、SHA-384、SHA-512、SHA-512/224、SHA-512/256。SHA-2目前沒有出現明顯的弱點。雖然至今尚未出現對SHA-2有效的攻擊,但它的演算法跟SHA-1基本上仍然相似。
  • SHA-3:2015年正式发布,由於對MD5出現成功的破解,以及對SHA-0和SHA-1出現理論上破解的方法,NIST感覺需要一個與之前演算法不同的,可替換的加密雜湊演算法,也就是現在的SHA-3。

SHA函数对比

[编辑]

下表中的中繼雜湊值(internal state)表示對每個資料區塊壓縮雜湊過後的中繼值(internal hash sum)。

SHA函数对比
算法和变体 輸出雜湊值長度
(bits)
中繼雜湊值長度
(bits)
資料區塊長度
(bits)
最大輸入訊息長度
(bits)
迴圈次數 使用到的運算子 碰撞攻击
(bits)
性能示例[3]
(MiB/s)
MD5(作为参考) 128 128
(4 × 32)
512 无限[4] 64 And, Xor, Rot, Add (mod 232), Or ≤18
(发现碰撞)
335
SHA-0 160 160
(5 × 32)
512 264 − 1 80 And, Xor, Rot, Add (mod 232), Or <34
(发现碰撞)
-
SHA-1 160 160
(5 × 32)
512 264 − 1 80 <63[5]
(发现碰撞[6]
192
SHA-2 SHA-224
SHA-256
224
256
256
(8 × 32)
512 264 − 1 64 And, Xor, Rot, Add (mod 232), Or, Shr 112
128
139
SHA-384
SHA-512
SHA-512/224
SHA-512/256
384
512
224
256
512
(8 × 64)
1024 2128 − 1 80 And, Xor, Rot, Add (mod 264), Or, Shr 192
256
112
128
154
SHA-3 SHA3-224
SHA3-256
SHA3-384
SHA3-512
224
256
384
512
1600
(5 × 5 × 64)
1152
1088
832
576
无限[7] 24[8] And, Xor, Rot, Not 112
128
192
256
-
SHAKE128
SHAKE256
d (arbitrary)
d (arbitrary)
1344
1088
min(d/2, 128)
min(d/2, 256)
-

SHAd

[编辑]

SHAd函數是一個簡單的相同SHA函數的重述:

SHAd-256(m)=SHA-256(SHA-256(m))。它會克服有關延伸長度攻擊的問題。

参考文献

[编辑]

引用

[编辑]
  1. ^ SHAttered. shattered.it. [2017-02-24]. (原始内容存档于2017-02-23) (英语). 
  2. ^ Crypto++ 5.6.0 Benchmarks. [2013-06-13]. (原始内容存档于2016-10-14). 
  3. ^ Found on an AMD Opteron 8354 2.2 GHz processor running 64-bit Linux[2]
  4. ^ The MD5 Message-Digest Algorithm. [2016-04-18]. (原始内容存档于2017-04-26). 
  5. ^ The SHAppening: freestart collisions for SHA-1. [2015-11-05]. (原始内容存档于2017-04-19). 
  6. ^ Announcing the first SHA1 collision. 2017-02-23 [2017-02-23]. (原始内容存档于2017-04-24). 
  7. ^ The Sponge Functions Corner. [2016-01-27]. (原始内容存档于2012-09-04). 
  8. ^ The Keccak sponge function family. [2016-01-27]. (原始内容存档于2016-08-06). 

来源

[编辑]
  • Eli Biham, Rafi Chen, Near-Collisions of SHA-0, Cryptology ePrint Archive, Report 2004/146, 2004 (to appear CRYPTO 2004) [1]页面存档备份,存于互联网档案馆
  • Florent Chabaud, Antoine Joux: Differential Collisions in SHA-0. CRYPTO 1998. pp. 56–71.
  • Henri Gilbert, Helena Handschuh: Security Analysis of SHA-256 and Sisters. Selected Areas in Cryptography 2003: pp. 175–193.

外部链接

[编辑]

參見

[编辑]