2016-05-18 02:20:24 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2016-05-18 02:20:24 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
2016-11-29 01:26:05 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2016-11-29 01:26:05 +08:00
|
|
|
#include <ctype.h>
|
1999-05-21 05:59:20 +08:00
|
|
|
|
2001-02-22 22:45:02 +08:00
|
|
|
#include "e_os.h"
|
2016-11-29 01:26:05 +08:00
|
|
|
#include <internal/numbers.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
2016-11-29 01:26:05 +08:00
|
|
|
#include <openssl/crypto.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/err.h>
|
2016-11-29 01:26:05 +08:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include "testutil.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-03-11 18:38:14 +08:00
|
|
|
/*
|
|
|
|
* In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
|
|
|
|
* This is fine in itself, it will end up as an unused static function in
|
2016-11-29 01:26:05 +08:00
|
|
|
* the worst case. However, it references bn_expand2(), which is a private
|
2016-03-11 18:38:14 +08:00
|
|
|
* function in libcrypto and therefore unavailable on some systems. This
|
|
|
|
* may result in a linker error because of unresolved symbols.
|
|
|
|
*
|
|
|
|
* To avoid this, we define a dummy variant of bn_expand2() here, and to
|
|
|
|
* avoid possible clashes with libcrypto, we rename it first, using a macro.
|
|
|
|
*/
|
|
|
|
#define bn_expand2 dummy_bn_expand2
|
2016-03-12 09:36:33 +08:00
|
|
|
BIGNUM *bn_expand2(BIGNUM *b, int words);
|
2016-03-12 02:22:58 +08:00
|
|
|
BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
|
2014-05-27 21:48:21 +08:00
|
|
|
#include "../crypto/bn/bn_lcl.h"
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
#define MAXPAIRS 20
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/*
|
|
|
|
* Things in boring, not in openssl. TODO we should add them.
|
|
|
|
*/
|
|
|
|
#define HAVE_BN_PADDED 0
|
|
|
|
#define HAVE_BN_SQRT 0
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
typedef struct pair_st {
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
} PAIR;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
typedef struct stanza_st {
|
2017-05-23 20:59:49 +08:00
|
|
|
int curr; /* Current line in file */
|
|
|
|
int start; /* Line where test starts */
|
2016-11-29 01:26:05 +08:00
|
|
|
int numpairs;
|
|
|
|
PAIR pairs[MAXPAIRS];
|
|
|
|
} STANZA;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
typedef struct filetest_st {
|
|
|
|
const char *name;
|
|
|
|
int (*func)(STANZA *s);
|
|
|
|
} FILETEST;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
typedef struct mpitest_st {
|
|
|
|
const char *base10;
|
|
|
|
const char *mpi;
|
|
|
|
size_t mpi_len;
|
|
|
|
} MPITEST;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static const int NUM0 = 100; /* number of tests */
|
|
|
|
static const int NUM1 = 50; /* additional tests for some functions */
|
2017-05-16 02:49:37 +08:00
|
|
|
static BIO *fp;
|
2016-11-29 01:26:05 +08:00
|
|
|
static BN_CTX *ctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
/*
|
|
|
|
* Polynomial coefficients used in GFM tests.
|
|
|
|
*/
|
|
|
|
static int p0[] = { 163, 7, 6, 3, 0, -1 };
|
|
|
|
static int p1[] = { 193, 15, 0, -1 };
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/*
|
|
|
|
* Look for |key| in the stanza and return it or NULL if not found.
|
|
|
|
*/
|
|
|
|
static const char *findattr(STANZA *s, const char *key)
|
|
|
|
{
|
|
|
|
int i = s->numpairs;
|
|
|
|
PAIR *pp = s->pairs;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for ( ; --i >= 0; pp++)
|
|
|
|
if (strcasecmp(pp->key, key) == 0)
|
|
|
|
return pp->value;
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/*
|
|
|
|
* Parse BIGNUM, return number of bytes parsed.
|
|
|
|
*/
|
|
|
|
static int parseBN(BIGNUM **out, const char *in)
|
|
|
|
{
|
|
|
|
*out = NULL;
|
|
|
|
return BN_hex2bn(out, in);
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int parsedecBN(BIGNUM **out, const char *in)
|
|
|
|
{
|
|
|
|
*out = NULL;
|
|
|
|
return BN_dec2bn(out, in);
|
|
|
|
}
|
2013-04-24 02:36:06 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static BIGNUM *getBN(STANZA *s, const char *attribute)
|
|
|
|
{
|
|
|
|
const char *hex;
|
|
|
|
BIGNUM *ret = NULL;
|
2016-08-26 05:45:20 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
if ((hex = findattr(s, attribute)) == NULL) {
|
2017-04-27 00:39:46 +08:00
|
|
|
TEST_error("Can't find %s in test at line %d", attribute, s->start);
|
2016-11-29 01:26:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
if (parseBN(&ret, hex) != (int)strlen(hex)) {
|
2017-04-27 00:39:46 +08:00
|
|
|
TEST_error("Could not decode '%s'", hex);
|
2016-11-29 01:26:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int getint(STANZA *s, int *out, const char *attribute)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *ret;
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_ULONG word;
|
|
|
|
int st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(ret = getBN(s, attribute))
|
|
|
|
|| !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
*out = (int)word;
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(ret);
|
|
|
|
return st;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
|
|
|
|
{
|
|
|
|
if (BN_cmp(expected, actual) == 0)
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-05-08 10:09:41 +08:00
|
|
|
TEST_error("unexpected %s value", op);
|
|
|
|
TEST_BN_eq(expected, actual);
|
2016-11-29 01:26:05 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/*
|
|
|
|
* Return a "random" flag for if a BN should be negated.
|
|
|
|
*/
|
|
|
|
static int rand_neg(void)
|
|
|
|
{
|
|
|
|
static unsigned int neg = 0;
|
|
|
|
static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
return sign[(neg++) % 8];
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
static int test_sub()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *c = NULL;
|
|
|
|
int i, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0 + NUM1; i++) {
|
|
|
|
if (i < NUM1) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_bntest_rand(a, 512, 0, 0);
|
|
|
|
BN_copy(b, a);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_ne(BN_set_bit(a, i), 0))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_add_word(b, i);
|
|
|
|
} else {
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
|
2015-01-22 11:40:55 +08:00
|
|
|
a->neg = rand_neg();
|
|
|
|
b->neg = rand_neg();
|
|
|
|
}
|
|
|
|
BN_sub(c, a, b);
|
|
|
|
BN_add(c, c, b);
|
|
|
|
BN_sub(c, c, a);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(c))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-04-27 00:39:46 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(c);
|
2017-04-27 00:39:46 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2004-07-22 17:32:11 +08:00
|
|
|
|
2004-06-18 04:03:56 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_div_recip()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
|
|
|
|
BN_RECP_CTX *recp = NULL;
|
|
|
|
int st = 0, i;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new())
|
|
|
|
|| !TEST_ptr(recp = BN_RECP_CTX_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0 + NUM1; i++) {
|
|
|
|
if (i < NUM1) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_bntest_rand(a, 400, 0, 0);
|
|
|
|
BN_copy(b, a);
|
|
|
|
BN_lshift(a, a, i);
|
|
|
|
BN_add_word(a, i);
|
|
|
|
} else
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
|
2015-01-22 11:40:55 +08:00
|
|
|
a->neg = rand_neg();
|
|
|
|
b->neg = rand_neg();
|
|
|
|
BN_RECP_CTX_set(recp, b, ctx);
|
|
|
|
BN_div_recp(d, c, a, recp, ctx);
|
|
|
|
BN_mul(e, d, b, ctx);
|
|
|
|
BN_add(d, e, c);
|
|
|
|
BN_sub(d, d, a);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(d))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-04-27 00:39:46 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
|
|
|
BN_RECP_CTX_free(recp);
|
2017-04-27 00:39:46 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
static int test_mod()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
|
|
|
|
int st = 0, i;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_bntest_rand(a, 1024, 0, 0);
|
|
|
|
for (i = 0; i < NUM0; i++) {
|
|
|
|
BN_bntest_rand(b, 450 + i * 10, 0, 0);
|
2015-01-22 11:40:55 +08:00
|
|
|
a->neg = rand_neg();
|
|
|
|
b->neg = rand_neg();
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_mod(c, a, b, ctx);
|
|
|
|
BN_div(d, e, a, b, ctx);
|
|
|
|
BN_sub(e, e, c);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(e))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-04-27 00:39:46 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
2017-04-27 00:39:46 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2017-01-27 03:42:56 +08:00
|
|
|
static const char *bn1strings[] = {
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *bn2strings[] = {
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
|
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *glue(const char *list[])
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
char *p, *save;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; list[i] != NULL; i++)
|
|
|
|
len += strlen(list[i]);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; list[i] != NULL; i++)
|
|
|
|
p += strlen(strcpy(p, list[i]));
|
2017-01-27 03:42:56 +08:00
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/*
|
|
|
|
* Test constant-time modular exponentiation with 1024-bit inputs, which on
|
|
|
|
* x86_64 cause a different code branch to be taken.
|
|
|
|
*/
|
|
|
|
static int test_modexp_mont5()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
|
|
|
|
BIGNUM *b = NULL, *n = NULL, *c = NULL;
|
|
|
|
BN_MONT_CTX *mont = NULL;
|
2017-01-27 03:42:56 +08:00
|
|
|
char *bigstring;
|
2017-04-27 00:39:46 +08:00
|
|
|
int st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(p = BN_new())
|
|
|
|
|| !TEST_ptr(m = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(n = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(mont = BN_MONT_CTX_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
|
|
|
|
/* Zero exponent */
|
|
|
|
BN_bntest_rand(a, 1024, 0, 0);
|
|
|
|
BN_zero(p);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
|
|
|
|
goto err;
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_one(d))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Regression test for carry bug in mulx4x_mont */
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_hex2bn(&a,
|
2016-11-29 01:26:05 +08:00
|
|
|
"7878787878787878787878787878787878787878787878787878787878787878"
|
|
|
|
"7878787878787878787878787878787878787878787878787878787878787878"
|
|
|
|
"7878787878787878787878787878787878787878787878787878787878787878"
|
|
|
|
"7878787878787878787878787878787878787878787878787878787878787878");
|
|
|
|
BN_hex2bn(&b,
|
|
|
|
"095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
|
|
|
|
"E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
|
|
|
|
"9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
|
|
|
|
"9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
|
|
|
|
BN_hex2bn(&n,
|
|
|
|
"D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
|
|
|
|
"91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
|
|
|
|
"D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
|
|
|
|
"2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
|
|
|
|
BN_MONT_CTX_set(mont, n, ctx);
|
|
|
|
BN_mod_mul_montgomery(c, a, b, mont, ctx);
|
|
|
|
BN_mod_mul_montgomery(d, b, a, mont, ctx);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq(c, d))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-01-22 04:28:18 +08:00
|
|
|
/* Regression test for carry bug in sqr[x]8x_mont */
|
2017-01-27 03:42:56 +08:00
|
|
|
bigstring = glue(bn1strings);
|
|
|
|
BN_hex2bn(&n, bigstring);
|
|
|
|
OPENSSL_free(bigstring);
|
|
|
|
bigstring = glue(bn2strings);
|
|
|
|
BN_hex2bn(&a, bigstring);
|
|
|
|
OPENSSL_free(bigstring);
|
|
|
|
BN_free(b);
|
2017-01-22 04:28:18 +08:00
|
|
|
b = BN_dup(a);
|
|
|
|
BN_MONT_CTX_set(mont, n, ctx);
|
|
|
|
BN_mod_mul_montgomery(c, a, a, mont, ctx);
|
|
|
|
BN_mod_mul_montgomery(d, a, b, mont, ctx);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq(c, d))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2017-01-22 04:28:18 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Zero input */
|
|
|
|
BN_bntest_rand(p, 1024, 0, 0);
|
|
|
|
BN_zero(a);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(d))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/*
|
|
|
|
* Craft an input whose Montgomery representation is 1, i.e., shorter
|
|
|
|
* than the modulus m, in order to test the const time precomputation
|
|
|
|
* scattering/gathering.
|
|
|
|
*/
|
|
|
|
BN_one(a);
|
|
|
|
BN_MONT_CTX_set(mont, m, ctx);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
|
|
|
|
|| !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
|
|
|
|
|| !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq(a, d))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Finally, some regular test vectors. */
|
|
|
|
BN_bntest_rand(e, 1024, 0, 0);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
|
|
|
|
|| !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq(a, d))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_MONT_CTX_free(mont);
|
|
|
|
BN_free(a);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(p);
|
|
|
|
BN_free(m);
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(d);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(e);
|
|
|
|
BN_free(b);
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(n);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(c);
|
2017-04-27 00:39:46 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
#ifndef OPENSSL_NO_EC2M
|
|
|
|
static int test_gf2m_add()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *c = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
|
|
|
BN_rand(a, 512, 0, 0);
|
|
|
|
BN_copy(b, BN_value_one());
|
2015-01-22 11:40:55 +08:00
|
|
|
a->neg = rand_neg();
|
|
|
|
b->neg = rand_neg();
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_GF2m_add(c, a, b);
|
|
|
|
/* Test that two added values have the correct parity. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
|
|
|
|
|| (!BN_is_odd(a) && !BN_is_odd(c))))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_GF2m_add(c, c, c);
|
|
|
|
/* Test that c + c = 0. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(c))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(c);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_mod()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
|
|
|
BN_bntest_rand(a, 1024, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
BN_GF2m_mod(c, a, b[j]);
|
|
|
|
BN_GF2m_add(d, a, c);
|
|
|
|
BN_GF2m_mod(e, d, b[j]);
|
|
|
|
/* Test that a + (a mod p) mod p == 0. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(e))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(a);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_mul()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
|
|
|
|
BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, st = 0;
|
2017-04-27 00:39:46 +08:00
|
|
|
|
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new())
|
|
|
|
|| !TEST_ptr(f = BN_new())
|
|
|
|
|| !TEST_ptr(g = BN_new())
|
|
|
|
|| !TEST_ptr(h = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
|
|
|
BN_bntest_rand(a, 1024, 0, 0);
|
|
|
|
BN_bntest_rand(c, 1024, 0, 0);
|
|
|
|
BN_bntest_rand(d, 1024, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
BN_GF2m_mod_mul(e, a, c, b[j], ctx);
|
|
|
|
BN_GF2m_add(f, a, d);
|
|
|
|
BN_GF2m_mod_mul(g, f, c, b[j], ctx);
|
|
|
|
BN_GF2m_mod_mul(h, d, c, b[j], ctx);
|
|
|
|
BN_GF2m_add(f, e, g);
|
|
|
|
BN_GF2m_add(f, f, h);
|
|
|
|
/* Test that (a+d)*c = a*c + d*c. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(f))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2015-12-01 16:00:32 +08:00
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
2017-04-27 00:39:46 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(a);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(f);
|
|
|
|
BN_free(g);
|
|
|
|
BN_free(h);
|
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_sqr()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new()))
|
|
|
|
goto err;
|
2015-08-31 21:51:27 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
|
|
|
BN_bntest_rand(a, 1024, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
BN_GF2m_mod_sqr(c, a, b[j], ctx);
|
|
|
|
BN_copy(d, a);
|
|
|
|
BN_GF2m_mod_mul(d, a, d, b[j], ctx);
|
|
|
|
BN_GF2m_add(d, c, d);
|
|
|
|
/* Test that a*a = a^2. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(d))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(a);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_modinv()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_bntest_rand(a, 512, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
BN_GF2m_mod_inv(c, a, b[j], ctx);
|
|
|
|
BN_GF2m_mod_mul(d, a, c, b[j], ctx);
|
|
|
|
/* Test that ((1/a)*a) = 1. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_one(d))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_moddiv()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
|
|
|
|
BIGNUM *e = NULL, *f = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new())
|
|
|
|
|| !TEST_ptr(f = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_bntest_rand(a, 512, 0, 0);
|
|
|
|
BN_bntest_rand(c, 512, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
BN_GF2m_mod_div(d, a, c, b[j], ctx);
|
|
|
|
BN_GF2m_mod_mul(e, d, c, b[j], ctx);
|
|
|
|
BN_GF2m_mod_div(f, a, e, b[j], ctx);
|
|
|
|
/* Test that ((a/c)*c)/a = 1. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_one(f))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(f);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_modexp()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
|
|
|
|
BIGNUM *e = NULL, *f = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new())
|
|
|
|
|| !TEST_ptr(f = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_bntest_rand(a, 512, 0, 0);
|
|
|
|
BN_bntest_rand(c, 512, 0, 0);
|
|
|
|
BN_bntest_rand(d, 512, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
BN_GF2m_mod_exp(e, a, c, b[j], ctx);
|
|
|
|
BN_GF2m_mod_exp(f, a, d, b[j], ctx);
|
|
|
|
BN_GF2m_mod_mul(e, e, f, b[j], ctx);
|
|
|
|
BN_add(f, c, d);
|
|
|
|
BN_GF2m_mod_exp(f, a, f, b[j], ctx);
|
|
|
|
BN_GF2m_add(f, e, f);
|
|
|
|
/* Test that a^(c+d)=a^c*a^d. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(f))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(f);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_modsqrt()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
|
|
|
|
BIGNUM *e = NULL, *f = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new())
|
|
|
|
|| !TEST_ptr(f = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_bntest_rand(a, 512, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
BN_GF2m_mod(c, a, b[j]);
|
|
|
|
BN_GF2m_mod_sqrt(d, a, b[j], ctx);
|
|
|
|
BN_GF2m_mod_sqr(e, d, b[j], ctx);
|
|
|
|
BN_GF2m_add(f, c, e);
|
|
|
|
/* Test that d^2 = a, where d = sqrt(a). */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(f))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(f);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_gf2m_modsolvequad()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
|
|
|
|
BIGNUM *e = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int i, j, s = 0, t, st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b[0] = BN_new())
|
|
|
|
|| !TEST_ptr(b[1] = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new())
|
|
|
|
|| !TEST_ptr(e = BN_new()))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
BN_GF2m_arr2poly(p0, b[0]);
|
|
|
|
BN_GF2m_arr2poly(p1, b[1]);
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (i = 0; i < NUM0; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_bntest_rand(a, 512, 0, 0);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
|
|
|
|
if (t) {
|
|
|
|
s++;
|
|
|
|
BN_GF2m_mod_sqr(d, c, b[j], ctx);
|
|
|
|
BN_GF2m_add(d, c, d);
|
|
|
|
BN_GF2m_mod(e, a, b[j]);
|
|
|
|
BN_GF2m_add(e, e, d);
|
|
|
|
/*
|
|
|
|
* Test that solution of quadratic c satisfies c^2 + c = a.
|
|
|
|
*/
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(e))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_ge(s, 0)) {
|
|
|
|
TEST_info("%d tests found no roots; probably an error", NUM0);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b[0]);
|
|
|
|
BN_free(b[1]);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
2016-11-29 01:26:05 +08:00
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2011-02-13 01:23:32 +08:00
|
|
|
#endif
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
static int test_kronecker()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
|
|
|
|
int i, legendre, kronecker, st = 0;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(r = BN_new())
|
|
|
|
|| !TEST_ptr(t = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
|
|
|
|
* this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
|
|
|
|
* congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
|
|
|
|
* generate a random prime b and compare these values for a number of
|
|
|
|
* random a's. (That is, we run the Solovay-Strassen primality test to
|
|
|
|
* confirm that b is prime, except that we don't want to test whether b
|
|
|
|
* is prime but whether BN_kronecker works.)
|
|
|
|
*/
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
b->neg = rand_neg();
|
|
|
|
|
|
|
|
for (i = 0; i < NUM0; i++) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
a->neg = rand_neg();
|
|
|
|
|
|
|
|
/* t := (|b|-1)/2 (note that b is odd) */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_copy(t, b)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
t->neg = 0;
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_sub_word(t, 1)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_rshift1(t, t)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
/* r := a^t mod b */
|
|
|
|
b->neg = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
b->neg = 1;
|
|
|
|
|
|
|
|
if (BN_is_word(r, 1))
|
|
|
|
legendre = 1;
|
|
|
|
else if (BN_is_zero(r))
|
|
|
|
legendre = 0;
|
|
|
|
else {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_add_word(r, 1)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
|
|
|
|
TEST_info("Legendre symbol computation failed");
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
legendre = -1;
|
|
|
|
}
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
/* we actually need BN_kronecker(a, |b|) */
|
|
|
|
if (a->neg && b->neg)
|
|
|
|
kronecker = -kronecker;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(legendre, kronecker))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(r);
|
|
|
|
BN_free(t);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_sum(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_ULONG b_word;
|
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(b = getBN(s, "B"))
|
|
|
|
|| !TEST_ptr(sum = getBN(s, "Sum"))
|
|
|
|
|| !TEST_ptr(ret = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_add(ret, a, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A + B", sum, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_sub(ret, sum, a))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum - A", b, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_sub(ret, sum, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum - B", a, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that the functions work when |r| and |a| point to the same BIGNUM,
|
|
|
|
* or when |r| and |b| point to the same BIGNUM.
|
|
|
|
* TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
|
|
|
|
*/
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_copy(ret, a))
|
|
|
|
|| !TEST_true(BN_add(ret, ret, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A + B (r is a)", sum, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, b))
|
|
|
|
|| !TEST_true(BN_add(ret, a, ret))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A + B (r is b)", sum, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, sum))
|
|
|
|
|| !TEST_true(BN_sub(ret, ret, a))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum - A (r is a)", b, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, a))
|
|
|
|
|| !TEST_true(BN_sub(ret, sum, ret))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum - A (r is b)", b, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, sum))
|
|
|
|
|| !TEST_true(BN_sub(ret, ret, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum - B (r is a)", a, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, b))
|
|
|
|
|| !TEST_true(BN_sub(ret, sum, ret))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum - B (r is b)", a, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test BN_uadd() and BN_usub() with the prerequisites they are
|
|
|
|
* documented as having. Note that these functions are frequently used
|
|
|
|
* when the prerequisites don't hold. In those cases, they are supposed
|
|
|
|
* to work as if the prerequisite hold, but we don't test that yet.
|
|
|
|
* TODO: test that.
|
|
|
|
*/
|
|
|
|
if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_uadd(ret, a, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A +u B", sum, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_usub(ret, sum, a))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum -u A", b, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_usub(ret, sum, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum -u B", a, ret))
|
|
|
|
goto err;
|
|
|
|
/*
|
|
|
|
* Test that the functions work when |r| and |a| point to the same
|
|
|
|
* BIGNUM, or when |r| and |b| point to the same BIGNUM.
|
|
|
|
* TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
|
|
|
|
*/
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_copy(ret, a))
|
|
|
|
|| !TEST_true(BN_uadd(ret, ret, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A +u B (r is a)", sum, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, b))
|
|
|
|
|| !TEST_true(BN_uadd(ret, a, ret))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A +u B (r is b)", sum, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, sum))
|
|
|
|
|| !TEST_true(BN_usub(ret, ret, a))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum -u A (r is a)", b, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, a))
|
|
|
|
|| !TEST_true(BN_usub(ret, sum, ret))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum -u A (r is b)", b, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, sum))
|
|
|
|
|| !TEST_true(BN_usub(ret, ret, b))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum -u B (r is a)", a, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, b))
|
|
|
|
|| !TEST_true(BN_usub(ret, sum, ret))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum -u B (r is b)", a, ret))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test with BN_add_word() and BN_sub_word() if |b| is small enough.
|
|
|
|
*/
|
|
|
|
b_word = BN_get_word(b);
|
|
|
|
if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_copy(ret, a))
|
|
|
|
|| !TEST_true(BN_add_word(ret, b_word))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A + B (word)", sum, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_copy(ret, sum))
|
|
|
|
|| !TEST_true(BN_sub_word(ret, b_word))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Sum - B (word)", a, ret))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
st = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(sum);
|
|
|
|
BN_free(ret);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_lshift1(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
|
|
|
|
BIGNUM *two = NULL, *remainder = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(lshift1 = getBN(s, "LShift1"))
|
|
|
|
|| !TEST_ptr(zero = BN_new())
|
|
|
|
|| !TEST_ptr(ret = BN_new())
|
|
|
|
|| !TEST_ptr(two = BN_new())
|
|
|
|
|| !TEST_ptr(remainder = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
BN_zero(zero);
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_set_word(two, 2))
|
|
|
|
|| !TEST_true(BN_add(ret, a, a))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A + A", lshift1, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_mul(ret, a, two, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A * 2", lshift1, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("LShift1 / 2", a, ret)
|
|
|
|
|| !equalBN("LShift1 % 2", zero, remainder)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_lshift1(ret, a))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A << 1", lshift1, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_rshift1(ret, lshift1))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("LShift >> 1", a, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_rshift1(ret, lshift1))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("LShift >> 1", a, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Set the LSB to 1 and test rshift1 again. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_set_bit(lshift1, 0))
|
|
|
|
|| !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("(LShift1 | 1) / 2", a, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_rshift1(ret, lshift1))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("(LShift | 1) >> 1", a, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(lshift1);
|
|
|
|
BN_free(zero);
|
|
|
|
BN_free(ret);
|
|
|
|
BN_free(two);
|
|
|
|
BN_free(remainder);
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_lshift(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
|
|
|
|
int n = 0, st = 0;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(lshift = getBN(s, "LShift"))
|
|
|
|
|| !TEST_ptr(ret = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_lshift(ret, a, n))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A << N", lshift, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_rshift(ret, lshift, n))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A >> N", a, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(lshift);
|
|
|
|
BN_free(ret);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_rshift(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
|
|
|
|
int n = 0, st = 0;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(rshift = getBN(s, "RShift"))
|
|
|
|
|| !TEST_ptr(ret = BN_new())
|
|
|
|
|| !getint(s, &n, "N"))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_rshift(ret, a, n))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A >> N", rshift, ret))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2017-02-01 07:46:58 +08:00
|
|
|
|
|
|
|
/* If N == 1, try with rshift1 as well */
|
|
|
|
if (n == 1) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_rshift1(ret, a))
|
2017-02-01 07:46:58 +08:00
|
|
|
|| !equalBN("A >> 1 (rshift1)", rshift, ret))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2017-02-01 07:46:58 +08:00
|
|
|
}
|
2017-04-27 00:39:46 +08:00
|
|
|
st = 1;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(rshift);
|
|
|
|
BN_free(ret);
|
2017-04-27 00:39:46 +08:00
|
|
|
return st;
|
2016-11-29 01:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int file_square(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
|
|
|
|
BIGNUM *remainder = NULL, *tmp = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(square = getBN(s, "Square"))
|
|
|
|
|| !TEST_ptr(zero = BN_new())
|
|
|
|
|| !TEST_ptr(ret = BN_new())
|
|
|
|
|| !TEST_ptr(remainder = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
BN_zero(zero);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_sqr(ret, a, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A^2", square, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_mul(ret, a, a, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A * A", square, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_div(ret, remainder, square, a, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Square / A", a, ret)
|
|
|
|
|| !equalBN("Square % A", zero, remainder))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
#if HAVE_BN_SQRT
|
|
|
|
BN_set_negative(a, 0);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_sqrt(ret, square, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("sqrt(Square)", a, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* BN_sqrt should fail on non-squares and negative numbers. */
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(square)) {
|
|
|
|
if (!TEST_ptr(tmp = BN_new())
|
|
|
|
|| !TEST_true(BN_copy(tmp, square)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_set_negative(tmp, 1);
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
|
|
|
|
|
|
|
BN_set_negative(tmp, 0);
|
|
|
|
if (BN_add(tmp, tmp, BN_value_one()))
|
|
|
|
goto err;
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(square);
|
|
|
|
BN_free(zero);
|
|
|
|
BN_free(ret);
|
|
|
|
BN_free(remainder);
|
|
|
|
BN_free(tmp);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_product(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
|
|
|
|
BIGNUM *remainder = NULL, *zero = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(b = getBN(s, "B"))
|
|
|
|
|| !TEST_ptr(product = getBN(s, "Product"))
|
|
|
|
|| !TEST_ptr(ret = BN_new())
|
|
|
|
|| !TEST_ptr(remainder = BN_new())
|
|
|
|
|| !TEST_ptr(zero = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
BN_zero(zero);
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mul(ret, a, b, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A * B", product, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_div(ret, remainder, product, a, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Product / A", b, ret)
|
|
|
|
|| !equalBN("Product % A", zero, remainder)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_div(ret, remainder, product, b, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Product / B", a, ret)
|
|
|
|
|| !equalBN("Product % B", zero, remainder))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(product);
|
|
|
|
BN_free(ret);
|
|
|
|
BN_free(remainder);
|
|
|
|
BN_free(zero);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_quotient(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
|
|
|
|
BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_ULONG b_word, ret_word;
|
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(b = getBN(s, "B"))
|
|
|
|
|| !TEST_ptr(quotient = getBN(s, "Quotient"))
|
|
|
|
|| !TEST_ptr(remainder = getBN(s, "Remainder"))
|
|
|
|
|| !TEST_ptr(ret = BN_new())
|
|
|
|
|| !TEST_ptr(ret2 = BN_new())
|
|
|
|
|| !TEST_ptr(nnmod = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A / B", quotient, ret)
|
|
|
|
|| !equalBN("A % B", remainder, ret2)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_mul(ret, quotient, b, ctx))
|
|
|
|
|| !TEST_true(BN_add(ret, ret, remainder))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("Quotient * B + Remainder", a, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test with BN_mod_word() and BN_div_word() if the divisor is
|
|
|
|
* small enough.
|
|
|
|
*/
|
|
|
|
b_word = BN_get_word(b);
|
|
|
|
if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
|
|
|
|
BN_ULONG remainder_word = BN_get_word(remainder);
|
|
|
|
|
|
|
|
assert(remainder_word != (BN_ULONG)-1);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(BN_copy(ret, a)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ret_word = BN_div_word(ret, b_word);
|
|
|
|
if (ret_word != remainder_word) {
|
|
|
|
#ifdef BN_DEC_FMT1
|
2017-04-27 00:39:46 +08:00
|
|
|
TEST_error(
|
|
|
|
"Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
|
2016-11-29 01:26:05 +08:00
|
|
|
ret_word, remainder_word);
|
|
|
|
#else
|
2017-04-27 00:39:46 +08:00
|
|
|
TEST_error("Got A %% B (word) mismatch");
|
2016-11-29 01:26:05 +08:00
|
|
|
#endif
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!equalBN ("A / B (word)", quotient, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret_word = BN_mod_word(a, b_word);
|
|
|
|
if (ret_word != remainder_word) {
|
|
|
|
#ifdef BN_DEC_FMT1
|
2017-04-27 00:39:46 +08:00
|
|
|
TEST_error(
|
|
|
|
"Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
|
2016-11-29 01:26:05 +08:00
|
|
|
ret_word, remainder_word);
|
|
|
|
#else
|
2017-04-27 00:39:46 +08:00
|
|
|
TEST_error("Got A %% B (word) mismatch");
|
2016-11-29 01:26:05 +08:00
|
|
|
#endif
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test BN_nnmod. */
|
|
|
|
if (!BN_is_negative(b)) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_copy(nnmod, remainder))
|
|
|
|
|| (BN_is_negative(nnmod)
|
|
|
|
&& !TEST_true(BN_add(nnmod, nnmod, b)))
|
|
|
|
|| !TEST_true(BN_nnmod(ret, a, b, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A % B (non-negative)", nnmod, ret))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(quotient);
|
|
|
|
BN_free(remainder);
|
|
|
|
BN_free(ret);
|
|
|
|
BN_free(ret2);
|
|
|
|
BN_free(nnmod);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_modmul(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(b = getBN(s, "B"))
|
|
|
|
|| !TEST_ptr(m = getBN(s, "M"))
|
|
|
|
|| !TEST_ptr(mod_mul = getBN(s, "ModMul"))
|
|
|
|
|| !TEST_ptr(ret = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A * B (mod M)", mod_mul, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (BN_is_odd(m)) {
|
|
|
|
/* Reduce |a| and |b| and test the Montgomery version. */
|
|
|
|
BN_MONT_CTX *mont = BN_MONT_CTX_new();
|
|
|
|
BIGNUM *a_tmp = BN_new();
|
|
|
|
BIGNUM *b_tmp = BN_new();
|
2017-04-27 00:39:46 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
if (mont == NULL || a_tmp == NULL || b_tmp == NULL
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
|
|
|
|
|| !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
|
|
|
|
|| !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
|
|
|
|
|| !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
|
|
|
|
|| !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
|
|
|
|
|| !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
|
|
|
|
mont, ctx))
|
|
|
|
|| !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
|
|
|
|
|| !equalBN("A * B (mod M) (mont)", mod_mul, ret))
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 0;
|
2017-04-27 00:39:46 +08:00
|
|
|
else
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
|
|
|
BN_MONT_CTX_free(mont);
|
|
|
|
BN_free(a_tmp);
|
|
|
|
BN_free(b_tmp);
|
|
|
|
if (st == 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(m);
|
|
|
|
BN_free(mod_mul);
|
|
|
|
BN_free(ret);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_modexp(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
|
|
|
|
BIGNUM *b = NULL, *c = NULL, *d = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(e = getBN(s, "E"))
|
|
|
|
|| !TEST_ptr(m = getBN(s, "M"))
|
|
|
|
|| !TEST_ptr(mod_exp = getBN(s, "ModExp"))
|
|
|
|
|| !TEST_ptr(ret = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A ^ E (mod M)", mod_exp, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (BN_is_odd(m)) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
|
|
|
|
ctx, NULL))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Regression test for carry propagation bug in sqr8x_reduction */
|
|
|
|
BN_hex2bn(&a, "050505050505");
|
|
|
|
BN_hex2bn(&b, "02");
|
|
|
|
BN_hex2bn(&c,
|
|
|
|
"4141414141414141414141274141414141414141414141414141414141414141"
|
|
|
|
"4141414141414141414141414141414141414141414141414141414141414141"
|
|
|
|
"4141414141414141414141800000000000000000000000000000000000000000"
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000"
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000"
|
|
|
|
"0000000000000000000000000000000000000000000000000000000001");
|
|
|
|
BN_mod_exp(d, a, b, c, ctx);
|
|
|
|
BN_mul(e, a, a, ctx);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq(d, e))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(m);
|
|
|
|
BN_free(mod_exp);
|
|
|
|
BN_free(ret);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_exp(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(e = getBN(s, "E"))
|
|
|
|
|| !TEST_ptr(exp = getBN(s, "Exp"))
|
|
|
|
|| !TEST_ptr(ret = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_exp(ret, a, e, ctx))
|
2016-11-29 01:26:05 +08:00
|
|
|
|| !equalBN("A ^ E", exp, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(exp);
|
|
|
|
BN_free(ret);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_modsqrt(STANZA *s)
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = getBN(s, "A"))
|
|
|
|
|| !TEST_ptr(p = getBN(s, "P"))
|
|
|
|
|| !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
|
|
|
|
|| !TEST_ptr(ret = BN_new())
|
|
|
|
|| !TEST_ptr(ret2 = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* There are two possible answers. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
|
|
|
|
|| !TEST_true(BN_sub(ret2, p, ret)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
/* The first condition should NOT be a test. */
|
2016-11-29 01:26:05 +08:00
|
|
|
if (BN_cmp(ret2, mod_sqrt) != 0
|
|
|
|
&& !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(p);
|
|
|
|
BN_free(mod_sqrt);
|
|
|
|
BN_free(ret);
|
|
|
|
BN_free(ret2);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_bn2padded()
|
|
|
|
{
|
|
|
|
#if HAVE_BN_PADDED
|
|
|
|
uint8_t zeros[256], out[256], reference[128];
|
|
|
|
BIGNUM *n = BN_new();
|
|
|
|
int st = 0;
|
|
|
|
|
|
|
|
/* Test edge case at 0. */
|
|
|
|
if (n == NULL)
|
|
|
|
goto err;
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
memset(out, -1, sizeof(out));
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
memset(zeros, 0, sizeof(zeros));
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Test a random numbers at various byte lengths. */
|
|
|
|
for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
|
|
|
|
#define TOP_BIT_ON 0
|
|
|
|
#define BOTTOM_BIT_NOTOUCH 0
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(BN_num_bytes(n),A) bytes
|
|
|
|
|| TEST_int_eq(BN_bn2bin(n, reference), bytes))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
/* Empty buffer should fail. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
/* One byte short should fail. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (BN_bn2bin_padded(out, bytes - 1, n))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
/* Exactly right size should encode. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
|
|
|
|
|| TEST_mem_eq(out, bytes, reference, bytes))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
/* Pad up one byte extra. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
|
|
|
|
|| !TEST_mem_eq(out + 1, bytes, reference, bytes)
|
|
|
|
|| !TEST_mem_eq(out, 1, zeros, 1))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
/* Pad up to 256. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
|
|
|
|
|| !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
|
|
|
|
reference, bytes)
|
|
|
|
|| !TEST_mem_eq(out, sizseof(out) - bytes,
|
|
|
|
zeros, sizeof(out) - bytes))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(n);
|
|
|
|
return st;
|
|
|
|
#else
|
|
|
|
return ctx != NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_dec2bn()
|
|
|
|
{
|
|
|
|
BIGNUM *bn = NULL;
|
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 0)
|
|
|
|
|| !TEST_BN_eq_zero(bn)
|
|
|
|
|| !TEST_BN_le_zero(bn)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 256)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_gt_zero(bn)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_abs_eq_word(bn, 42)
|
|
|
|
|| !TEST_BN_lt_zero(bn)
|
|
|
|
|| !TEST_BN_le_zero(bn)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
|
|
|
|
|
|
|
if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
|
|
|
|
|| !TEST_BN_eq_word(bn, 1)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_gt_zero(bn)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_eq_one(bn)
|
|
|
|
|| !TEST_BN_odd(bn))
|
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(bn)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_le_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_abs_eq_word(bn, 42)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_gt_zero(bn)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(bn);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_hex2bn()
|
|
|
|
{
|
|
|
|
BIGNUM *bn = NULL;
|
2017-04-27 00:39:46 +08:00
|
|
|
int st = 0;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parseBN(&bn, "0"), 1)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(bn)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parseBN(&bn, "256"), 3)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 0x256)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_gt_zero(bn)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_abs_eq_word(bn, 0x42)
|
|
|
|
|| !TEST_BN_lt_zero(bn)
|
|
|
|
|| !TEST_BN_le_zero(bn)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
|
|
|
bn = NULL;
|
|
|
|
|
|
|
|
if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
|
|
|
|
|| !TEST_BN_eq_word(bn, 0xCB)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_gt_zero(bn)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_odd(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(bn)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_le_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_free(bn);
|
2017-05-08 10:09:41 +08:00
|
|
|
bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 0xabc)
|
|
|
|
|| !TEST_BN_ge_zero(bn)
|
|
|
|
|| !TEST_BN_gt_zero(bn)
|
|
|
|
|| !TEST_BN_ne_zero(bn)
|
|
|
|
|| !TEST_BN_even(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
st = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_free(bn);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_asc2bn()
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(bn = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "0"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(bn)
|
|
|
|
|| !TEST_BN_ge_zero(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "256"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 256)
|
|
|
|
|| !TEST_BN_ge_zero(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "-42"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_abs_eq_word(bn, 42)
|
|
|
|
|| !TEST_BN_lt_zero(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 0x1234)
|
|
|
|
|| !TEST_BN_ge_zero(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 0x1234)
|
|
|
|
|| !TEST_BN_ge_zero(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_abs_eq_word(bn, 0xabcd)
|
|
|
|
|| !TEST_BN_lt_zero(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "-0"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(bn)
|
|
|
|
|| !TEST_BN_ge_zero(bn))
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 123)
|
|
|
|
|| !TEST_BN_ge_zero(bn))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(bn);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MPITEST kMPITests[] = {
|
|
|
|
{"0", "\x00\x00\x00\x00", 4},
|
|
|
|
{"1", "\x00\x00\x00\x01\x01", 5},
|
|
|
|
{"-1", "\x00\x00\x00\x01\x81", 5},
|
|
|
|
{"128", "\x00\x00\x00\x02\x00\x80", 6},
|
|
|
|
{"256", "\x00\x00\x00\x02\x01\x00", 6},
|
|
|
|
{"-256", "\x00\x00\x00\x02\x81\x00", 6},
|
|
|
|
};
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
static int test_mpi(int i)
|
2016-11-29 01:26:05 +08:00
|
|
|
{
|
|
|
|
uint8_t scratch[8];
|
2017-04-27 00:39:46 +08:00
|
|
|
const MPITEST *test = &kMPITests[i];
|
2016-11-29 01:26:05 +08:00
|
|
|
size_t mpi_len, mpi_len2;
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
BIGNUM *bn2 = NULL;
|
|
|
|
int st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(bn = BN_new())
|
|
|
|
|| !TEST_true(BN_asc2bn(&bn, test->base10)))
|
|
|
|
goto err;
|
|
|
|
mpi_len = BN_bn2mpi(bn, NULL);
|
|
|
|
if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
|
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
|
|
|
|
|| !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
|
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
|
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq(bn, bn2)) {
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(bn2);
|
2017-04-27 00:39:46 +08:00
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
}
|
2017-04-27 00:39:46 +08:00
|
|
|
BN_free(bn2);
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(bn);
|
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-11-29 19:06:50 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_rand()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *bn = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(bn = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
/* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
|
|
|
|
if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
|
|
|
|
|| !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
|
|
|
|
|| !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_one(bn)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
|
|
|
|
|| !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_one(bn)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_word(bn, 3))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(bn);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_negzero()
|
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
BIGNUM *numerator = NULL, *denominator = NULL;
|
|
|
|
int consttime, st = 0;
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(c = BN_new())
|
|
|
|
|| !TEST_ptr(d = BN_new()))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Test that BN_mul never gives negative zero. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_set_word(a, 1)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
BN_set_negative(a, 1);
|
|
|
|
BN_zero(b);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mul(c, a, b, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq_zero(c)
|
|
|
|
|| !TEST_BN_ge_zero(c))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (consttime = 0; consttime < 2; consttime++) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(numerator = BN_new())
|
|
|
|
|| !TEST_ptr(denominator = BN_new()))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
if (consttime) {
|
|
|
|
BN_set_flags(numerator, BN_FLG_CONSTTIME);
|
|
|
|
BN_set_flags(denominator, BN_FLG_CONSTTIME);
|
|
|
|
}
|
|
|
|
/* Test that BN_div never gives negative zero in the quotient. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_set_word(numerator, 1))
|
|
|
|
|| !TEST_true(BN_set_word(denominator, 2)))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_set_negative(numerator, 1);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(a)
|
|
|
|
|| !TEST_BN_ge_zero(a))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Test that BN_div never gives negative zero in the remainder. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_set_word(denominator, 1))
|
|
|
|
|| !TEST_true(BN_div(a, b, numerator, denominator, ctx))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(b)
|
|
|
|
|| !TEST_BN_ge_zero(b))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(numerator);
|
|
|
|
BN_free(denominator);
|
|
|
|
numerator = denominator = NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Test that BN_set_negative will not produce a negative zero. */
|
|
|
|
BN_zero(a);
|
|
|
|
BN_set_negative(a, 1);
|
2017-04-27 00:39:46 +08:00
|
|
|
if (BN_is_negative(a))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
st = 1;
|
2017-04-27 00:39:46 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
err:
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_free(a);
|
|
|
|
BN_free(b);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(c);
|
|
|
|
BN_free(d);
|
|
|
|
BN_free(numerator);
|
|
|
|
BN_free(denominator);
|
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-11-28 14:41:05 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_badmod()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *b = NULL, *zero = NULL;
|
|
|
|
BN_MONT_CTX *mont = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(b = BN_new())
|
|
|
|
|| !TEST_ptr(zero = BN_new())
|
|
|
|
|| !TEST_ptr(mont = BN_MONT_CTX_new()))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_zero(zero);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
|
|
|
|
zero, ctx, NULL)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
|
|
|
|
zero, ctx, NULL)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Some operations also may not be used with an even modulus. */
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_set_word(b, 16)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
|
|
|
|
b, ctx, NULL)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
|
|
|
|
b, ctx, NULL)))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
|
|
|
ERR_clear_error();
|
|
|
|
|
|
|
|
st = 1;
|
|
|
|
err:
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_free(a);
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_free(b);
|
|
|
|
BN_free(zero);
|
|
|
|
BN_MONT_CTX_free(mont);
|
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_expmodzero()
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *a = NULL, *r = NULL, *zero = NULL;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(zero = BN_new())
|
|
|
|
|| !TEST_ptr(a = BN_new())
|
|
|
|
|| !TEST_ptr(r = BN_new()))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_zero(zero);
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(r)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
|
|
|
|
NULL, NULL))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(r)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
|
|
|
|
BN_value_one(),
|
|
|
|
NULL, NULL))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(r)
|
2017-04-27 00:39:46 +08:00
|
|
|
|| !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
|
|
|
|
BN_value_one(), NULL, NULL))
|
2017-05-08 10:09:41 +08:00
|
|
|
|| !TEST_BN_eq_zero(r))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(zero);
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(r);
|
|
|
|
return st;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int test_smallprime()
|
2016-08-26 05:45:20 +08:00
|
|
|
{
|
2016-11-29 01:26:05 +08:00
|
|
|
static const int kBits = 10;
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *r;
|
2016-11-29 01:26:05 +08:00
|
|
|
int st = 0;
|
2016-08-26 05:45:20 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(r = BN_new())
|
|
|
|
|| !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
|
|
|
|
NULL, NULL, NULL))
|
|
|
|
|| !TEST_int_eq(BN_num_bits(r), kBits))
|
2016-11-29 01:26:05 +08:00
|
|
|
goto err;
|
2016-08-26 05:45:20 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
st = 1;
|
|
|
|
err:
|
|
|
|
BN_free(r);
|
|
|
|
return st;
|
|
|
|
}
|
2016-08-26 05:45:20 +08:00
|
|
|
|
2017-04-21 00:20:50 +08:00
|
|
|
static int test_3_is_prime()
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2017-04-27 00:39:46 +08:00
|
|
|
BIGNUM *r = NULL;
|
2017-04-21 00:20:50 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
/*
|
|
|
|
* For a long time, small primes were not considered prime when
|
|
|
|
* do_trial_division was set.
|
|
|
|
*/
|
|
|
|
if (!TEST_ptr(r = BN_new())
|
|
|
|
|| !TEST_true(BN_set_word(r, 3))
|
|
|
|
|| !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
|
|
|
|
0 /* do_trial_division */, NULL), 1)
|
|
|
|
|| !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
|
|
|
|
1 /* do_trial_division */, NULL), 1))
|
2017-04-21 00:20:50 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_free(r);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-26 05:45:20 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Delete leading and trailing spaces from a string */
|
|
|
|
static char *strip_spaces(char *p)
|
|
|
|
{
|
|
|
|
char *q;
|
2016-08-26 05:45:20 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Skip over leading spaces */
|
|
|
|
while (*p && isspace(*p))
|
|
|
|
p++;
|
|
|
|
if (!*p)
|
|
|
|
return NULL;
|
2016-08-26 05:45:20 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for (q = p + strlen(p) - 1; q != p && isspace(*q); )
|
|
|
|
*q-- = '\0';
|
|
|
|
return *p ? p : NULL;
|
2016-08-26 05:45:20 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/*
|
|
|
|
* Read next test stanza; return 1 if found, 0 on EOF or error.
|
|
|
|
*/
|
2017-05-23 20:59:49 +08:00
|
|
|
static int readstanza(STANZA *s)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-11-29 01:26:05 +08:00
|
|
|
PAIR *pp = s->pairs;
|
|
|
|
char *p, *equals, *key, *value;
|
|
|
|
char buff[1024];
|
|
|
|
|
2017-05-16 02:49:37 +08:00
|
|
|
while (BIO_gets(fp, buff, sizeof(buff))) {
|
2017-05-23 20:59:49 +08:00
|
|
|
s->curr++;
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(p = strchr(buff, '\n'))) {
|
2017-05-23 20:59:49 +08:00
|
|
|
TEST_info("Line %d too long", s->curr);
|
2016-11-29 01:26:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Blank line marks end of tests. */
|
|
|
|
if (buff[0] == '\0')
|
|
|
|
break;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Lines starting with a pound sign are ignored. */
|
|
|
|
if (buff[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
2017-05-23 20:59:49 +08:00
|
|
|
if (!TEST_ptr(equals = strchr(buff, '='))) {
|
|
|
|
TEST_info("Missing = at line %d\n", s->curr);
|
2016-11-29 01:26:05 +08:00
|
|
|
return 0;
|
2017-05-23 20:59:49 +08:00
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
*equals++ = '\0';
|
2017-05-23 20:59:49 +08:00
|
|
|
if (s->numpairs == 0)
|
|
|
|
s->start = s->curr;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
if (!TEST_ptr(key = strip_spaces(buff))
|
|
|
|
|| !TEST_ptr(value = strip_spaces(equals))
|
|
|
|
|| !TEST_int_lt(s->numpairs++, MAXPAIRS)
|
|
|
|
|| !TEST_ptr(pp->key = OPENSSL_strdup(key))
|
|
|
|
|| !TEST_ptr(pp->value = OPENSSL_strdup(value)))
|
2016-11-29 01:26:05 +08:00
|
|
|
return 0;
|
|
|
|
pp++;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
/* If we read anything, return ok. */
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static void clearstanza(STANZA *s)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-11-29 01:26:05 +08:00
|
|
|
PAIR *pp = s->pairs;
|
|
|
|
int i = s->numpairs;
|
|
|
|
int start = s->start;
|
2017-05-23 20:59:49 +08:00
|
|
|
int curr = s->curr;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for ( ; --i >= 0; pp++) {
|
|
|
|
OPENSSL_free(pp->key);
|
|
|
|
OPENSSL_free(pp->value);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
memset(s, 0, sizeof(*s));
|
2017-05-23 20:59:49 +08:00
|
|
|
s->curr = curr;
|
2016-11-29 01:26:05 +08:00
|
|
|
s->start = start;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
static int file_test_run(STANZA *s)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-11-29 01:26:05 +08:00
|
|
|
static const FILETEST filetests[] = {
|
|
|
|
{"Sum", file_sum},
|
|
|
|
{"LShift1", file_lshift1},
|
|
|
|
{"LShift", file_lshift},
|
|
|
|
{"RShift", file_rshift},
|
|
|
|
{"Square", file_square},
|
|
|
|
{"Product", file_product},
|
|
|
|
{"Quotient", file_quotient},
|
|
|
|
{"ModMul", file_modmul},
|
|
|
|
{"ModExp", file_modexp},
|
|
|
|
{"Exp", file_exp},
|
|
|
|
{"ModSqrt", file_modsqrt},
|
|
|
|
};
|
|
|
|
int numtests = OSSL_NELEM(filetests);
|
|
|
|
const FILETEST *tp = filetests;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
for ( ; --numtests >= 0; tp++) {
|
2017-04-27 00:39:46 +08:00
|
|
|
if (findattr(s, tp->name) != NULL) {
|
|
|
|
if (!tp->func(s)) {
|
|
|
|
TEST_info("Failed %s test at %d", tp->name, s->start);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-04-27 00:39:46 +08:00
|
|
|
TEST_info("Unknown test at %d", s->start);
|
2016-11-29 01:26:05 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2017-05-16 02:49:37 +08:00
|
|
|
static char * const *testfiles;
|
|
|
|
|
|
|
|
static int run_file_tests(int i)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-11-29 01:26:05 +08:00
|
|
|
STANZA s;
|
2017-05-23 20:59:49 +08:00
|
|
|
int errcnt = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-05-16 02:49:37 +08:00
|
|
|
if (!TEST_ptr(fp = BIO_new_file(testfiles[i], "rb")))
|
|
|
|
return 0;
|
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
/* Read test file. */
|
2017-05-16 02:49:37 +08:00
|
|
|
set_test_title(testfiles[i]);
|
2016-11-29 01:26:05 +08:00
|
|
|
memset(&s, 0, sizeof(s));
|
2017-05-23 20:59:49 +08:00
|
|
|
while (!BIO_eof(fp) && readstanza(&s)) {
|
2016-11-29 01:26:05 +08:00
|
|
|
if (s.numpairs == 0)
|
|
|
|
continue;
|
|
|
|
if (!file_test_run(&s)) {
|
2017-02-01 07:47:30 +08:00
|
|
|
errcnt++;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
clearstanza(&s);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-05-16 02:49:37 +08:00
|
|
|
BIO_free(fp);
|
2016-11-29 01:26:05 +08:00
|
|
|
|
2017-02-01 07:47:30 +08:00
|
|
|
return errcnt == 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2017-05-16 02:49:37 +08:00
|
|
|
|
2016-11-29 01:26:05 +08:00
|
|
|
int test_main(int argc, char *argv[])
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-11-29 01:26:05 +08:00
|
|
|
static const char rnd_seed[] =
|
|
|
|
"If not seeded, BN_generate_prime might fail";
|
2017-05-16 02:49:37 +08:00
|
|
|
int result = EXIT_FAILURE;
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
|
2017-05-16 02:49:37 +08:00
|
|
|
RAND_seed(rnd_seed, sizeof rnd_seed);
|
|
|
|
if (!TEST_ptr(ctx = BN_CTX_new()))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
ADD_TEST(test_sub);
|
|
|
|
ADD_TEST(test_div_recip);
|
|
|
|
ADD_TEST(test_mod);
|
|
|
|
ADD_TEST(test_modexp_mont5);
|
|
|
|
ADD_TEST(test_kronecker);
|
|
|
|
ADD_TEST(test_rand);
|
|
|
|
ADD_TEST(test_bn2padded);
|
|
|
|
ADD_TEST(test_dec2bn);
|
|
|
|
ADD_TEST(test_hex2bn);
|
|
|
|
ADD_TEST(test_asc2bn);
|
|
|
|
ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
|
|
|
|
ADD_TEST(test_negzero);
|
|
|
|
ADD_TEST(test_badmod);
|
|
|
|
ADD_TEST(test_expmodzero);
|
|
|
|
ADD_TEST(test_smallprime);
|
2016-11-29 01:26:05 +08:00
|
|
|
#ifndef OPENSSL_NO_EC2M
|
2017-05-16 02:49:37 +08:00
|
|
|
ADD_TEST(test_gf2m_add);
|
|
|
|
ADD_TEST(test_gf2m_mod);
|
|
|
|
ADD_TEST(test_gf2m_mul);
|
|
|
|
ADD_TEST(test_gf2m_sqr);
|
|
|
|
ADD_TEST(test_gf2m_modinv);
|
|
|
|
ADD_TEST(test_gf2m_moddiv);
|
|
|
|
ADD_TEST(test_gf2m_modexp);
|
|
|
|
ADD_TEST(test_gf2m_modsqrt);
|
|
|
|
ADD_TEST(test_gf2m_modsolvequad);
|
2016-11-29 01:26:05 +08:00
|
|
|
#endif
|
2017-05-16 02:49:37 +08:00
|
|
|
ADD_TEST(test_3_is_prime);
|
|
|
|
} else {
|
|
|
|
testfiles = &argv[1];
|
|
|
|
ADD_ALL_TESTS(run_file_tests, argc - 1);
|
|
|
|
}
|
2016-11-29 01:26:05 +08:00
|
|
|
|
|
|
|
result = run_tests(argv[0]);
|
|
|
|
|
2017-04-27 00:39:46 +08:00
|
|
|
end:
|
2016-11-29 01:26:05 +08:00
|
|
|
BN_CTX_free(ctx);
|
|
|
|
return result;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|