向openssl源码添加SHA-512/224 and SHA-512/256算法

来源:互联网 发布:云计算岗位多吗 编辑:程序博客网 时间:2024/05/21 02:33

首先,参考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!

原创粉丝点击