14cc321223574ffe07476550144bde975edf9c84
[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 static 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_BLISS_WITH_SHA256, HASH_SHA256, KEY_BLISS },
52 { OID_BLISS_WITH_SHA384, HASH_SHA384, KEY_BLISS },
53 { OID_BLISS_WITH_SHA512, HASH_SHA512, KEY_BLISS },
54 { OID_UNKNOWN, HASH_UNKNOWN, KEY_ECDSA }
55 };
56
57 START_TEST(test_hasher_from_oid)
58 {
59 ck_assert(hasher_algorithm_from_oid(oids[_i].oid) == oids[_i].alg);
60 }
61 END_TEST
62
63 START_TEST(test_hasher_to_oid)
64 {
65 ck_assert(hasher_algorithm_to_oid(oids[_i].alg) == oids[_i].oid);
66 }
67 END_TEST
68
69 START_TEST(test_hasher_sig_to_oid)
70 {
71 ck_assert(hasher_signature_algorithm_to_oid(oids[_i].alg,
72 oids[_i].key) == oids[_i].oid);
73 }
74 END_TEST
75
76 typedef struct {
77 pseudo_random_function_t prf;
78 hash_algorithm_t alg;
79 }hasher_prf_t;
80
81 static hasher_prf_t prfs[] = {
82 { PRF_HMAC_MD5, HASH_MD5 },
83 { PRF_HMAC_SHA1, HASH_SHA1 },
84 { PRF_FIPS_SHA1_160, HASH_SHA1 },
85 { PRF_KEYED_SHA1, HASH_SHA1 },
86 { PRF_HMAC_SHA2_256, HASH_SHA256 },
87 { PRF_HMAC_SHA2_384, HASH_SHA384 },
88 { PRF_HMAC_SHA2_512, HASH_SHA512 },
89 { PRF_HMAC_TIGER, HASH_UNKNOWN },
90 { PRF_AES128_XCBC, HASH_UNKNOWN },
91 { PRF_AES128_CMAC, HASH_UNKNOWN },
92 { PRF_FIPS_DES, HASH_UNKNOWN },
93 { PRF_CAMELLIA128_XCBC, HASH_UNKNOWN },
94 { PRF_UNDEFINED, HASH_UNKNOWN },
95 { 0, HASH_UNKNOWN }
96 };
97
98 START_TEST(test_hasher_from_prf)
99 {
100 ck_assert(hasher_algorithm_from_prf(prfs[_i].prf) == prfs[_i].alg);
101 }
102 END_TEST
103
104 typedef struct {
105 integrity_algorithm_t auth;
106 hash_algorithm_t alg;
107 size_t length;
108 }hasher_auth_t;
109
110 static hasher_auth_t auths[] = {
111 { AUTH_UNDEFINED, HASH_MD2, 0 },
112 { AUTH_UNDEFINED, HASH_MD4, 0 },
113 { AUTH_UNDEFINED, HASH_SHA224, 0 },
114 { AUTH_UNDEFINED, 9, 0 },
115 { AUTH_UNDEFINED, HASH_UNKNOWN, 0 },
116 { AUTH_HMAC_MD5_96, HASH_MD5, 12 },
117 { AUTH_HMAC_SHA1_96, HASH_SHA1, 12 },
118 { AUTH_HMAC_SHA2_256_96, HASH_SHA256, 12 },
119 { AUTH_HMAC_MD5_128, HASH_MD5, 16 },
120 { AUTH_HMAC_SHA1_128, HASH_SHA1, 16 },
121 { AUTH_HMAC_SHA2_256_128, HASH_SHA256, 16 },
122 { AUTH_HMAC_SHA1_160, HASH_SHA1, 20 },
123 { AUTH_HMAC_SHA2_384_192, HASH_SHA384, 24 },
124 { AUTH_HMAC_SHA2_256_256, HASH_SHA256, 32 },
125 { AUTH_HMAC_SHA2_512_256, HASH_SHA512, 32 },
126 { AUTH_HMAC_SHA2_384_384, HASH_SHA384, 48 },
127 { AUTH_HMAC_SHA2_512_512, HASH_SHA512, 64 },
128 { AUTH_AES_CMAC_96, HASH_UNKNOWN, 0 },
129 { AUTH_AES_128_GMAC, HASH_UNKNOWN, 0 },
130 { AUTH_AES_192_GMAC, HASH_UNKNOWN, 0 },
131 { AUTH_AES_256_GMAC, HASH_UNKNOWN, 0 },
132 { AUTH_AES_XCBC_96, HASH_UNKNOWN, 0 },
133 { AUTH_DES_MAC, HASH_UNKNOWN, 0 },
134 { AUTH_CAMELLIA_XCBC_96, HASH_UNKNOWN, 0 },
135 { 0, HASH_UNKNOWN, 0 }
136 };
137
138 START_TEST(test_hasher_from_integrity)
139 {
140 size_t length;
141
142 length = 0;
143 ck_assert(hasher_algorithm_from_integrity(auths[_i].auth, NULL) ==
144 auths[_i].alg);
145 ck_assert(hasher_algorithm_from_integrity(auths[_i].auth, &length) ==
146 auths[_i].alg);
147 ck_assert(length == auths[_i].length);
148 }
149 END_TEST
150
151 START_TEST(test_hasher_to_integrity)
152 {
153 ck_assert(hasher_algorithm_to_integrity(
154 auths[_i].alg, auths[_i].length) == auths[_i].auth);
155 ck_assert(hasher_algorithm_to_integrity(
156 auths[_i].alg, 0) == AUTH_UNDEFINED);
157 }
158 END_TEST
159
160 Suite *hasher_suite_create()
161 {
162 Suite *s;
163 TCase *tc;
164
165 s = suite_create("hasher");
166
167 tc = tcase_create("from_oid");
168 tcase_add_loop_test(tc, test_hasher_from_oid, 0, 15);
169 suite_add_tcase(s, tc);
170
171 tc = tcase_create("to_oid");
172 tcase_add_loop_test(tc, test_hasher_to_oid, 0, 8);
173 suite_add_tcase(s, tc);
174
175 tc = tcase_create("sig_to_oid");
176 tcase_add_loop_test(tc, test_hasher_sig_to_oid, 7, countof(oids));
177 suite_add_tcase(s, tc);
178
179 tc = tcase_create("from_prf");
180 tcase_add_loop_test(tc, test_hasher_from_prf, 0, countof(prfs));
181 suite_add_tcase(s, tc);
182
183 tc = tcase_create("from_integrity");
184 tcase_add_loop_test(tc, test_hasher_from_integrity, 4, countof(auths));
185 suite_add_tcase(s, tc);
186
187 tc = tcase_create("to_integrity");
188 tcase_add_loop_test(tc, test_hasher_to_integrity, 0, 17);
189 suite_add_tcase(s, tc);
190
191 return s;
192 }