mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-30 17:53:39 +08:00
some update
This commit is contained in:
@@ -171,6 +171,7 @@ void OpenSSL_add_all_ciphers(void)
|
||||
EVP_add_cipher(EVP_sms4_cfb8());
|
||||
EVP_add_cipher(EVP_sms4_ofb());
|
||||
EVP_add_cipher(EVP_sms4_ctr());
|
||||
EVP_add_cipher(EVP_sms4_wrap());
|
||||
EVP_add_cipher_alias(SN_sms4_cbc,"SMS4");
|
||||
EVP_add_cipher_alias(SN_sms4_cbc,"sms4");
|
||||
#endif
|
||||
|
||||
@@ -211,7 +211,7 @@ 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__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (ctx->digest != type) {
|
||||
if (ctx->digest && ctx->digest->ctx_size)
|
||||
OPENSSL_free(ctx->md_data);
|
||||
@@ -219,7 +219,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
|
||||
if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
|
||||
ctx->update = type->update;
|
||||
ctx->md_data = OPENSSL_malloc(type->ctx_size);
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (ctx->md_data == NULL) {
|
||||
EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
@@ -231,7 +231,7 @@ 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__);
|
||||
//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 +248,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
return ctx->digest->init(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -178,7 +178,7 @@ static int sms4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
EVP_SMS4_KEY *sms4 = (EVP_SMS4_KEY *)ctx->cipher_data;
|
||||
|
||||
CRYPTO_ctr128_encrypt_ctr32(in, out, len, &sms4->ks, ctx->iv, ctx->buf,
|
||||
&num, sms4_ctr128_encrypt);
|
||||
&num, (ctr128_f)sms4_encrypt);
|
||||
|
||||
ctx->num = (size_t)num;
|
||||
return 1;
|
||||
@@ -240,6 +240,7 @@ static int sms4_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
@@ -253,28 +254,81 @@ typedef struct {
|
||||
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;
|
||||
|
||||
if (key) {
|
||||
if (ctx->encrypt) {
|
||||
sms4_set_encrypt_key(&sms4_wrap->ks.ks, key);
|
||||
} else {
|
||||
sms4_set_decrypt_key(&sms4_wrap->ks.ks, key);
|
||||
}
|
||||
|
||||
if (!iv) {
|
||||
sms4_wrap->iv = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (iv) {
|
||||
memcpy(ctx->iv, iv, 8);
|
||||
sms4_wrap->iv = ctx->iv;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int sms4_wrap_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inlen)
|
||||
{
|
||||
return -1;
|
||||
EVP_SMS4_WRAP_CTX *sms4_wrap = ctx->cipher_data;
|
||||
size_t rv;
|
||||
|
||||
if (!in) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (inlen % 8) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ctx->encrypt && inlen < 8)
|
||||
return -1;
|
||||
|
||||
if (!ctx->encrypt && inlen < 8)
|
||||
return -1;
|
||||
|
||||
if (!out) {
|
||||
if (ctx->encrypt)
|
||||
return inlen + 8;
|
||||
else return inlen - 8;
|
||||
}
|
||||
|
||||
if (ctx->encrypt)
|
||||
rv = CRYPTO_128_wrap(&sms4_wrap->ks.ks, sms4_wrap->iv,
|
||||
out, in, inlen, (block128_f)sms4_encrypt);
|
||||
else rv = CRYPTO_128_unwrap(&sms4_wrap->ks.ks, sms4_wrap->iv,
|
||||
out, in, inlen, (block128_f)sms4_encrypt);
|
||||
|
||||
return rv ? (int)rv : -1;
|
||||
}
|
||||
|
||||
|
||||
#define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
|
||||
#define SMS4_WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
|
||||
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
|
||||
|
||||
|
||||
#define SMS4_WRAP_BLOCK_SIZE 8
|
||||
#define SMS4_WRAP_IV_LENGTH 8
|
||||
|
||||
const EVP_CIPHER sms4_wrap = {
|
||||
NID_sms4_wrap,
|
||||
SMS4_WRAP_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_WRAP_IV_LENGTH,
|
||||
WRAP_FLAGS,
|
||||
SMS4_WRAP_FLAGS,
|
||||
sms4_wrap_init_key,
|
||||
sms4_wrap_do_cipher,
|
||||
NULL, /* cleanup() */
|
||||
@@ -290,6 +344,5 @@ const EVP_CIPHER *EVP_sms4_wrap(void)
|
||||
return &sms4_wrap;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -657,13 +657,6 @@ int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
|
||||
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
|
||||
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
|
||||
|
||||
#ifndef OPENSSL_NO_GMSSL
|
||||
int EVP_Encrypt_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outlen,
|
||||
const unsigned char *in, int inlen);
|
||||
int EVP_Decrypt_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outlen,
|
||||
const unsigned char *in, int inlen);
|
||||
#endif
|
||||
|
||||
int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s,
|
||||
EVP_PKEY *pkey);
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
|
||||
if (ver) {
|
||||
if (ctx->pctx->pmeth->verifyctx_init) {
|
||||
@@ -104,16 +104,16 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
} else if (EVP_PKEY_sign_init(ctx->pctx) <= 0)
|
||||
return 0;
|
||||
}
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
|
||||
return 0;
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (pctx)
|
||||
*pctx = ctx->pctx;
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
|
||||
return 1;
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (!EVP_DigestInit_ex(ctx, type, e))
|
||||
return 0;
|
||||
return 1;
|
||||
|
||||
@@ -198,16 +198,25 @@ EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
|
||||
EVP_PKEY_CTX *mac_ctx = NULL;
|
||||
EVP_PKEY *mac_key = NULL;
|
||||
mac_ctx = EVP_PKEY_CTX_new_id(type, e);
|
||||
if (!mac_ctx)
|
||||
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)
|
||||
}
|
||||
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)
|
||||
keylen, (void *)key) <= 0) {
|
||||
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
|
||||
goto merr;
|
||||
if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
|
||||
}
|
||||
if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) {
|
||||
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
|
||||
goto merr;
|
||||
}
|
||||
merr:
|
||||
if (mac_ctx)
|
||||
EVP_PKEY_CTX_free(mac_ctx);
|
||||
|
||||
@@ -119,15 +119,30 @@ const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
|
||||
tmp.pkey_id = type;
|
||||
if (app_pkey_methods) {
|
||||
int idx;
|
||||
|
||||
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
|
||||
idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
|
||||
if (idx >= 0)
|
||||
return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
|
||||
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
|
||||
}
|
||||
//fprintf(stderr, "%s %d: t->pkey_id = %d\n", __FILE__, __LINE__, t->pkey_id);
|
||||
int i;
|
||||
for (i = 0; i < sizeof(standard_methods) / sizeof(EVP_PKEY_METHOD *); i++) {
|
||||
if (type == standard_methods[i]->pkey_id) {
|
||||
return standard_methods[i];
|
||||
}
|
||||
}
|
||||
/*
|
||||
ret = OBJ_bsearch_pmeth(&t, standard_methods,
|
||||
sizeof(standard_methods) /
|
||||
sizeof(EVP_PKEY_METHOD *));
|
||||
if (!ret || !*ret)
|
||||
|
||||
*/
|
||||
if (!ret || !*ret) {
|
||||
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
|
||||
return NULL;
|
||||
}
|
||||
return *ret;
|
||||
}
|
||||
|
||||
@@ -136,8 +151,10 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
|
||||
EVP_PKEY_CTX *ret;
|
||||
const EVP_PKEY_METHOD *pmeth;
|
||||
if (id == -1) {
|
||||
if (!pkey || !pkey->ameth)
|
||||
if (!pkey || !pkey->ameth) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
return NULL;
|
||||
}
|
||||
id = pkey->ameth->pkey_id;
|
||||
}
|
||||
#ifndef OPENSSL_NO_ENGINE
|
||||
@@ -159,7 +176,7 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
|
||||
|
||||
if (e)
|
||||
pmeth = ENGINE_get_pkey_meth(e, id);
|
||||
else
|
||||
else
|
||||
#endif
|
||||
pmeth = EVP_PKEY_meth_find(id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user