Commit 37916462 authored by Pauli's avatar Pauli Committed by Rich Salz
Browse files

Add output routines to allow consistent formatting of memory, strings


and bignums.  These have been refactored into their own file, along with
their error displays.  The formatting follows the output format used
on error, except that bignums of sixty four bits or less are displayed
in a more compact one line form.

Added a TEST_note function for producing output without file and line
information.

Update the three tests that call BN_print so they use the new test
infrastructure instead.

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
Reviewed-by: default avatarRich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3655)
parent 5511101a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ IF[{- !$disabled{tests} -}]
  LIBS_NO_INST=libtestutil.a
  SOURCE[libtestutil.a]=testutil/basic_output.c testutil/output_helpers.c \
          testutil/driver.c testutil/tests.c testutil/cb.c testutil/stanza.c \
          testutil/format_output.c \
          {- rebase_files("../apps", $target{apps_aux_src}) -} \
          testutil/test_main.c testutil/main.c
  INCLUDE[libtestutil.a]=.. ../include
+74 −125
Original line number Diff line number Diff line
@@ -161,7 +161,7 @@ static int prime_field_tests(void)
    const EC_POINT *points[4];
    const BIGNUM *scalars[4];
    unsigned char buf[100];
    size_t i, len, r = 0;
    size_t len, r = 0;
    int k;

    if (!TEST_ptr(ctx = BN_CTX_new())
@@ -187,15 +187,11 @@ static int prime_field_tests(void)
    if (!TEST_true(EC_GROUP_get_curve_GFp(group, p, a, b, ctx)))
        goto err;

    BIO_printf(bio_out,
            "Curve defined by Weierstrass equation\n"
            "     y^2 = x^3 + a*x + b  (mod 0x");
    BN_print(bio_out, p);
    BIO_printf(bio_out, ")\n     a = 0x");
    BN_print(bio_out, a);
    BIO_printf(bio_out, "\n     b = 0x");
    BN_print(bio_out, b);
    BIO_printf(bio_out, "\n");
    TEST_info("Curve defined by Weierstrass equation");
    TEST_note("     y^2 = x^3 + a*x + b (mod p)");
    test_output_bignum("a", a);
    test_output_bignum("b", b);
    test_output_bignum("p", p);

    buf[0] = 0;
    if (!TEST_ptr(P = EC_POINT_new(group))
@@ -219,32 +215,27 @@ static int prime_field_tests(void)
        if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, Q, x, y,
                                                           ctx)))
            goto err;
        BIO_printf(bio_err, "Point is not on curve: x = 0x");
        BN_print_fp(stderr, x);
        BIO_printf(bio_err, ", y = 0x");
        BN_print_fp(stderr, y);
        BIO_printf(bio_err, "\n");
        TEST_info("Point is not on curve");
        test_output_bignum("x", x);
        test_output_bignum("y", y);
        goto err;
    }

    BIO_printf(bio_out, "A cyclic subgroup:\n");
    TEST_note("A cyclic subgroup:");
    k = 100;
    do {
        if (!TEST_int_ne(k--, 0))
            goto err;

        if (EC_POINT_is_at_infinity(group, P)) {
            BIO_printf(bio_out, "     point at infinity\n");
            TEST_note("     point at infinity");
        } else {
            if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y,
                                                               ctx)))
                goto err;

            BIO_printf(bio_out, "     x = 0x");
            BN_print(bio_out, x);
            BIO_printf(bio_out, ", y = 0x");
            BN_print(bio_out, y);
            BIO_printf(bio_out, "\n");
            test_output_bignum("x", x);
            test_output_bignum("y", y);
        }

        if (!TEST_true(EC_POINT_copy(R, P))
@@ -264,9 +255,8 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
        goto err;
    BIO_printf(bio_out, "Generator as octet string, compressed form:\n     ");
    for (i = 0; i < len; i++)
        BIO_printf(bio_out, "%02X", buf[i]);
    test_output_memory("Generator as octet string, compressed form:",
                       buf, len);

    len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
                             buf, sizeof buf, ctx);
@@ -274,10 +264,8 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
        goto err;
    BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n"
                        "     ");
    for (i = 0; i < len; i++)
        BIO_printf(bio_out, "%02X", buf[i]);
    test_output_memory("Generator as octet string, uncompressed form:",
                       buf, len);

    len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
                             buf, sizeof buf, ctx);
@@ -285,23 +273,17 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
        goto err;
    BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n     ");
    for (i = 0; i < len; i++)
        BIO_printf(bio_out, "%02X", buf[i]);
    test_output_memory("Generator as octet string, hybrid form:",
                       buf, len);

    if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z,
                                                            ctx)))
        goto err;
    BIO_printf(bio_out,
               "\nA representation of the inverse of that generator in\n"
               "Jacobian projective coordinates:\n"
               "     X = 0x");
    BN_print(bio_out, x);
    BIO_printf(bio_out, ", Y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, ", Z = 0x");
    BN_print(bio_out, z);
    BIO_printf(bio_out, "\n");
    TEST_info("A representation of the inverse of that generator in");
    TEST_note("Jacobian projective coordinates");
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    test_output_bignum("z", z);

    if (!TEST_true(EC_POINT_invert(group, P, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
@@ -337,11 +319,9 @@ static int prime_field_tests(void)
        || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
        || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
        goto err;
    BIO_printf(bio_out, "\nSEC2 curve secp160r1 -- Generator:\n     x = 0x");
    BN_print(bio_out, x);
    BIO_printf(bio_out, "\n     y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, "\n");
    TEST_info("SEC2 curve secp160r1 -- Generator");
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    /* G_y value taken from the standard: */
    if (!TEST_true(BN_hex2bn(&z,                         "23a62855"
                                 "3168947d59dcc912042351377ac5fb32"))
@@ -372,11 +352,9 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
        goto err;

    BIO_printf(bio_out, "\nNIST curve P-192 -- Generator:\n     x = 0x");
    BN_print(bio_out, x);
    BIO_printf(bio_out, "\n     y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, "\n");
    TEST_info("NIST curve P-192 -- Generator");
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    /* G_y value taken from the standard: */
    if (!TEST_true(BN_hex2bn(&z,                 "07192B95FFC8DA78"
                                 "631011ED6B24CDD573F977A11E794811"))
@@ -414,11 +392,9 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
        goto err;

    BIO_printf(bio_out, "\nNIST curve P-224 -- Generator:\n     x = 0x");
    BN_print(bio_out, x);
    BIO_printf(bio_out, "\n     y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, "\n");
    TEST_info("NIST curve P-224 -- Generator");
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    /* G_y value taken from the standard: */
    if (!TEST_true(BN_hex2bn(&z,         "BD376388B5F723FB4C22DFE6"
                                 "CD4375A05A07476444D5819985007E34"))
@@ -457,11 +433,9 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
        goto err;

    BIO_printf(bio_out, "\nNIST curve P-256 -- Generator:\n     x = 0x");
    BN_print(bio_out, x);
    BIO_printf(bio_out, "\n     y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, "\n");
    TEST_info("NIST curve P-256 -- Generator");
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    /* G_y value taken from the standard: */
    if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
                                 "2BCE33576B315ECECBB6406837BF51F5"))
@@ -505,11 +479,9 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
        goto err;

    BIO_printf(bio_out, "\nNIST curve P-384 -- Generator:\n     x = 0x");
    BN_print(bio_out, x);
    BIO_printf(bio_out, "\n     y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, "\n");
    TEST_info("NIST curve P-384 -- Generator");
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    /* G_y value taken from the standard: */
    if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
                                 "F8F41DBD289A147CE9DA3113B5F0B8C0"
@@ -562,11 +534,9 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
        goto err;

    BIO_printf(bio_out, "\nNIST curve P-521 -- Generator:\n     x = 0x");
    BN_print(bio_out, x);
    BIO_printf(bio_out, "\n     y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, "\n");
    TEST_info("NIST curve P-521 -- Generator");
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    /* G_y value taken from the standard: */
    if (!TEST_true(BN_hex2bn(&z,                              "118"
                                 "39296A789A3BC0045C8A5FB42C7D1BD9"
@@ -613,7 +583,7 @@ static int prime_field_tests(void)
    scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
    scalars[1] = y;

    BIO_printf(bio_out, "combined multiplication ...");
    TEST_note("combined multiplication ...");

    /* z is still the group order */
    if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
@@ -646,7 +616,7 @@ static int prime_field_tests(void)
        || !TEST_true(EC_POINT_is_at_infinity(group, P)))
        goto err;

    BIO_printf(bio_out, " ok\n\n");
    TEST_note(" ok\n");


    r = 1;
@@ -898,11 +868,9 @@ static int char2_curve_test(int n)
        || !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
                                                           ctx)))
        goto err;
    BIO_printf(bio_out, "\n%s -- Generator:\n     x = 0x", test->name);
    BN_print(bio_out, x);
    BIO_printf(bio_out, "\n     y = 0x");
    BN_print(bio_out, y);
    BIO_printf(bio_out, "\n");
    TEST_info("%s -- Generator", test->name);
    test_output_bignum("x", x);
    test_output_bignum("y", y);
    /* G_y value taken from the standard: */
    if (!TEST_true(BN_hex2bn(&z, test->y))
        || !TEST_BN_eq(y, z))
@@ -920,11 +888,9 @@ static int char2_curve_test(int n)
        || !TEST_true(BN_hex2bn(&cof, test->cof))
        || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
        goto err;
    BIO_printf(bio_out, "\n%s -- Generator:\n     x = 0x", test->name); \
    BN_print(bio_out, x); \
    BIO_printf(bio_out, "\n     y = 0x"); \
    BN_print(bio_out, y); \
    BIO_printf(bio_out, "\n");
    TEST_info("%s -- Generator:", test->name);
    test_output_bignum("x", x);
    test_output_bignum("y", y);
# endif

    if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
@@ -959,7 +925,7 @@ static int char2_curve_test(int n)
        scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
        scalars[1] = y;

        BIO_printf(bio_out, "combined multiplication ...");
        TEST_note("combined multiplication ...");

        /* z is still the group order */
        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
@@ -1019,7 +985,7 @@ static int char2_field_tests(void)
    EC_POINT *P = NULL, *Q = NULL, *R = NULL;
    BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
    unsigned char buf[100];
    size_t i, len;
    size_t len;
    int k, r = 0;

    if (!TEST_ptr(ctx = BN_CTX_new())
@@ -1047,15 +1013,11 @@ static int char2_field_tests(void)
    if (!TEST_true(EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)))
        goto err;

    BIO_printf(bio_out,
            "Curve defined by Weierstrass equation\n"
            "     y^2 + x*y = x^3 + a*x^2 + b  (mod 0x");
    BN_print(bio_out, p);
    BIO_printf(bio_out, ")\n     a = 0x");
    BN_print(bio_out, a);
    BIO_printf(bio_out, "\n     b = 0x");
    BN_print(bio_out, b);
    BIO_printf(bio_out, "\n(0x... means binary polynomial)\n");
    TEST_info("Curve defined by Weierstrass equation");
    TEST_note("     y^2 + x*y = x^3 + a*x^2 + b (mod p)");
    test_output_bignum("a", a);
    test_output_bignum("b", b);
    test_output_bignum("p", p);

     if (!TEST_ptr(P = EC_POINT_new(group))
        || !TEST_ptr(Q = EC_POINT_new(group))
@@ -1091,32 +1053,27 @@ static int char2_field_tests(void)
                                                            ctx)))
            goto err;
#  endif
        BIO_printf(bio_err, "Point is not on curve: x = 0x");
        BN_print_fp(stderr, x);
        BIO_printf(bio_err, ", y = 0x");
        BN_print_fp(stderr, y);
        BIO_printf(bio_err, "\n");
        TEST_info("Point is not on curve");
        test_output_bignum("x", x);
        test_output_bignum("y", y);
        goto err;
    }

    BIO_printf(bio_out, "A cyclic subgroup:\n");
    TEST_note("A cyclic subgroup:");
    k = 100;
    do {
        if (!TEST_int_ne(k--, 0))
            goto err;

        if (EC_POINT_is_at_infinity(group, P))
            BIO_printf(bio_out, "     point at infinity\n");
            TEST_note("     point at infinity");
        else {
            if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
                                                                ctx)))
                goto err;

            BIO_printf(bio_out, "     x = 0x");
            BN_print(bio_out, x);
            BIO_printf(bio_out, ", y = 0x");
            BN_print(bio_out, y);
            BIO_printf(bio_out, "\n");
            test_output_bignum("x", x);
            test_output_bignum("y", y);
        }

        if (!TEST_true(EC_POINT_copy(R, P))
@@ -1137,9 +1094,8 @@ static int char2_field_tests(void)
        || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
        goto err;
    BIO_printf(bio_out, "Generator as octet string, compressed form:\n     ");
    for (i = 0; i < len; i++)
        BIO_printf(bio_out, "%02X", buf[i]);
    test_output_memory("Generator as octet string, compressed form:",
                       buf, len);
#  endif

    len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
@@ -1148,10 +1104,8 @@ static int char2_field_tests(void)
        || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
        goto err;
    BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n"
                        "     ");
    for (i = 0; i < len; i++)
        BIO_printf(bio_out, "%02X", buf[i]);
    test_output_memory("Generator as octet string, uncompressed form:",
                       buf, len);

/* Change test based on whether binary point compression is enabled or not. */
#  ifdef OPENSSL_EC_BIN_PT_COMP
@@ -1162,20 +1116,15 @@ static int char2_field_tests(void)
        || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
        goto err;
    BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n     ");
    for (i = 0; i < len; i++)
        BIO_printf(bio_out, "%02X", buf[i]);
    test_output_memory("Generator as octet string, hybrid form:",
                       buf, len);
#  endif
    BIO_printf(bio_out, "\n");

    if (!TEST_true(EC_POINT_invert(group, P, ctx))
        || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
        goto err;


#if 0
#endif
    BIO_printf(bio_out, "\n\n");
    TEST_note("\n");

    r = 1;
err:
@@ -1352,7 +1301,7 @@ static int nistp_single_test(int idx)
    EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
    int r = 0;

    BIO_printf(bio_out, "\nNIST curve P-%d (optimised implementation):\n",
    TEST_note("NIST curve P-%d (optimised implementation):",
              test->degree);
    if (!TEST_ptr(ctx = BN_CTX_new())
        || !TEST_ptr(p = BN_new())
@@ -1394,7 +1343,7 @@ static int nistp_single_test(int idx)
        || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
        goto err;

    BIO_printf(bio_out, "NIST test vectors ... ");
    TEST_note("NIST test vectors ... ");
    if (!TEST_true(BN_hex2bn(&n, test->d)))
        goto err;
    /* fixed point multiplication */
+4 −12
Original line number Diff line number Diff line
@@ -22,14 +22,7 @@

#define NUM_BITS        (BN_BITS2 * 4)

#define BN_print_var(v) bn_print_var(#v, v)

static void bn_print_var(const char *var, const BIGNUM *bn)
{
    fprintf(stderr, "%s (%3d) = ", var, BN_num_bits(bn));
    BN_print_fp(stderr, bn);
    fprintf(stderr, "\n");
}
#define BN_print_var(v) test_output_bignum(#v, v)

/*
 * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
@@ -39,8 +32,7 @@ static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
                             const BIGNUM *a)
{
    if (!BN_is_zero(r)) {
        fprintf(stderr, "%s failed:\n", method);
        fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
        TEST_error("%s failed: a ** 0 mod 1 = r (should be 0)", method);
        BN_print_var(a);
        BN_print_var(r);
        return 0;
@@ -110,8 +102,8 @@ static int test_mod_exp_zero()
        goto err;

    if (!TEST_BN_eq_zero(r)) {
        fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
        fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
        TEST_error("BN_mod_exp_mont_word failed: "
                   "1 ** 0 mod 1 = r (should be 0)");
        BN_print_var(r);
        goto err;
    }
+10 −23
Original line number Diff line number Diff line
@@ -27,19 +27,6 @@ static unsigned char rkey2[16];
static unsigned char plaintext[BIG_TEST_SIZE];
static unsigned char saved_iv[AES_BLOCK_SIZE * 4];

static void hexdump(FILE *f, const char *title, const unsigned char *s, int l)
{
    int n = 0;

    fprintf(f, "%s", title);
    for (; n < l; ++n) {
        if ((n % 16) == 0)
            fprintf(f, "\n%04x", n);
        fprintf(f, " %02x", s[n]);
    }
    fprintf(f, "\n");
}

#define MAX_VECTOR_SIZE 64

struct ige_test {
@@ -174,9 +161,9 @@ static int test_ige_vectors(int n)

    if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
        TEST_info("IGE test vector %d failed", n);
        hexdump(stderr, "key", v->key, sizeof v->key);
        hexdump(stderr, "iv", v->iv, sizeof v->iv);
        hexdump(stderr, "in", v->in, v->length);
        test_output_memory("key", v->key, sizeof v->key);
        test_output_memory("iv", v->iv, sizeof v->iv);
        test_output_memory("in", v->in, v->length);
        testresult = 0;
    }

@@ -187,9 +174,9 @@ static int test_ige_vectors(int n)

    if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
        TEST_info("IGE test vector %d failed (with in == out)", n);
        hexdump(stderr, "key", v->key, sizeof v->key);
        hexdump(stderr, "iv", v->iv, sizeof v->iv);
        hexdump(stderr, "in", v->in, v->length);
        test_output_memory("key", v->key, sizeof v->key);
        test_output_memory("iv", v->iv, sizeof v->iv);
        test_output_memory("in", v->in, v->length);
        testresult = 0;
    }

@@ -218,10 +205,10 @@ static int test_bi_ige_vectors(int n)
                       v->encrypt);

    if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
        hexdump(stderr, "key 1", v->key1, sizeof v->key1);
        hexdump(stderr, "key 2", v->key2, sizeof v->key2);
        hexdump(stderr, "iv", v->iv, sizeof v->iv);
        hexdump(stderr, "in", v->in, v->length);
        test_output_memory("key 1", v->key1, sizeof v->key1);
        test_output_memory("key 2", v->key2, sizeof v->key2);
        test_output_memory("iv", v->iv, sizeof v->iv);
        test_output_memory("in", v->in, v->length);
        return 0;
    }

+23 −27
Original line number Diff line number Diff line
@@ -18,22 +18,6 @@
# include <openssl/rand.h>
# include <openssl/err.h>

static void showbn(const char *name, const BIGNUM *bn)
{
    BIO *b;
    const char *text;

    if (!TEST_ptr(b = BIO_new(BIO_s_mem())))
        return;
    BIO_write(b, name, strlen(name));
    BIO_write(b, " = ", 3);
    BN_print(b, bn);
    BIO_write(b, "\0", 1);
    BIO_get_mem_data(b, &text);
    TEST_info("%s", text);
    BIO_free(b);
}

# define RANDOM_SIZE 32         /* use 256 bits on each side */

static int run_srp(const char *username, const char *client_pass,
@@ -62,21 +46,21 @@ static int run_srp(const char *username, const char *client_pass,
                                          &s, &v, GN->N, GN->g)))
        goto end;

    showbn("N", GN->N);
    showbn("g", GN->g);
    showbn("Salt", s);
    showbn("Verifier", v);
    test_output_bignum("N", GN->N);
    test_output_bignum("g", GN->g);
    test_output_bignum("Salt", s);
    test_output_bignum("Verifier", v);

    /* Server random */
    RAND_bytes(rand_tmp, sizeof(rand_tmp));
    b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
    if (!TEST_BN_ne_zero(b))
        goto end;
    showbn("b", b);
    test_output_bignum("b", b);

    /* Server's first message */
    Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
    showbn("B", Bpub);
    test_output_bignum("B", Bpub);

    if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
        goto end;
@@ -86,11 +70,11 @@ static int run_srp(const char *username, const char *client_pass,
    a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
    if (!TEST_BN_ne_zero(a))
        goto end;
    showbn("a", a);
    test_output_bignum("a", a);

    /* Client's response */
    Apub = SRP_Calc_A(a, GN->N, GN->g);
    showbn("A", Apub);
    test_output_bignum("A", Apub);

    if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
        goto end;
@@ -101,11 +85,11 @@ static int run_srp(const char *username, const char *client_pass,
    /* Client's key */
    x = SRP_Calc_x(s, username, client_pass);
    Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
    showbn("Client's key", Kclient);
    test_output_bignum("Client's key", Kclient);

    /* Server's key */
    Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
    showbn("Server's key", Kserver);
    test_output_bignum("Server's key", Kserver);

    if (!TEST_BN_eq(Kclient, Kserver))
        goto end;
@@ -167,6 +151,7 @@ static int run_srp_kat(void)
                                          GN->g)))
        goto err;

    TEST_info("checking v");
    if (!TEST_true(check_bn("v", v,
                 "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"
                 "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"
@@ -174,6 +159,7 @@ static int run_srp_kat(void)
                 "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"
                 "E955A5E29E7AB245DB2BE315E2099AFB")))
        goto err;
    TEST_note("    okay");

    /* Server random */
    BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
@@ -184,6 +170,7 @@ static int run_srp_kat(void)
    if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
        goto err;

    TEST_info("checking B");
    if (!TEST_true(check_bn("B", Bpub,
                  "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"
                  "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"
@@ -191,6 +178,7 @@ static int run_srp_kat(void)
                  "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"
                  "EB4012B7D7665238A8E3FB004B117B58")))
        goto err;
    TEST_note("    okay");

    /* Client random */
    BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
@@ -201,6 +189,7 @@ static int run_srp_kat(void)
    if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
        goto err;

    TEST_info("checking A");
    if (!TEST_true(check_bn("A", Apub,
                  "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"
                  "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"
@@ -208,6 +197,7 @@ static int run_srp_kat(void)
                  "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"
                  "B349EF5D76988A3672FAC47B0769447B")))
        goto err;
    TEST_note("    okay");

    /* Both sides calculate u */
    u = SRP_Calc_u(Apub, Bpub, GN->N);
@@ -219,6 +209,7 @@ static int run_srp_kat(void)
    /* Client's key */
    x = SRP_Calc_x(s, "alice", "password123");
    Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
    TEST_info("checking client's key");
    if (!TEST_true(check_bn("Client's key", Kclient,
                  "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
                  "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
@@ -226,9 +217,11 @@ static int run_srp_kat(void)
                  "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
                  "C346D7E474B29EDE8A469FFECA686E5A")))
        goto err;
    TEST_note("    okay");

    /* Server's key */
    Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
    TEST_info("checking server's key");
    if (!TEST_true(check_bn("Server's key", Kserver,
                  "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
                  "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
@@ -236,6 +229,7 @@ static int run_srp_kat(void)
                  "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
                  "C346D7E474B29EDE8A469FFECA686E5A")))
        goto err;
    TEST_note("    okay");

    ret = 1;

@@ -257,10 +251,12 @@ err:
static int run_srp_tests(void)
{
    /* "Negative" test, expect a mismatch */
    TEST_info("run_srp: expecting a mismatch");
    if (!TEST_false(run_srp("alice", "password1", "password2")))
        return 0;

    /* "Positive" test, should pass */
    TEST_info("run_srp: expecting a match");
    if (!TEST_true(run_srp("alice", "password", "password")))
        return 0;

Loading