1. 程式人生 > >向openssl原始碼新增SHA-512/224 and SHA-512/256演算法

向openssl原始碼新增SHA-512/224 and SHA-512/256演算法

首先,參考NIST.FIPS.180-4協議文件理解SHA-512/224 SHA-512/256和SHA-512的區別。協議指出,SHA-512/224演算法步驟其實和SHA-512一樣,只有兩處例外:

  • 1.初始向量不同
  • 2.需要截位,取最左邊的224bits
具體初始向量列舉如下:

For SHA-512/224, the initial hash value, H(0), shall consist of the following eight 64-bit words,
in hex:



These words were obtained by executing the SHA-512/t IV Generation Function with t = 224

For SHA-512/256, the initial hash value, H(0), shall consist of the following eight 64-bit words,
in hex:



These words were obtained by executing the SHA-512/t IV Generation Function with t = 256.

其次,參考openssl SHA-512演算法實現,可以看到SHA512演算法主要有三個函式實現:

SHA512_Init

SHA512_Update

SHA512_Final


再看SHA-384演算法實現,發現SHA-384也是呼叫的SHA-512演算法實現的。除了初始向量不同,SHA-384呼叫的是SHA384_Init函式。SHA512_Final中有關於SHA384的截位的分支。

參考以上分析,我們要實現SHA-512/t演算法,無非就是自定義兩個函式:SHA512T224_Init, SHA512T256_Init,修改一個函式SHA512_Final中新增關於SHA-512/224 SHA-512/256的分支。

最後,就是講SHA-512/t演算法,新增進EVP實現中。參考SHA512和SHA384 不難實現,自定義兩個資料結構就可以。


至於怎麼新增SHA-512/224 and SHA-512/256 的OID,分四步:

在crypto/objects下操作

  1. 在objetcts.txt 新增對應的OID描述
  2. perl objects.pl objects.txt obj_mac.num obj_mac.h
  3. perl obj_dat.pl obj_mac.h obj_dat.h
  4. obj_mac.h拷貝到include/openssl下。

參考doc/openssl/INSTALL, 重新編譯openssl即可。

Object identifiers (OIDs) for the SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224 and SHA-512/256 algorithms are posted at http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html.


PKCS #1 v2.2: RSA 標準中有關於hash演算法對應OID的定義,可以參考下圖來修改openssl 中object.txt中的SHA-512/224 and SHA-512/256的oid定義


驗證摘要演算法是否正確:

用perl5提供的Digest package對明文"abc" 進行摘要運算,


對openssl新新增SHA512T224演算法進行驗證:

產生的digest為:4634270f707b6a54daae7530460842e20e37ed265ceee9a43e8924aa

和perl5 sha512224_hex函式產生的結果一樣。

Done!