diff options
author | Jack Lloyd <jack.lloyd@ribose.com> | 2018-01-24 11:56:02 -0500 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2018-03-19 14:33:25 +0100 |
commit | 3d328a445c2ad0eff2e9e843c384711be58a7c2f (patch) | |
tree | 888d7dee8ebe744a5fd6a62a6b12c7b6b72bee83 /include | |
parent | df3a15512bd0f5ddd9f0dd74f0a058ee55b33904 (diff) |
Add SM2 signature and ECIES schemes
Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4793)
Diffstat (limited to 'include')
-rw-r--r-- | include/openssl/err.h | 2 | ||||
-rw-r--r-- | include/openssl/evp.h | 1 | ||||
-rw-r--r-- | include/openssl/obj_mac.h | 35 | ||||
-rw-r--r-- | include/openssl/sm2.h | 70 | ||||
-rw-r--r-- | include/openssl/sm2err.h | 95 |
5 files changed, 188 insertions, 15 deletions
diff --git a/include/openssl/err.h b/include/openssl/err.h index 6d460be30c..e435b5e84a 100644 --- a/include/openssl/err.h +++ b/include/openssl/err.h @@ -93,6 +93,7 @@ typedef struct err_state_st { # define ERR_LIB_CT 50 # define ERR_LIB_ASYNC 51 # define ERR_LIB_KDF 52 +# define ERR_LIB_SM2 53 # define ERR_LIB_USER 128 @@ -131,6 +132,7 @@ typedef struct err_state_st { # define CTerr(f,r) ERR_PUT_error(ERR_LIB_CT,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define ASYNCerr(f,r) ERR_PUT_error(ERR_LIB_ASYNC,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define KDFerr(f,r) ERR_PUT_error(ERR_LIB_KDF,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define SM2err(f,r) ERR_PUT_error(ERR_LIB_SM2,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define OSSL_STOREerr(f,r) ERR_PUT_error(ERR_LIB_OSSL_STORE,(f),(r),OPENSSL_FILE,OPENSSL_LINE) # define ERR_PACK(l,f,r) ( \ diff --git a/include/openssl/evp.h b/include/openssl/evp.h index 8b81b12362..29fd3e28fa 100644 --- a/include/openssl/evp.h +++ b/include/openssl/evp.h @@ -50,6 +50,7 @@ # define EVP_PKEY_DH NID_dhKeyAgreement # define EVP_PKEY_DHX NID_dhpublicnumber # define EVP_PKEY_EC NID_X9_62_id_ecPublicKey +# define EVP_PKEY_SM2 NID_sm2 # define EVP_PKEY_HMAC NID_hmac # define EVP_PKEY_CMAC NID_cmac # define EVP_PKEY_SCRYPT NID_id_scrypt diff --git a/include/openssl/obj_mac.h b/include/openssl/obj_mac.h index cc05f3f6e9..2078dc4995 100644 --- a/include/openssl/obj_mac.h +++ b/include/openssl/obj_mac.h @@ -109,6 +109,19 @@ #define NID_X9cm 185 #define OBJ_X9cm OBJ_X9_57,4L +#define SN_ISO_CN "ISO-CN" +#define LN_ISO_CN "ISO CN Member Body" +#define NID_ISO_CN 1140 +#define OBJ_ISO_CN OBJ_member_body,156L + +#define SN_oscca "oscca" +#define NID_oscca 1141 +#define OBJ_oscca OBJ_ISO_CN,10197L + +#define SN_sm_scheme "sm-scheme" +#define NID_sm_scheme 1142 +#define OBJ_sm_scheme OBJ_oscca,1L + #define SN_dsa "DSA" #define LN_dsa "dsaEncryption" #define NID_dsa 116 @@ -1151,15 +1164,20 @@ #define NID_hmacWithSHA1 163 #define OBJ_hmacWithSHA1 OBJ_rsadsi,2L,7L +#define SN_sm2 "SM2" +#define LN_sm2 "sm2" +#define NID_sm2 1172 +#define OBJ_sm2 OBJ_sm_scheme,301L + #define SN_sm3 "SM3" #define LN_sm3 "sm3" #define NID_sm3 1143 -#define OBJ_sm3 OBJ_member_body,156L,10197L,1L,401L +#define OBJ_sm3 OBJ_sm_scheme,401L #define SN_sm3WithRSAEncryption "RSA-SM3" #define LN_sm3WithRSAEncryption "sm3WithRSAEncryption" #define NID_sm3WithRSAEncryption 1144 -#define OBJ_sm3WithRSAEncryption OBJ_member_body,156L,10197L,1L,504L +#define OBJ_sm3WithRSAEncryption OBJ_sm_scheme,504L #define LN_hmacWithSHA224 "hmacWithSHA224" #define NID_hmacWithSHA224 798 @@ -4629,19 +4647,6 @@ #define NID_seed_ofb128 778 #define OBJ_seed_ofb128 OBJ_kisa,1L,6L -#define SN_ISO_CN "ISO-CN" -#define LN_ISO_CN "ISO CN Member Body" -#define NID_ISO_CN 1140 -#define OBJ_ISO_CN OBJ_member_body,156L - -#define SN_oscca "oscca" -#define NID_oscca 1141 -#define OBJ_oscca OBJ_ISO_CN,10197L - -#define SN_sm_scheme "sm-scheme" -#define NID_sm_scheme 1142 -#define OBJ_sm_scheme OBJ_oscca,1L - #define SN_sm4_ecb "SM4-ECB" #define LN_sm4_ecb "sm4-ecb" #define NID_sm4_ecb 1133 diff --git a/include/openssl/sm2.h b/include/openssl/sm2.h new file mode 100644 index 0000000000..d12dcadef2 --- /dev/null +++ b/include/openssl/sm2.h @@ -0,0 +1,70 @@ +/* + * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2017 Ribose Inc. All Rights Reserved. + * Ported from Ribose contributions from Botan. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SM2_H +# define HEADER_SM2_H + +# include <openssl/ec.h> + +/* The default user id as specified in GM/T 0009-2012 */ +# define SM2_DEFAULT_USERID "1234567812345678" + +int SM2_compute_userid_digest(uint8_t *out, + const EVP_MD *digest, + const char *user_id, const EC_KEY *key); + +/* + * SM2 signature operation. Computes ZA (user id digest) and then signs + * H(ZA || msg) using SM2 + */ +ECDSA_SIG *SM2_do_sign(const EC_KEY *key, + const EVP_MD *digest, + const char *user_id, const uint8_t *msg, size_t msg_len); + +int SM2_do_verify(const EC_KEY *key, + const EVP_MD *digest, + const ECDSA_SIG *signature, + const char *user_id, const uint8_t *msg, size_t msg_len); + +/* + * SM2 signature generation. Assumes input is an SM3 digest + */ +int SM2_sign(int type, const unsigned char *dgst, int dgstlen, + unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); + +/* + * SM2 signature verification. Assumes input is an SM3 digest + */ +int SM2_verify(int type, const unsigned char *dgst, int dgstlen, + const unsigned char *sig, int siglen, EC_KEY *eckey); + + +/* + * SM2 encryption + */ +size_t SM2_ciphertext_size(const EC_KEY *key, + const EVP_MD *digest, + size_t msg_len); + +int SM2_encrypt(const EC_KEY *key, + const EVP_MD *digest, + const uint8_t *msg, + size_t msg_len, + uint8_t *ciphertext_buf, size_t *ciphertext_len); + +int SM2_decrypt(const EC_KEY *key, + const EVP_MD *digest, + const uint8_t *ciphertext, + size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len); + +int ERR_load_SM2_strings(void); + +#endif diff --git a/include/openssl/sm2err.h b/include/openssl/sm2err.h new file mode 100644 index 0000000000..0c2dd1927e --- /dev/null +++ b/include/openssl/sm2err.h @@ -0,0 +1,95 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SM2ERR_H +# define HEADER_SM2ERR_H + +# ifdef __cplusplus +extern "C" { +# endif +int ERR_load_SM2_strings(void); +# ifdef __cplusplus +} +# endif + +/* + * SM2 function codes. + */ +# define SM2_F_PKEY_SM2_CTRL 274 +# define SM2_F_PKEY_SM2_CTRL_STR 275 +# define SM2_F_PKEY_SM2_KEYGEN 276 +# define SM2_F_PKEY_SM2_PARAMGEN 277 +# define SM2_F_PKEY_SM2_SIGN 278 + +/* + * SM2 reason codes. + */ +# define SM2_R_ASN1_ERROR 115 +# define SM2_R_ASN5_ERROR 1150 +# define SM2_R_BAD_SIGNATURE 156 +# define SM2_R_BIGNUM_OUT_OF_RANGE 144 +# define SM2_R_BUFFER_TOO_SMALL 100 +# define SM2_R_COORDINATES_OUT_OF_RANGE 146 +# define SM2_R_CURVE_DOES_NOT_SUPPORT_ECDH 160 +# define SM2_R_CURVE_DOES_NOT_SUPPORT_SIGNING 159 +# define SM2_R_D2I_ECPKPARAMETERS_FAILURE 117 +# define SM2_R_DECODE_ERROR 142 +# define SM2_R_DISCRIMINANT_IS_ZERO 118 +# define SM2_R_EC_GROUP_NEW_BY_NAME_FAILURE 119 +# define SM2_R_FIELD_TOO_LARGE 143 +# define SM2_R_GF2M_NOT_SUPPORTED 147 +# define SM2_R_GROUP2PKPARAMETERS_FAILURE 120 +# define SM2_R_I2D_ECPKPARAMETERS_FAILURE 121 +# define SM2_R_INCOMPATIBLE_OBJECTS 101 +# define SM2_R_INVALID_ARGUMENT 112 +# define SM2_R_INVALID_COMPRESSED_POINT 110 +# define SM2_R_INVALID_COMPRESSION_BIT 109 +# define SM2_R_INVALID_CURVE 141 +# define SM2_R_INVALID_DIGEST 151 +# define SM2_R_INVALID_DIGEST_TYPE 138 +# define SM2_R_INVALID_ENCODING 102 +# define SM2_R_INVALID_FIELD 103 +# define SM2_R_INVALID_FORM 104 +# define SM2_R_INVALID_GROUP_ORDER 122 +# define SM2_R_INVALID_KEY 116 +# define SM2_R_INVALID_OUTPUT_LENGTH 161 +# define SM2_R_INVALID_PEER_KEY 133 +# define SM2_R_INVALID_PENTANOMIAL_BASIS 132 +# define SM2_R_INVALID_PRIVATE_KEY 123 +# define SM2_R_INVALID_TRINOMIAL_BASIS 137 +# define SM2_R_KDF_PARAMETER_ERROR 148 +# define SM2_R_KEYS_NOT_SET 140 +# define SM2_R_MISSING_PARAMETERS 124 +# define SM2_R_MISSING_PRIVATE_KEY 125 +# define SM2_R_NEED_NEW_SETUP_VALUES 157 +# define SM2_R_NOT_A_NIST_PRIME 135 +# define SM2_R_NOT_IMPLEMENTED 126 +# define SM2_R_NOT_INITIALIZED 111 +# define SM2_R_NO_PARAMETERS_SET 139 +# define SM2_R_NO_PRIVATE_VALUE 154 +# define SM2_R_OPERATION_NOT_SUPPORTED 152 +# define SM2_R_PASSED_NULL_PARAMETER 134 +# define SM2_R_PEER_KEY_ERROR 149 +# define SM2_R_PKPARAMETERS2GROUP_FAILURE 127 +# define SM2_R_POINT_ARITHMETIC_FAILURE 155 +# define SM2_R_POINT_AT_INFINITY 106 +# define SM2_R_POINT_IS_NOT_ON_CURVE 107 +# define SM2_R_RANDOM_NUMBER_GENERATION_FAILED 158 +# define SM2_R_SHARED_INFO_ERROR 150 +# define SM2_R_SLOT_FULL 108 +# define SM2_R_UNDEFINED_GENERATOR 113 +# define SM2_R_UNDEFINED_ORDER 128 +# define SM2_R_UNKNOWN_GROUP 129 +# define SM2_R_UNKNOWN_ORDER 114 +# define SM2_R_UNSUPPORTED_FIELD 131 +# define SM2_R_WRONG_CURVE_PARAMETERS 145 +# define SM2_R_WRONG_ORDER 130 + +#endif |