ios平台上国密sm3的实现,iphone4s,5,6,6s验证通过

本文介绍在iOS设备上实现国密SM3算法的过程,针对goldboar版本存在的问题进行修正,特别是在iPhone 6s上由于long类型字节数导致的错误。提供了解决方案,包括修改数据类型为uint32_t,并提供了计算接口的示例代码。
摘要由CSDN通过智能技术生成

工作需要用到了sm3,上网一搜goldboar的版本被引用最多的,可这个版本却有问题,搜了些别人的版本都不如他的好,有的甚至移植过来编译不过。最后还是用goldboar版本修改了。

这里写图片描述

上图是我用goldboar和Odzhan提供的版本在不同环境下计算“abc”的结果,两个版本均无法在iphone6s上计算正确,最后跟踪发现是iphone6s上long形为8字节导致的。遂修改代码中原有的unsigned long 类型为uint32_t 。问题即得到解决,现将代码贴此,需者取之。

/*
 本代码在goldboar提供的版本的基础上进行修改,http://blog.csdn.net/goldboar/article/details/6932274
 包括
 1,参照网上的说法修改shl rotl宏 http://blog.csdn.net/wak0408/article/details/50372386
 2,在iphone6上long是8字节的,实际需要4字节的,导致iphone6s上计算结果不正确,所以把代码中的unsigned long 改成了 uint32_t

 mender:swibyn
 email:swibyn@qq.com
 */
/**
 * \file sm3.h
 * thanks to Xyssl
 * SM3 standards:http://www.oscca.gov.cn/News/201012/News_1199.htm
 * author:goldboar
 * email:goldboar@163.com
 * 2011-10-26
 */
#ifndef XYSSL_SM3_H_cc
#define XYSSL_SM3_H_cc


/**
 * \brief          SM3 context structure
 */
typedef struct
{
    uint32_t total[2];     /*!< number of bytes processed  */
    uint32_t state[8];     /*!< intermediate digest state  */
    unsigned char buffer[64];   /*!< data block being processed */

    unsigned char ipad[64];     /*!< HMAC: inner padding        */
    unsigned char opad[64];     /*!< HMAC: outer padding        */

}
sm3_context_cc;

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief          SM3 context setup
 *
 * \param ctx      context to be initialized
 */
void sm3_starts( sm3_context_cc *ctx );

/**
 * \brief          SM3 process buffer
 *
 * \param ctx      SM3 context
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 */
void sm3_update( sm3_context_cc *ctx, unsigned char *input, int ilen );

/**
 * \brief          SM3 final digest
 *
 * \param ctx      SM3 context
 */
void sm3_finish( sm3_context_cc *ctx, unsigned char output[32] );

/**
 * \brief          Output = SM3( input buffer )
 *
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 * \param output   SM3 checksum result
 */
void goldboar_cc_sm3( unsigned char *input, int ilen,
           unsigned char output[32]);

/**
 * \brief          Output = SM3( file contents )
 *
 * \param path     input file name
 * \param output   SM3 checksum result
 *
 * \return         0 if successful, 1 if fopen failed,
 *                 or 2 if fread failed
 */
int sm3_file( char *path, unsigned char output[32] );

/**
 * \brief          SM3 HMAC context setup
 *
 * \param ctx      HMAC context to be initialized
 * \param key      HMAC secret key
 * \param keylen   length of the HMAC key
 */
void sm3_hmac_starts( sm3_context_cc *ctx, unsigned char *key, int keylen);

/**
 * \brief          SM3 HMAC process buffer
 *
 * \param ctx      HMAC context
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 */
void sm3_hmac_update( sm3_context_cc *ctx, unsigned char *input, int ilen );

/**
 * \brief          SM3 HMAC final digest
 *
 * \param ctx      HMAC context
 * \param output   SM3 HMAC checksum result
 */
void sm3_hmac_finish( sm3_context_cc *ctx, unsigned char output[32] );

/**
 * \brief          Output = HMAC-SM3( hmac key, input buffer )
 *
 * \param key      HMAC secret key
 * \param keylen   length of the HMAC key
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 * \param output   HMAC-SM3 result
 */
void sm3_hmac( unsigned char *key, int keylen,
                unsigned char *input, int ilen,
                unsigned char output[32] );


#ifdef __cplusplus
}
#endif

#endif /* sm3.h */
/*
 本代码在goldboar提供的版本的基础上进行修改,包括
 1,参照网上的说法修改shl rotl宏 http://blog.csdn.net/wak0408/article/details/50372386
 2,在iphone6上long是8字节的,实际需要4字节的,导致iphone6s上计算结果不正确,所以把代码中的unsigned long 改成了 uint32_t

 mender:swibyn
 email:swibyn@qq.com
 */

/*
 * SM3 Hash alogrith  
 * thanks to Xyssl 
 * author:goldboar 
 * email:goldboar@163.com 
 * 2011-10-26 
 */


//Testing data from SM3 Standards  
//http://www.oscca.gov.cn/News/201012/News_1199.htm   
// Sample 1  
// Input:"abc"    
// Output:66c7f0f4 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0  

// Sample 2   
// Input:"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"  
// Outpuf:debe9ff9 2275b8a1 38604889 c18e5a4d 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值