Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 1 | /* |
Richard Levitte | 4333b89 | 2021-01-28 13:54:57 +0100 | [diff] [blame] | 2 | * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 3 | * Copyright 2017 Ribose Inc. All Rights Reserved. |
| 4 | * Ported from Ribose contributions from Botan. |
| 5 | * |
Richard Levitte | 48f4ad7 | 2018-12-06 13:12:35 +0100 | [diff] [blame] | 6 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 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 | |
Dr. Matthias St. Pierre | ae4186b | 2019-09-28 00:45:57 +0200 | [diff] [blame] | 12 | #ifndef OSSL_CRYPTO_SM2_H |
| 13 | # define OSSL_CRYPTO_SM2_H |
FdaSilvaYY | 80ce21f | 2021-02-06 22:36:46 +0100 | [diff] [blame] | 14 | # pragma once |
| 15 | |
Matt Caswell | 1bf2cc2 | 2018-03-19 16:17:58 +0000 | [diff] [blame] | 16 | # include <openssl/opensslconf.h> |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 17 | |
Tomas Mraz | dce7272 | 2021-06-04 17:01:24 +0200 | [diff] [blame] | 18 | # if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE) |
Matt Caswell | 1bf2cc2 | 2018-03-19 16:17:58 +0000 | [diff] [blame] | 19 | |
| 20 | # include <openssl/ec.h> |
Shane Lontis | 5b5eea4 | 2020-10-15 13:41:59 +1000 | [diff] [blame] | 21 | # include "crypto/types.h" |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 22 | |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 23 | int ossl_sm2_key_private_check(const EC_KEY *eckey); |
Nicola Tuveri | 9e49aff | 2020-11-10 01:11:48 +0200 | [diff] [blame] | 24 | |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 25 | /* The default user id as specified in GM/T 0009-2012 */ |
Matt Caswell | 1bf2cc2 | 2018-03-19 16:17:58 +0000 | [diff] [blame] | 26 | # define SM2_DEFAULT_USERID "1234567812345678" |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 27 | |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 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); |
Paul Yang | 00433ba | 2018-09-04 01:24:55 +0800 | [diff] [blame] | 33 | |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 34 | /* |
Paul Yang | 4803717 | 2018-09-04 17:21:10 +0800 | [diff] [blame] | 35 | * SM2 signature operation. Computes Z and then signs H(Z || msg) using SM2 |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 36 | */ |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 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, |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 44 | const EVP_MD *digest, |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 45 | const ECDSA_SIG *signature, |
Paul Yang | 00433ba | 2018-09-04 01:24:55 +0800 | [diff] [blame] | 46 | const uint8_t *id, |
| 47 | const size_t id_len, |
| 48 | const uint8_t *msg, size_t msg_len); |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 49 | |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 50 | /* |
Jack Lloyd | ddb634f | 2018-06-18 15:51:56 -0400 | [diff] [blame] | 51 | * SM2 signature generation. |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 52 | */ |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 53 | int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen, |
| 54 | unsigned char *sig, unsigned int *siglen, |
| 55 | EC_KEY *eckey); |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 56 | |
| 57 | /* |
Jack Lloyd | ddb634f | 2018-06-18 15:51:56 -0400 | [diff] [blame] | 58 | * SM2 signature verification. |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 59 | */ |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 60 | int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen, |
| 61 | const unsigned char *sig, int siglen, |
| 62 | EC_KEY *eckey); |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 63 | |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 64 | /* |
| 65 | * SM2 encryption |
| 66 | */ |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 67 | int ossl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, |
| 68 | size_t msg_len, size_t *ct_size); |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 69 | |
Matt Caswell | 36cf45e | 2021-08-13 14:14:51 +0100 | [diff] [blame] | 70 | int ossl_sm2_plaintext_size(const unsigned char *ct, size_t ct_size, |
| 71 | size_t *pt_size); |
Jack Lloyd | 4e66475 | 2018-02-09 12:21:56 -0500 | [diff] [blame] | 72 | |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 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); |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 77 | |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 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); |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 82 | |
Shane Lontis | 32ab57c | 2021-02-18 20:27:26 +1000 | [diff] [blame] | 83 | const unsigned char *ossl_sm2_algorithmidentifier_encoding(int md_nid, |
| 84 | size_t *len); |
Matt Caswell | 1bf2cc2 | 2018-03-19 16:17:58 +0000 | [diff] [blame] | 85 | # endif /* OPENSSL_NO_SM2 */ |
Jack Lloyd | 3d328a4 | 2018-01-24 11:56:02 -0500 | [diff] [blame] | 86 | #endif |