2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2011-01-06 15:20:01 +01:00
|
|
|
#include <polarssl/md.h>
|
|
|
|
#include <polarssl/md2.h>
|
|
|
|
#include <polarssl/md4.h>
|
|
|
|
#include <polarssl/md5.h>
|
|
|
|
#include <polarssl/sha1.h>
|
2013-06-30 14:49:12 +02:00
|
|
|
#include <polarssl/sha256.h>
|
|
|
|
#include <polarssl/sha512.h>
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:POLARSSL_MD_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char hash_str[1000];
|
|
|
|
unsigned char output[100];
|
|
|
|
const md_info_t *md_info = NULL;
|
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(hash_str, 0x00, 1000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strcpy( (char *) src_str, text_src_string );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
md_info = md_info_from_string(md_name);
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
|
|
|
|
hexify( hash_str, output, md_get_size(md_info) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char hash_str[10000];
|
|
|
|
unsigned char output[100];
|
|
|
|
int src_len;
|
|
|
|
const md_info_t *md_info = NULL;
|
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
md_info = md_info_from_string(md_name);
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
|
|
|
|
|
|
|
|
hexify( hash_str, output, md_get_size(md_info) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_text_multi( char *text_md_name, char *text_src_string,
|
|
|
|
char *hex_hash_string )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char hash_str[1000];
|
|
|
|
unsigned char output[100];
|
|
|
|
|
|
|
|
const md_info_t *md_info = NULL;
|
|
|
|
md_context_t ctx = MD_CONTEXT_T_INIT;
|
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(hash_str, 0x00, 1000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strcpy( (char *) src_str, text_src_string );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
md_info = md_info_from_string(md_name);
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
2011-01-20 17:42:01 +01:00
|
|
|
TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2011-01-20 17:42:01 +01:00
|
|
|
TEST_ASSERT ( 0 == md_starts( &ctx ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
|
|
|
TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
|
|
|
|
TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
|
|
|
|
TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
|
|
|
|
|
|
|
|
hexify( hash_str, output, md_get_size(md_info) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_hex_multi( char *text_md_name, char *hex_src_string,
|
|
|
|
char *hex_hash_string )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char hash_str[10000];
|
|
|
|
unsigned char output[100];
|
|
|
|
int src_len;
|
|
|
|
const md_info_t *md_info = NULL;
|
|
|
|
md_context_t ctx = MD_CONTEXT_T_INIT;
|
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
md_info = md_info_from_string(md_name);
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
2011-01-20 17:42:01 +01:00
|
|
|
TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2011-01-20 17:42:01 +01:00
|
|
|
TEST_ASSERT ( 0 == md_starts( &ctx ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
|
|
|
TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
|
|
|
|
TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
|
|
|
|
TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
|
|
|
|
|
|
|
|
hexify( hash_str, output, md_get_size(md_info) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
|
|
|
|
char *hex_src_string, char *hex_hash_string )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char key_str[10000];
|
|
|
|
unsigned char hash_str[10000];
|
|
|
|
unsigned char output[100];
|
|
|
|
int key_len, src_len;
|
|
|
|
const md_info_t *md_info = NULL;
|
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(key_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
md_info = md_info_from_string( md_name );
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
|
|
|
|
hexify( hash_str, output, md_get_size(md_info) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
|
|
|
|
char *hex_src_string, char *hex_hash_string )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char src_str[10000];
|
|
|
|
unsigned char key_str[10000];
|
|
|
|
unsigned char hash_str[10000];
|
|
|
|
unsigned char output[100];
|
|
|
|
int key_len, src_len;
|
|
|
|
const md_info_t *md_info = NULL;
|
|
|
|
md_context_t ctx = MD_CONTEXT_T_INIT;
|
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 10000);
|
|
|
|
memset(key_str, 0x00, 10000);
|
|
|
|
memset(hash_str, 0x00, 10000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
md_info = md_info_from_string( md_name );
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
2011-01-20 17:42:01 +01:00
|
|
|
TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
2011-01-20 17:42:01 +01:00
|
|
|
TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
|
2011-01-06 15:20:01 +01:00
|
|
|
TEST_ASSERT ( ctx.md_ctx != NULL );
|
|
|
|
TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
|
|
|
|
TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
|
|
|
|
TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
hexify( hash_str, output, md_get_size(md_info) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void md_file( char *text_md_name, char *filename, char *hex_hash_string )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
|
|
|
char md_name[100];
|
|
|
|
unsigned char hash_str[1000];
|
|
|
|
unsigned char output[100];
|
|
|
|
const md_info_t *md_info = NULL;
|
|
|
|
|
|
|
|
memset(md_name, 0x00, 100);
|
|
|
|
memset(hash_str, 0x00, 1000);
|
|
|
|
memset(output, 0x00, 100);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
strncpy( (char *) md_name, text_md_name, 100 );
|
2011-01-06 15:20:01 +01:00
|
|
|
md_info = md_info_from_string( md_name );
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
md_file( md_info, filename, output);
|
2011-01-06 15:20:01 +01:00
|
|
|
hexify( hash_str, output, md_get_size(md_info) );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
|
2011-01-06 15:20:01 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|