2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/sha1.h"
|
|
|
|
#include "mbedtls/sha256.h"
|
|
|
|
#include "mbedtls/sha512.h"
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 10:42:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SHA1_C */
|
2013-08-20 09:48:36 +00:00
|
|
|
void sha1_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_hash_string)
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char key_str[10000];
|
2014-04-02 15:17:19 +00:00
|
|
|
unsigned char hash_str[41];
|
|
|
|
unsigned char output[20];
|
2009-07-11 19:15:20 +00:00
|
|
|
int key_len, src_len;
|
2014-04-02 15:17:19 +00:00
|
|
|
sha1_context ctx;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
memset(src_str, 0x00, sizeof src_str);
|
|
|
|
memset(key_str, 0x00, sizeof key_str);
|
2014-06-26 10:25:06 +00:00
|
|
|
sha1_init( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
/* Test the all-in-one interface */
|
|
|
|
memset(hash_str, 0x00, sizeof hash_str);
|
|
|
|
memset(output, 0x00, sizeof output);
|
|
|
|
|
2009-06-28 21:50:27 +00:00
|
|
|
sha1_hmac( key_str, key_len, src_str, src_len, output );
|
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
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 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2014-06-26 10:25:06 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2014-06-26 10:25:06 +00:00
|
|
|
sha1_free( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 10:42:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SHA256_C */
|
2013-08-20 09:48:36 +00:00
|
|
|
void sha224_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_hash_string)
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char key_str[10000];
|
2014-04-02 15:17:19 +00:00
|
|
|
unsigned char hash_str[57];
|
|
|
|
unsigned char output[28];
|
2009-07-11 19:15:20 +00:00
|
|
|
int key_len, src_len;
|
2014-04-02 15:17:19 +00:00
|
|
|
sha256_context ctx;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
memset(src_str, 0x00, sizeof src_str);
|
|
|
|
memset(key_str, 0x00, sizeof key_str);
|
2014-06-26 10:25:06 +00:00
|
|
|
sha256_init( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
/* Test the all-in-one interface */
|
|
|
|
memset(hash_str, 0x00, sizeof hash_str);
|
|
|
|
memset(output, 0x00, sizeof output);
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_hmac( key_str, key_len, src_str, src_len, output, 1 );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
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 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2014-06-26 10:25:06 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2014-06-26 10:25:06 +00:00
|
|
|
sha256_free( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 10:42:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SHA256_C */
|
2013-08-20 09:48:36 +00:00
|
|
|
void sha256_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_hash_string)
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char key_str[10000];
|
2014-04-02 15:17:19 +00:00
|
|
|
unsigned char hash_str[65];
|
|
|
|
unsigned char output[32];
|
2009-07-11 19:15:20 +00:00
|
|
|
int key_len, src_len;
|
2014-04-02 15:17:19 +00:00
|
|
|
sha256_context ctx;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
memset(src_str, 0x00, sizeof src_str);
|
|
|
|
memset(key_str, 0x00, sizeof key_str);
|
2014-06-26 10:25:06 +00:00
|
|
|
sha256_init( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
/* Test the all-in-one interface */
|
|
|
|
memset(hash_str, 0x00, sizeof hash_str);
|
|
|
|
memset(output, 0x00, sizeof output);
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
sha256_hmac( key_str, key_len, src_str, src_len, output, 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
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 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2014-06-26 10:25:06 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2014-06-26 10:25:06 +00:00
|
|
|
sha256_free( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 10:42:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SHA512_C */
|
2013-08-20 09:48:36 +00:00
|
|
|
void sha384_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_hash_string)
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char key_str[10000];
|
2014-04-02 15:17:19 +00:00
|
|
|
unsigned char hash_str[97];
|
|
|
|
unsigned char output[48];
|
2009-07-11 19:15:20 +00:00
|
|
|
int key_len, src_len;
|
2014-04-02 15:17:19 +00:00
|
|
|
sha512_context ctx;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
memset(src_str, 0x00, sizeof src_str);
|
|
|
|
memset(key_str, 0x00, sizeof key_str);
|
2014-06-26 10:25:06 +00:00
|
|
|
sha512_init( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
/* Test the all-in-one interface */
|
|
|
|
memset(hash_str, 0x00, sizeof hash_str);
|
|
|
|
memset(output, 0x00, sizeof output);
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_hmac( key_str, key_len, src_str, src_len, output, 1 );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
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 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2014-06-26 10:25:06 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2014-06-26 10:25:06 +00:00
|
|
|
sha512_free( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 10:42:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SHA512_C */
|
2013-08-20 09:48:36 +00:00
|
|
|
void sha512_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_hash_string)
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char key_str[10000];
|
2014-04-02 15:17:19 +00:00
|
|
|
unsigned char hash_str[129];
|
|
|
|
unsigned char output[64];
|
2009-07-11 19:15:20 +00:00
|
|
|
int key_len, src_len;
|
2014-04-02 15:17:19 +00:00
|
|
|
sha512_context ctx;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
memset(src_str, 0x00, sizeof src_str);
|
|
|
|
memset(key_str, 0x00, sizeof key_str);
|
2014-06-26 10:25:06 +00:00
|
|
|
sha512_init( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
/* Test the all-in-one interface */
|
|
|
|
memset(hash_str, 0x00, sizeof hash_str);
|
|
|
|
memset(output, 0x00, sizeof output);
|
|
|
|
|
2013-06-30 12:34:05 +00:00
|
|
|
sha512_hmac( key_str, key_len, src_str, src_len, output, 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2014-04-02 15:17:19 +00:00
|
|
|
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 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2014-06-26 10:25:06 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2014-06-26 10:25:06 +00:00
|
|
|
sha512_free( &ctx );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|