跳至主要内容

白话 IT 之 聊聊数据加密

原文地址白话 IT 之 聊聊数据加密,作者是 嘀嗒嘀嗒 微信公众号运营者angela,表示感谢

嘀嗒嘀嗒按:今天的分享来自我们 Airbnb 的数据加密的大牛桑立锋。说到桑同学,那比我牛出不止一个数量级了。俄亥俄州立大学毕业的 PhD,曾在Yahoo!、LinkedIn 等多家公司担任技术骨干。在过去三四年间获各种数据加密相关的专利达五项之多。

除了技术扎实,桑同学平时也是极好相处,又特别愿意帮助别人。在我过去和他不多的几次合作中,也不时又机会请教一二。虽然对于数据加密仍然是个门外汉,但是桑同学解释问题总能听得懂。因此在我的再三请求下,有了这篇科普性的文章。

虽然我们都就职于 Airbnb,但是这篇文章和 Airbnb 现在使用的技术没有直接关联。下面是正文。



数据加密是一个古老的问题,但又是一个无法做到完美的问题,尤其在一个复杂的大型系统中需要考虑方方面面的问题,包括 security, availability, usability, consistentcy, performance,extensibility 等等。除了那些完全不做的,大部分公司都会根据自身的需求,环境,资源以及工程师的能力等,设计和开发适合公司实际情况的解决方案。有做的好的,也有坦诚做的不好的,还有自己认为做的好的但其实经不起真正考验的。各种各样的都有。

就算在同一个公司,很多也会有不同的解决方案并存。有些是因为历史原因,比方说早期用方案A,后来开发了更牛逼的方案B,但没有把A完全干掉;或者扩张原因,比方说兼并了一个用不同方案的别家公司;又或者是政治原因,不同部门谁都不屌谁,各自用自认为合适的方案。  无论公司选择什么方案,因为话题的敏感性(法律原因,或者圈内的潜规则,或者仅仅是不想当出头鸟而被黑客们盯上),绝大部分都不会公开数据加密的细节。同样原因,这篇文章也不会涉及笔者公司怎么做数据加密的具体细节。想到哪儿写到哪儿吧。


先说为什么要做数据加密。越来越多的数据泄露事件,比方说 Yahoo 2013年被盗超过10亿用户信息 [链接1], Yahoo 2014年又被盗超过5亿用户信息 [链接2], LinkedIn 被盗一亿多用户密码 [链接3],Ashley Madison 被盗三千多万用户数据以及大量支付信息 [链接4],Target 被盗近七千万用户数据和银行账号 [链接5],Adobe 被盗三千八百万用户数据 [链接6] 等等,以及由此导致的大量法律纠纷和巨额赔偿,还有很多很多大大小小没有被公布的安全事件,都说明了数据加密和保护的重要性。

说到这些安全事件中用户密码的保护,插个题外话。笔者曾经跟很多工程师(包括很多应用领域专家)聊天的时候,都会被问到,为什么笔者认为仅仅 Hash 用户密码是不够的。很多人(包括网上的很多文章)都误认为用户密码只要 Hash了,就安全了,其实不然。不说 weak Hash 函数诸如 RC4 [链接7], MD5 [链接8]就算用 Bcrypt [链接9],KDF [链接10] 等运算复杂的Hash函数,虽然能防 Rainbow Table Attack [链接11],但对 Dictionary Attack [链接12] 却是无效的。当然这不是 Hash 函数本身的错,而是很多人会选一个容易记的密码,而这类密码往往 entropy 不够,很容易被解密。对用户密码而言,笔者一直建议不仅需要Hash,还要加密 (比方说用 keyed hash function,or MAC) [链接13]

还有很多公司要做加密不仅仅是因为保护用户隐私,更是法律法规的要求,不得不做。有些涉及特殊数据,如信用卡号码,那就要做 PCI (The Payment Card Industry Data Security Standard );如用户健康信息,那就要做 HIPAA (The Health Insurance Portability and Accountability Act)等等。

在笔者看来,任何一个收集以及存储客户数据的公司,就算是初创公司,都应该认真对待这个问题。在圈内我们常说,不是系统会不会黑,数据会不会被盗,而是何时被黑被盗的问题。也许有人会说,就算偷了,盗了,那又如何?这种事可大可小,笔者私下就知道有公司被黑后,被迫关门了的。所以安全领域内的及时投资,对公司长期来讲都是非常有益的。对重要数据(包括系统密码,用户信息等)进行有效保护,数据被黑被盗的门槛就高了;就算有一天系统被黑了,数据被盗了,也能把损失降到最小。


你说数据加密这么重要,怎么做呢?如果把加密这个问题抽象出来,其实就是要计算一个加密函数:
encrypt(data, key)
(当然还有一个函数就是解密,跟加密类似道理,暂且不论)。看上去似乎是一个非常简单的问题,但要把它做好非常不容易。尤其是在一个系统复杂的公司,要考虑的问题很多很多。比方说,密码界有很多算法,应该用什么加密算法,对称的还是非对称的?具体选哪个,AES,DES,RSA,ECC等等,各有什么特点?题外话,笔者在曾经工作过的公司见过很多有趣的例子,比方说有些早期开发人员用 XOR 来加密,或者做点简单的迭代替换,或者自创所谓的加密算法(有点掩耳盗铃的感觉)等等,这些最后都变成 technical debt,需要花很大力气去清理。

每个算法也有不同的变种和模式,各有什么特点,性能如何?如果某个算法被宣布不安全了,如何快速迭代?这个 key 怎么来?多长才是安全的?怎么启动?怎么保存?怎么传播?怎么控制访问权限?怎么知道谁访问了什么?怎么来监控?怎么来预警?怎么来系统性的更新这些 key?key 能不能丢,丢了怎么办?不同的应用可能是用不同的语言写的,怎么兼容?怎么支持大流量等等,等等。还有更加不近人情的要求,比方说如何保证被加密的数据能够 preserve 原始数据的顺序,支持搜索,但又不牺牲安全性等(这方面 MIT 有学者在研究,有兴趣的朋友可以看看他们的论文)。

一个好的加密设计方案,不仅仅方案本身要满足安全上严格的要求,解决上面提到的很多问题,还需要实用,容易扩展和维护。在数据加密变得越来越重要,系统越来越复杂的年代,如果资源允许,应该把加密服务独立出来,然后提供高性能,统一,简单又容易理解的接口来进行数据加密,让应用开发人员很方便的使用,这样他们只需要专注他/她所擅长的领域,而不需要去思考怎么解决安全问题,因为术业有专攻。好的加密方案应该把数据和秘钥的存储分开,并且把存储和运算分开,尤其在 SOA 架构下, 这可能跟很多传统的数据加密方法(比方说直接实用某个语言的内置库加密解密)非常不一样。很多解决方案往往密钥和被加密的数据同时存在一个服务中,结果就是如果那个服务被黑了,那就整个被黑了。下面就捡几个要点简略讲讲。

(1)为什么要把运算和存储的分离?很多需要被加密的数据,往往和具体商业逻辑数据一起,属于不同的服务,比方说支付信息属于支付服务,护照号码属于用户服务等。把运算和存储分离可以带来很多好处,比方说:
  • 加密服务变得简单和高效。因为不需要存储那些被加密的数据,加密服务系统不会很复杂,也不需要负责存储系统所带来的维护,扩展等诸多问题;
  • 加密服务的安全性能提高很多。因为运算和存储的分离,如果仅仅是被加密的数据泄露(比方说数据库被盗),那些数据就没法被解密,因为黑客没法从外部访问加密服务。如果仅仅是客户服务被黑,想要盗取大量数据并且通过加密服务来解密而不被发现也很难。如果仅仅是加密服务服务被黑,因为加密服务本身并不拥有数据,被泄露的数据也不会很多。只有当加密服务和客户服务同时被黑,才会泄露大量数据,而同时能够侵入加密服务和客户服务的难度要高很多很多;
  • 灵动性。因为数据属于客户服务,不同的客户可以对数据进行不同的处理,比方说不同的有效性规则,数据交易完整性等等。

(2)其次是 granular control。假设支付服务要求加密/解密信用卡号码,用户服务要求加密/解密护照号码,如何能保证用户服务不能加密/解密信用卡号码。这就需要解决两个基本问题:客户认证(authentication)和权限控制(authorization)。怎么做客户认证(authentication)?客户认证要知道每一个请求是谁发出的。因为只有知道客户是谁才可以进行权限检查。常用的有基于客户证书的(如 client certificate over TLS),基于 OAuth(开放授权)的,或者各种各样定制的方案,如基于 Curve25519 [链接14] 等等。笔者一般遵循两个原则:
  1. 是不是业界公认的。如果不是,最好避免。业界公认的解决方案往往经过过严格的检验, 评价,批评,经得起考验;
  2. 有没有广泛的类库支持,以及多语言的支持。如果有,可以节省大量的开发以及维护的时间和精力。

(3)数据监控和预警。为了审计以及安全的需求,一般要对加密服务做很多的数据监控,预警的工作。这样可以知道谁在访问这些数据,何时访问的,怎么访问的,访问的模式是怎么样的。监控系统还需要侦测异常的流量变化,进行流量控制以及快速的反攻击保护。

(4)最后,重中之重,怎么保护 root key?无论是用哪种 envelope encryption的变种,都会涉及到怎么保护 root key。这是一个很有意思的话题,但篇幅关系就不展开来讲了。常见的有用 secret sharing [链接15] 的一些变种方式,也有通过公证人公证整个过程藏在银行保险箱的,也有藏在创始人地下室的:)怎么来 bootstrap 和 deploy 这个 root key 到加密服务里也是一个非常有意思和挑战的问题。

总的来说,数据加密不是一件神秘的事情,但要做好非常不容易,需要一定的技术积累和资源的投入。做任何一个安全系统,风险都不小。圈内人第一反应是怀疑,因为职业病;圈外人很多也觉得重要,但不关心,或者说不知道怎么关心。但不管如何,无论是因为潜在的法律风险还是用户的信任风险,这种安全上的投入从长期来说一定是值得的。


链接1:http://money.cnn.com/2016/12/14/technology/yahoo-breach-billion-users/
链接2:http://money.cnn.com/2016/09/22/technology/yahoo-data-breach/?iid=EL
链接3:https://techcrunch.com/2016/05/18/117-million-linkedin-emails-and-passwords-from-a-2012-hack-just-got-posted-online/
链接4:https://www.wired.com/2015/08/happened-hackers-posted-stolen-ashley-madison-data/
链接5:http://money.cnn.com/2015/10/14/pf/target-pin-credit-card/index.html?iid=EL
链接6:http://krebsonsecurity.com/2013/10/adobe-breach-impacted-at-least-38-million-users/
链接7:https://en.wikipedia.org/wiki/RC4
链接8:https://en.wikipedia.org/wiki/MD5
链接9:https://en.wikipedia.org/wiki/Bcrypt
链接10:https://en.wikipedia.org/wiki/Key_derivation_function
链接11:https://en.wikipedia.org/wiki/Rainbow_table
链接12:https://en.wikipedia.org/wiki/Dictionary_attack
链接13:https://en.wikipedia.org/wiki/Message_authentication_code
链接14:https://en.wikipedia.org/wiki/Curve25519
链接15:https://en.wikipedia.org/wiki/Secret_sharing

Popular posts from 产品随想的博客

Interview at the All Things Digital D5 Conference, Steve and Bill Gates spoke with journalists Kara Swisher and Walt Mossberg onstage in May 2007.

Kara Swisher: The first question I was interested in asking is what you think each has contributed to the computer and technology industry— starting with you, Steve, for Bill, and vice versa. Steve Jobs: Well, Bill built the first software company in the industry. And I think he built the first software company before anybody really in our industry knew what a software company was, except for these guys. And that was huge. That was really huge. And the business model that they ended up pursuing turned out to be the one that worked really well for the industry. I think the biggest thing was, Bill was really focused on software before almost anybody else had a clue that it was really the software that— KS: Was important? SJ: That’s what I see. I mean, a lot of other things you could say, but that’s the high-order bit. And I think building a company’s really hard, and it requires your greatest persuasive abilities to hire the best ...

Commencement Address at Stanford University--“Stay hungry. Stay foolish.”

I am honored to be with you today for your commencement from one of the finest universities in the world. Truth be told— I never graduated from college. This is the closest I’ve ever gotten to a college graduation. Today I want to tell you three stories from my life. That’s it. No big deal. Just three stories. The first story is about connecting the dots. I dropped out of Reed College after the first six months but then stayed around as a drop-in for another eighteen months or so before I really quit. So why did I drop out? It started before I was born. My biological mother was a young, unwed graduate student, and she decided to put me up for adoption. She felt very strongly that I should be adopted by college graduates, so everything was all set for me to be adopted at birth by a lawyer and his wife. Except that when I popped out they decided at the last minute that they really wanted a girl. So my parents, who were on a waiting...

产品随想 | 周刊 第43期:历史上的今天

Products Huberman Lab   https://hubermanlab.com/ 一款聚焦于健康的播客 今日热榜   https://tophub.today/ 聚合展示,国内各热门榜单,对跟进热点非常有帮助,热点运营的好帮手 SketchyBar   https://github.com/FelixKratz/SketchyBar A highly customizable macOS status bar replacement Mac菜单栏定制 自定义程度很高,看作者展示的案例,暂时没想出这样的好处(不过应用本身的编辑,确实也没啥意义)生命在于折腾吧! Thanks-Mirror   https://github.com/eryajf/Thanks-Mirror 整理记录各个包管理器,系统镜像,以及常用软件的好用镜像,Thanks Mirror。 Musicn   https://github.com/zonemeen/musicn 一个下载高品质音乐的命令行工具,音乐来源: 咪咕 Planet Minecraft A creative Minecraft community fansite sharing maps, minecraft skins, resource packs, servers, mods, and more. 里面有很多动人的故事 可能是世界上最大的Minecraft社区,从2010年至今 The Uncensored Library   https://www.uncensoredlibrary.com/en blockworks   https://www.blockworks.uk/ "Distinctive maps for Minecraft that have educated players and risen to the level of art" 游戏也可以让人有更高的实现,而不仅仅是沉迷其中,国外游戏厂商比我们做的好太多 Minecraft_Memory_Bypass_GUI   https://github.com/xingchuanzhen/Minecraft_Memory_Bypass_GUI 绕过Minecraft...

巴菲特致股东信-1976年

 笔记: 为什么选择轻资产行业:当竞争疯狂时,不会强迫加入降价大战 最终选择了费雪的思想,选择能理解的优秀企业,以合理的价格买入并长期拥有 翻译: 雪球:https://xueqiu.com/6217262310/131440258 备份:https://archive.ph/XLK0S 原文: To the Stockholders of Berkshire Hathaway Inc, After two dismal years, operating results in 1976 improved significantly. Last year we said the degree of progress in insurance underwriting would determine whether our gain in earnings would be "moderate" or "major." As it turned out, earnings exceeded even the high end of our expectations. In large part, this was due to the outstanding efforts of Phil Liesche's managerial group at National Indemnity Company. In dollar terms, operating earnings came to $16,073,000, or $16.47 per share. While this is a record figure, we consider return on shareholders' equity to be a much more significant yardstick of economic performance. Here our result was 17.3%, moderately above our long-term average and even further above the average o...

巴菲特致股东信-1973年

 笔记: 在上一年度预测的今年竞争加剧导致利润下滑,真的发生了 翻译Link: 雪球:https://xueqiu.com/6217262310/131257618 备份:https://archive.ph/KIfdT 原文: To the Stockholders of Berkshire Hathaway Inc.: Our financial results for 1973 were satisfactory, with operating earnings of $11,930,592, producing a return of 17.4% on beginning stockholders' equity. Although operating earnings improved from $11.43 to $12.18 per share, earnings on equity decreased from the 19.8% of 1972. This decline occurred because the gain in earnings was not commensurate with the increase in shareholders' investment. We had forecast in last year's report that such a decline was likely. Unfortunately, our forecast proved to be correct. Our textile, banking, and most insurance operations had good years, but certain segments of the insurance business turned in poor results. Overall, our insurance business continues to be a most attractive area in which to employ capital. Management'...

巴菲特致股东信-1975年

 笔记: 华盛顿邮报已成为伯克希尔第一重仓股 翻译: 雪球:https://xueqiu.com/6217262310/131409324 备份:https://archive.ph/4hgK3 原文: To the Stockholders of Berkshire Hathaway Inc.: Last year, when discussing the prospects for 1975, we stated “the outlook for 1975 is not encouraging.” This forecast proved to be distressingly accurate. Our operating earnings for 1975 were $6,713,592, or $6.85 per share, producing a return on beginning shareholders ’ equity of 7.6%. This is the lowest return on equity experienced since 1967. Furthermore, as explained later in this letter, a large segment of these earnings resulted from Federal income tax refunds which will not be available to assist performance in 1976. On balance, however, current trends indicate a somewhat brighter 1976. Operations and prospects will be discussed in greater detail below, under specific industry titles. Our expectation is that significantly better results in textiles, earnings added from recent acquisitio...

Steve Jobs introduced the iPhone on January 9, 2007.

This is a day I’ve been looking forward to for two and a half years. Link Every once in a while, a revolutionary product comes along that changes everything. And Apple has been— well, first of all, one’s very fortunate if you get to work on just one of these in your career. Apple’s been very fortunate. It’s been able to introduce a few of these into the world. In 1984, we introduced the Macintosh. It didn’t just change Apple, it changed the whole computer industry. In 2001, we introduced the first iPod, and it didn’t just change the way we all listen to music, it changed the entire music industry. Well, today, we’re introducing three revolutionary products of this class. The first one is a widescreen iPod with touch controls. The second is a revolutionary mobile phone. And the third is a breakthrough internet communications device. So, three things: a widescreen iPod with touch controls; a revolutionary mobile phone; and a breakthrough internet communicat...

产品随想 | 周刊 第90期:史家之绝唱,无韵之离骚

Why AI Will Save the World   https://a16z.com/2023/06/06/ai-will-save-the-world/ Marc Andreessen的雄文,十分有說服力,邏輯清晰 辯證了現今AI監管拋出的5個可能的AI問題 讀的過程中,腦海裏浮現的都是編程隨想那篇文章 为什么马克思是错的?——全面批判马列主义的知名著作导读   https://program-think.blogspot.com/2018/09/Book-Review-The-Errors-of-Marxism-Leninism.html 兩者的思維鏈條、敘事方式,非常相似 人民聖殿教   https://zh.wikipedia.org/zh-hk/人民圣殿教?useskin=vector 瓊斯自稱是神的化身,幾千年前轉世為釋迦牟尼,創建了佛教;後來又轉世為耶穌基督,創建了基督教;之後短期化身轉世為巴孛,建立巴哈伊信仰;最後轉世為列寧,將社會主義發揚光大。 邪教徒聲稱自己轉世成了列寧,這說明了什麼? Apple Vision   https://stratechery.com/2023/apple-vision Omnivore   https://github.com/omnivore-app/omnivore Omnivore is a complete, open source read-it-later solution for people who like reading. How the YouTube Algorithm Works in 2023: The Complete Guide   https://blog.hootsuite.com/how-the-youtube-algorithm-works/#A_brief_history_of_the_YouTube_algorithm 外人眼中的YouTube推薦算法變遷 Histography   https://histography.io/ “Histography" is interactive timeline that spans across 14 billion years of history, f...

巴菲特致股东信-1974年

 笔记: 价格战企业的逻辑:需要降价获取销量--->需要降低成本--->怎么降?扩大规模以摊低成本--->提高固定资产投入--->净资产回报率会降低 翻译: 雪球:https://xueqiu.com/6217262310/131257947 备份:https://archive.ph/5CEP6 原文: To the Stockholders of Berkshire Hathaway Inc.: Operating results for 1974 overall were unsatisfactory due to the poor performance of our insurance business. In last year's annual report some decline in profitability was predicted but the extent of this decline, which accelerated during the year, was a surprise. Operating earnings for 1974 were $8,383,576, or $8.56 per share, for a return on beginning shareholders' equity of 10.3%. This is the lowest return on equity realized since 1970. Our textile division and our bank both performed very well, turning in improved results against the already good figures of 1973. However, insurance underwriting, which has been mentioned in the last several annual reports as running at levels of unsustainable profitability, turned dramatically worse...

Interview with Steve Jobs, WGBH, 1990

Interviewer: what is it about this machine? Why is this machine so interesting? Why has it been so influential? Jobs: Ah ahm, I'll give you my point of view on it. I remember reading a magazine article a long time ago ah when I was ah twelve years ago maybe, in I think it was Scientific American . I'm not sure. And the article ahm proposed to measure the efficiency of locomotion for ah lots of species on planet earth to see which species was the most efficient at getting from point A to point B. Ah and they measured the kilocalories that each one expended. So ah they ranked them all and I remember that ahm...ah the Condor, Condor was the most efficient at [CLEARS THROAT] getting from point A to point B. And humankind, the crown of creation came in with a rather unimpressive showing about a third of the way down...