update paillier kmeth and ameth

This commit is contained in:
Zhi Guan
2017-02-23 17:27:23 +08:00
parent 9ae79dee03
commit d8e92d57c6
3 changed files with 235 additions and 75 deletions

View File

@@ -1607,3 +1607,9 @@ ibcs1 3 3 : ate-pairing
ibcs1 3 4 : r-ate-pairing
# CPK OID
sm-scheme 1000 : cpk
sm-scheme 1001 : paillier

View File

@@ -1,76 +1,108 @@
/* ====================================================================
* Copyright (c) 2015 - 2017 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 <openssl/asn1t.h>
#include <openssl/paillier.h>
#include "internal/cryptlib.h"
#include "internal/asn1_int.h"
#include "internal/evp_int.h"
static int paillier_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
static int paillier_pub_encode(X509_PUBKEY *pubkey, const EVP_PKEY *pkey)
{
unsigned char *penc = NULL;
int penclen;
if ((penclen = i2d_PAILLIER_PUBLIC_KEY(pkey->pkey.paillier, &penc)) < = 0) {
return 0;
}
if (X509_PUBKEY_set0_param(pubkey, OBJ_nid2obj(EVP_PKEY_PAILLIER),
V_ASN1_NULL, NULL, penc, penclen)) {
return 1;
}
OPENSSL_free(penc);
return 0;
}
static int paillier_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
{
return 0;
const unsigned char *cp;
int len;
PAILLIER *paillier = NULL;
if (!X509_PUBKEY_get0_param(NULL, &cp, &len, NULL, pubkey)) {
return 0;
}
if (!(paillier = d2i_PAILLIER_PUBLIC_KEY(NULL, &cp, len))) {
PAILLIERerr(PAILLIER_F_PAILLIER_PUB_DECODE, ERR_R_PAILLIER_LIB);
return 0;
}
EVP_PKEY_assign_PAILLIER(pkey, paillier);
return 1;
}
static int paillier_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
{
return 0;
return -1;
}
static int paillier_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
{
return 0;
}
const EVP_PKEY_ASN1_METHOD paillier_asn1_meth = {
EVP_PKEY_PAILLIER,
EVP_PKEY_PAILLIER,
0, //FIXME
static int paillier_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
{
return 0;
}
static int paillier_size(const EVP_PKEY *pkey)
{
return 0;
}
static int paillier_bits(const EVP_PKEY *pkey)
{
return 0;
}
static int paillier_security_bits(const EVP_PKEY *pkey)
{
return 0;
}
static void paillier_free(EVP_PKEY *pkey)
{
}
static int paillier_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
{
return 1;
}
static int paillier_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx)
{
return 1;
}
static int paillier_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx)
{
return 1;
}
static int paillier_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
{
return 1;
}
const EVP_PKEY_ASN1_METHOD paillier_ameth = {
NID_paillier,
NID_paillier,
0,
"PAILLER",
"GmSSL Paillier algorithm",
"PAILLIER",
"OpenSSL PAILLIER algorithm",
paillier_pub_decode,
paillier_pub_encode,
@@ -81,16 +113,15 @@ const EVP_PKEY_ASN1_METHOD paillier_ameth = {
paillier_priv_encode,
paillier_priv_print,
paillier_size,
int_paillier_size,
paillier_bits,
paillier_security_bits,
0, 0, 0, 0,
paillier_cmp_parameters,
0, 0,
0, 0, 0, 0, 0, 0,
0,
paillier_free,
paillier_ctrl,
NULL,
NULL
int_paillier_free,
paillier_pkey_ctrl,
old_paillier_priv_decode,
old_paillier_priv_encode
};

View File

@@ -1,33 +1,153 @@
/* ====================================================================
* Copyright (c) 2015 - 2017 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 <openssl/evp.h>
#include <openssl/paillier.h>
#include "internal/evp_int.h"
typedef struct {
int flags;
} PAILLIER_PKEY_CTX;
static int pkey_paillier_init(EVP_PKEY_CTX *ctx)
{
return 0;
return 1;
}
static int pkey_paillier_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
{
return 0;
return 1;
}
static void pkey_paillier_cleanup(EVP_PKEY_CTX *ctx)
{
}
//FIXME keygen
static int pkey_paillier_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen)
const unsigned char *in, size_t inlen)
{
return 0;
int ret = 0;
PAILLIER *key = ctx->pkey->pkey.paillier;
BIGNUM *m = NULL;
BIGNUM *c = NULL;
//FIXME: check inlen
if (!out) {
*outlen = PAILLIER_size(key);
return 1;
} else if (*outlen < (size_t)PAILLIER_size(key)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, PAILLIER_R_BUFFER_TOO_SMALL);
return 0;
}
if (!(m = BN_new()) || !(c = BN_new())) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!BN_bin2bn(in, (int)inlen, m)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_BN_LIB);
goto end;
}
if (!PAILLIER_encrypt(c, m, key)) {
goto end;
}
*outlen = BN_bn2bin(c, out);
ret = 1;
end:
BN_free(m);
BN_free(c);
return ret;
}
static int pkey_paillier_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen)
const unsigned char *in, size_t inlen)
{
return 0;
int ret = 0;
PAILLIER *key = ctx->pkey->pkey.paillier;
BIGNUM *m = NULL;
BIGNUM *c = NULL;
if (!out) {
*outlen = PAILLIER_size(key);
return 1;
} else if (*outlen < (size_t)PAILLIER_size(key)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, PAILLIER_R_BUFFER_TOO_SMALL);
return 0;
}
if (!(m = BN_new()) || !(c = BN_new())) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!BN_bin2bn(in, (int)inlen, c)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_BN_LIB);
goto end;
}
if (!PAILLIER_decrypt(m, c, key)) {
goto end;
}
*outlen = BN_bn2bin(m, out);
ret = 1;
end:
BN_free(m);
BN_free(c);
return ret;
}
static int pkey_paillier_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
@@ -41,6 +161,9 @@ static int pkey_paillier_ctrl_str(EVP_PKEY_CTX *ctx,
return 0;
}
#define EVP_PKEY_PAILLIER NID_paillier
const EVP_PKEY_METHOD paillier_pmeth = {
EVP_PKEY_PAILLIER,
0,