aboutsummaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorRonald Tse <ronald.tse@ribose.com>2017-10-30 17:59:00 +0800
committerRonald Tse <ronald.tse@ribose.com>2017-11-06 07:21:15 +0800
commit67e247fad12308e34817e60c9242113c285fb00c (patch)
treeb11943e669ecd8482fe95973c2eb407d295d38d7 /crypto
parenta0c3e4fa9089f571ff4b406cb914d0a504847b10 (diff)
downloadopenssl-67e247fad12308e34817e60c9242113c285fb00c.zip
openssl-67e247fad12308e34817e60c9242113c285fb00c.tar.gz
openssl-67e247fad12308e34817e60c9242113c285fb00c.tar.bz2
SM3: restructure to EVP internal and update doc to right location
Reviewed-by: Paul Dale <paul.dale@oracle.com> Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/4616)
Diffstat (limited to 'crypto')
-rw-r--r--crypto/evp/build.info2
-rw-r--r--crypto/include/internal/sm3.h39
-rw-r--r--crypto/sm3/build.info2
-rw-r--r--crypto/sm3/m_sm3.c (renamed from crypto/evp/m_sm3.c)13
-rw-r--r--crypto/sm3/sm3.c25
-rw-r--r--crypto/sm3/sm3_locl.h60
6 files changed, 79 insertions, 62 deletions
diff --git a/crypto/evp/build.info b/crypto/evp/build.info
index 96b44ef..0305738 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/crypto/include/internal/sm3.h b/crypto/include/internal/sm3.h
new file mode 100644
index 0000000..27eb471
--- /dev/null
+++ b/crypto/include/internal/sm3.h
@@ -0,0 +1,39 @@
+/*
+ * 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
+ */
+
+#ifndef HEADER_SM3_H
+# define HEADER_SM3_H
+
+# include <openssl/opensslconf.h>
+
+# ifdef OPENSSL_NO_SM3
+# error SM3 is disabled.
+# endif
+
+# define SM3_DIGEST_LENGTH 32
+# define SM3_WORD unsigned int
+
+# define SM3_CBLOCK 64
+# define SM3_LBLOCK (SM3_CBLOCK/4)
+
+typedef struct SM3state_st {
+ SM3_WORD A, B, C, D, E, F, G, H;
+ SM3_WORD Nl, Nh;
+ SM3_WORD data[SM3_LBLOCK];
+ 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_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 239ac87..6009b19 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
index 21ee1de..85538dc 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 <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
diff --git a/crypto/sm3/sm3.c b/crypto/sm3/sm3.c
index 615fcb2..1588dd1 100644
--- a/crypto/sm3/sm3.c
+++ b/crypto/sm3/sm3.c
@@ -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
+
diff --git a/crypto/sm3/sm3_locl.h b/crypto/sm3/sm3_locl.h
index 598c80a..efa6db5 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 <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)