iPXE
Data Structures | Defines | Functions | Variables
digest_test.c File Reference

Digest self-tests. More...

#include <stdlib.h>
#include <string.h>
#include <ipxe/crypto.h>
#include <ipxe/profile.h>
#include "digest_test.h"

Go to the source code of this file.

Data Structures

struct  digest_test_fragments
 A digest test fragment list. More...

Defines

#define NUM_DIGEST_TEST_FRAG   8
 Maximum number of digest test fragments.
#define PROFILE_COUNT   16
 Number of sample iterations for profiling.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void digest_frag_okx (struct digest_test *test, struct digest_test_fragments *fragments, const char *file, unsigned int line)
 Report a digest fragmented test result.
void digest_okx (struct digest_test *test, const char *file, unsigned int line)
 Report a digest test result.
unsigned long digest_cost (struct digest_algorithm *digest)
 Calculate digest algorithm cost.

Variables

static struct digest_test_fragments digest_test_fragments []
 Digest test fragment lists.

Detailed Description

Digest self-tests.

Definition in file digest_test.c.


Define Documentation

#define NUM_DIGEST_TEST_FRAG   8

Maximum number of digest test fragments.

Definition at line 42 of file digest_test.c.

#define PROFILE_COUNT   16

Number of sample iterations for profiling.

Definition at line 58 of file digest_test.c.

Referenced by digest_cost().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void digest_frag_okx ( struct digest_test test,
struct digest_test_fragments fragments,
const char *  file,
unsigned int  line 
)

Report a digest fragmented test result.

Parameters:
testDigest test
fragmentsFragment list
fileTest code file
lineTest code line

Definition at line 68 of file digest_test.c.

References ctx, digest_algorithm::ctxsize, digest_test::data, data, digest_test::digest, digest, digest_final(), digest_init(), digest_update(), digest_algorithm::digestsize, digest_test::expected, digest_test::expected_len, digest_test::len, digest_test_fragments::len, len, memcmp(), okx, and out.

Referenced by digest_okx().

                                                             {
        struct digest_algorithm *digest = test->digest;
        uint8_t ctx[digest->ctxsize];
        uint8_t out[digest->digestsize];
        const void *data = test->data;
        size_t len = test->len;
        size_t frag_len = 0;
        unsigned int i;

        /* Sanity check */
        okx ( test->expected_len == sizeof ( out ), file, line );

        /* Initialise digest */
        digest_init ( digest, ctx );

        /* Update digest fragment-by-fragment */
        for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
                                     sizeof ( fragments->len[0] ) ) ) ; i++ ) {
                if ( fragments )
                        frag_len = fragments->len[i];
                if ( ( frag_len == 0 ) || ( frag_len < len ) )
                        frag_len = len;
                digest_update ( digest, ctx, data, frag_len );
                data += frag_len;
                len -= frag_len;
        }

        /* Finalise digest */
        digest_final ( digest, ctx, out );

        /* Compare against expected output */
        okx ( memcmp ( test->expected, out, sizeof ( out ) ) == 0, file, line );
}
void digest_okx ( struct digest_test test,
const char *  file,
unsigned int  line 
)

Report a digest test result.

Parameters:
testDigest test
fileTest code file
lineTest code line

Definition at line 111 of file digest_test.c.

References digest_frag_okx(), digest_test_fragments, and NULL.

                                      {
        unsigned int i;

        /* Test with a single pass */
        digest_frag_okx ( test, NULL, file, line );

        /* Test with fragment lists */
        for ( i = 0 ; i < ( sizeof ( digest_test_fragments ) /
                            sizeof ( digest_test_fragments[0] ) ) ; i++ ) {
                digest_frag_okx ( test, &digest_test_fragments[i], file, line );
        }
}
unsigned long digest_cost ( struct digest_algorithm digest)

Calculate digest algorithm cost.

Parameters:
digestDigest algorithm
Return values:
costCost (in cycles per byte)

Definition at line 131 of file digest_test.c.

References cost, ctx, digest_algorithm::ctxsize, digest_final(), digest_init(), digest_update(), digest_algorithm::digestsize, memset(), out, PROFILE_COUNT, profile_mean(), profile_start(), profile_stop(), rand(), random(), and srand().

Referenced by md4_test_exec(), md5_test_exec(), sha1_test_exec(), sha256_test_exec(), and sha512_test_exec().

                                                              {
        static uint8_t random[8192]; /* Too large for stack */
        uint8_t ctx[digest->ctxsize];
        uint8_t out[digest->digestsize];
        struct profiler profiler;
        unsigned long cost;
        unsigned int i;

        /* Fill buffer with pseudo-random data */
        srand ( 0x1234568 );
        for ( i = 0 ; i < sizeof ( random ) ; i++ )
                random[i] = rand();

        /* Profile digest calculation */
        memset ( &profiler, 0, sizeof ( profiler ) );
        for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
                profile_start ( &profiler );
                digest_init ( digest, ctx );
                digest_update ( digest, ctx, random, sizeof ( random ) );
                digest_final ( digest, ctx, out );
                profile_stop ( &profiler );
        }

        /* Round to nearest whole number of cycles per byte */
        cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
                 sizeof ( random ) );

        return cost;
}

Variable Documentation

Initial value:
 {
        { { 0, -1UL, } },
        { { 1, 1, 1, 1, 1, 1, 1, 1 } },
        { { 2, 0, 23, 4, 6, 1, 0 } },
}

Digest test fragment lists.

Definition at line 51 of file digest_test.c.

Referenced by digest_okx().