Add paillier homomorphic encryption command

See http://gmssl.org/docs/paillier.md
This commit is contained in:
Zhi Guan
2019-02-01 23:49:16 +08:00
parent 7d5c18379f
commit 22027d6656
12 changed files with 5837 additions and 5461 deletions

View File

@@ -107,15 +107,21 @@ static int do_paillier_print(BIO *bp, const PAILLIER *x, int off, int priv)
{
char *str;
int ret = 0;
int bits;
if (!BIO_indent(bp, off, 128))
goto end;
bits = x->bits;
if (bits == 0)
bits = BN_num_bytes(x->n) * 8;
if (priv && x->lambda) {
if (BIO_printf(bp, "Private-Key: (%d bit)\n", x->bits) <= 0)
if (BIO_printf(bp, "Private-Key: (%d bit)\n", bits) <= 0)
goto end;
str = "modulus";
} else {
if (BIO_printf(bp, "Public-Key: (%d bit)\n", x->bits) <= 0)
if (BIO_printf(bp, "Public-Key: (%d bit)\n", bits) <= 0)
goto end;
str = "Modulus";
}
@@ -125,7 +131,7 @@ static int do_paillier_print(BIO *bp, const PAILLIER *x, int off, int priv)
if (priv) {
if (!ASN1_bn_print(bp, "lambda:", x->lambda, NULL, off))
goto end;
if (!ASN1_bn_print(bp, "x:", x->x, NULL, off))
if (x->x && !ASN1_bn_print(bp, "x:", x->x, NULL, off))
goto end;
}
ret = 1;
@@ -158,6 +164,7 @@ static int paillier_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
PAILLIERerr(PAILLIER_F_PAILLIER_PRIV_DECODE, ERR_R_PAILLIER_LIB);
return 0;
}
paillier->bits = BN_num_bytes(paillier->n) * 8;
EVP_PKEY_assign_PAILLIER(pkey, paillier);
return 1;
}

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2019 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
@@ -29,9 +29,11 @@ static ERR_STRING_DATA PAILLIER_str_functs[] = {
{ERR_FUNC(PAILLIER_F_PAILLIER_ENCRYPT), "PAILLIER_encrypt"},
{ERR_FUNC(PAILLIER_F_PAILLIER_GENERATE_KEY), "PAILLIER_generate_key"},
{ERR_FUNC(PAILLIER_F_PAILLIER_NEW), "PAILLIER_new"},
{ERR_FUNC(PAILLIER_F_PAILLIER_PLAINTEXT_SIZE), "paillier_plaintext_size"},
{ERR_FUNC(PAILLIER_F_PAILLIER_PRIV_DECODE), "paillier_priv_decode"},
{ERR_FUNC(PAILLIER_F_PAILLIER_PRIV_ENCODE), "paillier_priv_encode"},
{ERR_FUNC(PAILLIER_F_PAILLIER_PUB_DECODE), "paillier_pub_decode"},
{ERR_FUNC(PAILLIER_F_PAILLIER_SIZE), "PAILLIER_size"},
{ERR_FUNC(PAILLIER_F_PKEY_PAILLIER_CTRL), "pkey_paillier_ctrl"},
{ERR_FUNC(PAILLIER_F_PKEY_PAILLIER_CTRL_STR), "pkey_paillier_ctrl_str"},
{ERR_FUNC(PAILLIER_F_PKEY_PAILLIER_DECRYPT), "pkey_paillier_decrypt"},

View File

@@ -81,7 +81,20 @@ void PAILLIER_free(PAILLIER *key)
int PAILLIER_size(const PAILLIER *key)
{
return (BN_num_bits(key->n) * 2)/8;
ASN1_INTEGER a;
unsigned char buf[4] = {0xff};
int i;
if (!(i = BN_num_bytes(key->n))) {
PAILLIERerr(PAILLIER_F_PAILLIER_SIZE, ERR_R_BN_LIB);
return 0;
}
a.length = i * 2;
a.data = buf;
a.type = V_ASN1_INTEGER;
return i2d_ASN1_INTEGER(&a, NULL);
}
int PAILLIER_security_bits(const PAILLIER *key)
@@ -142,13 +155,11 @@ int PAILLIER_generate_key(PAILLIER *key, int bits)
/* n_plusone = n + 1 */
|| !BN_copy(key->n_plusone, key->n)
|| !BN_add_word(key->n_plusone, 1)
#if 0
/* x = (((g^lambda mod n^2) - 1)/n)^-1 mod n */
|| !BN_mod_exp(key->x, key->n_plusone, key->lambda, key->n_squared, bn_ctx)
|| !BN_sub_word(key->x, 1)
|| !BN_div(key->x, key->x, key->n)
|| !BN_div(key->x, NULL, key->x, key->n, bn_ctx)
|| !BN_mod_inverse(key->x, key->x, key->n, bn_ctx)
#endif
) {
PAILLIERerr(PAILLIER_F_PAILLIER_GENERATE_KEY, ERR_R_BN_LIB);
goto end;
@@ -176,9 +187,6 @@ int PAILLIER_encrypt(BIGNUM *c, const BIGNUM *m, PAILLIER *pub_key)
BIGNUM *r = NULL;
BN_CTX *bn_ctx = NULL;
fprintf(stderr, "%s %d: m = %s\n", __FILE__, __LINE__, BN_bn2hex(m));
if (BN_cmp(m, pub_key->n) >= 0) {
PAILLIERerr(PAILLIER_F_PAILLIER_ENCRYPT, PAILLIER_R_INVALID_PLAINTEXT);
goto end;
@@ -256,13 +264,6 @@ int PAILLIER_decrypt(BIGNUM *m, const BIGNUM *c, PAILLIER *key)
goto end;
}
/*
printf("m = %s\n", BN_bn2hex(m));
printf("c = %s\n", BN_bn2hex(c));
printf("lambda = %s\n", BN_bn2hex(key->lambda));
printf("n^2 = %s\n", BN_bn2hex(key->n_squared));
*/
if (!key->n_squared) {
if (!(key->n_squared = BN_new())) {
PAILLIERerr(PAILLIER_F_PAILLIER_DECRYPT, ERR_R_MALLOC_FAILURE);
@@ -274,35 +275,26 @@ printf("n^2 = %s\n", BN_bn2hex(key->n_squared));
}
}
fprintf(stderr, "%s %d: m = %s\n", __FILE__, __LINE__, BN_bn2hex(m));
if (!BN_mod_exp(m, c, key->lambda, key->n_squared, bn_ctx)) {
PAILLIERerr(PAILLIER_F_PAILLIER_DECRYPT, ERR_R_BN_LIB);
goto end;
}
fprintf(stderr, "%s %d: m = %s\n", __FILE__, __LINE__, BN_bn2hex(m));
if (!BN_sub_word(m, 1)) {
PAILLIERerr(PAILLIER_F_PAILLIER_DECRYPT, ERR_R_BN_LIB);
goto end;
}
fprintf(stderr, "%s %d: m = %s\n", __FILE__, __LINE__, BN_bn2hex(m));
if (!BN_div(m, NULL, m, key->n, bn_ctx)) {
PAILLIERerr(PAILLIER_F_PAILLIER_DECRYPT, ERR_R_BN_LIB);
goto end;
}
fprintf(stderr, "%s %d: m = %s\n", __FILE__, __LINE__, BN_bn2hex(m));
if (!BN_mod_mul(m, m, key->x, key->n, bn_ctx)) {
PAILLIERerr(PAILLIER_F_PAILLIER_DECRYPT, ERR_R_BN_LIB);
goto end;
}
printf("m = %s\n", BN_bn2hex(m));
ret = 1;
end:
BN_CTX_free(bn_ctx);
@@ -329,6 +321,17 @@ int PAILLIER_ciphertext_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, PAILLIE
}
} while (BN_is_zero(k));
if (!key->n_squared) {
if (!(key->n_squared = BN_new())) {
PAILLIERerr(PAILLIER_F_PAILLIER_CIPHERTEXT_ADD, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!BN_sqr(key->n_squared, key->n, bn_ctx)) {
PAILLIERerr(PAILLIER_F_PAILLIER_CIPHERTEXT_ADD, ERR_R_BN_LIB);
goto end;
}
}
if (!BN_mod_exp(k, k, key->n, key->n_squared, bn_ctx)) {
PAILLIERerr(PAILLIER_F_PAILLIER_CIPHERTEXT_ADD, ERR_R_BN_LIB);
goto end;
@@ -371,6 +374,17 @@ int PAILLIER_ciphertext_scalar_mul(BIGNUM *r, const BIGNUM *scalar, const BIGNUM
}
} while (BN_is_zero(k));
if (!key->n_squared) {
if (!(key->n_squared = BN_new())) {
PAILLIERerr(PAILLIER_F_PAILLIER_CIPHERTEXT_SCALAR_MUL, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!BN_sqr(key->n_squared, key->n, bn_ctx)) {
PAILLIERerr(PAILLIER_F_PAILLIER_CIPHERTEXT_SCALAR_MUL, ERR_R_BN_LIB);
goto end;
}
}
if (!BN_mod_exp(k, k, key->n, key->n_squared, bn_ctx)) {
PAILLIERerr(PAILLIER_F_PAILLIER_CIPHERTEXT_SCALAR_MUL, ERR_R_BN_LIB);
goto end;
@@ -390,7 +404,7 @@ int PAILLIER_ciphertext_scalar_mul(BIGNUM *r, const BIGNUM *scalar, const BIGNUM
end:
BN_clear_free(k);
BN_CTX_free(bn_ctx);
return 0;
return ret;
}
int PAILLIER_up_ref(PAILLIER *r)

View File

@@ -116,8 +116,11 @@ static int pkey_paillier_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *
{
int ret = 0;
PAILLIER *key = EVP_PKEY_get0_PAILLIER(EVP_PKEY_CTX_get0_pkey(ctx));
char *buf = NULL;
BIGNUM *m = NULL;
BIGNUM *c = NULL;
ASN1_INTEGER *ai = NULL;
int len;
if (!out) {
*outlen = PAILLIER_size(key);
@@ -127,26 +130,66 @@ static int pkey_paillier_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *
return 0;
}
if (!(m = BN_new()) || !(c = BN_new())) {
/* parse plaintext in decimal string format */
if (!(buf = OPENSSL_malloc(inlen + 1))) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_MALLOC_FAILURE);
return 0;
}
memcpy(buf, in, inlen);
buf[inlen] = 0;
if (!BN_dec2bn(&m, buf)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, PAILLIER_R_INVALID_PLAINTEXT);
goto end;
}
if (!BN_bin2bn(in, (int)inlen, m)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_BN_LIB);
/* encrypt and encode in asn1 integer format */
if (!(c = BN_new())) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!PAILLIER_encrypt(c, m, key)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_PAILLIER_LIB);
goto end;
}
/* the ciphertext has no prefix zeros */
*outlen = BN_bn2bin(c, out);
if (!(ai = BN_to_ASN1_INTEGER(c, NULL))) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_ASN1_LIB);
goto end;
}
if ((len = i2d_ASN1_INTEGER(ai, &out)) <= 0) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_ENCRYPT, ERR_R_ASN1_LIB);
goto end;
}
*outlen = len;
ret = 1;
end:
OPENSSL_clear_free(buf, inlen);
BN_clear_free(m);
BN_free(c);
ASN1_INTEGER_free(ai);
return ret;
}
static size_t paillier_plaintext_size(PAILLIER *key)
{
size_t ret = 0;
BIGNUM *m = NULL;
char *dec = NULL;
int i;
if (!(i = BN_num_bits(key->n))
|| !(m = BN_new())
|| !BN_one(m)
|| !BN_lshift(m, m, i * 2)
|| !(dec = BN_bn2dec(m))) {
PAILLIERerr(PAILLIER_F_PAILLIER_PLAINTEXT_SIZE, ERR_R_BN_LIB);
goto end;
}
ret = strlen(dec) + 1;
end:
BN_free(m);
OPENSSL_free(dec);
return ret;
}
@@ -155,34 +198,57 @@ static int pkey_paillier_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *
{
int ret = 0;
PAILLIER *key = EVP_PKEY_get0_PAILLIER(EVP_PKEY_CTX_get0_pkey(ctx));
const unsigned char *p = in;
ASN1_INTEGER *ai = NULL;
BIGNUM *m = NULL;
BIGNUM *c = NULL;
char *str = NULL;
size_t maxlen;
if (!(maxlen = paillier_plaintext_size(key))) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_PAILLIER_LIB);
return 0;
}
if (!out) {
*outlen = PAILLIER_size(key);
*outlen = maxlen;
return 1;
} else if (*outlen < (size_t)PAILLIER_size(key)) {
} else if (*outlen < maxlen) {
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);
/* decode ciphertext from asn1 integer */
if (!(ai = d2i_ASN1_INTEGER(NULL, &p, inlen))) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_ASN1_LIB);
return 0;
}
if (!(c = ASN1_INTEGER_to_BN(ai, NULL))) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_ASN1_LIB);
goto end;
}
if (!BN_bin2bn(in, (int)inlen, c)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_BN_LIB);
/* decrypt and convert to decimal string */
if (!(m = BN_new())) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!PAILLIER_decrypt(m, c, key)) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_PAILLIER_LIB);
goto end;
}
if (!(str = BN_bn2dec(m))) {
PAILLIERerr(PAILLIER_F_PKEY_PAILLIER_DECRYPT, ERR_R_BN_LIB);
goto end;
}
/* the plaintext has no prefix zeros */
*outlen = BN_bn2bin(m, out);
strcpy((char *)out, str);
*outlen = strlen(str) + 1;
ret = 1;
end:
ASN1_INTEGER_free(ai);
OPENSSL_free(str);
BN_free(m);
BN_free(c);
return ret;