• R/O
  • SSH
  • HTTPS

eirsamples: Commit


Commit MetaInfo

Revision27 (tree)
Time2019-06-18 18:50:43
Authorquiret

Log Message

- added hashlib unit tests for SHA256 and HMAC

Change Summary

Incremental Difference

--- bigunittest/src/hashtest.cpp (nonexistent)
+++ bigunittest/src/hashtest.cpp (revision 27)
@@ -0,0 +1,134 @@
1+#include <hashlib/sha256.h>
2+#include <hashlib/hmac.h>
3+#include <sdk/DataUtil.h>
4+
5+#include <stdio.h>
6+#include <string.h>
7+#include <assert.h>
8+
9+void HASH_TESTS( void )
10+{
11+ printf( "testing eir::ROTR..." );
12+ {
13+ assert( eir::ROTR <unsigned int> ( 1, 1 ) == 0x80000000 );
14+ assert( eir::ROTR <unsigned int> ( 2, 1 ) == 1 );
15+ assert( eir::ROTR <unsigned int> ( 0x20, 5 ) == 1 );
16+ }
17+ printf( "ok.\n" );
18+
19+ printf( "testing eir::ROTL..." );
20+ {
21+ assert( eir::ROTL <unsigned int> ( 1, 1 ) == 2 );
22+ assert( eir::ROTL <unsigned int> ( 0x80000000, 1 ) == 1 );
23+ assert( eir::ROTL <unsigned int> ( 0x20, 3 ) == 0x100 );
24+ }
25+ printf( "ok.\n" );
26+
27+ printf( "testing SHA256 hashing..." );
28+ {
29+ // Try a very simple hash.
30+ {
31+ static const unsigned char exp_hash[] =
32+ {
33+ 0x40, 0x4c, 0xdd, 0x7b, 0xc1, 0x09, 0xc4, 0x32,
34+ 0xf8, 0xcc, 0x24, 0x43, 0xb4, 0x5b, 0xcf, 0xe9,
35+ 0x59, 0x80, 0xf5, 0x10, 0x72, 0x15, 0xc6, 0x45,
36+ 0x23, 0x6e, 0x57, 0x79, 0x29, 0xac, 0x3e, 0x52
37+ };
38+ static_assert( countof(exp_hash) == SHA256Hasher::HASH_BYTE_LEN );
39+
40+ SHA256Hasher hasher;
41+
42+ hasher.Update( "meow", 4 );
43+ hasher.Finalize();
44+
45+ unsigned char reshash[ SHA256Hasher::HASH_BYTE_LEN ];
46+ hasher.CopyHashedValue( reshash );
47+
48+ assert( memcmp( reshash, exp_hash, SHA256Hasher::HASH_BYTE_LEN ) == 0 );
49+ }
50+
51+ // Now try a longer version.
52+ {
53+ static const unsigned char exp_hash[] =
54+ {
55+ 0x1b, 0xeb, 0x2d, 0x96, 0xd3, 0xfe, 0xa0,
56+ 0xfc, 0x58, 0x8a, 0xa7, 0x23, 0x08, 0xc1,
57+ 0x4a, 0x3f, 0xee, 0xd5, 0x66, 0x39, 0xed,
58+ 0xa8, 0xd2, 0x8b, 0x15, 0xb0, 0x8c, 0x8a,
59+ 0x22, 0x0a, 0x57, 0xb9
60+ };
61+ static_assert( countof(exp_hash) == SHA256Hasher::HASH_BYTE_LEN );
62+
63+ static const char test_sentence[] = "this is a test sentence that is pretty cool aint it? with it we can actually test very long input.";
64+
65+ SHA256Hasher hasher;
66+
67+ hasher.Update( test_sentence, sizeof(test_sentence)-1 );
68+ hasher.Finalize();
69+
70+ unsigned char reshash[ SHA256Hasher::HASH_BYTE_LEN ];
71+ hasher.CopyHashedValue( reshash );
72+
73+ assert( memcmp( reshash, exp_hash, SHA256Hasher::HASH_BYTE_LEN ) == 0 );
74+ }
75+ }
76+ printf( "ok.\n" );
77+
78+ printf( "testing HMAC based on SHA256..." );
79+ {
80+ // Doing a simple test.
81+ {
82+ static const char key[] = "TEST-KEY-MEOW";
83+
84+ HMACCalculator <SHA256Hasher> hmac( key, sizeof(key) - 1 );
85+
86+ static const char hash_sentence[] = "meow this is the police";
87+
88+ hmac.Update( hash_sentence, sizeof(hash_sentence) - 1 );
89+ hmac.Finalize();
90+
91+ static const unsigned char exp_hmac[] =
92+ {
93+ 0x8e, 0xee, 0x64, 0x8a, 0xe5, 0xea, 0x6f,
94+ 0x0b, 0x7c, 0x03, 0x7e, 0x79, 0x49, 0x94,
95+ 0xfb, 0xbf, 0x19, 0x5f, 0x35, 0xfe, 0x6c,
96+ 0x88, 0xe9, 0x2e, 0x09, 0x66, 0x9a, 0xd1,
97+ 0xec, 0x4c, 0x9c, 0xab
98+ };
99+ static_assert( countof(exp_hmac) == decltype(hmac)::HMAC_LEN );
100+
101+ unsigned char hmac_val[ decltype(hmac)::HMAC_LEN ];
102+ hmac.CopyHMACValue( hmac_val );
103+
104+ assert( memcmp( hmac_val, exp_hmac, decltype(hmac)::HMAC_LEN ) == 0 );
105+ }
106+
107+ // Now a test with a key that is longer than the SHA256 hash block length (64bytes).
108+ {
109+ static const char key[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890987654321";
110+
111+ HMACCalculator <SHA256Hasher> hmac( key, sizeof(key) - 1 );
112+
113+ static const char hash_sentence[] = "meow this is the police";
114+
115+ hmac.Update( hash_sentence, sizeof(hash_sentence) - 1 );
116+ hmac.Finalize();
117+
118+ static const unsigned char exp_hmac[] =
119+ {
120+ 0x83, 0xa1, 0x03, 0x9f, 0xd3, 0xc4, 0x67, 0x43, 0xe8,
121+ 0xf1, 0xac, 0x43, 0x5a, 0xfc, 0x06, 0xef, 0x99, 0xb3,
122+ 0x5c, 0xaf, 0xc8, 0x8e, 0xb5, 0xe5, 0xfd, 0xb1, 0x5a,
123+ 0x94, 0xe5, 0x5d, 0x13, 0xfd
124+ };
125+ static_assert( countof(exp_hmac) == decltype(hmac)::HMAC_LEN );
126+
127+ unsigned char hmac_val[ decltype(hmac)::HMAC_LEN ];
128+ hmac.CopyHMACValue( hmac_val );
129+
130+ assert( memcmp( hmac_val, exp_hmac, decltype(hmac)::HMAC_LEN ) == 0 );
131+ }
132+ }
133+ printf( "ok.\n" );
134+}
\ No newline at end of file
--- bigunittest/src/main.cpp (revision 26)
+++ bigunittest/src/main.cpp (revision 27)
@@ -5,6 +5,8 @@
55
66 #include <libparse/syntaxutil.h>
77
8+extern void HASH_TESTS( void );
9+
810 void SYNTAXUTIL_TEST( void )
911 {
1012 printf( "testing trim_scan_syntax positives..." );
@@ -31,7 +33,7 @@
3133 CSyntax <char> syntax( some_text, sizeof(some_text)-1 );
3234
3335 CSyntax <char> trimmed;
34-
36+
3537 bool gotToTrim = trim_scan_syntax( syntax, trimmed );
3638
3739 assert( gotToTrim == true );
@@ -151,6 +153,7 @@
151153 int main( int argc, char *argv[] )
152154 {
153155 SYNTAXUTIL_TEST();
156+ HASH_TESTS();
154157
155158 return 0;
156159 }
\ No newline at end of file
Show on old repository browser