summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/sm2/sm2.h
diff options
context:
space:
mode:
authortb <>2021-08-18 16:04:32 +0000
committertb <>2021-08-18 16:04:32 +0000
commit27a4a421da356671ca87fff69b8b7340d284659c (patch)
treeff95b8757df3a4b8d97a0f3f19a7c9962ba1076f /src/lib/libcrypto/sm2/sm2.h
parente9cfb6e09338ad2081dfa06cdae4acc1dc0c78d9 (diff)
downloadopenbsd-27a4a421da356671ca87fff69b8b7340d284659c.tar.gz
openbsd-27a4a421da356671ca87fff69b8b7340d284659c.tar.bz2
openbsd-27a4a421da356671ca87fff69b8b7340d284659c.zip
Import initial code for the SM2 cipher
This adds the SM2 algorithm defined in the Chinese standards GB/T 32918.1-2016, GB/T 32918.2-2016, GB/T 32918.3-2016, GB/T 32918.4-2016 and GB/T 32918.5-2017. This is an ISC licensed implementation contributed by Ribose.inc, based on the same code that was contributed to OpenSSL by Jack Lloyd. The port to LibreSSL was done by Ronald Tse and Nickolay Olshevsky. Github PR #105 I made quite a few cleanup passes on this, but more is needed, some of which will happen in-tree before this is linked to the build. ok deraadt inoguchi (a long time ago), jsing
Diffstat (limited to 'src/lib/libcrypto/sm2/sm2.h')
-rw-r--r--src/lib/libcrypto/sm2/sm2.h147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/lib/libcrypto/sm2/sm2.h b/src/lib/libcrypto/sm2/sm2.h
new file mode 100644
index 0000000000..92eef8a6de
--- /dev/null
+++ b/src/lib/libcrypto/sm2/sm2.h
@@ -0,0 +1,147 @@
1/* $OpenBSD: sm2.h,v 1.1.1.1 2021/08/18 16:04:32 tb Exp $ */
2/*
3 * Copyright (c) 2017, 2019 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#ifndef HEADER_SM2_H
19#define HEADER_SM2_H
20
21#include <openssl/opensslconf.h>
22
23#include <openssl/ec.h>
24#include <openssl/ecdsa.h>
25
26#ifdef OPENSSL_NO_SM2
27#error SM2 is disabled.
28#endif
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/*
35 * SM2 signature generation.
36 */
37int SM2_sign(const unsigned char *dgst, int dgstlen, unsigned char *sig,
38 unsigned int *siglen, EC_KEY *eckey);
39
40/*
41 * SM2 signature verification. Assumes input is an SM3 digest
42 */
43int SM2_verify(const unsigned char *dgst, int dgstlen, const unsigned char *sig,
44 int siglen, EC_KEY *eckey);
45
46/*
47 * SM2 encryption
48 */
49int SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
50 size_t *c_size);
51
52int SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
53 size_t *pl_size);
54
55int SM2_encrypt(const EC_KEY *key, const EVP_MD *digest, const uint8_t *msg,
56 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len);
57
58int SM2_decrypt(const EC_KEY *key, const EVP_MD *digest,
59 const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *ptext_buf,
60 size_t *ptext_len);
61
62/* BEGIN ERROR CODES */
63/* The following lines are auto generated by the script mkerr.pl. Any changes
64 * made after this point may be overwritten when the script is next run.
65 */
66void ERR_load_SM2_strings(void);
67
68/* Error codes for the SM2 functions. */
69
70/* Function codes. */
71# define SM2_F_PKEY_SM2_CTRL 274
72# define SM2_F_PKEY_SM2_CTRL_STR 275
73# define SM2_F_PKEY_SM2_KEYGEN 276
74# define SM2_F_PKEY_SM2_PARAMGEN 277
75# define SM2_F_PKEY_SM2_SIGN 278
76# define SM2_F_PKEY_SM2_VERIFY 279
77# define SM2_F_PKEY_SM2_ENCRYPT 280
78# define SM2_F_PKEY_SM2_DECRYPT 281
79
80/* Reason codes. */
81# define SM2_R_ASN1_ERROR 115
82# define SM2_R_ASN5_ERROR 1150
83# define SM2_R_BAD_SIGNATURE 156
84# define SM2_R_BIGNUM_OUT_OF_RANGE 144
85# define SM2_R_BUFFER_TOO_SMALL 100
86# define SM2_R_COORDINATES_OUT_OF_RANGE 146
87# define SM2_R_CURVE_DOES_NOT_SUPPORT_ECDH 160
88# define SM2_R_CURVE_DOES_NOT_SUPPORT_SIGNING 159
89# define SM2_R_D2I_ECPKPARAMETERS_FAILURE 117
90# define SM2_R_DECODE_ERROR 142
91# define SM2_R_DIGEST_FAILURE 163
92# define SM2_R_DISCRIMINANT_IS_ZERO 118
93# define SM2_R_EC_GROUP_NEW_BY_NAME_FAILURE 119
94# define SM2_R_FIELD_TOO_LARGE 143
95# define SM2_R_GF2M_NOT_SUPPORTED 147
96# define SM2_R_GROUP2PKPARAMETERS_FAILURE 120
97# define SM2_R_I2D_ECPKPARAMETERS_FAILURE 121
98# define SM2_R_INCOMPATIBLE_OBJECTS 101
99# define SM2_R_INVALID_ARGUMENT 112
100# define SM2_R_INVALID_COMPRESSED_POINT 110
101# define SM2_R_INVALID_COMPRESSION_BIT 109
102# define SM2_R_INVALID_CURVE 141
103# define SM2_R_INVALID_DIGEST 151
104# define SM2_R_INVALID_DIGEST_TYPE 138
105# define SM2_R_INVALID_ENCODING 102
106# define SM2_R_INVALID_FIELD 103
107# define SM2_R_INVALID_FORM 104
108# define SM2_R_INVALID_GROUP_ORDER 122
109# define SM2_R_INVALID_KEY 116
110# define SM2_R_INVALID_OUTPUT_LENGTH 161
111# define SM2_R_INVALID_PEER_KEY 133
112# define SM2_R_INVALID_PENTANOMIAL_BASIS 132
113# define SM2_R_INVALID_PRIVATE_KEY 123
114# define SM2_R_INVALID_TRINOMIAL_BASIS 137
115# define SM2_R_KDF_FAILURE 162
116# define SM2_R_KDF_PARAMETER_ERROR 148
117# define SM2_R_KEYS_NOT_SET 140
118# define SM2_R_MISSING_PARAMETERS 124
119# define SM2_R_MISSING_PRIVATE_KEY 125
120# define SM2_R_NEED_NEW_SETUP_VALUES 157
121# define SM2_R_NOT_A_NIST_PRIME 135
122# define SM2_R_NOT_IMPLEMENTED 126
123# define SM2_R_NOT_INITIALIZED 111
124# define SM2_R_NO_PARAMETERS_SET 139
125# define SM2_R_NO_PRIVATE_VALUE 154
126# define SM2_R_OPERATION_NOT_SUPPORTED 152
127# define SM2_R_PASSED_NULL_PARAMETER 134
128# define SM2_R_PEER_KEY_ERROR 149
129# define SM2_R_PKPARAMETERS2GROUP_FAILURE 127
130# define SM2_R_POINT_ARITHMETIC_FAILURE 155
131# define SM2_R_POINT_AT_INFINITY 106
132# define SM2_R_POINT_IS_NOT_ON_CURVE 107
133# define SM2_R_RANDOM_NUMBER_GENERATION_FAILED 158
134# define SM2_R_SHARED_INFO_ERROR 150
135# define SM2_R_SLOT_FULL 108
136# define SM2_R_UNDEFINED_GENERATOR 113
137# define SM2_R_UNDEFINED_ORDER 128
138# define SM2_R_UNKNOWN_GROUP 129
139# define SM2_R_UNKNOWN_ORDER 114
140# define SM2_R_UNSUPPORTED_FIELD 131
141# define SM2_R_WRONG_CURVE_PARAMETERS 145
142# define SM2_R_WRONG_ORDER 130
143
144#ifdef __cplusplus
145}
146#endif
147#endif