blob: 9ab6c0b722364b9e8554e2093675300e6a90bbe6 [file] [log] [blame]
Jack Lloyd3d328a42018-01-24 11:56:02 -05001/*
Richard Levitte4333b892021-01-28 13:54:57 +01002 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
Jack Lloyd3d328a42018-01-24 11:56:02 -05003 * Copyright 2017 Ribose Inc. All Rights Reserved.
4 * Ported from Ribose contributions from Botan.
5 *
Richard Levitte48f4ad72018-12-06 13:12:35 +01006 * Licensed under the Apache License 2.0 (the "License"). You may not use
Jack Lloyd3d328a42018-01-24 11:56:02 -05007 * 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
Dr. Matthias St. Pierreae4186b2019-09-28 00:45:57 +020012#ifndef OSSL_CRYPTO_SM2_H
13# define OSSL_CRYPTO_SM2_H
FdaSilvaYY80ce21f2021-02-06 22:36:46 +010014# pragma once
15
Matt Caswell1bf2cc22018-03-19 16:17:58 +000016# include <openssl/opensslconf.h>
Jack Lloyd3d328a42018-01-24 11:56:02 -050017
Tomas Mrazdce72722021-06-04 17:01:24 +020018# if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
Matt Caswell1bf2cc22018-03-19 16:17:58 +000019
20# include <openssl/ec.h>
Shane Lontis5b5eea42020-10-15 13:41:59 +100021# include "crypto/types.h"
Jack Lloyd3d328a42018-01-24 11:56:02 -050022
Shane Lontis32ab57c2021-02-18 20:27:26 +100023int ossl_sm2_key_private_check(const EC_KEY *eckey);
Nicola Tuveri9e49aff2020-11-10 01:11:48 +020024
Jack Lloyd3d328a42018-01-24 11:56:02 -050025/* The default user id as specified in GM/T 0009-2012 */
Matt Caswell1bf2cc22018-03-19 16:17:58 +000026# define SM2_DEFAULT_USERID "1234567812345678"
Jack Lloyd3d328a42018-01-24 11:56:02 -050027
Shane Lontis32ab57c2021-02-18 20:27:26 +100028int 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);
Paul Yang00433ba2018-09-04 01:24:55 +080033
Jack Lloyd3d328a42018-01-24 11:56:02 -050034/*
Paul Yang48037172018-09-04 17:21:10 +080035 * SM2 signature operation. Computes Z and then signs H(Z || msg) using SM2
Jack Lloyd3d328a42018-01-24 11:56:02 -050036 */
Shane Lontis32ab57c2021-02-18 20:27:26 +100037ECDSA_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
43int ossl_sm2_do_verify(const EC_KEY *key,
Jack Lloyd3d328a42018-01-24 11:56:02 -050044 const EVP_MD *digest,
Shane Lontis32ab57c2021-02-18 20:27:26 +100045 const ECDSA_SIG *signature,
Paul Yang00433ba2018-09-04 01:24:55 +080046 const uint8_t *id,
47 const size_t id_len,
48 const uint8_t *msg, size_t msg_len);
Jack Lloyd3d328a42018-01-24 11:56:02 -050049
Jack Lloyd3d328a42018-01-24 11:56:02 -050050/*
Jack Lloydddb634f2018-06-18 15:51:56 -040051 * SM2 signature generation.
Jack Lloyd3d328a42018-01-24 11:56:02 -050052 */
Shane Lontis32ab57c2021-02-18 20:27:26 +100053int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
54 unsigned char *sig, unsigned int *siglen,
55 EC_KEY *eckey);
Jack Lloyd3d328a42018-01-24 11:56:02 -050056
57/*
Jack Lloydddb634f2018-06-18 15:51:56 -040058 * SM2 signature verification.
Jack Lloyd3d328a42018-01-24 11:56:02 -050059 */
Shane Lontis32ab57c2021-02-18 20:27:26 +100060int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
61 const unsigned char *sig, int siglen,
62 EC_KEY *eckey);
Jack Lloyd3d328a42018-01-24 11:56:02 -050063
Jack Lloyd3d328a42018-01-24 11:56:02 -050064/*
65 * SM2 encryption
66 */
Shane Lontis32ab57c2021-02-18 20:27:26 +100067int ossl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest,
68 size_t msg_len, size_t *ct_size);
Jack Lloyd3d328a42018-01-24 11:56:02 -050069
Matt Caswell36cf45e2021-08-13 14:14:51 +010070int ossl_sm2_plaintext_size(const unsigned char *ct, size_t ct_size,
71 size_t *pt_size);
Jack Lloyd4e664752018-02-09 12:21:56 -050072
Shane Lontis32ab57c2021-02-18 20:27:26 +100073int 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);
Jack Lloyd3d328a42018-01-24 11:56:02 -050077
Shane Lontis32ab57c2021-02-18 20:27:26 +100078int 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);
Jack Lloyd3d328a42018-01-24 11:56:02 -050082
Shane Lontis32ab57c2021-02-18 20:27:26 +100083const unsigned char *ossl_sm2_algorithmidentifier_encoding(int md_nid,
84 size_t *len);
Matt Caswell1bf2cc22018-03-19 16:17:58 +000085# endif /* OPENSSL_NO_SM2 */
Jack Lloyd3d328a42018-01-24 11:56:02 -050086#endif