Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 2 | #include <polarssl/sha1.h> |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 3 | #include <polarssl/sha256.h> |
| 4 | #include <polarssl/sha512.h> |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 5 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 6 | |
Paul Bakker | 68a4fce | 2013-08-20 12:42:31 +0200 | [diff] [blame] | 7 | /* BEGIN_CASE depends_on:POLARSSL_SHA1_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 8 | void sha1_hmac( int trunc_size, char *hex_key_string, char *hex_src_string, |
| 9 | char *hex_hash_string) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 10 | { |
| 11 | unsigned char src_str[10000]; |
| 12 | unsigned char key_str[10000]; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 13 | unsigned char hash_str[41]; |
| 14 | unsigned char output[20]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 15 | int key_len, src_len; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 16 | sha1_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 18 | memset(src_str, 0x00, sizeof src_str); |
| 19 | memset(key_str, 0x00, sizeof key_str); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 20 | sha1_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 21 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 22 | key_len = unhexify( key_str, hex_key_string ); |
| 23 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 24 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 25 | /* Test the all-in-one interface */ |
| 26 | memset(hash_str, 0x00, sizeof hash_str); |
| 27 | memset(output, 0x00, sizeof output); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 29 | sha1_hmac( key_str, key_len, src_str, src_len, output ); |
| 30 | |
| 31 | hexify( hash_str, output, sizeof output ); |
| 32 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 33 | |
| 34 | /* Also test the "streaming" interface */ |
| 35 | memset( hash_str, 0x00, sizeof hash_str ); |
| 36 | memset( output, 0x00, sizeof output ); |
| 37 | memset( &ctx, 0x00, sizeof ctx ); |
| 38 | |
| 39 | sha1_hmac_starts( &ctx, key_str, key_len ); |
| 40 | sha1_hmac_update( &ctx, src_str, 0 ); |
| 41 | sha1_hmac_update( &ctx, src_str, src_len / 2 ); |
| 42 | sha1_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 43 | sha1_hmac_update( &ctx, src_str + src_len, 0 ); |
| 44 | sha1_hmac_finish( &ctx, output ); |
| 45 | |
| 46 | hexify( hash_str, output, sizeof output ); |
| 47 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 48 | |
| 49 | /* Again, to test hmac_reset() */ |
| 50 | memset( hash_str, 0x00, sizeof hash_str ); |
| 51 | memset( output, 0x00, sizeof output ); |
| 52 | |
| 53 | sha1_hmac_reset( &ctx ); |
| 54 | sha1_hmac_update( &ctx, src_str, src_len / 2 ); |
| 55 | sha1_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 56 | sha1_hmac_finish( &ctx, output ); |
| 57 | |
| 58 | hexify( hash_str, output, sizeof output ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 59 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 60 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 61 | exit: |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 62 | sha1_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 63 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 64 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 65 | |
Paul Bakker | 68a4fce | 2013-08-20 12:42:31 +0200 | [diff] [blame] | 66 | /* BEGIN_CASE depends_on:POLARSSL_SHA256_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 67 | void sha224_hmac( int trunc_size, char *hex_key_string, char *hex_src_string, |
| 68 | char *hex_hash_string) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 69 | { |
| 70 | unsigned char src_str[10000]; |
| 71 | unsigned char key_str[10000]; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 72 | unsigned char hash_str[57]; |
| 73 | unsigned char output[28]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 74 | int key_len, src_len; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 75 | sha256_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 76 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 77 | memset(src_str, 0x00, sizeof src_str); |
| 78 | memset(key_str, 0x00, sizeof key_str); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 79 | sha256_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 80 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 81 | key_len = unhexify( key_str, hex_key_string ); |
| 82 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 84 | /* Test the all-in-one interface */ |
| 85 | memset(hash_str, 0x00, sizeof hash_str); |
| 86 | memset(output, 0x00, sizeof output); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 87 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 88 | sha256_hmac( key_str, key_len, src_str, src_len, output, 1 ); |
| 89 | |
| 90 | hexify( hash_str, output, sizeof output ); |
| 91 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 92 | |
| 93 | /* Also test the "streaming" interface */ |
| 94 | memset( hash_str, 0x00, sizeof hash_str ); |
| 95 | memset( output, 0x00, sizeof output ); |
| 96 | memset( &ctx, 0x00, sizeof ctx ); |
| 97 | |
| 98 | sha256_hmac_starts( &ctx, key_str, key_len, 1 ); |
| 99 | sha256_hmac_update( &ctx, src_str, 0 ); |
| 100 | sha256_hmac_update( &ctx, src_str, src_len / 2 ); |
| 101 | sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 102 | sha256_hmac_update( &ctx, src_str + src_len, 0 ); |
| 103 | sha256_hmac_finish( &ctx, output ); |
| 104 | |
| 105 | hexify( hash_str, output, sizeof output ); |
| 106 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 107 | |
| 108 | /* Again, to test hmac_reset() */ |
| 109 | memset( hash_str, 0x00, sizeof hash_str ); |
| 110 | memset( output, 0x00, sizeof output ); |
| 111 | |
| 112 | sha256_hmac_reset( &ctx ); |
| 113 | sha256_hmac_update( &ctx, src_str, src_len / 2 ); |
| 114 | sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 115 | sha256_hmac_finish( &ctx, output ); |
| 116 | |
| 117 | hexify( hash_str, output, sizeof output ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 118 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 119 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 120 | exit: |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 121 | sha256_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 122 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 123 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 124 | |
Paul Bakker | 68a4fce | 2013-08-20 12:42:31 +0200 | [diff] [blame] | 125 | /* BEGIN_CASE depends_on:POLARSSL_SHA256_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 126 | void sha256_hmac( int trunc_size, char *hex_key_string, char *hex_src_string, |
| 127 | char *hex_hash_string) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 128 | { |
| 129 | unsigned char src_str[10000]; |
| 130 | unsigned char key_str[10000]; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 131 | unsigned char hash_str[65]; |
| 132 | unsigned char output[32]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 133 | int key_len, src_len; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 134 | sha256_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 135 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 136 | memset(src_str, 0x00, sizeof src_str); |
| 137 | memset(key_str, 0x00, sizeof key_str); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 138 | sha256_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 139 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 140 | key_len = unhexify( key_str, hex_key_string ); |
| 141 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 143 | /* Test the all-in-one interface */ |
| 144 | memset(hash_str, 0x00, sizeof hash_str); |
| 145 | memset(output, 0x00, sizeof output); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 147 | sha256_hmac( key_str, key_len, src_str, src_len, output, 0 ); |
| 148 | |
| 149 | hexify( hash_str, output, sizeof output ); |
| 150 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 151 | |
| 152 | /* Also test the "streaming" interface */ |
| 153 | memset( hash_str, 0x00, sizeof hash_str ); |
| 154 | memset( output, 0x00, sizeof output ); |
| 155 | memset( &ctx, 0x00, sizeof ctx ); |
| 156 | |
| 157 | sha256_hmac_starts( &ctx, key_str, key_len, 0 ); |
| 158 | sha256_hmac_update( &ctx, src_str, 0 ); |
| 159 | sha256_hmac_update( &ctx, src_str, src_len / 2 ); |
| 160 | sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 161 | sha256_hmac_update( &ctx, src_str + src_len, 0 ); |
| 162 | sha256_hmac_finish( &ctx, output ); |
| 163 | |
| 164 | hexify( hash_str, output, sizeof output ); |
| 165 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 166 | |
| 167 | /* Again, to test hmac_reset() */ |
| 168 | memset( hash_str, 0x00, sizeof hash_str ); |
| 169 | memset( output, 0x00, sizeof output ); |
| 170 | |
| 171 | sha256_hmac_reset( &ctx ); |
| 172 | sha256_hmac_update( &ctx, src_str, src_len / 2 ); |
| 173 | sha256_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 174 | sha256_hmac_finish( &ctx, output ); |
| 175 | |
| 176 | hexify( hash_str, output, sizeof output ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 177 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 178 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 179 | exit: |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 180 | sha256_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 181 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 182 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 183 | |
Paul Bakker | 68a4fce | 2013-08-20 12:42:31 +0200 | [diff] [blame] | 184 | /* BEGIN_CASE depends_on:POLARSSL_SHA512_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 185 | void sha384_hmac( int trunc_size, char *hex_key_string, char *hex_src_string, |
| 186 | char *hex_hash_string) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 187 | { |
| 188 | unsigned char src_str[10000]; |
| 189 | unsigned char key_str[10000]; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 190 | unsigned char hash_str[97]; |
| 191 | unsigned char output[48]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 192 | int key_len, src_len; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 193 | sha512_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 194 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 195 | memset(src_str, 0x00, sizeof src_str); |
| 196 | memset(key_str, 0x00, sizeof key_str); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 197 | sha512_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 198 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 199 | key_len = unhexify( key_str, hex_key_string ); |
| 200 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 202 | /* Test the all-in-one interface */ |
| 203 | memset(hash_str, 0x00, sizeof hash_str); |
| 204 | memset(output, 0x00, sizeof output); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 206 | sha512_hmac( key_str, key_len, src_str, src_len, output, 1 ); |
| 207 | |
| 208 | hexify( hash_str, output, sizeof output ); |
| 209 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 210 | |
| 211 | /* Also test the "streaming" interface */ |
| 212 | memset( hash_str, 0x00, sizeof hash_str ); |
| 213 | memset( output, 0x00, sizeof output ); |
| 214 | memset( &ctx, 0x00, sizeof ctx ); |
| 215 | |
| 216 | sha512_hmac_starts( &ctx, key_str, key_len, 1 ); |
| 217 | sha512_hmac_update( &ctx, src_str, 0 ); |
| 218 | sha512_hmac_update( &ctx, src_str, src_len / 2 ); |
| 219 | sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 220 | sha512_hmac_update( &ctx, src_str + src_len, 0 ); |
| 221 | sha512_hmac_finish( &ctx, output ); |
| 222 | |
| 223 | hexify( hash_str, output, sizeof output ); |
| 224 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 225 | |
| 226 | /* Again, to test hmac_reset() */ |
| 227 | memset( hash_str, 0x00, sizeof hash_str ); |
| 228 | memset( output, 0x00, sizeof output ); |
| 229 | |
| 230 | sha512_hmac_reset( &ctx ); |
| 231 | sha512_hmac_update( &ctx, src_str, src_len / 2 ); |
| 232 | sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 233 | sha512_hmac_finish( &ctx, output ); |
| 234 | |
| 235 | hexify( hash_str, output, sizeof output ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 236 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 237 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 238 | exit: |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 239 | sha512_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 240 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 241 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 242 | |
Paul Bakker | 68a4fce | 2013-08-20 12:42:31 +0200 | [diff] [blame] | 243 | /* BEGIN_CASE depends_on:POLARSSL_SHA512_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 244 | void sha512_hmac( int trunc_size, char *hex_key_string, char *hex_src_string, |
| 245 | char *hex_hash_string) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 246 | { |
| 247 | unsigned char src_str[10000]; |
| 248 | unsigned char key_str[10000]; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 249 | unsigned char hash_str[129]; |
| 250 | unsigned char output[64]; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 251 | int key_len, src_len; |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 252 | sha512_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 254 | memset(src_str, 0x00, sizeof src_str); |
| 255 | memset(key_str, 0x00, sizeof key_str); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 256 | sha512_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 257 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 258 | key_len = unhexify( key_str, hex_key_string ); |
| 259 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 260 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 261 | /* Test the all-in-one interface */ |
| 262 | memset(hash_str, 0x00, sizeof hash_str); |
| 263 | memset(output, 0x00, sizeof output); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 264 | |
Manuel Pégourié-Gonnard | f8708dd | 2014-04-02 17:17:19 +0200 | [diff] [blame] | 265 | sha512_hmac( key_str, key_len, src_str, src_len, output, 0 ); |
| 266 | |
| 267 | hexify( hash_str, output, sizeof output ); |
| 268 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 269 | |
| 270 | /* Also test the "streaming" interface */ |
| 271 | memset( hash_str, 0x00, sizeof hash_str ); |
| 272 | memset( output, 0x00, sizeof output ); |
| 273 | memset( &ctx, 0x00, sizeof ctx ); |
| 274 | |
| 275 | sha512_hmac_starts( &ctx, key_str, key_len, 0 ); |
| 276 | sha512_hmac_update( &ctx, src_str, 0 ); |
| 277 | sha512_hmac_update( &ctx, src_str, src_len / 2 ); |
| 278 | sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 279 | sha512_hmac_update( &ctx, src_str + src_len, 0 ); |
| 280 | sha512_hmac_finish( &ctx, output ); |
| 281 | |
| 282 | hexify( hash_str, output, sizeof output ); |
| 283 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
| 284 | |
| 285 | /* Again, to test hmac_reset() */ |
| 286 | memset( hash_str, 0x00, sizeof hash_str ); |
| 287 | memset( output, 0x00, sizeof output ); |
| 288 | |
| 289 | sha512_hmac_reset( &ctx ); |
| 290 | sha512_hmac_update( &ctx, src_str, src_len / 2 ); |
| 291 | sha512_hmac_update( &ctx, src_str + src_len / 2, src_len - src_len / 2 ); |
| 292 | sha512_hmac_finish( &ctx, output ); |
| 293 | |
| 294 | hexify( hash_str, output, sizeof output ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 295 | TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 296 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 297 | exit: |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 298 | sha512_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 299 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 300 | /* END_CASE */ |