SM3: restructure to EVP internal and update doc to right location
authorRonald Tse <ronald.tse@ribose.com>
Mon, 30 Oct 2017 09:59:00 +0000 (17:59 +0800)
committerRonald Tse <ronald.tse@ribose.com>
Sun, 5 Nov 2017 23:21:15 +0000 (07:21 +0800)
Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4616)

12 files changed:
INSTALL
crypto/evp/build.info
crypto/include/internal/sm3.h [moved from include/openssl/sm3.h with 59% similarity]
crypto/sm3/build.info
crypto/sm3/m_sm3.c [moved from crypto/evp/m_sm3.c with 80% similarity]
crypto/sm3/sm3.c
crypto/sm3/sm3_locl.h
doc/man3/EVP_DigestInit.pod
doc/man3/EVP_sm3.pod [new file with mode: 0644]
doc/man3/SM3.pod [deleted file]
fuzz/oids.txt
util/libcrypto.num

diff --git a/INSTALL b/INSTALL
index 4e954e3..9e67014 100644 (file)
--- a/INSTALL
+++ b/INSTALL
                    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, 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
index 96b44ef..0305738 100644 (file)
@@ -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 \
similarity index 59%
rename from include/openssl/sm3.h
rename to crypto/include/internal/sm3.h
index 84f8570..27eb471 100644 (file)
@@ -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
 #  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
index 239ac87..6009b19 100644 (file)
@@ -1,2 +1,2 @@
 LIBS=../../libcrypto
-SOURCE[../../libcrypto]=sm3.c
+SOURCE[../../libcrypto]=sm3.c m_sm3.c
similarity index 80%
rename from crypto/evp/m_sm3.c
rename to crypto/sm3/m_sm3.c
index 21ee1de..85538dc 100644 (file)
@@ -8,29 +8,26 @@
  * 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"
+# 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
index 615fcb2..1588dd1 100644 (file)
@@ -9,14 +9,10 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <stdio.h>
-
-#ifndef OPENSSL_NO_SM3
-
+#include <openssl/e_os2.h>
 #include "sm3_locl.h"
-#include <openssl/opensslv.h>
 
-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
+
index 598c80a..efa6db5 100644 (file)
@@ -9,34 +9,33 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <stdlib.h>
 #include <string.h>
-#include <openssl/e_os2.h>
-#include <openssl/sm3.h>
-
-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)
index 1885489..b2eec52 100644 (file)
@@ -301,6 +301,7 @@ L<EVP_ripemd160(3)>,
 L<EVP_sha1(3)>,
 L<EVP_sha224(3)>,
 L<EVP_sha3_224(3)>,
+L<EVP_sm3(3)>,
 L<EVP_whirlpool(3)>
 
 =head1 HISTORY
diff --git a/doc/man3/EVP_sm3.pod b/doc/man3/EVP_sm3.pod
new file mode 100644 (file)
index 0000000..a68fe66
--- /dev/null
@@ -0,0 +1,54 @@
+=pod
+
+=head1 NAME
+
+EVP_sm3
+- SM3 for EVP
+
+=head1 SYNOPSIS
+
+ #include <openssl/evp.h>
+
+ 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<EVP_MD> structure that contains the
+implementation of the symmetric cipher. See L<EVP_MD_meth_new(3)> for
+details of the B<EVP_MD> structure.
+
+=head1 CONFORMING TO
+
+GB/T 32905-2016 and GM/T 0004-2012.
+
+=head1 SEE ALSO
+
+L<evp(7)>,
+L<EVP_DigestInit(3)>
+
+=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<https://www.openssl.org/source/license.html>.
+
+=cut
+
diff --git a/doc/man3/SM3.pod b/doc/man3/SM3.pod
deleted file mode 100644 (file)
index afa7082..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-=pod
-
-=head1 NAME
-
-SM3_Init,
-SM3_Update,
-SM3_Final
-
-=head1 SYNOPSIS
-
- #include <openssl/sm3.h>
-
- 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<n> bytes at B<d> and places it
-in B<md> (which must have space for SM3_DIGEST_LENGTH == 32 bytes of output).
-If B<md> 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<SM3_CTX> structure.
-
-SM3_Update() can be called repeatedly with chunks of the message to be hashed
-(B<len> bytes at B<data>).
-
-SM3_Final() places the message digest in B<md>, which must have space for
-B<SM3_DIGEST_LENGTH> == 32 bytes of output, and erases the B<SM3_CTX>.
-
-=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<EVP_DigestInit(3)>
-instead of calling these functions directly.
-
-=head1 CONFORMING TO
-
-GB/T 32905-2016 and GM/T 0004-2012.
-
-=head1 SEE ALSO
-
-L<EVP_DigestInit(3)>
-
-=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<https://www.openssl.org/source/license.html>.
-
-=cut
index 79dc032..82782b1 100644 (file)
@@ -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"
index cf7575d..a6b4ceb 100644 (file)
@@ -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