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

@@ -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

@@ -1,117 +0,0 @@
/* crypto/evp/m_btc.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.
* ====================================================================
*/
/*
* This module is to support crypto-currency such as Bitcoin
*/
#include <stdio.h>
#include "cryptlib.h"
#ifndef OPENSSL_NO_GMSSL
#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)
{
SHA256_Init(ctx->md_data->sha256);
RIPEMD_Init(ctx->md_data->rmd160);
}
static int update(EVP_MD_CTX *ctx, const void *in, size_t inlen)
{
SHA256_Update(ctx, in, inlen);
}
static int final(EVP_MD_CTX *ctx, unsigned char *md)
{
return sm3_final(ctx->md_data, md);
}
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)
{
return &btc160_md;
}
const EVP_MD *EVP_btc256(void)
{
return &btc256_md;
}
const EVP_MD *EVP_sm3_rmd160(void)
{
return 0;
}
#endif

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
};