blob: 26a90660ee5b31426ea9582b0ecf79094b158a3a [file] [log] [blame]
Jack Lloyd3d328a42018-01-24 11:56:02 -05001/*
Matt Caswellb0edda12018-03-20 13:00:17 +00002 * Copyright 2017-2018 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 *
6 * Licensed under the OpenSSL license (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 HEADER_SM2_H
13# define HEADER_SM2_H
Matt Caswell1bf2cc22018-03-19 16:17:58 +000014# include <openssl/opensslconf.h>
Jack Lloyd3d328a42018-01-24 11:56:02 -050015
Matt Caswell1bf2cc22018-03-19 16:17:58 +000016# ifndef OPENSSL_NO_SM2
17
18# include <openssl/ec.h>
Jack Lloyd3d328a42018-01-24 11:56:02 -050019
20/* The default user id as specified in GM/T 0009-2012 */
Matt Caswell1bf2cc22018-03-19 16:17:58 +000021# define SM2_DEFAULT_USERID "1234567812345678"
Jack Lloyd3d328a42018-01-24 11:56:02 -050022
Paul Yang00433ba2018-09-04 01:24:55 +080023int sm2_compute_userid_digest(uint8_t *out,
24 const EVP_MD *digest,
25 const uint8_t *id,
26 const size_t id_len,
27 const EC_KEY *key);
28
Jack Lloyd3d328a42018-01-24 11:56:02 -050029/*
30 * SM2 signature operation. Computes ZA (user id digest) and then signs
31 * H(ZA || msg) using SM2
32 */
Matt Caswell21672392018-05-31 15:53:30 +010033ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
Jack Lloyd3d328a42018-01-24 11:56:02 -050034 const EVP_MD *digest,
Paul Yang00433ba2018-09-04 01:24:55 +080035 const uint8_t *id,
36 const size_t id_len,
37 const uint8_t *msg, size_t msg_len);
Jack Lloyd3d328a42018-01-24 11:56:02 -050038
Matt Caswell21672392018-05-31 15:53:30 +010039int sm2_do_verify(const EC_KEY *key,
Jack Lloyd3d328a42018-01-24 11:56:02 -050040 const EVP_MD *digest,
41 const ECDSA_SIG *signature,
Paul Yang00433ba2018-09-04 01:24:55 +080042 const uint8_t *id,
43 const size_t id_len,
44 const uint8_t *msg, size_t msg_len);
Jack Lloyd3d328a42018-01-24 11:56:02 -050045
46/*
Jack Lloydddb634f2018-06-18 15:51:56 -040047 * SM2 signature generation.
Jack Lloyd3d328a42018-01-24 11:56:02 -050048 */
Jack Lloydddb634f2018-06-18 15:51:56 -040049int sm2_sign(const unsigned char *dgst, int dgstlen,
Jack Lloyd3d328a42018-01-24 11:56:02 -050050 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
51
52/*
Jack Lloydddb634f2018-06-18 15:51:56 -040053 * SM2 signature verification.
Jack Lloyd3d328a42018-01-24 11:56:02 -050054 */
Jack Lloydddb634f2018-06-18 15:51:56 -040055int sm2_verify(const unsigned char *dgst, int dgstlen,
Jack Lloyd3d328a42018-01-24 11:56:02 -050056 const unsigned char *sig, int siglen, EC_KEY *eckey);
57
Jack Lloyd3d328a42018-01-24 11:56:02 -050058/*
59 * SM2 encryption
60 */
Matt Caswell21672392018-05-31 15:53:30 +010061int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
Matt Caswelle14d6cf2018-05-31 13:49:47 +010062 size_t *ct_size);
Jack Lloyd3d328a42018-01-24 11:56:02 -050063
Matt Caswell21672392018-05-31 15:53:30 +010064int sm2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
Matt Caswelle14d6cf2018-05-31 13:49:47 +010065 size_t *pt_size);
Jack Lloyd4e664752018-02-09 12:21:56 -050066
Matt Caswell21672392018-05-31 15:53:30 +010067int sm2_encrypt(const EC_KEY *key,
Jack Lloyd3d328a42018-01-24 11:56:02 -050068 const EVP_MD *digest,
69 const uint8_t *msg,
70 size_t msg_len,
71 uint8_t *ciphertext_buf, size_t *ciphertext_len);
72
Matt Caswell21672392018-05-31 15:53:30 +010073int sm2_decrypt(const EC_KEY *key,
Jack Lloyd3d328a42018-01-24 11:56:02 -050074 const EVP_MD *digest,
75 const uint8_t *ciphertext,
76 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len);
77
Matt Caswell1bf2cc22018-03-19 16:17:58 +000078# endif /* OPENSSL_NO_SM2 */
Jack Lloyd3d328a42018-01-24 11:56:02 -050079#endif