summaryrefslogtreecommitdiffstats
path: root/crypto/sm3
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/sm3')
-rw-r--r--crypto/sm3/build.info2
-rw-r--r--crypto/sm3/m_sm3.c52
-rw-r--r--crypto/sm3/sm3.c25
-rw-r--r--crypto/sm3/sm3_locl.h60
4 files changed, 86 insertions, 53 deletions
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/sm3/m_sm3.c b/crypto/sm3/m_sm3.c
new file mode 100644
index 0000000000..85538dc8af
--- /dev/null
+++ b/crypto/sm3/m_sm3.c
@@ -0,0 +1,52 @@
+/*
+ * 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 "internal/cryptlib.h"
+
+#ifndef OPENSSL_NO_SM3
+# include <openssl/evp.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));
+}
+
+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
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 <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 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 <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)