blob: 4a15322667017f49d2d9d3818bea347b354baf42 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00002#include <polarssl/sha1.h>
Paul Bakkerd2681d82013-06-30 14:49:12 +02003#include <polarssl/sha256.h>
4#include <polarssl/sha512.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00006
Paul Bakker68a4fce2013-08-20 12:42:31 +02007/* BEGIN_CASE depends_on:POLARSSL_SHA1_C */
Paul Bakker33b43f12013-08-20 11:48:36 +02008void sha1_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
9 char *hex_hash_string)
Paul Bakker367dae42009-06-28 21:50:27 +000010{
11 unsigned char src_str[10000];
12 unsigned char key_str[10000];
13 unsigned char hash_str[10000];
14 unsigned char output[41];
Paul Bakker69998dd2009-07-11 19:15:20 +000015 int key_len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000016
17 memset(src_str, 0x00, 10000);
18 memset(key_str, 0x00, 10000);
19 memset(hash_str, 0x00, 10000);
20 memset(output, 0x00, 41);
21
Paul Bakker33b43f12013-08-20 11:48:36 +020022 key_len = unhexify( key_str, hex_key_string );
23 src_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000024
25 sha1_hmac( key_str, key_len, src_str, src_len, output );
26 hexify( hash_str, output, 20 );
27
Paul Bakker33b43f12013-08-20 11:48:36 +020028 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000029}
Paul Bakker33b43f12013-08-20 11:48:36 +020030/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000031
Paul Bakker68a4fce2013-08-20 12:42:31 +020032/* BEGIN_CASE depends_on:POLARSSL_SHA256_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020033void sha224_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
34 char *hex_hash_string)
Paul Bakker367dae42009-06-28 21:50:27 +000035{
36 unsigned char src_str[10000];
37 unsigned char key_str[10000];
38 unsigned char hash_str[10000];
39 unsigned char output[57];
Paul Bakker69998dd2009-07-11 19:15:20 +000040 int key_len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000041
42 memset(src_str, 0x00, 10000);
43 memset(key_str, 0x00, 10000);
44 memset(hash_str, 0x00, 10000);
45 memset(output, 0x00, 57);
46
Paul Bakker33b43f12013-08-20 11:48:36 +020047 key_len = unhexify( key_str, hex_key_string );
48 src_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000049
Paul Bakker9e36f042013-06-30 14:34:05 +020050 sha256_hmac( key_str, key_len, src_str, src_len, output, 1 );
Paul Bakker367dae42009-06-28 21:50:27 +000051 hexify( hash_str, output, 28 );
52
Paul Bakker33b43f12013-08-20 11:48:36 +020053 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000054}
Paul Bakker33b43f12013-08-20 11:48:36 +020055/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000056
Paul Bakker68a4fce2013-08-20 12:42:31 +020057/* BEGIN_CASE depends_on:POLARSSL_SHA256_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020058void sha256_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
59 char *hex_hash_string)
Paul Bakker367dae42009-06-28 21:50:27 +000060{
61 unsigned char src_str[10000];
62 unsigned char key_str[10000];
63 unsigned char hash_str[10000];
64 unsigned char output[65];
Paul Bakker69998dd2009-07-11 19:15:20 +000065 int key_len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000066
67 memset(src_str, 0x00, 10000);
68 memset(key_str, 0x00, 10000);
69 memset(hash_str, 0x00, 10000);
70 memset(output, 0x00, 65);
71
Paul Bakker33b43f12013-08-20 11:48:36 +020072 key_len = unhexify( key_str, hex_key_string );
73 src_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000074
Paul Bakker9e36f042013-06-30 14:34:05 +020075 sha256_hmac( key_str, key_len, src_str, src_len, output, 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000076 hexify( hash_str, output, 32 );
77
Paul Bakker33b43f12013-08-20 11:48:36 +020078 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000079}
Paul Bakker33b43f12013-08-20 11:48:36 +020080/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000081
Paul Bakker68a4fce2013-08-20 12:42:31 +020082/* BEGIN_CASE depends_on:POLARSSL_SHA512_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020083void sha384_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
84 char *hex_hash_string)
Paul Bakker367dae42009-06-28 21:50:27 +000085{
86 unsigned char src_str[10000];
87 unsigned char key_str[10000];
88 unsigned char hash_str[10000];
89 unsigned char output[97];
Paul Bakker69998dd2009-07-11 19:15:20 +000090 int key_len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000091
92 memset(src_str, 0x00, 10000);
93 memset(key_str, 0x00, 10000);
94 memset(hash_str, 0x00, 10000);
95 memset(output, 0x00, 97);
96
Paul Bakker33b43f12013-08-20 11:48:36 +020097 key_len = unhexify( key_str, hex_key_string );
98 src_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000099
Paul Bakker9e36f042013-06-30 14:34:05 +0200100 sha512_hmac( key_str, key_len, src_str, src_len, output, 1 );
Paul Bakker367dae42009-06-28 21:50:27 +0000101 hexify( hash_str, output, 48 );
102
Paul Bakker33b43f12013-08-20 11:48:36 +0200103 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000104}
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000106
Paul Bakker68a4fce2013-08-20 12:42:31 +0200107/* BEGIN_CASE depends_on:POLARSSL_SHA512_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200108void sha512_hmac( int trunc_size, char *hex_key_string, char *hex_src_string,
109 char *hex_hash_string)
Paul Bakker367dae42009-06-28 21:50:27 +0000110{
111 unsigned char src_str[10000];
112 unsigned char key_str[10000];
113 unsigned char hash_str[10000];
114 unsigned char output[129];
Paul Bakker69998dd2009-07-11 19:15:20 +0000115 int key_len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000116
117 memset(src_str, 0x00, 10000);
118 memset(key_str, 0x00, 10000);
119 memset(hash_str, 0x00, 10000);
120 memset(output, 0x00, 129);
121
Paul Bakker33b43f12013-08-20 11:48:36 +0200122 key_len = unhexify( key_str, hex_key_string );
123 src_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000124
Paul Bakker9e36f042013-06-30 14:34:05 +0200125 sha512_hmac( key_str, key_len, src_str, src_len, output, 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000126 hexify( hash_str, output, 64 );
127
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000129}
Paul Bakker33b43f12013-08-20 11:48:36 +0200130/* END_CASE */