Update - OpenSSL 1.1.1-pre7-dev
This commit is contained in:
+118
-14
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -16,6 +16,10 @@
|
||||
#include <openssl/evp.h>
|
||||
#include "internal/evp_int.h"
|
||||
|
||||
#if !defined(OPENSSL_NO_SM2)
|
||||
# include <openssl/sm2.h>
|
||||
#endif
|
||||
|
||||
/* EC pkey context structure */
|
||||
|
||||
typedef struct {
|
||||
@@ -42,9 +46,10 @@ static int pkey_ec_init(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
EC_PKEY_CTX *dctx;
|
||||
|
||||
dctx = OPENSSL_zalloc(sizeof(*dctx));
|
||||
if (dctx == NULL)
|
||||
if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
|
||||
ECerr(EC_F_PKEY_EC_INIT, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dctx->cofactor_mode = -1;
|
||||
dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
|
||||
@@ -102,6 +107,7 @@ static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
|
||||
unsigned int sltmp;
|
||||
EC_PKEY_CTX *dctx = ctx->data;
|
||||
EC_KEY *ec = ctx->pkey->pkey.ec;
|
||||
const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
|
||||
|
||||
if (!sig) {
|
||||
*siglen = ECDSA_size(ec);
|
||||
@@ -116,7 +122,15 @@ static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
|
||||
else
|
||||
type = NID_sha1;
|
||||
|
||||
ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
|
||||
if (ec_nid == NID_sm2) {
|
||||
#if defined(OPENSSL_NO_SM2)
|
||||
ret = -1;
|
||||
#else
|
||||
ret = SM2_sign(type, tbs, tbslen, sig, &sltmp, ec);
|
||||
#endif
|
||||
} else {
|
||||
ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
|
||||
}
|
||||
|
||||
if (ret <= 0)
|
||||
return ret;
|
||||
@@ -131,20 +145,28 @@ static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
|
||||
int ret, type;
|
||||
EC_PKEY_CTX *dctx = ctx->data;
|
||||
EC_KEY *ec = ctx->pkey->pkey.ec;
|
||||
const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
|
||||
|
||||
if (dctx->md)
|
||||
type = EVP_MD_type(dctx->md);
|
||||
else
|
||||
type = NID_sha1;
|
||||
|
||||
ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
|
||||
if (ec_nid == NID_sm2) {
|
||||
#if defined(OPENSSL_NO_SM2)
|
||||
ret = -1;
|
||||
#else
|
||||
ret = SM2_verify(type, tbs, tbslen, sig, siglen, ec);
|
||||
#endif
|
||||
} else {
|
||||
ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
|
||||
size_t *keylen)
|
||||
static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
|
||||
{
|
||||
int ret;
|
||||
size_t outlen;
|
||||
@@ -180,6 +202,85 @@ static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int pkey_ecies_encrypt(EVP_PKEY_CTX *ctx,
|
||||
unsigned char *out, size_t *outlen,
|
||||
const unsigned char *in, size_t inlen)
|
||||
{
|
||||
int ret;
|
||||
EC_KEY *ec = ctx->pkey->pkey.ec;
|
||||
const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
|
||||
|
||||
if (ec_nid == NID_sm2) {
|
||||
# if defined(OPENSSL_NO_SM2)
|
||||
ret = -1;
|
||||
# else
|
||||
int md_type;
|
||||
EC_PKEY_CTX *dctx = ctx->data;
|
||||
|
||||
if (dctx->md)
|
||||
md_type = EVP_MD_type(dctx->md);
|
||||
else if (ec_nid == NID_sm2)
|
||||
md_type = NID_sm3;
|
||||
else
|
||||
md_type = NID_sha256;
|
||||
|
||||
if (out == NULL) {
|
||||
*outlen = SM2_ciphertext_size(ec, EVP_get_digestbynid(md_type),
|
||||
inlen);
|
||||
ret = 1;
|
||||
}
|
||||
else {
|
||||
ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
|
||||
in, inlen, out, outlen);
|
||||
}
|
||||
# endif
|
||||
} else {
|
||||
/* standard ECIES not implemented */
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int pkey_ecies_decrypt(EVP_PKEY_CTX *ctx,
|
||||
unsigned char *out, size_t *outlen,
|
||||
const unsigned char *in, size_t inlen)
|
||||
{
|
||||
int ret;
|
||||
EC_KEY *ec = ctx->pkey->pkey.ec;
|
||||
const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
|
||||
|
||||
if (ec_nid == NID_sm2) {
|
||||
# if defined(OPENSSL_NO_SM2)
|
||||
ret = -1;
|
||||
# else
|
||||
int md_type;
|
||||
EC_PKEY_CTX *dctx = ctx->data;
|
||||
|
||||
if (dctx->md)
|
||||
md_type = EVP_MD_type(dctx->md);
|
||||
else if (ec_nid == NID_sm2)
|
||||
md_type = NID_sm3;
|
||||
else
|
||||
md_type = NID_sha256;
|
||||
|
||||
if (out == NULL) {
|
||||
*outlen = SM2_plaintext_size(ec, EVP_get_digestbynid(md_type), inlen);
|
||||
ret = 1;
|
||||
}
|
||||
else {
|
||||
ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
|
||||
in, inlen, out, outlen);
|
||||
}
|
||||
# endif
|
||||
} else {
|
||||
/* standard ECIES not implemented */
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
|
||||
unsigned char *key, size_t *keylen)
|
||||
{
|
||||
@@ -197,9 +298,10 @@ static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
|
||||
return 0;
|
||||
if (!pkey_ec_derive(ctx, NULL, &ktmplen))
|
||||
return 0;
|
||||
ktmp = OPENSSL_malloc(ktmplen);
|
||||
if (ktmp == NULL)
|
||||
if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL) {
|
||||
ECerr(EC_F_PKEY_EC_KDF_DERIVE, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
|
||||
goto err;
|
||||
/* Do KDF stuff */
|
||||
@@ -244,8 +346,7 @@ static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
return dctx->cofactor_mode;
|
||||
else {
|
||||
EC_KEY *ec_key = ctx->pkey->pkey.ec;
|
||||
return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 :
|
||||
0;
|
||||
return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
|
||||
}
|
||||
} else if (p1 < -1 || p1 > 1)
|
||||
return -2;
|
||||
@@ -318,7 +419,8 @@ static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
|
||||
EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
|
||||
EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
|
||||
EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
|
||||
EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
|
||||
EVP_MD_type((const EVP_MD *)p2) != NID_sm3) {
|
||||
ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
|
||||
return 0;
|
||||
}
|
||||
@@ -448,9 +550,11 @@ const EVP_PKEY_METHOD ec_pkey_meth = {
|
||||
|
||||
0, 0, 0, 0,
|
||||
|
||||
0, 0,
|
||||
0,
|
||||
pkey_ecies_encrypt,
|
||||
|
||||
0, 0,
|
||||
0,
|
||||
pkey_ecies_decrypt,
|
||||
|
||||
0,
|
||||
#ifndef OPENSSL_NO_EC
|
||||
|
||||
Reference in New Issue
Block a user