1 /* $OpenBSD: m_sm3.c,v 1.3 2022/01/14 08:38:06 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 #include "evp_locl.h"
29
30 static int
sm3_init(EVP_MD_CTX * ctx)31 sm3_init(EVP_MD_CTX *ctx)
32 {
33 return SM3_Init(ctx->md_data);
34 }
35
36 static int
sm3_update(EVP_MD_CTX * ctx,const void * data,size_t count)37 sm3_update(EVP_MD_CTX *ctx, const void *data, size_t count)
38 {
39 return SM3_Update(ctx->md_data, data, count);
40 }
41
42 static int
sm3_final(EVP_MD_CTX * ctx,unsigned char * md)43 sm3_final(EVP_MD_CTX *ctx, unsigned char *md)
44 {
45 return SM3_Final(md, ctx->md_data);
46 }
47
48 static const EVP_MD sm3_md = {
49 .type = NID_sm3,
50 .pkey_type = NID_sm3WithRSAEncryption,
51 .md_size = SM3_DIGEST_LENGTH,
52 .flags = EVP_MD_FLAG_DIGALGID_ABSENT,
53 .init = sm3_init,
54 .update = sm3_update,
55 .final = sm3_final,
56 .copy = NULL,
57 .cleanup = NULL,
58 .block_size = SM3_CBLOCK,
59 .ctx_size = sizeof(EVP_MD *) + sizeof(SM3_CTX),
60 };
61
62 const EVP_MD *
EVP_sm3(void)63 EVP_sm3(void)
64 {
65 return &sm3_md;
66 }
67
68 #endif /* OPENSSL_NO_SM3 */
69