From 1d52a77751bd718c4bc8afa60ff78c4306636cd9 Mon Sep 17 00:00:00 2001 From: tb <> Date: Sun, 11 Nov 2018 06:53:31 +0000 Subject: Add Ribose Inc's implementation of the SM3 hashing function with tweaks from jsing and myself. The SM2/SM3/SM4 algorithms are mandatory for legal use of cryptography within China and [are] widely applied in the country, covering identification/financial cards, contactless, TPM 2.0 and PKI. ok beck inoguchi jsing --- src/lib/libcrypto/evp/evp.h | 5 ++- src/lib/libcrypto/evp/m_sm3.c | 73 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/lib/libcrypto/evp/m_sm3.c (limited to 'src/lib/libcrypto/evp') diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h index c09e2c046a..04e0455623 100644 --- a/src/lib/libcrypto/evp/evp.h +++ b/src/lib/libcrypto/evp/evp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: evp.h,v 1.69 2018/09/12 06:35:38 djm Exp $ */ +/* $OpenBSD: evp.h,v 1.70 2018/11/11 06:53:31 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -686,6 +686,9 @@ const EVP_MD *EVP_sha256(void); const EVP_MD *EVP_sha384(void); const EVP_MD *EVP_sha512(void); #endif +#ifndef OPENSSL_NO_SM3 +const EVP_MD *EVP_sm3(void); +#endif #ifndef OPENSSL_NO_RIPEMD const EVP_MD *EVP_ripemd160(void); #endif diff --git a/src/lib/libcrypto/evp/m_sm3.c b/src/lib/libcrypto/evp/m_sm3.c new file mode 100644 index 0000000000..66582b8e4a --- /dev/null +++ b/src/lib/libcrypto/evp/m_sm3.c @@ -0,0 +1,73 @@ +/* $OpenBSD: m_sm3.c,v 1.1 2018/11/11 06:53:31 tb Exp $ */ +/* + * Copyright (c) 2018, Ribose Inc + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#ifndef OPENSSL_NO_SM3 +#include +#include + +#ifndef OPENSSL_NO_RSA +#include +#endif + +static int +sm3_init(EVP_MD_CTX *ctx) +{ + return SM3_Init(ctx->md_data); +} + +static int +sm3_update(EVP_MD_CTX *ctx, const void *data, size_t count) +{ + return SM3_Update(ctx->md_data, data, count); +} + +static int +sm3_final(EVP_MD_CTX *ctx, unsigned char *md) +{ + return SM3_Final(md, ctx->md_data); +} + +static const EVP_MD sm3_md = { + .type = NID_sm3, + .pkey_type = NID_sm3WithRSAEncryption, + .md_size = SM3_DIGEST_LENGTH, + .flags = EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT, + .init = sm3_init, + .update = sm3_update, + .final = sm3_final, + .copy = NULL, + .cleanup = NULL, +#ifndef OPENSSL_NO_RSA + .sign = (evp_sign_method *)RSA_sign, + .verify = (evp_verify_method *)RSA_verify, + .required_pkey_type = { + EVP_PKEY_RSA, EVP_PKEY_RSA2, 0, 0, + }, +#endif + .block_size = SM3_CBLOCK, + .ctx_size = sizeof(EVP_MD *) + sizeof(SM3_CTX), +}; + +const EVP_MD * +EVP_sm3(void) +{ + return &sm3_md; +} + +#endif /* OPENSSL_NO_SM3 */ -- cgit v1.2.3-55-g6feb