This commit is contained in:
Zhi Guan
2016-06-06 22:04:44 +02:00
parent 2bf25bd29f
commit 2cb43b7f80
142 changed files with 7768 additions and 1678 deletions

View File

@@ -135,6 +135,7 @@ int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
unsigned char *p, *buf_in = NULL, *buf_out = NULL;
int i, inl = 0, outl = 0, outll = 0;
X509_ALGOR *a;
printf("%s %d: %s executed\n", __FILE__, __LINE__, __FUNCTION__);
EVP_MD_CTX_init(&ctx);
for (i = 0; i < 2; i++) {
@@ -222,8 +223,10 @@ int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
{
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
printf("%s %d: %s executed\n", __FILE__, __LINE__, __FUNCTION__);
if (!EVP_DigestSignInit(&ctx, NULL, type, NULL, pkey)) {
EVP_MD_CTX_cleanup(&ctx);
ERR_print_errors_fp(stderr);
return 0;
}
return ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, &ctx);
@@ -239,6 +242,7 @@ int ASN1_item_sign_ctx(const ASN1_ITEM *it,
size_t inl = 0, outl = 0, outll = 0;
int signid, paramtype;
int rv;
printf("%s %d: %s executed\n", __FILE__, __LINE__, __FUNCTION__);
type = EVP_MD_CTX_md(ctx);
pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx);

View File

@@ -72,9 +72,6 @@ extern const EVP_PKEY_ASN1_METHOD dhx_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD cmac_asn1_meth;
#ifndef OPENSSL_NO_SM2
extern const EVP_PKEY_ASN1_METHOD sm2_asn1_meth;
#endif
/* Keep this sorted in type order !! */
static const EVP_PKEY_ASN1_METHOD *standard_methods[] = {

View File

@@ -55,7 +55,7 @@ int CBCMAC_Init(CBCMAC_CTX *ctx, const void *key, size_t keylen,
const EVP_CIPHER *cipher, ENGINE *eng)
{
int i, block_size;
if (!EVP_EncryptInit_ex(&ctx->cipher_ctx, cipher, eng, key, NULL)) {
CBCMACerr(CBCMAC_F_CBCMAC_INIT, CBCMAC_R_CIPHER_CTX_INIT_FAILED);
return 0;
@@ -67,7 +67,7 @@ int CBCMAC_Init(CBCMAC_CTX *ctx, const void *key, size_t keylen,
ctx->worklen = 0;
block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx);
bzero(ctx->cbcstate, block_size);
return 0;
return 1;
}
int CBCMAC_Update(CBCMAC_CTX *ctx, const void *data, size_t datalen)
@@ -119,7 +119,7 @@ int CBCMAC_Update(CBCMAC_CTX *ctx, const void *data, size_t datalen)
}
return 0;
return 1;
}
int CBCMAC_Final(CBCMAC_CTX *ctx, unsigned char *out, size_t *outlen)

View File

@@ -72,7 +72,7 @@ typedef struct cpk_master_secret_st {
ASN1_OCTET_STRING *secret_factors;
} CPK_MASTER_SECRET;
DECLARE_ASN1_FUNCTIONS(CPK_MASTER_SECRET)
typedef struct cpk_public_params_st {
long version;
@@ -82,6 +82,7 @@ typedef struct cpk_public_params_st {
ASN1_OCTET_STRING *public_factors;
} CPK_PUBLIC_PARAMS;
DECLARE_ASN1_FUNCTIONS(CPK_PUBLIC_PARAMS)
X509_ALGOR *CPK_MAP_new_default(void);
int CPK_MAP_is_valid(const X509_ALGOR *algor);

View File

@@ -1,126 +0,0 @@
/* crypto/cpk/cpk_err.c */
/* ====================================================================
* Copyright (c) 2007 - 2015 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 <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
}

View File

@@ -1004,7 +1004,7 @@ int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL)
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
# define EVP_PKEY_CTX_set_ec_sign_type(ctx, type) \
EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
EVP_PKEY_OP_SIGN|EVP_PKEY_OP_SIGNCTX| \
@@ -1036,7 +1036,7 @@ int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
EVP_PKEY_OP_DERIVE, \
EVP_PKEY_CTRL_EC_DH_TYPE, -2, NULL);
# endif /* OPENSSL_NO_GMSSL */
# endif /* NO_GMSSL */
@@ -1101,7 +1101,7 @@ int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
# define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
# define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
# define EVP_PKEY_CTRL_EC_SIGN_TYPE (EVP_PKEY_ALG_CTRL + 11)
# define EVP_PKEY_CTRL_GET_EC_SIGN_TYPE (EVP_PKEY_ALG_CTRL + 12)
# define EVP_PKEY_CTRL_EC_ENC_TYPE (EVP_PKEY_ALG_CTRL + 13)

View File

@@ -613,7 +613,7 @@ const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
EVP_PKEY_EC,
0,
"EC",
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
"GmSSL EC/SM2 algorithm",
#else
"OpenSSL EC algorithm",

View File

@@ -61,7 +61,7 @@
#include <openssl/err.h>
#include <openssl/asn1t.h>
#include <openssl/objects.h>
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
#include <openssl/evp.h>
#endif

View File

@@ -2835,7 +2835,7 @@ static const struct {
}
};
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
static const struct {
EC_CURVE_DATA h;
unsigned char data[0 + 32 * 6];
@@ -3112,7 +3112,7 @@ static const ec_list_element curve_list[] = {
"RFC 5639 curve over a 512 bit prime field"},
{NID_brainpoolP512t1, &_EC_brainpoolP512t1.h, 0,
"RFC 5639 curve over a 512 bit prime field"},
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
{NID_sm2p256v1, &_EC_SM2_PRIME_256V1.h, 0,
"SM2 curve over a 256 bit prime field"},
{NID_wapip192v1, &_EC_WAPI_PRIME_192V1.h, 0,

View File

@@ -88,15 +88,21 @@ typedef struct {
size_t kdf_ukmlen;
/* KDF output length */
size_t kdf_outlen;
#ifndef NO_GMSSL
/* SECG, SM2 or other standards */
int sign_type;
int enc_type;
int dh_type;
union {
void *ptr;
ECIES_PARAMS *ecies;
SM2_ENC_PARAMS *sm2;
} enc_param;
unsigned char sm2_id_dgst[EVP_MAX_MD_SIZE];
size_t sm2_id_dgstlen;
union {
void *ptr;
ECIES_PARAMS *ecies;
SM2_ENC_PARAMS *sm2;
} enc_param;
#endif
} EC_PKEY_CTX;
static int pkey_ec_init(EVP_PKEY_CTX *ctx)
@@ -115,11 +121,13 @@ static int pkey_ec_init(EVP_PKEY_CTX *ctx)
dctx->kdf_outlen = 0;
dctx->kdf_ukm = NULL;
dctx->kdf_ukmlen = 0;
#ifndef NO_GMSSL
dctx->sign_type = NID_sm_scheme;
dctx->enc_type = NID_sm_scheme;
dctx->dh_type = NID_sm_scheme;
dctx->enc_param.ptr = NULL;
dctx->enc_type = NID_sm_scheme;
dctx->dh_type = NID_sm_scheme;
dctx->sm2_id_dgstlen = 0;
dctx->enc_param.ptr = NULL;
#endif
ctx->data = dctx;
return 1;
@@ -157,6 +165,7 @@ static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
dctx->sign_type = sctx->sign_type;
dctx->enc_type = sctx->enc_type;
dctx->dh_type = sctx->dh_type;
// fixme
return 1;
}
@@ -256,8 +265,7 @@ static int pkey_ec_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
unsigned char zid[EVP_MAX_MD_SIZE];
unsigned int zidlen = sizeof(zid);
//FIXME: it is wrong to do it here!
if (dctx->sign_type == NID_sm_scheme) {
if (!SM2_compute_id_digest(md, zid, &zidlen, ec_key)) {
ECerr(EC_F_PKEY_EC_SIGNCTX_INIT, ERR_R_SM2_LIB);
@@ -272,7 +280,6 @@ static int pkey_ec_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
}
}
return 1;
}
@@ -595,13 +602,16 @@ static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
dctx->kdf_type = p1;
return 1;
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
case EVP_PKEY_CTRL_EC_SIGN_TYPE:
if (p1 == -2)
return dctx->sign_type;
if (p1 != NID_secg_scheme && p1 != NID_sm_scheme)
return -2;
dctx->sign_type = p1;
//SM2_compute_id_digest();
//set this data to pkey_ctx
return 1;
case EVP_PKEY_CTRL_GET_EC_SIGN_TYPE:
@@ -670,7 +680,7 @@ static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
EVP_MD_type((const EVP_MD *)p2) != NID_sm3 &&
#endif
EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
@@ -722,7 +732,7 @@ static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
else
return -2;
return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
} else if (!strcmp(type, "ec_sign_algor")) {
int sign_type;
if (!strcmp(value, "ecdsa"))

View File

@@ -122,4 +122,4 @@ ecies_lib.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
ecies_lib.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
ecies_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
ecies_lib.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
ecies_lib.o: ecies_lib.c
ecies_lib.o: ../o_str.h ecies_lib.c

View File

@@ -118,7 +118,7 @@ int i2d_ECIESParameters(const ECIES_PARAMS *param, unsigned char **out);
ECIES_PARAMS *d2i_ECIESParameters(ECIES_PARAMS **param, const unsigned char **in, long len);
int ECIES_set_parameters(EC_KEY *ec_key, const ECIES_PARAMS *param);
ECIES_PARAMS *ECIES_get_parameters(const EC_KEY *ec_key);
ECIES_PARAMS *ECIES_get_parameters(EC_KEY *ec_key);
ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
const unsigned char *in, size_t inlen, EC_KEY *ec_key);
int ECIES_do_decrypt(const ECIES_CIPHERTEXT_VALUE *cv,

View File

@@ -59,6 +59,7 @@
#include <openssl/ecdh.h>
#include <openssl/kdf.h>
#include <openssl/ecies.h>
#include "../o_str.h"
static void *ecies_data_dup(void *data) {
@@ -71,7 +72,7 @@ static void *ecies_data_dup(void *data) {
return NULL;
}
memcpy(ret, param, sizeof(*param));
memcpy(ret, param, sizeof(*param));
return ret;
}
@@ -97,7 +98,8 @@ int ECIES_set_parameters(EC_KEY *ec_key, const ECIES_PARAMS *param)
return 1;
}
ECIES_PARAMS *ECIES_get_parameters(const EC_KEY *ec_key)
//FIXME: is is _get0_ ?
ECIES_PARAMS *ECIES_get_parameters(EC_KEY *ec_key)
{
ECIES_PARAMS *ret;
if (!(ret = EC_KEY_get_key_method_data(ec_key,
@@ -120,7 +122,7 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
EVP_CIPHER_CTX cipher_ctx;
EVP_CIPHER_CTX_init(&cipher_ctx);
if (!(cv = ECIES_CIPHERTEXT_VALUE_new()))
if (!(cv = ECIES_CIPHERTEXT_VALUE_new()))
{
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
ECIESerr(ECIES_F_ECIES_DO_ENCRYPT, ERR_R_MALLOC_FAILURE);
@@ -164,7 +166,7 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
ECIESerr(ECIES_F_ECIES_DO_ENCRYPT, ERR_R_EC_LIB);
goto err;
}
}
/*
* use ecdh to compute enckey and mackey
@@ -205,7 +207,7 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
goto err;
}
if (!ECDH_compute_key(share, sharelen,
if (!ECDH_compute_key(share, sharelen,
EC_KEY_get0_public_key(pub_key), ephem_key,
KDF_get_x9_63(param->kdf_md)))
{
@@ -222,15 +224,15 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
if (param->sym_cipher)
len = (int)(inlen + EVP_MAX_BLOCK_LENGTH * 2);
else len = inlen;
if (!M_ASN1_OCTET_STRING_set(cv->ciphertext, NULL, len))
{
ECIESerr(ECIES_F_ECIES_DO_ENCRYPT, ERR_R_MALLOC_FAILURE);
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto err;
}
if (param->sym_cipher)
if (param->sym_cipher)
{
unsigned char iv[EVP_MAX_IV_LENGTH];
memset(iv, 0, sizeof(iv));
@@ -243,7 +245,7 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
goto err;
}
p = cv->ciphertext->data;
if (!EVP_EncryptUpdate(&cipher_ctx, p, &len, in, (int)inlen))
if (!EVP_EncryptUpdate(&cipher_ctx, p, &len, in, (int)inlen))
{
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
ECIESerr(ECIES_F_ECIES_DO_ENCRYPT,
@@ -259,7 +261,7 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
goto err;
}
p += len;
cv->ciphertext->length = (int)(p - cv->ciphertext->data);
cv->ciphertext->length = (int)(p - cv->ciphertext->data);
}
else
{
@@ -273,12 +275,12 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
* calculate mactag of ciphertext and encode
*/
cv->mactag->length = maclen;
if (!M_ASN1_OCTET_STRING_set(cv->mactag, NULL, cv->mactag->length))
{
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
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,
@@ -293,8 +295,8 @@ ECIES_CIPHERTEXT_VALUE *ECIES_do_encrypt(const ECIES_PARAMS *param,
err:
EVP_CIPHER_CTX_cleanup(&cipher_ctx);
if (share) OPENSSL_free(share);
if (ephem_key) EC_KEY_free(ephem_key);
if (e && cv)
if (ephem_key) EC_KEY_free(ephem_key);
if (e && cv)
{
ECIES_CIPHERTEXT_VALUE_free(cv);
cv = NULL;
@@ -303,7 +305,7 @@ err:
}
int ECIES_do_decrypt(const ECIES_CIPHERTEXT_VALUE *cv,
const ECIES_PARAMS *param, unsigned char *out, size_t *outlen,
const ECIES_PARAMS *param, unsigned char *out, size_t *outlen,
EC_KEY *pri_key)
{
int r = 0;
@@ -351,10 +353,10 @@ int ECIES_do_decrypt(const ECIES_CIPHERTEXT_VALUE *cv,
ECIESerr(ECIES_F_ECIES_DO_DECRYPT, ECIES_R_BAD_DATA);
goto err;
}
/*
* use ecdh to compute enckey and mackey
*/
*/
if (param->sym_cipher)
enckeylen = EVP_CIPHER_key_length(param->sym_cipher);
else enckeylen = cv->ciphertext->length;
@@ -366,17 +368,17 @@ int ECIES_do_decrypt(const ECIES_CIPHERTEXT_VALUE *cv,
ECIESerr(ECIES_F_ECIES_DO_DECRYPT, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!ECDH_compute_key(share, enckeylen + mackeylen,
ephem_point, pri_key,
KDF_get_x9_63(param->kdf_md)))
KDF_get_x9_63(param->kdf_md)))
{
ECIESerr(ECIES_F_ECIES_DO_DECRYPT, ECIES_R_ECDH_FAILED);
goto err;
}
}
enckey = share;
mackey = share + enckeylen;
/*
* generate and verify mac
*/
@@ -387,7 +389,7 @@ int ECIES_do_decrypt(const ECIES_CIPHERTEXT_VALUE *cv,
}
if (!HMAC(param->mac_md, mackey, mackeylen,
cv->ciphertext->data, (size_t)cv->ciphertext->length,
mac, (unsigned int *)&len))
mac, (unsigned int *)&len))
{
ECIESerr(ECIES_F_ECIES_DO_DECRYPT, ECIES_R_GEN_MAC_FAILED);
goto err;
@@ -431,14 +433,14 @@ int ECIES_do_decrypt(const ECIES_CIPHERTEXT_VALUE *cv,
p += len;
*outlen = (int)(p - out);
}
else
else
{
int i;
for (i = 0; i < cv->ciphertext->length; i++)
out[i] = cv->ciphertext->data[i] ^ enckey[i];
*outlen = cv->ciphertext->length;
}
r = 1;
err:
if (share) OPENSSL_free(share);
@@ -463,7 +465,7 @@ int ECIES_encrypt(const ECIES_PARAMS *param,
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
return 0;
}
if ((len = i2d_ECIES_CIPHERTEXT_VALUE(cv, NULL)) <= 0) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
ECIESerr(ECIES_F_ECIES_ENCRYPT, ECIES_R_ENCRYPT_FAILED);

View File

@@ -113,7 +113,7 @@ void ENGINE_load_builtin_engines(void)
# ifndef OPENSSL_NO_GOST
ENGINE_load_gost();
# endif
# ifndef OPENSSL_NO_SKF
# ifndef NO_GMSSL
//ENGINE_load_skf();
# endif
# ifndef OPENSSL_NO_GMP

View File

@@ -411,7 +411,7 @@ void ENGINE_load_gmp(void);
# ifndef OPENSSL_NO_GOST
void ENGINE_load_gost(void);
# endif
# ifndef OPENSSL_NO_SKF
# ifndef NO_GMSSL
void ENGINE_load_skf(void);
# endif
# endif

View File

@@ -83,31 +83,32 @@ err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
err.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h
err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
err.o: ../cryptlib.h err.c
err_all.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
err_all.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h
err_all.o: ../../include/openssl/cbcmac.h ../../include/openssl/cms.h
err_all.o: ../../include/openssl/comp.h ../../include/openssl/conf.h
err_all.o: ../../include/openssl/cpk.h ../../include/openssl/crypto.h
err_all.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h
err_all.o: ../../include/openssl/dso.h ../../include/openssl/e_os2.h
err_all.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h
err_all.o: ../../include/openssl/ecdsa.h ../../include/openssl/ecies.h
err_all.o: ../../include/openssl/engine.h ../../include/openssl/err.h
err_all.o: ../../include/openssl/evp.h ../../include/openssl/kdf.h
err_all.o: ../../include/openssl/aes.h ../../include/openssl/asn1.h
err_all.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
err_all.o: ../../include/openssl/buffer.h ../../include/openssl/cbcmac.h
err_all.o: ../../include/openssl/cms.h ../../include/openssl/comp.h
err_all.o: ../../include/openssl/conf.h ../../include/openssl/cpk.h
err_all.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h
err_all.o: ../../include/openssl/dsa.h ../../include/openssl/dso.h
err_all.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
err_all.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h
err_all.o: ../../include/openssl/ecies.h ../../include/openssl/engine.h
err_all.o: ../../include/openssl/err.h ../../include/openssl/evp.h
err_all.o: ../../include/openssl/ffx.h ../../include/openssl/kdf.h
err_all.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
err_all.o: ../../include/openssl/objects.h ../../include/openssl/ocsp.h
err_all.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
err_all.o: ../../include/openssl/ossl_typ.h ../../include/openssl/otp.h
err_all.o: ../../include/openssl/pem2.h ../../include/openssl/pkcs12.h
err_all.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
err_all.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h
err_all.o: ../../include/openssl/sha.h ../../include/openssl/skf.h
err_all.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
err_all.o: ../../include/openssl/sm9.h ../../include/openssl/stack.h
err_all.o: ../../include/openssl/symhacks.h ../../include/openssl/ts.h
err_all.o: ../../include/openssl/ui.h ../../include/openssl/x509.h
err_all.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h
err_all.o: err_all.c
err_all.o: ../../include/openssl/paillier.h ../../include/openssl/pem2.h
err_all.o: ../../include/openssl/pkcs12.h ../../include/openssl/pkcs7.h
err_all.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
err_all.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
err_all.o: ../../include/openssl/skf.h ../../include/openssl/sm2.h
err_all.o: ../../include/openssl/sm3.h ../../include/openssl/sm9.h
err_all.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
err_all.o: ../../include/openssl/ts.h ../../include/openssl/ui.h
err_all.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
err_all.o: ../../include/openssl/x509v3.h err_all.c
err_prn.o: ../../e_os.h ../../include/openssl/bio.h
err_prn.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
err_prn.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h

View File

@@ -155,7 +155,7 @@ static ERR_STRING_DATA ERR_str_libraries[] = {
{ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
{ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
{ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
{ERR_PACK(ERR_LIB_ECIES, 0, 0), "ECIES routines"},
{ERR_PACK(ERR_LIB_CPK, 0, 0), "CPK routines"},
{ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},

View File

@@ -198,7 +198,7 @@ typedef struct err_state_st {
# define ERR_LIB_HMAC 48
# define ERR_LIB_JPAKE 49
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
# define ERR_LIB_ECIES 50
# define ERR_LIB_CPK 51
# define ERR_LIB_SM2 52
@@ -246,16 +246,16 @@ typedef struct err_state_st {
# define HMACerr(f,r) ERR_PUT_error(ERR_LIB_HMAC,(f),(r),__FILE__,__LINE__)
# define JPAKEerr(f,r) ERR_PUT_error(ERR_LIB_JPAKE,(f),(r),__FILE__,__LINE__)
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
# define ECIESerr(f,r) ERR_PUT_error(ERR_LIB_ECIES,(f),(r),__FILE__,__LINE__)
# define CPKerr(f,r) ERR_PUT_error(ERR_LIB_CPK,(f),(r),__FILE__,__LINE__)
# define SM2err(f,r) ERR_PUT_error(ERR_LIB_SM2,(f),(r),__FILE__,__LINE__)
# define SKFerr(f,r) ERR_PUT_error(ERR_LIB_SKF,(f),(r),__FILE__,__LINE__)
# define CBCMACerr(f,r) ERR_PUT_error(ERR_LIB_CBCMAC,(f),(r),__FILE__,__LINE__);
# define OTPerr(f,r) ERR_PUT_error(ERR_LIB_OTP,(f),(r),__FILE__,__LINE__);
# define SM9err(f,r) ERR_PUT_error(ERR_LIB_SM9,(f),(r),__FILE__,__LINE__);
# define PAILLIERerr(f,r) ERR_PUT_error(ERR_LIB_PAILLIER,(f),(r),__FILE__,__LINE__);
# define FFXerr(f,r) ERR_PUT_error(ERR_LIB_FFX,(f),(r),__FILE__,__LINE__);
# define CBCMACerr(f,r) ERR_PUT_error(ERR_LIB_CBCMAC,(f),(r),__FILE__,__LINE__)
# define OTPerr(f,r) ERR_PUT_error(ERR_LIB_OTP,(f),(r),__FILE__,__LINE__)
# define SM9err(f,r) ERR_PUT_error(ERR_LIB_SM9,(f),(r),__FILE__,__LINE__)
# define PAILLIERerr(f,r) ERR_PUT_error(ERR_LIB_PAILLIER,(f),(r),__FILE__,__LINE__)
# define FFXerr(f,r) ERR_PUT_error(ERR_LIB_FFX,(f),(r),__FILE__,__LINE__)
# endif
/*
@@ -314,7 +314,7 @@ typedef struct err_state_st {
# define ERR_R_STORE_LIB ERR_LIB_STORE/* 44 */
# define ERR_R_TS_LIB ERR_LIB_TS/* 45 */
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
# define ERR_R_ECIES_LIB ERR_LIB_ECIES/* 50 */
# define ERR_R_CPK_LIB ERR_LIB_CPK/* 51 */
# define ERR_R_SM2_LIB ERR_LIB_SM2/* 52 */
@@ -326,6 +326,8 @@ typedef struct err_state_st {
# define ERR_R_FFX_LIB ERR_LIB_FFX/* 58 */
# endif
//FIXME: it seems we used up error numbers
# define ERR_R_NESTED_ASN1_ERROR 58
# define ERR_R_BAD_ASN1_OBJECT_HEADER 59
# define ERR_R_BAD_GET_ASN1_OBJECT_CALL 60

View File

@@ -108,7 +108,7 @@
# include <openssl/jpake.h>
#endif
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
# include <openssl/sm2.h>
# include <openssl/skf.h>
# include <openssl/cpk.h>
@@ -176,7 +176,7 @@ void ERR_load_crypto_strings(void)
# ifndef OPENSSL_NO_JPAKE
ERR_load_JPAKE_strings();
# endif
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
ERR_load_ECIES_strings();
ERR_load_CPK_strings();
ERR_load_SM2_strings();

View File

@@ -163,7 +163,7 @@ void OpenSSL_add_all_ciphers(void)
EVP_add_cipher_alias(SN_rc5_cbc, "RC5");
#endif
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
EVP_add_cipher(EVP_sms4_ecb());
EVP_add_cipher(EVP_sms4_cbc());
EVP_add_cipher(EVP_sms4_cfb());

View File

@@ -67,7 +67,7 @@ void OpenSSL_add_all_digests(void)
#ifndef OPENSSL_NO_MD4
EVP_add_digest(EVP_md4());
#endif
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
EVP_add_digest(EVP_sm3());
#endif
#ifndef OPENSSL_NO_MD5

View File

@@ -211,7 +211,6 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
type = ctx->digest;
}
#endif
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
if (ctx->digest != type) {
if (ctx->digest && ctx->digest->ctx_size)
OPENSSL_free(ctx->md_data);
@@ -220,7 +219,6 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
ctx->update = type->update;
ctx->md_data = OPENSSL_malloc(type->ctx_size);
if (ctx->md_data == NULL) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
return 0;
}
@@ -231,7 +229,6 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
#endif
if (ctx->pctx) {
int r;
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
if (r <= 0 && (r != -2))
@@ -248,7 +245,6 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
return 0;
}
#endif
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
return ctx->digest->init(ctx);
}

View File

@@ -53,7 +53,8 @@
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/crypto.h>
@@ -61,8 +62,6 @@
#include "modes_lcl.h"
#include <openssl/sms4.h>
#define SMS4_IV_LENGTH SMS4_BLOCK_SIZE
typedef struct {
sms4_key_t ks;
} EVP_SMS4_KEY;
@@ -95,7 +94,7 @@ static int sms4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t len)
{
EVP_SMS4_KEY *sms4_key = (EVP_SMS4_KEY *)ctx->cipher_data;
if (ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) {
CRYPTO_cfb128_1_encrypt(in, out, len, &sms4_key->ks,
ctx->iv, &ctx->num, ctx->encrypt, (block128_f)sms4_encrypt);
@@ -144,7 +143,7 @@ static int sms4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
CRYPTO_cfb128_8_encrypt(in, out, len, &sms4_key->ks,
ctx->iv, &ctx->num, ctx->encrypt, (block128_f)sms4_encrypt);
return 1;
}
@@ -236,7 +235,7 @@ static int sms4_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
case EVP_CTRL_COPY:
default:
return -1;
}
}
}
@@ -255,7 +254,7 @@ static int sms4_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
EVP_SMS4_WRAP_CTX *sms4_wrap = ctx->cipher_data;
if (!iv && !key)
return 1;
@@ -337,7 +336,7 @@ static int sms4_wrap_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
NULL, /* get_asn1_parameters() */
NULL, /* ctrl() */
NULL /* app_data */
};
};
const EVP_CIPHER *EVP_sms4_wrap(void)
{

View File

@@ -1,47 +1,88 @@
/* crypto/evp/e_zuc.c */
/* ====================================================================
* Copyright (c) 2014 - 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 <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
#include <openssl/evp.h>
#include "evp_locl.h"
#include <openssl/objects.h>
#include <openssl/zuc.h>
#include <openssl/objects.h>
#include "evp_locl.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, key, iv);
zuc_set_key((zuc_key_t *)&ctx->cipher_data, key, iv);
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);
zuc_encrypt((zuc_key_t *)&ctx->cipher_data, inlen, in, out);
return 1;
}
/*
* FIXME:
* evp_enc.c assert block_size in {1, 8, 16}, 4 not ok!
*/
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 */
NID_zuc,
1, 16, 16,
EVP_CIPH_VARIABLE_LENGTH,
zuc_init,
zuc_do_cipher,
NULL,
sizeof(zuc_key_t),
NULL, NULL, NULL, NULL
};
const EVP_CIPHER *EVP_zuc(void)

View File

@@ -741,7 +741,7 @@ const EVP_MD *EVP_ripemd160(void);
# ifndef OPENSSL_NO_WHIRLPOOL
const EVP_MD *EVP_whirlpool(void);
# endif
# ifndef OPENSSL_NO_GMSSL
# ifndef NO_GMSSL
const EVP_MD *EVP_sm3(void);
# endif
const EVP_CIPHER *EVP_enc_null(void); /* does nothing :-) */
@@ -830,7 +830,7 @@ const EVP_CIPHER *EVP_rc5_32_12_16_cfb64(void);
# define EVP_rc5_32_12_16_cfb EVP_rc5_32_12_16_cfb64
const EVP_CIPHER *EVP_rc5_32_12_16_ofb(void);
# endif
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
const EVP_CIPHER *EVP_sms4_ecb(void);
const EVP_CIPHER *EVP_sms4_cbc(void);
const EVP_CIPHER *EVP_sms4_cfb1(void);

View File

@@ -210,7 +210,7 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
#endif
/* we assume block size is a power of 2 in *cryptUpdate */
OPENSSL_assert(ctx->cipher->block_size == 1
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
|| ctx->cipher->block_size == 4
#endif
|| ctx->cipher->block_size == 8
@@ -668,18 +668,3 @@ int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
return 1;
}
#ifndef OPENSSL_NO_GMSSL
int EVP_Encrypt_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outlen,
const unsigned char *in, int inlen)
{
return 0;
}
int EVP_Decrypt_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outlen,
const unsigned char *in, int inlen)
{
return 1;
}
#endif /* GMSSL */

View File

@@ -86,7 +86,6 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
return 0;
}
}
if (ver) {
if (ctx->pctx->pmeth->verifyctx_init) {
if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
@@ -97,14 +96,12 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
} else {
if (ctx->pctx->pmeth->signctx_init) {
if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0) {
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
return 0;
}
ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
} else if (EVP_PKEY_sign_init(ctx->pctx) <= 0)
return 0;
}
if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
return 0;
if (pctx)

View File

@@ -52,7 +52,7 @@
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
#include <openssl/evp.h>
#include <openssl/objects.h>
@@ -62,17 +62,29 @@
static int init(EVP_MD_CTX *ctx)
{
return sm3_init(ctx->md_data);
if (!ctx || !ctx->md_data) {
return 0;
}
sm3_init(ctx->md_data);
return 1;
}
static int update(EVP_MD_CTX *ctx, const void *in, size_t inlen)
{
return sm3_update(ctx->md_data, in, inlen);
if (!ctx || !ctx->md_data || !in) {
return 0;
}
sm3_update(ctx->md_data, in, inlen);
return 1;
}
static int final(EVP_MD_CTX *ctx, unsigned char *md)
{
return sm3_final(ctx->md_data, md);
if (!ctx || !ctx->md_data || !md) {
return 0;
}
sm3_final(ctx->md_data, md);
return 1;
}
static const EVP_MD sm3_md = {

View File

@@ -200,21 +200,17 @@ EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
mac_ctx = EVP_PKEY_CTX_new_id(type, e);
if (!mac_ctx) {
if (e) fprintf(stderr, "engine is not null\n");
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
return NULL;
}
if (EVP_PKEY_keygen_init(mac_ctx) <= 0) {
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
goto merr;
}
if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_SET_MAC_KEY,
keylen, (void *)key) <= 0) {
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
goto merr;
}
if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) {
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
goto merr;
}
merr:

View File

@@ -95,7 +95,7 @@ static const EVP_PKEY_METHOD *standard_methods[] = {
#ifndef OPENSSL_NO_DH
&dhx_pkey_meth,
#endif
#ifndef OPENSSL_NO_GMSSL
#ifndef NO_GMSSL
&cbcmac_pkey_meth,
#endif
};

View File

@@ -61,6 +61,8 @@ tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
@@ -74,3 +76,17 @@ clean:
# DO NOT DELETE THIS LINE -- make depend depends on it.
ffx.o: ../../include/openssl/aes.h ../../include/openssl/bio.h
ffx.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
ffx.o: ../../include/openssl/err.h ../../include/openssl/ffx.h
ffx.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
ffx.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
ffx.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
ffx.o: ../../include/openssl/symhacks.h ffx.c
ffx_err.o: ../../include/openssl/aes.h ../../include/openssl/bio.h
ffx_err.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
ffx_err.o: ../../include/openssl/err.h ../../include/openssl/ffx.h
ffx_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
ffx_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
ffx_err.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
ffx_err.o: ../../include/openssl/symhacks.h ffx_err.c

View File

@@ -56,6 +56,13 @@ static const nid_triple sigoid_srt[] = {
NID_dh_cofactor_kdf},
{NID_dhSinglePass_cofactorDH_sha512kdf_scheme, NID_sha512,
NID_dh_cofactor_kdf},
{NID_sm2sign_with_sm3, NID_sm3, NID_X9_62_id_ecPublicKey},
{NID_sm2sign_with_sha1, NID_sha1, NID_sm2sign},
{NID_sm2sign_with_sha256, NID_sha256, NID_sm2sign},
{NID_sm2sign_with_sha512, NID_sha512, NID_sm2sign},
{NID_sm2sign_with_sha224, NID_sha224, NID_sm2sign},
{NID_sm2sign_with_sha384, NID_sha384, NID_sm2sign},
{NID_sm2sign_with_rmd160, NID_ripemd160, NID_sm2sign},
};
static const nid_triple *const sigoid_srt_xref[] = {
@@ -71,29 +78,36 @@ static const nid_triple *const sigoid_srt_xref[] = {
&sigoid_srt[12],
&sigoid_srt[30],
&sigoid_srt[35],
&sigoid_srt[41],
&sigoid_srt[6],
&sigoid_srt[10],
&sigoid_srt[46],
&sigoid_srt[11],
&sigoid_srt[13],
&sigoid_srt[24],
&sigoid_srt[20],
&sigoid_srt[32],
&sigoid_srt[37],
&sigoid_srt[42],
&sigoid_srt[14],
&sigoid_srt[21],
&sigoid_srt[33],
&sigoid_srt[38],
&sigoid_srt[45],
&sigoid_srt[15],
&sigoid_srt[22],
&sigoid_srt[34],
&sigoid_srt[39],
&sigoid_srt[43],
&sigoid_srt[16],
&sigoid_srt[23],
&sigoid_srt[19],
&sigoid_srt[31],
&sigoid_srt[36],
&sigoid_srt[44],
&sigoid_srt[25],
&sigoid_srt[26],
&sigoid_srt[27],
&sigoid_srt[28],
&sigoid_srt[40],
};

View File

@@ -29,6 +29,7 @@ dsaWithSHA1 sha1 dsa
dsaWithSHA1_2 sha1 dsa_2
# GMSSL: here is the problem!
ecdsa_with_SHA1 sha1 X9_62_id_ecPublicKey
ecdsa_with_SHA224 sha224 X9_62_id_ecPublicKey
ecdsa_with_SHA256 sha256 X9_62_id_ecPublicKey
@@ -36,6 +37,7 @@ ecdsa_with_SHA384 sha384 X9_62_id_ecPublicKey
ecdsa_with_SHA512 sha512 X9_62_id_ecPublicKey
ecdsa_with_Recommended undef X9_62_id_ecPublicKey
ecdsa_with_Specified undef X9_62_id_ecPublicKey
sm2sign_with_sm3 sm3 X9_62_id_ecPublicKey
dsa_with_SHA224 sha224 dsa
dsa_with_SHA256 sha256 dsa
@@ -56,3 +58,11 @@ dhSinglePass_cofactorDH_sha224kdf_scheme sha224 dh_cofactor_kdf
dhSinglePass_cofactorDH_sha256kdf_scheme sha256 dh_cofactor_kdf
dhSinglePass_cofactorDH_sha384kdf_scheme sha384 dh_cofactor_kdf
dhSinglePass_cofactorDH_sha512kdf_scheme sha512 dh_cofactor_kdf
# GMSSL SM2 schemes
sm2sign_with_sha1 sha1 sm2sign
sm2sign_with_sha256 sha256 sm2sign
sm2sign_with_sha512 sha512 sm2sign
sm2sign_with_sha224 sha224 sm2sign
sm2sign_with_sha384 sha384 sm2sign
sm2sign_with_rmd160 ripemd160 sm2sign

View File

@@ -66,6 +66,8 @@ tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)

View File

@@ -66,6 +66,8 @@ tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
@@ -79,3 +81,16 @@ clean:
# DO NOT DELETE THIS LINE -- make depend depends on it.
pai_err.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
pai_err.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
pai_err.o: ../../include/openssl/err.h ../../include/openssl/lhash.h
pai_err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
pai_err.o: ../../include/openssl/ossl_typ.h ../../include/openssl/paillier.h
pai_err.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
pai_err.o: ../../include/openssl/symhacks.h pai_err.c
pai_lib.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h
pai_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
pai_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
pai_lib.o: ../../include/openssl/paillier.h ../../include/openssl/safestack.h
pai_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
pai_lib.o: pai_lib.c

View File

@@ -137,10 +137,6 @@ extern "C" {
# define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
# define PEM_STRING_PARAMETERS "PARAMETERS"
# define PEM_STRING_CMS "CMS"
# ifndef OPENSSL_NO_GMSSL
# define PEM_STRING_SM2PARAMETERS "SM2 PARAMETERS"
# define PEM_STRING_SM2PRIVATEKEY "SM2 PRIVATE KEY"
# endif
/*
* Note that this structure is initialised by PEM_SealInit and cleaned up

View File

@@ -65,6 +65,8 @@ tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
@@ -74,7 +76,7 @@ dclean:
mv -f Makefile.new $(MAKEFILE)
clean:
rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff a.out
# DO NOT DELETE THIS LINE -- make depend depends on it.

View File

@@ -1,6 +1,6 @@
/* crypto/skf/skf.h */
/* ====================================================================
* Copyright (c) 2014 - 2015 The GmSSL Project. All rights reserved.
* Copyright (c) 2014 - 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

View File

@@ -58,6 +58,7 @@
#define DEV_NAME "pseudo_dev"
#define DEV_NAME_LIST DEV_NAME"\0"
SKF_HANDLE skf_dev_handle;
ULONG DEVAPI SKF_EnumDev(BOOL bPresent,
LPSTR szNameList,
@@ -79,11 +80,14 @@ ULONG DEVAPI SKF_EnumDev(BOOL bPresent,
ULONG DEVAPI SKF_ConnectDev(LPSTR szName,
DEVHANDLE *phDev)
{
*phDev = &skf_dev_handle;
return SAR_OK;
}
ULONG DEVAPI SKF_DisConnectDev(DEVHANDLE hDev)
{
//FIXME: close all handles
hDev = NULL;
return SAR_OK;
}
@@ -155,4 +159,33 @@ int SKF_print_dev_info(DEVINFO *devInfo)
return 1;
}
char *SKF_get_alg_name(ULONG ulAlgID)
{
//FIXME: make these name compatible with OBJ short name
switch (ulAlgID) {
case SGD_SM1_ECB: return "SM1-ECB";
case SGD_SM1_CBC: return "SM1-CBC";
case SGD_SM1_CFB: return "SM1-CFB";
case SGD_SM1_OFB: return "SM1-OFB";
case SGD_SM1_MAC: return "SM1-MAC";
case SGD_SM4_ECB: return "SM4-ECB";
case SGD_SM4_CBC: return "SM4-CBC";
case SGD_SM4_CFB: return "SM4-CFB";
case SGD_SM4_OFB: return "SM4-OFB";
case SGD_SM4_MAC: return "SM4-MAC";
case SGD_SSF33_ECB: return "SSF33-ECB";
case SGD_SSF33_CBC: return "SSF33-CBC";
case SGD_SSF33_CFB: return "SSF33-CFB";
case SGD_SSF33_OFB: return "SSF33-OFB";
case SGD_SSF33_MAC: return "SSF33-MAC";
case SGD_RSA: return "RSA";
case SGD_SM2_1: return "SM2-1";
case SGD_SM2_2: return "SM2-2";
case SGD_SM2_3: return "SM2-3";
case SGD_SM3: return "SM3";
case SGD_SHA1: return "SHA-1";
case SGD_SHA256: return "SHA256";
}
return "(unknown)";
}

View File

@@ -67,7 +67,7 @@ ULONG DEVAPI SKF_DigestInit(DEVHANDLE hDev,
const EVP_MD *md;
EVP_MD_CTX *mdctx = NULL;
EC_KEY *ec_key = NULL;
SKF_HANDLE *handle;
SKF_HANDLE *hHash;
unsigned char dgst[EVP_MAX_MD_SIZE];
unsigned int dgstlen = 0;
@@ -137,17 +137,20 @@ ULONG DEVAPI SKF_DigestInit(DEVHANDLE hDev,
}
}
if (!(handle = SKF_HANDLE_new(SKF_HASH_HANDLE))) {
SKFerr(SKF_F_SKF_DIGESTINIT, SKF_R_MALLOC_FAILED);
ret = SAR_MEMORYERR;
if (!(hHash = OPENSSL_malloc(sizeof(*hHash)))) {
SKFerr(SKF_F_SKF_DIGESTINIT, ERR_R_MALLOC_FAILURE);
goto end;
}
handle->u.md_ctx = mdctx;
bzero(hHash, sizeof(*hHash));
hHash->magic = SKF_HANDLE_MAGIC;
hHash->type = SKF_HASH_HANDLE;
hHash->u.md_ctx = mdctx;
mdctx = NULL;
*phHash = hHash;
ret = SAR_OK;
end:
EVP_MD_CTX_destroy(mdctx);
EC_KEY_free(ec_key);
@@ -204,11 +207,8 @@ ULONG DEVAPI SKF_DigestFinal(HANDLE hHash,
return SAR_FAIL;
}
if (!SKF_HANDLE_free(hHash)) {
SKFerr(SKF_F_SKF_DIGESTFINAL, SKF_R_FREE_HANDLE_FAILED);
return SAR_FAIL;
}
EVP_MD_CTX_destroy(mdctx);
((SKF_HANDLE *)hHash)->u.md_ctx = NULL;
return SAR_OK;
}

View File

@@ -56,6 +56,30 @@
#include <openssl/skf_ex.h>
#include "skf_lcl.h"
ULONG DEVAPI SKF_GenExtECCKeyPair(DEVHANDLE hDev,
ECCPRIVATEKEYBLOB *priKey,
ECCPUBLICKEYBLOB *pubKey)
{
ULONG ret = SAR_FAIL;
EC_KEY *ec_key;
if(!(ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1))) {
SKFerr(SKF_F_SKF_GENEXTECCKEYPAIR, ERR_R_EC_LIB);
return SAR_FAIL;
}
if (!EC_KEY_get_ECCPRIVATEKEYBLOB(ec_key, priKey)) {
SKFerr(SKF_F_SKF_GENEXTECCKEYPAIR, SKF_R_GET_PRIVATE_KEY_FAILED);
goto end;
}
if (!EC_KEY_get_ECCPUBLICKEYBLOB(ec_key, pubKey)) {
SKFerr(SKF_F_SKF_GENEXTECCKEYPAIR, SKF_R_GET_PUBLIC_KEY_FAILED);
goto end;
}
ret = SAR_OK;
end:
EC_KEY_free(ec_key);
return ret;
}
ULONG DEVAPI SKF_ExtECCSign(DEVHANDLE hDev,
ECCPRIVATEKEYBLOB *pECCPriKeyBlob,

View File

@@ -59,6 +59,101 @@
#define PADDING_TYPE_NO_PADDING 0
#define PADDING_TYPE_PKCS5 1
/*
229 typedef struct Struct_BLOCKCIPHERPARAM {
230 BYTE IV[MAX_IV_LEN];
231 ULONG IVLen;
232 ULONG PaddingType;
233 ULONG FeedBitLen;
234 } BLOCKCIPHERPARAM, *PBLOCKCIPHERPARAM;
*/
int SKF_nid_to_encparams(int nid, ULONG *algID, BLOCKCIPHERPARAM *params)
{
ULONG ulAlgID = 0;
switch (nid) {
case NID_ssf33_ecb:
ulAlgID = SGD_SSF33_ECB;
break;
case NID_ssf33_cbc:
ulAlgID = SGD_SSF33_CBC;
break;
case NID_ssf33_cfb1:
case NID_ssf33_cfb8:
case NID_ssf33_cfb128:
ulAlgID = SGD_SSF33_CFB;
break;
case NID_ssf33_ofb128:
ulAlgID = SGD_SSF33_OFB;
break;
case NID_sm1_ecb:
ulAlgID = SGD_SM1_ECB;
break;
case NID_sm1_cbc:
ulAlgID = SGD_SM1_CBC;
break;
case NID_sm1_cfb1:
case NID_sm1_cfb8:
case NID_sm1_cfb128:
ulAlgID = SGD_SM1_CFB;
break;
case NID_sm1_ofb128:
ulAlgID = SGD_SM1_OFB;
break;
case NID_sms4_ecb:
ulAlgID = SGD_SM4_ECB;
break;
case NID_sms4_cbc:
ulAlgID = SGD_SM4_CBC;
break;
case NID_sms4_cfb1:
case NID_sms4_cfb8:
case NID_sms4_cfb128:
ulAlgID = SGD_SM4_CFB;
break;
case NID_sms4_ofb128:
ulAlgID = SGD_SM4_OFB;
break;
default:
return 0;
}
*algID = ulAlgID;
switch (nid) {
case NID_sm1_cfb1:
case NID_sms4_cfb1:
case NID_ssf33_cfb1:
params->FeedBitLen = 1;
break;
case NID_sm1_cfb8:
case NID_sms4_cfb8:
case NID_ssf33_cfb8:
params->FeedBitLen = 8;
break;
case NID_sm1_cfb128:
case NID_sms4_cfb128:
case NID_ssf33_cfb128:
params->FeedBitLen = 128;
break;
default:
params->FeedBitLen = 0;
}
switch (nid) {
case NID_sm1_cbc:
case NID_sms4_cbc:
case NID_ssf33_cbc:
params->PaddingType = SKF_PKCS5_PADDING;
break;
default:
params->PaddingType = SKF_NO_PADDING;
}
return 1;
}
ULONG DEVAPI SKF_EncryptInit(HANDLE hKey,
BLOCKCIPHERPARAM encryptParam)
@@ -70,25 +165,25 @@ ULONG DEVAPI SKF_EncryptInit(HANDLE hKey,
unsigned char *key;
unsigned char *iv;
//FIXME: CFB bits!
if (!(cipher = SKF_HANDLE_get_cipher(hKey, encparam))) {
SKFerr(SKF_F_SKF_ENCRYPTINIT, SKF_R_INVALID_KEY_HANDLE);
return SAR_INVALIDPARAMERR;
}
if (!(key = SKF_HANDLE_get_key(hKey))) {
SKFerr(SKF_F_SKF_ENCRYPTINIT, SKF_R_INVALID_KEY_HANDLE);
ret = SAR_INVALIDPARAMERR;
goto end;
return SAR_INVALIDPARAMERR;
}
if (encparam->IVLen != SMS4_IV_LENGTH) {
SKFerr(SKF_F_SKF_ENCRYPTINIT, SKF_R_INVALID_IV_LENGTH);
ret = SAR_INVALIDPARAMERR;
goto end;
return SAR_INVALIDPARAMERR;
}
iv = encparam->IV;
if (!(ctx = EVP_CIPHER_CTX_new())) {
SKFerr(SKF_F_SKF_ENCRYPTINIT, ERR_R_EVP_LIB);
goto end;
return SAR_INVALIDPARAMERR;
}
if (!EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv)) {
@@ -96,10 +191,8 @@ ULONG DEVAPI SKF_EncryptInit(HANDLE hKey,
goto end;
}
if (!SKF_HANDLE_set1_cipher_ctx(hKey, ctx)) {
SKFerr(SKF_F_SKF_ENCRYPTINIT, SKF_R_SET_CIPHER_CTX_FAILED);
goto end;
}
((SKF_HANDLE *)hKey)->type = SKF_CIPHER_HANDLE;
((SKF_HANDLE *)hKey)->u.cipher_ctx = ctx;
ctx = NULL;
ret = SAR_OK;
@@ -153,7 +246,9 @@ ULONG DEVAPI SKF_EncryptFinal(HANDLE hKey,
}
*pulEncryptedDataLen = outlen;
SKF_HANDLE_free_cipher_ctx(hKey);
EVP_CIPHER_CTX_free(ctx);
((SKF_HANDLE *)hKey)->u.cipher_ctx = NULL;
((SKF_HANDLE *)hKey)->type = SKF_KEY_HANDLE;
return SAR_OK;
}
@@ -167,18 +262,19 @@ ULONG DEVAPI SKF_DecryptInit(HANDLE hKey,
unsigned char *key;
unsigned char *iv;
if (!(cipher = SKF_HANDLE_get_cipher(hKey, param))) {
SKFerr(SKF_F_SKF_DECRYPTINIT, SKF_R_INVALID_KEY_HANDLE);
return SAR_INVALIDPARAMERR;
}
if (!(key = SKF_HANDLE_get_key(hKey))) {
SKFerr(SKF_F_SKF_DECRYPTINIT, SKF_R_INVALID_KEY_HANDLE);
ret = SAR_INVALIDPARAMERR;
goto end;
return SAR_INVALIDPARAMERR;
}
if (param->IVLen != SMS4_IV_LENGTH) {
SKFerr(SKF_F_SKF_DECRYPTINIT, SKF_R_INVALID_IV_LENGTH);
ret = SAR_INVALIDPARAMERR;
goto end;
}
iv = param->IV;
if (!(ctx = EVP_CIPHER_CTX_new())) {
@@ -191,10 +287,8 @@ ULONG DEVAPI SKF_DecryptInit(HANDLE hKey,
goto end;
}
if (!SKF_HANDLE_set1_cipher_ctx(hKey, ctx)) {
SKFerr(SKF_F_SKF_DECRYPTINIT, SKF_R_SET_CIPHER_CTX_FAILED);
goto end;
}
((SKF_HANDLE *)hKey)->type = SKF_CIPHER_HANDLE;
((SKF_HANDLE *)hKey)->u.cipher_ctx = ctx;
ctx = NULL;
ret = SAR_OK;
@@ -248,7 +342,9 @@ ULONG DEVAPI SKF_DecryptFinal(HANDLE hKey,
}
*pulDecryptedDataLen = len;
SKF_HANDLE_free_cipher_ctx(hKey);
EVP_CIPHER_CTX_free(ctx);
((SKF_HANDLE *)hKey)->u.cipher_ctx = NULL;
((SKF_HANDLE *)hKey)->type = SKF_KEY_HANDLE;
return SAR_OK;
}

View File

@@ -89,6 +89,7 @@ static ERR_STRING_DATA SKF_str_functs[] = {
{ERR_FUNC(SKF_F_EC_KEY_SET_ECCPUBLICKEYBLOB),
"EC_KEY_set_ECCPUBLICKEYBLOB"},
{ERR_FUNC(SKF_F_RSA_GET_RSAPRIVATEKEYBLOB), "RSA_get_RSAPRIVATEKEYBLOB"},
{ERR_FUNC(SKF_F_RSA_GET_RSAPUBLICKEYBLOB), "RSA_get_RSAPUBLICKEYBLOB"},
{ERR_FUNC(SKF_F_RSA_NEW_FROM_RSAPRIVATEKEYBLOB),
"RSA_new_from_RSAPRIVATEKEYBLOB"},
{ERR_FUNC(SKF_F_RSA_NEW_FROM_RSAPUBLICKEYBLOB),
@@ -127,6 +128,7 @@ static ERR_STRING_DATA SKF_str_functs[] = {
{ERR_FUNC(SKF_F_SKF_EXTECCVERIFY), "SKF_EXTECCVERIFY"},
{ERR_FUNC(SKF_F_SKF_EXTRSAPRIKEYOPERATION), "SKF_EXTRSAPRIKEYOPERATION"},
{ERR_FUNC(SKF_F_SKF_EXTRSAPUBKEYOPERATION), "SKF_EXTRSAPUBKEYOPERATION"},
{ERR_FUNC(SKF_F_SKF_GENEXTECCKEYPAIR), "SKF_GENEXTECCKEYPAIR"},
{ERR_FUNC(SKF_F_SKF_GENEXTRSAKEY), "SKF_GENEXTRSAKEY"},
{ERR_FUNC(SKF_F_SKF_GENRANDOM), "SKF_GENRANDOM"},
{ERR_FUNC(SKF_F_SKF_GETDEVINFO), "SKF_GETDEVINFO"},
@@ -134,7 +136,9 @@ static ERR_STRING_DATA SKF_str_functs[] = {
{ERR_FUNC(SKF_F_SKF_GETFILEINFO), "SKF_GETFILEINFO"},
{ERR_FUNC(SKF_F_SKF_GETPININFO), "SKF_GETPININFO"},
{ERR_FUNC(SKF_F_SKF_HANDLE_GET_CBCMAC_CTX), "SKF_HANDLE_GET_CBCMAC_CTX"},
{ERR_FUNC(SKF_F_SKF_HANDLE_GET_CIPHER), "SKF_HANDLE_GET_CIPHER"},
{ERR_FUNC(SKF_F_SKF_HANDLE_GET_CIPHER_CTX), "SKF_HANDLE_GET_CIPHER_CTX"},
{ERR_FUNC(SKF_F_SKF_HANDLE_GET_KEY), "SKF_HANDLE_GET_KEY"},
{ERR_FUNC(SKF_F_SKF_HANDLE_GET_MD_CTX), "SKF_HANDLE_GET_MD_CTX"},
{ERR_FUNC(SKF_F_SKF_LOCKDEV), "SKF_LOCKDEV"},
{ERR_FUNC(SKF_F_SKF_MAC), "SKF_MAC"},
@@ -171,11 +175,15 @@ static ERR_STRING_DATA SKF_str_reasons[] = {
{ERR_REASON(SKF_R_DECRYPT_FAILED), "decrypt failed"},
{ERR_REASON(SKF_R_ENCODE_CIPHERTEXT_FAILED), "encode ciphertext failed"},
{ERR_REASON(SKF_R_ENCODE_FAILED), "encode failed"},
{ERR_REASON(SKF_R_ENCODE_RSA_PUBLIC_KEY_FAILED),
"encode rsa public key failed"},
{ERR_REASON(SKF_R_ENCODE_SIGNATURE_FAILED), "encode signature failed"},
{ERR_REASON(SKF_R_ENCRYPT_FAILED), "encrypt failed"},
{ERR_REASON(SKF_R_FAIL), "fail"},
{ERR_REASON(SKF_R_FREE_HANDLE_FAILED), "free handle failed"},
{ERR_REASON(SKF_R_GEN_RSA_FAILED), "gen rsa failed"},
{ERR_REASON(SKF_R_GET_PRIVATE_KEY_FAILED), "get private key failed"},
{ERR_REASON(SKF_R_GET_PUBLIC_KEY_FAILED), "get public key failed"},
{ERR_REASON(SKF_R_INVALID_ALGID), "invalid algid"},
{ERR_REASON(SKF_R_INVALID_ALGOR), "invalid algor"},
{ERR_REASON(SKF_R_INVALID_APP_HANDLE), "invalid app handle"},
@@ -194,7 +202,9 @@ static ERR_STRING_DATA SKF_str_reasons[] = {
{ERR_REASON(SKF_R_INVALID_EC_KEY), "invalid ec key"},
{ERR_REASON(SKF_R_INVALID_EC_PRIVATE_KEY), "invalid ec private key"},
{ERR_REASON(SKF_R_INVALID_EC_PUBLIC_KEY), "invalid ec public key"},
{ERR_REASON(SKF_R_INVALID_FEED_BIT_LENGTH), "invalid feed bit length"},
{ERR_REASON(SKF_R_INVALID_HANDLE), "invalid handle"},
{ERR_REASON(SKF_R_INVALID_HANDLE_ALGOR), "invalid handle algor"},
{ERR_REASON(SKF_R_INVALID_HANDLE_MAGIC), "invalid handle magic"},
{ERR_REASON(SKF_R_INVALID_HANDLE_TYPE), "invalid handle type"},
{ERR_REASON(SKF_R_INVALID_HASH_HANDLE), "invalid hash handle"},
@@ -208,6 +218,7 @@ static ERR_STRING_DATA SKF_str_reasons[] = {
{ERR_REASON(SKF_R_INVALID_PRIVATE_KEY), "invalid private key"},
{ERR_REASON(SKF_R_INVALID_PUBLIC_KEY), "invalid public key"},
{ERR_REASON(SKF_R_INVALID_RANDOM_LENGTH), "invalid random length"},
{ERR_REASON(SKF_R_INVALID_RSA_KEY_LENGTH), "invalid rsa key length"},
{ERR_REASON(SKF_R_INVALID_RSA_PRIVATE_KEY), "invalid rsa private key"},
{ERR_REASON(SKF_R_INVALID_RSA_PUBLIC_KEY), "invalid rsa public key"},
{ERR_REASON(SKF_R_INVALID_SIGNATURE), "invalid signature"},

View File

@@ -115,10 +115,10 @@ char *SKF_get_errstr(ULONG ulError)
int i;
for (i = 0; i < sizeof(skf_errstr)/sizeof(skf_errstr[0]); i++) {
if (ulError == skf_errstr[i].err_no) {
return (LPSTR)skf_errstr[i].err_str;
return skf_errstr[i].err_str;
}
}
return (LPSTR)"(undef)";
return "(undef)";
}

View File

@@ -65,7 +65,9 @@ extern "C" {
#endif
int SKF_print_dev_info(DEVINFO *devInfo);
char *SKF_get_alg_name(ULONG ulAlgID);
char *SKF_get_errstr(ULONG ulError);
int SKF_nid_to_encparams(int nid, ULONG *algID, BLOCKCIPHERPARAM *params);
RSA *RSA_new_from_RSAPUBLICKEYBLOB(const RSAPUBLICKEYBLOB *blob);
RSA *RSA_new_from_RSAPRIVATEKEYBLOB(const RSAPRIVATEKEYBLOB *blob);
@@ -74,6 +76,10 @@ int RSA_get_RSAPUBLICKEYBLOB(RSA *rsa, RSAPUBLICKEYBLOB *blob);
int RSA_set_RSAPRIVATEKEYBLOB(RSA *rsa, const RSAPRIVATEKEYBLOB *blob);
int RSA_get_RSAPRIVATEKEYBLOB(RSA *rsa, RSAPRIVATEKEYBLOB *blob);
ULONG DEVAPI SKF_GenExtECCKeyPair(DEVHANDLE hDev,
ECCPRIVATEKEYBLOB *priKey,
ECCPUBLICKEYBLOB *pubKey);
EC_KEY *EC_KEY_new_from_ECCPUBLICKEYBLOB(const ECCPUBLICKEYBLOB *blob);
EC_KEY *EC_KEY_new_from_ECCPRIVATEKEYBLOB(const ECCPRIVATEKEYBLOB *blob);
int EC_KEY_set_ECCPUBLICKEYBLOB(EC_KEY *ec_key, const ECCPUBLICKEYBLOB *blob);
@@ -110,6 +116,7 @@ void ERR_load_SKF_strings(void);
# define SKF_F_EC_KEY_SET_ECCPRIVATEKEYBLOB 157
# define SKF_F_EC_KEY_SET_ECCPUBLICKEYBLOB 158
# define SKF_F_RSA_GET_RSAPRIVATEKEYBLOB 169
# define SKF_F_RSA_GET_RSAPUBLICKEYBLOB 173
# define SKF_F_RSA_NEW_FROM_RSAPRIVATEKEYBLOB 163
# define SKF_F_RSA_NEW_FROM_RSAPUBLICKEYBLOB 164
# define SKF_F_RSA_SET_RSAPRIVATEKEYBLOB 170
@@ -146,6 +153,7 @@ void ERR_load_SKF_strings(void);
# define SKF_F_SKF_EXTECCVERIFY 151
# define SKF_F_SKF_EXTRSAPRIKEYOPERATION 166
# define SKF_F_SKF_EXTRSAPUBKEYOPERATION 167
# define SKF_F_SKF_GENEXTECCKEYPAIR 174
# define SKF_F_SKF_GENEXTRSAKEY 132
# define SKF_F_SKF_GENRANDOM 133
# define SKF_F_SKF_GETDEVINFO 116
@@ -153,7 +161,9 @@ void ERR_load_SKF_strings(void);
# define SKF_F_SKF_GETFILEINFO 118
# define SKF_F_SKF_GETPININFO 119
# define SKF_F_SKF_HANDLE_GET_CBCMAC_CTX 140
# define SKF_F_SKF_HANDLE_GET_CIPHER 171
# define SKF_F_SKF_HANDLE_GET_CIPHER_CTX 141
# define SKF_F_SKF_HANDLE_GET_KEY 172
# define SKF_F_SKF_HANDLE_GET_MD_CTX 142
# define SKF_F_SKF_LOCKDEV 120
# define SKF_F_SKF_MAC 168
@@ -183,11 +193,14 @@ void ERR_load_SKF_strings(void);
# define SKF_R_DECRYPT_FAILED 133
# define SKF_R_ENCODE_CIPHERTEXT_FAILED 134
# define SKF_R_ENCODE_FAILED 114
# define SKF_R_ENCODE_RSA_PUBLIC_KEY_FAILED 162
# define SKF_R_ENCODE_SIGNATURE_FAILED 135
# define SKF_R_ENCRYPT_FAILED 136
# define SKF_R_FAIL 104
# define SKF_R_FREE_HANDLE_FAILED 128
# define SKF_R_GEN_RSA_FAILED 115
# define SKF_R_GET_PRIVATE_KEY_FAILED 164
# define SKF_R_GET_PUBLIC_KEY_FAILED 165
# define SKF_R_INVALID_ALGID 122
# define SKF_R_INVALID_ALGOR 156
# define SKF_R_INVALID_APP_HANDLE 105
@@ -206,7 +219,9 @@ void ERR_load_SKF_strings(void);
# define SKF_R_INVALID_EC_KEY 151
# define SKF_R_INVALID_EC_PRIVATE_KEY 145
# define SKF_R_INVALID_EC_PUBLIC_KEY 146
# define SKF_R_INVALID_FEED_BIT_LENGTH 160
# define SKF_R_INVALID_HANDLE 107
# define SKF_R_INVALID_HANDLE_ALGOR 161
# define SKF_R_INVALID_HANDLE_MAGIC 125
# define SKF_R_INVALID_HANDLE_TYPE 126
# define SKF_R_INVALID_HASH_HANDLE 127
@@ -220,6 +235,7 @@ void ERR_load_SKF_strings(void);
# define SKF_R_INVALID_PRIVATE_KEY 152
# define SKF_R_INVALID_PUBLIC_KEY 153
# define SKF_R_INVALID_RANDOM_LENGTH 119
# define SKF_R_INVALID_RSA_KEY_LENGTH 163
# define SKF_R_INVALID_RSA_PRIVATE_KEY 158
# define SKF_R_INVALID_RSA_PUBLIC_KEY 159
# define SKF_R_INVALID_SIGNATURE 148

View File

@@ -54,21 +54,84 @@
#include <openssl/skf_ex.h>
#include "skf_lcl.h"
EVP_MD_CTX *SKF_HANDLE_get_md_ctx(SKF_HANDLE *handle)
unsigned char *SKF_HANDLE_get_key(HANDLE hKey)
{
EVP_MD_CTX *ret;
SKF_HANDLE *handle;
if (!handle) {
SKFerr(SKF_F_SKF_HANDLE_GET_MD_CTX, SKF_R_NULL_ARGUMENT);
if (!(handle = (SKF_HANDLE *)hKey)) {
SKFerr(SKF_F_SKF_HANDLE_GET_KEY, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
if (handle->magic != SKF_HANDLE_MAGIC) {
SKFerr(SKF_F_SKF_HANDLE_GET_KEY, SKF_R_INVALID_HANDLE_MAGIC);
return NULL;
}
if (handle->type < SKF_KEY_HANDLE) {
SKFerr(SKF_F_SKF_HANDLE_GET_KEY, SKF_R_INVALID_HANDLE_TYPE);
return NULL;
}
switch (handle->algid) {
case SGD_SM4_ECB:
case SGD_SM4_CBC:
case SGD_SM4_CFB:
case SGD_SM4_OFB:
case SGD_SM4_MAC:
break;
default:
SKFerr(SKF_F_SKF_HANDLE_GET_KEY, SKF_R_INVALID_ALGOR);
return NULL;
}
if (!handle->keylen) {
SKFerr(SKF_F_SKF_HANDLE_GET_KEY, SKF_R_INVALID_KEY_HANDLE);
return NULL;
}
return handle->key;
}
const EVP_CIPHER *SKF_HANDLE_get_cipher(HANDLE hKey, BLOCKCIPHERPARAM *param)
{
SKF_HANDLE *handle = (SKF_HANDLE *)hKey;
if (!SKF_HANDLE_get_key(hKey)) {
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER, SKF_R_INVALID_KEY_HANDLE);
return NULL;
}
switch (handle->algid) {
case SGD_SM4_ECB:
return EVP_sms4_ecb();
case SGD_SM4_CBC:
return EVP_sms4_cbc();
case SGD_SM4_OFB:
return EVP_sms4_ofb();
case SGD_SM4_CFB:
switch (param->FeedBitLen) {
case 1: return EVP_sms4_cfb1();
case 8: return EVP_sms4_cfb8();
case 128: return EVP_sms4_cfb128();
}
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER, SKF_R_INVALID_FEED_BIT_LENGTH);
return NULL;
}
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER, SKF_R_INVALID_HANDLE_ALGOR);
return NULL;
}
EVP_MD_CTX *SKF_HANDLE_get_md_ctx(HANDLE hHash)
{
EVP_MD_CTX *ret;
SKF_HANDLE *handle;
if (!(handle = (SKF_HANDLE *)hHash)) {
SKFerr(SKF_F_SKF_HANDLE_GET_MD_CTX, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
if (handle->magic != SKF_HANDLE_MAGIC) {
SKFerr(SKF_F_SKF_HANDLE_GET_MD_CTX, SKF_R_INVALID_HANDLE_MAGIC);
return NULL;
}
if (handle->type != SKF_HASH_HANDLE) {
SKFerr(SKF_F_SKF_HANDLE_GET_MD_CTX, SKF_R_INVALID_HANDLE_TYPE);
return NULL;
@@ -78,24 +141,22 @@ EVP_MD_CTX *SKF_HANDLE_get_md_ctx(SKF_HANDLE *handle)
SKFerr(SKF_F_SKF_HANDLE_GET_MD_CTX, SKF_R_CTX_NOT_CREATED);
return NULL;
}
return ret;
}
CBCMAC_CTX *SKF_HANDLE_get_cbcmac_ctx(SKF_HANDLE *handle)
CBCMAC_CTX *SKF_HANDLE_get_cbcmac_ctx(HANDLE hMac)
{
CBCMAC_CTX *ret;
SKF_HANDLE *handle;
if (!handle) {
SKFerr(SKF_F_SKF_HANDLE_GET_CBCMAC_CTX, SKF_R_NULL_ARGUMENT);
if (!(handle = (SKF_HANDLE *)hMac)) {
SKFerr(SKF_F_SKF_HANDLE_GET_CBCMAC_CTX, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
if (handle->magic != SKF_HANDLE_MAGIC) {
SKFerr(SKF_F_SKF_HANDLE_GET_CBCMAC_CTX, SKF_R_INVALID_HANDLE_MAGIC);
return NULL;
}
if (handle->type != SKF_MAC_HANDLE) {
SKFerr(SKF_F_SKF_HANDLE_GET_CBCMAC_CTX, SKF_R_INVALID_HANDLE_TYPE);
return NULL;
@@ -105,24 +166,22 @@ CBCMAC_CTX *SKF_HANDLE_get_cbcmac_ctx(SKF_HANDLE *handle)
SKFerr(SKF_F_SKF_HANDLE_GET_CBCMAC_CTX, SKF_R_CTX_NOT_CREATED);
return NULL;
}
return ret;
}
EVP_CIPHER_CTX *SKF_HANDLE_get_cipher_ctx(SKF_HANDLE *handle)
EVP_CIPHER_CTX *SKF_HANDLE_get_cipher_ctx(HANDLE hKey)
{
EVP_CIPHER_CTX *ret;
SKF_HANDLE *handle;
if (!handle) {
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER_CTX, SKF_R_NULL_ARGUMENT);
if (!(handle = (SKF_HANDLE *)hKey)) {
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER_CTX, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
if (handle->magic != SKF_HANDLE_MAGIC) {
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER_CTX, SKF_R_INVALID_HANDLE_MAGIC);
return NULL;
}
if (handle->type != SKF_CIPHER_HANDLE) {
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER_CTX, SKF_R_INVALID_HANDLE_TYPE);
return NULL;
@@ -132,44 +191,28 @@ EVP_CIPHER_CTX *SKF_HANDLE_get_cipher_ctx(SKF_HANDLE *handle)
SKFerr(SKF_F_SKF_HANDLE_GET_CIPHER_CTX, SKF_R_CTX_NOT_CREATED);
return NULL;
}
return ret;
}
int SKF_HANDLE_free_cipher_ctx(SKF_HANDLE *handle)
int SKF_HANDLE_free(HANDLE handle)
{
return 0;
}
int SKF_HANDLE_free(SKF_HANDLE *handle)
{
return 0;
}
unsigned char *SKF_HANDLE_get_key(SKF_HANDLE *handle)
HANDLE SKF_HANDLE_new(int type)
{
return NULL;
}
SKF_HANDLE *SKF_HANDLE_new(int type)
{
return NULL;
}
int SKF_HANDLE_set1_cipher_ctx(SKF_HANDLE *handle, EVP_CIPHER_CTX *ctx)
{
return 0;
}
ULONG DEVAPI SKF_CloseHandle(HANDLE hHandle)
{
SKF_HANDLE *handle;
return SAR_OK; //FIXME:
if (!(handle = (SKF_HANDLE *)hHandle)) {
SKFerr(SKF_F_SKF_CLOSEHANDLE, SKF_R_NULL_ARGUMENT);
return SAR_INVALIDPARAMERR;
return SAR_OK;
}
if (handle->magic != SKF_HANDLE_MAGIC) {

View File

@@ -83,15 +83,13 @@ struct SKF_HANDLE {
typedef struct SKF_HANDLE SKF_HANDLE;
SKF_HANDLE *SKF_HANDLE_new(int type);
EVP_MD_CTX *SKF_HANDLE_get_md_ctx(SKF_HANDLE *hHash);
CBCMAC_CTX *SKF_HANDLE_get_cbcmac_ctx(SKF_HANDLE *hMac);
EVP_CIPHER_CTX *SKF_HANDLE_get_cipher_ctx(SKF_HANDLE *hKey);
unsigned char *SKF_HANDLE_get_key(SKF_HANDLE *hKey);
int SKF_HANDLE_set1_md_ctx(SKF_HANDLE *hHash, EVP_MD_CTX *ctx);
int SKF_HANDLE_set1_cbcmac_ctx(SKF_HANDLE *hMac, CBCMAC_CTX *ctx);
int SKF_HANDLE_set1_cipher_ctx(SKF_HANDLE *hKey, EVP_CIPHER_CTX *ctx);
int SKF_HANDLE_free(SKF_HANDLE *handle);
EVP_MD_CTX *SKF_HANDLE_get_md_ctx(HANDLE hHash);
CBCMAC_CTX *SKF_HANDLE_get_cbcmac_ctx(HANDLE hMac);
const EVP_CIPHER *SKF_HANDLE_get_cipher(HANDLE hKey, BLOCKCIPHERPARAM *param);
EVP_CIPHER_CTX *SKF_HANDLE_get_cipher_ctx(HANDLE hKey);
unsigned char *SKF_HANDLE_get_key(HANDLE hKey);
#ifdef __cplusplus

View File

@@ -61,7 +61,6 @@ ULONG DEVAPI SKF_MacInit(HANDLE hKey,
BLOCKCIPHERPARAM *pMacParam,
HANDLE *phMac)
{
int ok = 0;
SKF_HANDLE *key;
SKF_HANDLE *hMac = NULL;
const EVP_CIPHER *cipher;
@@ -71,14 +70,14 @@ ULONG DEVAPI SKF_MacInit(HANDLE hKey,
return SAR_INVALIDPARAMERR;
}
OPENSSL_assert(!pMacParam);
//TODO: check pMacParam
if (key->magic != SKF_HANDLE_MAGIC) {
SKFerr(SKF_F_SKF_MACINIT, SKF_R_INVALID_HANDLE_MAGIC);
return SAR_INVALIDPARAMERR;
}
if (key->type <= SKF_KEY_HANDLE) {
if (key->type < SKF_KEY_HANDLE) {
SKFerr(SKF_F_SKF_MACINIT, SKF_R_INVALID_KEY_HANDLE);
return SAR_INVALIDPARAMERR;
}
@@ -142,17 +141,20 @@ ULONG DEVAPI SKF_MacFinal(HANDLE hMac,
ULONG *pulMacDataLen)
{
CBCMAC_CTX *ctx;
size_t size;
if (!(ctx = SKF_HANDLE_get_cbcmac_ctx(hMac))) {
SKFerr(SKF_F_SKF_MACFINAL, SKF_R_INVALID_MAC_HANDLE);
return SAR_INVALIDPARAMERR;
}
if (!CBCMAC_Final(ctx, pbMacData, pulMacDataLen)) {
size = *pulMacDataLen;
if (!CBCMAC_Final(ctx, pbMacData, &size)) {
SKFerr(SKF_F_SKF_MACFINAL, ERR_R_CBCMAC_LIB);
return SAR_FAIL;
}
*pulMacDataLen = (ULONG)size;
return SAR_OK;
}

View File

@@ -54,8 +54,7 @@
#include <openssl/skf.h>
#include <openssl/skf_ex.h>
#include "skf_lcl.h"
#if 0
#if 1
ULONG DEVAPI SKF_GenExtRSAKey(DEVHANDLE hDev,
ULONG ulBitsLen,
RSAPRIVATEKEYBLOB *pBlob)
@@ -226,104 +225,101 @@ ULONG DEVAPI SKF_RSAVerify(DEVHANDLE hDev,
RSA *RSA_new_from_RSAPUBLICKEYBLOB(const RSAPUBLICKEYBLOB *blob)
{
RSA *ret;
if (!(ret = RSA_new())) {
SKFerr(SKF_F_RSA_NEW_FROM_RSAPUBLICKEYBLOB, ERR_R_RSA_LIB);
return NULL;
}
if (!RSA_set_RSAPUBLICKEYBLOB(ret, blob)) {
SKFerr(SKF_F_RSA_NEW_FROM_RSAPUBLICKEYBLOB, SKF_R_INVALID_RSA_PUBLIC_KEY);
RSA_free(ret);
return NULL;
}
return ret;
}
RSA *RSA_new_from_RSAPRIVATEKEYBLOB(const RSAPRIVATEKEYBLOB *blob)
{
RSA *ret;
if (!(ret = RSA_new())) {
SKFerr(SKF_F_RSA_NEW_FROM_RSAPRIVATEKEYBLOB, ERR_R_RSA_LIB);
return NULL;
}
if (!RSA_set_RSAPRIVATEKEYBLOB(ret, blob)) {
SKFerr(SKF_F_RSA_NEW_FROM_RSAPRIVATEKEYBLOB, SKF_R_INVALID_RSA_PRIVATE_KEY);
RSA_free(ret);
return NULL;
}
return ret;
}
int RSA_set_RSAPUBLICKEYBLOB(RSA *rsa, const RSAPUBLICKEYBLOB *blob)
{
int ret = 0;
if (!rsa || !blob) {
SKFerr(SKF_F_RSA_SET_RSAPUBLICKEYBLOB, SKF_R_NULL_ARGUMENT);
SKFerr(SKF_F_RSA_SET_RSAPUBLICKEYBLOB,
ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (!(rsa->n = BN_bin2bn(blob->Modulus, blob->BitLen/8, rsa->n))) {
goto end;
if ((blob->BitLen < 1024) || (blob->BitLen > MAX_RSA_MODULUS_LEN*8) ||
(blob->BitLen / 8 != 0)) {
SKFerr(SKF_F_RSA_SET_RSAPUBLICKEYBLOB, SKF_R_INVALID_RSA_KEY_LENGTH);
return 0;
}
if (!(rsa->e = BN_bin2bn(blob->PublicExponent, blob->BitLen/8, rsa->e))) {
goto end;
if (!(rsa->n = BN_bin2bn(blob->Modulus, blob->BitLen/8, rsa->n))) {
SKFerr(SKF_F_RSA_SET_RSAPUBLICKEYBLOB, SKF_R_INVALID_RSA_PUBLIC_KEY);
return 0;
}
if (!(rsa->e = BN_bin2bn(blob->PublicExponent, MAX_RSA_EXPONENT_LEN, rsa->e))) {
SKFerr(SKF_F_RSA_SET_RSAPUBLICKEYBLOB, SKF_R_INVALID_RSA_PUBLIC_KEY);
return 0;
}
if (!RSA_check_key(rsa)) {
goto end;
SKFerr(SKF_F_RSA_SET_RSAPUBLICKEYBLOB, SKF_R_INVALID_RSA_PUBLIC_KEY);
return 0;
}
end:
return ret;
return 1;
}
int RSA_get_RSAPUBLICKEYBLOB(RSA *rsa, RSAPUBLICKEYBLOB *blob)
{
int ret = 0;
int nbytes;
if (!rsa || !blob) {
SKFerr(SKF_F_RSA_GET_RSAPUBLICKEYBLOB,
ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (!rsa->n || !rsa->e) {
goto end;
SKFerr(SKF_F_RSA_GET_RSAPUBLICKEYBLOB,
SKF_R_INVALID_RSA_PUBLIC_KEY);
return 0;
}
nbytes = BN_num_bytes(rsa->n);
if (!BN_bn2bin(rsa->n, blob->Modulus + bnlen - BN_num_bytes(rsa->n)) ||
!BN_bn2bin(rsa->e, blob->PublicExponent + bnlen - BN_num_bytes(rsa->e))) {
if (!BN_bn2bin(rsa->n, blob->Modulus) || !BN_bn2bin(rsa->e,
blob->PublicExponent + MAX_RSA_EXPONENT_LEN - BN_num_bytes(rsa->e))) {
SKFerr(SKF_F_RSA_GET_RSAPUBLICKEYBLOB,
SKF_R_ENCODE_RSA_PUBLIC_KEY_FAILED);
return 0;
}
return ret;
return 1;
}
int RSA_set_RSAPRIVATEKEYBLOB(RSA *rsa, const RSAPRIVATEKEYBLOB *blob)
{
int ret = 0;
int nbytes;
if (!rsa || !blob) {
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB, SKF_R_NULL_ARGUMENT);
return SAR_INVALIDPARAMERR;
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB,
ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (blob->AlgID != SGD_RSA) {
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB, SKR_R_INVALID_ALGOR);
return SAR_INVALIDPARAMERR;
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB, SKF_R_INVALID_ALGOR);
return 0;
}
if ((blob->BitLen < 1024) || (blob->BitLen > MAX_RSA_MODULUS_LEN*8) ||
(blob->BitLen % 8 != 0) || (blob->BitLen % 16 != 0)) {
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB, SKR_R_INVALID_KEY_LENGTH);
return SAR_INVALIDPARAMERR;
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB, SKF_R_INVALID_KEY_LENGTH);
return 0;
}
nbytes = blob->BitLen/8;
if (!(rsa->n = BN_bin2bn(blob->Modulus, nbytes, rsa->n)) ||
!(rsa->e = BN_bin2bn(blob->PublicExponent, MAX_RSA_EXPONENT_LEN, rsa->e)) ||
!(rsa->d = BN_bin2bn(blob->PrivateExponent, nbytes, rsa->d)) ||
@@ -332,26 +328,24 @@ int RSA_set_RSAPRIVATEKEYBLOB(RSA *rsa, const RSAPRIVATEKEYBLOB *blob)
!(rsa->dmp1 = BN_bin2bn(blob->Prime1Exponent, nbytes/2, rsa->dmp1)) ||
!(rsa->dmq1 = BN_bin2bn(blob->Prime2Exponent, nbytes/2, rsa->dmq1)) ||
!(rsa->iqmp = BN_bin2bn(blob->Coefficient, nbytes/2, rsa->iqmp))) {
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB, SKR_R_INVALID_RSA_PRIVATE_KEY);
return SAR_INVALIDPARAMERR;
SKFerr(SKF_F_RSA_SET_RSAPRIVATEKEYBLOB, SKF_R_INVALID_RSA_PRIVATE_KEY);
return 0;
}
return SAR_OK;
return 1;
}
int RSA_get_RSAPRIVATEKEYBLOB(RSA *rsa, RSAPRIVATEKEYBLOB *blob)
{
int nbytes;
if (!rsa || !blob) {
SKFerr(SKF_F_RSA_GET_RSAPRIVATEKEYBLOB, SKF_R_NULL_ARGUMENT);
return SAR_INVALIDPARAMERR;
SKFerr(SKF_F_RSA_GET_RSAPRIVATEKEYBLOB,
ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (!rsa->n || !rsa->e || !rsa->d || !rsa->p || !rsa->q ||
!rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
!rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
SKFerr(SKF_F_RSA_GET_RSAPRIVATEKEYBLOB, SKF_R_INVALID_RSA_PRIVATE_KEY);
return SAR_INVALIDPARAMERR;
return 0;
}
bzero(blob, sizeof(*blob));
@@ -367,11 +361,10 @@ int RSA_get_RSAPRIVATEKEYBLOB(RSA *rsa, RSAPRIVATEKEYBLOB *blob)
!BN_bn2bin(rsa->dmp1, blob->Prime1Exponent + nbytes/2 - BN_num_bytes(rsa->dmp1)) ||
!BN_bn2bin(rsa->dmq1, blob->Prime2Exponent + nbytes/2 - BN_num_bytes(rsa->dmq1)) ||
!BN_bn2bin(rsa->iqmp, blob->Coefficient + nbytes/2 - BN_num_bytes(rsa->iqmp))) {
SKFerr(SKF_F_RSA_GET_RSAPRIVATEKEYBLOB, SKF_R_INVALID_RSA_PRIVATE_KEY);
return SAR_INVALIDPARAMERR;
return 0;
}
return SAR_OK;
return 1;
}
#endif

383
crypto/skf/skftest.c Normal file
View File

@@ -0,0 +1,383 @@
/* crypto/skf/skftest.c */
/* ====================================================================
* Copyright (c) 2015-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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/skf.h>
#include <openssl/skf_ex.h>
#define PRINT_ERRSTR(rv) \
fprintf(stderr, "error: %s %d: %s\n", __FILE__, __LINE__, SKF_get_errstr(rv))
DEVHANDLE open_dev(LPSTR devName, int verbose)
{
DEVHANDLE hDev;
ULONG rv;
if ((rv = SKF_ConnectDev(devName, &hDev)) != SAR_OK) {
PRINT_ERRSTR(rv);
return NULL;
}
if (verbose > 1) {
DEVINFO devInfo;
if ((rv = SKF_GetDevInfo(hDev, &devInfo)) != SAR_OK) {
PRINT_ERRSTR(rv);
SKF_DisConnectDev(hDev);
return NULL;
}
SKF_print_dev_info(&devInfo);
}
return hDev;
}
int test_skf_mac(DEVHANDLE hDev, ULONG ulAlgID, int verbose)
{
int ret = 0;
HANDLE hKey = NULL;
HANDLE hMac = NULL;
BLOCKCIPHERPARAM param;
BYTE key[EVP_MAX_KEY_LENGTH];
BYTE data[128] = {0};
BYTE mac[EVP_MAX_MD_SIZE];
ULONG dataLen, macLen;
ULONG rv;
if ((rv = SKF_SetSymmKey(hDev, key, ulAlgID, &hKey)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
bzero(&param, sizeof(param));
param.IVLen = 0;
param.PaddingType = SKF_NO_PADDING;
if ((rv = SKF_MacInit(hKey, &param, &hMac)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
dataLen = (ULONG)sizeof(data);
macLen = (ULONG)sizeof(mac);
if ((rv = SKF_Mac(hMac, data, dataLen, mac, &macLen)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
if (macLen != 16) {
printf("macLen = %d\n", (int)macLen);
fprintf(stderr, "error: %s %d: %s\n", __FILE__, __LINE__, "mac length != 16");
goto end;
}
ret = 1;
end:
if ((rv = SKF_CloseHandle(hMac)) != SAR_OK) {
PRINT_ERRSTR(rv);
ret = 0;
}
if ((rv = SKF_CloseHandle(hKey)) != SAR_OK) {
PRINT_ERRSTR(rv);
ret = 0;
}
if (ret && verbose) {
printf("%s(%s) passed\n", __FUNCTION__, SKF_get_alg_name(ulAlgID));
}
return ret;
}
int test_skf_dgst(DEVHANDLE hDev, ULONG ulAlgID, int verbose)
{
int ret = 0;
HANDLE hHash = NULL;
BYTE data[200] = {0};
BYTE dgst[EVP_MAX_MD_SIZE];
ULONG dataLen, dgstLen;
ULONG rv;
if ((rv = SKF_DigestInit(hDev, ulAlgID, NULL, NULL, 0, &hHash)) != SAR_OK) {
PRINT_ERRSTR(rv);
return 0;
}
dataLen = (ULONG)sizeof(data);
dgstLen = (ULONG)sizeof(dgst);
if ((rv = SKF_Digest(hHash, data, dataLen, dgst, &dgstLen)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
if (verbose > 1) {
ULONG i;
printf("%s (%u-Byte) = ", SKF_get_alg_name(ulAlgID), dgstLen);
for (i = 0; i < dgstLen; i++) {
printf("%02x", dgst[i]);
}
printf("\n");
}
ret = 1;
end:
if ((rv = SKF_CloseHandle(hHash)) != SAR_OK) {
PRINT_ERRSTR(rv);
ret = 0;
}
if (ret && verbose) {
printf("%s(%s) passed\n", __FUNCTION__, SKF_get_alg_name(ulAlgID));
}
return ret;
}
int test_skf_enc(DEVHANDLE hDev, ULONG ulAlgID, BLOCKCIPHERPARAM param, int verbose)
{
int ret = 0;
HANDLE hKey = NULL;
BYTE key[EVP_MAX_KEY_LENGTH];
BYTE data[] = "message to be encrypted";
BYTE cbuf[256];
BYTE mbuf[256];
ULONG mlen, clen;
ULONG rv;
if ((rv = SKF_SetSymmKey(hDev, key, ulAlgID, &hKey)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
if ((rv = SKF_EncryptInit(hKey, param)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
mlen = (ULONG)sizeof(data);
clen = (ULONG)sizeof(cbuf);
if ((rv = SKF_Encrypt(hKey, data, mlen, cbuf, &clen)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
if ((rv = SKF_DecryptInit(hKey, param)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
mlen = (ULONG)sizeof(mbuf);
if ((rv = SKF_Decrypt(hKey, cbuf, clen, mbuf, &mlen)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
//FIXME: compare data with mbuf
if (verbose > 1) {
//FIXME: print ciphertext
}
ret = 1;
end:
if ((rv = SKF_CloseHandle(hKey)) != SAR_OK) {
PRINT_ERRSTR(rv);
ret = 0;
}
if (ret && verbose) {
//FIXME: print success info
}
return ret;
}
int test_skf_rsa(DEVHANDLE hDev, ULONG ulBitsLen, int verbose)
{
int ret = 0;
RSAPRIVATEKEYBLOB rsa;
RSAPUBLICKEYBLOB rsaPubKey;
BYTE data[] = "message to be encrypted or signed";
BYTE cbuf[512];
BYTE mbuf[256];
BYTE sig[512];
ULONG len, clen, mlen, siglen;
ULONG rv;
if ((rv = SKF_GenExtRSAKey(hDev, 2048, &rsa)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
memcpy(&rsaPubKey, &rsa, sizeof(rsaPubKey));
len = (ULONG)sizeof(data);
clen = (ULONG)sizeof(cbuf);
if ((rv = SKF_ExtRSAPubKeyOperation(hDev, &rsaPubKey, data, len, cbuf, &clen)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
mlen = (ULONG)sizeof(mbuf);
if ((rv = SKF_ExtRSAPriKeyOperation(hDev, &rsa, cbuf, clen, mbuf, &mlen)) != SAR_OK) {
PRINT_ERRSTR(rv);
goto end;
}
ret = 1;
end:
return 0;
}
int test_skf_ec(DEVHANDLE hDev, int verbose)
{
ECCPRIVATEKEYBLOB priKey;
ECCPUBLICKEYBLOB pubKey;
ECCSIGNATUREBLOB sig;
BYTE cbuf[sizeof(ECCCIPHERBLOB) + 512];
BYTE msg[] = "message to be signed and encrypted";
BYTE mbuf[128];
ULONG mlen, clen;
ULONG rv;
bzero(&priKey, sizeof(priKey));
bzero(&pubKey, sizeof(pubKey));
if ((rv = SKF_GenExtECCKeyPair(hDev, &priKey, &pubKey)) != SAR_OK) {
PRINT_ERRSTR(rv);
return 0;
}
mlen = (ULONG)sizeof(msg);
bzero(&sig, sizeof(sig));
if ((rv = SKF_ExtECCSign(hDev, &priKey, msg, mlen, &sig)) != SAR_OK) {
PRINT_ERRSTR(rv);
return 0;
}
if ((rv = SKF_ExtECCVerify(hDev, &pubKey, msg, mlen, &sig)) != SAR_OK) {
PRINT_ERRSTR(rv);
return 0;
}
mlen = (ULONG)sizeof(msg);
bzero(cbuf, sizeof(cbuf));
if ((rv = SKF_ExtECCEncrypt(hDev, &pubKey, msg, mlen, (ECCCIPHERBLOB *)cbuf)) != SAR_OK) {
PRINT_ERRSTR(rv);
return 0;
}
mlen = (ULONG)sizeof(mbuf);
bzero(mbuf, sizeof(mbuf));
if ((rv = SKF_ExtECCDecrypt(hDev, &priKey, (ECCCIPHERBLOB *)cbuf, mbuf, &mlen)) != SAR_OK) {
PRINT_ERRSTR(rv);
return 0;
}
return 1;
}
int main(int argc, char **argv)
{
int verbose = 2;
DEVHANDLE hDev = NULL;
ULONG digestAlgors[] = {
SGD_SM3,
SGD_SHA1,
SGD_SHA256
};
ULONG cipherAlgors[] = {
SGD_SM4_ECB,
SGD_SM4_CBC,
SGD_SM4_CFB,
SGD_SM4_CFB,
SGD_SM4_CFB,
SGD_SM4_OFB
};
BLOCKCIPHERPARAM cipherParams[] = {
{{0}, 0, SKF_NO_PADDING, 0},
{{0}, 16, SKF_PKCS5_PADDING, 0},
{{0}, 16, SKF_NO_PADDING, 1},
{{0}, 16, SKF_NO_PADDING, 8},
{{0}, 16, SKF_NO_PADDING, 128},
{{0}, 16, SKF_NO_PADDING, 0},
};
ULONG rsaBits[] = { 1024, 2048 };
int i;
hDev = open_dev((LPSTR)"name", verbose);
/*
if (!test_skf_mac(hDev, SGD_SM4_MAC, verbose)) {
goto end;
}
*/
for (i = 0; i < sizeof(digestAlgors)/sizeof(digestAlgors[0]); i++) {
if (!test_skf_dgst(hDev, digestAlgors[i], verbose)) {
goto end;
}
}
for (i = 0; i < sizeof(cipherAlgors)/sizeof(cipherAlgors[0]); i++) {
if (!test_skf_enc(hDev, cipherAlgors[i], cipherParams[i], verbose)) {
goto end;
}
}
for (i = 0; i < sizeof(rsaBits)/sizeof(rsaBits[0]); i++) {
if (!test_skf_rsa(hDev, test_skf_rsa(hDev, rsaBits[i], verbose))) {
goto end;
}
}
if (!test_skf_ec(hDev, verbose)) {
goto end;
}
end:
ERR_print_errors_fp(stderr);
SKF_DisConnectDev(hDev);
return -1;
}

View File

@@ -103,8 +103,8 @@ sm2_enc.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
sm2_enc.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
sm2_enc.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
sm2_enc.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
sm2_enc.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h sm2.h
sm2_enc.o: sm2_enc.c
sm2_enc.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
sm2_enc.o: ../o_str.h sm2_enc.c
sm2_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
sm2_err.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
sm2_err.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h

View File

@@ -228,6 +228,7 @@ void ERR_load_SM2_strings(void);
# define SM2_F_SM2_CIPHERTEXT_VALUE_PRINT 103
# define SM2_F_SM2_CIPHERTEXT_VALUE_SIZE 104
# define SM2_F_SM2_COMPUTE_ID_DIGEST 105
# define SM2_F_SM2_COMPUTE_ID_DIGEST_EX 127
# define SM2_F_SM2_DECRYPT 106
# define SM2_F_SM2_DO_DECRYPT 107
# define SM2_F_SM2_DO_ENCRYPT 108

View File

@@ -79,6 +79,7 @@ static ERR_STRING_DATA SM2_str_functs[] = {
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_PRINT), "SM2_CIPHERTEXT_VALUE_print"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_SIZE), "SM2_CIPHERTEXT_VALUE_size"},
{ERR_FUNC(SM2_F_SM2_COMPUTE_ID_DIGEST), "SM2_compute_id_digest"},
{ERR_FUNC(SM2_F_SM2_COMPUTE_ID_DIGEST_EX), "SM2_COMPUTE_ID_DIGEST_EX"},
{ERR_FUNC(SM2_F_SM2_DECRYPT), "SM2_decrypt"},
{ERR_FUNC(SM2_F_SM2_DO_DECRYPT), "SM2_do_decrypt"},
{ERR_FUNC(SM2_F_SM2_DO_ENCRYPT), "SM2_do_encrypt"},

View File

@@ -58,7 +58,7 @@ int SM2_KAP_CTX_init(SM2_KAP_CTX *ctx, EC_KEY *ec_key,
{
int ret = 0;
int w;
memset(ctx, 0, sizeof(*ctx));
ctx->id_dgst_md = EVP_sm3();
@@ -90,7 +90,7 @@ int SM2_KAP_CTX_init(SM2_KAP_CTX *ctx, EC_KEY *ec_key,
SM2err(SM2_F_SM2_KAP_CTX_INIT, ERR_R_EC_LIB);
goto end;
}
if (!SM2_compute_id_digest(ctx->id_dgst_md, ctx->remote_id_dgst,
&ctx->remote_id_dgstlen, remote_pubkey)) {
SM2err(SM2_F_SM2_KAP_CTX_INIT, 0);
@@ -155,97 +155,6 @@ void SM2_KAP_CTX_cleanup(SM2_KAP_CTX *ctx)
memset(ctx, 0, sizeof(*ctx));
}
#if 0
int SM2_update_key(EC_KEY *ec_key, EC_POINT **point)
{
EC_KEY *tmp = NULL;
BIGNUM *d = EC_KEY_get0_private_key(ec_key);
if (!(tmp = EC_KEY_new())) {
goto end;
}
if (!EC_KEY_set_group(tmp, EC_KEY_get0_group(ec_key))) {
goto end;
}
if (!EC_KEY_generate_key(tmp)) {
goto end;
}
if (!EC_KEY_get_affine_coordinates(tmp, x, y)) {
goto end;
}
/* convert x to x' */
if (**point == NULL) {
*point = EC_POINT_dup(EC_KEY_get0_public_key(ec_key), EC_KEY_get0_group(ec_key));
} else {
EC_POINT_copy(*point, EC_KEY_get0_public_key(ec_key), EC_KEY_get0_group(ec_key));
}
end:
EC_KEY_free(tmp);
return 0;
}
int SM2_update_public_key(EC_KEY *ec_key, const EC_POINT *pub_key)
{
EC_GROUP *group;
group = EC_KEY_get0_group(ec_key);
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(group, pub_key, x, NULL, bn_ctx)) {
goto end;
}
} else {
if (!EC_POINT_get_affine_coordinates_GF2m(group, pub_key, x, NULL, bn_ctx)) {
goto end;
}
}
if (!BN_nnmod(x, x, ctx->two_pow_w, bn_ctx)) {
goto end;
}
if (!BN_add(x, x, ctx->two_pow_w)) {
goto end;
}
if (!BN_mod_mul(ctx->t, x, r, ctx->order, ctx->bn_ctx)) {
goto end;
}
if (!EC_POINT_mul(group, point, NULL, point, x, ctx->bn_ctx)) {
goto end;
}
if (!EC_POINT_add(group, pubkey, pubkey, point, bn_ctx)) {
goto end;
}
ret = 1;
end:
return ret;
}
int SM2_derive_key(void *out, size_t outlen,
const EC_POINT *pub_key, EC_KEY *ec_key,
void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen))
{
return 0;
}
#endif
/* FIXME: ephem_point_len should be both input and output */
int SM2_KAP_prepare(SM2_KAP_CTX *ctx, unsigned char *ephem_point,
size_t *ephem_point_len)
@@ -306,7 +215,7 @@ int SM2_KAP_prepare(SM2_KAP_CTX *ctx, unsigned char *ephem_point,
* w = ceil(keybits / 2) - 1
* x = 2^w + (x and (2^w - 1)) = 2^w + (x mod 2^w)
* t = (d + x * r) mod n
* t = (h * t) mod n
* t = (h * t) mod n
*/
if (!ctx->t) {
@@ -377,7 +286,7 @@ int SM2_KAP_compute_key(SM2_KAP_CTX *ctx, const unsigned char *remote_point,
unsigned int len, bnlen;
size_t klen = keylen;
EVP_MD_CTX_init(&md_ctx);
if (!(x = BN_new())) {

View File

@@ -199,6 +199,14 @@ err:
return ret;
}
int SM2_compute_id_digest_ex(const EVP_MD *md,
unsigned char *dgst, size_t dgstlen,
const char *id, size_t idlen, EC_KEY *ec_key)
{
SM2err(SM2_F_SM2_COMPUTE_ID_DIGEST_EX, ERR_R_SM2_LIB);
return 0;
}
//TODO: review this function again.
int SM2_compute_id_digest(const EVP_MD *md, unsigned char *dgst,
unsigned int *dgstlen, EC_KEY *ec_key)
@@ -209,7 +217,7 @@ int SM2_compute_id_digest(const EVP_MD *md, unsigned char *dgst,
unsigned char idbits[2];
int pkdatalen;
char *id;
if ((pkdatalen = sm2_get_public_key_data(pkdata, ec_key)) < 0) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto err;
@@ -272,7 +280,7 @@ int SM2_compute_message_digest(const EVP_MD *id_md, const EVP_MD *msg_md,
goto err;
}
if (!SM2_compute_id_digest(id_md, buf, &len, ec_key)) {
if (!SM2_compute_id_digest(id_md, buf, &len, ec_key)) {
goto err;
}
@@ -300,7 +308,7 @@ int SM2_digest(const void *msg, size_t msglen, unsigned char *dgst,
{
const EVP_MD *id_md = EVP_sm3();
const EVP_MD *msg_md = EVP_sm3();
return SM2_compute_message_digest(id_md, msg_md,
msg, msglen, dgst, dgstlen, ec_key);
}

View File

@@ -22,8 +22,8 @@ TEST=sm3test.c
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC=sm3.c
LIBOBJ=sm3.o
LIBSRC=sm3.c sm3_hmac.c
LIBOBJ=sm3.o sm3_hmac.o
SRC= $(LIBSRC)
@@ -66,6 +66,8 @@ tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
@@ -79,4 +81,5 @@ clean:
# DO NOT DELETE THIS LINE -- make depend depends on it.
sm3.o: ../byteorder.h sm3.c sm3.h
sm3.o: ../../include/openssl/sm3.h ../byteorder.h sm3.c
sm3_hmac.o: ../../include/openssl/sm3.h sm3_hmac.c

66
crypto/sm3/README.md Normal file
View File

@@ -0,0 +1,66 @@
## SM3 Sub-library of GMSSL
SM3 Cryptographic Hash Algorithm is a chinese national cryptographic hash
algorithm standard published by the State Cryptography Administration Office
of Security Commercial Code Administration (OSCCA) of China in December 2010.
A draft of this algorithm can be found at
[http://tools.ietf.org/html/draft-shen-sm3-hash-00](http://tools.ietf.org/html/draft-shen-sm3-hash-00 "RFC Draft")
The SM3 take input messages as 512 bits blocks and generates
256 bits digest values, same as SHA-256.
The `SM3` sub-library of GmSSL provides the implementation of SM3 hash
algorithm, with init/update/final style of interfaces. There is also a
demo program in `demo/gmssl/sm3.c` on how to implement a command line
tool with the the inner API of SM3 sub-library.
### Usage
The SM3 sub-library provides the following C API:
```
void sm3_init(sm3_ctx_t *ctx);
void sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len);
void sm3_final(sm3_ctx_t *ctx, unsigned char digest[SM3_DIGEST_LENGTH]);
void sm3_compress(uint32_t digest[8], const unsigned char block[SM3_BLOCK_SIZE]);
void sm3(const unsigned char *data, size_t datalen, unsigned char digest[SM3_DIGEST_LENGTH]);
void sm3_hmac_init(sm3_hmac_ctx_t *ctx, const unsigned char *key, size_t key_len);
void sm3_hmac_update(sm3_hmac_ctx_t *ctx, const unsigned char *data, size_t data_len);
void sm3_hmac_final(sm3_hmac_ctx_t *ctx, unsigned char mac[sm3_hmac_MAC_SIZE]);
void sm3_hmac(const unsigned char *data, size_t data_len,
const unsigned char *key, size_t key_len, unsigned char mac[sm3_hmac_MAC_SIZE]);
```
Example on using C API to digest a message:
```
unsigend char buffer[SM3_DIGEST_LENGTH];
sm3("hello", strlen("hello"), buffer);
```
Example on using C API to digest a stream:
```
unsigned char dgst[SM3_DIGEST_LENGTH];
sm3_ctx_t ctx;
sm3_init(&ctx);
sm3_update(&ctx, "hello", strlen("hello"));
sm3_update(&ctx, "world", strlen("world"));
sm3_final(&ctx, dgst);
```
Example on using C API to generate a HMAC tag:
```
unsigned char mac[sm3_hmac_MAC_SIZE];
sm3_hmac_ctx_t ctx;
unsigned char key[16];
sm3_hmac_init(&ctx, key, sizeof(key));
sm3_hmac_update(&ctx, "hello", strlen("hello"));
sm3_hmac_update(&ctx, "world", strlen("world"));
sm3_hmac_final(&ctx, mac);
```

View File

@@ -1,78 +0,0 @@
#include <string.h>
#include <assert.h>
#include "hmac_sm3.h"
/**
* HMAC_k(m) = H((k ^ opad), H((k ^ ipad), m))
* pseudo-code:
* function hmac(key, message)
* opad = [0x5c * blocksize]
* ipad = [0x36 * blocksize]
* if (length(key) > blocksize) then
* key = hash(key)
* end if
* for i from 0 to length(key) - 1 step 1
* ipad[i] = ipad[i] XOR key[i]
* opad[i] = opad[i] XOR key[i]
* end for
* return hash(opad || hash(ipad || message))
* end function
*/
#define IPAD 0x36
#define OPAD 0x5C
void hmac_sm3_init(hmac_sm3_ctx_t *ctx, const unsigned char *key, size_t key_len)
{
int i;
unsigned char ipad[SM3_DIGEST_LENGTH];
if (key_len <= SM3_BLOCK_SIZE) {
memcpy(ctx->key, key, key_len);
memset(ctx->key + key_len, 0, SM3_BLOCK_SIZE - key_len);
} else {
sm3_init(&ctx->sm3_ctx);
sm3_update(&ctx->sm3_ctx, key, key_len);
sm3_final(&ctx->sm3_ctx, ctx->key);
memset(ctx->key + SM3_DIGEST_LENGTH, 0,
SM3_BLOCK_SIZE - SM3_DIGEST_LENGTH);
}
for (i = 0; i < SM3_BLOCK_SIZE; i++) {
ctx->key[i] ^= IPAD;
}
sm3_init(&ctx->sm3_ctx);
sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE);
}
void hmac_sm3_update(hmac_sm3_ctx_t *ctx, const unsigned char *data, size_t data_len)
{
sm3_update(&ctx->sm3_ctx, data, data_len);
}
void hmac_sm3_final(hmac_sm3_ctx_t *ctx, unsigned char mac[HMAC_SM3_MAC_SIZE])
{
int i;
for (i = 0; i < SM3_BLOCK_SIZE; i++) {
ctx->key[i] ^= (IPAD ^ OPAD);
}
sm3_final(&ctx->sm3_ctx, mac);
sm3_init(&ctx->sm3_ctx);
sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE);
sm3_update(&ctx->sm3_ctx, mac, SM3_DIGEST_LENGTH);
sm3_final(&ctx->sm3_ctx, mac);
}
void hmac_sm3(const unsigned char *data, size_t data_len,
const unsigned char *key, size_t key_len, unsigned char mac[HMAC_SM3_MAC_SIZE])
{
hmac_sm3_ctx_t ctx;
hmac_sm3_init(&ctx, key, key_len);
hmac_sm3_update(&ctx, data, data_len);
hmac_sm3_final(&ctx, mac);
memset(&ctx, 0, sizeof(hmac_sm3_ctx_t));
}

View File

@@ -1,29 +0,0 @@
#ifndef LIBSM3_HMAC_SM3_H
#define LIBSM3_HMAC_SM3_H
#include "sm3.h"
#define HMAC_SM3_MAC_SIZE SM3_DIGEST_LENGTH
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
sm3_ctx_t sm3_ctx;
unsigned char key[SM3_DIGEST_LENGTH];
} hmac_sm3_ctx_t;
void hmac_sm3_init(hmac_sm3_ctx_t *ctx, const unsigned char *key, size_t key_len);
void hmac_sm3_update(hmac_sm3_ctx_t *ctx, const unsigned char *data, size_t data_len);
void hmac_sm3_final(hmac_sm3_ctx_t *ctx, unsigned char mac[HMAC_SM3_MAC_SIZE]);
void hmac_sm3(const unsigned char *data, size_t data_len,
const unsigned char *key, size_t key_len, unsigned char mac[HMAC_SM3_MAC_SIZE]);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,68 +0,0 @@
#include "hmac_sm3.h"
#include <stdio.h>
#include <string.h>
int hmac_sm3_test()
{
int ret = 0;
int i, j;
unsigned char mac[HMAC_SM3_MAC_SIZE];
hmac_sm3_ctx_t ctx;
char *testarray[4] = {
"abc",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"a",
"01234567012345670123456701234567"
};
int repeatcount[4] = { 1, 1, 1000000, 20 };
unsigned char key[4] = {
"hello",
"world",
"23492304982304982340923480",
"a"
};
unsigned char result[4][32] = {
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
};
for (i = 0; i < sizeof(testarray)/sizeof(testarray[0]); i++) {
hmac_sm3_init(&ctx, key[i], key_length[i]);
for (j = 0; j < repeatcount[i]; j++) {
hmac_sm3_update(&ctx, (const unsigned char *)testarray[i],
strlen(testarray[i]));
}
hmac_sm3_final(&ctx, mac);
if (memcmp(mac, &result[i][0], sizeof(mac)) != 0) {
fprintf(stderr, "hmac-sm3 test-%d failed\n", i);
ret = 1;
}
}
if (ret == 0) {
printf("hmac-sm3 test success!\n");
}
return ret;
}
int main()
{
return hmac_sm3_test();
}

View File

@@ -49,13 +49,11 @@
*
*/
#include "sm3.h"
#include "../byteorder.h"
#include <string.h>
#include "../byteorder.h"
#include <openssl/sm3.h>
int sm3_init(sm3_ctx_t *ctx)
void sm3_init(sm3_ctx_t *ctx)
{
ctx->digest[0] = 0x7380166F;
ctx->digest[1] = 0x4914B2B9;
@@ -65,22 +63,19 @@ int sm3_init(sm3_ctx_t *ctx)
ctx->digest[5] = 0x163138AA;
ctx->digest[6] = 0xE38DEE4D;
ctx->digest[7] = 0xB0FB0E4E;
ctx->nblocks = 0;
ctx->num = 0;
if(ctx == NULL) return 0;
return 1;
}
int sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len)
void sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len)
{
if(ctx == NULL) return 0;
if (ctx->num) {
unsigned int left = SM3_BLOCK_SIZE - ctx->num;
if (data_len < left) {
memcpy(ctx->block + ctx->num, data, data_len);
ctx->num += data_len;
return 1;
return;
} else {
memcpy(ctx->block + ctx->num, data, left);
sm3_compress(ctx->digest, ctx->block);
@@ -99,18 +94,16 @@ int sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len)
if (data_len) {
memcpy(ctx->block, data, data_len);
}
return 1;
}
int sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
void sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
{
if(ctx == NULL) return 0;
int i;
uint32_t *pdigest = (uint32_t *)digest;
uint32_t *count = (uint32_t *)(ctx->block + SM3_BLOCK_SIZE - 8);
ctx->block[ctx->num] = 0x80;
if (ctx->num + 9 <= SM3_BLOCK_SIZE) {
memset(ctx->block + ctx->num + 1, 0, SM3_BLOCK_SIZE - ctx->num - 9);
} else {
@@ -121,23 +114,22 @@ int sm3_final(sm3_ctx_t *ctx, unsigned char *digest)
count[0] = cpu_to_be32((ctx->nblocks) >> 23);
count[1] = cpu_to_be32((ctx->nblocks << 9) + (ctx->num << 3));
sm3_compress(ctx->digest, ctx->block);
for (i = 0; i < sizeof(ctx->digest)/sizeof(ctx->digest[0]); i++) {
pdigest[i] = cpu_to_be32(ctx->digest[i]);
}
return 1;
}
#define ROTATELEFT(X,n) (((X)<<(n)) | ((X)>>(32-(n))))
#define P0(x) ((x) ^ ROTATELEFT((x),9) ^ ROTATELEFT((x),17))
#define P1(x) ((x) ^ ROTATELEFT((x),15) ^ ROTATELEFT((x),23))
#define P0(x) ((x) ^ ROTATELEFT((x),9) ^ ROTATELEFT((x),17))
#define P1(x) ((x) ^ ROTATELEFT((x),15) ^ ROTATELEFT((x),23))
#define FF0(x,y,z) ( (x) ^ (y) ^ (z))
#define FF0(x,y,z) ( (x) ^ (y) ^ (z))
#define FF1(x,y,z) (((x) & (y)) | ( (x) & (z)) | ( (y) & (z)))
#define GG0(x,y,z) ( (x) ^ (y) ^ (z))
#define GG0(x,y,z) ( (x) ^ (y) ^ (z))
#define GG1(x,y,z) (((x) & (y)) | ( (~(x)) & (z)) )
@@ -146,7 +138,7 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
int j;
uint32_t W[68], W1[64];
const uint32_t *pblock = (const uint32_t *)block;
uint32_t A = digest[0];
uint32_t B = digest[1];
uint32_t C = digest[2];
@@ -170,7 +162,7 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
for(j =0; j < 16; j++) {
T[j] = 0x79CC4519;
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
SS2 = SS1 ^ ROTATELEFT(A,12);
TT1 = FF0(A,B,C) + D + SS2 + W1[j];
TT2 = GG0(E,F,G) + H + SS1 + W[j];
@@ -187,7 +179,7 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
for(j =16; j < 64; j++) {
T[j] = 0x7A879D8A;
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
SS1 = ROTATELEFT((ROTATELEFT(A,12) + E + ROTATELEFT(T[j],j)), 7);
SS2 = SS1 ^ ROTATELEFT(A,12);
TT1 = FF1(A,B,C) + D + SS2 + W1[j];
TT2 = GG1(E,F,G) + H + SS1 + W[j];
@@ -211,7 +203,8 @@ void sm3_compress(uint32_t digest[8], const unsigned char block[64])
digest[7] ^= H;
}
void sm3(const unsigned char *msg, size_t msglen, unsigned char dgst[SM3_DIGEST_LENGTH])
void sm3(const unsigned char *msg, size_t msglen,
unsigned char dgst[SM3_DIGEST_LENGTH])
{
sm3_ctx_t ctx;

View File

@@ -54,6 +54,7 @@
#define SM3_DIGEST_LENGTH 32
#define SM3_BLOCK_SIZE 64
#define SM3_HMAC_SIZE (SM3_DIGEST_LENGTH)
#include <sys/types.h>
#include <stdint.h>
@@ -71,11 +72,24 @@ typedef struct {
int num;
} sm3_ctx_t;
int sm3_init(sm3_ctx_t *ctx);
int sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len);
int sm3_final(sm3_ctx_t *ctx, unsigned char digest[SM3_DIGEST_LENGTH]);
void sm3_init(sm3_ctx_t *ctx);
void sm3_update(sm3_ctx_t *ctx, const unsigned char* data, size_t data_len);
void sm3_final(sm3_ctx_t *ctx, unsigned char digest[SM3_DIGEST_LENGTH]);
void sm3_compress(uint32_t digest[8], const unsigned char block[SM3_BLOCK_SIZE]);
void sm3(const unsigned char *data, size_t datalen, unsigned char digest[SM3_DIGEST_LENGTH]);
void sm3(const unsigned char *data, size_t datalen,
unsigned char digest[SM3_DIGEST_LENGTH]);
typedef struct {
sm3_ctx_t sm3_ctx;
unsigned char key[SM3_DIGEST_LENGTH];
} sm3_hmac_ctx_t;
void sm3_hmac_init(sm3_hmac_ctx_t *ctx, const unsigned char *key, size_t key_len);
void sm3_hmac_update(sm3_hmac_ctx_t *ctx, const unsigned char *data, size_t data_len);
void sm3_hmac_final(sm3_hmac_ctx_t *ctx, unsigned char mac[SM3_HMAC_SIZE]);
void sm3_hmac(const unsigned char *data, size_t data_len,
const unsigned char *key, size_t key_len, unsigned char mac[SM3_HMAC_SIZE]);
#ifdef __cplusplus
}

View File

@@ -1,6 +1,6 @@
/* crypto/evp/m_btc.c */
/* crypto/sm3/sm3_hmac.c */
/* ====================================================================
* Copyright (c) 2014 - 2016 The GmSSL Project. All rights reserved.
* Copyright (c) 2014 - 2015 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
@@ -46,72 +46,84 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
/*
* This module is to support crypto-currency such as Bitcoin
#include <string.h>
#include <assert.h>
#include <strings.h>
#include <openssl/sm3.h>
/**
* HMAC_k(m) = H((k ^ opad), H((k ^ ipad), m))
* pseudo-code:
* function hmac(key, message)
* opad = [0x5c * blocksize]
* ipad = [0x36 * blocksize]
* if (length(key) > blocksize) then
* key = hash(key)
* end if
* for i from 0 to length(key) - 1 step 1
* ipad[i] = ipad[i] XOR key[i]
* opad[i] = opad[i] XOR key[i]
* end for
* return hash(opad || hash(ipad || message))
* end function
*/
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_GMSSL
#define IPAD 0x36
#define OPAD 0x5C
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/sha.h>
#include <openssl/ripemd.h>
static int init(EVP_MD_CTX *ctx)
void sm3_hmac_init(sm3_hmac_ctx_t *ctx, const unsigned char *key, size_t key_len)
{
SHA256_Init(ctx->md_data->sha256);
RIPEMD_Init(ctx->md_data->rmd160);
int i;
//unsigned char ipad[SM3_DIGEST_LENGTH];
if (key_len <= SM3_BLOCK_SIZE) {
memcpy(ctx->key, key, key_len);
memset(ctx->key + key_len, 0, SM3_BLOCK_SIZE - key_len);
} else {
sm3_init(&ctx->sm3_ctx);
sm3_update(&ctx->sm3_ctx, key, key_len);
sm3_final(&ctx->sm3_ctx, ctx->key);
memset(ctx->key + SM3_DIGEST_LENGTH, 0,
SM3_BLOCK_SIZE - SM3_DIGEST_LENGTH);
}
for (i = 0; i < SM3_BLOCK_SIZE; i++) {
ctx->key[i] ^= IPAD;
}
sm3_init(&ctx->sm3_ctx);
sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE);
}
static int update(EVP_MD_CTX *ctx, const void *in, size_t inlen)
void sm3_hmac_update(sm3_hmac_ctx_t *ctx,
const unsigned char *data, size_t data_len)
{
SHA256_Update(ctx, in, inlen);
sm3_update(&ctx->sm3_ctx, data, data_len);
}
static int final(EVP_MD_CTX *ctx, unsigned char *md)
void sm3_hmac_final(sm3_hmac_ctx_t *ctx, unsigned char mac[SM3_HMAC_SIZE])
{
return sm3_final(ctx->md_data, md);
int i;
for (i = 0; i < SM3_BLOCK_SIZE; i++) {
ctx->key[i] ^= (IPAD ^ OPAD);
}
sm3_final(&ctx->sm3_ctx, mac);
sm3_init(&ctx->sm3_ctx);
sm3_update(&ctx->sm3_ctx, ctx->key, SM3_BLOCK_SIZE);
sm3_update(&ctx->sm3_ctx, mac, SM3_DIGEST_LENGTH);
sm3_final(&ctx->sm3_ctx, mac);
}
static const EVP_MD sm3_md = {
NID_btchash,
NID_sm2sign_with_sm3,
SM3_DIGEST_LENGTH,
0,
init,
update,
final,
NULL,
NULL,
(evp_sign_method *)SM2_sign,
(evp_verify_method *)SM2_verify,
{EVP_PKEY_EC, 0, 0, 0},
SM3_BLOCK_SIZE,
sizeof(EVP_MD *) + sizeof(sm3_ctx_t),
};
const EVP_MD *EVP_btc160(void)
void sm3_hmac(const unsigned char *data, size_t data_len,
const unsigned char *key, size_t key_len,
unsigned char mac[SM3_HMAC_SIZE])
{
return &btc160_md;
sm3_hmac_ctx_t ctx;
sm3_hmac_init(&ctx, key, key_len);
sm3_hmac_update(&ctx, data, data_len);
sm3_hmac_final(&ctx, mac);
bzero(&ctx, sizeof(ctx));
}
const EVP_MD *EVP_btc256(void)
{
return &btc256_md;
}
const EVP_MD *EVP_sm3_rmd160(void)
{
return 0;
}
#endif

View File

@@ -1,46 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include "sm3.h"
/*
* usage of sm3dgst:
* ./sm3dgst <file>
* 324234234234235234234234234234
*
* echo "hello world" | sm3dgst
* lksjdlfksdjlfkjsdlfkjsdlfkjsdljkfffffffldjfk=
*
*/
int main(int argc, char **argv)
{
sm3_ctx_t ctx;
unsigned char dgst[SM3_DIGEST_LENGTH];
unsigned char buf[4096];
ssize_t len;
int i;
if (argc > 1) {
printf("usage: %s < file\n", basename(argv[0]));
return 0;
}
sm3_init(&ctx);
while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
sm3_update(&ctx, buf, len);
}
memset(dgst, 0, sizeof(dgst));
sm3_final(&ctx, dgst);
for (i = 0; i < sizeof(dgst); i++) {
printf("%02x", dgst[i]);
}
printf("\n");
return 0;
}

View File

@@ -120,26 +120,4 @@ static char *pt(unsigned char *md)
return (buf);
}
/*
*/
#endif

View File

@@ -82,6 +82,65 @@ int sm3_test2()
return 0;
}
int hmac_sm3_test()
{
int ret = 0;
int i, j;
unsigned char mac[HMAC_SM3_MAC_SIZE];
hmac_sm3_ctx_t ctx;
char *testarray[4] = {
"abc",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"a",
"01234567012345670123456701234567"
};
int repeatcount[4] = { 1, 1, 1000000, 20 };
unsigned char key[4] = {
"hello",
"world",
"23492304982304982340923480",
"a"
};
unsigned char result[4][32] = {
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0},
};
for (i = 0; i < sizeof(testarray)/sizeof(testarray[0]); i++) {
hmac_sm3_init(&ctx, key[i], key_length[i]);
for (j = 0; j < repeatcount[i]; j++) {
hmac_sm3_update(&ctx, (const unsigned char *)testarray[i],
strlen(testarray[i]));
}
hmac_sm3_final(&ctx, mac);
if (memcmp(mac, &result[i][0], sizeof(mac)) != 0) {
fprintf(stderr, "hmac-sm3 test-%d failed\n", i);
ret = 1;
}
}
if (ret == 0) {
printf("hmac-sm3 test success!\n");
}
return ret;
}
int main(int argc, char *argv[])
{
if (sm3_test1())

View File

@@ -66,6 +66,8 @@ tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
@@ -88,3 +90,12 @@ sm9_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
sm9_err.o: ../../include/openssl/safestack.h ../../include/openssl/sm9.h
sm9_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
sm9_err.o: sm9_err.c
sm9_lib.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
sm9_lib.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
sm9_lib.o: ../../include/openssl/err.h ../../include/openssl/evp.h
sm9_lib.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
sm9_lib.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
sm9_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
sm9_lib.o: ../../include/openssl/safestack.h ../../include/openssl/sm9.h
sm9_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h sm9.h
sm9_lib.o: sm9_lcl.h sm9_lib.c

View File

@@ -49,6 +49,7 @@
*
*/
#include <openssl/err.h>
#include <openssl/sm9.h>
#include "sm9_lcl.h"
@@ -104,7 +105,7 @@ int SM9_encrypt(SM9_PUBLIC_PARAMS *params,
const char *id, size_t idlen)
{
SM9err(SM9_F_SM9_ENCRYPT, SM9_R_NOT_IMPLEMENTED);
return NULL;
return 0;
}
int SM9_decrypt(SM9_PUBLIC_PARAMS *params,

View File

@@ -61,6 +61,8 @@ tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
update: depend
depend:
@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)

22
crypto/sms4/README.md Normal file
View File

@@ -0,0 +1,22 @@
## SMS4 Sub-Library of GMSSL
Encryption modes:
* ECB: encrypt or decrypt a block, process only one block per call.
* CBC: process variable length input with zero padding, IV works as both input and output.
* CFB: same as CBC
* OFB: same as CBC
Optimization for specific architecture:
* AVX2: support ECB, CTR and CBC-decrypt (todo)
* KNC-NI: support ECB, CTR and CBC-decrypt (todo)
Some future plans:
* more operation modes, GCM, XTS, FFX, OFB ...
* more optimiazations: ARM/NEON, X86 ASM, GPU, ...
* secure implementation: Bit Slicing, Timing attack ...
* formally verified.

View File

@@ -48,10 +48,10 @@ int main(int argc, char **argv)
goto end;
}
printf("sms4 key scheduling passed!\n");
/* test encrypt once */
sms4_encrypt(&key, plaintext, buf);
if (memcmp(buf, ciphertext1, sizeof(ciphertext1)) != 0) {
printf("sms4 encrypt not pass!\n");
goto end;
@@ -70,7 +70,7 @@ int main(int argc, char **argv)
}
printf("sms4 encrypt 1000000 times pass!\n");
printf("sms4 all test vectors pass!\n");
return 0;
end:
printf("some test vector failed\n");

View File

@@ -91,7 +91,7 @@
# include <openssl/ecdh.h>
# endif
# ifndef OPENSSL_NO_SM2
# ifndef NO_GMSSL
# include <openssl/sm2.h>
# endif

View File

@@ -116,9 +116,6 @@ int X509_certificate_type(X509 *x, EVP_PKEY *pkey)
case NID_X9_62_id_ecPublicKey:
ret |= EVP_PKS_EC;
break;
#ifndef OPENSSL_NO_SM2
/* what to do ? */
#endif
default:
break;
}

View File

@@ -1,250 +1,70 @@
#include <assert.h>
#include "../modes/modes_lcl.h"
#include "zuc.h"
/* crypto/zuc/zuc.c */
/* ====================================================================
* Copyright (c) 2015 - 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.
* ====================================================================
*
*/
/*
* this file includes code written by YOU for the GmSSL project
*/
#include <string.h>
#include <openssl/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)
void zuc_set_key(zuc_key_t *key, const unsigned char *user_key, const unsigned char *iv)
{
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)
void zuc_generate_keystream(zuc_key_t *key, size_t nwords, uint32_t *words)
{
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)
void zuc_encrypt(zuc_key_t *key, size_t len, const unsigned char *in, unsigned char *out)
{
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;
memcpy(out, in, len);
}
/* 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;
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]);
key->F_R1 = 0;
key->F_R2 = 0;
nCount = 32;
while (nCount > 0) {
BitReorganization(key);
w = F(key);
LFSRWithInitialisationMode(key, w >> 1);
nCount--;
}
BitReorganization(key);
F(key);
LFSRWithWorkMode(key);
}
void ZUC_encrypt(ZUC_KEY *key, size_t inlen, const unsigned char *in, unsigned char *out)
{
#if 0
uint32_t word;
int n = key->buf_index;
assert(n < 4);
while (n && inlen) {
*(out++) = *(in++) ^ key->buf[n];
n = (n + 1) % 4;
inlen--;
}
while (inlen >= 4) {
BitReorganization(key);
word = GETU32(in);
word ^= F(key) ^ key->BRC_X3;
PUTU32(out, word);
LFSRWithWorkMode(key);
inlen -= 4;
in += 4;
out += 4;
}
if (inlen) {
BitReorganization(key);
word = F(key) ^ key->BRC_X3;
LFSRWithWorkMode(key);
PUTU32(key->buf, word);
while (inlen-- > 0) {
out[n] = in[n] ^ key->buf[n];
n++;
}
}
key->buf_index = n;
return;
#endif
}

View File

@@ -1,3 +1,54 @@
/* crypto/zuc/zuc.h */
/* ====================================================================
* Copyright (c) 2015 - 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.
* ====================================================================
*
*/
#ifndef HEADER_ZUC_H
#define HEADER_ZUC_H
@@ -10,43 +61,32 @@ extern "C" {
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;
/* word buffer */
unsigned char buf[4];
int buf_index;
} ZUC_KEY;
unsigned char zuc_state[100]; /* change what you like */
unsigned char word_buf[4];
} zuc_key_t;
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);
void zuc_set_key(zuc_key_t *key, const unsigned char *user_key, const unsigned char *iv);
void zuc_generate_keystream(zuc_key_t *key, size_t nwords, uint32_t *words);
void zuc_encrypt(zuc_key_t *key, size_t len, const unsigned char *in, unsigned char *out);
#define EEA3_MIN_BITS 1
#define EEA3_MAX_BITS 65504
/* 128-EEA3
* Input:
* count - the counter
* bearer - the bearer ientity 5-bit
* direction - 0 or 1
* nbits - the length of input message, in [1, 65504]
*/
#if 0
void eea3_set_key(zuc_key_t *key, const unsigned char *user_key,
uint32_t count, uint32_t bearer, uint32_t direction);
void eea3_encrypt(zuc_key_t *key, size_t len, const unsigned char *in, unsigned char *out);
#endif
#ifdef __cplusplus
}

View File

@@ -1,3 +1,512 @@
/* crypto/zuc/zuctest.c */
/* ====================================================================
* Copyright (c) 2015 - 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/zuc.h>
#include "../../e_os.h"
typedef struct {
uint32_t *words;
size_t nwords;
} test_words_t;
/* The following test suites are from
*
* ETSI/SAGE Specification
* Specification of the 3GPP Confidentiality and Integrity Algorithms
* 128-EEA3 & 128-EIA3
* Document 3: Implementor's Test Data
* Version 1.1 Date: Jan. 4, 2011
*
* NOTE: eea3_test_count[1] might be error
*/
/* ZUC Test Suits */
static unsigned char zuc_test_key[][16] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
{0x3d, 0x4c, 0x4b, 0xe9, 0x6a, 0x82, 0xfd, 0xae,
0xb5, 0x8f, 0x64, 0x1d, 0xb1, 0x7b, 0x45, 0x5b},
{0x4d, 0x32, 0x0b, 0xfa, 0xd4, 0xc2, 0x85, 0xbf,
0xd6, 0xb8, 0xbd, 0x00, 0xf3, 0x9d, 0x8b, 0x41},
};
static unsigned char zuc_test_iv[][16] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
{0x84, 0x31, 0x9a, 0xa8, 0xde, 0x69, 0x15, 0xca,
0x1f, 0x6b, 0xda, 0x6b, 0xfb, 0xd8, 0xc7, 0x66},
{0x52, 0x95, 0x9d, 0xab, 0xa0, 0xbf, 0x17, 0x6e,
0xce, 0x2d, 0xc3, 0x15, 0x04, 0x9e, 0xb5, 0x74},
};
static uint32_t zuc_test_words[][2] = {
{0x27bede74, 0x018082da},
{0x0657cfa0, 0x7096398b},
{0x14f1c272, 0x3279c419},
{0xed4400e7, 0x0633e5c5},
};
static uint32_t zuc_test4_word1999 = 0x7a574cdb;
/* 128-EEA3 Test Suites */
static unsigned char eea3_test_key[][16] = {
{0x17, 0x3d, 0x14, 0xba, 0x50, 0x03, 0x73, 0x1d,
0x7a, 0x60, 0x04, 0x94, 0x70, 0xf0, 0x0a, 0x29},
{0xe5, 0xbd, 0x3e, 0xa0, 0xeb, 0x55, 0xad, 0xe8,
0x66, 0xc6, 0xac, 0x58, 0xbd, 0x54, 0x30, 0x2a},
{0xd4, 0x55, 0x2a, 0x8f, 0xd6, 0xe6, 0x1c, 0xc8,
0x1a, 0x20, 0x09, 0x14, 0x1a, 0x29, 0xc1, 0x0b},
{0xdb, 0x84, 0xb4, 0xfb, 0xcc, 0xda, 0x56, 0x3b,
0x66, 0x22, 0x7b, 0xfe, 0x45, 0x6f, 0x0f, 0x77},
{0xe1, 0x3f, 0xed, 0x21, 0xb4, 0x6e, 0x4e, 0x7e,
0xc3, 0x12, 0x53, 0xb2, 0xbb, 0x17, 0xb3, 0xe0},
};
static uint32_t eea3_test_count[] = {
0x66035492, /* XXX: ? */0x56823, 0x76452ec1, 0xe4850fe1,
0x2738cdaa,
};
static uint8_t eea3_test_bearer[] = {
0x0f, 0x18, 0x02, 0x10, 0x1a,
};
static int eea3_test_direction[] = {
0, 1, 1, 1 , 0,
};
static int eea3_test_nbits[] = {
193, 800, 1570, 2798, 4019,
};
static uint32_t eea3_test1_plaintext[] = {
0x6cf65340, 0x735552ab, 0x0c9752fa, 0x6f9025fe,
0x0bd675d9, 0x005875b2, 0x00000000,
};
static uint32_t eea3_test1_ciphertext[] = {
0xa6c85fc6, 0x6afb8533, 0xaafc2518, 0xdfe78494,
0x0ee1e4b0, 0x30238cc8, 0x00000000,
};
static uint32_t eea3_test2_plaintext[] = {
0x14a8ef69, 0x3d678507, 0xbbe7270a, 0x7f67ff50,
0x06c3525b, 0x9807e467, 0xc4e56000, 0xba338f5d,
0x42955903, 0x67518222, 0x46c80d3b, 0x38f07f4b,
0xe2d8ff58, 0x05f51322, 0x29bde93b, 0xbbdcaf38,
0x2bf1ee97, 0x2fbf9977, 0xbada8945, 0x847a2a6c,
0x9ad34a66, 0x7554e04d, 0x1f7fa2c3, 0x3241bd8f,
0x01ba220d,
};
static uint32_t eea3_test2_ciphertext[] = {
0x131d43e0, 0xdea1be5c, 0x5a1bfd97, 0x1d852cbf,
0x712d7b4f, 0x57961fea, 0x3208afa8, 0xbca433f4,
0x56ad09c7, 0x417e58bc, 0x69cf8866, 0xd1353f74,
0x865e8078, 0x1d202dfb, 0x3ecff7fc, 0xbc3b190f,
0xe82a204e, 0xd0e350fc, 0x0f6f2613, 0xb2f2bca6,
0xdf5a473a, 0x57a4a00d, 0x985ebad8, 0x80d6f238,
0x64a07b01,
};
static uint32_t eea3_test3_plaintext[] = {
0x38f07f4b, 0xe2d8ff58, 0x05f51322, 0x29bde93b,
0xbbdcaf38, 0x2bf1ee97, 0x2fbf9977, 0xbada8945,
0x847a2a6c, 0x9ad34a66, 0x7554e04d, 0x1f7fa2c3,
0x3241bd8f, 0x01ba220d, 0x3ca4ec41, 0xe074595f,
0x54ae2b45, 0x4fd97143, 0x20436019, 0x65cca85c,
0x2417ed6c, 0xbec3bada, 0x84fc8a57, 0x9aea7837,
0xb0271177, 0x242a64dc, 0x0a9de71a, 0x8edee86c,
0xa3d47d03, 0x3d6bf539, 0x804eca86, 0xc584a905,
0x2de46ad3, 0xfced6554, 0x3bd90207, 0x372b27af,
0xb79234f5, 0xff43ea87, 0x0820e2c2, 0xb78a8aae,
0x61cce52a, 0x0515e348, 0xd196664a, 0x3456b182,
0xa07c406e, 0x4a207912, 0x71cfeda1, 0x65d535ec,
0x5ea2d4df, 0x40000000,
};
static uint32_t eea3_test3_ciphertext[] = {
0x8383b022, 0x9fcc0b9d, 0x2295ec41, 0xc977e9c2,
0xbb72e220, 0x378141f9, 0xc8318f3a, 0x270dfbcd,
0xee6411c2, 0xb3044f17, 0x6dc6e00f, 0x8960f97a,
0xfacd131a, 0xd6a3b49b, 0x16b7babc, 0xf2a509eb,
0xb16a75dc, 0xab14ff27, 0x5dbeeea1, 0xa2b155f9,
0xd52c2645, 0x2d0187c3, 0x10a4ee55, 0xbeaa78ab,
0x4024615b, 0xa9f5d5ad, 0xc7728f73, 0x560671f0,
0x13e5e550, 0x085d3291, 0xdf7d5fec, 0xedded559,
0x641b6c2f, 0x585233bc, 0x71e9602b, 0xd2305855,
0xbbd25ffa, 0x7f17ecbc, 0x042daae3, 0x8c1f57ad,
0x8e8ebd37, 0x346f71be, 0xfdbb7432, 0xe0e0bb2c,
0xfc09bcd9, 0x6570cb0c, 0x0c39df5e, 0x29294e82,
0x703a637f, 0x80000000,
};
static uint32_t eea3_test4_plaintext[] = {
0xe539f3b8, 0x973240da, 0x03f2b8aa, 0x05ee0a00,
0xdbafc0e1, 0x82055dfe, 0x3d7383d9, 0x2cef40e9,
0x2928605d, 0x52d05f4f, 0x9018a1f1, 0x89ae3997,
0xce19155f, 0xb1221db8, 0xbb0951a8, 0x53ad852c,
0xe16cff07, 0x382c93a1, 0x57de00dd, 0xb125c753,
0x9fd85045, 0xe4ee07e0, 0xc43f9e9d, 0x6f414fc4,
0xd1c62917, 0x813f74c0, 0x0fc83f3e, 0x2ed7c45b,
0xa5835264, 0xb43e0b20, 0xafda6b30, 0x53bfb642,
0x3b7fce25, 0x479ff5f1, 0x39dd9b5b, 0x995558e2,
0xa56be18d, 0xd581cd01, 0x7c735e6f, 0x0d0d97c4,
0xddc1d1da, 0x70c6db4a, 0x12cc9277, 0x8e2fbbd6,
0xf3ba52af, 0x91c9c6b6, 0x4e8da4f7, 0xa2c266d0,
0x2d001753, 0xdf089603, 0x93c5d568, 0x88bf49eb,
0x5c16d9a8, 0x0427a416, 0xbcb597df, 0x5bfe6f13,
0x890a07ee, 0x1340e647, 0x6b0d9aa8, 0xf822ab0f,
0xd1ab0d20, 0x4f40b7ce, 0x6f2e136e, 0xb67485e5,
0x07804d50, 0x4588ad37, 0xffd81656, 0x8b2dc403,
0x11dfb654, 0xcdead47e, 0x2385c343, 0x6203dd83,
0x6f9c64d9, 0x7462ad5d, 0xfa63b5cf, 0xe08acb95,
0x32866f5c, 0xa787566f, 0xca93e6b1, 0x693ee15c,
0xf6f7a2d6, 0x89d97417, 0x98dc1c23, 0x8e1be650,
0x733b18fb, 0x34ff880e, 0x16bbd21b, 0x47ac0000,
};
static uint32_t eea3_test4_ciphertext[] = {
0x4bbfa91b, 0xa25d47db, 0x9a9f190d, 0x962a19ab,
0x323926b3, 0x51fbd39e, 0x351e05da, 0x8b8925e3,
0x0b1cce0d, 0x12211010, 0x95815cc7, 0xcb631950,
0x9ec0d679, 0x40491987, 0xe13f0aff, 0xac332aa6,
0xaa64626d, 0x3e9a1917, 0x519e0b97, 0xb655c6a1,
0x65e44ca9, 0xfeac0790, 0xd2a321ad, 0x3d86b79c,
0x5138739f, 0xa38d887e, 0xc7def449, 0xce8abdd3,
0xe7f8dc4c, 0xa9e7b733, 0x14ad310f, 0x9025e619,
0x46b3a56d, 0xc649ec0d, 0xa0d63943, 0xdff592cf,
0x962a7efb, 0x2c8524e3, 0x5a2a6e78, 0x79d62604,
0xef268695, 0xfa400302, 0x7e22e608, 0x30775220,
0x64bd4a5b, 0x906b5f53, 0x1274f235, 0xed506cff,
0x0154c754, 0x928a0ce5, 0x476f2cb1, 0x020a1222,
0xd32c1455, 0xecaef1e3, 0x68fb344d, 0x1735bfbe,
0xdeb71d0a, 0x33a2a54b, 0x1da5a294, 0xe679144d,
0xdf11eb1a, 0x3de8cf0c, 0xc0619179, 0x74f35c1d,
0x9ca0ac81, 0x807f8fcc, 0xe6199a6c, 0x7712da86,
0x5021b04c, 0xe0439516, 0xf1a526cc, 0xda9fd9ab,
0xbd53c3a6, 0x84f9ae1e, 0x7ee6b11d, 0xa138ea82,
0x6c5516b5, 0xaadf1abb, 0xe36fa7ff, 0xf92e3a11,
0x76064e8d, 0x95f2e488, 0x2b5500b9, 0x3228b219,
0x4a475c1a, 0x27f63f9f, 0xfd264989, 0xa1bc0000,
};
static uint32_t eea3_test5_plaintext[] = {
0x8d74e20d, 0x54894e06, 0xd3cb13cb, 0x3933065e,
0x8674be62, 0xadb1c72b, 0x3a646965, 0xab63cb7b,
0x7854dfdc, 0x27e84929, 0xf49c64b8, 0x72a490b1,
0x3f957b64, 0x827e71f4, 0x1fbd4269, 0xa42c97f8,
0x24537027, 0xf86e9f4a, 0xd82d1df4, 0x51690fdd,
0x98b6d03f, 0x3a0ebe3a, 0x312d6b84, 0x0ba5a182,
0x0b2a2c97, 0x09c090d2, 0x45ed267c, 0xf845ae41,
0xfa975d33, 0x33ac3009, 0xfd40eba9, 0xeb5b8857,
0x14b768b6, 0x97138baf, 0x21380eca, 0x49f644d4,
0x8689e421, 0x5760b906, 0x739f0d2b, 0x3f091133,
0xca15d981, 0xcbe401ba, 0xf72d05ac, 0xe05cccb2,
0xd297f4ef, 0x6a5f58d9, 0x1246cfa7, 0x7215b892,
0xab441d52, 0x78452795, 0xccb7f5d7, 0x9057a1c4,
0xf77f80d4, 0x6db2033c, 0xb79bedf8, 0xe60551ce,
0x10c667f6, 0x2a97abaf, 0xabbcd677, 0x2018df96,
0xa282ea73, 0x7ce2cb33, 0x1211f60d, 0x5354ce78,
0xf9918d9c, 0x206ca042, 0xc9b62387, 0xdd709604,
0xa50af16d, 0x8d35a890, 0x6be484cf, 0x2e74a928,
0x99403643, 0x53249b27, 0xb4c9ae29, 0xeddfc7da,
0x6418791a, 0x4e7baa06, 0x60fa6451, 0x1f2d685c,
0xc3a5ff70, 0xe0d2b742, 0x92e3b8a0, 0xcd6b04b1,
0xc790b8ea, 0xd2703708, 0x540dea2f, 0xc09c3da7,
0x70f65449, 0xe84d817a, 0x4f551055, 0xe19ab850,
0x18a0028b, 0x71a144d9, 0x6791e9a3, 0x57793350,
0x4eee0060, 0x340c69d2, 0x74e1bf9d, 0x805dcbcc,
0x1a6faa97, 0x6800b6ff, 0x2b671dc4, 0x63652fa8,
0xa33ee509, 0x74c1c21b, 0xe01eabb2, 0x16743026,
0x9d72ee51, 0x1c9dde30, 0x797c9a25, 0xd86ce74f,
0x5b961be5, 0xfdfb6807, 0x814039e7, 0x137636bd,
0x1d7fa9e0, 0x9efd2007, 0x505906a5, 0xac45dfde,
0xed7757bb, 0xee745749, 0xc2963335, 0x0bee0ea6,
0xf409df45, 0x80160000,
};
static uint32_t eea3_test5_ciphertext[] = {
0x94eaa4aa, 0x30a57137, 0xddf09b97, 0xb25618a2,
0x0a13e2f1, 0x0fa5bf81, 0x61a879cc, 0x2ae797a6,
0xb4cf2d9d, 0xf31debb9, 0x905ccfec, 0x97de605d,
0x21c61ab8, 0x531b7f3c, 0x9da5f039, 0x31f8a064,
0x2de48211, 0xf5f52ffe, 0xa10f392a, 0x04766998,
0x5da454a2, 0x8f080961, 0xa6c2b62d, 0xaa17f33c,
0xd60a4971, 0xf48d2d90, 0x9394a55f, 0x48117ace,
0x43d708e6, 0xb77d3dc4, 0x6d8bc017, 0xd4d1abb7,
0x7b7428c0, 0x42b06f2f, 0x99d8d07c, 0x9879d996,
0x00127a31, 0x985f1099, 0xbbd7d6c1, 0x519ede8f,
0x5eeb4a61, 0x0b349ac0, 0x1ea23506, 0x91756bd1,
0x05c974a5, 0x3eddb35d, 0x1d4100b0, 0x12e522ab,
0x41f4c5f2, 0xfde76b59, 0xcb8b96d8, 0x85cfe408,
0x0d1328a0, 0xd636cc0e, 0xdc05800b, 0x76acca8f,
0xef672084, 0xd1f52a8b, 0xbd8e0993, 0x320992c7,
0xffbae17c, 0x408441e0, 0xee883fc8, 0xa8b05e22,
0xf5ff7f8d, 0x1b48c74c, 0x468c467a, 0x028f09fd,
0x7ce91109, 0xa570a2d5, 0xc4d5f4fa, 0x18c5dd3e,
0x4562afe2, 0x4ef77190, 0x1f59af64, 0x5898acef,
0x088abae0, 0x7e92d52e, 0xb2de5504, 0x5bb1b7c4,
0x164ef2d7, 0xa6cac15e, 0xeb926d7e, 0xa2f08b66,
0xe1f759f3, 0xaee44614, 0x725aa3c7, 0x482b3084,
0x4c143ff8, 0x5b53f1e5, 0x83c50125, 0x7dddd096,
0xb81268da, 0xa303f172, 0x34c23335, 0x41f0bb8e,
0x190648c5, 0x807c866d, 0x71932286, 0x09adb948,
0x686f7de2, 0x94a802cc, 0x38f7fe52, 0x08f5ea31,
0x96d0167b, 0x9bdd02f0, 0xd2a5221c, 0xa508f893,
0xaf5c4b4b, 0xb9f4f520, 0xfd84289b, 0x3dbe7e61,
0x497a7e2a, 0x584037ea, 0x637b6981, 0x127174af,
0x57b471df, 0x4b2768fd, 0x79c1540f, 0xb3edf2ea,
0x22cb69be, 0xc0cf8d93, 0x3d9c6fdd, 0x645e8505,
0x91cca3d6, 0x2c0cc000,
};
static test_words_t eea3_test_plaintext[] = {
{eea3_test1_plaintext, sizeof(eea3_test1_plaintext)/4},
{eea3_test2_plaintext, sizeof(eea3_test2_plaintext)/4},
{eea3_test3_plaintext, sizeof(eea3_test3_plaintext)/4},
{eea3_test4_plaintext, sizeof(eea3_test4_plaintext)/4},
{eea3_test5_plaintext, sizeof(eea3_test5_plaintext)/4},
};
static test_words_t eea3_test_ciphertext[] = {
{eea3_test1_ciphertext, sizeof(eea3_test1_ciphertext)/4},
{eea3_test2_ciphertext, sizeof(eea3_test2_ciphertext)/4},
{eea3_test3_ciphertext, sizeof(eea3_test3_ciphertext)/4},
{eea3_test4_ciphertext, sizeof(eea3_test4_ciphertext)/4},
{eea3_test5_ciphertext, sizeof(eea3_test5_ciphertext)/4},
};
/* 128-EIA3 Test Suites */
static unsigned char eia3_test_key[][16] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x47, 0x05, 0x41, 0x25, 0x56, 0x1e, 0xb2, 0xdd,
0xa9, 0x40, 0x59, 0xda, 0x05, 0x09, 0x78, 0x50},
{0xc9, 0xe6, 0xce, 0xc4, 0x60, 0x7c, 0x72, 0xdb,
0x00, 0x0a, 0xef, 0xa8, 0x83, 0x85, 0xab, 0x0a},
{0xc8, 0xa4, 0x82, 0x62, 0xd0, 0xc2, 0xe2, 0xba,
0xc4, 0xb9, 0x6e, 0xf7, 0x7e, 0x80, 0xca, 0x59},
{0x6b, 0x8b, 0x08, 0xee, 0x79, 0xe0, 0xb5, 0x98,
0x2d, 0x6d, 0x12, 0x8e, 0xa9, 0xf2, 0x20, 0xcb},
};
static uint32_t eia3_test_count[] = {
0x00000000, 0x561eb2dd, 0xa94059da, 0x05097850,
0x561eb2dd,
};
static uint8_t eia3_test_bearer[] = {
0x00, 0x14, 0x0a, 0x10, 0x1c,
};
static int eia3_test_direction[] = {
0, 0, 1, 1, 0,
};
static int eia3_test_nbits[] = {
1, 90, 577, 2079, 5670,
};
static uint32_t eia3_test1_msg[] = {
0x00000000,
};
static uint32_t eia3_test2_msg[] = {
0x00000000, 0x00000000, 0x00000000,
};
static uint32_t eia3_test3_msg[] = {
0x983b41d4, 0x7d780c9e, 0x1ad11d7e, 0xb70391b1,
0xde0b35da, 0x2dc62f83, 0xe7b78d63, 0x06ca0ea0,
0x7e941b7b, 0xe91348f9, 0xfcb170e2, 0x217fecd9,
0x7f9f68ad, 0xb16e5d7d, 0x21e569d2, 0x80ed775c,
0xebde3f40, 0x93c53881, 0x00000000
};
static uint32_t eia3_test4_msg[] = {
0xb546430b, 0xf87b4f1e, 0xe834704c, 0xd6951c36,
0xe26f108c, 0xf731788f, 0x48dc34f1, 0x678c0522,
0x1c8fa7ff, 0x2f39f477, 0xe7e49ef6, 0x0a4ec2c3,
0xde24312a, 0x96aa26e1, 0xcfba5756, 0x3838b297,
0xf47e8510, 0xc779fd66, 0x54b14338, 0x6fa639d3,
0x1edbd6c0, 0x6e47d159, 0xd94362f2, 0x6aeeedee,
0x0e4f49d9, 0xbf841299, 0x5415bfad, 0x56ee82d1,
0xca7463ab, 0xf085b082, 0xb09904d6, 0xd990d43c,
0xf2e062f4, 0x0839d932, 0x48b1eb92, 0xcdfed530,
0x0bc14828, 0x0430b6d0, 0xcaa094b6, 0xec8911ab,
0x7dc36824, 0xb824dc0a, 0xf6682b09, 0x35fde7b4,
0x92a14dc2, 0xf4364803, 0x8da2cf79, 0x170d2d50,
0x133fd494, 0x16cb6e33, 0xbea90b8b, 0xf4559b03,
0x732a01ea, 0x290e6d07, 0x4f79bb83, 0xc10e5800,
0x15cc1a85, 0xb36b5501, 0x046e9c4b, 0xdcae5135,
0x690b8666, 0xbd54b7a7, 0x03ea7b6f, 0x220a5469,
0xa568027e,
};
static uint32_t eia3_test5_msg[] = {
0x5bad7247, 0x10ba1c56, 0xd5a315f8, 0xd40f6e09,
0x3780be8e, 0x8de07b69, 0x92432018, 0xe08ed96a,
0x5734af8b, 0xad8a575d, 0x3a1f162f, 0x85045cc7,
0x70925571, 0xd9f5b94e, 0x454a77c1, 0x6e72936b,
0xf016ae15, 0x7499f054, 0x3b5d52ca, 0xa6dbeab6,
0x97d2bb73, 0xe41b8075, 0xdce79b4b, 0x86044f66,
0x1d4485a5, 0x43dd7860, 0x6e0419e8, 0x059859d3,
0xcb2b67ce, 0x0977603f, 0x81ff839e, 0x33185954,
0x4cfbc8d0, 0x0fef1a4c, 0x8510fb54, 0x7d6b06c6,
0x11ef44f1, 0xbce107cf, 0xa45a06aa, 0xb360152b,
0x28dc1ebe, 0x6f7fe09b, 0x0516f9a5, 0xb02a1bd8,
0x4bb0181e, 0x2e89e19b, 0xd8125930, 0xd178682f,
0x3862dc51, 0xb636f04e, 0x720c47c3, 0xce51ad70,
0xd94b9b22, 0x55fbae90, 0x6549f499, 0xf8c6d399,
0x47ed5e5d, 0xf8e2def1, 0x13253e7b, 0x08d0a76b,
0x6bfc68c8, 0x12f375c7, 0x9b8fe5fd, 0x85976aa6,
0xd46b4a23, 0x39d8ae51, 0x47f680fb, 0xe70f978b,
0x38effd7b, 0x2f7866a2, 0x2554e193, 0xa94e98a6,
0x8b74bd25, 0xbb2b3f5f, 0xb0a5fd59, 0x887f9ab6,
0x8159b717, 0x8d5b7b67, 0x7cb546bf, 0x41eadca2,
0x16fc1085, 0x0128f8bd, 0xef5c8d89, 0xf96afa4f,
0xa8b54885, 0x565ed838, 0xa950fee5, 0xf1c3b0a4,
0xf6fb71e5, 0x4dfd169e, 0x82cecc72, 0x66c850e6,
0x7c5ef0ba, 0x960f5214, 0x060e71eb, 0x172a75fc,
0x1486835c, 0xbea65344, 0x65b055c9, 0x6a72e410,
0x52241823, 0x25d83041, 0x4b40214d, 0xaa8091d2,
0xe0fb010a, 0xe15c6de9, 0x0850973b, 0xdf1e423b,
0xe148a237, 0xb87a0c9f, 0x34d4b476, 0x05b803d7,
0x43a86a90, 0x399a4af3, 0x96d3a120, 0x0a62f3d9,
0x507962e8, 0xe5bee6d3, 0xda2bb3f7, 0x237664ac,
0x7a292823, 0x900bc635, 0x03b29e80, 0xd63f6067,
0xbf8e1716, 0xac25beba, 0x350deb62, 0xa99fe031,
0x85eb4f69, 0x937ecd38, 0x7941fda5, 0x44ba67db,
0x09117749, 0x38b01827, 0xbcc69c92, 0xb3f772a9,
0xd2859ef0, 0x03398b1f, 0x6bbad7b5, 0x74f7989a,
0x1d10b2df, 0x798e0dbf, 0x30d65874, 0x64d24878,
0xcd00c0ea, 0xee8a1a0c, 0xc753a279, 0x79e11b41,
0xdb1de3d5, 0x038afaf4, 0x9f5c682c, 0x3748d8a3,
0xa9ec54e6, 0xa371275f, 0x1683510f, 0x8e4f9093,
0x8f9ab6e1, 0x34c2cfdf, 0x4841cba8, 0x8e0cff2b,
0x0bcc8e6a, 0xdcb71109, 0xb5198fec, 0xf1bb7e5c,
0x531aca50, 0xa56a8a3b, 0x6de59862, 0xd41fa113,
0xd9cd9578, 0x08f08571, 0xd9a4bb79, 0x2af271f6,
0xcc6dbb8d, 0xc7ec36e3, 0x6be1ed30, 0x8164c31c,
0x7c0afc54, 0x1c000000,
};
static test_words_t eia3_test_msg[5] = {
{eia3_test1_msg, sizeof(eia3_test1_msg)/4},
{eia3_test2_msg, sizeof(eia3_test2_msg)/4},
{eia3_test3_msg, sizeof(eia3_test3_msg)/4},
{eia3_test4_msg, sizeof(eia3_test4_msg)/4},
{eia3_test5_msg, sizeof(eia3_test5_msg)/4},
};
static uint32_t eia3_test_mac[5] = {
0xc8a9595e, 0x6719a088, 0xfae8ff0b, 0x004ac4d6,
0x0ca12792,
};
static int test_zuc(int verbose)
{
#if 0
int i;
for (i = 0; i < sizeof(zuc_test_key)/sizeof(zuc_test_key[0]); i++) {
zuc_key_t key;
uint32_t word;
zuc_set_key(&key, zuc_test_key[i], zuc_test_iv[i]);
for (j = 0; j < 2; j++) {
zuc_generate_keystream(&key, 1, &word);
if (word != zuc_test_word[i][j]) {
return 0;
}
}
}
#endif
return 0;
}
static int test_eea3(int verbose)
{
#if 0
zuc_key_t key;
int i;
for (i = 0; i < sizeof(eea3_test_key)/sizeof(eea3_test_key[0]); i++) {
eea3_set_key(&key, eea3_test_count[i], eea3_test_bearer[i], eea3_test_direction[i]);
}
#endif
return 0;
}
static int test_eia3(int verbose)
{
return 0;
}
int main(int argc, char *argv[])
{
return 0;
}