Add sm9 and paillier pem support

This commit is contained in:
Zhi Guan
2018-11-16 15:26:30 +08:00
parent 5baad7e043
commit 9a999ae907
38 changed files with 7956 additions and 6992 deletions

View File

@@ -44,7 +44,14 @@ static const EVP_PKEY_ASN1_METHOD *standard_methods[] = {
&dhx_asn1_meth,
#endif
#ifndef OPENSSL_NO_EC
&ecx25519_asn1_meth
&ecx25519_asn1_meth,
#endif
#ifndef OPENSSL_NO_SM9
&sm9_asn1_meth,
&sm9_master_asn1_meth,
#endif
#ifndef OPENSSL_NO_PAILLIER
&paillier_asn1_meth,
#endif
};

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2018 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
@@ -99,6 +99,8 @@ static ERR_STRING_DATA ASN1_str_functs[] = {
{ERR_FUNC(ASN1_F_I2D_PRIVATEKEY), "i2d_PrivateKey"},
{ERR_FUNC(ASN1_F_I2D_PUBLICKEY), "i2d_PublicKey"},
{ERR_FUNC(ASN1_F_I2D_RSA_PUBKEY), "i2d_RSA_PUBKEY"},
{ERR_FUNC(ASN1_F_I2D_SM9_MASTER_PUBKEY), "i2d_SM9_MASTER_PUBKEY"},
{ERR_FUNC(ASN1_F_I2D_SM9_PUBKEY), "i2d_SM9_PUBKEY"},
{ERR_FUNC(ASN1_F_LONG_C2I), "long_c2i"},
{ERR_FUNC(ASN1_F_OID_MODULE_INIT), "oid_module_init"},
{ERR_FUNC(ASN1_F_PARSE_TAGGING), "parse_tagging"},

View File

@@ -55,6 +55,7 @@ static ERR_STRING_DATA CPK_str_reasons[] = {
{ERR_REASON(CPK_R_BAD_DATA), "bad data"},
{ERR_REASON(CPK_R_INVALID_ALGORITHM), "invalid algorithm"},
{ERR_REASON(CPK_R_INVALID_ARGUMENT), "invalid argument"},
{ERR_REASON(CPK_R_INVALID_CURVE), "invalid curve"},
{ERR_REASON(CPK_R_INVALID_ID_LENGTH), "invalid id length"},
{ERR_REASON(CPK_R_INVALID_MAP_ALGOR), "invalid map algor"},
{ERR_REASON(CPK_R_INVALID_PKEY_TYPE), "invalid pkey type"},

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2018 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
@@ -70,6 +70,12 @@ static ERR_STRING_DATA EVP_str_functs[] = {
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_HMAC), "EVP_PKEY_get0_hmac"},
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_PAILLIER), "EVP_PKEY_get0_PAILLIER"},
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_RSA), "EVP_PKEY_get0_RSA"},
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_SM9), "EVP_PKEY_get0_SM9"},
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_SM9MASTERSECRET),
"EVP_PKEY_get0_SM9MasterSecret"},
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_SM9PUBLICPARAMETERS),
"EVP_PKEY_get0_SM9PublicParameters"},
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_SM9_MASTER), "EVP_PKEY_get0_SM9_MASTER"},
{ERR_FUNC(EVP_F_EVP_PKEY_KEYGEN), "EVP_PKEY_keygen"},
{ERR_FUNC(EVP_F_EVP_PKEY_KEYGEN_INIT), "EVP_PKEY_keygen_init"},
{ERR_FUNC(EVP_F_EVP_PKEY_NEW), "EVP_PKEY_new"},
@@ -120,6 +126,11 @@ static ERR_STRING_DATA EVP_str_reasons[] = {
{ERR_REASON(EVP_R_EXPECTING_A_DSA_KEY), "expecting a dsa key"},
{ERR_REASON(EVP_R_EXPECTING_A_EC_KEY), "expecting a ec key"},
{ERR_REASON(EVP_R_EXPECTING_A_PAILLIER), "expecting a paillier"},
{ERR_REASON(EVP_R_EXPECTING_A_SM9_KEY), "expecting a sm9 key"},
{ERR_REASON(EVP_R_EXPECTING_A_SM9_MASTER_KEY),
"expecting a sm9 master key"},
{ERR_REASON(EVP_R_EXPECTING_A_SM9_MASTER_SECRET),
"expecting a sm9 master secret"},
{ERR_REASON(EVP_R_FIPS_MODE_NOT_SUPPORTED), "fips mode not supported"},
{ERR_REASON(EVP_R_ILLEGAL_SCRYPT_PARAMETERS),
"illegal scrypt parameters"},

View File

@@ -370,6 +370,60 @@ PAILLIER *EVP_PKEY_get1_PAILLIER(EVP_PKEY *pkey)
}
#endif
#ifndef OPENSSL_NO_SM9
int EVP_PKEY_set1_SM9_MASTER(EVP_PKEY *pkey, SM9_MASTER_KEY *key)
{
int ret = EVP_PKEY_assign_SM9_MASTER(pkey, key);
if (ret)
SM9_MASTER_KEY_up_ref(key);
return ret;
}
SM9_MASTER_KEY *EVP_PKEY_get0_SM9_MASTER(EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_SM9_MASTER) {
EVPerr(EVP_F_EVP_PKEY_GET0_SM9_MASTER, EVP_R_EXPECTING_A_SM9_MASTER_KEY);
return NULL;
}
return pkey->pkey.sm9_master;
}
SM9_MASTER_KEY *EVP_PKEY_get1_SM9_MASTER(EVP_PKEY *pkey)
{
SM9_MASTER_KEY *ret = EVP_PKEY_get0_SM9_MASTER(pkey);
if (ret != NULL)
SM9_MASTER_KEY_up_ref(ret);
return ret;
}
int EVP_PKEY_set1_SM9(EVP_PKEY *pkey, SM9_KEY *key)
{
int ret = EVP_PKEY_assign_SM9(pkey, key);
if (ret)
SM9_KEY_up_ref(key);
return ret;
}
SM9_KEY *EVP_PKEY_get0_SM9(EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_SM9) {
EVPerr(EVP_F_EVP_PKEY_GET0_SM9, EVP_R_EXPECTING_A_SM9_KEY);
return NULL;
}
return pkey->pkey.sm9;
}
SM9_KEY *EVP_PKEY_get1_SM9(EVP_PKEY *pkey)
{
SM9_KEY *ret = EVP_PKEY_get0_SM9(pkey);
if (ret != NULL)
SM9_KEY_up_ref(ret);
return ret;
}
#endif
#ifndef OPENSSL_NO_DH
int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)

View File

@@ -64,6 +64,13 @@ extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2];
#ifndef OPENSSL_NO_PAILLIER
extern const EVP_PKEY_ASN1_METHOD paillier_asn1_meth;
#endif
#ifndef OPENSSL_NO_SM9
extern const EVP_PKEY_ASN1_METHOD sm9_master_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD sm9_asn1_meth;
#endif
/*
* These are used internally in the ASN1_OBJECT to keep track of whether the

View File

@@ -370,8 +370,15 @@ struct evp_pkey_st {
# ifndef OPENSSL_NO_EC
struct ec_key_st *ec; /* ECC */
# endif
# ifndef OPENSSL_NO_PAILLIER
# ifndef OPENSSL_NO_PAILLIER /* PAILLIER */
struct paillier_st *paillier;
# endif
# ifndef OPENSSL_NO_SM9 /* SM9 */
struct SM9_MASTER_KEY_st *sm9_master;
struct SM9_KEY_st *sm9;
# endif
# ifndef OPENSSL_NO_CPK /* CPK */
struct CPK_MASTER_SECERT *cpk;
# endif
} pkey;
int save_parameters;

File diff suppressed because it is too large Load Diff

View File

@@ -1080,143 +1080,129 @@ cmac_aes128_ecies 1079
cmac_aes192_ecies 1080
cmac_aes256_ecies 1081
cbc_mac 1082
ISO_CN 1083
oscca 1084
sm_scheme 1085
sm6_ecb 1086
sm6_cbc 1087
sm6_ofb128 1088
sm6_cfb128 1089
sm1_ecb 1090
sm1_cbc 1091
sm1_ofb128 1092
sm1_cfb128 1093
sm1_cfb1 1094
sm1_cfb8 1095
ssf33_ecb 1096
ssf33_cbc 1097
ssf33_ofb128 1098
ssf33_cfb128 1099
ssf33_cfb1 1100
ssf33_cfb8 1101
sms4_ecb 1102
sms4_cbc 1103
sms4_ofb128 1104
sms4_cfb128 1105
sms4_cfb1 1106
sms4_cfb8 1107
sms4_ctr 1108
sms4_gcm 1109
sms4_ccm 1110
sms4_xts 1111
sms4_wrap 1112
sms4_wrap_pad 1113
sms4_ocb 1114
sm5 1115
sm2p256v1 1116
sm2sign 1117
sm2keyagreement 1118
sm2encrypt 1119
sm2encrypt_recommendedParameters 1120
sm2encrypt_specifiedParameters 1121
id_sm9PublicKey 1122
sm9sign 1123
sm9keyagreement 1124
sm9encrypt 1125
sm3 1126
hmac_sm3 1127
sm2sign_with_sm3 1128
sm2sign_with_sha1 1129
sm2sign_with_sha256 1130
sm2sign_with_sha512 1131
sm2sign_with_sha224 1132
sm2sign_with_sha384 1133
sm2sign_with_rmd160 1134
wapip192v1 1135
zuc 1136
bfibe 1137
bb1 1138
type1curve 1139
type2curve 1140
type3curve 1141
type4curve 1142
tate_pairing 1143
weil_pairing 1144
ate_pairing 1145
r_ate_pairing 1146
cpk 1147
paillier 1148
sm2exchange 1149
GmSSL 1150
cpk_map 1151
cpk_sha1_map 1152
cpk_sha256_map 1153
cpk_sm3_map 1154
sm2encrypt_with_sm3 1155
sm2encrypt_with_sha1 1156
sm2encrypt_with_sha224 1157
sm2encrypt_with_sha256 1158
sm2encrypt_with_sha384 1159
sm2encrypt_with_sha512 1160
sm2encrypt_with_rmd160 1161
sm2encrypt_with_whirlpool 1162
sm2encrypt_with_blake2b512 1163
sm2encrypt_with_blake2s256 1164
sm2encrypt_with_md5 1165
sm2sign_with_whirlpool 1166
sm2sign_with_blake2b512 1167
sm2sign_with_blake2s256 1168
ecies_with_x9_63_sha1_xor_hmac 1169
ecies_with_x9_63_sha256_xor_hmac 1170
ecies_with_x9_63_sha512_xor_hmac 1171
ecies_with_x9_63_sha1_aes128_cbc_hmac 1172
ecies_with_x9_63_sha256_aes128_cbc_hmac 1173
ecies_with_x9_63_sha512_aes256_cbc_hmac 1174
ecies_with_x9_63_sha256_aes128_ctr_hmac 1175
ecies_with_x9_63_sha512_aes256_ctr_hmac 1176
ecies_with_x9_63_sha256_aes128_cbc_hmac_half 1177
ecies_with_x9_63_sha512_aes256_cbc_hmac_half 1178
ecies_with_x9_63_sha256_aes128_ctr_hmac_half 1179
ecies_with_x9_63_sha512_aes256_ctr_hmac_half 1180
ecies_with_x9_63_sha1_aes128_cbc_cmac 1181
ecies_with_x9_63_sha256_aes128_cbc_cmac 1182
ecies_with_x9_63_sha512_aes256_cbc_cmac 1183
ecies_with_x9_63_sha256_aes128_ctr_cmac 1184
ecies_with_x9_63_sha512_aes256_ctr_cmac 1185
kx_sm2 1186
auth_sm2 1187
kx_sm9 1188
auth_sm9 1189
kx_sm2dhe 1190
kx_sm2_psk 1191
kx_sm9dhe 1192
zuc_128eea3 1193
zuc_128eia3 1194
pbe_WithSM3AndSMS4_CBC 1195
bwips 1196
wapi_crypto 1197
wapi_ec 1198
wapi_ecdsa192_sha256 1199
sm9bn256v1 1200
sm9hash1 1201
sm9hash1_with_sm3 1202
sm9hash1_with_sha256 1203
sm9sign_with_sm3 1204
sm9sign_with_sha256 1205
sm9encrypt_with_sm3_xor 1206
sm9encrypt_with_sm3_sms4_cbc 1207
sm9encrypt_with_sm3_sms4_ctr 1208
sm9kdf 1209
sm9kdf_with_sm3 1210
sm9kdf_with_sha256 1211
cpk_map32x32_sha1 1212
cpk_map32x256_sha256 1213
cpk_map32x256_sm3 1214
cpk_map_with_sha1 1215
cpk_map_with_sha256 1216
cpk_map_with_sm3 1217
cpk_map_sha1 1218
cpk_map_sha256 1219
cpk_map_sm3 1220
cpk_map_sha384 1221
cpk_map_sha512 1222
pbe_WithSM3AndSMS4_CBC 1083
ecies_with_x9_63_sha1_xor_hmac 1084
ecies_with_x9_63_sha256_xor_hmac 1085
ecies_with_x9_63_sha512_xor_hmac 1086
ecies_with_x9_63_sha1_aes128_cbc_hmac 1087
ecies_with_x9_63_sha256_aes128_cbc_hmac 1088
ecies_with_x9_63_sha512_aes256_cbc_hmac 1089
ecies_with_x9_63_sha256_aes128_ctr_hmac 1090
ecies_with_x9_63_sha512_aes256_ctr_hmac 1091
ecies_with_x9_63_sha256_aes128_cbc_hmac_half 1092
ecies_with_x9_63_sha512_aes256_cbc_hmac_half 1093
ecies_with_x9_63_sha256_aes128_ctr_hmac_half 1094
ecies_with_x9_63_sha512_aes256_ctr_hmac_half 1095
ecies_with_x9_63_sha1_aes128_cbc_cmac 1096
ecies_with_x9_63_sha256_aes128_cbc_cmac 1097
ecies_with_x9_63_sha512_aes256_cbc_cmac 1098
ecies_with_x9_63_sha256_aes128_ctr_cmac 1099
ecies_with_x9_63_sha512_aes256_ctr_cmac 1100
ibcs1 1101
type1curve 1102
bfibe 1103
bb1ibe 1104
ISO_CN 1105
oscca 1106
sm_scheme 1107
ssf33_ecb 1108
ssf33_cbc 1109
ssf33_ofb128 1110
ssf33_cfb128 1111
ssf33_cfb1 1112
ssf33_cfb8 1113
ssf33_cbc_mac 1114
sm1_ecb 1115
sm1_cbc 1116
sm1_ofb128 1117
sm1_cfb128 1118
sm1_cfb1 1119
sm1_cfb8 1120
sm2p256v1 1121
sm2sign 1122
sm2exchange 1123
sm2encrypt 1124
sm2sign_with_sm3 1125
sm2sign_with_sha1 1126
sm2sign_with_sha256 1127
sm2sign_with_sha512 1128
sm2sign_with_sha224 1129
sm2sign_with_sha384 1130
sm2sign_with_rmd160 1131
sm2sign_with_whirlpool 1132
sm2sign_with_blake2b512 1133
sm2sign_with_blake2s256 1134
sm2encrypt_recommendedParameters 1135
sm2encrypt_specifiedParameters 1136
sm2encrypt_with_sm3 1137
sm2encrypt_with_sha1 1138
sm2encrypt_with_sha224 1139
sm2encrypt_with_sha256 1140
sm2encrypt_with_sha384 1141
sm2encrypt_with_sha512 1142
sm2encrypt_with_rmd160 1143
sm2encrypt_with_whirlpool 1144
sm2encrypt_with_blake2b512 1145
sm2encrypt_with_blake2s256 1146
sm2encrypt_with_md5 1147
sm3 1148
hmac_sm3 1149
sms4_ecb 1150
sms4_cbc 1151
sms4_ofb128 1152
sms4_cfb128 1153
sms4_cfb1 1154
sms4_cfb8 1155
sms4_ctr 1156
sms4_gcm 1157
sms4_ccm 1158
sms4_xts 1159
sms4_wrap 1160
sms4_wrap_pad 1161
sms4_ocb 1162
sm5 1163
sm6_ecb 1164
sm6_cbc 1165
sm6_ofb128 1166
sm6_cfb128 1167
id_sm9PublicKey 1168
sm9sign 1169
sm9keyagreement 1170
sm9encrypt 1171
sm9hash1 1172
sm9kdf 1173
id_sm9MasterSecret 1174
sm9bn256v1 1175
sm9sign_with_sm3 1176
sm9sign_with_sha256 1177
sm9encrypt_with_sm3_xor 1178
sm9encrypt_with_sm3_sms4_cbc 1179
sm9encrypt_with_sm3_sms4_ctr 1180
sm9hash1_with_sm3 1181
sm9hash1_with_sha256 1182
sm9kdf_with_sm3 1183
sm9kdf_with_sha256 1184
zuc 1185
zuc_128eea3 1186
zuc_128eia3 1187
bwips 1188
wapi_crypto 1189
wapi_ec 1190
wapi_ecdsa192_sha256 1191
wapip192v1 1192
kx_sm2 1193
kx_sm2dhe 1194
kx_sm2_psk 1195
kx_sm9 1196
kx_sm9dhe 1197
auth_sm2 1198
auth_sm9 1199
GmSSL 1200
cpk 1201
cpk_map 1202
cpk_map_sha1 1203
cpk_map_sha256 1204
cpk_map_sm3 1205
cpk_map_sha384 1206
cpk_map_sha512 1207
paillier 1208

View File

@@ -1480,11 +1480,6 @@ id-pkinit 5 : pkInitKDC : Signing KDC Response
: KxPSK : kx-psk
: KxSRP : kx-srp
: KxGOST : kx-gost
: KxSM2 : kx-sm2
: KxSM2DHE : kx-sm2dhe
: KxSM2-PSK : kx-sm2-psk
: KxSM9 : kx-sm9
: KxSM9DHE : kx-sm9dhe
# NIDs for cipher authentication
: AuthRSA : auth-rsa
@@ -1495,10 +1490,8 @@ id-pkinit 5 : pkInitKDC : Signing KDC Response
: AuthGOST12 : auth-gost12
: AuthSRP : auth-srp
: AuthNULL : auth-null
: AuthSM2 : auth-sm2
: AuthSM9 : auth-sm9
# GmSSL SECG ECIES OID
# SECG ECIES OID
secg-scheme 7 : ecies-recommendedParameters
secg-scheme 8 : ecies-specifiedParameters
secg-scheme 17 0 : x9-63-kdf
@@ -1518,8 +1511,6 @@ secg-scheme 23 : hmac-half-ecies
secg-scheme 24 0 : cmac-aes128-ecies
secg-scheme 24 1 : cmac-aes192-ecies
secg-scheme 24 2 : cmac-aes256-ecies
ecies-specifiedParameters 1 : ecies-with-x9-63-sha1-xor-hmac
ecies-specifiedParameters 2 : ecies-with-x9-63-sha256-xor-hmac
ecies-specifiedParameters 3 : ecies-with-x9-63-sha512-xor-hmac
@@ -1538,18 +1529,27 @@ ecies-specifiedParameters 15 : ecies-with-x9-63-sha512-aes256-cbc-cmac
ecies-specifiedParameters 16 : ecies-with-x9-63-sha256-aes128-ctr-cmac
ecies-specifiedParameters 17 : ecies-with-x9-63-sha512-aes256-ctr-cmac
# RFC 5091 IBCS #1
!Alias ibcs ISO-US 1 114334 1
ibcs 1 : ibcs1
ibcs1 1 1 : type1curve
ibcs1 2 1 : bfibe
ibcs1 2 2 : bb1ibe
# GmSSL SM OID
member-body 156 : ISO-CN : ISO CN Member Body
# GM/T
member-body 156 : ISO-CN : ISO CN Member Body
ISO-CN 10197 : oscca
oscca 1 : sm-scheme
sm-scheme 101 1 : SM6-ECB : sm6-ecb
sm-scheme 101 2 : SM6-CBC : sm6-cbc
!Cname sm6-ofb128
sm-scheme 101 3 : SM6-OFB : sm6-ofb
!Cname sm6-cfb128
sm-scheme 101 4 : SM6-CFB : sm6-cfb
sm-scheme 103 1 : SSF33-ECB : ssf33-ecb
sm-scheme 103 2 : SSF33-CBC : ssf33-cbc
!Cname ssf33-ofb128
sm-scheme 103 3 : SSF33-OFB : ssf33-ofb
!Cname ssf33-cfb128
sm-scheme 103 4 : SSF33-CFB : ssf33-cfb
sm-scheme 103 5 : SSF33-CFB1 : ssf33-cfb1
sm-scheme 103 6 : SSF33-CFB8 : ssf33-cfb8
sm-scheme 103 7 : SSF33-CBC-MAC : ssf33-cbc-mac
sm-scheme 102 1 : SM1-ECB : sm1-ecb
sm-scheme 102 2 : SM1-CBC : sm1-cbc
@@ -1560,15 +1560,43 @@ sm-scheme 102 4 : SM1-CFB : sm1-cfb
sm-scheme 102 5 : SM1-CFB1 : sm1-cfb1
sm-scheme 102 6 : SM1-CFB8 : sm1-cfb8
sm-scheme 103 1 : SSF33-ECB : ssf33-ecb
sm-scheme 103 2 : SSF33-CBC : ssf33-cbc
!Cname ssf33-ofb128
sm-scheme 103 3 : SSF33-OFB : ssf33-ofb
!Cname ssf33-cfb128
sm-scheme 103 4 : SSF33-CFB : ssf33-cfb
sm-scheme 103 5 : SSF33-CFB1 : ssf33-cfb1
sm-scheme 103 6 : SSF33-CFB8 : ssf33-cfb8
# SM2 OIDs
sm-scheme 301 : sm2p256v1
sm-scheme 301 1 : sm2sign
sm-scheme 301 2 : sm2exchange
sm-scheme 301 3 : sm2encrypt
sm-scheme 501 : SM2Sign-with-SM3 : sm2sign-with-sm3
sm-scheme 502 : SM2Sign-with-SHA1 : sm2sign-with-sha1
sm-scheme 503 : SM2Sign-with-SHA256 : sm2sign-with-sha256
sm-scheme 504 : SM2Sign-with-SHA511 : sm2sign-with-sha512
sm-scheme 505 : SM2Sign-with-SHA224 : sm2sign-with-sha224
sm-scheme 506 : SM2Sign-with-SHA384 : sm2sign-with-sha384
sm-scheme 507 : SM2Sign-with-RMD160 : sm2sign-with-rmd160
sm-scheme 520 : SM2Sign-with-Whirlpool : sm2sign-with-whirlpool
sm-scheme 521 : SM2Sign-with-Blake2b512 : sm2sign-with-blake2b512
sm-scheme 522 : SM2Sign-with-Blake2s256 : sm2sign-with-blake2s256
sm2encrypt 1 : sm2encrypt-recommendedParameters
sm2encrypt 2 : sm2encrypt-specifiedParameters
sm2encrypt 2 1 : sm2encrypt-with-sm3
sm2encrypt 2 2 : sm2encrypt-with-sha1
sm2encrypt 2 3 : sm2encrypt-with-sha224
sm2encrypt 2 4 : sm2encrypt-with-sha256
sm2encrypt 2 5 : sm2encrypt-with-sha384
sm2encrypt 2 6 : sm2encrypt-with-sha512
sm2encrypt 2 7 : sm2encrypt-with-rmd160
sm2encrypt 2 8 : sm2encrypt-with-whirlpool
sm2encrypt 2 9 : sm2encrypt-with-blake2b512
sm2encrypt 2 10 : sm2encrypt-with-blake2s256
sm2encrypt 2 11 : sm2encrypt-with-md5
# SM3
sm-scheme 401 : SM3 : sm3
sm-scheme 401 2 : HMAC-SM3 : hmac-sm3
# SM4
sm-scheme 104 1 : SMS4-ECB : sms4-ecb
sm-scheme 104 2 : SMS4-CBC : sms4-cbc
!Cname sms4-ofb128
@@ -1585,89 +1613,65 @@ sm-scheme 104 11 : SMS4-WRAP : sms4-wrap
sm-scheme 104 12 : SMS4-WRAP-PAD : sms4-wrap-pad
sm-scheme 104 100 : SMS4-OCB : sms4-ocb
# SM5/6/7/8
sm-scheme 201 : SM5 : sm5
sm-scheme 101 1 : SM6-ECB : sm6-ecb
sm-scheme 101 2 : SM6-CBC : sm6-cbc
!Cname sm6-ofb128
sm-scheme 101 3 : SM6-OFB : sm6-ofb
!Cname sm6-cfb128
sm-scheme 101 4 : SM6-CFB : sm6-cfb
!Alias sm7 sm-scheme 105
!Alias sm8 sm-scheme 106
sm-scheme 201 : SM5 : sm5
sm-scheme 301 : sm2p256v1
sm-scheme 301 1 : sm2sign
sm-scheme 301 2 : sm2exchange
sm-scheme 301 3 : sm2encrypt
#sm-scheme 301 101 : wapip192v1
sm2encrypt 1 : sm2encrypt-recommendedParameters
sm2encrypt 2 : sm2encrypt-specifiedParameters
sm2encrypt 2 1 : sm2encrypt-with-sm3
sm2encrypt 2 2 : sm2encrypt-with-sha1
sm2encrypt 2 3 : sm2encrypt-with-sha224
sm2encrypt 2 4 : sm2encrypt-with-sha256
sm2encrypt 2 5 : sm2encrypt-with-sha384
sm2encrypt 2 6 : sm2encrypt-with-sha512
sm2encrypt 2 7 : sm2encrypt-with-rmd160
sm2encrypt 2 8 : sm2encrypt-with-whirlpool
sm2encrypt 2 9 : sm2encrypt-with-blake2b512
sm2encrypt 2 10 : sm2encrypt-with-blake2s256
sm2encrypt 2 11 : sm2encrypt-with-md5
# SM9
sm-scheme 302 : id-sm9PublicKey
sm-scheme 302 1 : sm9sign
sm9sign 1 : sm9sign-with-sm3
sm9sign 2 : sm9sign-with-sha256
sm-scheme 302 2 : sm9keyagreement
sm-scheme 302 3 : sm9encrypt
sm-scheme 302 4 : sm9hash1
sm-scheme 302 5 : sm9kdf
sm-scheme 302 6 : id-sm9MasterSecret
sm-scheme 302 6 1 : sm9bn256v1
sm9sign 1 : sm9sign-with-sm3
sm9sign 2 : sm9sign-with-sha256
sm9encrypt 1 : sm9encrypt-with-sm3-xor
sm9encrypt 2 : sm9encrypt-with-sm3-sms4-cbc
sm9encrypt 3 : sm9encrypt-with-sm3-sms4-ctr
sm-scheme 302 4 : sm9hash1
sm-scheme 302 5 : sm9kdf
sm9hash1 1 : sm9hash1-with-sm3
sm9hash1 2 : sm9hash1-with-sha256
sm9kdf 1 : sm9kdf-with-sm3
sm9kdf 2 : sm9kdf-with-sha256
sm-scheme 401 : SM3 : sm3
sm-scheme 401 2 : HMAC-SM3 : hmac-sm3
# ZUC
sm-scheme 800 : ZUC : zuc
zuc 1 : zuc-128eea3
zuc 2 : zuc-128eia3
sm-scheme 501 : SM2Sign-with-SM3 : sm2sign-with-sm3
sm-scheme 502 : SM2Sign-with-SHA1 : sm2sign-with-sha1
sm-scheme 503 : SM2Sign-with-SHA256 : sm2sign-with-sha256
sm-scheme 504 : SM2Sign-with-SHA511 : sm2sign-with-sha512
sm-scheme 505 : SM2Sign-with-SHA224 : sm2sign-with-sha224
sm-scheme 506 : SM2Sign-with-SHA384 : sm2sign-with-sha384
sm-scheme 507 : SM2Sign-with-RMD160 : sm2sign-with-rmd160
sm-scheme 520 : SM2Sign-with-Whirlpool : sm2sign-with-whirlpool
sm-scheme 521 : SM2Sign-with-Blake2b512 : sm2sign-with-blake2b512
sm-scheme 522 : SM2Sign-with-Blake2s256 : sm2sign-with-blake2s256
# WAPI (GB 15629.11-2003-XG1-2006)
ISO-CN 11235 : bwips
bwips 1 : wapi-crypto
wapi-crypto 1 : wapi-ec
wapi-ec 1 : wapi-ecdsa192-sha256
wapi-ec 2 1 : wapip192v1
# GmSSL ZUC OID
sm-scheme 800 : ZUC : zuc
zuc 1 : zuc-128eea3
zuc 2 : zuc-128eia3
# NIDs for cipher key exchange
: KxSM2 : kx-sm2
: KxSM2DHE : kx-sm2dhe
: KxSM2-PSK : kx-sm2-psk
: KxSM9 : kx-sm9
: KxSM9DHE : kx-sm9dhe
# IBCS1
!Alias ibcs1 ISO-US 1 114334 1
ibcs1 2 1 : bfibe
ibcs1 2 2 : bb1
# 1 tate, 2 weil, 3 ate, 4 r-ate
ibcs1 1 1 : type1curve
ibcs1 1 2 : type2curve
ibcs1 1 3 : type3curve
ibcs1 1 4 : type4curve
# NIDs for cipher authentication
: AuthSM2 : auth-sm2
: AuthSM9 : auth-sm9
ibcs1 3 1 : tate-pairing
ibcs1 3 2 : weil-pairing
ibcs1 3 3 : ate-pairing
ibcs1 3 4 : r-ate-pairing
# GmSSL
# gmssl.org
Enterprises 49549 : gmssl : GmSSL
# CPK
GmSSL 1 : CPK : cpk
# map algorithms
cpk 1 : cpk-map
cpk-map 1 : cpk-map-sha1
cpk-map 2 : cpk-map-sha256
@@ -1675,14 +1679,6 @@ cpk-map 3 : cpk-map-sm3
cpk-map 4 : cpk-map-sha384
cpk-map 5 : cpk-map-sha512
# paillier
GmSSL 21 : paillier
# WAPI (GB 15629.11-2003-XG1-2006)
ISO-CN 11235 : bwips
bwips 1 : wapi-crypto
wapi-crypto 1 : wapi-ec
wapi-ec 1 : wapi-ecdsa192-sha256
wapi-ec 2 1 : wapip192v1
# SM9 curve
GmSSL 9 1 : sm9bn256v1

View File

@@ -1,2 +1,2 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=pai_lib.c pai_err.c pai_asn1.c
SOURCE[../../libcrypto]=pai_lib.c pai_err.c pai_asn1.c pai_ameth.c

View File

@@ -48,11 +48,16 @@
*/
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/asn1t.h>
#include <openssl/objects.h>
#include <openssl/paillier.h>
#include "internal/cryptlib.h"
#include "internal/asn1_int.h"
#include "internal/evp_int.h"
#include "pai_lcl.h"
static int paillier_pub_encode(X509_PUBKEY *pubkey, const EVP_PKEY *pkey)
@@ -60,7 +65,7 @@ static int paillier_pub_encode(X509_PUBKEY *pubkey, const EVP_PKEY *pkey)
unsigned char *penc = NULL;
int penclen;
if ((penclen = i2d_PAILLIER_PUBLIC_KEY(pkey->pkey.paillier, &penc)) < = 0) {
if ((penclen = i2d_PaillierPublicKey(pkey->pkey.paillier, &penc)) <= 0) {
return 0;
}
if (X509_PUBKEY_set0_param(pubkey, OBJ_nid2obj(EVP_PKEY_PAILLIER),
@@ -81,8 +86,7 @@ static int paillier_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
if (!X509_PUBKEY_get0_param(NULL, &cp, &len, NULL, pubkey)) {
return 0;
}
if (!(paillier = d2i_PAILLIER_PUBLIC_KEY(NULL, &cp, len))) {
if (!(paillier = d2i_PaillierPublicKey(NULL, &cp, len))) {
PAILLIERerr(PAILLIER_F_PAILLIER_PUB_DECODE, ERR_R_PAILLIER_LIB);
return 0;
}
@@ -93,7 +97,131 @@ static int paillier_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
static int paillier_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
{
return -1;
if (BN_cmp(a->pkey.paillier->n, b->pkey.paillier->n) != 0) {
return 0;
}
return 1;
}
static int do_paillier_print(BIO *bp, const PAILLIER *x, int off, int priv)
{
char *str;
int ret = 0;
if (!BIO_indent(bp, off, 128))
goto end;
if (priv && x->lambda) {
if (BIO_printf(bp, "Private-Key: (%d bit)\n", x->bits) <= 0)
goto end;
str = "modulus";
} else {
if (BIO_printf(bp, "Public-Key: (%d bit)\n", x->bits) <= 0)
goto end;
str = "Modulus";
}
if (!ASN1_bn_print(bp, str, x->n, NULL, off))
goto end;
if (priv) {
if (!ASN1_bn_print(bp, "lambda:", x->lambda, NULL, off))
goto end;
if (!ASN1_bn_print(bp, "x:", x->x, NULL, off))
goto end;
}
ret = 1;
end:
return ret;
}
static int paillier_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *ctx)
{
return do_paillier_print(bp, pkey->pkey.paillier, indent, 0);
}
static int paillier_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *ctx)
{
return do_paillier_print(bp, pkey->pkey.paillier, indent, 1);
}
static int paillier_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
{
const unsigned char *p;
int pklen;
PAILLIER *paillier;
if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
return 0;
if (!(paillier = d2i_PaillierPrivateKey(NULL, &p, pklen))) {
PAILLIERerr(PAILLIER_F_PAILLIER_PRIV_DECODE, ERR_R_PAILLIER_LIB);
return 0;
}
EVP_PKEY_assign_PAILLIER(pkey, paillier);
return 1;
}
static int paillier_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
{
unsigned char *rk = NULL;
int rklen;
if ((rklen = i2d_PaillierPrivateKey(pkey->pkey.paillier, &rk)) <= 0) {
PAILLIERerr(PAILLIER_F_PAILLIER_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_paillier), 0, V_ASN1_NULL, NULL,
rk, rklen)) {
PAILLIERerr(PAILLIER_F_PAILLIER_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
return 1;
}
static int int_paillier_size(const EVP_PKEY *pkey)
{
return PAILLIER_size(pkey->pkey.paillier);
}
static int paillier_bits(const EVP_PKEY *pkey)
{
return pkey->pkey.paillier->bits;
}
static int paillier_security_bits(const EVP_PKEY *pkey)
{
return PAILLIER_security_bits(pkey->pkey.paillier);
}
static void int_paillier_free(EVP_PKEY *pkey)
{
PAILLIER_free(pkey->pkey.paillier);
}
static int paillier_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
{
return -2;
}
static int old_paillier_priv_decode(EVP_PKEY *pkey,
const unsigned char **pder, int derlen)
{
PAILLIER *pai;
if ((pai = d2i_PaillierPrivateKey(NULL, pder, derlen)) == NULL) {
PAILLIERerr(PAILLIER_F_OLD_PAILLIER_PRIV_DECODE,
PAILLIER_R_DECODE_ERROR);
return 0;
}
EVP_PKEY_assign_PAILLIER(pkey, pai);
return 1;
}
static int old_paillier_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
{
return i2d_PaillierPrivateKey(pkey->pkey.paillier, pder);
}
const EVP_PKEY_ASN1_METHOD paillier_asn1_meth = {

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2018 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
@@ -19,6 +19,8 @@
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_PAILLIER,0,reason)
static ERR_STRING_DATA PAILLIER_str_functs[] = {
{ERR_FUNC(PAILLIER_F_OLD_PAILLIER_PRIV_DECODE),
"old_paillier_priv_decode"},
{ERR_FUNC(PAILLIER_F_PAILLIER_CHECK_KEY), "PAILLIER_check_key"},
{ERR_FUNC(PAILLIER_F_PAILLIER_CIPHERTEXT_ADD), "PAILLIER_ciphertext_add"},
{ERR_FUNC(PAILLIER_F_PAILLIER_CIPHERTEXT_SCALAR_MUL),
@@ -27,6 +29,8 @@ static ERR_STRING_DATA PAILLIER_str_functs[] = {
{ERR_FUNC(PAILLIER_F_PAILLIER_ENCRYPT), "PAILLIER_encrypt"},
{ERR_FUNC(PAILLIER_F_PAILLIER_GENERATE_KEY), "PAILLIER_generate_key"},
{ERR_FUNC(PAILLIER_F_PAILLIER_NEW), "PAILLIER_new"},
{ERR_FUNC(PAILLIER_F_PAILLIER_PRIV_DECODE), "paillier_priv_decode"},
{ERR_FUNC(PAILLIER_F_PAILLIER_PRIV_ENCODE), "paillier_priv_encode"},
{ERR_FUNC(PAILLIER_F_PAILLIER_PUB_DECODE), "paillier_pub_decode"},
{ERR_FUNC(PAILLIER_F_PKEY_PAILLIER_DECRYPT), "pkey_paillier_decrypt"},
{ERR_FUNC(PAILLIER_F_PKEY_PAILLIER_ENCRYPT), "pkey_paillier_encrypt"},
@@ -35,6 +39,7 @@ static ERR_STRING_DATA PAILLIER_str_functs[] = {
static ERR_STRING_DATA PAILLIER_str_reasons[] = {
{ERR_REASON(PAILLIER_R_BUFFER_TOO_SMALL), "buffer too small"},
{ERR_REASON(PAILLIER_R_DECODE_ERROR), "decode error"},
{ERR_REASON(PAILLIER_R_GENERATE_PRIME_FAILED), "generate prime failed"},
{ERR_REASON(PAILLIER_R_INVALID_PLAINTEXT), "invalid plaintext"},
{ERR_REASON(PAILLIER_R_MALLOC_FAILED), "malloc failed"},

View File

@@ -45,7 +45,6 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
#include <stdio.h>
@@ -70,7 +69,6 @@ PAILLIER *PAILLIER_new(void)
void PAILLIER_free(PAILLIER *key)
{
if (key) {
BN_free(key->n);
BN_free(key->lambda);
@@ -81,6 +79,16 @@ void PAILLIER_free(PAILLIER *key)
OPENSSL_clear_free(key, sizeof(*key));
}
int PAILLIER_size(const PAILLIER *key)
{
return BN_num_bits(key->n)/8;
}
int PAILLIER_security_bits(const PAILLIER *key)
{
return BN_security_bits(BN_num_bits(key->n)/2, -1);
}
int PAILLIER_generate_key(PAILLIER *key, int bits)
{
int ret = 0;

View File

@@ -70,9 +70,6 @@ static void pkey_paillier_cleanup(EVP_PKEY_CTX *ctx)
{
}
//FIXME keygen
static int pkey_paillier_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen)
{

View File

@@ -56,16 +56,13 @@
# include <openssl/pkcs7.h>
#endif
#include <openssl/pem.h>
#include <openssl/pem3.h>
#ifndef OPENSSL_NO_PAILLIER
# include <openssl/paillier.h>
#endif
#ifndef OPENSSL_NO_SM9
# include <openssl/sm9.h>
#endif
/*
extern PAILLIER *EVP_PKEY_get1_PAILLIER(EVP_PKEY *key);
extern int i2d_PAILLIER_PUBKEY(PAILLIER *a, unsigned char **p);
extern PAILLIER *d2i_PAILLIER_PUBKEY(PAILLIER **a, const unsigned char **p, long len);
*/
#ifndef OPENSSL_NO_PAILLIER
static PAILLIER *pkey_get_paillier(EVP_PKEY *key, PAILLIER **paillier)
@@ -109,4 +106,87 @@ IMPLEMENT_PEM_rw_const(PaillierPublicKey, PAILLIER, PEM_STRING_PAILLIER_PUBLIC,
PaillierPublicKey)
IMPLEMENT_PEM_rw(PAILLIER_PUBKEY, PAILLIER, PEM_STRING_PUBLIC, PAILLIER_PUBKEY)
#endif
#endif /* OPENSSL_NO_PAILLIER */
#ifndef OPENSSL_NO_SM9
static SM9_MASTER_KEY *pkey_get_sm9_master(EVP_PKEY *key, SM9_MASTER_KEY **sm9_master)
{
SM9_MASTER_KEY *rtmp;
if (!key)
return NULL;
rtmp = EVP_PKEY_get1_SM9_MASTER(key);
EVP_PKEY_free(key);
if (!rtmp)
return NULL;
if (sm9_master) {
SM9_MASTER_KEY_free(*sm9_master);
*sm9_master = rtmp;
}
return rtmp;
}
SM9_MASTER_KEY *PEM_read_bio_SM9MasterSecret(BIO *bp, SM9_MASTER_KEY **sm9_master,
pem_password_cb *cb, void *u)
{
EVP_PKEY *pktmp;
pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
return pkey_get_sm9_master(pktmp, sm9_master);
}
static SM9_KEY *pkey_get_sm9(EVP_PKEY *key, SM9_KEY **sm9)
{
SM9_KEY *rtmp;
if (!key)
return NULL;
rtmp = EVP_PKEY_get1_SM9(key);
EVP_PKEY_free(key);
if (!rtmp)
return NULL;
if (sm9) {
SM9_KEY_free(*sm9);
*sm9 = rtmp;
}
return rtmp;
}
SM9_KEY *PEM_read_bio_SM9PrivateKey(BIO *bp, SM9_KEY **sm9,
pem_password_cb *cb, void *u)
{
EVP_PKEY *pktmp;
pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
return pkey_get_sm9(pktmp, sm9);
}
# ifndef OPENSSL_NO_STDIO
SM9_MASTER_KEY *PEM_read_SM9MasterSecret(FILE *fp, SM9_MASTER_KEY **sm9_master,
pem_password_cb *cb, void *u)
{
EVP_PKEY *pktmp;
pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
return pkey_get_sm9_master(pktmp, sm9_master);
}
SM9_KEY *PEM_read_SM9PrivateKey(FILE *fp, SM9_KEY **sm9,
pem_password_cb *cb, void *u)
{
EVP_PKEY *pktmp;
pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
return pkey_get_sm9(pktmp, sm9);
}
# endif
IMPLEMENT_PEM_write_cb_const(SM9MasterSecret, SM9_MASTER_KEY,
PEM_STRING_SM9_MASTER, SM9MasterSecret)
IMPLEMENT_PEM_rw_const(SM9PublicParameters, SM9_MASTER_KEY,
PEM_STRING_SM9_MASTER_PUBLIC, SM9PublicParameters)
IMPLEMENT_PEM_rw(SM9_MASTER_PUBKEY, SM9_MASTER_KEY,
PEM_STRING_PUBLIC, SM9_MASTER_PUBKEY)
IMPLEMENT_PEM_write_cb_const(SM9PrivateKey, SM9_KEY,
PEM_STRING_SM9, SM9PrivateKey)
IMPLEMENT_PEM_rw_const(SM9PublicKey, SM9_KEY,
PEM_STRING_SM9_PUBLIC, SM9PublicKey)
IMPLEMENT_PEM_rw(SM9_PUBKEY, SM9_KEY,
PEM_STRING_PUBLIC, SM9_PUBKEY)
#endif /* OPENSSL_NO_SM9 */

View File

@@ -1,3 +1,4 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=sm9_lib.c sm9_err.c sm9_asn1.c sm9_params.c \
sm9_setup.c sm9_keygen.c sm9_sign.c sm9_enc.c sm9_exch.c sm9_rate.c
sm9_setup.c sm9_keygen.c sm9_sign.c sm9_enc.c sm9_exch.c sm9_rate.c \
sm9_ameth.c

411
crypto/sm9/sm9_ameth.c Normal file
View File

@@ -0,0 +1,411 @@
/* ====================================================================
* Copyright (c) 2015 - 2018 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <stdio.h>
#include <openssl/sm9.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/asn1t.h>
#include <openssl/objects.h>
#include "internal/cryptlib.h"
#include "internal/asn1_int.h"
#include "internal/evp_int.h"
#include "sm9_lcl.h"
static int sm9_params_encode(X509_PUBKEY *pubkey, const EVP_PKEY *pkey)
{
unsigned char *penc = NULL;
int penclen;
if ((penclen = i2d_SM9PublicParameters(pkey->pkey.sm9_master, &penc)) <= 0) {
return 0;
}
printf("penclen = %d\n", penclen);
printf("penc == NULL: %d\n", penc == NULL);
printf("%lu\n", OBJ_nid2obj(EVP_PKEY_SM9_MASTER));
OPENSSL_assert(pubkey);
if (X509_PUBKEY_set0_param(pubkey, OBJ_nid2obj(EVP_PKEY_SM9_MASTER),
V_ASN1_NULL, NULL, penc, penclen)) {
return 1;
}
OPENSSL_free(penc);
return 0;
}
static int sm9_params_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
{
const unsigned char *cp;
int len;
SM9PublicParameters *sm9_params = NULL;
if (!X509_PUBKEY_get0_param(NULL, &cp, &len, NULL, pubkey)) {
return 0;
}
if (!(sm9_params = d2i_SM9PublicParameters(NULL, &cp, len))) {
SM9err(SM9_F_SM9_PARAMS_DECODE, ERR_R_SM9_LIB);
return 0;
}
EVP_PKEY_assign_SM9PublicParameters(pkey, sm9_params);
return 1;
}
static int sm9_params_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
{
/*
if (OBJ_cmp(a->pkey.sm9_master->pairing, b->pkey.sm9_master->pairing) != 0
|| OBJ_cmp(a->pkey.sm9_master->scheme, b->pkey.sm9_master->scheme) != 0
|| OBJ_cmp(a->pkey.sm9_master->hash1, b->pkey.sm9_master->hash1) != 0
|| ASN1_OCTET_STRING_cmp(a->pkey.sm9_master->pointPpub,
b->pkey.sm9_master->pointPpub) != 0) {
return 0;
}
*/
return 1;
}
static int do_sm9_master_print(BIO *bp, const SM9MasterSecret *x, int off, int priv)
{
return -2;
}
static int sm9_params_print(BIO *bp, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *ctx)
{
return do_sm9_master_print(bp, pkey->pkey.sm9_master, indent, 0);
}
static int sm9_master_print(BIO *bp, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *ctx)
{
return do_sm9_master_print(bp, pkey->pkey.sm9_master, indent, 1);
}
static int sm9_master_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
{
const unsigned char *p;
int pklen;
SM9MasterSecret *sm9_master;
if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
return 0;
if (!(sm9_master = d2i_SM9MasterSecret(NULL, &p, pklen))) {
SM9err(SM9_F_SM9_MASTER_DECODE, ERR_R_SM9_LIB);
return 0;
}
EVP_PKEY_assign_SM9MasterSecret(pkey, sm9_master);
return 1;
}
static int sm9_master_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
{
unsigned char *rk = NULL;
int rklen;
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, __FUNCTION__);
if ((rklen = i2d_SM9MasterSecret(pkey->pkey.sm9_master, &rk)) <= 0) {
SM9err(SM9_F_SM9_MASTER_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(EVP_PKEY_SM9_MASTER), 0,
V_ASN1_NULL, NULL, rk, rklen)) {
SM9err(SM9_F_SM9_MASTER_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
return 1;
}
static int int_sm9_size(const EVP_PKEY *pkey)
{
return 32 * 12;
}
static int sm9_bits(const EVP_PKEY *pkey)
{
return 256 * 12;
}
static int sm9_security_bits(const EVP_PKEY *pkey)
{
return 256/2;
}
static void int_sm9_master_free(EVP_PKEY *pkey)
{
SM9MasterSecret_free(pkey->pkey.sm9_master);
}
static int sm9_master_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
{
return -2;
}
static int old_sm9_master_decode(EVP_PKEY *pkey,
const unsigned char **pder, int derlen)
{
SM9MasterSecret *sm9_master;
if ((sm9_master = d2i_SM9MasterSecret(NULL, pder, derlen)) == NULL) {
SM9err(SM9_F_OLD_SM9_MASTER_DECODE, SM9_R_DECODE_ERROR);
return 0;
}
EVP_PKEY_assign_SM9MasterSecret(pkey, sm9_master);
return 1;
}
static int old_sm9_master_encode(const EVP_PKEY *pkey, unsigned char **pder)
{
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, __FUNCTION__);
return i2d_SM9MasterSecret(pkey->pkey.sm9_master, pder);
}
const EVP_PKEY_ASN1_METHOD sm9_master_asn1_meth = {
EVP_PKEY_SM9_MASTER, /* pkey_id */
EVP_PKEY_SM9_MASTER, /* pkey_base_id */
0, /* pkey_flags */
"SM9 MASTER", /* pem_str */
"GmSSL SM9 algorithm", /* info */
sm9_params_decode, /* pub_decode */
sm9_params_encode, /* pub_encode */
sm9_params_cmp, /* pub_cmp */
sm9_params_print, /* pub_print */
sm9_master_decode, /* priv_decode */
sm9_master_encode, /* priv_encode */
sm9_master_print, /* priv_print */
int_sm9_size, /* pkey_size */
sm9_bits, /* pkey_bits */
sm9_security_bits, /* pkey_security_bits */
NULL, /* param_decode */
NULL, /* param_encode */
NULL, /* param_missing */
NULL, /* param_copy */
NULL, /* param_cmp */
NULL, /* param_print */
NULL, /* sig_print */
int_sm9_master_free, /* pkey_free */
sm9_master_pkey_ctrl, /* pkey_ctrl */
old_sm9_master_decode, /* old_priv_decode */
old_sm9_master_encode, /* old_priv_encode */
NULL, /* item_verify */
NULL, /* item_sign */
};
static int sm9_pub_encode(X509_PUBKEY *pubkey, const EVP_PKEY *pkey)
{
unsigned char *penc = NULL;
int penclen;
if ((penclen = i2d_SM9PublicKey(pkey->pkey.sm9, &penc)) <= 0) {
return 0;
}
if (X509_PUBKEY_set0_param(pubkey, OBJ_nid2obj(EVP_PKEY_SM9),
V_ASN1_NULL, NULL, penc, penclen)) {
return 1;
}
OPENSSL_free(penc);
return 0;
}
static int sm9_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
{
const unsigned char *cp;
int len;
SM9PublicKey *sm9 = NULL;
if (!X509_PUBKEY_get0_param(NULL, &cp, &len, NULL, pubkey)) {
return 0;
}
if (!(sm9 = d2i_SM9PublicKey(NULL, &cp, len))) {
SM9err(SM9_F_SM9_PUB_DECODE, ERR_R_SM9_LIB);
return 0;
}
EVP_PKEY_assign_SM9PublicKey(pkey, sm9);
return 1;
}
static int sm9_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
{
const SM9PublicKey *ax = a->pkey.sm9;
const SM9PublicKey *bx = b->pkey.sm9;
if (OBJ_cmp(ax->pairing, bx->pairing)
|| OBJ_cmp(ax->scheme, bx->scheme)
|| OBJ_cmp(ax->hash1, bx->hash1)
/* FIXME: decode point then compare to support point compression */
|| ASN1_OCTET_STRING_cmp(ax->pointPpub, bx->pointPpub)
|| ASN1_OCTET_STRING_cmp(ax->identity, bx->identity)
|| ASN1_OCTET_STRING_cmp(ax->publicPoint, bx->publicPoint)) {
return 0;
}
return 1;
}
static int do_sm9_print(BIO *bp, const SM9PrivateKey *x, int off, int priv)
{
return -2;
}
static int sm9_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *ctx)
{
return do_sm9_print(bp, pkey->pkey.sm9, indent, 0);
}
static int sm9_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *ctx)
{
return do_sm9_print(bp, pkey->pkey.sm9, indent, 1);
}
static int sm9_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
{
const unsigned char *p;
int pklen;
SM9PrivateKey *sm9;
if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
return 0;
if (!(sm9 = d2i_SM9PrivateKey(NULL, &p, pklen))) {
SM9err(SM9_F_SM9_PRIV_DECODE, ERR_R_SM9_LIB);
return 0;
}
EVP_PKEY_assign_SM9PrivateKey(pkey, sm9);
return 1;
}
static int sm9_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
{
unsigned char *rk = NULL;
int rklen;
/*
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, __FUNCTION__);
fprintf(stderr, "%s %s: %d\n", __FILE__, __LINE__, pkey->pkey.sm9->privatePoint != NULL);
*/
if ((rklen = i2d_SM9PrivateKey(pkey->pkey.sm9, &rk)) <= 0) {
SM9err(SM9_F_SM9_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(EVP_PKEY_SM9), 0,
V_ASN1_NULL, NULL, rk, rklen)) {
SM9err(SM9_F_SM9_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
return 1;
}
static void int_sm9_free(EVP_PKEY *pkey)
{
SM9PrivateKey_free(pkey->pkey.sm9);
}
static int sm9_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
{
return -2;
}
static int old_sm9_priv_decode(EVP_PKEY *pkey,
const unsigned char **pder, int derlen)
{
SM9PrivateKey *sm9;
if ((sm9 = d2i_SM9PrivateKey(NULL, pder, derlen)) == NULL) {
SM9err(SM9_F_OLD_SM9_PRIV_DECODE, SM9_R_DECODE_ERROR);
return 0;
}
EVP_PKEY_assign_SM9PrivateKey(pkey, sm9);
return 1;
}
static int old_sm9_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
{
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, __FUNCTION__);
return i2d_SM9PrivateKey(pkey->pkey.sm9, pder);
}
const EVP_PKEY_ASN1_METHOD sm9_asn1_meth = {
EVP_PKEY_SM9, /* pkey_id */
EVP_PKEY_SM9, /* pkey_base_id */
0, /* pkey_flags */
"SM9", /* pem_str */
"GmSSL SM9 algorithm", /* info */
sm9_pub_decode, /* pub_decode */
sm9_pub_encode, /* pub_encode */
sm9_pub_cmp, /* pub_cmp */
sm9_pub_print, /* pub_print */
sm9_priv_decode, /* priv_decode */
sm9_priv_encode, /* priv_encode */
sm9_priv_print, /* priv_print */
int_sm9_size, /* pkey_size */
sm9_bits, /* pkey_bits */
sm9_security_bits, /* pkey_security_bits */
NULL, /* param_decode */
NULL, /* param_encode */
NULL, /* param_missing */
NULL, /* param_copy */
NULL, /* param_cmp */
NULL, /* param_print */
NULL, /* sig_print */
int_sm9_free, /* pkey_free */
sm9_pkey_ctrl, /* pkey_ctrl */
old_sm9_priv_decode, /* old_priv_decode */
old_sm9_priv_encode, /* old_priv_encode */
NULL, /* item_verify */
NULL, /* item_sign */
};

View File

@@ -55,47 +55,76 @@
#include <openssl/sm9.h>
#include "sm9_lcl.h"
ASN1_SEQUENCE(SM9MasterSecret) = {
ASN1_SIMPLE(SM9MasterSecret, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9MasterSecret, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9MasterSecret, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9MasterSecret, pointPpub, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9MasterSecret, masterSecret, BIGNUM)
} ASN1_SEQUENCE_END(SM9MasterSecret)
IMPLEMENT_ASN1_FUNCTIONS(SM9MasterSecret)
IMPLEMENT_ASN1_DUP_FUNCTION(SM9MasterSecret)
static int sm9_master_key_cb(int operation, ASN1_VALUE **pval,
const ASN1_ITEM *it, void *exarg)
{
if (operation == ASN1_OP_NEW_PRE) {
*pval = (ASN1_VALUE *)SM9_MASTER_KEY_new();
if (*pval)
return 2;
return 0;
} else if (operation == ASN1_OP_FREE_PRE) {
SM9_MASTER_KEY_free((SM9_MASTER_KEY *)*pval);
*pval = NULL;
return 2;
}
return 1;
}
ASN1_SEQUENCE(SM9PublicParameters) = {
ASN1_SIMPLE(SM9PublicParameters, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9PublicParameters, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9PublicParameters, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9PublicParameters, pointPpub, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(SM9PublicParameters)
IMPLEMENT_ASN1_FUNCTIONS(SM9PublicParameters)
IMPLEMENT_ASN1_DUP_FUNCTION(SM9PublicParameters)
ASN1_SEQUENCE_cb(SM9MasterSecret, sm9_master_key_cb) = {
ASN1_SIMPLE(SM9_MASTER_KEY, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9_MASTER_KEY, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9_MASTER_KEY, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9_MASTER_KEY, pointPpub, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9_MASTER_KEY, masterSecret, BIGNUM)
} ASN1_SEQUENCE_END_cb(SM9_MASTER_KEY, SM9MasterSecret)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(SM9_MASTER_KEY,SM9MasterSecret,SM9MasterSecret)
ASN1_SEQUENCE(SM9PrivateKey) = {
ASN1_SIMPLE(SM9PrivateKey, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9PrivateKey, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9PrivateKey, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9PrivateKey, pointPpub, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9PrivateKey, identity, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9PrivateKey, publicPoint, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9PrivateKey, privatePoint, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(SM9PrivateKey)
IMPLEMENT_ASN1_FUNCTIONS(SM9PrivateKey)
IMPLEMENT_ASN1_DUP_FUNCTION(SM9PrivateKey)
ASN1_SEQUENCE_cb(SM9PublicParameters, sm9_master_key_cb) = {
ASN1_SIMPLE(SM9_MASTER_KEY, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9_MASTER_KEY, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9_MASTER_KEY, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9_MASTER_KEY, pointPpub, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END_cb(SM9_MASTER_KEY, SM9PublicParameters)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(SM9_MASTER_KEY,SM9PublicParameters,SM9PublicParameters)
ASN1_SEQUENCE(SM9PublicKey) = {
ASN1_SIMPLE(SM9PublicKey, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9PublicKey, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9PublicKey, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9PublicKey, pointPpub, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9PublicKey, identity, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9PublicKey, publicPoint, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(SM9PublicKey)
IMPLEMENT_ASN1_FUNCTIONS(SM9PublicKey)
IMPLEMENT_ASN1_DUP_FUNCTION(SM9PublicKey)
static int sm9_key_cb(int operation, ASN1_VALUE **pval,
const ASN1_ITEM *it, void *exarg)
{
if (operation == ASN1_OP_NEW_PRE) {
*pval = (ASN1_VALUE *)SM9_KEY_new();
if (*pval)
return 2;
return 0;
} else if (operation == ASN1_OP_FREE_PRE) {
SM9_KEY_free((SM9_KEY *)*pval);
*pval = NULL;
return 2;
}
return 1;
}
ASN1_SEQUENCE_cb(SM9PrivateKey, sm9_key_cb) = {
ASN1_SIMPLE(SM9_KEY, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9_KEY, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9_KEY, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9_KEY, pointPpub, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9_KEY, identity, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9_KEY, publicPoint, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9_KEY, privatePoint, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END_cb(SM9_KEY, SM9PrivateKey)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(SM9_KEY,SM9PrivateKey,SM9PrivateKey)
ASN1_SEQUENCE_cb(SM9PublicKey, sm9_key_cb) = {
ASN1_SIMPLE(SM9_KEY, pairing, ASN1_OBJECT),
ASN1_SIMPLE(SM9_KEY, scheme, ASN1_OBJECT),
ASN1_SIMPLE(SM9_KEY, hash1, ASN1_OBJECT),
ASN1_SIMPLE(SM9_KEY, pointPpub, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9_KEY, identity, ASN1_OCTET_STRING),
ASN1_SIMPLE(SM9_KEY, publicPoint, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END_cb(SM9_KEY, SM9PublicKey)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(SM9_KEY,SM9PublicKey,SM9PublicKey)
ASN1_SEQUENCE(SM9Ciphertext) = {
ASN1_SIMPLE(SM9Ciphertext, pointC1, ASN1_OCTET_STRING),

View File

@@ -19,6 +19,8 @@
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_SM9,0,reason)
static ERR_STRING_DATA SM9_str_functs[] = {
{ERR_FUNC(SM9_F_OLD_SM9_MASTER_DECODE), "old_sm9_master_decode"},
{ERR_FUNC(SM9_F_OLD_SM9_PRIV_DECODE), "old_sm9_priv_decode"},
{ERR_FUNC(SM9_F_SM9CIPHERTEXT_CHECK), "SM9Ciphertext_check"},
{ERR_FUNC(SM9_F_SM9ENCPARAMETERS_DECRYPT), "SM9EncParameters_decrypt"},
{ERR_FUNC(SM9_F_SM9ENCPARAMETERS_ENCRYPT), "SM9EncParameters_encrypt"},
@@ -40,6 +42,16 @@ static ERR_STRING_DATA SM9_str_functs[] = {
{ERR_FUNC(SM9_F_SM9_GENERATE_KEY_EXCHANGE), "SM9_generate_key_exchange"},
{ERR_FUNC(SM9_F_SM9_GENERATE_MASTER_SECRET),
"SM9_generate_master_secret"},
{ERR_FUNC(SM9_F_SM9_KEY_NEW), "SM9_KEY_new"},
{ERR_FUNC(SM9_F_SM9_MASTER_DECODE), "sm9_master_decode"},
{ERR_FUNC(SM9_F_SM9_MASTER_ENCODE), "sm9_master_encode"},
{ERR_FUNC(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY),
"SM9_MASTER_KEY_extract_key"},
{ERR_FUNC(SM9_F_SM9_MASTER_KEY_NEW), "SM9_MASTER_KEY_new"},
{ERR_FUNC(SM9_F_SM9_PARAMS_DECODE), "sm9_params_decode"},
{ERR_FUNC(SM9_F_SM9_PRIV_DECODE), "sm9_priv_decode"},
{ERR_FUNC(SM9_F_SM9_PRIV_ENCODE), "sm9_priv_encode"},
{ERR_FUNC(SM9_F_SM9_PUB_DECODE), "sm9_pub_decode"},
{ERR_FUNC(SM9_F_SM9_SIGN), "SM9_sign"},
{ERR_FUNC(SM9_F_SM9_SIGNFINAL), "SM9_SignFinal"},
{ERR_FUNC(SM9_F_SM9_SIGNINIT), "SM9_SignInit"},
@@ -53,6 +65,7 @@ static ERR_STRING_DATA SM9_str_functs[] = {
static ERR_STRING_DATA SM9_str_reasons[] = {
{ERR_REASON(SM9_R_BUFFER_TOO_SMALL), "buffer too small"},
{ERR_REASON(SM9_R_DECODE_ERROR), "decode error"},
{ERR_REASON(SM9_R_DIGEST_FAILURE), "digest failure"},
{ERR_REASON(SM9_R_EC_LIB), "ec lib"},
{ERR_REASON(SM9_R_EXTENSION_FIELD_ERROR), "extension field error"},
@@ -80,6 +93,7 @@ static ERR_STRING_DATA SM9_str_reasons[] = {
{ERR_REASON(SM9_R_INVALID_SIGNATURE_FORMAT), "invalid signature format"},
{ERR_REASON(SM9_R_INVALID_TYPE1CURVE), "invalid type1curve"},
{ERR_REASON(SM9_R_KDF_FAILURE), "kdf failure"},
{ERR_REASON(SM9_R_NO_MASTER_SECRET), "no master secret"},
{ERR_REASON(SM9_R_PAIRING_ERROR), "pairing error"},
{ERR_REASON(SM9_R_RATE_PAIRING_ERROR), "rate pairing error"},
{ERR_REASON(SM9_R_TWIST_CURVE_ERROR), "twist curve error"},

View File

@@ -54,30 +54,6 @@
#include <openssl/bn_hash.h>
#include "sm9_lcl.h"
#if 0
int SM9_hash1(const EVP_MD *md, BIGNUM **r,
const char *id, size_t idlen,
unsigned char hid,
const BIGNUM *range,
BN_CTX *ctx)
{
unsigned char *buf;
if (!(buf = OPENSSL_malloc(idlen + 1))) {
return 0;
}
memcpy(buf, id, idlen);
buf[idlen] = hid;
if (!BN_hash_to_range(md, r, buf, idlen + 1, range, ctx)) {
OPENSSL_free(buf);
return 0;
}
OPENSSL_free(buf);
return 1;
}
#endif
int SM9_hash1(const EVP_MD *md, BIGNUM **r, const char *id, size_t idlen,
unsigned char hid, const BIGNUM *n, BN_CTX *ctx)
@@ -130,12 +106,12 @@ end:
return ret;
}
SM9PrivateKey *SM9_extract_private_key(SM9MasterSecret *msk,
const char *id, size_t idlen)
SM9_KEY *SM9_MASTER_KEY_extract_key(SM9_MASTER_KEY *master,
const char *id, size_t idlen, int priv)
{
SM9PrivateKey *ret = NULL;
SM9PrivateKey *sk = NULL;
EC_GROUP *group = NULL;
const BIGNUM *p = SM9_get0_prime();
const BIGNUM *n = SM9_get0_order();
int scheme;
@@ -148,24 +124,24 @@ SM9PrivateKey *SM9_extract_private_key(SM9MasterSecret *msk,
size_t len = sizeof(buf);
/* check args */
if (!msk || !id) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY,
if (!master || !id) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY,
ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
if (strlen(id) != idlen || idlen <= 0 || idlen > SM9_MAX_ID_LENGTH) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY,
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY,
SM9_R_INVALID_ID);
return NULL;
}
/* check pairing */
if (OBJ_obj2nid(msk->pairing) != NID_sm9bn256v1) {
if (OBJ_obj2nid(master->pairing) != NID_sm9bn256v1) {
return NULL;
}
/* check scheme */
scheme = OBJ_obj2nid(msk->scheme);
scheme = OBJ_obj2nid(master->scheme);
switch (scheme) {
case NID_sm9sign:
hid = SM9_HID_SIGN;
@@ -180,9 +156,14 @@ SM9PrivateKey *SM9_extract_private_key(SM9MasterSecret *msk,
return NULL;
}
/* check if master */
if (priv && master->masterSecret == NULL) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, SM9_R_NO_MASTER_SECRET);
return NULL;
}
/* check hash1 and set hash1 md */
switch (OBJ_obj2nid(msk->hash1)) {
switch (OBJ_obj2nid(master->hash1)) {
case NID_sm9hash1_with_sm3:
md = EVP_sm3();
break;
@@ -190,102 +171,140 @@ SM9PrivateKey *SM9_extract_private_key(SM9MasterSecret *msk,
md = EVP_sha256();
break;
default:
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY, SM9_R_INVALID_HASH1);
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, SM9_R_INVALID_HASH1);
return NULL;
}
/* malloc */
if (!(sk = SM9PrivateKey_new())
|| !(group = EC_GROUP_new_by_curve_name(NID_sm9bn256v1))
|| !(ctx = BN_CTX_new())) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY,
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY,
ERR_R_MALLOC_FAILURE);
goto end;
}
BN_CTX_start(ctx);
/* t1 = H1(ID||hid) + msk (mod n) */
if (!(sk->pairing = master->pairing)
|| !(sk->scheme = master->scheme)
|| !(sk->hash1 = master->hash1)
|| !(sk->pointPpub = ASN1_OCTET_STRING_dup(master->pointPpub))
|| !(sk->identity = ASN1_OCTET_STRING_new())
|| !ASN1_OCTET_STRING_set(sk->identity, (unsigned char *)id, idlen)
|| !(sk->publicPoint = ASN1_OCTET_STRING_new())
|| !(sk->privatePoint = ASN1_OCTET_STRING_new())) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_ASN1_LIB);
goto end;
}
/* h1 = H1(id||HID) */
if (!SM9_hash1(md, &t, id, idlen, hid, n, ctx)) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY, ERR_R_SM9_LIB);
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_SM9_LIB);
goto end;
}
if (!BN_mod_add(t, t, msk->masterSecret, n, ctx)) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY, ERR_R_BN_LIB);
goto end;
}
/* if t1 is zero, return failed */
if (BN_is_zero(t)) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY, SM9_R_ZERO_ID);
goto end;
}
/* t2 = msk * t1^-1 (mod n) */
if (!BN_mod_inverse(t, t, n, ctx)) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY, ERR_R_BN_LIB);
goto end;
}
if (!BN_mod_mul(t, msk->masterSecret, t, n, ctx)) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY, ERR_R_BN_LIB);
goto end;
}
/* generate ds or de */
/* generate (Ppubs, ds) or (Ppube, de) */
if (scheme == NID_sm9sign) {
EC_GROUP *group = NULL;
EC_POINT *ds = NULL;
/* ds = t2 * P1 */
if (!(group = EC_GROUP_new_by_curve_name(NID_sm9bn256v1))
|| !(ds = EC_POINT_new(group))
|| !EC_POINT_mul(group, ds, t, NULL, NULL, ctx)
|| !(len = EC_POINT_point2oct(group, ds, point_form, buf, len, ctx))) {
EC_GROUP_free(group);
EC_POINT_free(ds);
/* publicPoint = h1 * P2 + Ppubs */
point_t Ppubs;
point_t point;
if (!point_init(&point, ctx)
|| !point_init(&Ppubs, ctx)
|| ASN1_STRING_length(master->pointPpub) != sizeof(buf)
|| !point_from_octets(&Ppubs, ASN1_STRING_get0_data(master->pointPpub), p, ctx)
|| !point_mul_generator(&point, t, p, ctx)
|| !point_add(&point, &point, &Ppubs, p, ctx)
|| !point_to_octets(&point, buf, ctx)
|| !ASN1_OCTET_STRING_set(sk->publicPoint, buf, sizeof(buf))) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_SM9_LIB);
point_cleanup(&Ppubs);
point_cleanup(&point);
goto end;
}
EC_GROUP_free(group);
EC_POINT_free(ds);
point_cleanup(&Ppubs);
point_cleanup(&point);
} else {
point_t de;
/* de = t2 * P2 */
if (!point_init(&de, ctx)
|| !point_mul_generator(&de, t, p, ctx)
|| !point_to_octets(&de, buf, ctx)) {
point_cleanup(&de);
/* publicPoint = h1 * P1 + Ppube */
EC_POINT *Ppube = NULL;
EC_POINT *point = NULL;
if (!(group = EC_GROUP_new_by_curve_name(NID_sm9bn256v1))
|| !(point = EC_POINT_new(group))
|| !(Ppube = EC_POINT_new(group))
|| !EC_POINT_oct2point(group, Ppube,
ASN1_STRING_get0_data(master->pointPpub),
ASN1_STRING_length(master->pointPpub), ctx)
|| !EC_POINT_mul(group, point, t, NULL, NULL, ctx)
|| !EC_POINT_add(group, point, point, Ppube, ctx)
|| !(len = EC_POINT_point2oct(group, point, point_form, buf, len, ctx))
|| !ASN1_OCTET_STRING_set(sk->publicPoint, buf, len)) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_SM9_LIB);
EC_POINT_free(Ppube);
EC_POINT_free(point);
goto end;
}
EC_POINT_free(Ppube);
EC_POINT_free(point);
}
if (priv) {
/* t1 = H1(ID||hid) + master (mod n) */
if (!BN_mod_add(t, t, master->masterSecret, n, ctx)) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_BN_LIB);
goto end;
}
point_cleanup(&de);
/* if t1 is zero, return failed */
if (BN_is_zero(t)) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, SM9_R_ZERO_ID);
goto end;
}
}
/* t2 = master * t1^-1 (mod n) */
if (!BN_mod_inverse(t, t, n, ctx)) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_BN_LIB);
goto end;
}
if (!BN_mod_mul(t, master->masterSecret, t, n, ctx)) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_BN_LIB);
goto end;
}
ASN1_OBJECT_free(sk->pairing);
ASN1_OBJECT_free(sk->scheme);
ASN1_OBJECT_free(sk->hash1);
sk->pairing = NULL;
sk->scheme = NULL;
sk->hash1 = NULL;
if (!(sk->pairing = msk->pairing)
|| !(sk->scheme = msk->scheme)
|| !(sk->hash1 = msk->hash1)
|| !ASN1_STRING_copy(sk->pointPpub, msk->pointPpub)
|| !ASN1_STRING_set(sk->identity, id, idlen)
/* FIXME: create publicPoint */
|| !ASN1_STRING_set(sk->privatePoint, buf, len)) {
SM9err(SM9_F_SM9_EXTRACT_PRIVATE_KEY, ERR_R_ASN1_LIB);
goto end;
if (scheme == NID_sm9sign) {
/* ds = t2 * P1 */
EC_POINT *ds = NULL;
if (!(ds = EC_POINT_new(group))
|| !EC_POINT_mul(group, ds, t, NULL, NULL, ctx)
|| !(len = EC_POINT_point2oct(group, ds, point_form, buf, len, ctx))
|| !ASN1_OCTET_STRING_set(sk->privatePoint, buf, len)) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_SM9_LIB);
EC_POINT_free(ds);
goto end;
}
EC_POINT_free(ds);
} else {
/* de = t2 * P2 */
point_t de;
if (!point_init(&de, ctx)
|| !point_mul_generator(&de, t, p, ctx)
|| !point_to_octets(&de, buf, ctx)
|| !ASN1_OCTET_STRING_set(sk->privatePoint, buf, sizeof(buf))) {
SM9err(SM9_F_SM9_MASTER_KEY_EXTRACT_KEY, ERR_R_SM9_LIB);
point_cleanup(&de);
goto end;
}
point_cleanup(&de);
}
}
ret = sk;
sk = NULL;
end:
EC_GROUP_free(group);
if (ctx) {
BN_CTX_end(ctx);
}
@@ -295,133 +314,16 @@ end:
return ret;
}
SM9PublicKey *SM9_extract_public_key(SM9PublicParameters *mpk,
SM9PublicKey *SM9_extract_public_key(SM9PublicParameters *master,
const char *id, size_t idlen)
{
SM9PublicKey *ret = NULL;
SM9PublicKey *pk = NULL;
BN_CTX *ctx = NULL;
BIGNUM *h1 = NULL;
int scheme;
unsigned char hid;
const EVP_MD *md;
const BIGNUM *p = SM9_get0_prime();
const BIGNUM *n = SM9_get0_order();
int point_form = POINT_CONVERSION_UNCOMPRESSED;
unsigned char buf[129];
size_t len = sizeof(buf);
return SM9_MASTER_KEY_extract_key(master, id, idlen, 0);
}
if (!(pk = SM9PublicKey_new())
|| !(ctx = BN_CTX_new())) {
goto end;
}
/* check pairing */
if (OBJ_obj2nid(mpk->pairing) != NID_sm9bn256v1) {
goto end;
}
/* check scheme and set hash1 hid */
scheme = OBJ_obj2nid(mpk->scheme);
switch (scheme) {
case NID_sm9sign:
hid = SM9_HID_SIGN;
break;
case NID_sm9encrypt:
hid = SM9_HID_ENC;
break;
case NID_sm9keyagreement:
hid = SM9_HID_EXCH;
break;
default:
goto end;
}
/* check hash1 and set hash1 md */
switch (OBJ_obj2nid(mpk->hash1)) {
case NID_sm9hash1_with_sm3:
md = EVP_sm3();
break;
case NID_sm9hash1_with_sha256:
md = EVP_sha256();
break;
default:
goto end;
}
/* h1 = H1(ID || hid) in [1, n-1] */
if (!SM9_hash1(md, &h1, id, idlen, hid, n, ctx)) {
goto end;
}
if (scheme == NID_sm9sign) {
/* publicPoint = h1 * P2 + Ppubs */
point_t point;
point_t Ppubs;
if (!point_init(&point, ctx)
|| !point_init(&Ppubs, ctx)
|| ASN1_STRING_length(mpk->pointPpub) != sizeof(buf)
|| !point_from_octets(&Ppubs, ASN1_STRING_get0_data(mpk->pointPpub), p, ctx)
|| !point_mul_generator(&point, h1, p, ctx)
|| !point_add(&point, &point, &Ppubs, p, ctx)
|| !point_to_octets(&point, buf, ctx)) {
point_cleanup(&point);
point_cleanup(&Ppubs);
goto end;
}
point_cleanup(&point);
point_cleanup(&Ppubs);
} else if (OBJ_obj2nid(mpk->scheme) == NID_sm9encrypt) {
/* publicPoint = h1 * P1 + Ppube */
EC_GROUP *group = NULL;
EC_POINT *point = NULL;
EC_POINT *Ppube = NULL;
if (!(group = EC_GROUP_new_by_curve_name(NID_sm9bn256v1))
|| !(point = EC_POINT_new(group))
|| !(Ppube = EC_POINT_new(group))
|| !EC_POINT_oct2point(group, Ppube,
ASN1_STRING_get0_data(mpk->pointPpub),
ASN1_STRING_length(mpk->pointPpub), ctx)
|| !EC_POINT_mul(group, point, h1, NULL, NULL, ctx)
|| !EC_POINT_add(group, point, point, Ppube, ctx)
|| !(len = EC_POINT_point2oct(group, point, point_form, buf, len, ctx))) {
EC_GROUP_free(group);
EC_POINT_free(point);
EC_POINT_free(Ppube);
goto end;
}
EC_GROUP_free(group);
EC_POINT_free(point);
EC_POINT_free(Ppube);
}
/* init object */
ASN1_OBJECT_free(pk->pairing);
ASN1_OBJECT_free(pk->scheme);
ASN1_OBJECT_free(pk->hash1);
pk->pairing = NULL;
pk->scheme = NULL;
pk->hash1 = NULL;
if (!(pk->pairing = OBJ_dup(mpk->pairing))
|| !(pk->scheme = OBJ_dup(mpk->scheme))
|| !(pk->hash1 = OBJ_dup(mpk->hash1))
|| !ASN1_STRING_copy(pk->pointPpub, mpk->pointPpub)
|| !ASN1_STRING_set(pk->publicPoint, buf, len)
|| !ASN1_STRING_set(pk->identity, id, idlen)) {
goto end;
}
ret = pk;
pk = NULL;
end:
SM9PublicKey_free(pk);
return ret;
SM9PrivateKey *SM9_extract_private_key(SM9MasterSecret *master,
const char *id, size_t idlen)
{
return SM9_MASTER_KEY_extract_key(master, id, idlen, 1);
}
SM9PublicKey *SM9PrivateKey_get_public_key(SM9PrivateKey *sk)

View File

@@ -52,6 +52,7 @@
#include <openssl/err.h>
#include <openssl/sm9.h>
#include "e_os.h"
/* private key extract algorithms */
#define SM9_HID_SIGN 0x01
@@ -86,38 +87,38 @@ extern "C" {
#endif
struct SM9MasterSecret_st {
struct SM9_MASTER_KEY_st {
/* public */
ASN1_OBJECT *pairing;
ASN1_OBJECT *scheme;
ASN1_OBJECT *hash1;
ASN1_OCTET_STRING *pointPpub;
/* private */
BIGNUM *masterSecret;
int references;
int flags;
CRYPTO_EX_DATA ex_data;
CRYPTO_RWLOCK *lock;
};
struct SM9PublicParameters_st {
ASN1_OBJECT *pairing;
ASN1_OBJECT *scheme;
ASN1_OBJECT *hash1;
ASN1_OCTET_STRING *pointPpub;
};
struct SM9PrivateKey_st {
struct SM9_KEY_st {
/* public */
ASN1_OBJECT *pairing;
ASN1_OBJECT *scheme;
ASN1_OBJECT *hash1;
ASN1_OCTET_STRING *pointPpub;
ASN1_OCTET_STRING *identity;
ASN1_OCTET_STRING *publicPoint;
/* private */
ASN1_OCTET_STRING *privatePoint;
};
struct SM9PublicKey_st {
ASN1_OBJECT *pairing;
ASN1_OBJECT *scheme;
ASN1_OBJECT *hash1;
ASN1_OCTET_STRING *pointPpub;
ASN1_OCTET_STRING *identity;
ASN1_OCTET_STRING *publicPoint;
int references;
int flags;
CRYPTO_EX_DATA ex_data;
CRYPTO_RWLOCK *lock;
};
struct SM9Ciphertext_st {

View File

@@ -50,9 +50,60 @@
#include <string.h>
#include <openssl/err.h>
#include <openssl/sm9.h>
#include <openssl/crypto.h>
#include <openssl/bn_hash.h>
#include "sm9_lcl.h"
SM9_MASTER_KEY *SM9_MASTER_KEY_new(void)
{
SM9_MASTER_KEY *ret = NULL;
if (!(ret = OPENSSL_zalloc(sizeof(*ret)))) {
SM9err(SM9_F_SM9_MASTER_KEY_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
}
return ret;
}
void SM9_MASTER_KEY_free(SM9_MASTER_KEY *key)
{
if (key) {
ASN1_OBJECT_free(key->pairing);
ASN1_OBJECT_free(key->scheme);
ASN1_OBJECT_free(key->hash1);
ASN1_OCTET_STRING_free(key->pointPpub);
BN_clear_free(key->masterSecret);
}
OPENSSL_clear_free(key, sizeof(*key));
}
SM9_KEY *SM9_KEY_new(void)
{
SM9_KEY *ret = NULL;
if (!(ret = OPENSSL_zalloc(sizeof(*ret)))) {
SM9err(SM9_F_SM9_KEY_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
}
return ret;
}
void SM9_KEY_free(SM9_KEY *key)
{
if (key) {
ASN1_OBJECT_free(key->pairing);
ASN1_OBJECT_free(key->scheme);
ASN1_OBJECT_free(key->hash1);
ASN1_OCTET_STRING_free(key->pointPpub);
ASN1_OCTET_STRING_free(key->identity);
ASN1_OCTET_STRING_free(key->publicPoint);
}
OPENSSL_clear_free(key, sizeof(*key));
}
int SM9PrivateKey_get_gmtls_public_key(SM9PublicParameters *mpk,
SM9PrivateKey *sk, unsigned char pub_key[1024])
{
@@ -99,32 +150,26 @@ int SM9_DigestInit(EVP_MD_CTX *ctx, unsigned char prefix,
return 1;
}
#if 0
int SM9_DigestFinal(EVP_MD_CTX *ctx1, BIGNUM *h, const BIGNUM *n_1)
int SM9_MASTER_KEY_up_ref(SM9_MASTER_KEY *msk)
{
int ret = 0;
EVP_MD_CTX *ctx2 = NULL;
const unsigned char ct1[4] = {0x00, 0x00, 0x00, 0x01};
const unsigned char ct2[4] = {0x00, 0x00, 0x00, 0x02};
unsigned char Ha[EVP_MAX_MD_SIZE * 2];
unsigned int len = 0;
int i;
if (!(ctx2 = EVP_MD_CTX_new())
|| !EVP_MD_CTX_copy(ctx2, ctx1)
|| !EVP_DigestUpdate(ctx1, ct1, sizeof(ct))
|| !EVP_DigestUpdate(ctx2, ct2, sizeof(ct2))
|| !EVP_DigestFinal_ex(ctx1, Ha, &len)
|| !EVP_DigestFinal_ex(ctx2, Ha + len, &len)
|| !BN_bin2bn(Ha, 40, h)
|| !BN_mod(h, h, n_1, bn_ctx)
|| !BN_add_word(h, 1)) {
ERR_print_errors_fp(stderr);
goto end;
}
if (CRYPTO_atomic_add(&msk->references, 1, &i, msk->lock) <= 0)
return 0;
ret = 1;
end:
EVP_MD_CTX_free(ctx2);
return ret;
REF_PRINT_COUNT("SM9_MASTER_KEY", msk);
REF_ASSERT_ISNT(i < 2);
return ((i > 1) ? 1 : 0);
}
int SM9_KEY_up_ref(SM9_KEY *sk)
{
int i;
if (CRYPTO_atomic_add(&sk->references, 1, &i, sk->lock) <= 0)
return 0;
REF_PRINT_COUNT("SM9_KEY", sk);
REF_ASSERT_ISNT(i < 2);
return ((i > 1) ? 1 : 0);
}
#endif

151
crypto/sm9/sm9_prn.c Normal file
View File

@@ -0,0 +1,151 @@
/* ====================================================================
* Copyright (c) 2016 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <string.h>
#include <openssl/err.h>
#include <openssl/sm9.h>
#include <openssl/bn_hash.h>
#include "sm9_lcl.h"
int SM9PrivateKey_get_gmtls_public_key(SM9PublicParameters *mpk,
SM9PrivateKey *sk, unsigned char pub_key[1024])
{
return 0;
}
int SM9PublicKey_get_gmtls_encoded(SM9PublicParameters *mpk,
SM9PublicKey *pk, unsigned char encoded[1024])
{
return 0;
}
int SM9_hash2(const EVP_MD *md, BIGNUM **r,
const unsigned char *data, size_t datalen,
const unsigned char *elem, size_t elemlen,
const BIGNUM *range, BN_CTX *ctx)
{
unsigned char *buf;
if (!(buf = OPENSSL_malloc(datalen + elemlen))) {
return 0;
}
memcpy(buf, data, datalen);
memcpy(buf + datalen, elem, elemlen);
if (!BN_hash_to_range(md, r, buf, datalen + elemlen, range, ctx)) {
OPENSSL_free(buf);
return 0;
}
OPENSSL_free(buf);
return 1;
}
int SM9_DigestInit(EVP_MD_CTX *ctx, unsigned char prefix,
const EVP_MD *md, ENGINE *impl)
{
if (!EVP_DigestInit_ex(ctx, md, impl)
|| !EVP_DigestUpdate(ctx, &prefix, 1)) {
ERR_print_errors_fp(stderr);
return 0;
}
return 1;
}
//FIXME: implement this !!!
int SM9MasterSecret_up_ref(SM9MasterSecret *msk)
{
return 1;
}
int SM9PublicParameters_up_ref(SM9PublicParameters *mpk)
{
return 1;
}
int SM9PrivateKey_up_ref(SM9PrivateKey *sk)
{
return 1;
}
int SM9PublicKey_up_ref(SM9PublicKey *pk)
{
return 1;
}
#if 0
int SM9_DigestFinal(EVP_MD_CTX *ctx1, BIGNUM *h, const BIGNUM *n_1)
{
int ret = 0;
EVP_MD_CTX *ctx2 = NULL;
const unsigned char ct1[4] = {0x00, 0x00, 0x00, 0x01};
const unsigned char ct2[4] = {0x00, 0x00, 0x00, 0x02};
unsigned char Ha[EVP_MAX_MD_SIZE * 2];
unsigned int len = 0;
if (!(ctx2 = EVP_MD_CTX_new())
|| !EVP_MD_CTX_copy(ctx2, ctx1)
|| !EVP_DigestUpdate(ctx1, ct1, sizeof(ct))
|| !EVP_DigestUpdate(ctx2, ct2, sizeof(ct2))
|| !EVP_DigestFinal_ex(ctx1, Ha, &len)
|| !EVP_DigestFinal_ex(ctx2, Ha + len, &len)
|| !BN_bin2bn(Ha, 40, h)
|| !BN_mod(h, h, n_1, bn_ctx)
|| !BN_add_word(h, 1)) {
ERR_print_errors_fp(stderr);
goto end;
}
ret = 1;
end:
EVP_MD_CTX_free(ctx2);
return ret;
}
#endif

View File

@@ -73,7 +73,6 @@ SM9MasterSecret *SM9_generate_master_secret(int pairing, int scheme, int hash1)
/* set pairing type */
switch (pairing) {
case NID_sm9bn256v1:
ASN1_OBJECT_free(msk->pairing);
if (!(msk->pairing = OBJ_nid2obj(pairing))) {
SM9err(SM9_F_SM9_GENERATE_MASTER_SECRET, ERR_R_OBJ_LIB);
goto end;
@@ -89,7 +88,6 @@ SM9MasterSecret *SM9_generate_master_secret(int pairing, int scheme, int hash1)
case NID_sm9sign:
case NID_sm9encrypt:
case NID_sm9keyagreement:
ASN1_OBJECT_free(msk->scheme);
if (!(msk->scheme = OBJ_nid2obj(scheme))) {
SM9err(SM9_F_SM9_GENERATE_MASTER_SECRET, ERR_R_OBJ_LIB);
goto end;
@@ -104,7 +102,6 @@ SM9MasterSecret *SM9_generate_master_secret(int pairing, int scheme, int hash1)
switch (hash1) {
case NID_sm9hash1_with_sm3:
case NID_sm9hash1_with_sha256:
ASN1_OBJECT_free(msk->hash1);
if (!(msk->hash1 = OBJ_nid2obj(hash1))) {
SM9err(SM9_F_SM9_GENERATE_MASTER_SECRET, ERR_R_OBJ_LIB);
goto end;
@@ -117,6 +114,11 @@ SM9MasterSecret *SM9_generate_master_secret(int pairing, int scheme, int hash1)
/* generate master secret k = rand(1, n - 1) */
do {
if (!(msk->masterSecret = BN_new())) {
SM9err(SM9_F_SM9_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!BN_rand_range(msk->masterSecret, n)) {
SM9err(SM9_F_SM9_GENERATE_MASTER_SECRET, ERR_R_BN_LIB);
goto end;
@@ -165,6 +167,10 @@ SM9MasterSecret *SM9_generate_master_secret(int pairing, int scheme, int hash1)
goto end;
}
if (!(msk->pointPpub = ASN1_OCTET_STRING_new())) {
SM9err(SM9_F_SM9_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!ASN1_OCTET_STRING_set(msk->pointPpub, buf, (int)len)) {
ERR_print_errors_fp(stderr);
goto end;
@@ -193,17 +199,10 @@ SM9PublicParameters *SM9_extract_public_parameters(SM9MasterSecret *msk)
return NULL;
}
ASN1_OBJECT_free(mpk->pairing);
ASN1_OBJECT_free(mpk->scheme);
ASN1_OBJECT_free(mpk->hash1);
mpk->pairing = NULL;
mpk->scheme = NULL;
mpk->hash1 = NULL;
if (!(mpk->pairing = OBJ_dup(msk->pairing))
|| !(mpk->scheme = OBJ_dup(msk->scheme))
|| !(mpk->hash1 = OBJ_dup(msk->hash1))
|| !ASN1_STRING_copy(mpk->pointPpub, msk->pointPpub)) {
|| !(mpk->pointPpub = ASN1_OCTET_STRING_dup(msk->pointPpub))) {
SM9err(SM9_F_SM9_EXTRACT_PUBLIC_PARAMETERS, ERR_R_MALLOC_FAILURE);
goto end;
}
@@ -211,7 +210,7 @@ SM9PublicParameters *SM9_extract_public_parameters(SM9MasterSecret *msk)
ret = mpk;
mpk = NULL;
end:
end:
SM9PublicParameters_free(mpk);
return ret;
}

View File

@@ -302,6 +302,84 @@ int i2d_PAILLIER_PUBKEY(PAILLIER *a, unsigned char **pp)
}
#endif
#ifndef OPENSSL_NO_SM9
SM9_MASTER_KEY *d2i_SM9_MASTER_PUBKEY(SM9_MASTER_KEY **a, const unsigned char **pp, long length)
{
EVP_PKEY *pkey;
SM9_MASTER_KEY *key;
const unsigned char *q;
q = *pp;
pkey = d2i_PUBKEY(NULL, &q, length);
if (!pkey)
return NULL;
key = EVP_PKEY_get1_SM9_MASTER(pkey);
EVP_PKEY_free(pkey);
if (!key)
return NULL;
*pp = q;
if (a) {
SM9_MASTER_KEY_free(*a);
*a = key;
}
return key;
}
int i2d_SM9_MASTER_PUBKEY(SM9_MASTER_KEY *a, unsigned char **pp)
{
EVP_PKEY *pktmp;
int ret;
if (!a)
return 0;
pktmp = EVP_PKEY_new();
if (pktmp == NULL) {
ASN1err(ASN1_F_I2D_SM9_MASTER_PUBKEY, ERR_R_MALLOC_FAILURE);
return 0;
}
EVP_PKEY_set1_SM9_MASTER(pktmp, a);
ret = i2d_PUBKEY(pktmp, pp);
EVP_PKEY_free(pktmp);
return ret;
}
SM9_KEY *d2i_SM9_PUBKEY(SM9_KEY **a, const unsigned char **pp, long length)
{
EVP_PKEY *pkey;
SM9_KEY *key;
const unsigned char *q;
q = *pp;
pkey = d2i_PUBKEY(NULL, &q, length);
if (!pkey)
return NULL;
key = EVP_PKEY_get1_SM9(pkey);
EVP_PKEY_free(pkey);
if (!key)
return NULL;
*pp = q;
if (a) {
SM9_KEY_free(*a);
*a = key;
}
return key;
}
int i2d_SM9_PUBKEY(SM9_KEY *a, unsigned char **pp)
{
EVP_PKEY *pktmp;
int ret;
if (!a)
return 0;
pktmp = EVP_PKEY_new();
if (pktmp == NULL) {
//ASN1err(ASN1_F_I2D_SM9_PUBKEY, ERR_R_MALLOC_FAILURE);
return 0;
}
EVP_PKEY_set1_SM9(pktmp, a);
ret = i2d_PUBKEY(pktmp, pp);
EVP_PKEY_free(pktmp);
return ret;
}
#endif
#ifndef OPENSSL_NO_DSA
DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
{