Skip to content

Commit

Permalink
SM3: Add SM3 hash function
Browse files Browse the repository at this point in the history
SM3 is a secure hash function which is part of the Chinese
"Commercial Cryptography" suite of algorithms which use is
required for certain commercial applications in China.

Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Richard Levitte <[email protected]>
(Merged from openssl#4616)
  • Loading branch information
Jack Lloyd authored and ronaldtse committed Nov 5, 2017
1 parent cf72c75 commit a0c3e4f
Show file tree
Hide file tree
Showing 20 changed files with 567 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

Changes between 1.1.0f and 1.1.1 [xx XXX xxxx]

*) Add SM3 implemented according to GB/T 32905-2016
[ Jack Lloyd <[email protected]>,
Ronald Tse <[email protected]>,
Erick Borsboom <[email protected]> ]

*) Add 'Maximum Fragment Length' TLS extension negotiation and support
as documented in RFC6066.
Based on a patch from Tomasz Moń
Expand Down
3 changes: 2 additions & 1 deletion Configure
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ $config{dirs} = [ "crypto", "ssl", "engines", "apps", "test", "util", "tools", "
# crypto/ subdirectories to build
$config{sdirs} = [
"objects",
"md2", "md4", "md5", "sha", "mdc2", "hmac", "ripemd", "whrlpool", "poly1305", "blake2", "siphash",
"md2", "md4", "md5", "sha", "mdc2", "hmac", "ripemd", "whrlpool", "poly1305", "blake2", "siphash", "sm3",
"des", "aes", "rc2", "rc4", "rc5", "idea", "aria", "bf", "cast", "camellia", "seed", "sm4", "chacha", "modes",
"bn", "ec", "rsa", "dsa", "dh", "dso", "engine",
"buffer", "bio", "stack", "lhash", "rand", "err",
Expand Down Expand Up @@ -394,6 +394,7 @@ my @disablables = (
"seed",
"shared",
"siphash",
"sm3",
"sm4",
"sock",
"srp",
Expand Down
6 changes: 3 additions & 3 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@
Build without support for the specified algorithm, where
<alg> is one of: bf, blake2, camellia, cast, chacha, cmac,
des, dh, dsa, ecdh, ecdsa, idea, md4, mdc2, ocb, poly1305,
rc2, rc4, rmd160, scrypt, seed, siphash, sm4 or whirlpool.
The "ripemd" algorithm is deprecated and if used is
synonymous with rmd160.
rc2, rc4, rmd160, scrypt, seed, siphash, sm3, sm4 or
whirlpool. The "ripemd" algorithm is deprecated and if used
is synonymous with rmd160.

-Dxxx, lxxx, -Lxxx, -Wl, -rpath, -R, -framework, -static
These system specific options will be recognised and
Expand Down
2 changes: 1 addition & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ case "$GUESSOS" in
i386-*) options="$options 386" ;;
esac

for i in aes aria bf camellia cast des dh dsa ec hmac idea md2 md5 mdc2 rc2 rc4 rc5 ripemd rsa seed sm4 sha
for i in aes aria bf camellia cast des dh dsa ec hmac idea md2 md5 mdc2 rc2 rc4 rc5 ripemd rsa seed sha sm3 sm4
do
if [ ! -d $THERE/crypto/$i ]
then
Expand Down
2 changes: 1 addition & 1 deletion crypto/evp/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SOURCE[../../libcrypto]=\
e_rc4.c e_aes.c names.c e_seed.c e_aria.c e_sm4.c \
e_xcbc_d.c e_rc2.c e_cast.c e_rc5.c \
m_null.c m_md2.c m_md4.c m_md5.c m_sha1.c m_wp.c \
m_md5_sha1.c m_mdc2.c m_ripemd.c m_sha3.c \
m_md5_sha1.c m_mdc2.c m_ripemd.c m_sha3.c m_sm3.c \
p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c \
bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c \
c_allc.c c_alld.c evp_lib.c bio_ok.c \
Expand Down
3 changes: 3 additions & 0 deletions crypto/evp/c_alld.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ void openssl_add_all_digests_int(void)
#ifndef OPENSSL_NO_WHIRLPOOL
EVP_add_digest(EVP_whirlpool());
#endif
#ifndef OPENSSL_NO_SM3
EVP_add_digest(EVP_sm3());
#endif
#ifndef OPENSSL_NO_BLAKE2
EVP_add_digest(EVP_blake2b512());
EVP_add_digest(EVP_blake2s256());
Expand Down
55 changes: 55 additions & 0 deletions crypto/evp/m_sm3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2017 Ribose Inc. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include <stdio.h>
#include "internal/cryptlib.h"

#ifndef OPENSSL_NO_SM3

# include <openssl/evp.h>
# include <openssl/objects.h>
# include <openssl/sm3.h>
# include "internal/evp_int.h"

static int init(EVP_MD_CTX *ctx)
{
return SM3_Init(EVP_MD_CTX_md_data(ctx));
}

static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
{
return SM3_Update(EVP_MD_CTX_md_data(ctx), data, count);
}

static int final(EVP_MD_CTX *ctx, unsigned char *md)
{
return SM3_Final(md, EVP_MD_CTX_md_data(ctx));
}

static const EVP_MD sm3_md = {
NID_sm3,
NID_sm3WithRSAEncryption,
SM3_DIGEST_LENGTH,
0,
init,
update,
final,
NULL,
NULL,
SM3_CBLOCK,
sizeof(EVP_MD *) + sizeof(SM3_CTX),
};

const EVP_MD *EVP_sm3(void)
{
return &sm3_md;
}
#endif

20 changes: 15 additions & 5 deletions crypto/objects/obj_dat.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

/* Serialized OID's */
static const unsigned char so[7308] = {
static const unsigned char so[7324] = {
0x2A,0x86,0x48,0x86,0xF7,0x0D, /* [ 0] OBJ_rsadsi */
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01, /* [ 6] OBJ_pkcs */
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x02,0x02, /* [ 13] OBJ_md2 */
Expand Down Expand Up @@ -1028,9 +1028,11 @@ static const unsigned char so[7308] = {
0x2A,0x81,0x1C, /* [ 7293] OBJ_ISO_CN */
0x2A,0x81,0x1C,0xCF,0x55, /* [ 7296] OBJ_oscca */
0x2A,0x81,0x1C,0xCF,0x55,0x01, /* [ 7301] OBJ_sm_scheme */
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x83,0x11, /* [ 7307] OBJ_sm3 */
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x83,0x78, /* [ 7315] OBJ_sm3WithRSAEncryption */
};

#define NUM_NID 1143
#define NUM_NID 1145
static const ASN1_OBJECT nid_objs[NUM_NID] = {
{"UNDEF", "undefined", NID_undef},
{"rsadsi", "RSA Data Security, Inc.", NID_rsadsi, 6, &so[0]},
Expand Down Expand Up @@ -2175,9 +2177,11 @@ static const ASN1_OBJECT nid_objs[NUM_NID] = {
{"ISO-CN", "ISO CN Member Body", NID_ISO_CN, 3, &so[7293]},
{"oscca", "oscca", NID_oscca, 5, &so[7296]},
{"sm-scheme", "sm-scheme", NID_sm_scheme, 6, &so[7301]},
{"SM3", "sm3", NID_sm3, 8, &so[7307]},
{"RSA-SM3", "sm3WithRSAEncryption", NID_sm3WithRSAEncryption, 8, &so[7315]},
};

#define NUM_SN 1134
#define NUM_SN 1136
static const unsigned int sn_objs[NUM_SN] = {
364, /* "AD_DVCS" */
419, /* "AES-128-CBC" */
Expand Down Expand Up @@ -2418,6 +2422,7 @@ static const unsigned int sn_objs[NUM_SN] = {
668, /* "RSA-SHA256" */
669, /* "RSA-SHA384" */
670, /* "RSA-SHA512" */
1144, /* "RSA-SM3" */
919, /* "RSAES-OAEP" */
912, /* "RSASSA-PSS" */
777, /* "SEED-CBC" */
Expand All @@ -2438,6 +2443,7 @@ static const unsigned int sn_objs[NUM_SN] = {
1095, /* "SHA512-256" */
1100, /* "SHAKE128" */
1101, /* "SHAKE256" */
1143, /* "SM3" */
1134, /* "SM4-CBC" */
1137, /* "SM4-CFB" */
1136, /* "SM4-CFB1" */
Expand Down Expand Up @@ -3315,7 +3321,7 @@ static const unsigned int sn_objs[NUM_SN] = {
1093, /* "x509ExtAdmission" */
};

#define NUM_LN 1134
#define NUM_LN 1136
static const unsigned int ln_objs[NUM_LN] = {
363, /* "AD Time Stamping" */
405, /* "ANSI X9.62" */
Expand Down Expand Up @@ -4399,6 +4405,8 @@ static const unsigned int ln_objs[NUM_LN] = {
496, /* "singleLevelQuality" */
1062, /* "siphash" */
1142, /* "sm-scheme" */
1143, /* "sm3" */
1144, /* "sm3WithRSAEncryption" */
1134, /* "sm4-cbc" */
1137, /* "sm4-cfb" */
1136, /* "sm4-cfb1" */
Expand Down Expand Up @@ -4453,7 +4461,7 @@ static const unsigned int ln_objs[NUM_LN] = {
125, /* "zlib compression" */
};

#define NUM_OBJ 1023
#define NUM_OBJ 1025
static const unsigned int obj_objs[NUM_OBJ] = {
0, /* OBJ_undef 0 */
181, /* OBJ_iso 1 */
Expand Down Expand Up @@ -4915,6 +4923,8 @@ static const unsigned int obj_objs[NUM_OBJ] = {
1136, /* OBJ_sm4_cfb1 1 2 156 10197 1 104 5 */
1138, /* OBJ_sm4_cfb8 1 2 156 10197 1 104 6 */
1139, /* OBJ_sm4_ctr 1 2 156 10197 1 104 7 */
1143, /* OBJ_sm3 1 2 156 10197 1 401 */
1144, /* OBJ_sm3WithRSAEncryption 1 2 156 10197 1 504 */
776, /* OBJ_seed_ecb 1 2 410 200004 1 3 */
777, /* OBJ_seed_cbc 1 2 410 200004 1 4 */
779, /* OBJ_seed_cfb128 1 2 410 200004 1 5 */
Expand Down
2 changes: 2 additions & 0 deletions crypto/objects/obj_mac.num
Original file line number Diff line number Diff line change
Expand Up @@ -1140,3 +1140,5 @@ sm4_ctr 1139
ISO_CN 1140
oscca 1141
sm_scheme 1142
sm3 1143
sm3WithRSAEncryption 1144
3 changes: 3 additions & 0 deletions crypto/objects/objects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ rsadsi 2 5 : MD5 : md5
rsadsi 2 6 : : hmacWithMD5
rsadsi 2 7 : : hmacWithSHA1

member-body 156 10197 1 401 : SM3 : sm3
member-body 156 10197 1 504 : RSA-SM3 : sm3WithRSAEncryption

# From RFC4231
rsadsi 2 8 : : hmacWithSHA224
rsadsi 2 9 : : hmacWithSHA256
Expand Down
2 changes: 2 additions & 0 deletions crypto/sm3/build.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=sm3.c
Loading

0 comments on commit a0c3e4f

Please sign in to comment.