blob: b31d7726c41fb1098e0aa7bbd59159b43a97985b [file] [log] [blame]
/* BEGIN_HEADER */
#include "polarssl/sha1.h"
#include "polarssl/sha256.h"
#include "polarssl/sha512.h"
/* END_HEADER */
/* BEGIN_CASE depends_on:POLARSSL_SHA1_C */
void sha1_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
char *hex_hash_string)
{
unsigned char src_str[10000];
unsigned char key_str[10000];
unsigned char hash_str[41];
unsigned char output[20];
int key_len, src_len;
sha1_context ctx;
memset(src_str, 0x00, sizeof src_str);
memset(key_str, 0x00, sizeof key_str);
sha1_init( &ctx );
key_len = unhexify( key_str, hex_key_string );
src_len = unhexify( src_str, hex_src_string );
/* Test the all-in-one interface */
memset(hash_str, 0x00, sizeof hash_str);
memset(output, 0x00, sizeof output);
sha1_hmac( key_str, key_len, src_str, src_len, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Also test the "streaming" interface */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
memset( &ctx, 0x00, sizeof ctx );
sha1_hmac_starts( &ctx, key_str, key_len );
sha1_hmac_update( &ctx, src_str, 0 );
sha1_hmac_update( &ctx, src_str, src_len / 2 );
sha1_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha1_hmac_update( &ctx, src_str + src_len, 0 );
sha1_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Again, to test hmac_reset() */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
sha1_hmac_reset( &ctx );
sha1_hmac_update( &ctx, src_str, src_len / 2 );
sha1_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha1_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
exit:
sha1_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_SHA256_C */
void sha224_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
char *hex_hash_string)
{
unsigned char src_str[10000];
unsigned char key_str[10000];
unsigned char hash_str[57];
unsigned char output[28];
int key_len, src_len;
sha256_context ctx;
memset(src_str, 0x00, sizeof src_str);
memset(key_str, 0x00, sizeof key_str);
sha256_init( &ctx );
key_len = unhexify( key_str, hex_key_string );
src_len = unhexify( src_str, hex_src_string );
/* Test the all-in-one interface */
memset(hash_str, 0x00, sizeof hash_str);
memset(output, 0x00, sizeof output);
sha256_hmac( key_str, key_len, src_str, src_len, output, 1 );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Also test the "streaming" interface */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
memset( &ctx, 0x00, sizeof ctx );
sha256_hmac_starts( &ctx, key_str, key_len, 1 );
sha256_hmac_update( &ctx, src_str, 0 );
sha256_hmac_update( &ctx, src_str, src_len / 2 );
sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha256_hmac_update( &ctx, src_str + src_len, 0 );
sha256_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Again, to test hmac_reset() */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
sha256_hmac_reset( &ctx );
sha256_hmac_update( &ctx, src_str, src_len / 2 );
sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha256_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
exit:
sha256_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_SHA256_C */
void sha256_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
char *hex_hash_string)
{
unsigned char src_str[10000];
unsigned char key_str[10000];
unsigned char hash_str[65];
unsigned char output[32];
int key_len, src_len;
sha256_context ctx;
memset(src_str, 0x00, sizeof src_str);
memset(key_str, 0x00, sizeof key_str);
sha256_init( &ctx );
key_len = unhexify( key_str, hex_key_string );
src_len = unhexify( src_str, hex_src_string );
/* Test the all-in-one interface */
memset(hash_str, 0x00, sizeof hash_str);
memset(output, 0x00, sizeof output);
sha256_hmac( key_str, key_len, src_str, src_len, output, 0 );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Also test the "streaming" interface */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
memset( &ctx, 0x00, sizeof ctx );
sha256_hmac_starts( &ctx, key_str, key_len, 0 );
sha256_hmac_update( &ctx, src_str, 0 );
sha256_hmac_update( &ctx, src_str, src_len / 2 );
sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha256_hmac_update( &ctx, src_str + src_len, 0 );
sha256_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Again, to test hmac_reset() */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
sha256_hmac_reset( &ctx );
sha256_hmac_update( &ctx, src_str, src_len / 2 );
sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha256_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
exit:
sha256_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_SHA512_C */
void sha384_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
char *hex_hash_string)
{
unsigned char src_str[10000];
unsigned char key_str[10000];
unsigned char hash_str[97];
unsigned char output[48];
int key_len, src_len;
sha512_context ctx;
memset(src_str, 0x00, sizeof src_str);
memset(key_str, 0x00, sizeof key_str);
sha512_init( &ctx );
key_len = unhexify( key_str, hex_key_string );
src_len = unhexify( src_str, hex_src_string );
/* Test the all-in-one interface */
memset(hash_str, 0x00, sizeof hash_str);
memset(output, 0x00, sizeof output);
sha512_hmac( key_str, key_len, src_str, src_len, output, 1 );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Also test the "streaming" interface */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
memset( &ctx, 0x00, sizeof ctx );
sha512_hmac_starts( &ctx, key_str, key_len, 1 );
sha512_hmac_update( &ctx, src_str, 0 );
sha512_hmac_update( &ctx, src_str, src_len / 2 );
sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha512_hmac_update( &ctx, src_str + src_len, 0 );
sha512_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Again, to test hmac_reset() */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
sha512_hmac_reset( &ctx );
sha512_hmac_update( &ctx, src_str, src_len / 2 );
sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha512_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
exit:
sha512_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_SHA512_C */
void sha512_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
char *hex_hash_string)
{
unsigned char src_str[10000];
unsigned char key_str[10000];
unsigned char hash_str[129];
unsigned char output[64];
int key_len, src_len;
sha512_context ctx;
memset(src_str, 0x00, sizeof src_str);
memset(key_str, 0x00, sizeof key_str);
sha512_init( &ctx );
key_len = unhexify( key_str, hex_key_string );
src_len = unhexify( src_str, hex_src_string );
/* Test the all-in-one interface */
memset(hash_str, 0x00, sizeof hash_str);
memset(output, 0x00, sizeof output);
sha512_hmac( key_str, key_len, src_str, src_len, output, 0 );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Also test the "streaming" interface */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
memset( &ctx, 0x00, sizeof ctx );
sha512_hmac_starts( &ctx, key_str, key_len, 0 );
sha512_hmac_update( &ctx, src_str, 0 );
sha512_hmac_update( &ctx, src_str, src_len / 2 );
sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha512_hmac_update( &ctx, src_str + src_len, 0 );
sha512_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
/* Again, to test hmac_reset() */
memset( hash_str, 0x00, sizeof hash_str );
memset( output, 0x00, sizeof output );
sha512_hmac_reset( &ctx );
sha512_hmac_update( &ctx, src_str, src_len / 2 );
sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 );
sha512_hmac_finish( &ctx, output );
hexify( hash_str, output, sizeof output );
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
exit:
sha512_free( &ctx );
}
/* END_CASE */