From 67e247fad12308e34817e60c9242113c285fb00c Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Mon, 30 Oct 2017 17:59:00 +0800 Subject: [PATCH] SM3: restructure to EVP internal and update doc to right location Reviewed-by: Paul Dale Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/4616) --- INSTALL | 6 +- crypto/evp/build.info | 2 +- .../openssl => crypto/include/internal}/sm3.h | 22 ++---- crypto/sm3/build.info | 2 +- crypto/{evp => sm3}/m_sm3.c | 13 ++-- crypto/sm3/sm3.c | 25 +----- crypto/sm3/sm3_locl.h | 60 +++++++-------- doc/man3/EVP_DigestInit.pod | 1 + doc/man3/EVP_sm3.pod | 54 +++++++++++++ doc/man3/SM3.pod | 76 ------------------- fuzz/oids.txt | 2 + util/libcrypto.num | 4 - 12 files changed, 107 insertions(+), 160 deletions(-) rename {include/openssl => crypto/include/internal}/sm3.h (59%) rename crypto/{evp => sm3}/m_sm3.c (80%) create mode 100644 doc/man3/EVP_sm3.pod delete mode 100644 doc/man3/SM3.pod diff --git a/INSTALL b/INSTALL index 4e954e34c2..9e67014cd6 100644 --- a/INSTALL +++ b/INSTALL @@ -512,9 +512,9 @@ Build without support for the specified algorithm, where 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, sm3, 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 diff --git a/crypto/evp/build.info b/crypto/evp/build.info index 96b44efbfb..0305738011 100644 --- a/crypto/evp/build.info +++ b/crypto/evp/build.info @@ -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_sm3.c \ + m_md5_sha1.c m_mdc2.c m_ripemd.c m_sha3.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 \ diff --git a/include/openssl/sm3.h b/crypto/include/internal/sm3.h similarity index 59% rename from include/openssl/sm3.h rename to crypto/include/internal/sm3.h index 84f8570235..27eb471c28 100644 --- a/include/openssl/sm3.h +++ b/crypto/include/internal/sm3.h @@ -1,6 +1,6 @@ /* * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. - * Copyright 2017 [Ribose Inc.](https://www.ribose.com). 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 @@ -17,12 +17,8 @@ # error SM3 is disabled. # endif -# ifdef __cplusplus -extern "C" { -# endif - -#define SM3_DIGEST_LENGTH 32 -#define SM3_WORD unsigned int +# define SM3_DIGEST_LENGTH 32 +# define SM3_WORD unsigned int # define SM3_CBLOCK 64 # define SM3_LBLOCK (SM3_CBLOCK/4) @@ -34,14 +30,10 @@ typedef struct SM3state_st { unsigned int num; } SM3_CTX; -int SM3_Init(SM3_CTX *c); -int SM3_Update(SM3_CTX *c, const void *data, size_t len); -int SM3_Final(unsigned char *md, SM3_CTX *c); -void SM3_Transform(SM3_CTX *c, const unsigned char *data); -unsigned char *SM3(const unsigned char *d, size_t n, unsigned char *md); +int sm3_init(SM3_CTX *c); +int sm3_update(SM3_CTX *c, const void *data, size_t len); +int sm3_final(unsigned char *md, SM3_CTX *c); -# ifdef __cplusplus -} -# endif +void sm3_block_data_order(SM3_CTX *c, const void *p, size_t num); #endif diff --git a/crypto/sm3/build.info b/crypto/sm3/build.info index 239ac8755e..6009b1949e 100644 --- a/crypto/sm3/build.info +++ b/crypto/sm3/build.info @@ -1,2 +1,2 @@ LIBS=../../libcrypto -SOURCE[../../libcrypto]=sm3.c +SOURCE[../../libcrypto]=sm3.c m_sm3.c diff --git a/crypto/evp/m_sm3.c b/crypto/sm3/m_sm3.c similarity index 80% rename from crypto/evp/m_sm3.c rename to crypto/sm3/m_sm3.c index 21ee1de136..85538dc8af 100644 --- a/crypto/evp/m_sm3.c +++ b/crypto/sm3/m_sm3.c @@ -8,29 +8,26 @@ * https://www.openssl.org/source/license.html */ -#include #include "internal/cryptlib.h" #ifndef OPENSSL_NO_SM3 - # include -# include -# include # include "internal/evp_int.h" +# include "internal/sm3.h" static int init(EVP_MD_CTX *ctx) { - return SM3_Init(EVP_MD_CTX_md_data(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); + 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)); + return sm3_final(md, EVP_MD_CTX_md_data(ctx)); } static const EVP_MD sm3_md = { @@ -51,5 +48,5 @@ const EVP_MD *EVP_sm3(void) { return &sm3_md; } -#endif +#endif diff --git a/crypto/sm3/sm3.c b/crypto/sm3/sm3.c index 615fcb21cd..1588dd115a 100644 --- a/crypto/sm3/sm3.c +++ b/crypto/sm3/sm3.c @@ -9,14 +9,10 @@ * https://www.openssl.org/source/license.html */ -#include - -#ifndef OPENSSL_NO_SM3 - +#include #include "sm3_locl.h" -#include -int SM3_Init(SM3_CTX *c) +int sm3_init(SM3_CTX *c) { memset(c, 0, sizeof(*c)); c->A = SM3_A; @@ -30,21 +26,6 @@ int SM3_Init(SM3_CTX *c) return 1; } -unsigned char *SM3(const unsigned char *d, size_t n, unsigned char *md) -{ - SM3_CTX c; - static unsigned char m[SM3_DIGEST_LENGTH]; - - if (md == NULL) - md = m; - if (!SM3_Init(&c)) - return NULL; - SM3_Update(&c, d, n); - SM3_Final(md, &c); - OPENSSL_cleanse(&c, sizeof(c)); /* security consideration */ - return md; -} - void sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num) { const unsigned char *data = p; @@ -212,4 +193,4 @@ void sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num) ctx->H ^= H; } } -#endif + diff --git a/crypto/sm3/sm3_locl.h b/crypto/sm3/sm3_locl.h index 598c80aa1d..efa6db57c6 100644 --- a/crypto/sm3/sm3_locl.h +++ b/crypto/sm3/sm3_locl.h @@ -9,34 +9,33 @@ * https://www.openssl.org/source/license.html */ -#include #include -#include -#include - -void sm3_block_data_order(SM3_CTX *c, const void *p, size_t num); +#include "internal/sm3.h" #define DATA_ORDER_IS_BIG_ENDIAN #define HASH_LONG SM3_WORD #define HASH_CTX SM3_CTX #define HASH_CBLOCK SM3_CBLOCK -#define HASH_UPDATE SM3_Update -#define HASH_TRANSFORM SM3_Transform -#define HASH_FINAL SM3_Final -#define HASH_MAKE_STRING(c,s) do { \ - unsigned long ll; \ - ll=(c)->A; (void)HOST_l2c(ll,(s)); \ - ll=(c)->B; (void)HOST_l2c(ll,(s)); \ - ll=(c)->C; (void)HOST_l2c(ll,(s)); \ - ll=(c)->D; (void)HOST_l2c(ll,(s)); \ - ll=(c)->E; (void)HOST_l2c(ll,(s)); \ - ll=(c)->F; (void)HOST_l2c(ll,(s)); \ - ll=(c)->G; (void)HOST_l2c(ll,(s)); \ - ll=(c)->H; (void)HOST_l2c(ll,(s)); \ - } while (0) +#define HASH_UPDATE sm3_update +#define HASH_TRANSFORM sm3_transform +#define HASH_FINAL sm3_final +#define HASH_MAKE_STRING(c, s) \ + do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll, (s)); \ + ll=(c)->B; (void)HOST_l2c(ll, (s)); \ + ll=(c)->C; (void)HOST_l2c(ll, (s)); \ + ll=(c)->D; (void)HOST_l2c(ll, (s)); \ + ll=(c)->E; (void)HOST_l2c(ll, (s)); \ + ll=(c)->F; (void)HOST_l2c(ll, (s)); \ + ll=(c)->G; (void)HOST_l2c(ll, (s)); \ + ll=(c)->H; (void)HOST_l2c(ll, (s)); \ + } while (0) #define HASH_BLOCK_DATA_ORDER sm3_block_data_order +void sm3_transform(SM3_CTX *c, const unsigned char *data); + #include "internal/md32_common.h" #define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17)) @@ -51,17 +50,18 @@ void sm3_block_data_order(SM3_CTX *c, const void *p, size_t num); #define EXPAND(W0,W7,W13,W3,W10) \ (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10) -#define RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF,GG) do { \ - const SM3_WORD A12 = ROTATE(A, 12); \ - const SM3_WORD A12_SM = A12 + E + TJ; \ - const SM3_WORD SS1 = ROTATE(A12_SM, 7); \ - const SM3_WORD TT1 = FF(A,B,C) + D + (SS1 ^ A12) + (Wj); \ - const SM3_WORD TT2 = GG(E,F,G) + H + SS1 + Wi; \ - B = ROTATE(B, 9); \ - D = TT1; \ - F = ROTATE(F, 19); \ - H = P0(TT2); \ - } while(0); +#define RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) \ + do { \ + const SM3_WORD A12 = ROTATE(A, 12); \ + const SM3_WORD A12_SM = A12 + E + TJ; \ + const SM3_WORD SS1 = ROTATE(A12_SM, 7); \ + const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \ + const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi; \ + B = ROTATE(B, 9); \ + D = TT1; \ + F = ROTATE(F, 19); \ + H = P0(TT2); \ + } while(0) #define R1(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \ RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF0,GG0) diff --git a/doc/man3/EVP_DigestInit.pod b/doc/man3/EVP_DigestInit.pod index 1885489590..b2eec52384 100644 --- a/doc/man3/EVP_DigestInit.pod +++ b/doc/man3/EVP_DigestInit.pod @@ -301,6 +301,7 @@ L, L, L, L, +L, L =head1 HISTORY diff --git a/doc/man3/EVP_sm3.pod b/doc/man3/EVP_sm3.pod new file mode 100644 index 0000000000..a68fe667f3 --- /dev/null +++ b/doc/man3/EVP_sm3.pod @@ -0,0 +1,54 @@ +=pod + +=head1 NAME + +EVP_sm3 +- SM3 for EVP + +=head1 SYNOPSIS + + #include + + const EVP_MD *EVP_sm3(void) + +=head1 DESCRIPTION + +SM3 is a cryptographic hash function with a 256-bit output, defined in GB/T +32905-2016. + +=over 4 + +=item EVP_sm3() + +The SM3 hash function. + +=back + + +=head1 RETURN VALUES + +These functions return a B structure that contains the +implementation of the symmetric cipher. See L for +details of the B structure. + +=head1 CONFORMING TO + +GB/T 32905-2016 and GM/T 0004-2012. + +=head1 SEE ALSO + +L, +L + +=head1 COPYRIGHT + +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 +L. + +=cut + diff --git a/doc/man3/SM3.pod b/doc/man3/SM3.pod deleted file mode 100644 index afa7082618..0000000000 --- a/doc/man3/SM3.pod +++ /dev/null @@ -1,76 +0,0 @@ -=pod - -=head1 NAME - -SM3_Init, -SM3_Update, -SM3_Final - -=head1 SYNOPSIS - - #include - - unsigned char *SM3(const unsigned char *d, size_t n, unsigned char *md); - - int SM3_Init(SM3_CTX *c); - int SM3_Update(SM3_CTX *c, const void *data, size_t len); - int SM3_Final(unsigned char *md, SM3_CTX *c); - -=head1 DESCRIPTION - -SM3 is a cryptographic hash function with a 256-bit output, defined in GB/T -32905-2016. - -SM3() computes the SM3 message digest of the B bytes at B and places it -in B (which must have space for SM3_DIGEST_LENGTH == 32 bytes of output). -If B is NULL, the digest is placed in a static array. - -The following functions may be used if the message is not completely stored in -memory: - -SM3_Init() initializes a B structure. - -SM3_Update() can be called repeatedly with chunks of the message to be hashed -(B bytes at B). - -SM3_Final() places the message digest in B, which must have space for -B == 32 bytes of output, and erases the B. - -=head1 RETURN VALUES - -=over 4 - -=item SM3() - -Returns pointers to the hash value. - -=item SM3_Init(), SM3_Update(), SM3_Final() - -Returns 1 for success, 0 otherwise. - -=back - -=head1 NOTE - -Applications should use the higher level functions such as L -instead of calling these functions directly. - -=head1 CONFORMING TO - -GB/T 32905-2016 and GM/T 0004-2012. - -=head1 SEE ALSO - -L - -=head1 COPYRIGHT - -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 -L. - -=cut diff --git a/fuzz/oids.txt b/fuzz/oids.txt index 79dc032217..82782b1d6f 100644 --- a/fuzz/oids.txt +++ b/fuzz/oids.txt @@ -1015,3 +1015,5 @@ OBJ_sm4_ctr="\x2A\x81\x1C\xCF\x55\x01\x68\x07" OBJ_ISO_CN="\x2A\x81\x1C" OBJ_oscca="\x2A\x81\x1C\xCF\x55" OBJ_sm_scheme="\x2A\x81\x1C\xCF\x55\x01" +OBJ_sm3="\x2A\x81\x1C\xCF\x55\x01\x83\x11" +OBJ_sm3WithRSAEncryption="\x2A\x81\x1C\xCF\x55\x01\x83\x78" diff --git a/util/libcrypto.num b/util/libcrypto.num index cf7575de5f..a6b4ceb0a9 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -4426,7 +4426,3 @@ EVP_sm4_ofb 4370 1_1_1 EXIST::FUNCTION:SM4 EVP_sm4_ecb 4371 1_1_1 EXIST::FUNCTION:SM4 EVP_sm4_cfb128 4372 1_1_1 EXIST::FUNCTION:SM4 EVP_sm3 4373 1_1_1 EXIST::FUNCTION:SM3 -SM3_Update 4374 1_1_1 EXIST::FUNCTION:SM3 -SM3 4375 1_1_1 EXIST::FUNCTION:SM3 -SM3_Init 4376 1_1_1 EXIST::FUNCTION:SM3 -SM3_Final 4377 1_1_1 EXIST::FUNCTION:SM3 -- 2.39.2