本文是加密系列中的第五篇,前文请参考如下链接:
前面几篇文章从对称加密一直讲到了非对称加密以及混合加密。加密技术与认证技术通常是紧密联系在一起的,很多嵌入式工程师对加解密多多少少会有一些了解,但是对散列、数字签名、证书这些互联网中常见的名词却是一头雾水。因此从本篇文章开始,我们将进入本系列文章的第二个部分:认证技术体系。
什么是认证?
关于什么是认证,我觉得还是举几个例子讲更容易被人理解:“认证”就是证明“你是谁”,或者证明“这个东西是谁的”,又或者证明"这个文件有没有被篡改过"。这些问题看似简单,其实挺有难度,尤其是在毫无信任度的开放网络世界。
二进制数据的指纹:单向散列函数
在开放的网络世界,我们经常需要判断自己所接收到的软件或者数据是否被篡改过。这个工作就可以交由单向散列函数进行。
单向散列函数(one-way hash function)将不同的输入值,进行计算后,得到相同固定长度的不同的输出值。这个输入值称为消息(message),输出值称为散列值(hash value)。
散列值的长度与消息长度(单向散列函数的输入)无关。以常见单向散列函数MD5为例,MD5的输出永远是128比特(16个字节)。另外,输入的信息不同,所产生的散列值也不同。因此一旦信息遭到篡改,那么对应的散列值就会发生变化,根据这个特点,信息的发布者在发布信息的同时也会将散列值一同发布出来。
有人会好奇,如果攻击者将信息篡改的同时,也将散列值篡改成假信息的散列值,然后伪装成真正的发送者发送给接收者,不就还是可以绕过检查吗?这个想法非常正确,所以单向散列函数的用途是防止篡改,而不是防止伪装。单向散列函数只能证明信息与散列值之间是否匹配,只能用于检测信息的完整性。
单向散列函数主要特点如下:
【1】散列值长度固定。
【2】消息不同散列值也不同。两个消息只要有一个比特不同,散列值也会产生很大差异。
【3】抗碰撞性。两个不同的消息产生相同的散列值,这种情况叫碰撞。散列算法需要确保不能被人为的发现或制造碰撞。
【4】单向性。所谓单向就是指:不能由散列值(输出)反向算出信息(输入)。
常见的单向散列算法
MD4: 128比特散列值,碰撞性已经被攻破,很少看见有人使用。
MD5: 128比特散列值,碰撞性也已被攻破,但是仍被广泛使用。
SHA-1:160比特散列值,碰撞性已经被攻破,但是仍被广泛使用。
SHA-256:256比特散列值。
SHA-384:384比特散列值。
SHA-512:512比特散列值。
其中SHA-256、SHA-384和SHA-512统称为SHA-2。
<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="2" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="3" target="13" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="3" value="12B 用户密码" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1"><mxGeometry x="80" y="610" width="120" height="30" as="geometry"/></mxCell><mxCell id="4" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="5" target="15" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="5" value="1MB 图片文件" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1"><mxGeometry x="80" y="660" width="120" height="30" as="geometry"/></mxCell><mxCell id="6" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="7" target="19" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="7" value="1GB 软件安装包" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1"><mxGeometry x="80" y="760" width="120" height="30" as="geometry"/></mxCell><mxCell id="8" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="9" target="17" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="9" value="100M 软件源码" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1"><mxGeometry x="80" y="710" width="120" height="30" as="geometry"/></mxCell><mxCell id="10" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="11" target="21" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="11" value="......." style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1"><mxGeometry x="80" y="810" width="120" height="30" as="geometry"/></mxCell><mxCell id="12" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="13" target="22" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="13" value="MD5" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1"><mxGeometry x="280" y="610" width="120" height="30" as="geometry"/></mxCell><mxCell id="14" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="15" target="23" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="15" value="MD5" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1"><mxGeometry x="280" y="660" width="120" height="30" as="geometry"/></mxCell><mxCell id="16" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="17" target="24" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="17" value="MD5" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1"><mxGeometry x="280" y="710" width="120" height="30" as="geometry"/></mxCell><mxCell id="18" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="19" target="25" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="19" value="MD5" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1"><mxGeometry x="280" y="760" width="120" height="30" as="geometry"/></mxCell><mxCell id="20" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" source="21" target="26" parent="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="21" value="MD5" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1"><mxGeometry x="280" y="810" width="120" height="30" as="geometry"/></mxCell><mxCell id="22" value="16B 散列值" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1"><mxGeometry x="470" y="610" width="120" height="30" as="geometry"/></mxCell><mxCell id="23" value="16B 散列值" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1"><mxGeometry x="470" y="660" width="120" height="30" as="geometry"/></mxCell><mxCell id="24" value="16B 散列值" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1"><mxGeometry x="470" y="710" width="120" height="30" as="geometry"/></mxCell><mxCell id="25" value="16B 散列值" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1"><mxGeometry x="470" y="760" width="120" height="30" as="geometry"/></mxCell><mxCell id="26" value="16B 散列值" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1"><mxGeometry x="470" y="810" width="120" height="30" as="geometry"/></mxCell></root></mxGraphModel>
%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%22%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3B%22%20edge%3D%221%22%20source%3D%223%22%20target%3D%225%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%223%22%20value%3D%22%E6%B6%88%E6%81%AF%22%20style%3D%22rounded%3D0%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3BfillColor%3D%23dae8fc%3BstrokeColor%3D%236c8ebf%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22290%22%20y%3D%22200%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%224%22%20value%3D%22%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3B%22%20edge%3D%221%22%20source%3D%225%22%20target%3D%227%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%225%22%20value%3D%22%E5%8D%95%E5%90%91%E6%95%A3%E5%88%97%E5%87%BD%E6%95%B0%22%20style%3D%22rounded%3D0%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3BfillColor%3D%23ffe6cc%3BstrokeColor%3D%23d79b00%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22290%22%20y%3D%22320%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%226%22%20value%3D%22%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3Bshape%3Dlink%3Bdashed%3D1%3BdashPattern%3D1%202%3B%22%20edge%3D%221%22%20source%3D%227%22%20target%3D%228%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%227%22%20value%3D%22%E6%95%A3%E5%88%97%E5%80%BC%22%20style%3D%22rounded%3D0%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3BfillColor%3D%23d5e8d4%3BstrokeColor%3D%2382b366%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22290%22%20y%3D%22440%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%228%22%20value%3D%22%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E6%8D%AE%EF%BC%8C%E5%9B%BA%E5%AE%9A%E9%95%BF%E5%BA%A6%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3D%23d79b00%3BfillColor%3D%23ffe6cc%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22470%22%20y%3D%22455%22%20width%3D%22150%22%20height%3D%2230%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%229%22%20value%3D%22%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3Bshape%3Dlink%3Bdashed%3D1%3BdashPattern%3D1%202%3B%22%20edge%3D%221%22%20source%3D%2210%22%20target%3D%223%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%2210%22%20value%3D%22%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E6%8D%AE%EF%BC%8C%E4%BB%BB%E6%84%8F%E9%95%BF%E5%BA%A6%22%20style%3D%22text%3Bhtml%3D1%3BstrokeColor%3D%23d79b00%3BfillColor%3D%23ffe6cc%3Balign%3Dcenter%3BverticalAlign%3Dmiddle%3BwhiteSpace%3Dwrap%3Brounded%3D0%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22470%22%20y%3D%22215%22%20width%3D%22140%22%20height%3D%2230%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphMode