core: ltc: SM2 PKE: export sm2_kdf()

The Key Derivation Function used by the SM2 Public Key Encryption
algorithm is also used by the Key Exchange Protocol. Move it to its
file in order to be able to re-use it.

Signed-off-by: Jerome Forissier <jerome@forissier.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/core/lib/libtomcrypt/acipher_helpers.h b/core/lib/libtomcrypt/acipher_helpers.h
index 7781e82..b055728 100644
--- a/core/lib/libtomcrypt/acipher_helpers.h
+++ b/core/lib/libtomcrypt/acipher_helpers.h
@@ -50,4 +50,6 @@
                 mp_to_unsigned_bin(_a, (b) + (c) - mp_unsigned_bin_size(_a)); \
         } while(0)
 
+TEE_Result sm2_kdf(const uint8_t *Z, size_t Z_len, uint8_t *t, size_t tlen);
+
 #endif /* ACIPHER_HELPERS_H */
diff --git a/core/lib/libtomcrypt/sm2-pke.c b/core/lib/libtomcrypt/sm2-pke.c
index 28ba02e..4937bb9 100644
--- a/core/lib/libtomcrypt/sm2-pke.c
+++ b/core/lib/libtomcrypt/sm2-pke.c
@@ -89,62 +89,6 @@
 	return TEE_ERROR_GENERIC;
 }
 
-/*
- * GM/T 0003.1‒2012 Part 4 Sections 5.4.2 and 5.4.3
- * Key derivation function based on the SM3 hash function
- */
-static TEE_Result sm2_kdf(const uint8_t *Z, size_t Z_len, uint8_t *t,
-			  size_t tlen)
-{
-	TEE_Result res = TEE_SUCCESS;
-	size_t remain = tlen;
-	uint32_t count = 1;
-	uint32_t be_count = 0;
-	void *ctx = NULL;
-	uint8_t *out = t;
-
-	res = crypto_hash_alloc_ctx(&ctx, TEE_ALG_SM3);
-	if (res)
-		return res;
-
-	while (remain) {
-		uint8_t tmp[TEE_SM3_HASH_SIZE] = { };
-		uint8_t *buf = NULL;
-
-		if (remain >= TEE_SM3_HASH_SIZE)
-			buf = out;
-		else
-			buf = tmp;
-
-		put_be32(&be_count, count);
-		res = crypto_hash_init(ctx);
-		if (res)
-			goto out;
-		res = crypto_hash_update(ctx, Z, Z_len);
-		if (res)
-			goto out;
-		res = crypto_hash_update(ctx, (const uint8_t *)&be_count,
-					 sizeof(be_count));
-		if (res)
-			goto out;
-		res = crypto_hash_final(ctx, buf, TEE_SM3_HASH_SIZE);
-		if (res)
-			goto out;
-
-		if (remain < TEE_SM3_HASH_SIZE) {
-			memcpy(out, tmp, remain);
-			break;
-		}
-
-		out += TEE_SM3_HASH_SIZE;
-		remain -= TEE_SM3_HASH_SIZE;
-		count++;
-	}
-out:
-	crypto_hash_free_ctx(ctx);
-	return res;
-}
-
 static bool is_zero(const uint8_t *buf, size_t size)
 {
 	uint8_t v = 0;
diff --git a/core/lib/libtomcrypt/sm2_kdf.c b/core/lib/libtomcrypt/sm2_kdf.c
new file mode 100644
index 0000000..1171e22
--- /dev/null
+++ b/core/lib/libtomcrypt/sm2_kdf.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2020 Huawei Technologies Co., Ltd
+ */
+
+#include <crypto/crypto.h>
+#include <io.h>
+#include <stdint.h>
+#include <tee_api_types.h>
+#include <unistd.h>
+#include <utee_defines.h>
+
+#include "acipher_helpers.h"
+
+/*
+ * GM/T 0003.1‒2012 Part 4 Sections 5.4.2 and 5.4.3
+ * GM/T 0003.1‒2012 Part 5 Sections 5.4.2 and 5.4.3
+ * Key derivation function based on the SM3 hash function
+ */
+TEE_Result sm2_kdf(const uint8_t *Z, size_t Z_len, uint8_t *t, size_t tlen)
+{
+	TEE_Result res = TEE_SUCCESS;
+	size_t remain = tlen;
+	uint32_t count = 1;
+	uint32_t be_count = 0;
+	void *ctx = NULL;
+	uint8_t *out = t;
+
+	res = crypto_hash_alloc_ctx(&ctx, TEE_ALG_SM3);
+	if (res)
+		return res;
+
+	while (remain) {
+		uint8_t tmp[TEE_SM3_HASH_SIZE] = { };
+		uint8_t *buf = NULL;
+
+		if (remain >= TEE_SM3_HASH_SIZE)
+			buf = out;
+		else
+			buf = tmp;
+
+		put_be32(&be_count, count);
+		res = crypto_hash_init(ctx);
+		if (res)
+			goto out;
+		res = crypto_hash_update(ctx, Z, Z_len);
+		if (res)
+			goto out;
+		res = crypto_hash_update(ctx, (const uint8_t *)&be_count,
+					 sizeof(be_count));
+		if (res)
+			goto out;
+		res = crypto_hash_final(ctx, buf, TEE_SM3_HASH_SIZE);
+		if (res)
+			goto out;
+
+		if (remain < TEE_SM3_HASH_SIZE) {
+			memcpy(out, tmp, remain);
+			break;
+		}
+
+		out += TEE_SM3_HASH_SIZE;
+		remain -= TEE_SM3_HASH_SIZE;
+		count++;
+	}
+out:
+	crypto_hash_free_ctx(ctx);
+	return res;
+}
+
diff --git a/core/lib/libtomcrypt/sub.mk b/core/lib/libtomcrypt/sub.mk
index 6a841c2..ddcab27 100644
--- a/core/lib/libtomcrypt/sub.mk
+++ b/core/lib/libtomcrypt/sub.mk
@@ -139,6 +139,7 @@
 srcs-$(_CFG_CORE_LTC_AES) += aes.c
 srcs-$(_CFG_CORE_LTC_SM2_DSA) += sm2-dsa.c
 srcs-$(_CFG_CORE_LTC_SM2_PKE) += sm2-pke.c
+srcs-$(_CFG_CORE_LTC_SM2_PKE) += sm2_kdf.c
 
 ifeq ($(_CFG_CORE_LTC_ACIPHER),y)
 ifeq ($(_CFG_CORE_LTC_MPI),y)