六.? TLS協議給我們的啟發 — 現代加密通信協議設計
在看了這么多的分析和案例之后,我們已經可以歸納出加密通信協議設計的普遍問題,和常見設計決策,
設計決策點:
四類基礎算法 加密/MAC/簽名/密鑰交換 如何選擇?對稱加密目前毫無疑問應該直接用aead,最佳選擇就是 aes-128-gcm/aes-256-gcm/chacha20-poly1305了數字簽名/驗證方案,如果是移動互聯網,應該考慮直接放棄 RSA,考慮 P-256 的 ECDSA 公鑰證書,或者更進一步的 ed25519 公鑰證書。密鑰交換算法,目前最佳選擇就是 curve25519,或者 P-256。
對稱加密算法+認證算法,如何選擇?或者直接用aead?
簽名算法如何選擇?RSA or ECDSA or Ed25519?
考慮將來的算法調整,要加版本號機制嗎?建議是加上,起碼在密鑰協商的步驟,要加上版本號。便于將來更新算法。
RSA用作密鑰交換是一個好的選擇嗎?考慮PFS建議直接放棄RSA,RSA服務器端性能比ECDSA更差,簽名更大費流量,而且沒有前向安全性,給私鑰保管帶來更大風險。
自建PKI,是個好的選擇嗎?crl如何解決?自建PKI可以做到更安全,比如簡單的客戶端內置數字簽名公鑰??墒钱斝枰o急吊銷一個證書的時候,只能通過緊急發布新版客戶端來解決。
必須用糟糕的openssl嗎?or something better?crypto++,botan, nacl/libsodium, polarssl?libsodium: ed25519+curve2519+chacha20+poly1305
重放攻擊如何解決?某種seq?或者nonce如何生成?
握手過程被中間人篡改的問題怎么解決?
性能:私鑰運算的cpu消耗可以承受嗎?加上某種cache?要解決私鑰運算的高cpu消耗,必然就需要 session ticket/session id 這種cache機制。顯然session ticket 更好
延遲:密鑰協商需要幾個rtt?最少多少?加上cache后?和tcp對比如何
TLS的性能(主要指服務器cpu消耗)還有空間可以壓榨嗎?我能設計一個性能更牛逼的嗎?
七. 附錄:密碼學基礎概念
本文已經很長了,基礎概念的內容更多,再展開介紹就太長了,下面就列一下點,貼一下參考資料,就先這樣,以后再說吧。
當然,最好的資料是下面列的書。
1. 塊加密算法 block cipher
AES 等
《AES后分組密碼的研究現狀 及發展趨勢》http://www.ccf.org.cn/resources/1190201776262/2010/04/15/019026.pdf
aead的介紹(作者是大神)https://www.imperialviolet.org/2015/05/16/aeads.html
3種組合方式之爭http://www.thoughtcrime.org/blog/the-cryptographic-doom-principle/
CBC模式+MAC-then-encrypt的padding oracle 攻擊, tls POODLE 漏洞http://drops.wooyun.org/papers/3194https://defuse.ca/blog/recovering-cbc-mode-iv-chosen-ciphertext.html
128 bit 和 256 bit key size之爭https://www.schneier.com/blog/archives/2009/07/another_new_aes.html
nist 對 aes gcm 的技術標準,官方權威文檔:http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
一個gcm的調用范例https://github.com/facebook/conceal/blob/master/native/crypto/gcm_util.c
DES1天之內破解DES(2008年)http://www.sciengines.com/company/news-a-events/74-des-in-1-day.html
iPhone 5S開始,A7芯片也有了aes硬件指令 (ARMv8 指令集),有825%的性能提升:http://www.anandtech.com/show/7335/the-iphone-5s-review/4
2. 流加密算法 stream cipher
RC4,ChaCha20 等
序列密碼發展現狀http://www.ccf.org.cn/resources/1190201776262/2010/04/15/019018.pdf
rc4 : http://www.rc4nomore.com/
[RC4加密已不再安全,破解效率極高(含視頻)]? http://www.freebuf.com/news/72622.html
3. Hash函數 hash funtion
MD5,sha1,sha256,sha512 , ripemd 160,poly1305 等
MD5被碰撞:http://natmchugh.blogspot.com/2014/10/how-i-created-two-images-with-same-md5.html
http://blog.avira.com/md5-the-broken-algorithm/
4. 消息驗證碼函數 message authentication code
HMAC-sha256,AEAD 等
為什么要用MAChttp://www.happybearsoftware.com/you-are-dangerously-bad-at-cryptography.html
Flickr的漏洞案例:http://netifera.com/research/flickr_api_signature_forgery.pdf
http://www.ietf.org/rfc/rfc2104.txt
5. 密鑰交換 key exchange
DH,ECDH,RSA,PFS方式的(DHE,ECDHE)等
https://blog.cloudflare.com/a-relatively-easy-to-understand-primer-on-elliptic-curve-cryptography/
關于 前向安全性( Perfect Forward Secrecy )http://vincent.bernat.im/en/blog/2011-ssl-perfect-forward-secrecy.html
http://www.cryptopp.com/wiki/Elliptic_Curve_Cryptography
google對openssl里面的橢圓曲線的優化:http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/37376.pdf
http://www.math.brown.edu/~jhs/Presentations/WyomingEllipticCurve.pdf
ripple從nistp256k1曲線遷移到ed25519https://ripple.com/uncategorized/curves-with-a-twist/
openssh 6.5 開始支持 ed25519, curve25519, chacha20-poly1305http://www.openssh.org/txt/release-6.5
6. 公鑰加密 public-key encryption
RSA,rabin-williams 等
RSA入門必讀(斯坦福,普渡的課件):http://crypto.stanford.edu/~dabo/courses/cs255_winter07/rsa.ppthttps://engineering.purdue.edu/kak/compsec/NewLectures/Lecture12.pdf
PKCS1 標準,應用RSA必讀:https://www.ietf.org/rfc/rfc3447
RSA 的公鑰為什么比AES的key長?http://crypto.stackexchange.com/questions/8687/security-strength-of-rsa-in-relation-with-the-modulus-size
http://cryptofails.blogspot.ca/2013/07/saltstack-rsa-e-d-1.html
使用什么padding? OAEP,為什么不要用PKCS V1.5
http://crypto.stackexchange.com/questions/12688/can-you-explain-bleichenbachers-cca-attack-on-pkcs1-v1-5http://en.wikipedia.org/wiki/Adaptive_chosen-ciphertext_attack
PKCS #1 — #15標準協議官方網站:http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/public-key-cryptography-standards.htmhttp://arxiv.org/pdf/1207.5446v1.pdf
blinding 一種實現上的技術,用來解決 timing 側通道攻擊的問題https://en.wikipedia.org/wiki/Blinding_(cryptography)http://crypto.stanford.edu/~dabo/papers/ssl-timing.pdf
Twenty Years of Attacks on the RSA Cryptosystem:http://crypto.stanford.edu/~dabo/papers/RSA-survey.pdf
電子信封(digital envelope)http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/what-is-a-digital-envelope.htm
在openssl的evp接口中有直接支持:https://wiki.openssl.org/index.php/EVP_Asymmetric_Encryption_and_Decryption_of_an_Envelope
7. 數字簽名算法 signature algorithm
RSA,DSA,ECDSA (secp256r1 , ed25519) 等
三大公鑰體制:RSA,DSA,ECDSARSA目前是主流,占據絕大多數市場份額DSA已經被廢棄ECDSA是未來的趨勢,例如bitcoin就用ECDSAhttps://blog.cloudflare.com/ecdsa-the-digital-signature-algorithm-of-a-better-internet/https://blog.cloudflare.com/a-relatively-easy-to-understand-primer-on-elliptic-curve-cryptography/
8. 密碼衍生函數 key derivation function
TLS-12-PRF(SHA-256) , bcrypto,scrypto,pbkdf2 等
hkdf:? ? http://tools.ietf.org/html/rfc5869https://cryptography.io/en/latest/hazmat/primitives/key-derivation-functions/
9. 隨機數生成器 random number generators
/dev/urandom 等
[現代密碼學實踐指南[2015年]]? https://blog.helong.info/blog/2015/06/05/modern-crypto/
八. 參考文獻:
TLS/SSL 相關RFC及標準
[TLS 1.2] 鏈接 https://tools.ietf.org/html/rfc5246
[TLS 1.3 draft specification] 鏈接 https://github.com/tlswg/tls13-spec
[AES GCM for TLS] 鏈接 https://tools.ietf.org/html/rfc5288
[ECC cipher suites for TLS] 鏈接 https://tools.ietf.org/html/rfc4492
[TLS extensions] 鏈接 https://tools.ietf.org/html/rfc6066
[Application-Layer Protocol Negotiation Extension] 鏈接 https://tools.ietf.org/html/rfc7301
[X.509 PKI] 鏈接 https://tools.ietf.org/html/rfc4210
[X.509 PKI and CRLs] 鏈接 https://tools.ietf.org/html/rfc5280
[美國國家標準局NIST 的密碼學標準] 鏈接 http://csrc.nist.gov/groups/ST/toolkit/index.html
[NIST SP 800-90A ] 鏈接 http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
[nsa 的 SuiteB 密碼學標準] 鏈接 https://www.nsa.gov/ia/programs/suiteb_cryptography/
[TLS on wikipedia] 鏈接 https://en.wikipedia.org/wiki/Transport_Layer_Security
協議分析文章
http://www.root.org/talks/TLS_Design20071129_2.pdf
[20 Years of SSL/TLS Research An Analysis of the Internet’s Security Foundation] 鏈接 http://www-brs.ub.ruhr-uni-bochum.de/netahtml/HSS/Diss/MeyerChristopher/diss.pdf
https://www.slideshare.net/yassl/securing-data-in-transit
http://security.stackexchange.com/questions/20803/how-does-ssl-tls-work
[SSL/TLS in Detail] 鏈接 https://technet.microsoft.com/en-us/library/cc785811(v=ws.10).aspx
[SSL/TLS] 鏈接 http://www.spiegel.de/media/media-35511.pdf
[The Sorry State Of SSL] 鏈接 https://hynek.me/talks/tls/
[What’s the matter with TLS?] 鏈接 http://hyperelliptic.org/internetcrypto/OpenSSLPresentation.pdf
http://blog.csdn.net/CaesarZou/article/details/9331993
)
[X.509 PKI and CRLs] 鏈接 https://tools.ietf.org/html/rfc5280
[Layman’s Guide to ASN.1] 鏈接 http://luca.ntop.org/Teaching/Appunti/asn1.html
實際部署調優相關
https://www.imperialviolet.org/
http://chimera.labs.oreilly.com/books/1230000000545/ch04.to/crypto.html
[RSA Conference 2015 : New Trends In Cryptographic Algorithm Suites Used For TLS Communications] 鏈接 http://www.rsaconference.com/writable/presentations/file_upload/dsp-f03-new-trends-in-cryptographic-algorithm-suites-used-for-tls-communications.pdf
密碼學相關
[Stanford Cryptography open course] 鏈接 https://www.coursera.org/course/crypto
[crypto101,一本很棒的開源電子書] 鏈接 http://crypto101.io
[現代密碼學理論與實踐] 鏈接 https://book.douban.com/subject/1172109/ - 毛文波
[現代密碼學:原理與協議] 鏈接 http://book.douban.com/subject/5954556/ - Katz and Lindell
[“Modern Crypto: 15 Years of Advancement in Cryptography”? — 2015 defcon 大會Steve Weis 演講] 鏈接 http://saweis.net/pdfs/weis-modern-crypto-defcon-2015.pdf
強烈建議不要看90年代的書,普遍嚴重過時,比如《應用密碼學:協議、算法與C源程序(原書第2版)》
[DJBs crypto page] 鏈接 http://cr.yp.to/crypto.html
[DJBs entropy attacks] 鏈接 http://blog.cr.yp.to/20140205-entropy.html
[Cryptographic Right Answers] 鏈接 https://gist.github.com/tqbf/be58d2d39690c3b366ad
http://www.slideshare.net/yassl/securing-data-in-transit
[Schneier 關于密碼學2010年現狀的評述] 鏈接 <https://www.schneier.com/blog/archives/2013/07/is_cryptography.html
http://kodu.ut.ee/~swen/publications/articles/laur-thesis-binded.pdf
https://github.com/sweis/crypto-might-not-suck
[Cryptographic Best Practices in the Post-Snowden Era] 鏈接 http://pages.uoregon.edu/joe/crypto-bcp/crypto-bcp.pdf
[Crypto War] 鏈接 http://en.wikipedia.org/wiki/Crypto_Wars
[52 Things People Should Know To Do Cryptography] 鏈接 http://www.cs.bris.ac.uk/Research/CryptographySecurity/knowledge.html
http://bristolcrypto.blogspot.com/
https://www.imperialviolet.org/2015/05/16/aeads.html
https://crypto.stanford.edu/~dabo/cryptobook/draft_0_2.pdf
http://saweis.net/pdfs/weis-modern-crypto-defcon-2015.pdf
相關開源項目
[GoTLS] 鏈接 http://golang.org/pkg/crypto/tls/ - go語言自己搞的 TLS 協議實現
[OpenSSL] 鏈接 https://www.openssl.org/ - 事實上的標準
[LibreSSL] 鏈接 http://www.libressl.org/ - OpenBSD搞的OpenSSL的分支,代碼可讀性大大提高
[BoringSSL] 鏈接 https://boringssl.googlesource.com/boringssl/ - Google Security team 維護的OpenSSL分支
[NSS] 鏈接 https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS - Mozilla 維護的TLS協議實現
[s2n] 鏈接 https://github.com/awslabs/s2n/ - Amazon搞的tls協議實現
[MiTLS] 鏈接 http://www.mitls.org/wsgi/home ,? [TLS Attacks] 鏈接 http://www.mitls.org/wsgi/tls-attacks
[NaCL] 鏈接 http://nacl.cr.yp.to/ and [libsodium] 鏈接 https://github.com/jedisct1/libsodium
[spiped] 鏈接 http://www.tarsnap.com/spiped.html
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 本文轉自微信后臺團隊,如有侵犯,請聯系我們立即刪除
OpenIMgithub開源地址:
https://github.com/OpenIMSDK/Open-IM-Server
OpenIM官網 : https://www.rentsoft.cn
OpenIM官方論壇: https://forum.rentsoft.cn/
更多技術文章:
開源OpenIM:高性能、可伸縮、易擴展的即時通訊架構https://forum.rentsoft.cn/thread/3
【OpenIM原創】簡單輕松入門 一文講解WebRTC實現1對1音視頻通信原理https://forum.rentsoft.cn/thread/4
【OpenIM原創】開源OpenIM:輕量、高效、實時、可靠、低成本的消息模型https://forum.rentsoft.cn/thread/1