unit-tests: Added hasher tests
[strongswan.git] / src / libstrongswan / tests / suites / test_hasher.c
1 /*
2 * Copyright (C) 2013 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <crypto/hashers/hasher.h>
19 #include <crypto/prfs/prf.h>
20 #include <crypto/signers/signer.h>
21 #include <asn1/oid.h>
22 #include <utils/test.h>
23
24 typedef struct {
25 int oid;
26 hash_algorithm_t alg;
27 key_type_t key;
28 }hasher_oid_t;
29
30 hasher_oid_t oids[] = {
31 { OID_MD2, HASH_MD2, KEY_ANY },
32 { OID_MD5, HASH_MD5, KEY_ANY },
33 { OID_SHA1, HASH_SHA1, KEY_ANY },
34 { OID_SHA224, HASH_SHA224, KEY_ANY },
35 { OID_SHA256, HASH_SHA256, KEY_ANY },
36 { OID_SHA384, HASH_SHA384, KEY_ANY },
37 { OID_SHA512, HASH_SHA512, KEY_ANY },
38 { OID_UNKNOWN, HASH_UNKNOWN, KEY_ANY },
39 { OID_MD2_WITH_RSA, HASH_MD2, KEY_RSA },
40 { OID_MD5_WITH_RSA, HASH_MD5, KEY_RSA },
41 { OID_SHA1_WITH_RSA, HASH_SHA1, KEY_RSA },
42 { OID_SHA224_WITH_RSA, HASH_SHA224, KEY_RSA },
43 { OID_SHA256_WITH_RSA, HASH_SHA256, KEY_RSA },
44 { OID_SHA384_WITH_RSA, HASH_SHA384, KEY_RSA },
45 { OID_SHA512_WITH_RSA, HASH_SHA512, KEY_RSA },
46 { OID_UNKNOWN, HASH_UNKNOWN, KEY_RSA },
47 { OID_ECDSA_WITH_SHA1, HASH_SHA1, KEY_ECDSA },
48 { OID_ECDSA_WITH_SHA256, HASH_SHA256, KEY_ECDSA },
49 { OID_ECDSA_WITH_SHA384, HASH_SHA384, KEY_ECDSA },
50 { OID_ECDSA_WITH_SHA512, HASH_SHA512, KEY_ECDSA },
51 { OID_UNKNOWN, HASH_UNKNOWN, KEY_ECDSA }
52 };
53
54 START_TEST(test_hasher_from_oid)
55 {
56 ck_assert(hasher_algorithm_from_oid(oids[_i].oid) == oids[_i].alg);
57 }
58 END_TEST
59
60 START_TEST(test_hasher_to_oid)
61 {
62 ck_assert(hasher_algorithm_to_oid(oids[_i].alg) == oids[_i].oid);
63 }
64 END_TEST
65
66 START_TEST(test_hasher_sig_to_oid)
67 {
68 ck_assert(hasher_signature_algorithm_to_oid(oids[_i].alg,
69 oids[_i].key) == oids[_i].oid);
70 }
71 END_TEST
72
73 typedef struct {
74 pseudo_random_function_t prf;
75 hash_algorithm_t alg;
76 }hasher_prf_t;
77
78 hasher_prf_t prfs[] = {
79 { PRF_HMAC_MD5, HASH_MD5 },
80 { PRF_HMAC_SHA1, HASH_SHA1 },
81 { PRF_FIPS_SHA1_160, HASH_SHA1 },
82 { PRF_KEYED_SHA1, HASH_SHA1 },
83 { PRF_HMAC_SHA2_256, HASH_SHA256 },
84 { PRF_HMAC_SHA2_384, HASH_SHA384 },
85 { PRF_HMAC_SHA2_512, HASH_SHA512 },
86 { PRF_HMAC_TIGER, HASH_UNKNOWN },
87 { PRF_AES128_XCBC, HASH_UNKNOWN },
88 { PRF_AES128_CMAC, HASH_UNKNOWN },
89 { PRF_FIPS_DES, HASH_UNKNOWN },
90 { PRF_CAMELLIA128_XCBC, HASH_UNKNOWN },
91 { PRF_UNDEFINED, HASH_UNKNOWN },
92 { 0, HASH_UNKNOWN }
93 };
94
95 START_TEST(test_hasher_from_prf)
96 {
97 ck_assert(hasher_algorithm_from_prf(prfs[_i].prf) == prfs[_i].alg);
98 }
99 END_TEST
100
101 typedef struct {
102 integrity_algorithm_t auth;
103 hash_algorithm_t alg;
104 size_t length;
105 }hasher_auth_t;
106
107 hasher_auth_t auths[] = {
108 { AUTH_UNDEFINED, HASH_MD2, 0 },
109 { AUTH_UNDEFINED, HASH_MD4, 0 },
110 { AUTH_UNDEFINED, HASH_SHA224, 0 },
111 { AUTH_UNDEFINED, 9, 0 },
112 { AUTH_UNDEFINED, HASH_UNKNOWN, 0 },
113 { AUTH_HMAC_MD5_96, HASH_MD5, 12 },
114 { AUTH_HMAC_SHA1_96, HASH_SHA1, 12 },
115 { AUTH_HMAC_SHA2_256_96, HASH_SHA256, 12 },
116 { AUTH_HMAC_MD5_128, HASH_MD5, 16 },
117 { AUTH_HMAC_SHA1_128, HASH_SHA1, 16 },
118 { AUTH_HMAC_SHA2_256_128, HASH_SHA256, 16 },
119 { AUTH_HMAC_SHA1_160, HASH_SHA1, 20 },
120 { AUTH_HMAC_SHA2_384_192, HASH_SHA384, 24 },
121 { AUTH_HMAC_SHA2_256_256, HASH_SHA256, 32 },
122 { AUTH_HMAC_SHA2_512_256, HASH_SHA512, 32 },
123 { AUTH_HMAC_SHA2_384_384, HASH_SHA384, 48 },
124 { AUTH_HMAC_SHA2_512_512, HASH_SHA512, 64 },
125 { AUTH_AES_CMAC_96, HASH_UNKNOWN, 0 },
126 { AUTH_AES_128_GMAC, HASH_UNKNOWN, 0 },
127 { AUTH_AES_192_GMAC, HASH_UNKNOWN, 0 },
128 { AUTH_AES_256_GMAC, HASH_UNKNOWN, 0 },
129 { AUTH_AES_XCBC_96, HASH_UNKNOWN, 0 },
130 { AUTH_DES_MAC, HASH_UNKNOWN, 0 },
131 { AUTH_CAMELLIA_XCBC_96, HASH_UNKNOWN, 0 },
132 { 0, HASH_UNKNOWN, 0 }
133 };
134
135 START_TEST(test_hasher_from_integrity)
136 {
137 size_t length;
138
139 length = 0;
140 ck_assert(hasher_algorithm_from_integrity(auths[_i].auth, NULL) ==
141 auths[_i].alg);
142 ck_assert(hasher_algorithm_from_integrity(auths[_i].auth, &length) ==
143 auths[_i].alg);
144 ck_assert(length == auths[_i].length);
145 }
146 END_TEST
147
148 START_TEST(test_hasher_to_integrity)
149 {
150 ck_assert(hasher_algorithm_to_integrity(
151 auths[_i].alg, auths[_i].length) == auths[_i].auth);
152 ck_assert(hasher_algorithm_to_integrity(
153 auths[_i].alg, 0) == AUTH_UNDEFINED);
154 }
155 END_TEST
156
157 Suite *hasher_suite_create()
158 {
159 Suite *s;
160 TCase *tc;
161
162 s = suite_create("hasher");
163
164 tc = tcase_create("from_oid");
165 tcase_add_loop_test(tc, test_hasher_from_oid, 0, 15);
166 suite_add_tcase(s, tc);
167
168 tc = tcase_create("to_oid");
169 tcase_add_loop_test(tc, test_hasher_to_oid, 0, 8);
170 suite_add_tcase(s, tc);
171
172 tc = tcase_create("sig_to_oid");
173 tcase_add_loop_test(tc, test_hasher_sig_to_oid, 7, countof(oids));
174 suite_add_tcase(s, tc);
175
176 tc = tcase_create("from_prf");
177 tcase_add_loop_test(tc, test_hasher_from_prf, 0, countof(prfs));
178 suite_add_tcase(s, tc);
179
180 tc = tcase_create("from_integrity");
181 tcase_add_loop_test(tc, test_hasher_from_integrity, 4, countof(auths));
182 suite_add_tcase(s, tc);
183
184 tc = tcase_create("to_integrity");
185 tcase_add_loop_test(tc, test_hasher_to_integrity, 0, 17);
186 suite_add_tcase(s, tc);
187
188 return s;
189 }