1 /* $OpenBSD: m_sm3.c,v 1.1 2018/11/11 06:53:31 tb Exp $ */ 2 /* 3 * Copyright (c) 2018, Ribose Inc 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <openssl/opensslconf.h> 19 20 #ifndef OPENSSL_NO_SM3 21 #include <openssl/evp.h> 22 #include <openssl/sm3.h> 23 24 #ifndef OPENSSL_NO_RSA 25 #include <openssl/rsa.h> 26 #endif 27 28 static int 29 sm3_init(EVP_MD_CTX *ctx) 30 { 31 return SM3_Init(ctx->md_data); 32 } 33 34 static int 35 sm3_update(EVP_MD_CTX *ctx, const void *data, size_t count) 36 { 37 return SM3_Update(ctx->md_data, data, count); 38 } 39 40 static int 41 sm3_final(EVP_MD_CTX *ctx, unsigned char *md) 42 { 43 return SM3_Final(md, ctx->md_data); 44 } 45 46 static const EVP_MD sm3_md = { 47 .type = NID_sm3, 48 .pkey_type = NID_sm3WithRSAEncryption, 49 .md_size = SM3_DIGEST_LENGTH, 50 .flags = EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT, 51 .init = sm3_init, 52 .update = sm3_update, 53 .final = sm3_final, 54 .copy = NULL, 55 .cleanup = NULL, 56 #ifndef OPENSSL_NO_RSA 57 .sign = (evp_sign_method *)RSA_sign, 58 .verify = (evp_verify_method *)RSA_verify, 59 .required_pkey_type = { 60 EVP_PKEY_RSA, EVP_PKEY_RSA2, 0, 0, 61 }, 62 #endif 63 .block_size = SM3_CBLOCK, 64 .ctx_size = sizeof(EVP_MD *) + sizeof(SM3_CTX), 65 }; 66 67 const EVP_MD * 68 EVP_sm3(void) 69 { 70 return &sm3_md; 71 } 72 73 #endif /* OPENSSL_NO_SM3 */ 74