new modules

This commit is contained in:
Zhi Guan
2015-10-05 15:34:54 +08:00
parent 8afb3ef97b
commit 254a1266d6
49 changed files with 4895 additions and 187 deletions

1487
MINFO Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -145,12 +145,13 @@ SHLIBDIRS= crypto ssl
# dirs in crypto to build
SDIRS= \
objects \
md4 md5 sha mdc2 hmac sm3 sms4 ripemd whrlpool \
des aes rc2 rc4 idea bf cast camellia seed modes \
md4 md5 sha mdc2 hmac ripemd whrlpool sm3 \
des aes rc2 rc4 idea bf cast camellia seed modes sms4 zuc \
bn ec rsa dsa ecdsa dh ecdh ecies dso engine \
buffer bio stack lhash rand err \
evp asn1 pem x509 x509v3 conf txt_db pkcs7 pkcs12 comp ocsp ui krb5 \
cms pqueue ts srp cmac
# keep in mind that the above list is adjusted by ./Configure
# according to no-xxx arguments...

View File

@@ -145,8 +145,8 @@ SHLIBDIRS= crypto ssl
# dirs in crypto to build
SDIRS= \
objects \
md4 md5 sha mdc2 hmac sm3 sms4 ripemd whrlpool \
des aes rc2 rc4 idea bf cast camellia seed modes \
md4 md5 sha mdc2 hmac ripemd whrlpool sm3 \
des aes rc2 rc4 idea bf cast camellia seed modes sms4 zuc \
bn ec rsa dsa ecdsa dh ecdh ecies dso engine \
buffer bio stack lhash rand err \
evp asn1 pem x509 x509v3 conf txt_db pkcs7 pkcs12 comp ocsp ui krb5 \

View File

@@ -143,8 +143,8 @@ SHLIBDIRS= crypto ssl
# dirs in crypto to build
SDIRS= \
objects \
md2 md4 md5 sha mdc2 hmac sm3 sms4 ripemd whrlpool \
des aes rc2 rc4 rc5 idea bf cast camellia seed modes \
md2 md4 md5 sha mdc2 hmac ripemd whrlpool sm3 \
des aes rc2 rc4 rc5 idea bf cast camellia seed modes sms4 zuc \
bn ec rsa dsa ecdsa dh ecdh ecies dso engine \
buffer bio stack lhash rand err \
evp asn1 pem x509 x509v3 conf txt_db pkcs7 pkcs12 comp ocsp ui krb5 \

View File

@@ -1,14 +1,64 @@
OpenSSL-SM
==========
# GmSSL
The OpenSSL integrated with Chinese national cryptography standard:
GmSSL(国密SSL)是支持国密算法和标准的OpenSSL分支增加了对国密SM2/SM3/SM4算法和ECIES、CPK、ZUC算法的支持实现了这些算法与EVP API和命令行工具的集成。
* SM2: Elliptic curve cryptography, including signature scheme, encryption and key agreement, use the modified `ECDSA` module.
* SM3: Hash algorithm with 256-bit digest length, use `EVP_sm3()`
* SM4: or SMS4, a Feistel-style block cipher with 128-bit key length and 128-bit block size, use `EVP_sms4_ecb()`, `EVP_sms4_cbc()`, `EVP_sms4_cfb()`, `EVP_sms4_ofb()`.
GmSSL的`libcrypto`密码库增加的密码算法包括:
Also SECG algoirthms:
- SM2是国密椭圆曲线公钥密码标准其中包含数字签名、公钥加密和密钥交换三个具体方案以及一个256比特素域上的推荐椭圆曲线参数。GmSSL内置了SM2的推荐曲线参数实现了SM2的签名算法和公钥加密算法。
- SM3是国密密码杂凑算法标准输出的杂凑值长度为256比特。
- SM4是国密分组密码标准又名SMS4其分组长度和密钥长度均为128比特。GmSSL实现了SMS4密码及SMS4的ECB、CBC、CFB、OFB等工作模式。
- X9.63 KDF是密钥派生函数国际标准之一ECIES和SM2公钥加密方案依赖该算法GmSSL实现了X9.63 KDF并用于支持ECIES和SM2公钥加密。
- ECIES (Elliptic Curve Integrated Encryption Scheme)是椭圆曲线公钥加密的国际标准,可用于加密数据。
- CPK是由南相浩和陈钟设计的基于身份的密码。GmSSL实现了CPK的系统建立和密钥生成算法生成的密钥可以用于DSA、ECDH、ECDSA、ECIES、SM2等公钥密码算法。
- ZUC(祖冲之算法)是由我国设计的序列密码以32位字为单位输出密钥流其密钥长度和IV长度均为128比特。GmSSL的ZUC算法处于开发中。
GmSSL提供命令行工具`gmssl`可用于生成SM2签名、SM3摘要、HMAC-SM3消息认证码支持SM4和ZUC数据加解密。
``` bash
$ echo -n abc | gmssl dgst -sm3
66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0
```
GmSSL新增的EVP对象包括`EVP_sm3()`、`EVP_sm4_ecb()`、`EVP_sm4_cbc()`、`EVP_sm4_ofb()`、`EVP_sm4_cfb()`和`EVP_zuc()`。
## 安装
``` bash
./Configure darwin64-x86_64-cc --prefix=/usr/local --openssldir=/usr/local/openssl
make
sudo make install
```
## 注意事项
### SM2签名中的摘要生成
SM2数字签名方案中被签名的杂凑值不仅仅是消息的杂凑值而是签名者身份信息串联上消息的杂凑值其中签名者身份信息是签名者的可辨别标识字串、椭圆曲线方程系数、生成元和签名者公钥串联后字串的杂凑值。SM2标准中并未说明签名者的可辨别标识字串格式及获得方式应用可以采用X.509证书中的Subject字段、CN (CommonName)或自行规定。
### SM2参数选择
SM2标准中规定采用256比特的椭圆曲线域参数并采用256比特的密码杂凑算法并规定某些步骤中须采用SM3。GmSSL的实现支持灵活的参数设定并支持内置的所有密码杂凑算法因此应用可以选择安全程度更高的椭圆曲线域参数和密码杂凑算法如521比特的域参数和SHA-512算法。
### 国密对SSL/TLS的扩展
国密《中华人民共和国密码行业标准(GM/T 0024-2014) SSL VPN技术规范》在RFC 4346 (TLS 1.1)的握手协议基础上增加了基于ECC和IBC的认证模式和密钥交换模式增加的相关密码套件包括
1. ECDHE_SM4_SM3 {0xe0,0x11}
2. ECC_SM4_SM3 {0xe0,0x13}
3. IBSDH_SM4_SM3 {0xe0,0x15}
4. IBC_SM4_SM3 {0xe0,0x17}
5. RSA_SM4_SM3 {0xe0,0x19}
6. RSA_SM4_SHA1 {0xe0,0x1a}
## 开发计划
- 版本号的定义
- 在EVP_PKEY中增加对ECIES和SM2的支持
- 增加对Pairing、IBE和SM9的支持
- 增加对SSL的支持
- 通过ENGINE机制增加对密码硬件设备的支持
* ECIES: Elliptic Curve Integrated Encryption Scheme

View File

@@ -29,7 +29,7 @@ DLIBSSL=../libssl.a
LIBCRYPTO=-L.. -lcrypto
LIBSSL=-L.. -lssl
PROGRAM= openssl
PROGRAM= gmssl
SCRIPTS=CA.sh CA.pl tsget
@@ -945,6 +945,7 @@ speed.o: ../include/openssl/hmac.h ../include/openssl/idea.h
speed.o: ../include/openssl/lhash.h ../include/openssl/md4.h
speed.o: ../include/openssl/md5.h ../include/openssl/mdc2.h
speed.o: ../include/openssl/sm3.h ../include/openssl/sms4.h
speed.o: ../include/openssl/zuc.h
speed.o: ../include/openssl/modes.h ../include/openssl/obj_mac.h
speed.o: ../include/openssl/objects.h ../include/openssl/ocsp.h
speed.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h

View File

@@ -386,7 +386,7 @@ int main(int Argc, char *ARGV[])
if (i++)
prompt = ">";
else
prompt = "OpenSSL> ";
prompt = "GmSSL> ";
fputs(prompt, stdout);
fflush(stdout);
if (!fgets(p, n, stdin))

BIN
apps/openssl Executable file

Binary file not shown.

View File

@@ -1,52 +1,6 @@
/* apps/progs.h */
/* automatically generated by progs.pl for openssl.c */
extern int verify_main(int argc,char *argv[]);
extern int asn1parse_main(int argc,char *argv[]);
extern int req_main(int argc,char *argv[]);
extern int dgst_main(int argc,char *argv[]);
extern int dh_main(int argc,char *argv[]);
extern int dhparam_main(int argc,char *argv[]);
extern int enc_main(int argc,char *argv[]);
extern int passwd_main(int argc,char *argv[]);
extern int gendh_main(int argc,char *argv[]);
extern int errstr_main(int argc,char *argv[]);
extern int ca_main(int argc,char *argv[]);
extern int crl_main(int argc,char *argv[]);
extern int rsa_main(int argc,char *argv[]);
extern int rsautl_main(int argc,char *argv[]);
extern int dsa_main(int argc,char *argv[]);
extern int dsaparam_main(int argc,char *argv[]);
extern int ec_main(int argc,char *argv[]);
extern int ecparam_main(int argc,char *argv[]);
extern int x509_main(int argc,char *argv[]);
extern int genrsa_main(int argc,char *argv[]);
extern int gendsa_main(int argc,char *argv[]);
extern int genpkey_main(int argc,char *argv[]);
extern int s_server_main(int argc,char *argv[]);
extern int s_client_main(int argc,char *argv[]);
extern int speed_main(int argc,char *argv[]);
extern int s_time_main(int argc,char *argv[]);
extern int version_main(int argc,char *argv[]);
extern int pkcs7_main(int argc,char *argv[]);
extern int cms_main(int argc,char *argv[]);
extern int crl2pkcs7_main(int argc,char *argv[]);
extern int sess_id_main(int argc,char *argv[]);
extern int ciphers_main(int argc,char *argv[]);
extern int nseq_main(int argc,char *argv[]);
extern int pkcs12_main(int argc,char *argv[]);
extern int pkcs8_main(int argc,char *argv[]);
extern int pkey_main(int argc,char *argv[]);
extern int pkeyparam_main(int argc,char *argv[]);
extern int pkeyutl_main(int argc,char *argv[]);
extern int spkac_main(int argc,char *argv[]);
extern int smime_main(int argc,char *argv[]);
extern int rand_main(int argc,char *argv[]);
extern int engine_main(int argc,char *argv[]);
extern int ocsp_main(int argc,char *argv[]);
extern int prime_main(int argc,char *argv[]);
extern int ts_main(int argc,char *argv[]);
extern int srp_main(int argc,char *argv[]);
#define FUNC_TYPE_GENERAL 1
#define FUNC_TYPE_MD 2
@@ -63,94 +17,6 @@ typedef struct {
DECLARE_LHASH_OF(FUNCTION);
FUNCTION functions[] = {
{FUNC_TYPE_GENERAL,"verify",verify_main},
{FUNC_TYPE_GENERAL,"asn1parse",asn1parse_main},
{FUNC_TYPE_GENERAL,"req",req_main},
{FUNC_TYPE_GENERAL,"dgst",dgst_main},
#ifndef OPENSSL_NO_DH
{FUNC_TYPE_GENERAL,"dh",dh_main},
#endif
#ifndef OPENSSL_NO_DH
{FUNC_TYPE_GENERAL,"dhparam",dhparam_main},
#endif
{FUNC_TYPE_GENERAL,"enc",enc_main},
{FUNC_TYPE_GENERAL,"passwd",passwd_main},
#ifndef OPENSSL_NO_DH
{FUNC_TYPE_GENERAL,"gendh",gendh_main},
#endif
{FUNC_TYPE_GENERAL,"errstr",errstr_main},
{FUNC_TYPE_GENERAL,"ca",ca_main},
{FUNC_TYPE_GENERAL,"crl",crl_main},
#ifndef OPENSSL_NO_RSA
{FUNC_TYPE_GENERAL,"rsa",rsa_main},
#endif
#ifndef OPENSSL_NO_RSA
{FUNC_TYPE_GENERAL,"rsautl",rsautl_main},
#endif
#ifndef OPENSSL_NO_DSA
{FUNC_TYPE_GENERAL,"dsa",dsa_main},
#endif
#ifndef OPENSSL_NO_DSA
{FUNC_TYPE_GENERAL,"dsaparam",dsaparam_main},
#endif
#ifndef OPENSSL_NO_EC
{FUNC_TYPE_GENERAL,"ec",ec_main},
#endif
#ifndef OPENSSL_NO_EC
{FUNC_TYPE_GENERAL,"ecparam",ecparam_main},
#endif
{FUNC_TYPE_GENERAL,"x509",x509_main},
#ifndef OPENSSL_NO_RSA
{FUNC_TYPE_GENERAL,"genrsa",genrsa_main},
#endif
#ifndef OPENSSL_NO_DSA
{FUNC_TYPE_GENERAL,"gendsa",gendsa_main},
#endif
{FUNC_TYPE_GENERAL,"genpkey",genpkey_main},
#if !defined(OPENSSL_NO_SOCK)
{FUNC_TYPE_GENERAL,"s_server",s_server_main},
#endif
#if !defined(OPENSSL_NO_SOCK)
{FUNC_TYPE_GENERAL,"s_client",s_client_main},
#endif
#ifndef OPENSSL_NO_SPEED
{FUNC_TYPE_GENERAL,"speed",speed_main},
#endif
#if !defined(OPENSSL_NO_SOCK)
{FUNC_TYPE_GENERAL,"s_time",s_time_main},
#endif
{FUNC_TYPE_GENERAL,"version",version_main},
{FUNC_TYPE_GENERAL,"pkcs7",pkcs7_main},
#ifndef OPENSSL_NO_CMS
{FUNC_TYPE_GENERAL,"cms",cms_main},
#endif
{FUNC_TYPE_GENERAL,"crl2pkcs7",crl2pkcs7_main},
{FUNC_TYPE_GENERAL,"sess_id",sess_id_main},
#if !defined(OPENSSL_NO_SOCK)
{FUNC_TYPE_GENERAL,"ciphers",ciphers_main},
#endif
{FUNC_TYPE_GENERAL,"nseq",nseq_main},
#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
{FUNC_TYPE_GENERAL,"pkcs12",pkcs12_main},
#endif
{FUNC_TYPE_GENERAL,"pkcs8",pkcs8_main},
{FUNC_TYPE_GENERAL,"pkey",pkey_main},
{FUNC_TYPE_GENERAL,"pkeyparam",pkeyparam_main},
{FUNC_TYPE_GENERAL,"pkeyutl",pkeyutl_main},
{FUNC_TYPE_GENERAL,"spkac",spkac_main},
{FUNC_TYPE_GENERAL,"smime",smime_main},
{FUNC_TYPE_GENERAL,"rand",rand_main},
#ifndef OPENSSL_NO_ENGINE
{FUNC_TYPE_GENERAL,"engine",engine_main},
#endif
#ifndef OPENSSL_NO_OCSP
{FUNC_TYPE_GENERAL,"ocsp",ocsp_main},
#endif
{FUNC_TYPE_GENERAL,"prime",prime_main},
{FUNC_TYPE_GENERAL,"ts",ts_main},
#ifndef OPENSSL_NO_SRP
{FUNC_TYPE_GENERAL,"srp",srp_main},
#endif
#ifndef OPENSSL_NO_MD2
{FUNC_TYPE_MD,"md2",dgst_main},
#endif
@@ -172,6 +38,9 @@ FUNCTION functions[] = {
#ifndef OPENSSL_NO_RMD160
{FUNC_TYPE_MD,"rmd160",dgst_main},
#endif
#ifndef OPENSSL_NO_SM3
{FUNC_TYPE_MD,"sm3",dgst_main},
#endif
#ifndef OPENSSL_NO_AES
{FUNC_TYPE_CIPHER,"aes-128-cbc",enc_main},
#endif
@@ -376,6 +245,9 @@ FUNCTION functions[] = {
#endif
#ifndef OPENSSL_NO_SMS4
{FUNC_TYPE_CIPHER,"sms4-ofb",enc_main},
#endif
#ifndef OPENSSL_NO_ZUC
{FUNC_TYPE_CIPHER,"zuc",enc_main},
#endif
{0,NULL,NULL}
};

View File

@@ -57,7 +57,7 @@ foreach (@ARGV)
{ print $str; }
}
foreach ("md2","md4","md5","sha","sha1","mdc2","rmd160")
foreach ("md2","md4","md5","sha","sha1","mdc2","rmd160","sm3")
{
push(@files,$_);
printf "#ifndef OPENSSL_NO_".uc($_)."\n\t{FUNC_TYPE_MD,\"".$_."\",dgst_main},\n#endif\n";
@@ -83,7 +83,7 @@ foreach (
"bf-cbc", "bf-ecb", "bf-cfb", "bf-ofb",
"cast5-cbc","cast5-ecb", "cast5-cfb","cast5-ofb",
"cast-cbc", "rc5-cbc", "rc5-ecb", "rc5-cfb", "rc5-ofb",
"sms4-cbc", "sms4-ecb", "sms4-cfb", "sms4-ofb")
"sms4-cbc", "sms4-ecb", "sms4-cfb", "sms4-ofb", "zuc")
{
push(@files,$_);
@@ -99,6 +99,7 @@ foreach (
elsif ($_ =~ /cast/) { $t="#ifndef OPENSSL_NO_CAST\n${t}#endif\n"; }
elsif ($_ =~ /rc5/) { $t="#ifndef OPENSSL_NO_RC5\n${t}#endif\n"; }
elsif ($_ =~ /sms4/) { $t="#ifndef OPENSSL_NO_SMS4\n${t}#endif\n"; }
elsif ($_ =~ /zuc/) { $t="#ifndef OPENSSL_NO_ZUC\n${t}#endif\n"; }
elsif ($_ =~ /zlib/) { $t="#ifdef ZLIB\n${t}#endif\n"; }
print $t;
}

539
crypto/cpk/cpk.h Normal file
View File

@@ -0,0 +1,539 @@
#ifndef HEADER_CPK_H
#define HEADER_CPK_H
#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/ossl_typ.h>
#include "ecies.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CPK_LIB_VERSION "0.9"
#define CPK_MAX_ID_LENGTH 64
/**
* @struct CPK_MASTER_SECRET
* @brief The in-memory structure to represent a cpk master secret.
*/
typedef struct cpk_master_secret_st {
long version; /**< The version of the master secret.*/
X509_NAME *id; /**< The id of the master secret.*/
X509_ALGOR *pkey_algor; /**< The public key algorithm used in the master secret.*/
X509_ALGOR *map_algor; /**< The map algorithm used in the master secret.*/
ASN1_OCTET_STRING *secret_factors; /**< The secret factors of the master secret.*/
} CPK_MASTER_SECRET;
/**
* @def
* @brief Declare 4 basic ASN1 functions of CPK_MASTER_SECRET and a pointer
* to an ASN1_ITEM with detail information of the fields of
* CPK_MASTER_SECRET in it.
*
* The macro would generate following 4 function declarations and 1 pointer:\n
* CPK_MASTER_SECRET *CPK_MASTER_SECRET_new(void): alloc a new instance of CPK_MASTER_SECRET.\n
* void CPK_MASTER_SECRET_free(CPK_MASTER_SECRET* a): free the instance a.\n
* CPK_MASTER_SECRET *d2i_CPK_MASTER_SECRET(CPK_MASTER_SECRET **a, const unsigned char **in, long len):
* convert the CPK_MASTER_SECRET instance from the DER format to the internal format.\n
* int i2d_CPK_MASTER_SECRET(CPK_MASTER_SECRET *a, unsigned char* out): convert an CPK_MASTER_SECRET
* to the DER format.\n
* ASN1_ITEM* CPK_MASTER_SECRET_it: a pointer to a instance of ASN1_ITEM struct which contains
* information on the conversion between DER and internal.\n
*/
DECLARE_ASN1_FUNCTIONS(CPK_MASTER_SECRET)
/**
* @struct CPK_PUBLIC_PARAMS
* @brief The in-memory structure to represent a set of cpk public parameters.
*/
typedef struct cpk_public_params_st {
long version; /**< The version of the public parameters.*/
X509_NAME *id; /**< The id of the public parameters.*/
X509_ALGOR *pkey_algor; /**< The public key algorithm used in the public parameters.*/
X509_ALGOR *map_algor; /**< The map algorithm used in the public parameters.*/
ASN1_OCTET_STRING *public_factors;/**< The public factors of the public parameters.*/
} CPK_PUBLIC_PARAMS;
/**
* @brief Declare 4 basic ASN1 functions of CPK_PUBLIC_PARAMS and a pointer to an ASN1_ITEM
* with detail information of the fields of CPK_PUBLIC_PARAMS in it.
*
* The macro would generate following 4 function declarations and 1 pointer:\n
* CPK_PUBLIC_PARAMS *CPK_PUBLIC_PARAMS_new(void): alloc a new instance of CPK_PUBLIC_PARAMS.\n
* void CPK_PUBLIC_PARAMS_free(CPK_PUBLIC_PARAMS* a): free the instance a.\n
* CPK_PUBLIC_PARAMS *d2i_CPK_PUBLIC_PARAMS(CPK_PUBLIC_PARAMS **a, const unsigned char **in, long len):
* convert the CPK_PUBLIC_PARAMS instance from the DER format to the internal format.\n
* int i2d_CPK_PUBLIC_PARAMS(CPK_PUBLIC_PARAMS *a, unsigned char* out): convert a CPK_PUBLIC_PARAMS
* to the DER format.\n
* ASN1_ITEM* CPK_PUBLIC_PARAMS_it: a pointer to a instance of ASN1_ITEM struct which contains
* information on the conversion between DER and internal.\n
*/
DECLARE_ASN1_FUNCTIONS(CPK_PUBLIC_PARAMS)
/**
* @brief Get a new default map algorithm.
*
* @return Returns a pointer to a new instance of the default map algorithm of the type X509_ALGOR.
*/
X509_ALGOR *CPK_MAP_new_default();
/**
* @brief Check if the given map algorithm is valid.
*
* @param[in] algor The pointer to the algorithm to check.
* @return Returns 1 if the given algorithm is valid.
*/
int CPK_MAP_is_valid(const X509_ALGOR *algor);
/**
* @brief Get the number of factors of the given algorithm.
*
* @param[in] algor The pointer to the algorithm.
* @return Returns the number of factors of the algorithm.
*/
int CPK_MAP_num_factors(const X509_ALGOR *algor);
/**
* @brief Get the number of indexes of the given algorithm.
*
* @param[in] algor The pointer to the algorithm.
* @return Returns the number of indexes of the algorithm.
*/
int CPK_MAP_num_indexes(const X509_ALGOR *algor);
/**
* @brief Convert the string to the index vector.
*
* @param[in] algor The pointer to the algorithm to do the map function.
* @param[in] str The pointer to a string in the memory, ended by '\0'.
* @param[out] index The pointer to a array which will receive the index.
* @return Returns 1 on success.
*/
int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index);
/**
* @brief Print the parameters of the map algortihm.
*
* @param[out] out A IO abstraction to receive the output stream.
* @param[in] indent The amount of the indentation in the output stream.
* @param[in] flags The flag set to control the ouput.
* @return Returns 1 on success.
*/
int CPK_MAP_print(BIO *out, X509_ALGOR *map, int indent, unsigned long flags);
/**
* @brief Create a master secret with the given domain id, public key algorithm and map algorithm.
*
* @param[in] domain_id The domain identifier of the master secret.
* @param[in] pkey The pointer to the public key algorithm of the master secret.
* @param[in] map_algor The pointer to the map algorithm of the master secret.
* @return Returns a poniter to the created master secret on success, or NULL on failure.
*/
CPK_MASTER_SECRET *CPK_MASTER_SECRET_create(const char *domain_id, EVP_PKEY *pkey, X509_ALGOR *map_algor);
/**
* @brief Extract the public parameters from the master secret.
*
* @param[in] master The master secret to extract from.
* @return Returns the pointer to the extracted public parameters on success, or NULL on failure.
*/
CPK_PUBLIC_PARAMS *CPK_MASTER_SECRET_extract_public_params(CPK_MASTER_SECRET *master);
/**
* @brief Extract the private key of a given identifier from the master secret.
*
* @param[in] master The master secret to extract from.
* @param[in] id The identifier which is used to maps to the private key.
* @return Returns the pointer to the extracted private key on success, or NULL on failure.
*/
EVP_PKEY *CPK_MASTER_SECRET_extract_private_key(CPK_MASTER_SECRET *master, const char *id);
/**
* @brief Extract the public key of a given identifier from the public parameters.
*
* @param[in] params The public parameters to extract from.
* @param[in] id The identifier which is used to maps to the public key.
* @return Returns the pointer to the extracted public key EVP_PKEY on success, or NULL on failure.
*/
EVP_PKEY *CPK_PUBLIC_PARAMS_extract_public_key(CPK_PUBLIC_PARAMS *params, const char *id);
int CPK_PUBLIC_PARAMS_compute_share_key(CPK_PUBLIC_PARAMS *params,
void *out, size_t outlen, const char *id, EVP_PKEY *priv_key,
void *(*kdf)(const void *in, size_t inlen, void *out, size_t *outlen));
char *CPK_MASTER_SECRET_get_name(CPK_MASTER_SECRET *master, char *buf, int size);
char *CPK_PUBLIC_PARAMS_get_name(CPK_PUBLIC_PARAMS *params);
/**
* @brief Generate the message digest of the given master secret with the given parameters.
*
* This function takes the secret_factors field of the given parameter of CPK_MASTER_SECRET as
* the input and the parameter type of EVP_MD as the message digest
* algorithm to compute the message digest, and put the result in the parameter md, the length
* of the result in the paramter len.
* @param[in] master The master secret to compute the digest.
* @param[in] type The message digest algorithm to use to comput the digest.
* @param[out] md The buffer to receive the result of the computation of message digest.
* @param[out] len If len is not null, the variable it point to will be assigned the length of
* the message digest.
* @return Returns 1 on success, 0 on failure.
*/
int CPK_MASTER_SECRET_digest(CPK_MASTER_SECRET *master, const EVP_MD *type, unsigned char *md, unsigned int *len);
/**
* @brief Generate the message digest of the given public parameters with the given parameters.
*
* This function takes the secret_factors field of the given parameter of CPK_PUBLIC_PARAMS as
* the input and the parameter type of EVP_MD as the message digest
* algorithm to compute the message digest, and put the result in the parameter md, the length
* of the result in the paramter len.
* @param[in] params The public parameters to compute the digest.
* @param[in] type The message digest algorithm to use to comput the digest.
* @param[out] md The buffer to receive the result of the computation of message digest.
* @param[out] len If len is not null, the variable it point to will be assigned the length of
* the message digest.
* @return Returns 1 on success, 0 on failure.
*/
int CPK_PUBLIC_PARAMS_digest(CPK_PUBLIC_PARAMS *params, const EVP_MD *type, unsigned char *md, unsigned int *len);
/**
* @brief Print the master secret to a BIO, including the version, the domain uri, the public
* algorithm and the map algorithm.
*
* @param[out] out A IO abstraction to receive the output stream.
* @param[in] master The CPK_MASTER_SECRET instance to print.
* @param[in] indent The amount of the indentation.
* @param[in] flags The flag set to control the ouput.
* @return Returns 1 on success, 0 on failure.
*/
int CPK_MASTER_SECRET_print(BIO *out, CPK_MASTER_SECRET *master, int indent, unsigned long flags);
/**
* @brief Print the public parameters to a BIO.
*
* @param[out] out A IO abstraction to receive the output stream.
* @param[in] params The CPK_PUBLIC_PARAMS instance to print.
* @param[in] indent The amount of the indentation.
* @param[in] flags The flag set to control the ouput.
* @return Returns 1 on success, 0 on failure.
*/
int CPK_PUBLIC_PARAMS_print(BIO *out, CPK_PUBLIC_PARAMS *params, int indent, unsigned long flags);
/**
* @brief Validate the public parameters with the given master secret.
*
* @param[in] master The master secret used for the validation.
* @param[in] params The public parameters to validate.
* @return Returns 1 if the public parameter is valid, returns 0 otherwise.
*/
int CPK_MASTER_SECRET_validate_public_params(CPK_MASTER_SECRET *master, CPK_PUBLIC_PARAMS *params);
/**
* @brief Validate the private key with the given public parameters.
*
* @param[in] params The public parameter used for the validation.
* @param[in] id the identifier of the private key owner.
* @param[in] pkey pkey The private key to validate.
* @return Returns 1 if the private key is valid, returns a integer less or equal than 0 otherwise.
*/
int CPK_PUBLIC_PARAMS_validate_private_key(CPK_PUBLIC_PARAMS *params, const char *id, const EVP_PKEY *pkey);
/**
* @brief Convert the master secret in DER format in the IO abstraction to an instance of CPK_MASTER_SECRET.
*
* @param[in] bp A pointer to the IO abstraction which ocntaints the master secret in DER format.
* @param[out] master A pointer to receive the pointer to the converted master secret of the type CPK_MASTER_SECRET.
* @return Returns the pointer to the converted master secret of the type CPK_MASTER_SECRET on success,
* or null on failure.
*/
CPK_MASTER_SECRET *d2i_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET **master);
/**
* @brief Convert the master key from CPK_MASTER_SECRET to a byte stream in DER format, and write
* the stream to an IO abstraction.
*
* @param[out] bp A pointer to the IO abstraction which receives the stream.
* @param[in] master A pointer to the master key of the type CPK_MASTER_SECRET.
* @return Returns the size of the output stream on success, of an integer less or equal than 0
* indicating an error.
*/
int i2d_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET *master);
/**
* @brief Convert the public parameters in DER format in the IO abstraction to an instance of CPK_PUBLIC_PARAMS.
*
* @param[in] bp A pointer to the IO abstraction which ocntaints the public parameters in DER format.
* @param[out] params A pointer to receive the pointer to the converted public parameters of the type CPK_PUBLIC_PARAMS.
* @return Returns the pointer to the converted public parameters of the type CPK_PUBLIC_PARAMS on success,
* or null on failure.
*/
CPK_PUBLIC_PARAMS *d2i_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS **params);
/**
* @brief Convert the public parameters from CPK_PUBLIC_PARAMS to a byte stream in DER format,
* and write the stream to an IO abstraction.
*
* @param[out] bp A pointer to the IO abstraction which receives the stream.
* @param[in] master A pointer to the public parameters of the type CPK_PUBLIC_PARAMS.
* @return Returns the size of the output stream on success, of an integer less or equal than 0
* indicating an error.
*/
int i2d_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS *params);
/*
* SignerInfo ::= SEQUENCE {
* version INTEGER {1},
* signer IssuerAndSerialNumber,
* digestAlgor DigestAlgorithmIdentifier,
* signedAttrs [0] IMPLICIT Attributes OPTIONAL,
* signingAlgor SigningAlgorithmIdentifier,
* signature OCTET STRING {{ECDSASigValue}},
* unsignedAttrs [1] IMPLICIT Attributes OPTIONAL
* }
*/
typedef struct cpk_signer_info_st {
long version;
X509_NAME *signer;
X509_ALGOR *digest_algor;
STACK_OF(X509_ATTRIBUTE) *signed_attr;
X509_ALGOR *sign_algor;
ASN1_OCTET_STRING *signature;
STACK_OF(X509_ATTRIBTE) *unsigned_attr;
EVP_PKEY *_privkey; /* private member */
} CPK_SIGNER_INFO;
DECLARE_STACK_OF(CPK_SIGNER_INFO)
DECLARE_ASN1_SET_OF(CPK_SIGNER_INFO)
DECLARE_ASN1_FUNCTIONS(CPK_SIGNER_INFO)
int CPK_SIGNER_INFO_set(CPK_SIGNER_INFO *si, const EVP_MD *sign_alg, const EVP_PKEY *sign_key);
int CPK_SIGNER_INFO_add_attr(CPK_SIGNER_INFO *si, int nid, int atrtype, void *value);
int CPK_SIGNER_INFO_add_signed_attr(CPK_SIGNER_INFO *si, int nid, int atrtype, void *value);
int CPK_SIGNER_INFO_add_signed_time(CPK_SIGNER_INFO *si);
int CPK_SIGNER_INFO_add_signed_digest(CPK_SIGNER_INFO *si, const EVP_MD_CTX *ctx);
ASN1_TYPE *CPK_SIGNER_INFO_get_attr(CPK_SIGNER_INFO *si, int nid);
ASN1_TYPE *CPK_SIGNER_INFO_get_signed_attr(CPK_SIGNER_INFO *si, int nid);
ASN1_UTCTIME *CPK_SIGNER_INFO_get_signed_time(CPK_SIGNER_INFO *si);
int CPK_SIGNER_INFO_do_sign(CPK_SIGNER_INFO *si, EVP_MD_CTX *md_ctx);
int CPK_SIGNER_INFO_do_verify(const CPK_SIGNER_INFO *si, EVP_MD_CTX *ctx, const CPK_PUBLIC_PARAMS *params);
/*
* RecipientInfo ::= SEQUENCE {
* version INTEGER {0},
* recipient IssuerAndSerialNumber,
* keyEncryptionAlgor EncryptionAlgorithmIdentifier,
* encryptedKey OCTET STRING
* }
* RecipientInfos ::= SET OF RecipientInfo
*/
typedef struct cpk_recip_info_st {
long version;
X509_NAME *recipient;
X509_ALGOR *enc_algor;
ASN1_OCTET_STRING *enc_data;
/* private */
EVP_PKEY *_pubkey;
} CPK_RECIP_INFO;
DECLARE_STACK_OF(CPK_RECIP_INFO)
DECLARE_ASN1_SET_OF(CPK_RECIP_INFO)
DECLARE_ASN1_FUNCTIONS(CPK_RECIP_INFO)
int CPK_RECIP_INFO_set(CPK_RECIP_INFO *ri, const X509_NAME *recipient, const ECIES_PARAMS *ecies);
int CPK_RECIP_INFO_do_encrypt(CPK_RECIP_INFO *ri, const unsigned char *in, size_t inlen);
int CPK_RECIP_INFO_do_decrypt(CPK_RECIP_INFO *ri, const EVP_PKEY *pkey, unsigned char *out, size_t *outlen);
/*
* SingerInfos ::= SET OF SignerInfo
*
* SignedData ::= SEQUENCE {
* version INTEGER,
* digestAlgors DigestAlgorithmIdentifiers,
* contentInfo ContentInfo,
* signerInfos SignerInfos,
* }
*/
typedef struct cpk_signed_st {
long version;
STACK_OF(X509_ALGOR) *digest_algors;
STACK_OF(X509) *cert; /* [ 0 ] */
STACK_OF(X509_CRL) *crl; /* [ 1 ] */
STACK_OF(CPK_SIGINFO) *signer_infos;
struct CPK_CMS_st *contents;
} CPK_SIGNED;
DECLARE_ASN1_FUNCTIONS(CPK_SIGNED)
typedef struct cpk_enc_content_st {
ASN1_OBJECT *content_type;
X509_ALGOR *enc_algor;
ASN1_OCTET_STRING *enc_data; /* [ 0 ] */
/* private */
const EVP_CIPHER *cipher;
} CPK_ENC_CONTENT;
DECLARE_ASN1_FUNCTIONS(CPK_ENC_CONTENT)
typedef struct cpk_envelope_st {
long version;
STACK_OF(CPK_RECIP_INFO) *recip_infos;
CPK_ENC_CONTENT *enc_data;
} CPK_ENVELOPE;
DECLARE_ASN1_FUNCTIONS(CPK_ENVELOPE)
typedef struct cpk_sign_envelope_st {
long version;
STACK_OF(X509_ALGOR) *digest_algors;
STACK_OF(X509) *cert; /* [ 0 ] */
STACK_OF(X509_CRL) *crl; /* [ 1 ] */
STACK_OF(CPK_SIGNER_INFO) *signer_infos;
CPK_ENC_CONTENT *enc_data;
STACK_OF(CPK_RECIP_INFO) *recip_infos;
} CPK_SIGN_ENVELOPE;
DECLARE_ASN1_FUNCTIONS(CPK_SIGN_ENVELOPE)
typedef struct cpk_cms_st {
int state; /* used during processing */
int detached;
ASN1_OBJECT *type;
union {
char *ptr;
/* NID_pkcs7_data */
ASN1_OCTET_STRING *data;
/* NID_pkcs7_signed */
CPK_SIGNED *sign;
/* NID_pkcs7_enveloped */
CPK_ENVELOPE *enveloped;
/* NID_pkcs7_signedAndEnveloped */
CPK_SIGN_ENVELOPE *signed_and_enveloped;
/* Anything else */
ASN1_TYPE *other;
} d;
} CPK_CMS;
DECLARE_STACK_OF(CPK_CMS)
DECLARE_ASN1_SET_OF(CPK_CMS)
DECLARE_PKCS12_STACK_OF(CPK_CMS)
DECLARE_ASN1_FUNCTIONS(CPK_CMS)
DECLARE_ASN1_ITEM(CPK_CMS_ATTR_SIGN)
DECLARE_ASN1_ITEM(CPK_CMS_ATTR_VERIFY)
DECLARE_ASN1_NDEF_FUNCTION(CPK_CMS)
#define CPK_CMS_OP_SET_DETACHED_SIGNATURE 1
#define CPK_CMS_OP_GET_DETACHED_SIGNATURE 2
#define CPK_CMS_get_signed_attributes(si) ((si)->auth_attr)
#define CPK_CMS_get_attributes(si) ((si)->unauth_attr)
#define CPK_CMS_type_is_signed(a) \
(OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
#define CPK_CMS_type_is_enveloped(a) \
(OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped)
#define CPK_CMS_type_is_signedAndEnveloped(a) \
(OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped)
#define CPK_CMS_type_is_data(a) \
(OBJ_obj2nid((a)->type) == NID_pkcs7_data)
#define CPK_CMS_set_detached(p,v) \
CPK_CMS_ctrl(p,CPK_CMS_OP_SET_DETACHED_SIGNATURE,v,NULL)
#define CPK_CMS_get_detached(p) \
CPK_CMS_ctrl(p,CPK_CMS_OP_GET_DETACHED_SIGNATURE,0,NULL)
#define CPK_CMS_is_detached(p7) \
(CPK_CMS_type_is_signed(p7) && CPK_CMS_get_detached(p7))
long CPK_CMS_ctrl(CPK_CMS *p7, int cmd, long larg, char *parg);
int CPK_CMS_set_type(CPK_CMS *p7, int type);
int CPK_CMS_set_cipher(CPK_CMS *p7, const EVP_CIPHER *cipher);
int CPK_CMS_set_content(CPK_CMS *p7, CPK_CMS *p7_data);
int CPK_CMS_content_new(CPK_CMS *p7, int type);
int CPK_CMS_add_signer(CPK_CMS *p7, const EVP_MD *sign_alg, const EVP_PKEY *sign_key);
int CPK_CMS_add_recipient(CPK_CMS *p7, const X509_NAME *id, const ECIES_PARAMS *params);
BIO *CPK_CMS_dataInit(CPK_CMS *p7, BIO *bio);
BIO *CPK_CMS_dataDecode(CPK_CMS *p7, BIO *in_bio, const EVP_PKEY *keyinfo);
int CPK_CMS_dataUpdate(CPK_CMS *p7, BIO *bio, const unsigned char *data, int len);
int CPK_CMS_dataFinal(CPK_CMS *p7, BIO *bio);
STACK_OF(CPK_SIGNER_INFO) *CPK_CMS_get_signer_infos(CPK_CMS *p7);
int CPK_CMS_dataVerify(CPK_PUBLIC_PARAMS *params, BIO *bio, CPK_CMS *p7, CPK_SIGNER_INFO *si);
/* ERR function (should in openssl/err.h) begin */
#define ERR_LIB_CPK 130
#define ERR_R_CPK_LIB ERR_LIB_CPK
#define CPKerr(f,r) ERR_PUT_error(ERR_LIB_CPK,(f),(r),__FILE__,__LINE__)
/* end */
void ERR_load_CPK_strings(void);
/**
* @defgroup error_cpk Definations to handle errors of cpk runtime.
* @{
*/
/* Error codes for the ECIES functions. */
/* Function codes. */
#define CPK_F_CPK_MASTER_SECRET_CREATE 100
#define CPK_F_CPK_MASTER_SECRET_EXTRACT_PUBLIC_PARAMS 101
#define CPK_F_CPK_MASTER_SECRET_EXTRACT_PRIVATE_KEY 102
#define CPK_F_CPK_PUBLIC_PARAMS_EXTRACT_PUBLIC_KEY 103
#define CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY 116
#define CPK_F_CPK_MASTER_SECRET_DIGEST 104
#define CPK_F_CPK_PUBLIC_PARAMS_DIGEST 105
#define CPK_F_CPK_MASTER_SECRET_PRINT 106
#define CPK_F_CPK_PUBLIC_PARAMS_PRINT 107
#define CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS 108
#define CPK_F_CPK_PUBLIC_PARAMS_VALIDATE_PRIVATE_KEY 109
#define CPK_F_CPK_MAP_NEW_DEFAULT 110
#define CPK_F_CPK_MAP_NUM_FACTORS 111
#define CPK_F_CPK_MAP_NUM_INDEXES 112
#define CPK_F_CPK_MAP_STR2INDEX 113
#define CPK_F_X509_ALGOR_GET1_EC_KEY 114
#define CPK_F_X509_ALGOR_GET1_DSA 115
/* Reason codes. */
#define CPK_R_BAD_ARGUMENT 100
#define CPK_R_UNKNOWN_DIGEST_TYPE 101
#define CPK_R_UNKNOWN_CIPHER_TYPE 102
#define CPK_R_UNKNOWN_MAP_TYPE 103
#define CPK_R_UNKNOWN_CURVE 104
#define CPK_R_STACK_ERROR 105
#define CPK_R_DERIVE_KEY_FAILED 106
#define CPK_R_ECIES_ENCRYPT_FAILED 107
#define CPK_R_ECIES_DECRYPT_FAILED 108
#define CPK_R_DER_DECODE_FAILED 109
#define CPK_R_UNSUPPORTED_PKCS7_CONTENT_TYPE 110
#define CPK_R_SET_SIGNER 111
#define CPK_R_SET_RECIP_INFO 112
#define CPK_R_UNABLE_TO_FIND_MESSAGE_DIGEST 113
#define CPK_R_BAD_DATA 114
#define CPK_R_MAP_FAILED 115
#define CPK_R_ADD_SIGNING_TIME 116
#define CPK_R_VERIFY_FAILED 117
#define CPK_R_UNKNOWN_ECDH_TYPE 118
#define CPK_R_DIGEST_FAILED 119
#define CPK_R_WITHOUT_DECRYPT_KEY 120
#define CPK_R_UNKNOWN_PKCS7_TYPE 121
#define CPK_R_INVALID_ID_LENGTH 122
#define CPK_R_INVALID_PKEY_TYPE 123
#define CPK_R_INVALID_MAP_ALGOR 124
#define CPK_R_PKEY_TYPE_NOT_MATCH 125
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

124
crypto/cpk/cpk_asn1.c Normal file
View File

@@ -0,0 +1,124 @@
#include <openssl/asn1t.h>
#include "cpk.h"
ASN1_SEQUENCE(CPK_MASTER_SECRET) = {
ASN1_SIMPLE(CPK_MASTER_SECRET, version, LONG),
ASN1_SIMPLE(CPK_MASTER_SECRET, id, X509_NAME),
ASN1_SIMPLE(CPK_MASTER_SECRET, pkey_algor, X509_ALGOR),
ASN1_SIMPLE(CPK_MASTER_SECRET, map_algor, X509_ALGOR),
ASN1_SIMPLE(CPK_MASTER_SECRET, secret_factors, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(CPK_MASTER_SECRET)
IMPLEMENT_ASN1_FUNCTIONS(CPK_MASTER_SECRET)
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_MASTER_SECRET)
ASN1_SEQUENCE(CPK_PUBLIC_PARAMS) = {
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, version, LONG),
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, id, X509_NAME),
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, pkey_algor, X509_ALGOR),
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, map_algor, X509_ALGOR),
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, public_factors, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(CPK_PUBLIC_PARAMS)
IMPLEMENT_ASN1_FUNCTIONS(CPK_PUBLIC_PARAMS)
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_PUBLIC_PARAMS)
CPK_MASTER_SECRET *d2i_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET **master) {
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CPK_MASTER_SECRET), bp, master);
}
int i2d_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET *master) {
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CPK_MASTER_SECRET), bp, master);
}
CPK_PUBLIC_PARAMS *d2i_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS **params) {
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CPK_PUBLIC_PARAMS), bp, params);
}
int i2d_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS *params) {
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CPK_PUBLIC_PARAMS), bp, params);
}
/* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
ASN1_ADB_TEMPLATE(cpkcmsdefault) = ASN1_EXP_OPT(CPK_CMS, d.other, ASN1_ANY, 0);
ASN1_ADB(CPK_CMS) = {
ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(CPK_CMS, d.data, ASN1_OCTET_STRING_NDEF, 0)),
ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(CPK_CMS, d.sign, CPK_SIGNED, 0)),
ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(CPK_CMS, d.enveloped, CPK_ENVELOPE, 0)),
ADB_ENTRY(NID_pkcs7_signedAndEnveloped,
ASN1_NDEF_EXP_OPT(CPK_CMS, d.signed_and_enveloped, CPK_SIGN_ENVELOPE, 0)),
} ASN1_ADB_END(CPK_CMS, 0, type, 0, &cpkcmsdefault_tt, NULL);
ASN1_NDEF_SEQUENCE(CPK_CMS) = {
ASN1_SIMPLE(CPK_CMS, type, ASN1_OBJECT),
ASN1_ADB_OBJECT(CPK_CMS)
}ASN1_NDEF_SEQUENCE_END(CPK_CMS)
IMPLEMENT_ASN1_FUNCTIONS(CPK_CMS)
IMPLEMENT_ASN1_NDEF_FUNCTION(CPK_CMS)
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_CMS)
ASN1_NDEF_SEQUENCE(CPK_SIGNED) = {
ASN1_SIMPLE(CPK_SIGNED, version, LONG),
ASN1_SET_OF(CPK_SIGNED, digest_algors, X509_ALGOR),
ASN1_SIMPLE(CPK_SIGNED, contents, CPK_CMS),
ASN1_IMP_SEQUENCE_OF_OPT(CPK_SIGNED, cert, X509, 0),
ASN1_IMP_SET_OF_OPT(CPK_SIGNED, crl, X509_CRL, 1),
ASN1_SET_OF(CPK_SIGNED, signer_infos, CPK_SIGNER_INFO)
} ASN1_NDEF_SEQUENCE_END(CPK_SIGNED)
IMPLEMENT_ASN1_FUNCTIONS(CPK_SIGNED)
ASN1_SEQUENCE(CPK_SIGNER_INFO) = {
ASN1_SIMPLE(CPK_SIGNER_INFO, version, LONG),
ASN1_SIMPLE(CPK_SIGNER_INFO, signer, X509_NAME),
ASN1_SIMPLE(CPK_SIGNER_INFO, digest_algor, X509_ALGOR),
ASN1_IMP_SEQUENCE_OF_OPT(CPK_SIGNER_INFO, signed_attr, X509_ATTRIBUTE, 0),
ASN1_SIMPLE(CPK_SIGNER_INFO, sign_algor, X509_ALGOR),
ASN1_SIMPLE(CPK_SIGNER_INFO, signature, ASN1_OCTET_STRING),
ASN1_IMP_SET_OF_OPT(CPK_SIGNER_INFO, unsigned_attr, X509_ATTRIBUTE, 1)
} ASN1_SEQUENCE_END(CPK_SIGNER_INFO)
IMPLEMENT_ASN1_FUNCTIONS(CPK_SIGNER_INFO)
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_SIGNER_INFO)
ASN1_NDEF_SEQUENCE(CPK_ENVELOPE) = {
ASN1_SIMPLE(CPK_ENVELOPE, version, LONG),
ASN1_SET_OF(CPK_ENVELOPE, recip_infos, CPK_RECIP_INFO),
ASN1_SIMPLE(CPK_ENVELOPE, enc_data, CPK_ENC_CONTENT)
} ASN1_NDEF_SEQUENCE_END(CPK_ENVELOPE)
IMPLEMENT_ASN1_FUNCTIONS(CPK_ENVELOPE)
ASN1_SEQUENCE(CPK_RECIP_INFO) = {
ASN1_SIMPLE(CPK_RECIP_INFO, version, LONG),
ASN1_SIMPLE(CPK_RECIP_INFO, recipient, X509_NAME),
ASN1_SIMPLE(CPK_RECIP_INFO, enc_algor, X509_ALGOR),
ASN1_SIMPLE(CPK_RECIP_INFO, enc_data, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(CPK_RECIP_INFO)
IMPLEMENT_ASN1_FUNCTIONS(CPK_RECIP_INFO)
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_RECIP_INFO)
ASN1_NDEF_SEQUENCE(CPK_ENC_CONTENT) = {
ASN1_SIMPLE(CPK_ENC_CONTENT, content_type, ASN1_OBJECT),
ASN1_SIMPLE(CPK_ENC_CONTENT, enc_algor, X509_ALGOR),
ASN1_IMP_OPT(CPK_ENC_CONTENT, enc_data, ASN1_OCTET_STRING, 0)
} ASN1_NDEF_SEQUENCE_END(CPK_ENC_CONTENT)
IMPLEMENT_ASN1_FUNCTIONS(CPK_ENC_CONTENT)
ASN1_NDEF_SEQUENCE(CPK_SIGN_ENVELOPE) = {
ASN1_SIMPLE(CPK_SIGN_ENVELOPE, version, LONG),
ASN1_SET_OF(CPK_SIGN_ENVELOPE, recip_infos, CPK_RECIP_INFO),
ASN1_SET_OF(CPK_SIGN_ENVELOPE, digest_algors, X509_ALGOR),
ASN1_SIMPLE(CPK_SIGN_ENVELOPE, enc_data, CPK_ENC_CONTENT),
ASN1_IMP_SET_OF_OPT(CPK_SIGN_ENVELOPE, cert, X509, 0),
ASN1_IMP_SET_OF_OPT(CPK_SIGN_ENVELOPE, crl, X509_CRL, 1),
ASN1_SET_OF(CPK_SIGN_ENVELOPE, signer_infos, CPK_SIGNER_INFO)
} ASN1_NDEF_SEQUENCE_END(CPK_SIGN_ENVELOPE)
IMPLEMENT_ASN1_FUNCTIONS(CPK_SIGN_ENVELOPE)

75
crypto/cpk/cpk_err.c Normal file
View File

@@ -0,0 +1,75 @@
#include <openssl/err.h>
#include "cpk.h"
/* BEGIN ERROR CODES */
#ifndef OPENSSL_NO_ERR
#define ERR_FUNC(func) ERR_PACK(ERR_LIB_CPK,func,0)
#define ERR_REASON(reason) ERR_PACK(ERR_LIB_CPK,0,reason)
static ERR_STRING_DATA CPK_str_functs[] =
{
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_CREATE), "CPK_MASTER_SECRET_create"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_EXTRACT_PUBLIC_PARAMS), "CPK_MASTER_SECRET_extract_public_params"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_EXTRACT_PRIVATE_KEY), "CPK_MASTER_SECRET_extract_private_key"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_EXTRACT_PUBLIC_KEY), "CPK_PUBLIC_PARAMS_extract_public_key"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY), "CPK_PUBLIC_PARAMS_compute_share_key"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_DIGEST), "CPK_F_CPK_MASTER_SECRET_digest"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_DIGEST), "CPK_F_CPK_PUBLIC_PARAMS_digest"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_PRINT), "CPK_F_CPK_MASTER_SECRET_print"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_PRINT), "CPK_F_CPK_PUBLIC_PARAMS_print"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS), "CPK_F_CPK_MASTER_SECRET_validate_public_params"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_VALIDATE_PRIVATE_KEY), "CPK_F_CPK_PUBLIC_PARAMS_validate_private_key"},
{ERR_FUNC(CPK_F_CPK_MAP_NEW_DEFAULT), "CPK_F_CPK_MAP_new_default"},
{ERR_FUNC(CPK_F_CPK_MAP_NUM_FACTORS), "CPK_F_CPK_MAP_num_factors"},
{ERR_FUNC(CPK_F_CPK_MAP_NUM_INDEXES), "CPK_F_CPK_MAP_num_indexes"},
{ERR_FUNC(CPK_F_CPK_MAP_STR2INDEX), "CPK_F_CPK_MAP_STR2INDEX"},
{ERR_FUNC(CPK_F_X509_ALGOR_GET1_EC_KEY), "X509_ALGOR_get1_ec_key"},
{ERR_FUNC(CPK_F_X509_ALGOR_GET1_DSA), "X509_ALGOR_get1_dsa"},
{0, NULL}
};
static ERR_STRING_DATA CPK_str_reasons[] =
{
{ERR_REASON(CPK_R_BAD_ARGUMENT), "bad argument"},
{ERR_REASON(CPK_R_UNKNOWN_DIGEST_TYPE), "unknown digest algorithm"},
{ERR_REASON(CPK_R_UNKNOWN_CIPHER_TYPE), "unknown cipher algorithm"},
{ERR_REASON(CPK_R_UNKNOWN_MAP_TYPE), "unknown cpk map algorithm"},
{ERR_REASON(CPK_R_UNKNOWN_CURVE), "unknown elliptic curve"},
{ERR_REASON(CPK_R_STACK_ERROR), "stack error"},
{ERR_REASON(CPK_R_DERIVE_KEY_FAILED), "derive key failed"},
{ERR_REASON(CPK_R_ECIES_ENCRYPT_FAILED), "ecies encryption failed"},
{ERR_REASON(CPK_R_ECIES_DECRYPT_FAILED), "ecies decryption failed"},
{ERR_REASON(CPK_R_DER_DECODE_FAILED), "DER decode failed"},
{ERR_REASON(CPK_R_UNSUPPORTED_PKCS7_CONTENT_TYPE),"CPK_R_UNSUPPORTED_PKCS7_CONTENT_TYPE"},
{ERR_REASON(CPK_R_SET_SIGNER), "CPK_R_SET_SIGNER"},
{ERR_REASON(CPK_R_SET_RECIP_INFO), "CPK_R_SET_RECIP_INFO"},
{ERR_REASON(CPK_R_UNABLE_TO_FIND_MESSAGE_DIGEST),"CPK_R_UNABLE_TO_FIND_MESSAGE_DIGEST"},
{ERR_REASON(CPK_R_BAD_DATA), "bad data"},
{ERR_REASON(CPK_R_MAP_FAILED), "CPK_R_MAP_FAILED"},
{ERR_REASON(CPK_R_ADD_SIGNING_TIME), "CPK_R_ADD_SIGNING_TIME"},
{ERR_REASON(CPK_R_VERIFY_FAILED), "CPK_R_VERIFY_FAILED"},
{ERR_REASON(CPK_R_UNKNOWN_ECDH_TYPE), "CPK_R_UNKNOWN_ECDH_TYPE"},
{ERR_REASON(CPK_R_DIGEST_FAILED), "CPK_R_DIGEST_FAILED"},
{ERR_REASON(CPK_R_WITHOUT_DECRYPT_KEY), "CPK_R_WITHOUT_DECRYPT_KEY"},
{ERR_REASON(CPK_R_UNKNOWN_PKCS7_TYPE), "CPK_R_UNKNOWN_PKCS7_TYPE"},
{ERR_REASON(CPK_R_INVALID_ID_LENGTH), "invalid identity length"},
{ERR_REASON(CPK_R_INVALID_PKEY_TYPE), "invalid public key type"},
{ERR_REASON(CPK_R_INVALID_MAP_ALGOR), "invalid map algorithm"},
{ERR_REASON(CPK_R_PKEY_TYPE_NOT_MATCH), "public key type not match"},
{0, NULL}
};
#endif
void ERR_load_CPK_strings(void)
{
#ifndef OPENSSL_NO_ERR
if (ERR_func_error_string(CPK_str_functs[0].error) == NULL) {
ERR_load_strings(0, CPK_str_functs);
ERR_load_strings(0, CPK_str_reasons);
}
#endif
}

46
crypto/cpk/cpk_kap.c Normal file
View File

@@ -0,0 +1,46 @@
#include <openssl/evp.h>
#include <openssl/ecdh.h>
#include <openssl/objects.h>
#include "cpk.h"
int CPK_PUBLIC_PARAMS_compute_share_key(CPK_PUBLIC_PARAMS *params,
void *out, size_t outlen, const char *id, EVP_PKEY *priv_key,
void *(*kdf)(const void *in, size_t inlen, void *out, size_t *outlen))
{
int ret = 0;
EVP_PKEY *pub_key = NULL;
int pkey_type = OBJ_obj2nid(params->pkey_algor->algorithm);
OPENSSL_assert(kdf != NULL);
printf("%d\n", __LINE__);
if (EVP_PKEY_id(priv_key) != pkey_type) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY,
ERR_R_MALLOC_FAILURE); //FIXME: ERR_R_XXX
goto err;
}
if (!(pub_key = CPK_PUBLIC_PARAMS_extract_public_key(params, id))) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY,
ERR_R_MALLOC_FAILURE); //FIXME: ERR_R_XXX
goto err;
}
if (pkey_type == EVP_PKEY_EC) {
if (!ECDH_compute_key(out, outlen,
EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0(pub_key)),
(EC_KEY *)EVP_PKEY_get0(priv_key), kdf)) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY,
ERR_R_MALLOC_FAILURE); //FIXME: ERR_R_XXX
goto err;
}
} else if (pkey_type == EVP_PKEY_DH) {
// not supported yet
goto err;
}
ret = 1;
err:
return ret;
}

1097
crypto/cpk/cpk_lib.c Normal file

File diff suppressed because it is too large Load Diff

128
crypto/cpk/cpk_map.c Normal file
View File

@@ -0,0 +1,128 @@
#include <string.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include "cpk.h"
X509_ALGOR *CPK_MAP_new_default()
{
X509_ALGOR *algor = NULL;
const EVP_MD *md = EVP_sha1();
if (md != EVP_sha1() && md != EVP_sha384()) {
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, CPK_R_BAD_ARGUMENT);
goto end;
}
if (!(algor = X509_ALGOR_new())) {
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, ERR_R_X509_LIB);
goto end;
}
if (!X509_ALGOR_set0(algor, OBJ_nid2obj(EVP_MD_nid(md)),
V_ASN1_UNDEF, NULL)) {
X509_ALGOR_free(algor);
algor = NULL;
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, ERR_R_X509_LIB);
goto end;
}
end:
return algor;
}
int CPK_MAP_is_valid(const X509_ALGOR *algor)
{
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
switch (OBJ_obj2nid(algor->algorithm)) {
case NID_sha1:
case NID_sha384:
return 1;
}
return 0;
}
int CPK_MAP_num_subset(const X509_ALGOR *algor)
{
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
switch (OBJ_obj2nid(algor->algorithm)) {
case NID_sha1:
return 32;
case NID_sha384:
return 4096;
}
return -1;
}
int CPK_MAP_num_factors(const X509_ALGOR *algor)
{
return 1024;
}
int CPK_MAP_num_indexes(const X509_ALGOR *algor)
{
return 32;
}
int CPK_MAP_num_index(const X509_ALGOR *algor)
{
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
switch (OBJ_obj2nid(algor->algorithm)) {
case NID_sha1:
return 32;
case NID_sha384:
return 32;
}
return -1;
}
int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index)
{
int ret = 0;
const EVP_MD *md;
unsigned char dgst[EVP_MAX_MD_SIZE];
unsigned int dgstlen;
BIGNUM *bn = NULL;
int i, num_index, num_subset;
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
OPENSSL_assert(str);
OPENSSL_assert(strlen(str) > 0);
if (!CPK_MAP_is_valid(algor)) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, CPK_R_INVALID_MAP_ALGOR);
goto err;
}
if (!index) {
ret = CPK_MAP_num_index(algor);
goto err;
}
if (!(md = EVP_get_digestbyobj(algor->algorithm))) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_EVP_LIB);
goto err;
}
if (!EVP_Digest(str, strlen(str), dgst, &dgstlen, md, NULL)) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_EVP_LIB);
return 0;
}
if (!(bn = BN_new())) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_BN_LIB);
goto err;
}
if (!BN_bin2bn(dgst, dgstlen, bn)) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_BN_LIB);
goto err;
}
num_index = CPK_MAP_num_index(algor);
num_subset = CPK_MAP_num_subset(algor);
for (i = 0; i < num_index; i++) {
int r = BN_mod_word(bn, num_subset);
index[i] = num_subset * i + r;
}
ret = num_index;
err:
if (bn) BN_free(bn);
return ret;
}

20
crypto/cpk/cpk_prn.c Normal file
View File

@@ -0,0 +1,20 @@
#include <string.h>
#include <openssl/bio.h>
#include "cpk.h"
int CPK_MASTER_SECRET_print(BIO *out, CPK_MASTER_SECRET *master,
int indent, unsigned long flags)
{
BIO_printf(out, "%s() not implemented\n", __FUNCTION__);
return 1;
}
int CPK_PUBLIC_PARAMS_print(BIO *out, CPK_PUBLIC_PARAMS *params,
int indent, unsigned long flags)
{
BIO_printf(out, "%s() not implemented\n", __FUNCTION__);
return 1;
}

View File

@@ -12,10 +12,22 @@ extern "C" {
#endif
/*
KDFSet ALGORITHM ::= {
{ OID x9-63-kdf PARMS HashAlgorithm } |
{ OID nist-concatenation-kdf PARMS HashAlgorithm } |
{ OID tls-kdf PARMS HashAlgorithm } |
{ OID ikev2-kdf PARMS HashAlgorithm }
}
*/
typedef struct ecies_params_st {
int kdf_nid;
const EVP_MD *kdf_md;
const EVP_CIPHER *sym_cipher;
int mac_nid;
const EVP_MD *mac_md;
const EVP_CIPHER *mac_cipher;
} ECIES_PARAMS;
typedef struct ecies_ciphertext_value_st {
@@ -36,14 +48,6 @@ int ECIES_do_decrypt(const ECIES_CIPHERTEXT_VALUE *cv,
EC_KEY *pri_key);
/* ERR function (should in openssl/err.h) begin */
/*
#define ERR_LIB_ECIES 129
#define ERR_R_ECIES_LIB ERR_LIB_ECIES
#define ECIESerr(f,r) ERR_PUT_error(ERR_LIB_ECIES,(f),(r),__FILE__,__LINE__)
*/
void ERR_load_ECIES_strings(void);
/* Error codes for the ECIES functions. */

View File

@@ -95,6 +95,9 @@ secg-scheme 22 : hmac-full-ecies
secg-scheme 23 : hmac-half-ecies
secg-scheme 24 0 : cmac-aes128-ecies
secg-scheme 24 1 : cmac-aes192-ecies
FIXME: we can not get an EVP_algor object from these new NIDs
*/
typedef struct ecies_parameters_st {

View File

@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <openssl/ecdh.h>
@@ -16,7 +17,7 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
EC_KEY *ephem_key = NULL;
unsigned char *share = NULL;
unsigned char *enckey, *mackey, *p;
int sharelen, enckeylen, mackeylen, len;
int sharelen, enckeylen, mackeylen, maclen, len;
EVP_CIPHER_CTX cipher_ctx;
EVP_CIPHER_CTX_init(&cipher_ctx);
@@ -67,7 +68,29 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
if (param->sym_cipher)
enckeylen = EVP_CIPHER_key_length(param->sym_cipher);
else enckeylen = inlen;
mackeylen = EVP_MD_size(param->mac_md); //TODO: is this true for hmac-half-ecies?
switch (param->mac_nid) {
case NID_hmac_full_ecies:
maclen = EVP_MD_size(param->mac_md);
mackeylen = EVP_MD_size(param->mac_md);
break;
case NID_hmac_half_ecies:
maclen = EVP_MD_size(param->mac_md)/2;
mackeylen = EVP_MD_size(param->mac_md);
break;
case NID_cmac_aes128_ecies:
maclen = AES_BLOCK_SIZE;
mackeylen = 128/8;
break;
case NID_cmac_aes192_ecies:
maclen = AES_BLOCK_SIZE;
mackeylen = 192/8;
break;
default:
ECIESerr(ECIES_F_ECIES_DO_ENCRYPT, ERR_R_EC_LIB);
goto err;
}
sharelen = enckeylen + mackeylen;
if (!(share = OPENSSL_malloc(sharelen)))
@@ -138,12 +161,12 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
/*
* calculate mactag of ciphertext and encode
*/
cv->mactag->length = EVP_MD_size(param->mac_md);
cv->mactag->length = maclen;
if (!M_ASN1_OCTET_STRING_set(cv->mactag, NULL, EVP_MD_size(param->mac_md)))
if (!M_ASN1_OCTET_STRING_set(cv->mactag, NULL, cv->mactag->length))
{
ECIESerr(ECIES_F_ECIES_DO_ENCRYPT, ERR_R_MALLOC_FAILURE);
goto err;
goto err;
}
if (!HMAC(param->mac_md, mackey, mackeylen,
cv->ciphertext->data, (size_t)cv->ciphertext->length,

View File

@@ -14,7 +14,7 @@ static void *x963_kdf(const EVP_MD *md, const void *share, size_t sharelen,
void *key, size_t keylen)
{
EVP_MD_CTX ctx;
unsigned int counter = 1;
unsigned int counter = 1; //FIXME: uint32_t
unsigned int counter_be;
unsigned char dgst[EVP_MAX_MD_SIZE];
unsigned int dgstlen;

View File

@@ -291,7 +291,7 @@ typedef struct err_state_st {
# define ERR_R_ECDH_LIB ERR_LIB_ECDH/* 43 */
# define ERR_R_STORE_LIB ERR_LIB_STORE/* 44 */
# define ERR_R_TS_LIB ERR_LIB_TS/* 45 */
# define ERR_R_EICES_LIB ERR_LIB_ECIES/* 50 */
# define ERR_R_ECIES_LIB ERR_LIB_ECIES/* 50 */
# define ERR_R_NESTED_ASN1_ERROR 58
# define ERR_R_BAD_ASN1_OBJECT_HEADER 59

View File

@@ -30,7 +30,7 @@ LIBSRC= encode.c digest.c evp_enc.c evp_key.c evp_acnf.c evp_cnf.c \
evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c \
e_old.c pmeth_lib.c pmeth_fn.c pmeth_gn.c m_sigver.c \
e_aes_cbc_hmac_sha1.c e_aes_cbc_hmac_sha256.c e_rc4_hmac_md5.c \
m_sm3.c e_sms4.c
m_sm3.c e_sms4.c e_zuc.c
LIBOBJ= encode.o digest.o evp_enc.o evp_key.o evp_acnf.o evp_cnf.o \
e_des.o e_bf.o e_idea.o e_des3.o e_camellia.o\
@@ -44,7 +44,7 @@ LIBOBJ= encode.o digest.o evp_enc.o evp_key.o evp_acnf.o evp_cnf.o \
evp_pkey.o evp_pbe.o p5_crpt.o p5_crpt2.o \
e_old.o pmeth_lib.o pmeth_fn.o pmeth_gn.o m_sigver.o \
e_aes_cbc_hmac_sha1.o e_aes_cbc_hmac_sha256.o e_rc4_hmac_md5.o \
m_sm3.o e_sms4.o
m_sm3.o e_sms4.o e_zuc.o
SRC= $(LIBSRC)
@@ -354,6 +354,16 @@ e_sms4.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
e_sms4.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
e_sms4.o: ../../include/openssl/symhacks.h ../cryptlib.h e_sms4.c evp_locl.h
e_zuc.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h
e_zuc.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
e_zuc.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
#e_zuc.o: ../../include/openssl/evp.h ../../include/openssl/zuc.h
e_zuc.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
e_zuc.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
e_zuc.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
e_zuc.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
e_zuc.o: ../../include/openssl/symhacks.h ../cryptlib.h e_zuc.c evp_locl.h
e_seed.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
e_seed.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
e_seed.o: ../../include/openssl/err.h ../../include/openssl/evp.h

View File

@@ -172,6 +172,10 @@ void OpenSSL_add_all_ciphers(void)
EVP_add_cipher_alias(SN_sms4_cbc,"sms4");
#endif
#ifndef OPENSSL_NO_ZUC
EVP_add_cipher(EVP_zuc());
#endif
#ifndef OPENSSL_NO_AES
EVP_add_cipher(EVP_aes_128_ecb());
EVP_add_cipher(EVP_aes_128_cbc());

48
crypto/evp/e_zuc.c Normal file
View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_ZUC
#include <openssl/evp.h>
#include "evp_locl.h"
#include <openssl/objects.h>
#include <openssl/zuc.h>
static int zuc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
//ZUC_set_key((ZUC_KEY *)&ctx->cipher_data, EVP_CIPHER_CTX_key_length(ctx), key);
return 1;
}
static int zuc_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inlen)
{
//ZUC_encrypt((ZUC_KEY *)&ctx->cipher_data, inlen, in, out);
return 1;
}
static const EVP_CIPHER zuc_cipher = {
NID_zuc, /* nid */
4, /* block_size */
16, /* key_len */
16, /* iv_len */
0, /* flags */
zuc_init, /* init() */
zuc_do_cipher, /* do_cipher() */
NULL, /* cleanup() */
sizeof(ZUC_KEY), /* ctx_size */
NULL, /* set_asn1_parameters() */
NULL, /* get_asn1_parameters() */
NULL, /* ctrl() */
NULL /* app_data */
};
const EVP_CIPHER *EVP_zuc(void)
{
return &zuc_cipher;
}
#endif

View File

@@ -838,6 +838,9 @@ const EVP_CIPHER *EVP_sms4_ofb128(void);
#define EVP_sm4_cfb128 EVP_sms4_cfb128
#define EVP_sm4_ofb128 EVP_sms4_ofb128
#endif
#ifndef OPENSSL_NO_ZUC
const EVP_CIPHER *EVP_zuc(void);
#endif
# ifndef OPENSSL_NO_AES
const EVP_CIPHER *EVP_aes_128_ecb(void);
const EVP_CIPHER *EVP_aes_128_cbc(void);

View File

@@ -62,12 +62,12 @@
* [including the GNU Public Licence.]
*/
#define NUM_NID 1000
#define NUM_SN 985
#define NUM_LN 985
#define NUM_OBJ 924
#define NUM_NID 1001
#define NUM_SN 986
#define NUM_LN 986
#define NUM_OBJ 925
static const unsigned char lvalues[6483]={
static const unsigned char lvalues[6491]={
0x2A,0x86,0x48,0x86,0xF7,0x0D, /* [ 0] OBJ_rsadsi */
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01, /* [ 6] OBJ_pkcs */
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x02,0x02, /* [ 13] OBJ_md2 */
@@ -986,6 +986,7 @@ static const unsigned char lvalues[6483]={
0x2B,0x81,0x04,0x01,0x17, /* [6465] OBJ_hmac_half_ecies */
0x2B,0x81,0x04,0x01,0x18,0x00, /* [6470] OBJ_cmac_aes128_ecies */
0x2B,0x81,0x04,0x01,0x18,0x01, /* [6476] OBJ_cmac_aes192_ecies */
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x81,0x48, /* [6482] OBJ_zuc */
};
static const ASN1_OBJECT nid_objs[NUM_NID]={
@@ -2607,6 +2608,7 @@ static const ASN1_OBJECT nid_objs[NUM_NID]={
&(lvalues[6470]),0},
{"cmac-aes192-ecies","cmac-aes192-ecies",NID_cmac_aes192_ecies,6,
&(lvalues[6476]),0},
{"ZUC","zuc",NID_zuc,8,&(lvalues[6482]),0},
};
static const unsigned int sn_objs[NUM_SN]={
@@ -2813,6 +2815,7 @@ static const unsigned int sn_objs[NUM_SN]={
184, /* "X9-57" */
185, /* "X9cm" */
125, /* "ZLIB" */
1000, /* "ZUC" */
478, /* "aRecord" */
289, /* "aaControls" */
287, /* "ac-auditEntity" */
@@ -4583,6 +4586,7 @@ static const unsigned int ln_objs[NUM_LN]={
985, /* "x9-63-kdf" */
989, /* "xor-in-ecies" */
125, /* "zlib compression" */
1000, /* "zuc" */
};
static const unsigned int obj_objs[NUM_OBJ]={
@@ -5021,6 +5025,7 @@ static const unsigned int obj_objs[NUM_OBJ]={
978, /* OBJ_sms4_cbc 1 2 156 10197 1 104 2 */
982, /* OBJ_sms4_cfb128 1 2 156 10197 1 104 3 */
981, /* OBJ_sms4_ofb128 1 2 156 10197 1 104 4 */
1000, /* OBJ_zuc 1 2 156 10197 1 200 */
969, /* OBJ_sm2 1 2 156 10197 1 301 */
962, /* OBJ_sm3 1 2 156 10197 1 401 */
973, /* OBJ_sm2sign_with_sm3 1 2 156 10197 1 501 */

View File

@@ -4340,3 +4340,8 @@
#define LN_sms4_ofb128 "sms4-ofb"
#define NID_sms4_ofb128 981
#define OBJ_sms4_ofb128 OBJ_sm,104L,4L
#define SN_zuc "ZUC"
#define LN_zuc "zuc"
#define NID_zuc 1000
#define OBJ_zuc OBJ_sm,200L

View File

@@ -997,3 +997,4 @@ hmac_full_ecies 996
hmac_half_ecies 997
cmac_aes128_ecies 998
cmac_aes192_ecies 999
zuc 1000

View File

@@ -1393,3 +1393,6 @@ sm 104 2 : SMS4-CBC : sms4-cbc
sm 104 3 : SMS4-CFB : sms4-cfb
!Cname sms4-ofb128
sm 104 4 : SMS4-OFB : sms4-ofb
sm 200 : ZUC : zuc

79
crypto/sm2/Makefile Normal file
View File

@@ -0,0 +1,79 @@
#
# crypto/ecies/Makefile
#
DIR= sm2
TOP= ../..
CC= cc
INCLUDES= -I.. -I$(TOP) -I../../include
CFLAG=-g -Wall
MAKEFILE= Makefile
AR= ar r
CFLAGS= $(INCLUDES) $(CFLAG)
GENERAL=Makefile
TEST=sm2test.c
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC= sm2_dgst.c sm2_enc.c
LIBOBJ= sm2_dgst.o sm2_enc.o
SRC= $(LIBSRC)
EXHEADER= sm2_enc.h
HEADER= $(EXHEADER)
ALL= $(GENERAL) $(SRC) $(HEADER)
top:
(cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
all: lib
lib: $(LIBOBJ)
$(AR) $(LIB) $(LIBOBJ)
$(RANLIB) $(LIB) || echo Never mind.
@touch lib
files:
$(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
links:
@$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
@$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
@$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
install:
@[ -n "$(INSTALLTOP)" ] # should be set by top Makefile...
@headerlist="$(EXHEADER)"; for i in $$headerlist; \
do \
(cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
done;
tags:
ctags $(SRC)
tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
dclean:
$(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
mv -f Makefile.new $(MAKEFILE)
clean:
rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
# DO NOT DELETE THIS LINE -- make depend depends on it.

170
crypto/sm2/sm2_dgst.c Normal file
View File

@@ -0,0 +1,170 @@
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#define EC_MAX_NBYTES ((OPENSSL_ECC_MAX_FIELD_BITS + 7)/8)
/*
* pkdata = a || b || G.x || G.y || P.x || P.y
*/
static int sm2_get_public_key_data(unsigned char *buf, EC_KEY *ec_key)
{
int ret = -1;
int nbytes = (EC_GROUP_get_degree(ec_group) + 7) / 8;
const EC_GROUP *ec_group = EC_KEY_get0_group(ec_key);
const EC_POINT *point;
unsigned char oct[EC_MAX_NBYTES * 2 + 1];
BN_CTX *ctx = NULL;
BIGNUM *p = NULL;
BIGNUM *x = NULL;
BIGNUM *y = NULL;
OPENSSL_assert(ec_key);
OPENSSL_assert(nbytes == 256/8);
if (!buf) {
return (nbytes * 6);
}
bzero(buf, nbytes * 6);
ctx = BN_CTX_new();
p = BN_new();
x = BN_new();
y = BN_new();
if (!ctx || !p || !x || !y) {
goto err;
}
/* get curve coefficients a, b */
if (!EC_GROUP_get_curve_GFp(ec_group, p, x, y, ctx)) {
goto err;
}
buf += nbytes;
if (!BN_bn2bin(x, buf - BN_num_bytes(x))) {
goto err;
}
buf += nbytes;
if (!BN_bn2bin(y, buf - BN_num_bytes(y))) {
goto err;
}
/* get curve generator coordinates */
if (!(point = EC_GROUP_get0_generator(ec_group))) {
goto err;
}
if (!(len = EC_POINT_point2oct(ec_group, point,
POINT_CONVERSION_UNCOMPRESSED, oct, sizeof(oct), bn_ctx))) {
goto err;
}
OPENSSL_assert(len == 32 * 2 + 1);
memcpy(buf, oct + 1, len - 1);
buf += len - 1;
/* get pub_key coorindates */
if (!(point = EC_KEY_get0_public_key(ec_key))) {
goto err;
}
if (!(len = EC_POINT_point2oct(ec_group, point,
POINT_CONVERSION_UNCOMPRESSED, oct, sizeof(oct), bn_ctx))) {
goto err;
}
OPENSSL_assert(len == 32 * 2 + 1);
memcpy(buf, oct + 1, len - 1);
buf += len - 1;
ret = (nbytes * 6);
err:
if (ctx) BN_CTX_free(ctx);
if (p) BN_free(p);
if (x) BN_free(x);
if (y) BN_free(y);
return ret;
}
int SM2_compute_za(unsigned char *za, const EVP_MD *md,
const void *id, size_t idlen, EC_KEY *ec_key)
{
int ret = 0;
EVP_MD_CTX *ctx = NULL;
unsigned char pkdata[EC_MAX_NBYTES * 6];
uint16_t idbits;
int len;
idbits = cpu_to_be16(idlen * 8);
if ((pkdatalen = sm2_get_public_key_data(pkdata, ec_key)) < 0) {
goto err;
}
if (!(ctx = EVP_MD_CTX_create())) {
goto err;
}
if (!EVP_DigestInit_ex(ctx, md, NULL)) {
goto end;
}
if (!EVP_DigestUpdate(ctx, &idbits, sizeof(idbits))) {
goto end;
}
if (!EVP_DigestUpdate(ctx, id, idlen)) {
goto end;
}
if (!EVP_DigestUpdate(ctx, pkdata, pkdatalen)) {
goto end;
}
if (!EVP_DigestFinal(ctx, za, &zalen)) {
}
ret = SM3_DIGEST_LENGTH;
err:
if (ctx) EVP_MD_CTX_destroy(ctx);
return ret;
}
int SM2_compute_digest(unsigned char *dgst, unsigned int *dgstlen,
const EVP_MD *za_md, const void *id, size_t idlen, EC_KEY *ec_key,
const EVP_MD *msg_md, const void *msg, size_t msglen)
{
int ret = 0;
unsigned char za[EVP_MAX_MD_SIZE];
int zalen;
EVP_MD_CTX *ctx = NULL;
/* compute Za */
if (idlen > SM2_MAX_ID_LENGTH) {
goto err;
}
if ((zalen = SM2_compute_za(za, za_md, id, idlen, ec_key)) < 0) {
goto err;
}
/* compute digest */
if (!(ctx = EVP_MD_CTX_create())) {
goto err;
}
if (!EVP_DigestInit_ex(ctx, msg_md, NULL)) {
goto err;
}
if (!EVP_DigestUpdate(ctx, za, zalen)) {
goto err;
}
if (!EVP_DigestUpdate(ctx, msg, msglen)) {
goto err;
}
if (!EVP_DigestFinal_ex(ctx, dgst, dgstlen)) {
goto err;
}
ret = 1;
err:
if (ctx) EVP_MD_CTX_destroy(ctx);
return ret;
}

284
crypto/sm2/sm2_enc.c Normal file
View File

@@ -0,0 +1,284 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <strings.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/rand.h>
#include <oepnssl/kdf.h>
#include "sm2_enc.h"
void SM2_CIPEHRTEXT_VALUE_free(SM2_CIPHERTEXT_VALUE *cv)
{
if (cv->ephem_point) EC_POINT_free(cv->ephem_point);
if (cv->ciphertext) OPENSSL_free(cv->ciphertext);
bzero(cv, sizeof(SM2_CIPHERTEXT_VALUE));
OPENSSL_free(cv);
}
SM2_CIPHERTEXT_VALUE *SM2_do_encrypt(
const EVP_MD *kdf_md, const EVP_MD *mac_md,
const void *in, size_t inlen, const EC_KEY *ec_key);
{
int ok = 0;
SM2_CIPHERTEXT_VALUE *cv = NULL;
const EC_GROUP *ec_group = EC_KEY_get0_group(ec_key);
const EC_POINT *pub_key = EC_KEY_get0_public_key(ec_key);
KDF_FUNC kdf = KDF_get_x9_63(kdf_md);
EC_POINT *point = NULL;
BIGNUM *n = NULL;
BIGNUM *h = NULL;
BIGNUM *k = NULL;
BN_CTX *bn_ctx = NULL;
EVP_MD_CTX *md_ctx = NULL;
unsigned char buf[(OPENSSL_ECC_MAX_FIELD_BITS + 7)/4 + 1];
int nbytes;
int i;
if (!ec_group || !pub_key) {
goto err;
}
if (!kdf) {
goto err;
}
/* init ciphertext_value */
if (!(cv = OPENSSL_malloc(sizeof(SM2_CIPHERTEXT_VALUE)))) {
goto err;
}
bzero(cv, sizeof(SM2_CIPHERTEXT_VALUE));
cv->ephem_point = EC_POINT_new(ec_group);
cv->ciphertext = OPENSSL_malloc(inlen);
cv->ciphertext_size = inlen;
if (!cv->ephem_point || !cv->ciphertext) {
goto err;
}
point = EC_POINT_new(ec_group);
n = BN_new();
h = BN_new();
k = BN_new();
bn_ctx = BN_CTX_new();
md_ctx = EVP_MD_CTX_create();
if (!point || !n || !h || !k || !bn_ctx || !md_ctx) {
goto err;
}
/* init ec domain parameters */
if (!EC_GROUP_get_order(ec_group, n, bn_ctx)) {
goto err;
}
if (!EC_GROUP_get_cofactor(ec_group, h, bn_ctx)) {
goto err;
}
nbytes = (EC_GROPU_get_degree(ec_group) + 7) / 8;
OPENSSL_assert(nbytes == BN_num_bytes(n));
/* check sm2 curve and md is 256 bits */
OPENSSL_assert(nbytes == 32);
OPENSSL_assert(EVP_MD_size(kdf_md) == 32);
OPENSSL_assert(EVP_MD_size(mac_md) == 32);
do
{
/* A1: rand k in [1, n-1] */
do {
BN_rand_range(k, n);
} while (BN_is_zero(k));
/* A2: C1 = [k]G = (x1, y1) */
if (!EC_POINT_mul(ec_group, cv->ephem_point, k, NULL, NULL, bn_ctx)) {
goto err;
}
/* A3: check [h]P_B != O */
if (!EC_POINT_mul(ec_group, point, NULL, pub_key, h, bn_ctx)) {
goto err;
}
if (EC_POINT_is_at_infinity(ec_group, point)) {
goto err;
}
/* A4: compute ECDH [k]P_B = (x2, y2) */
if (!EC_POINT_mul(ec_group, point, NULL, pub_key, k, bn_ctx)) {
goto err;
}
if (!(len = EC_POINT_point2oct(ec_group, point,
POINT_CONVERSION_UNCOMPRESSED, buf, sizeof(buf), bn_ctx))) {
goto err;
}
OPENSSL_assert(len == nbytes * 2 + 1);
/* A5: t = KDF(x2 || y2, klen) */
kdf(buf - 1, len - 1, cv->ciphertext, &cv->ciphertext_size);
for (i = 0; i < cv->ciphertext_size; i++) {
if (cv->ciphertext[i]) {
break;
}
}
if (i == cv->ciphertext_size) {
continue;
}
break;
} while (1);
/* A6: C2 = M xor t */
for (i = 0; i < inlen; i++) {
cv->ciphertext[i] ^= in[i];
}
/* A7: C3 = Hash(x2 || M || y2) */
if (!EVP_DigestInit_ex(md_ctx, mac_md, NULL)) {
goto err;
}
if (!EVP_DigestUpdate(md_ctx, buf + 1, nbytes)) {
goto err;
}
if (!EVP_DigestUpdate(md_ctx, in, inlen)) {
goto err;
}
if (!EVP_DigestUpdate(md_ctx, buf + 1 + nbytes, nbytes)) {
goto err;
}
if (!EVP_DigestFinal_ex(md_ctx, cv->mactag, &cv->mactag_size)) {
goto err;
}
ok = 1;
err:
if (!ok && cv) {
SM2_CIPHERTEXT_VALUE_free(cv);
cv = NULL;
}
if (n) BN_free(n);
if (h) BN_free(h);
if (k) BN_free(k);
if (bn_ctx) BN_CTX_free(bn_ctx);
if (md_ctx) EVP_MD_CTX_destroy(md_ctx);
return cv;
}
int SM2_do_decrypt(const SM2_CIPHERTEXT_VALUE *cv,
const EVP_MD *kdf_md, const EVP_MD *mac_md,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
int ret = 0
const EC_GROUP *ec_group = EC_KEY_get0_group(ec_key);
const BIGNUM *pri_key = EC_KEY_get0_private_key(ec_key);
KDF_FUNC kdf = KDF_get_x9_63(kdf_md);
EC_POINT *point = NULL;
BIGNUM *n = NULL;
BIGNUM *h = NULL;
BN_CTX *bn_ctx = NULL;
EVP_MD_CTX *md_ctx = NULL;
unsigned char buf[(OPENSSL_ECC_MAX_FIELD_BITS + 7)/4 + 1];
unsigned char mac[EVP_MAX_MD_SIZE];
int nbytes;
int i;
if (!ec_group || !pub_key) {
goto err;
}
if (!kdf) {
goto err;
}
if (!out) {
*outlen = cv->ciphertext_size;
return 1;
}
if (*outlen < cv->ciphertext_size) {
goto err;
}
/* init vars */
point = EC_POINT_new(ec_group);
n = BN_new();
h = BN_new();
bn_ctx = BN_CTX_new();
md_ctx = EVP_MD_CTX_create();
if (!point || !n || !h || !bn_ctx || !md_ctx) {
goto err;
}
/* init ec domain parameters */
if (!EC_GROUP_get_order(ec_group, n, bn_ctx)) {
goto err;
}
if (!EC_GROUP_get_cofactor(ec_group, h, bn_ctx)) {
goto err;
}
nbytes = (EC_GROPU_get_degree(ec_group) + 7) / 8;
OPENSSL_assert(nbytes == BN_num_bytes(n));
/* check sm2 curve and md is 256 bits */
OPENSSL_assert(nbytes == 32);
OPENSSL_assert(EVP_MD_size(kdf_md) == 32);
OPENSSL_assert(EVP_MD_size(mac_md) == 32);
/* B2: check [h]C1 != O */
if (!EC_POINT_mul(ec_group, point, NULL, cv->ephem_point, h, bn_ctx)) {
goto err;
}
if (EC_POINT_is_at_infinity(ec_group, point)) {
goto err;
}
/* B3: compute ECDH [d]C1 = (x2, y2) */
if (!EC_POINT_mul(ec_group, point, NULL, cv->ephem_point, pri_key, bn_ctx)) {
goto err;
}
if (!(len = EC_POINT_point2oct(ec_group, point,
POINT_CONVERSION_UNCOMPRESSED, buf, sizeof(buf), bn_ctx))) {
goto err;
}
/* B4: compute t = KDF(x2 || y2, clen) */
kdf(buf - 1, len - 1, out, outlen);
/* B5: compute M = C2 xor t */
for (i = 0; i < cv->ciphertext_size; i++) {
out[i] ^= cv->ciphertext[i];
}
/* B6: check Hash(x2 || M || y2) == C3 */
if (!EVP_DigestInit_ex(md_ctx, mac_md, NULL)) {
goto err;
}
if (!EVP_DigestUpdate(md_ctx, buf + 1, nbytes)) {
goto err;
}
if (!EVP_DigestUpdate(md_ctx, out, *outlen)) {
goto err;
}
if (!EVP_DigestUpdate(md_ctx, buf + 1 + nbytes, nbytes)) {
goto err;
}
if (!EVP_DigestFinal_ex(md_ctx, mac, &maclen)) {
goto err;
}
if (cv->mactag_size != maclen || memcmp(cv->mactag, mac, maclen)) {
goto err;
}
ret = 1;
err:
if (point) EC_POINT_free(point);
if (n) BN_free(n);
if (h) BN_free(h);
if (bn_ctx) BN_CTX_free(bn_ctx);
if (md_ctx) EVP_MD_CTX_destroy(md_ctx);
return ret;
}

59
crypto/sm2/sm2_enc.h Normal file
View File

@@ -0,0 +1,59 @@
#ifndef HEADER_SM2_ENC_H
#define HEADER_SM2_ENC_H
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/asn1.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct sm2_ciphertext_value_st {
EC_POINT *ephem_point;
unsigned char *ciphertext;
size_t ciphertext_size;
unsigned char mactag[EVP_MAX_MD_SIZE];
size_t mactag_size;
} SM2_CIPHERTEXT_VALUE;
SM2_CIPHERTEXT_VALUE *SM2_do_encrypt(
const EVP_MD *kdf_md, const EVP_MD *mac_md,
const void *in, size_t inlen, const EC_KEY *pub_key);
int SM2_do_decrypt(const SM2_CIPHERTEXT_VALUE *cv,
const EVP_MD *kdf_md, const EVP_MD *mac_md,
unsigned char *out, size_t *outlen, EC_KEY *pri_key);
void SM2_CIPHERTEXT_VALUE_free(SM2_CIPHERTEXT_VALUE *cv);
void ERR_load_SM2_strings(void);
/* Error codes for the ECIES functions. */
/* Function codes. */
#define SM2_F_SM2_DO_ENCRYPT 100
#define SM2_F_SM2_DO_DECRYPT 101
#define SM2_F_SM2_CIPHERTEXT_VALUE_FREE 102
/* Reason codes. */
#define SM2_R_BAD_DATA 100
#define SM2_R_UNKNOWN_CIPHER_TYPE 101
#define SM2_R_ENCRYPT_FAILED 102
#define SM2_R_DECRYPT_FAILED 103
#define SM2_R_UNKNOWN_MAC_TYPE 104
#define SM2_R_GEN_MAC_FAILED 105
#define SM2_R_VERIFY_MAC_FAILED 106
#define SM2_R_ECDH_FAILED 107
#define SM2_R_BUFFER_TOO_SMALL 108
#ifdef __cplusplus
}
#endif
#endif

114
crypto/sm2/sm2test.c Normal file
View File

@@ -0,0 +1,114 @@
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include "sm2.h"
#include "sm3.h"
void SM2PKE_test3()
{
/* test3 params */
const char *p = "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3";
const char *a = "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498";
const char *b = "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A";
const char *xG = "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D";
const char *yG = "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2";
const char *n = "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7";
const char *dB = "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0";
const char *xB = "435B39CCA8F3B508C1488AFC67BE491A0F7BA07E581A0E4849A5CF70628A7E0A";
const char *yB = "75DDBA78F15FEECB4C7895E2C1CDF5FE01DEBB2CDBADF45399CCF77BBA076A42";
BIGNUM *bn_p = BN_new();
BN_hex2bn(&bn_p, p);
BIGNUM *bn_a = BN_new();
BN_hex2bn(&bn_a, a);
BIGNUM *bn_b = BN_new();
BN_hex2bn(&bn_b, b);
BIGNUM *bn_xG = BN_new();
BN_hex2bn(&bn_xG, xG);
BIGNUM *bn_yG = BN_new();
BN_hex2bn(&bn_yG, yG);
BIGNUM *bn_n = BN_new();
BN_hex2bn(&bn_n, n);
BIGNUM *bn_dB = BN_new();
BN_hex2bn(&bn_dB, dB);
BIGNUM *bn_xB = BN_new();
BN_hex2bn(&bn_xB, xB);
BIGNUM *bn_yB = BN_new();
BN_hex2bn(&bn_yB, yB);
BN_CTX *bn_ctx = BN_CTX_new();
EC_GROUP *ec_group = EC_GROUP_new(EC_GFp_mont_method());
EC_GROUP_set_curve_GFp(ec_group, bn_p, bn_a, bn_b, bn_ctx);
EC_POINT *G = EC_POINT_new(ec_group);
EC_POINT_set_affine_coordinates_GFp(ec_group, G, bn_xG, bn_yG, bn_ctx);
BIGNUM *bn_h = BN_new(); /* cofactor h = #E(Fp) / n */
BN_div(bn_h, NULL, bn_p, bn_n, bn_ctx);
EC_GROUP_set_generator(ec_group, G, bn_n, bn_h);
EC_POINT *PB = EC_POINT_new(ec_group);
EC_POINT_set_affine_coordinates_GFp(ec_group, PB, bn_xB, bn_yB, bn_ctx);
EC_KEY *ec_key_B = EC_KEY_new();
EC_KEY_set_group(ec_key_B, ec_group);
EC_KEY_set_private_key(ec_key_B, bn_dB);
EC_KEY_set_public_key(ec_key_B, PB);
BN_free(bn_p);
BN_free(bn_a);
BN_free(bn_b);
BN_free(bn_n);
BN_free(bn_xG);
BN_free(bn_yG);
BN_free(bn_dB);
BN_free(bn_xB);
BN_free(bn_yB);
BN_free(bn_h);
BN_CTX_free(bn_ctx);
EC_POINT_free(G);
EC_POINT_free(PB);
EC_GROUP_free(ec_group);
char *M = "encryption standard";
char *ctest = "04245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F6252"
"E776CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84400F01"
"B8650053A89B41C418B0C3AAD00D886C002864679C3D7360C30156FAB7C80A02"
"76712DA9D8094A634B766D3A285E07480653426D";
BIGNUM *ct = BN_new();
BN_hex2bn(&ct, ctest);
unsigned char ct2bin[116];
BN_bn2bin(ct, ct2bin);
BN_free(ct);
int mlen = strlen(M);
int c1len = PRIME_SIZE / 8 * 2 + 1;
int clen = c1len + mlen + HASH_V / 8;
unsigned char *C = malloc(sizeof(unsigned char) * clen);
sm2_pke_encrypt(C, M, mlen, ec_key_B);
if (memcmp(C, ct2bin, clen) == 0)
printf("sm2_pke_encrypt passed.\n");
else
printf("sm2_pke_encrypt failed.\n");
free(C);
int m1len = clen - c1len - HASH_V / 8;
unsigned char *M1bin = malloc(sizeof(unsigned char) * m1len);
sm2_pke_decrypt((char *)ct2bin, M1bin, m1len, ec_key_B);
if (memcmp(M1bin, M, m1len) == 0)
printf("sm2_pke_decrypt passed.\n");
else
printf("sm2_pke_decrypt failed.\n");
free(M1bin);
EC_KEY_free(ec_key_B);
}
int main()
{
SM2PKE_test3();
return 0;
}

View File

@@ -1,5 +1,5 @@
#ifndef LIBSM_SMS4_H
#define LIBSM_SMS4_H
#ifndef HEADER_SMS4_H
#define HEADER_SMS4_H
#define SMS4_KEY_LENGTH 16
#define SMS4_BLOCK_SIZE 16
@@ -9,6 +9,8 @@
#include <stdint.h>
#include <string.h>
#include "openssl/modes.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -20,18 +22,21 @@ typedef struct {
void sms4_set_encrypt_key(sms4_key_t *key, const unsigned char *user_key);
void sms4_set_decrypt_key(sms4_key_t *key, const unsigned char *user_key);
void sms4_encrypt(const unsigned char *in, unsigned char *out, sms4_key_t *key);
void sms4_cbc_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const sms4_key_t *key,
unsigned char *ivec, int encrypt);
void sms4_ecb_encrypt(const unsigned char *in, unsigned char *out, const sms4_key_t *key, int encrypt);
void sms4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len,
const sms4_key_t *key, unsigned char *ivec, int encrypt);
void sms4_cfb128_encrypt(const unsigned char *in, unsigned char *out,
size_t length, const sms4_key_t *key,
size_t length, const sms4_key_t *key,
unsigned char *ivec, int *num, int encrypt);
void sms4_ofb128_encrypt(const unsigned char *in, unsigned char *out,
size_t length, const sms4_key_t *key,
unsigned char ivec[SMS4_BLOCK_SIZE],
unsigned int *num);
void sms4_ecb_encrypt(const unsigned char *in, unsigned char *out, const sms4_key_t *key, int encrypt);
#define sms4_decrypt(in,out,key) sms4_encrypt(in,out,key)
#ifdef __cplusplus
}
#endif

View File

@@ -5,7 +5,8 @@
void sms4_cbc_encrypt(const unsigned char *in, unsigned char *out,
size_t len, const sms4_key_t *key,
unsigned char *ivec, int encrypt) {
unsigned char *ivec, int encrypt)
{
if(encrypt)
CRYPTO_cbc128_encrypt(in,out,len,key,ivec,(block128_f)sms4_encrypt);
else

View File

@@ -12,4 +12,4 @@ void sms4_ecb_encrypt(const unsigned char *in, unsigned char *out, const sms4_ke
sms4_decrypt(in, out, key);
}
//sms4_decrypt = sms4_encrypt, but the key is in reverse order
//sms4_decrypt = sms4_encrypt, but the key is in reverse order

94
crypto/zuc/Makefile Normal file
View File

@@ -0,0 +1,94 @@
#
# OpenSSL/crypto/zuc/Makefile
#
DIR=zuc
TOP=../..
CC=cc
CPP=$(CC) -E
INCLUDES=
CFLAG=-g
AR=ar r
ZUC_ENC=zuc.o
CFLAGS= $(INCLUDES) $(CFLAG)
ASFLAGS= $(INCLUDES) $(ASFLAG)
AFLAGS= $(ASFLAGS)
GENERAL=Makefile
TEST=
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC=zuc.c
LIBOBJ=$(ZUC_ENC)
SRC= $(LIBSRC)
EXHEADER=zuc.h
HEADER=$(EXHEADER)
ALL= $(GENERAL) $(SRC) $(HEADER)
top:
(cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
all: lib
lib: $(LIBOBJ)
$(AR) $(LIB) $(LIBOBJ)
$(RANLIB) $(LIB) || echo Never mind.
@touch lib
files:
$(PERL) $(TOP)/util/files.pl "RC4_ENC=$(RC4_ENC)" Makefile >> $(TOP)/MINFO
links:
@$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
@$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
@$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
install:
@[ -n "$(INSTALLTOP)" ] # should be set by top Makefile...
@headerlist="$(EXHEADER)"; for i in $$headerlist ; \
do \
(cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
done;
tags:
ctags $(SRC)
tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
dclean:
$(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
mv -f Makefile.new $(MAKEFILE)
clean:
rm -f *.s *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
# DO NOT DELETE THIS LINE -- make depend depends on it.
zuc.o: ../../e_os.h ../../include/openssl/bio.h
zuc.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
zuc.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
zuc.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
zuc.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
zuc.o: ../../include/openssl/rc4.h ../../include/openssl/safestack.h
zuc.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
zuc.o: ../cryptlib.h zuc.c zuc.h

221
crypto/zuc/zuc.c Executable file
View File

@@ -0,0 +1,221 @@
#include "zuc.h"
static uint8_t S0[256] = {
0x3e,0x72,0x5b,0x47,0xca,0xe0,0x00,0x33,0x04,0xd1,0x54,0x98,0x09,0xb9,0x6d,0xcb,
0x7b,0x1b,0xf9,0x32,0xaf,0x9d,0x6a,0xa5,0xb8,0x2d,0xfc,0x1d,0x08,0x53,0x03,0x90,
0x4d,0x4e,0x84,0x99,0xe4,0xce,0xd9,0x91,0xdd,0xb6,0x85,0x48,0x8b,0x29,0x6e,0xac,
0xcd,0xc1,0xf8,0x1e,0x73,0x43,0x69,0xc6,0xb5,0xbd,0xfd,0x39,0x63,0x20,0xd4,0x38,
0x76,0x7d,0xb2,0xa7,0xcf,0xed,0x57,0xc5,0xf3,0x2c,0xbb,0x14,0x21,0x06,0x55,0x9b,
0xe3,0xef,0x5e,0x31,0x4f,0x7f,0x5a,0xa4,0x0d,0x82,0x51,0x49,0x5f,0xba,0x58,0x1c,
0x4a,0x16,0xd5,0x17,0xa8,0x92,0x24,0x1f,0x8c,0xff,0xd8,0xae,0x2e,0x01,0xd3,0xad,
0x3b,0x4b,0xda,0x46,0xeb,0xc9,0xde,0x9a,0x8f,0x87,0xd7,0x3a,0x80,0x6f,0x2f,0xc8,
0xb1,0xb4,0x37,0xf7,0x0a,0x22,0x13,0x28,0x7c,0xcc,0x3c,0x89,0xc7,0xc3,0x96,0x56,
0x07,0xbf,0x7e,0xf0,0x0b,0x2b,0x97,0x52,0x35,0x41,0x79,0x61,0xa6,0x4c,0x10,0xfe,
0xbc,0x26,0x95,0x88,0x8a,0xb0,0xa3,0xfb,0xc0,0x18,0x94,0xf2,0xe1,0xe5,0xe9,0x5d,
0xd0,0xdc,0x11,0x66,0x64,0x5c,0xec,0x59,0x42,0x75,0x12,0xf5,0x74,0x9c,0xaa,0x23,
0x0e,0x86,0xab,0xbe,0x2a,0x02,0xe7,0x67,0xe6,0x44,0xa2,0x6c,0xc2,0x93,0x9f,0xf1,
0xf6,0xfa,0x36,0xd2,0x50,0x68,0x9e,0x62,0x71,0x15,0x3d,0xd6,0x40,0xc4,0xe2,0x0f,
0x8e,0x83,0x77,0x6b,0x25,0x05,0x3f,0x0c,0x30,0xea,0x70,0xb7,0xa1,0xe8,0xa9,0x65,
0x8d,0x27,0x1a,0xdb,0x81,0xb3,0xa0,0xf4,0x45,0x7a,0x19,0xdf,0xee,0x78,0x34,0x60
};
static uint8_t S1[256] = {
0x55,0xc2,0x63,0x71,0x3b,0xc8,0x47,0x86,0x9f,0x3c,0xda,0x5b,0x29,0xaa,0xfd,0x77,
0x8c,0xc5,0x94,0x0c,0xa6,0x1a,0x13,0x00,0xe3,0xa8,0x16,0x72,0x40,0xf9,0xf8,0x42,
0x44,0x26,0x68,0x96,0x81,0xd9,0x45,0x3e,0x10,0x76,0xc6,0xa7,0x8b,0x39,0x43,0xe1,
0x3a,0xb5,0x56,0x2a,0xc0,0x6d,0xb3,0x05,0x22,0x66,0xbf,0xdc,0x0b,0xfa,0x62,0x48,
0xdd,0x20,0x11,0x06,0x36,0xc9,0xc1,0xcf,0xf6,0x27,0x52,0xbb,0x69,0xf5,0xd4,0x87,
0x7f,0x84,0x4c,0xd2,0x9c,0x57,0xa4,0xbc,0x4f,0x9a,0xdf,0xfe,0xd6,0x8d,0x7a,0xeb,
0x2b,0x53,0xd8,0x5c,0xa1,0x14,0x17,0xfb,0x23,0xd5,0x7d,0x30,0x67,0x73,0x08,0x09,
0xee,0xb7,0x70,0x3f,0x61,0xb2,0x19,0x8e,0x4e,0xe5,0x4b,0x93,0x8f,0x5d,0xdb,0xa9,
0xad,0xf1,0xae,0x2e,0xcb,0x0d,0xfc,0xf4,0x2d,0x46,0x6e,0x1d,0x97,0xe8,0xd1,0xe9,
0x4d,0x37,0xa5,0x75,0x5e,0x83,0x9e,0xab,0x82,0x9d,0xb9,0x1c,0xe0,0xcd,0x49,0x89,
0x01,0xb6,0xbd,0x58,0x24,0xa2,0x5f,0x38,0x78,0x99,0x15,0x90,0x50,0xb8,0x95,0xe4,
0xd0,0x91,0xc7,0xce,0xed,0x0f,0xb4,0x6f,0xa0,0xcc,0xf0,0x02,0x4a,0x79,0xc3,0xde,
0xa3,0xef,0xea,0x51,0xe6,0x6b,0x18,0xec,0x1b,0x2c,0x80,0xf7,0x74,0xe7,0xff,0x21,
0x5a,0x6a,0x54,0x1e,0x41,0x31,0x92,0x35,0xc4,0x33,0x07,0x0a,0xba,0x7e,0x0e,0x34,
0x88,0xb1,0x98,0x7c,0xf3,0x3d,0x60,0x6c,0x7b,0xca,0xd3,0x1f,0x32,0x65,0x04,0x28,
0x64,0xbe,0x85,0x9b,0x2f,0x59,0x8a,0xd7,0xb0,0x25,0xac,0xaf,0x12,0x03,0xe2,0xf2
};
static uint32_t EK_d[16] = {
0x44D7, 0x26BC, 0x626B, 0x135E, 0x5789, 0x35E2, 0x7135, 0x09AF,
0x4D78, 0x2F13, 0x6BC4, 0x1AF1, 0x5E26, 0x3C4D, 0x789A, 0x47AC
};
static inline uint32_t AddM(uint32_t a, uint32_t b)
{
uint32_t c = a + b;
return (c & 0x7FFFFFFF) + (c >> 31);
}
#define MulByPow2(x, k) ((((x) << k) | ((x) >> (31 - k))) & 0x7FFFFFFF)
static void LFSRWithInitialisationMode(ZUC_KEY *key, uint32_t u)
{
uint32_t f, v;
f = key->LFSR_S0;
v = MulByPow2(key->LFSR_S0, 8);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S4, 20);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S10, 21);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S13, 17);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S15, 15);
f = AddM(f, v);
f = AddM(f, u);
/* update the state */
key->LFSR_S0 = key->LFSR_S1;
key->LFSR_S1 = key->LFSR_S2;
key->LFSR_S2 = key->LFSR_S3;
key->LFSR_S3 = key->LFSR_S4;
key->LFSR_S4 = key->LFSR_S5;
key->LFSR_S5 = key->LFSR_S6;
key->LFSR_S6 = key->LFSR_S7;
key->LFSR_S7 = key->LFSR_S8;
key->LFSR_S8 = key->LFSR_S9;
key->LFSR_S9 = key->LFSR_S10;
key->LFSR_S10 = key->LFSR_S11;
key->LFSR_S11 = key->LFSR_S12;
key->LFSR_S12 = key->LFSR_S13;
key->LFSR_S13 = key->LFSR_S14;
key->LFSR_S14 = key->LFSR_S15;
key->LFSR_S15 = f;
}
/* LFSR with work mode */
static void LFSRWithWorkMode(ZUC_KEY *key)
{
uint32_t f, v;
f = key->LFSR_S0;
v = MulByPow2(key->LFSR_S0, 8);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S4, 20);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S10, 21);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S13, 17);
f = AddM(f, v);
v = MulByPow2(key->LFSR_S15, 15);
f = AddM(f, v);
/* update the state */
key->LFSR_S0 = key->LFSR_S1;
key->LFSR_S1 = key->LFSR_S2;
key->LFSR_S2 = key->LFSR_S3;
key->LFSR_S3 = key->LFSR_S4;
key->LFSR_S4 = key->LFSR_S5;
key->LFSR_S5 = key->LFSR_S6;
key->LFSR_S6 = key->LFSR_S7;
key->LFSR_S7 = key->LFSR_S8;
key->LFSR_S8 = key->LFSR_S9;
key->LFSR_S9 = key->LFSR_S10;
key->LFSR_S10 = key->LFSR_S11;
key->LFSR_S11 = key->LFSR_S12;
key->LFSR_S12 = key->LFSR_S13;
key->LFSR_S13 = key->LFSR_S14;
key->LFSR_S14 = key->LFSR_S15;
key->LFSR_S15 = f;
}
/* BitReorganization */
static inline void BitReorganization(ZUC_KEY *key)
{
key->BRC_X0 = ((key->LFSR_S15 & 0x7FFF8000) << 1) | (key->LFSR_S14 & 0xFFFF);
key->BRC_X1 = ((key->LFSR_S11 & 0xFFFF) << 16) | (key->LFSR_S9 >> 15);
key->BRC_X2 = ((key->LFSR_S7 & 0xFFFF) << 16) | (key->LFSR_S5 >> 15);
key->BRC_X3 = ((key->LFSR_S2 & 0xFFFF) << 16) | (key->LFSR_S0 >> 15);
}
#define ROT(a, k) (((a) << k) | ((a) >> (32 - k)))
static inline uint32_t L1(uint32_t X)
{
return (X ^ ROT(X, 2) ^ ROT(X, 10) ^ ROT(X, 18) ^ ROT(X, 24));
}
static inline uint32_t L2(uint32_t X)
{
return (X ^ ROT(X, 8) ^ ROT(X, 14) ^ ROT(X, 22) ^ ROT(X, 30));
}
#define MAKEuint32_t(a, b, c, d) (((uint32_t)(a) << 24) | ((uint32_t)(b) << 16) | ((uint32_t)(c) << 8) | ((uint32_t)(d)))
static uint32_t F(ZUC_KEY *key)
{
uint32_t W, W1, W2, u, v;
W = (key->BRC_X0 ^ key->F_R1) + key->F_R2;
W1 = key->F_R1 + key->BRC_X1;
W2 = key->F_R2 ^ key->BRC_X2;
u = L1((W1 << 16) | (W2 >> 16));
v = L2((W2 << 16) | (W1 >> 16));
key->F_R1 = MAKEuint32_t(S0[u >> 24], S1[(u >> 16) & 0xFF], S0[(u >> 8) & 0xFF], S1[u & 0xFF]);
key->F_R2 = MAKEuint32_t(S0[v >> 24], S1[(v >> 16) & 0xFF], S0[(v >> 8) & 0xFF], S1[v & 0xFF]);
return W;
}
#define MAKEU31(a, b, c) (((uint32_t)(a) << 23) | ((uint32_t)(b) << 8) | (uint32_t)(c))
void ZUC_set_key(ZUC_KEY *key, const unsigned char *k, const unsigned char *iv)
{
uint32_t w, nCount;
/* expand key */
key->LFSR_S0 = MAKEU31(k[0], EK_d[0], iv[0]);
key->LFSR_S1 = MAKEU31(k[1], EK_d[1], iv[1]);
key->LFSR_S2 = MAKEU31(k[2], EK_d[2], iv[2]);
key->LFSR_S3 = MAKEU31(k[3], EK_d[3], iv[3]);
key->LFSR_S4 = MAKEU31(k[4], EK_d[4], iv[4]);
key->LFSR_S5 = MAKEU31(k[5], EK_d[5], iv[5]);
key->LFSR_S6 = MAKEU31(k[6], EK_d[6], iv[6]);
key->LFSR_S7 = MAKEU31(k[7], EK_d[7], iv[7]);
key->LFSR_S8 = MAKEU31(k[8], EK_d[8], iv[8]);
key->LFSR_S9 = MAKEU31(k[9], EK_d[9], iv[9]);
key->LFSR_S10 = MAKEU31(k[10], EK_d[10], iv[10]);
key->LFSR_S11 = MAKEU31(k[11], EK_d[11], iv[11]);
key->LFSR_S12 = MAKEU31(k[12], EK_d[12], iv[12]);
key->LFSR_S13 = MAKEU31(k[13], EK_d[13], iv[13]);
key->LFSR_S14 = MAKEU31(k[14], EK_d[14], iv[14]);
key->LFSR_S15 = MAKEU31(k[15], EK_d[15], iv[15]);
/* set F_R1 and F_R2 to zero */
key->F_R1 = 0;
key->F_R2 = 0;
nCount = 32;
while (nCount > 0)
{
BitReorganization(key);
w = F(key);
LFSRWithInitialisationMode(key, w >> 1);
nCount--;
}
}
void ZUC_encrypt(ZUC_KEY *key, size_t inlen, const unsigned char *in, unsigned char *out)
{
#if 0
int i;
BitReorganization(key);
F(key); /* discard the output of F */
LFSRWithWorkMode(key);
for (i = 0; i < KeystreamLen; i ++)
{
BitReorganization(key);
pKeystream[i] = F(key) ^ key->BRC_X3;
LFSRWithWorkMode(key);
}
#endif
}

51
crypto/zuc/zuc.h Executable file
View File

@@ -0,0 +1,51 @@
#ifndef HEADER_ZUC_H
#define HEADER_ZUC_H
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint32_t LFSR_S0;
uint32_t LFSR_S1;
uint32_t LFSR_S2;
uint32_t LFSR_S3;
uint32_t LFSR_S4;
uint32_t LFSR_S5;
uint32_t LFSR_S6;
uint32_t LFSR_S7;
uint32_t LFSR_S8;
uint32_t LFSR_S9;
uint32_t LFSR_S10;
uint32_t LFSR_S11;
uint32_t LFSR_S12;
uint32_t LFSR_S13;
uint32_t LFSR_S14;
uint32_t LFSR_S15;
/* the registers of F */
uint32_t F_R1;
uint32_t F_R2;
/* the outputs of BitReorganization */
uint32_t BRC_X0;
uint32_t BRC_X1;
uint32_t BRC_X2;
uint32_t BRC_X3;
} ZUC_KEY;
void ZUC_set_key(ZUC_KEY *key, const unsigned char *k, const unsigned char *iv);
void ZUC_encrypt(ZUC_KEY *key, size_t inlen, const unsigned char *in, unsigned char *out);
#ifdef __cplusplus
}
#endif
#endif

26
demos/sm2/sm2-pkcs7.pem Normal file
View File

@@ -0,0 +1,26 @@
-----BEGIN PKCS7-----
MIIEbgYJKoZIhvcNAQcCoIIEXzCCBFsCAQExADALBgkqhkiG9w0BBwGgggRDMIIB
szCCAVegAwIBAgIIaeL+wBcKxnswDAYIKoEcz1UBg3UFADAuMQswCQYDVQQGEwJD
TjEOMAwGA1UECgwFTlJDQUMxDzANBgNVBAMMBlJPT1RDQTAeFw0xMjA3MTQwMzEx
NTlaFw00MjA3MDcwMzExNTlaMC4xCzAJBgNVBAYTAkNOMQ4wDAYDVQQKDAVOUkNB
QzEPMA0GA1UEAwwGUk9PVENBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEMPCc
a6pmgcchsTf2UnBeL9rtp4nw+itk1Kzrmbnqo05lUwkwlWK+4OIrtFdAqnRTV7Q9
v1htkv42TsIutzd126NdMFswHwYDVR0jBBgwFoAUTDKxl9kzG8SmBcHG5YtiW/CX
dlgwDAYDVR0TBAUwAwEB/zALBgNVHQ8EBAMCAQYwHQYDVR0OBBYEFEwysZfZMxvE
pgXBxuWLYlvwl3ZYMAwGCCqBHM9VAYN1BQADSAAwRQIgG1bSLeOXp3oB8H7b53W+
CKOPl2PknmWEq/lMhtn25HkCIQDaHDgWxWFtnCrBjH16/W3Ezn7/U/Vjo5xIpDoi
VhsLwjCCAogwggIsoAMCAQICEC2hpr0M52xiTmNLfWop0CowDAYIKoEcz1UBg3UF
ADAuMQswCQYDVQQGEwJDTjEOMAwGA1UECgwFTlJDQUMxDzANBgNVBAMMBlJPT1RD
QTAeFw0xMzA5MTMwODEwMjVaFw0zMzA5MDgwODEwMjVaMDQxCzAJBgNVBAYTAkNO
MREwDwYDVQQKDAhVbmlUcnVzdDESMBAGA1UEAwwJU0hFQ0EgU00yMFkwEwYHKoZI
zj0CAQYIKoEcz1UBgi0DQgAEfdEfkS0GSlQQ8ISEVSUdvKL7tcd3bsNssWlmmOhN
5VCg1iLJgMDDqhO9TFt4EDsZuvECXz8uiU+BL4pddBcMgKOCASIwggEeMB8GA1Ud
IwQYMBaAFEwysZfZMxvEpgXBxuWLYlvwl3ZYMA8GA1UdEwEB/wQFMAMBAf8wgboG
A1UdHwSBsjCBrzBBoD+gPaQ7MDkxCzAJBgNVBAYTAkNOMQ4wDAYDVQQKDAVOUkNB
QzEMMAoGA1UECwwDQVJMMQwwCgYDVQQDDANhcmwwKqAooCaGJGh0dHA6Ly93d3cu
cm9vdGNhLmdvdi5jbi9hcmwvYXJsLmNybDA+oDygOoY4bGRhcDovL2xkYXAucm9v
dGNhLmdvdi5jbjozODkvQ049YXJsLE9VPUFSTCxPPU5SQ0FDLEM9Q04wDgYDVR0P
AQH/BAQDAgEGMB0GA1UdDgQWBBSJMQSRe0Oqqpq/hB2bhu7wuHCZoDAMBggqgRzP
VQGDdQUAA0gAMEUCIQCKe/9772vmcRXjynapM3RqFchrHxh4Yiy0HiqwmUNkOQIg
JjDnX9H9G/Aopa1VnYvKX2cjukamH56XCet/Jeyh3zQxAA==
-----END PKCS7-----

16
demos/sm2/sm2-x509.pem Normal file
View File

@@ -0,0 +1,16 @@
-----BEGIN CERTIFICATE-----
MIICiDCCAiygAwIBAgIQLaGmvQznbGJOY0t9ainQKjAMBggqgRzPVQGDdQUAMC4x
CzAJBgNVBAYTAkNOMQ4wDAYDVQQKDAVOUkNBQzEPMA0GA1UEAwwGUk9PVENBMB4X
DTEzMDkxMzA4MTAyNVoXDTMzMDkwODA4MTAyNVowNDELMAkGA1UEBhMCQ04xETAP
BgNVBAoMCFVuaVRydXN0MRIwEAYDVQQDDAlTSEVDQSBTTTIwWTATBgcqhkjOPQIB
BggqgRzPVQGCLQNCAAR90R+RLQZKVBDwhIRVJR28ovu1x3duw2yxaWaY6E3lUKDW
IsmAwMOqE71MW3gQOxm68QJfPy6JT4Evil10FwyAo4IBIjCCAR4wHwYDVR0jBBgw
FoAUTDKxl9kzG8SmBcHG5YtiW/CXdlgwDwYDVR0TAQH/BAUwAwEB/zCBugYDVR0f
BIGyMIGvMEGgP6A9pDswOTELMAkGA1UEBhMCQ04xDjAMBgNVBAoMBU5SQ0FDMQww
CgYDVQQLDANBUkwxDDAKBgNVBAMMA2FybDAqoCigJoYkaHR0cDovL3d3dy5yb290
Y2EuZ292LmNuL2FybC9hcmwuY3JsMD6gPKA6hjhsZGFwOi8vbGRhcC5yb290Y2Eu
Z292LmNuOjM4OS9DTj1hcmwsT1U9QVJMLE89TlJDQUMsQz1DTjAOBgNVHQ8BAf8E
BAMCAQYwHQYDVR0OBBYEFIkxBJF7Q6qqmr+EHZuG7vC4cJmgMAwGCCqBHM9VAYN1
BQADSAAwRQIhAIp7/3vva+ZxFePKdqkzdGoVyGsfGHhiLLQeKrCZQ2Q5AiAmMOdf
0f0b8CilrVWdi8pfZyO6RqYfnpcJ638l7KHfNA==
-----END CERTIFICATE-----

1
include/openssl/zuc.h Symbolic link
View File

@@ -0,0 +1 @@
../../crypto/zuc/zuc.h

29
sm2test.c Normal file
View File

@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/ecdsa.h>
#include <openssl/objects.h>
int main(int argc, char **argv)
{
int ok;
EC_KEY *ec_key;
ECDSA_SIG *sig;
unsigned char dgst[32];
ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1);
OPENSSL_assert(ec_key);
ok = EC_KEY_generate_key(ec_key);
OPENSSL_assert(ok);
sig = ECDSA_do_sign(dgst, 32, ec_key);
ok = ECDSA_do_verify(dgst, 32, sig, ec_key);
printf("ok = %d\n", ok);
return 0;
}

View File

@@ -111,7 +111,7 @@ and [options] can be one of
no-ripemd no-sm3
no-rc2 no-rc4 no-rc5 no-idea no-des - Skip this symetric cipher
no-bf no-cast no-aes no-camellia no-seed
no-sms4
no-sms4 no-zuc
no-rsa no-dsa no-dh - Skip this public key cipher
no-ssl2 no-ssl3 - Skip this version of SSL
just-ssl - remove all non-ssl keys/digest
@@ -295,6 +295,7 @@ $cflags.= " -DZLIB" if $zlib_opt;
$cflags.= " -DZLIB_SHARED" if $zlib_opt == 2;
$cflags.=" -DOPENSSL_NO_SM3" if $no_sm3;
$cflags.=" -DOPENSSL_NO_SMS4" if $no_sms4;
$cflags.=" -DOPENSSL_NO_ZUC" if $no_zuc;
if ($no_static_engine)
{
@@ -919,6 +920,7 @@ sub var_add
@a=grep(!/(^sm3)|(_sm3$)/,@a) if $no_sm3;
@a=grep(!/(^sms4)|(_sms4$)/,@a) if $no_sms4;
@a=grep(!/(^zuc)|(_zuc$)/,@a) if $no_zuc;
grep($_="$dir/$_",@a);
@a=grep(!/(^|\/)s_/,@a) if $no_sock;
@@ -1248,7 +1250,8 @@ sub read_options
"no-multiblock" => 0,
"fips" => \$fips,
"no-sm3" => \$no_sm3,
"no-sms4" => \$no_sms4
"no-sms4" => \$no_sms4,
"no-zuc" => \$no_zuc
);
if (exists $valid_options{$_})

View File

@@ -146,7 +146,7 @@ my $no_rfc3779; my $no_psk; my $no_tlsext; my $no_cms; my $no_capieng;
my $no_jpake; my $no_srp; my $no_ssl2; my $no_ec2m; my $no_nistp_gcc;
my $no_nextprotoneg; my $no_sctp; my $no_srtp; my $no_ssl_trace;
my $no_unit_test; my $no_ssl3_method;
my $no_sm3; my $no_sms4;
my $no_sm3; my $no_sms4; my $no_zuc;
my $fips;
@@ -251,6 +251,7 @@ foreach (@ARGV, split(/ /, $options))
elsif (/^no-unit-test$/){ $no_unit_test=1; }
elsif (/^no-sm3$/) { $no_sm3=1; }
elsif (/^no-sms4$/) { $no_sms4=1; }
elsif (/^no-zuc$/) { $no_zuc=1; }
}
@@ -361,6 +362,7 @@ $crypto.=" crypto/srp/srp.h";
$crypto.=" crypto/sm3/sm3.h" ; # unless $no_sm3;
$crypto.=" crypto/sms4/sms4.h" ; # unless $no_sms4;
$crypto.=" crypto/zuc/zuc.h" ; # unless $no_zuc;
my $symhacks="crypto/symhacks.h";
@@ -983,6 +985,7 @@ sub do_defs
$a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
$a .= ",SM3" if($s =~ /EVP_sm3/);
$a .= ",SMS4" if($s =~ /EVP_sms4/);
$a .= ",ZUC" if($s =~ /EVP_zuc/);
$platform{$s} =
&reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
@@ -1236,6 +1239,7 @@ sub is_valid
if ($keyword eq "DEPRECATED" && $no_deprecated) { return 0; }
if ($keyword eq "SM3" && $no_sm3) { return 0; }
if ($keyword eq "SMS4" && $no_sms4) { return 0; }
if ($keyword eq "ZUC" && $no_zuc) { return 0; }
# Nothing recognise as true
return 1;

View File

@@ -23,6 +23,7 @@ my @dirs = (
"crypto/rc4",
"crypto/rc5",
"crypto/sms4",
"crypto/zuc",
"crypto/idea",
"crypto/bf",
"crypto/cast",

View File

@@ -13,7 +13,7 @@
$line=0;
foreach $a ("md2","md4","md5","sha","sha1","rc4","des cfb","des cbc","des ede3",
"idea cfb","idea cbc","rc2 cfb","rc2 cbc","blowfish cbc","cast cbc",
"sm3","sms4 cfb","sms4 cbc")
"sm3","sms4 cfb","sms4 cbc","zuc")
{
if (defined($one{$a,8}) && defined($two{$a,8}))
{