[openssl] master update

Richard Levitte levitte at openssl.org
Wed Jan 26 20:39:19 UTC 2022


The branch master has been updated
       via  748a2967ffd52cf86696582fb1074d513493f469 (commit)
       via  17898ec6011cc583c5af69ca8f25f5d165ff3e6a (commit)
       via  f1719858a05a9568ccbd052f160746cf4c027a9e (commit)
      from  3f6a12a07f52c55dc3f4b0def42680f589f89ed4 (commit)


- Log -----------------------------------------------------------------
commit 748a2967ffd52cf86696582fb1074d513493f469
Author: Richard Levitte <levitte at openssl.org>
Date:   Sun Nov 28 18:03:40 2021 +0100

    TEST: Add addition OSSL_PARAM tests for signed BIGNUMs
    
    Reviewed-by: Paul Dale <pauli at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/17162)

commit 17898ec6011cc583c5af69ca8f25f5d165ff3e6a
Author: Richard Levitte <levitte at openssl.org>
Date:   Thu Nov 25 18:46:32 2021 +0100

    Add support for signed BIGNUMs in the OSSL_PARAM_BLD API
    
    Reviewed-by: Paul Dale <pauli at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/17162)

commit f1719858a05a9568ccbd052f160746cf4c027a9e
Author: Richard Levitte <levitte at openssl.org>
Date:   Thu Nov 25 18:45:50 2021 +0100

    Add support for signed BIGNUMs in the OSSL_PARAM API
    
    Reviewed-by: Paul Dale <pauli at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/17162)

-----------------------------------------------------------------------

Summary of changes:
 crypto/param_build.c               |  45 ++++++++++----
 crypto/params.c                    |  57 ++++++++++++------
 doc/man3/OSSL_PARAM_BLD.pod        |  17 +++++-
 doc/man3/OSSL_PARAM_int.pod        |   9 ++-
 include/internal/param_build_set.h |   5 ++
 test/param_build_test.c            |  66 ++++++++++++++------
 test/params_api_test.c             | 120 +++++++++++++++++++++++++------------
 7 files changed, 222 insertions(+), 97 deletions(-)

diff --git a/crypto/param_build.c b/crypto/param_build.c
index a86f4baf17..918bebda6b 100644
--- a/crypto/param_build.c
+++ b/crypto/param_build.c
@@ -192,23 +192,20 @@ int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key,
     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL);
 }
 
-int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
-                           const BIGNUM *bn)
-{
-    return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn,
-                                      bn == NULL ? 0 : BN_num_bytes(bn));
-}
-
-int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
-                               const BIGNUM *bn, size_t sz)
+static int push_BN(OSSL_PARAM_BLD *bld, const char *key,
+                   const BIGNUM *bn, size_t sz, int type)
 {
     int n, secure = 0;
     OSSL_PARAM_BLD_DEF *pd;
 
+    if (!ossl_assert(type == OSSL_PARAM_UNSIGNED_INTEGER
+                     || type == OSSL_PARAM_INTEGER))
+        return 0;
+
     if (bn != NULL) {
-        if (BN_is_negative(bn)) {
+        if (type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(bn)) {
             ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
-                           "Negative big numbers are unsupported for OSSL_PARAM");
+                           "Negative big numbers are unsupported for OSSL_PARAM_UNSIGNED_INTEGER");
             return 0;
         }
 
@@ -224,13 +221,32 @@ int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
         if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE)
             secure = 1;
     }
-    pd = param_push(bld, key, sz, sz, OSSL_PARAM_UNSIGNED_INTEGER, secure);
+    pd = param_push(bld, key, sz, sz, type, secure);
     if (pd == NULL)
         return 0;
     pd->bn = bn;
     return 1;
 }
 
+int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
+                           const BIGNUM *bn)
+{
+    if (BN_is_negative(bn))
+        return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn) + 1,
+                       OSSL_PARAM_INTEGER);
+    return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn),
+                   OSSL_PARAM_UNSIGNED_INTEGER);
+}
+
+int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
+                               const BIGNUM *bn, size_t sz)
+{
+    if (BN_is_negative(bn))
+        return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn),
+                       OSSL_PARAM_INTEGER);
+    return push_BN(bld, key, bn, sz, OSSL_PARAM_UNSIGNED_INTEGER);
+}
+
 int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
                                     const char *buf, size_t bsize)
 {
@@ -328,7 +344,10 @@ static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param,
         param[i].data = p;
         if (pd->bn != NULL) {
             /* BIGNUM */
-            BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size);
+            if (pd->type == OSSL_PARAM_UNSIGNED_INTEGER)
+                BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size);
+            else
+                BN_signed_bn2native(pd->bn, (unsigned char *)p, pd->size);
         } else if (pd->type == OSSL_PARAM_OCTET_PTR
                    || pd->type == OSSL_PARAM_UTF8_PTR) {
             /* PTR */
diff --git a/crypto/params.c b/crypto/params.c
index cf86eea3c7..a1a04a6b4d 100644
--- a/crypto/params.c
+++ b/crypto/params.c
@@ -1018,24 +1018,32 @@ OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
 
 int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
 {
-    BIGNUM *b;
+    BIGNUM *b = NULL;
 
     if (val == NULL || p == NULL) {
         err_null_argument;
         return 0;
     }
-    if (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER) {
+
+    switch (p->data_type) {
+    case OSSL_PARAM_UNSIGNED_INTEGER:
+        b = BN_native2bn(p->data, (int)p->data_size, *val);
+        break;
+    case OSSL_PARAM_INTEGER:
+        b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
+        break;
+    default:
         err_bad_type;
-        return 0;
+        break;
     }
 
-    b = BN_native2bn(p->data, (int)p->data_size, *val);
-    if (b != NULL) {
-        *val = b;
-        return 1;
+    if (b == NULL) {
+        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
+        return 0;
     }
-    ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
-    return 0;
+
+    *val = b;
+    return 1;
 }
 
 int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
@@ -1051,26 +1059,37 @@ int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
         err_null_argument;
         return 0;
     }
-    if (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER) {
+    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
         err_bad_type;
         return 0;
     }
 
-    /* For the moment, only positive values are permitted */
-    if (BN_is_negative(val)) {
-        err_unsigned_negative;
-        return 0;
-    }
-
     bytes = (size_t)BN_num_bytes(val);
+    /* We add 1 byte for signed numbers, to make space for a sign extension */
+    if (p->data_type == OSSL_PARAM_INTEGER)
+        bytes++;
+
     p->return_size = bytes;
     if (p->data == NULL)
         return 1;
     if (p->data_size >= bytes) {
         p->return_size = p->data_size;
-        if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
-            return 1;
-        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
+
+        switch (p->data_type) {
+        case OSSL_PARAM_UNSIGNED_INTEGER:
+            if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
+                return 1;
+            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
+            break;
+        case OSSL_PARAM_INTEGER:
+            if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
+                return 1;
+            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
+            break;
+        default:
+            err_bad_type;
+            break;
+        }
         return 0;
     }
     err_too_small;
diff --git a/doc/man3/OSSL_PARAM_BLD.pod b/doc/man3/OSSL_PARAM_BLD.pod
index 114ce44489..8393cf0e92 100644
--- a/doc/man3/OSSL_PARAM_BLD.pod
+++ b/doc/man3/OSSL_PARAM_BLD.pod
@@ -73,8 +73,16 @@ OSSL_PARAM objects of the specified size and correct type for the I<val>
 argument.
 I<val> is stored by value and an expression or auto variable can be used.
 
+When B<I<TYPE>> denotes an integer type, signed integer types will normally
+get the OSSL_PARAM type B<OSSL_PARAM_INTEGER> params.
+When B<I<TYPE>> denotes an unsigned integer type will get the OSSL_PARAM type
+B<OSSL_PARAM_UNSIGNED_INTEGER>.
+
 OSSL_PARAM_BLD_push_BN() is a function that will create an OSSL_PARAM object
 that holds the specified BIGNUM I<bn>.
+When the I<bn> is zero or positive, its OSSL_PARAM type becomes
+B<OSSL_PARAM_UNSIGNED_INTEGER>.
+When the I<bn> is negative, its OSSL_PARAM type becomes B<OSSL_PARAM_INTEGER>.
 If I<bn> is marked as being securely allocated, its OSSL_PARAM representation
 will also be securely allocated.
 The I<bn> argument is stored by reference and the underlying BIGNUM object
@@ -84,6 +92,9 @@ OSSL_PARAM_BLD_push_BN_pad() is a function that will create an OSSL_PARAM object
 that holds the specified BIGNUM I<bn>.
 The object will be padded to occupy exactly I<sz> bytes, if insufficient space
 is specified an error results.
+When the I<bn> is zero or positive, its OSSL_PARAM type becomes
+B<OSSL_PARAM_UNSIGNED_INTEGER>.
+When the I<bn> is negative, its OSSL_PARAM type becomes B<OSSL_PARAM_INTEGER>.
 If I<bn> is marked as being securely allocated, its OSSL_PARAM representation
 will also be securely allocated.
 The I<bn> argument is stored by reference and the underlying BIGNUM object
@@ -126,8 +137,10 @@ on error.
 
 =head1 NOTES
 
-OSSL_PARAM_BLD_push_BN() and OSSL_PARAM_BLD_push_BN_pad() currently only
-support nonnegative B<BIGNUM>s.  They return an error on negative B<BIGNUM>s.
+OSSL_PARAM_BLD_push_BN() and OSSL_PARAM_BLD_push_BN_pad() only
+support nonnegative B<BIGNUM>s.  They return an error on negative
+B<BIGNUM>s.
+To pass signed B<BIGNUM>s, use OSSL_PARAM_BLD_push_signed_BN().
 
 =head1 EXAMPLES
 
diff --git a/doc/man3/OSSL_PARAM_int.pod b/doc/man3/OSSL_PARAM_int.pod
index 9e03ec4ddc..290855e676 100644
--- a/doc/man3/OSSL_PARAM_int.pod
+++ b/doc/man3/OSSL_PARAM_int.pod
@@ -331,11 +331,10 @@ representable by the target type or parameter.
 Apart from that, the functions must be used appropriately for the
 expected type of the parameter.
 
-OSSL_PARAM_get_BN() and OSSL_PARAM_set_BN() currently only support
-nonnegative B<BIGNUM>s, and by consequence, only
-B<OSSL_PARAM_UNSIGNED_INTEGER>.  OSSL_PARAM_construct_BN() currently
-constructs an B<OSSL_PARAM> structure with the data type
-B<OSSL_PARAM_UNSIGNED_INTEGER>.
+OSSL_PARAM_get_BN() and OSSL_PARAM_set_BN() only support nonnegative
+B<BIGNUM>s when the desired data type is B<OSSL_PARAM_UNSIGNED_INTEGER>.
+OSSL_PARAM_construct_BN() currently constructs an B<OSSL_PARAM> structure
+with the data type B<OSSL_PARAM_UNSIGNED_INTEGER>.
 
 For OSSL_PARAM_construct_utf8_ptr() and OSSL_PARAM_consstruct_octet_ptr(),
 I<bsize> is not relevant if the purpose is to send the B<OSSL_PARAM> array
diff --git a/include/internal/param_build_set.h b/include/internal/param_build_set.h
index 126211b7f2..8ca7720379 100644
--- a/include/internal/param_build_set.h
+++ b/include/internal/param_build_set.h
@@ -39,6 +39,11 @@ int ossl_param_build_set_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
                             const char *key, const BIGNUM *bn);
 int ossl_param_build_set_bn_pad(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
                                 const char *key, const BIGNUM *bn,  size_t sz);
+int ossl_param_build_set_signed_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
+                                   const char *key, const BIGNUM *bn);
+int ossl_param_build_set_signed_bn_pad(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
+                                       const char *key, const BIGNUM *bn,
+                                       size_t sz);
 int ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
                                       const char *names[],
                                       STACK_OF(BIGNUM_const) *stk);
diff --git a/test/param_build_test.c b/test/param_build_test.c
index f802b90dfc..8a83fa869d 100644
--- a/test/param_build_test.c
+++ b/test/param_build_test.c
@@ -20,7 +20,8 @@ static int template_public_test(int tstid)
 {
     OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
     OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
-    BIGNUM *bn = NULL, *bn_res = NULL;
+    BIGNUM *pbn = NULL, *pbn_res = NULL;
+    BIGNUM *nbn = NULL, *nbn_res = NULL;
     int i;
     long int l;
     int32_t i32;
@@ -37,9 +38,13 @@ static int template_public_test(int tstid)
         || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
         || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224))
         || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875))
-        || !TEST_ptr(bn = BN_new())
-        || !TEST_true(BN_set_word(bn, 1729))
-        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
+        || !TEST_ptr(pbn = BN_new())
+        || !TEST_true(BN_set_word(pbn, 1729))
+        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn))
+        || !TEST_ptr(nbn = BN_secure_new())
+        || !TEST_true(BN_set_word(nbn, 1733))
+        || !TEST_true((BN_set_negative(nbn, 1), 1))
+        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn))
         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo",
                                                       sizeof("foo")))
         || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
@@ -118,12 +123,18 @@ static int template_public_test(int tstid)
         || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
         || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
         || !TEST_str_eq(cutf, "bar-boom")
-        /* Check BN */
+        /* Check BN (positive BN becomes unsigned integer) */
         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
         || !TEST_str_eq(p->key, "bignumber")
         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
-        || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
-        || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
+        || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res))
+        || !TEST_BN_eq(pbn_res, pbn)
+        /* Check BN (negative BN becomes signed integer) */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber"))
+        || !TEST_str_eq(p->key, "negativebignumber")
+        || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
+        || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res))
+        || !TEST_BN_eq(nbn_res, nbn))
         goto err;
     res = 1;
 err:
@@ -133,8 +144,10 @@ err:
     OSSL_PARAM_free(params_blt);
     OSSL_PARAM_BLD_free(bld);
     OPENSSL_free(utf);
-    BN_free(bn);
-    BN_free(bn_res);
+    BN_free(pbn);
+    BN_free(pbn_res);
+    BN_free(nbn);
+    BN_free(nbn_res);
     return res;
 }
 
@@ -152,7 +165,8 @@ static int template_private_test(int tstid)
     uint32_t i32;
     uint64_t i64;
     size_t st;
-    BIGNUM *bn = NULL, *bn_res = NULL;
+    BIGNUM *pbn = NULL, *pbn_res = NULL;
+    BIGNUM *nbn = NULL, *nbn_res = NULL;
     int res = 0;
 
     if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size))
@@ -170,9 +184,13 @@ static int template_private_test(int tstid)
         || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
         || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
         || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
-        || !TEST_ptr(bn = BN_secure_new())
-        || !TEST_true(BN_set_word(bn, 1729))
-        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
+        || !TEST_ptr(pbn = BN_secure_new())
+        || !TEST_true(BN_set_word(pbn, 1729))
+        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn))
+        || !TEST_ptr(nbn = BN_secure_new())
+        || !TEST_true(BN_set_word(nbn, 1733))
+        || !TEST_true((BN_set_negative(nbn, 1), 1))
+        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn))
         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1,
                                                        data1_size))
         || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
@@ -251,14 +269,22 @@ static int template_private_test(int tstid)
         || !TEST_str_eq(p->key, "oct_p")
         || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
         || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size)
-        /* Check BN */
+        /* Check BN (positive BN becomes unsigned integer) */
         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
         || !TEST_true(CRYPTO_secure_allocated(p->data))
         || !TEST_str_eq(p->key, "bignumber")
         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
-        || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
-        || !TEST_int_eq(BN_get_flags(bn, BN_FLG_SECURE), BN_FLG_SECURE)
-        || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
+        || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res))
+        || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE)
+        || !TEST_BN_eq(pbn_res, pbn)
+        /* Check BN (negative BN becomes signed integer) */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber"))
+        || !TEST_true(CRYPTO_secure_allocated(p->data))
+        || !TEST_str_eq(p->key, "negativebignumber")
+        || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
+        || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res))
+        || !TEST_int_eq(BN_get_flags(nbn, BN_FLG_SECURE), BN_FLG_SECURE)
+        || !TEST_BN_eq(nbn_res, nbn))
         goto err;
     res = 1;
 err:
@@ -269,8 +295,10 @@ err:
     OSSL_PARAM_BLD_free(bld);
     OPENSSL_secure_free(data1);
     OPENSSL_secure_free(data2);
-    BN_free(bn);
-    BN_free(bn_res);
+    BN_free(pbn);
+    BN_free(pbn_res);
+    BN_free(nbn);
+    BN_free(nbn_res);
     return res;
 }
 
diff --git a/test/params_api_test.c b/test/params_api_test.c
index 62e458c993..d073477d5a 100644
--- a/test/params_api_test.c
+++ b/test/params_api_test.c
@@ -34,14 +34,18 @@ static void swap_copy(unsigned char *out, const void *in, size_t len)
  * big endian machine copying from native to or from little endian involves
  * byte reversal.
  */
-static void le_copy(unsigned char *out, const void *in, size_t len)
+static void le_copy(unsigned char *out, size_t outlen,
+                    const void *in, size_t inlen)
 {
     DECLARE_IS_ENDIAN;
 
-    if (IS_LITTLE_ENDIAN)
-        memcpy(out, in, len);
-    else
-        swap_copy(out, in, len);
+    if (IS_LITTLE_ENDIAN) {
+        memcpy(out, in, outlen);
+    } else {
+        if (outlen < inlen)
+            in = (const char *)in + inlen - outlen;
+        swap_copy(out, in, outlen);
+    }
 }
 
 static const struct {
@@ -98,17 +102,17 @@ static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
 
     /* Check signed types */
     if (bit32) {
-        le_copy(buf, &i32, sizeof(i32));
+        le_copy(buf, sizeof(i32), &i32, sizeof(i32));
         sz = sizeof(i32) < width ? sizeof(i32) : width;
         if (!TEST_mem_eq(buf, sz, cmp, sz))
             return 0;
     }
-    le_copy(buf, &i64, sizeof(i64));
+    le_copy(buf, sizeof(i64), &i64, sizeof(i64));
     sz = sizeof(i64) < width ? sizeof(i64) : width;
     if (!TEST_mem_eq(buf, sz, cmp, sz))
         return 0;
     if (sizet && !signd) {
-        le_copy(buf, &s, sizeof(s));
+        le_copy(buf, sizeof(s), &s, sizeof(s));
         sz = sizeof(s) < width ? sizeof(s) : width;
         if (!TEST_mem_eq(buf, sz, cmp, sz))
             return 0;
@@ -154,18 +158,18 @@ static int test_param_int(int n)
     OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_int(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_int(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -181,18 +185,18 @@ static int test_param_long(int n)
     OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_long(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_long(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -207,18 +211,18 @@ static int test_param_uint(int n)
     OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_uint(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_uint(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -234,18 +238,18 @@ static int test_param_ulong(int n)
     OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_ulong(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -261,18 +265,18 @@ static int test_param_int32(int n)
     OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_int32(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_int32(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -288,18 +292,18 @@ static int test_param_uint32(int n)
     OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_uint32(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -315,18 +319,18 @@ static int test_param_int64(int n)
     OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_int64(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_int64(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -342,18 +346,18 @@ static int test_param_uint64(int n)
     OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_uint64(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -369,18 +373,18 @@ static int test_param_size_t(int n)
     OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_size_t(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -396,18 +400,18 @@ static int test_param_time_t(int n)
     OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL);
 
     memset(buf, 0, sizeof(buf));
-    le_copy(buf, raw_values[n].value, sizeof(in));
+    le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
     memcpy(&in, buf, sizeof(in));
     param.data = &out;
     if (!TEST_true(OSSL_PARAM_set_time_t(&param, in)))
         return 0;
-    le_copy(cmp, &out, sizeof(out));
+    le_copy(cmp, sizeof(out), &out, sizeof(out));
     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
         return 0;
     in = 0;
     if (!TEST_true(OSSL_PARAM_get_time_t(&param, &in)))
         return 0;
-    le_copy(cmp, &in, sizeof(in));
+    le_copy(cmp, sizeof(in), &in, sizeof(in));
     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
         return 0;
     param.data = &out;
@@ -426,7 +430,7 @@ static int test_param_bignum(int n)
     param.data = bnbuf;
     param.data_size = len;
 
-    le_copy(buf, raw_values[n].value, len);
+    le_copy(buf, len, raw_values[n].value, len);
     if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
         goto err;
 
@@ -445,6 +449,43 @@ err:
     return ret;
 }
 
+static int test_param_signed_bignum(int n)
+{
+    unsigned char buf1[MAX_LEN], buf2[MAX_LEN], bnbuf[MAX_LEN];
+    const size_t len = raw_values[n].len;
+    BIGNUM *b = NULL, *c = NULL;
+    OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_INTEGER, NULL, 0);
+    int ret = 0;
+
+    param.data = bnbuf;
+    param.data_size = sizeof(bnbuf);
+
+    le_copy(buf1, len, raw_values[n].value, len);
+    if (!TEST_ptr(b = BN_signed_lebin2bn(raw_values[n].value, (int)len, NULL)))
+        goto err;
+
+    /* raw_values are little endian */
+    if (!TEST_false(!!(raw_values[n].value[len - 1] & 0x80) ^ BN_is_negative(b)))
+        goto err;
+    if (!TEST_true(OSSL_PARAM_set_BN(&param, b)))
+        goto err;
+    le_copy(buf2, len, bnbuf, sizeof(bnbuf));
+    if (!TEST_mem_eq(buf1, len, buf2, len))
+        goto err;
+    param.data_size = param.return_size;
+    if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
+        || !TEST_BN_eq(b, c)) {
+        BN_print_fp(stderr, c);
+        goto err;
+    }
+
+    ret = 1;
+err:
+    BN_free(b);
+    BN_free(c);
+    return ret;
+}
+
 static int test_param_real(void)
 {
     double p;
@@ -706,6 +747,7 @@ int setup_tests(void)
     ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
     ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
     ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
+    ADD_ALL_TESTS(test_param_signed_bignum, OSSL_NELEM(raw_values));
     ADD_TEST(test_param_real);
     ADD_ALL_TESTS(test_param_construct, 4);
     ADD_TEST(test_param_modified);


More information about the openssl-commits mailing list