xref: /freebsd/crypto/openssl/include/crypto/sm2.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*
2  * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2017 Ribose Inc. All Rights Reserved.
4  * Ported from Ribose contributions from Botan.
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11 
12 #ifndef OSSL_CRYPTO_SM2_H
13 # define OSSL_CRYPTO_SM2_H
14 # pragma once
15 
16 # include <openssl/opensslconf.h>
17 
18 # if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
19 
20 #  include <openssl/ec.h>
21 #  include "crypto/types.h"
22 
23 int ossl_sm2_key_private_check(const EC_KEY *eckey);
24 
25 /* The default user id as specified in GM/T 0009-2012 */
26 #  define SM2_DEFAULT_USERID "1234567812345678"
27 
28 int ossl_sm2_compute_z_digest(uint8_t *out,
29                               const EVP_MD *digest,
30                               const uint8_t *id,
31                               const size_t id_len,
32                               const EC_KEY *key);
33 
34 /*
35  * SM2 signature operation. Computes Z and then signs H(Z || msg) using SM2
36  */
37 ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
38                             const EVP_MD *digest,
39                             const uint8_t *id,
40                             const size_t id_len,
41                             const uint8_t *msg, size_t msg_len);
42 
43 int ossl_sm2_do_verify(const EC_KEY *key,
44                        const EVP_MD *digest,
45                        const ECDSA_SIG *signature,
46                        const uint8_t *id,
47                        const size_t id_len,
48                        const uint8_t *msg, size_t msg_len);
49 
50 /*
51  * SM2 signature generation.
52  */
53 int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
54                            unsigned char *sig, unsigned int *siglen,
55                            EC_KEY *eckey);
56 
57 /*
58  * SM2 signature verification.
59  */
60 int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
61                              const unsigned char *sig, int siglen,
62                              EC_KEY *eckey);
63 
64 /*
65  * SM2 encryption
66  */
67 int ossl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest,
68                              size_t msg_len, size_t *ct_size);
69 
70 int ossl_sm2_plaintext_size(const unsigned char *ct, size_t ct_size,
71                             size_t *pt_size);
72 
73 int ossl_sm2_encrypt(const EC_KEY *key,
74                      const EVP_MD *digest,
75                      const uint8_t *msg, size_t msg_len,
76                      uint8_t *ciphertext_buf, size_t *ciphertext_len);
77 
78 int ossl_sm2_decrypt(const EC_KEY *key,
79                      const EVP_MD *digest,
80                      const uint8_t *ciphertext, size_t ciphertext_len,
81                      uint8_t *ptext_buf, size_t *ptext_len);
82 
83 const unsigned char *ossl_sm2_algorithmidentifier_encoding(int md_nid,
84                                                            size_t *len);
85 # endif /* OPENSSL_NO_SM2 */
86 #endif
87