iPXE
Data Structures | Defines | Functions
digest_test.h File Reference
#include <stdint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  digest_test
 A digest test. More...

Defines

#define DATA(...)   { __VA_ARGS__ }
 Define inline test data.
#define DIGEST(...)   { __VA_ARGS__ }
 Define inline expected digest value.
#define DIGEST_TEST(name, DIGEST, DATA, EXPECTED)
 Define a digest test.
#define DIGEST_EMPTY   DATA()
 Standard test vector: empty data.
#define DIGEST_NIST_ABC   DATA ( 0x61, 0x62, 0x63 )
 Standard test vector: NIST string "abc".
#define DIGEST_NIST_ABC_OPQ
 Standard test vector: NIST string "abc...opq".
#define DIGEST_NIST_ABC_STU
 Standard test vector: NIST string "abc...stu".
#define digest_ok(test)   digest_okx ( test, __FILE__, __LINE__ )
 Report a digest test result.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
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.

Define Documentation

#define DATA (   ...)    { __VA_ARGS__ }

Define inline test data.

Definition at line 25 of file digest_test.h.

#define DIGEST (   ...)    { __VA_ARGS__ }

Define inline expected digest value.

Definition at line 28 of file digest_test.h.

#define DIGEST_TEST (   name,
  DIGEST,
  DATA,
  EXPECTED 
)
Value:
static const uint8_t name ## _data[] = DATA;                    \
        static const uint8_t name ## _expected[] = EXPECTED;            \
        static struct digest_test name = {                              \
                .digest = DIGEST,                                       \
                .data = name ## _data,                                  \
                .len = sizeof ( name ## _data ),                        \
                .expected = name ## _expected,                          \
                .expected_len = sizeof ( name ## _expected ),           \
        };

Define a digest test.

Parameters:
nameTest name
DIGESTDigest algorithm
DATATest data
EXPECTEDExpected digest value
Return values:
testDigest test

Definition at line 39 of file digest_test.h.

#define DIGEST_EMPTY   DATA()

Standard test vector: empty data.

Definition at line 51 of file digest_test.h.

#define DIGEST_NIST_ABC   DATA ( 0x61, 0x62, 0x63 )

Standard test vector: NIST string "abc".

The NIST Cryptographic Toolkit examples for all digest algorithms include a test vector which is the unterminated string

"abc"

Definition at line 60 of file digest_test.h.

Value:
DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63,    \
               0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66,    \
               0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69,    \
               0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,    \
               0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c,    \
               0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f,    \
               0x70, 0x71 )

Standard test vector: NIST string "abc...opq".

The NIST Cryptographic Toolkit examples for all 32-bit digest algorithms (SHA-1 and the SHA-256 family) include a test vector which is the unterminated string

"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"

Definition at line 71 of file digest_test.h.

Value:
DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62,    \
               0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64,    \
               0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66,    \
               0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68,    \
               0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a,    \
               0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,    \
               0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,    \
               0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,    \
               0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b,    \
               0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d,    \
               0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f,    \
               0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71,    \
               0x72, 0x73, 0x74, 0x75 )

Standard test vector: NIST string "abc...stu".

The NIST Cryptographic Toolkit examples for all 64-bit digest algorithms (SHA-512 family) include a test vector which is the unterminated string

"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"

Definition at line 89 of file digest_test.h.

#define digest_ok (   test)    digest_okx ( test, __FILE__, __LINE__ )

Report a digest test result.

Parameters:
testDigest test

Definition at line 109 of file digest_test.h.

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


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
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;
}