Commit 7ff3a3b9 authored by Richard Levitte's avatar Richard Levitte
Browse files

Code cleanup: remove benchmark code from internal tests

parent a3a5d1b7
Loading
Loading
Loading
Loading
+0 −66
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
#include "../crypto/modes/modes_lcl.h"
#include "testutil.h"
#include "internal/nelem.h"
#include "internal/cryptlib.h"

typedef struct {
    size_t size;
@@ -884,75 +883,10 @@ static int test_gcm128(int idx)
    return 1;
}

static void benchmark_gcm128(const unsigned char *K, size_t Klen,
                             const unsigned char *IV, size_t IVlen)
{
#ifdef OPENSSL_CPUID_OBJ
    GCM128_CONTEXT ctx;
    AES_KEY key;
    uint32_t start, gcm_t, ctr_t;
    union {
        u64 u;
        u8 c[1024];
    } buf;

    AES_set_encrypt_key(K, Klen * 8, &key);
    CRYPTO_gcm128_init(&ctx, &key, (block128_f) AES_encrypt);
    CRYPTO_gcm128_setiv(&ctx, IV, IVlen);

    CRYPTO_gcm128_encrypt(&ctx, buf.c, buf.c, sizeof(buf));
    start = OPENSSL_rdtsc();
    CRYPTO_gcm128_encrypt(&ctx, buf.c, buf.c, sizeof(buf));
    gcm_t = OPENSSL_rdtsc() - start;

    CRYPTO_ctr128_encrypt(buf.c, buf.c, sizeof(buf),
                          &key, ctx.Yi.c, ctx.EKi.c, &ctx.mres,
                          (block128_f) AES_encrypt);
    start = OPENSSL_rdtsc();
    CRYPTO_ctr128_encrypt(buf.c, buf.c, sizeof(buf),
                          &key, ctx.Yi.c, ctx.EKi.c, &ctx.mres,
                          (block128_f) AES_encrypt);
    ctr_t = OPENSSL_rdtsc() - start;

    printf("%.2f-%.2f=%.2f\n",
           gcm_t / (double)sizeof(buf),
           ctr_t / (double)sizeof(buf),
           (gcm_t - ctr_t) / (double)sizeof(buf));
# ifdef GHASH
    {
        void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
                             const u8 *inp, size_t len) = ctx.ghash;

        GHASH((&ctx), buf.c, sizeof(buf));
        start = OPENSSL_rdtsc();
        for (i = 0; i < 100; ++i)
            GHASH((&ctx), buf.c, sizeof(buf));
        gcm_t = OPENSSL_rdtsc() - start;
        printf("%.2f\n", gcm_t / (double)sizeof(buf) / (double)i);
    }
# endif
#else
    fprintf(stderr,
            "Benchmarking of modes isn't available on this platform\n");
#endif
}

int setup_tests(void)
{
    if (test_has_option("-h")) {
        printf("-h\tThis help\n");
        printf("-b\tBenchmark gcm128 in addition to the tests\n");
        return 1;
    }

    ADD_ALL_TESTS(test_aes_cts128, OSSL_NELEM(aes_cts128_vectors));
    ADD_ALL_TESTS(test_aes_cts128_nist, OSSL_NELEM(aes_cts128_vectors));
    ADD_ALL_TESTS(test_gcm128, OSSL_NELEM(gcm128_vectors));
    return 1;
}

void cleanup_tests(void)
{
    if (test_has_option("-b"))
        benchmark_gcm128(K1, sizeof(K1), IV1, sizeof(IV1));
}
+0 −52
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@
#include "internal/poly1305.h"
#include "../crypto/poly1305/poly1305_local.h"
#include "internal/nelem.h"
#include "internal/cryptlib.h"

typedef struct {
    size_t size;
@@ -35,45 +34,6 @@ typedef struct {
 *
 ***/

static void benchmark_poly1305()
{
# ifdef OPENSSL_CPUID_OBJ
    POLY1305 poly1305;
    unsigned char key[32];
    unsigned char buf[8192];
    uint32_t stopwatch;
    unsigned int i;

    memset (buf,0x55,sizeof(buf));
    memset (key,0xAA,sizeof(key));

    Poly1305_Init(&poly1305, key);

    for (i=0;i<100000;i++)
        Poly1305_Update(&poly1305,buf,sizeof(buf));

    stopwatch = OPENSSL_rdtsc();
    for (i=0;i<10000;i++)
        Poly1305_Update(&poly1305,buf,sizeof(buf));
    stopwatch = OPENSSL_rdtsc() - stopwatch;

    printf("%g\n",stopwatch/(double)(i*sizeof(buf)));

    stopwatch = OPENSSL_rdtsc();
    for (i=0;i<10000;i++) {
        Poly1305_Init(&poly1305, key);
        Poly1305_Update(&poly1305,buf,16);
        Poly1305_Final(&poly1305,buf);
    }
    stopwatch = OPENSSL_rdtsc() - stopwatch;

    printf("%g\n",stopwatch/(double)(i));
# else
    fprintf(stderr,
            "Benchmarking of poly1305 isn't available on this platform\n");
# endif
}

static TESTDATA tests[] = {
    /*
     * RFC7539
@@ -1611,18 +1571,6 @@ static int test_poly1305(int idx)

int setup_tests(void)
{
    if (test_has_option("-h")) {
        printf("-h\tThis help\n");
        printf("-b\tBenchmark in addition to the tests\n");
        return 1;
    }

    ADD_ALL_TESTS(test_poly1305, OSSL_NELEM(tests));
    return 1;
}

void cleanup_tests(void)
{
    if (test_has_option("-b"))
        benchmark_poly1305();
}
+0 −65
Original line number Diff line number Diff line
@@ -17,10 +17,6 @@
#include "internal/siphash.h"
#include "../crypto/siphash/siphash_local.h"
#include "internal/nelem.h"
#include "internal/cryptlib.h"

static BIO* b_stderr = NULL;
static BIO* b_stdout = NULL;

typedef struct {
    size_t size;
@@ -38,46 +34,6 @@ typedef struct {
 *
 ***/

static int benchmark_siphash(void)
{
# ifdef OPENSSL_CPUID_OBJ
    SIPHASH siphash;
    unsigned char key[SIPHASH_KEY_SIZE];
    unsigned char buf[8192];
    uint32_t stopwatch;
    unsigned int i;

    memset (buf,0x55,sizeof(buf));
    memset (key,0xAA,sizeof(key));

    (void)SipHash_Init(&siphash, key, 0, 0, 0);

    for (i=0;i<100000;i++)
        SipHash_Update(&siphash, buf, sizeof(buf));

    stopwatch = OPENSSL_rdtsc();
    for (i=0;i<10000;i++)
        SipHash_Update(&siphash, buf, sizeof(buf));
    stopwatch = OPENSSL_rdtsc() - stopwatch;

    BIO_printf(b_stdout, "%g\n",stopwatch/(double)(i*sizeof(buf)));

    stopwatch = OPENSSL_rdtsc();
    for (i=0;i<10000;i++) {
        (void)SipHash_Init(&siphash, key, 0, 0, 0);
        SipHash_Update(&siphash, buf, 16);
        (void)SipHash_Final(&siphash, buf, SIPHASH_MAX_DIGEST_SIZE);
    }
    stopwatch = OPENSSL_rdtsc() - stopwatch;

    BIO_printf(b_stdout, "%g\n",stopwatch/(double)(i));
# else
    BIO_printf(b_stderr,
               "Benchmarking of siphash isn't available on this platform\n");
# endif
    return 1;
}

/* From C reference: https://131002.net/siphash/ */

static TESTDATA tests[] = {
@@ -321,28 +277,7 @@ static int test_siphash_basic(void)

int setup_tests(void)
{
    if (test_has_option("-h")) {
        BIO_printf(bio_out, "-h\tThis help\n");
        BIO_printf(bio_out, "-b\tBenchmark in addition to the tests\n");
        return 1;
    }

    b_stderr = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
    b_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
#ifdef OPENSSL_SYS_VMS
    b_stderr = BIO_push(BIO_new(BIO_f_linebuffer()), b_stderr);
    b_stdout = BIO_push(BIO_new(BIO_f_linebuffer()), b_stdout);
#endif

    ADD_TEST(test_siphash_basic);
    ADD_ALL_TESTS(test_siphash, OSSL_NELEM(tests));
    if (test_has_option("-b"))
        ADD_TEST(benchmark_siphash);
    return 1;
}

void cleanup_tests(void)
{
    BIO_free(b_stdout);
    BIO_free(b_stderr);
}