150
密碼學漏洞與他們的產地 [email protected]

密碼學漏洞與他們的產地 Crypto fail and where to find them

Embed Size (px)

Citation preview

Page 1: 密碼學漏洞與他們的產地   Crypto fail and where to find them

密碼學漏洞與他們的產地[email protected]

Page 2: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Who am I

- Zuan

- chroot讀書會成員- 什麼都略懂一點,但什麼都不精通- 愛玩資訊跟電子領域的各種事務- 興趣是騎腳踏車跟買學生證

Page 3: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Content

- 非對稱式演算法- 對稱式演算法- 雜湊- 其他

Page 4: 密碼學漏洞與他們的產地   Crypto fail and where to find them

這邊有人沒有聽過 RSA的嗎 ?請舉個手

Page 5: 密碼學漏洞與他們的產地   Crypto fail and where to find them

c ≡ ne (mod (N))n ≡ cd (mod (N))

ed ≡ 1 (mod φ(N))

Page 6: 密碼學漏洞與他們的產地   Crypto fail and where to find them

c -> 密文n -> 明文

(e,N) -> 公鑰(d,N) -> 私鑰

Page 7: 密碼學漏洞與他們的產地   Crypto fail and where to find them

一般為了加速計算, e會取很小(e=65537 or e=17 or e=3)

Page 8: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果 ne < N且 e = 3

Page 9: 密碼學漏洞與他們的產地   Crypto fail and where to find them

那不是開方根就能解密了?

Page 10: 密碼學漏洞與他們的產地   Crypto fail and where to find them

...

Page 11: 密碼學漏洞與他們的產地   Crypto fail and where to find them

OK,那如果效能不是問題,我 e選大一點總可以吧?

Page 12: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果要加密 0或 1

Page 13: 密碼學漏洞與他們的產地   Crypto fail and where to find them

0^e = 01^e = 1

Page 14: 密碼學漏洞與他們的產地   Crypto fail and where to find them

...

Page 15: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Encrypt(‘A’) = YCD6WGtUMNDedQ2HjC5KzSVTEncrypt(‘B’) = REfHH3SCzed6jkRhP6JZwytHEncrypt(‘C’) = aSWP4Z6cX2VYWMxwA9bGMKRAEncrypt(‘D’) = sRuHR8t8Vmy3th8Gf2RAVfkzEncrypt(‘E’) = F4ahebRHXUyzzeNgcBzN4r26

Encrypt(t) = aSWP4Z6cX2VYWMxwA9bGMKRAt = ?

Page 16: 密碼學漏洞與他們的產地   Crypto fail and where to find them

就說該都猜 C

Page 17: 密碼學漏洞與他們的產地   Crypto fail and where to find them

一個加密演算法不該有那麼多毛

Page 18: 密碼學漏洞與他們的產地   Crypto fail and where to find them

以上這些弱點都可以透過 Padding來解決

Page 19: 密碼學漏洞與他們的產地   Crypto fail and where to find them

加解密: PKCS #1 v1.5, OAEP簽章: PKCS #1 v1.5, PSS

Page 20: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Plaintext n

Padded Plaintext n

Ciphertext n

OAEP

RSA

Page 21: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果自己實作 Signature Padding應依照標準驗證簽章

Page 22: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Python RSA出過類似包CVE-2016-1494

Page 23: 密碼學漏洞與他們的產地   Crypto fail and where to find them

[問掛 ] 有沒有 RSA很慢的八卦?

Page 24: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 25: 密碼學漏洞與他們的產地   Crypto fail and where to find them

2048 bit/op * 768.3 op/s ≈ 200 kB/s2048 bit/op * 26270.6 op/s ≈ 6.7 MB/s

Page 26: 密碼學漏洞與他們的產地   Crypto fail and where to find them

撥接喔?!╯ -____-) ~═╩════╩═╯

Page 27: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 28: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 29: 密碼學漏洞與他們的產地   Crypto fail and where to find them

加解密 (AES): 75~90 MB/s簽章 (SHA2): 30~300 MB/s

Page 30: 密碼學漏洞與他們的產地   Crypto fail and where to find them

RSA iPad AES/SHA!

Page 31: 密碼學漏洞與他們的產地   Crypto fail and where to find them

RSA Weak Key

Page 32: 密碼學漏洞與他們的產地   Crypto fail and where to find them

RSA的 Key不是找兩個大質數就夠了

Page 33: 密碼學漏洞與他們的產地   Crypto fail and where to find them

CVE-2008-0166

Page 34: 密碼學漏洞與他們的產地   Crypto fail and where to find them

亂數產生器不夠亂,某些質數出現機率高

Page 35: 密碼學漏洞與他們的產地   Crypto fail and where to find them

=> 直接猜那些質數可破密

Page 36: 密碼學漏洞與他們的產地   Crypto fail and where to find them

=> 拿一堆 N來 GCD也可以破一發( shared factor)

Page 37: 密碼學漏洞與他們的產地   Crypto fail and where to find them

N1 = p1*q1N2 = p2*q2

若 p1 = p2, p1 = GCD(N1,N2)

Page 38: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Weiner’s Attack

Page 39: 密碼學漏洞與他們的產地   Crypto fail and where to find them

不常見,記得 d > ⅓ N¼就好

Page 40: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Pollard p-1 Factorization

Page 41: 密碼學漏洞與他們的產地   Crypto fail and where to find them

也不常見,建議 (p-1)/2也要是質數

Page 42: 密碼學漏洞與他們的產地   Crypto fail and where to find them

TL;DR懶人包 : 生 Key? OpenSSL

Page 43: 密碼學漏洞與他們的產地   Crypto fail and where to find them

[問掛 ] 有沒有 RSA很慢的八卦?

Page 44: 密碼學漏洞與他們的產地   Crypto fail and where to find them

...

Page 45: 密碼學漏洞與他們的產地   Crypto fail and where to find them

你有聽過橢圓曲線嗎?

Page 46: 密碼學漏洞與他們的產地   Crypto fail and where to find them

對稱 RSA ECC

80 1024 160

112 2048 224

128 3072 256

192 7680 384

Page 47: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 48: 密碼學漏洞與他們的產地   Crypto fail and where to find them

總時間上,橢圓曲線遠比同級的 RSA快

Page 49: 密碼學漏洞與他們的產地   Crypto fail and where to find them

ECDSA 256 bit Public Key: AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCv5hlgEJBGfQfF/4RdBDMv0hiaeNbwqwkyW4ntYsUUroqqFQMROgYSdHr2bqmX0BCX87l95Hynh2nUPxbOvWtg=

Page 50: 密碼學漏洞與他們的產地   Crypto fail and where to find them

RSA 2048 bit Public Key: AAAAB3NzaC1yc2EAAAADAQABAAABAQD4XK2ooKXQZFIrzTlu6sIVOTdnLDcKc1Qn/WvrGCBFRx5jTfKlJflCVb0BdhPayaYwibMzu87rn6IzfKdc3yEivcRIKJ0Vv/z86jjbK1V7zlTXyhvp0I2IUgL46HTFQVykn88dcj0CX0vuuITfr+sHjSLG8icBrZHOoiAXYI8xdbZFZ5BZFp5IbeD5QZ2BNotK1SkMAt1ls2AZD48toBoX0lMiWf7pufdviQw8GIkQo7Jm52QDFPj2QPUocVLeqCvzreWgu0SSfaIEDUMScM9alS/OBajrorQI9/eT85H57sv3che8ascbwN48EpQngBwwzylUl0WmsjYneybGW8xT

Page 51: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Key小就是爽啦

Page 52: 密碼學漏洞與他們的產地   Crypto fail and where to find them

下次也考慮下 ECC系列吧

Page 53: 密碼學漏洞與他們的產地   Crypto fail and where to find them

但如果 Public端為嵌入式,則可考慮RSA

Page 54: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果用 ECDSA一定要記得

Page 55: 密碼學漏洞與他們的產地   Crypto fail and where to find them

不要 Sony

Page 56: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 57: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Sony進行 PS3的 ECDSA簽章時每次 k都一樣

Page 58: 密碼學漏洞與他們的產地   Crypto fail and where to find them

兩次 k 一樣的簽章 = 洩漏私鑰

Page 59: 密碼學漏洞與他們的產地   Crypto fail and where to find them

RSA有 Side Channel問題

Page 60: 密碼學漏洞與他們的產地   Crypto fail and where to find them

想想Modular Exponentiation

Page 61: 密碼學漏洞與他們的產地   Crypto fail and where to find them

def modexp( base, exp, mod ): out = base if (exp[0] == 1) else 1 for i in range(1,len(exp)): out = (out * out) % mod; if exp[i] == 1: out = (out * base) % mod return out

Page 62: 密碼學漏洞與他們的產地   Crypto fail and where to find them

def modexp( base, exp, mod ): out = base if (exp[0] == 1) else 1 for i in range(1,len(exp)): out = (out * out) % mod; if exp[i] == 1: out = (out * base) % mod return out

Page 63: 密碼學漏洞與他們的產地   Crypto fail and where to find them

量測 CPU Cache Hit/執行時間 /使用功率可以看出這步有沒有執行

Page 64: 密碼學漏洞與他們的產地   Crypto fail and where to find them

執行在同一顆 CPU的其他程式或是對CPU有實體權限的攻擊者有機會破密

Page 65: 密碼學漏洞與他們的產地   Crypto fail and where to find them

RSA要避免類似 Side channel較麻煩ECC也有類似問題 (Double or add)

Page 66: 密碼學漏洞與他們的產地   Crypto fail and where to find them

進幾年 HITCON有示範過

Page 67: 密碼學漏洞與他們的產地   Crypto fail and where to find them

bit 0 1 2 3 4 ...

0 1 1 1 1 1 ...

1 M^1 M^2 M^4 M^8 M^16 ...

Page 68: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果有這考量,可以用現成的 Library,或是用 Ed25519

Page 69: 密碼學漏洞與他們的產地   Crypto fail and where to find them

玩夠非對稱式的,來聊聊對稱式的吧

Page 70: 密碼學漏洞與他們的產地   Crypto fail and where to find them

用過 Stream Cipher吧?

Page 71: 密碼學漏洞與他們的產地   Crypto fail and where to find them

RC4, OFB/CTR Mode的 Block Cipher,Salsa20, ChaCha20

Page 72: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Stream Cipher的 IV/Nounce每次加密一定要不一樣,否則加密多少,就能解密多

Page 73: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Win32/Dircrypt.A

Page 74: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果有使用某些舊的 Block Cipher,需要注意Weak Key

Page 75: 密碼學漏洞與他們的產地   Crypto fail and where to find them

尤其是 Blowfish

Page 76: 密碼學漏洞與他們的產地   Crypto fail and where to find them

AES沒這煩惱,把把都是好 Key

Page 77: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Block Cipher跟 RSA一樣不能單用

Page 78: 密碼學漏洞與他們的產地   Crypto fail and where to find them

要搭配 Cipher Mode of Operation

Page 79: 密碼學漏洞與他們的產地   Crypto fail and where to find them

看維基百科秒懂

Page 80: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 81: 密碼學漏洞與他們的產地   Crypto fail and where to find them

那不然要用什麼?

Page 82: 密碼學漏洞與他們的產地   Crypto fail and where to find them

大多人會推薦 CBC

Page 83: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Source: Wikipedia

Page 84: 密碼學漏洞與他們的產地   Crypto fail and where to find them

但是 CBC有 Padding Oracle問題

Page 85: 密碼學漏洞與他們的產地   Crypto fail and where to find them

解密時如果 Padding不對,不能讓對方知道

Page 86: 密碼學漏洞與他們的產地   Crypto fail and where to find them

if not VerifyPadding( … ): // ERROR return

if not VerifyHMAC( … ): // ERROR return

Page 87: 密碼學漏洞與他們的產地   Crypto fail and where to find them

除了 CBC以外,還有什麼可以用?

Page 88: 密碼學漏洞與他們的產地   Crypto fail and where to find them

看看 Google用什麼吧

Page 89: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 90: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 91: 密碼學漏洞與他們的產地   Crypto fail and where to find them

[問掛 ] 有沒有 GCM的八卦

Page 92: 密碼學漏洞與他們的產地   Crypto fail and where to find them

GCM其實很好用

Page 93: 密碼學漏洞與他們的產地   Crypto fail and where to find them

內帶驗證,且效能高

Page 94: 密碼學漏洞與他們的產地   Crypto fail and where to find them

考慮拿 GCM取代 CBC+HMAC

Page 95: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Hash

Page 96: 密碼學漏洞與他們的產地   Crypto fail and where to find them

還在用md5的舉手

Page 97: 密碼學漏洞與他們的產地   Crypto fail and where to find them

md5跟 sha1都已經過時讓他們安息吧

Page 98: 密碼學漏洞與他們的產地   Crypto fail and where to find them

大家密碼都怎麼存?

Page 99: 密碼學漏洞與他們的產地   Crypto fail and where to find them

a)明碼md5sha1sha256/sha512md5+saltsha1+saltsha256/sha512+salt

Page 100: 密碼學漏洞與他們的產地   Crypto fail and where to find them

salt可以避免 Rainbow Table

Page 101: 密碼學漏洞與他們的產地   Crypto fail and where to find them

不要再猜了,答案是以上階非

Page 102: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Source:https://gist.github.com/epixoip/973da7352f4cc005746c627527e4d073

Page 103: 密碼學漏洞與他們的產地   Crypto fail and where to find them

hash的設計都是在安全的狀況下盡量快

Page 104: 密碼學漏洞與他們的產地   Crypto fail and where to find them

所以要用另一類專門的函數 -- KDF

Page 105: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Key Derivation Function

Page 106: 密碼學漏洞與他們的產地   Crypto fail and where to find them

設計就是要慢

Page 107: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Source:https://gist.github.com/epixoip/973da7352f4cc005746c627527e4d073

Page 108: 密碼學漏洞與他們的產地   Crypto fail and where to find them

考慮使用 PBKDF2, scrypt, bcrypt

Page 109: 密碼學漏洞與他們的產地   Crypto fail and where to find them

內建 Salt, 可調運算強度

Page 110: 密碼學漏洞與他們的產地   Crypto fail and where to find them

純 Hash不應該拿來做驗證

Page 111: 密碼學漏洞與他們的產地   Crypto fail and where to find them

不少 Hash有 Length Extension的bug feature

Page 112: 密碼學漏洞與他們的產地   Crypto fail and where to find them

給 H(A), 不知道 A, 可以求 H(A+B)

Page 113: 密碼學漏洞與他們的產地   Crypto fail and where to find them

H(secret || msg) 不安全

Page 114: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果有驗證需求,請用 HMAC

Page 115: 密碼學漏洞與他們的產地   Crypto fail and where to find them

HMAC = H( K1 || H(K2 || M) )

Page 116: 密碼學漏洞與他們的產地   Crypto fail and where to find them

其他

Page 117: 密碼學漏洞與他們的產地   Crypto fail and where to find them

亂數 ?

Page 118: 密碼學漏洞與他們的產地   Crypto fail and where to find them

rand()

Page 119: 密碼學漏洞與他們的產地   Crypto fail and where to find them

當然不是

Page 120: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果需要少量的亂數 (生 Key)就跟系統拿 Entropy

Page 121: 密碼學漏洞與他們的產地   Crypto fail and where to find them

*nix: /dev/randomWin: RtlGenRandom()

Page 122: 密碼學漏洞與他們的產地   Crypto fail and where to find them

/dev/random是稀有資源

Page 123: 密碼學漏洞與他們的產地   Crypto fail and where to find them

urandom沒有快到哪裡去

Page 124: 密碼學漏洞與他們的產地   Crypto fail and where to find them

需要大量亂數 ? 用 entropy去 seed PRNG

Page 125: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Hash_DBRG, CTR_DBRG

Page 126: 密碼學漏洞與他們的產地   Crypto fail and where to find them

不要 Dual_EC_DBRG

Page 127: 密碼學漏洞與他們的產地   Crypto fail and where to find them

封包大小有 Side Channel問題

Page 128: 密碼學漏洞與他們的產地   Crypto fail and where to find them

透過 ssh tunnel上網

Page 129: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Google 台科大官網

Page 130: 密碼學漏洞與他們的產地   Crypto fail and where to find them

不難看出誰是誰

Page 131: 密碼學漏洞與他們的產地   Crypto fail and where to find them

GFW就是這樣抓 VPN的

Page 132: 密碼學漏洞與他們的產地   Crypto fail and where to find them

若要避免,可以用 obfs4/ScrambleSuit

Page 133: 密碼學漏洞與他們的產地   Crypto fail and where to find them

語音也有一樣問題

Page 134: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 135: 密碼學漏洞與他們的產地   Crypto fail and where to find them

HTTPS很好用

Page 136: 密碼學漏洞與他們的產地   Crypto fail and where to find them

但是它其實會洩漏 Server的 Domain

Page 137: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 138: 密碼學漏洞與他們的產地   Crypto fail and where to find them

最後,東西用完記得收

Page 139: 密碼學漏洞與他們的產地   Crypto fail and where to find them
Page 140: 密碼學漏洞與他們的產地   Crypto fail and where to find them

有些勒索軟體可以用刪除復原 /硬碟救援工具還原

Page 141: 密碼學漏洞與他們的產地   Crypto fail and where to find them

因為他們檔案用完沒有 Shred

Page 142: 密碼學漏洞與他們的產地   Crypto fail and where to find them

*nix: shredwin: sdelete

Page 143: 密碼學漏洞與他們的產地   Crypto fail and where to find them

記憶體也可能殘留,用完記得複寫再釋放

Page 144: 密碼學漏洞與他們的產地   Crypto fail and where to find them

結論

Page 145: 密碼學漏洞與他們的產地   Crypto fail and where to find them

能用現成整套的,就用

Page 146: 密碼學漏洞與他們的產地   Crypto fail and where to find them

HTTPS/TLS, SSH, GnuPG

Page 147: 密碼學漏洞與他們的產地   Crypto fail and where to find them

如果需要單獨使用密碼學的元素請用現成 Library

Page 148: 密碼學漏洞與他們的產地   Crypto fail and where to find them

Crypto++, CryptoJS, libsodium… etc

Page 149: 密碼學漏洞與他們的產地   Crypto fail and where to find them

END謝謝大家

Page 150: 密碼學漏洞與他們的產地   Crypto fail and where to find them

小抄 /懶人包1. 非對稱加密相關問題與建議1a. 非對稱式加密應該搭配對稱式加密使用。1b. RSA不能直接用,需要搭配專門的 Padding Scheme。1c. RSA存在Weak Key。 (Pollard p-1 Factorization, Shared Factor, Weiner)。1d. 可以考慮 ECC系列演算法,因為安全性下,需要較少運算。1e. RSA跟 ECC都存在 Timing/Power Side Channel,如果要避免,

可以考慮 Ed25519。1f. ECDSA每次簽章的 k必須隨機不重複。 (PS3因此被破解 )1g. 簽章驗證應該依照標準檢測所有參數。 (ie. CVE-2016-1494)2. 對稱演算法相關問題與建議2a. Stream Cipher必須有 Nounce,且必須每次不一樣。2b. 部份 Blocker Cipher有Weak Key。 (例如 Blowfish)。2c. Block Cipher應注意 Cipher Mode,不該單純用 ECB。2d. Block Cipher的 Padding Oracle問題。2e. Block Cipher Mode建議使用GCM模式,因為性能較優異且帶有驗證功能。

3. Hash相關問題與建議3a. md5/sha1應淘汰。3b. 密碼不應該用單純 Hash存,應該使用 PBKDF或 scrypt。3c. 注意 Length Extension Attack,因此 HMAC請照標準 HMAC格式走。4. 其他4a. 亂數應該使用標準的亂數演算法 (NIST系列, DUAL_EC_DRBG除外 )。4b. 亂數應確保有足夠的 Entropy。4c. 封包的大小與 Timing有 Side Channel。4d. 承 4c,該漏洞可利用於破解通話加密,以及分析 TLS裡面瀏覽的網站內容。4e. TLS有 SNI會洩漏你瀏覽哪個網站。4f. 除非必要,建議使用現成,常用的套件或協定 (ie. OpenSSL+TLS)4g. 注意殘留,硬碟 /記憶體需要 Shred,如果使用現成套件,用完要釋放。