Explicitly mention SHA2 algorithm in BLISS OIDs and signature schemes
[strongswan.git] / src / libstrongswan / tests / suites / test_hasher.c
1 /*
2 * Copyright (C) 2013-2015 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 }, /* 0 */
32 { OID_MD5, HASH_MD5, KEY_ANY }, /* 1 */
33 { OID_SHA1, HASH_SHA1, KEY_ANY }, /* 2 */
34 { OID_SHA224, HASH_SHA224, KEY_ANY }, /* 3 */
35 { OID_SHA256, HASH_SHA256, KEY_ANY }, /* 4 */
36 { OID_SHA384, HASH_SHA384, KEY_ANY }, /* 5 */
37 { OID_SHA512, HASH_SHA512, KEY_ANY }, /* 6 */
38 { OID_SHA3_224, HASH_SHA3_224, KEY_ANY }, /* 7 */
39 { OID_SHA3_256, HASH_SHA3_256, KEY_ANY }, /* 8 */
40 { OID_SHA3_384, HASH_SHA3_384, KEY_ANY }, /* 9 */
41 { OID_SHA3_512, HASH_SHA3_512, KEY_ANY }, /* 10 */
42 { OID_UNKNOWN, HASH_UNKNOWN, KEY_ANY }, /* 11 */
43 { OID_MD2_WITH_RSA, HASH_MD2, KEY_RSA }, /* 12 */
44 { OID_MD5_WITH_RSA, HASH_MD5, KEY_RSA }, /* 13 */
45 { OID_SHA1_WITH_RSA, HASH_SHA1, KEY_RSA }, /* 14 */
46 { OID_SHA224_WITH_RSA, HASH_SHA224, KEY_RSA }, /* 15 */
47 { OID_SHA256_WITH_RSA, HASH_SHA256, KEY_RSA }, /* 16 */
48 { OID_SHA384_WITH_RSA, HASH_SHA384, KEY_RSA }, /* 17 */
49 { OID_SHA512_WITH_RSA, HASH_SHA512, KEY_RSA }, /* 18 */
50 { OID_UNKNOWN, HASH_UNKNOWN, KEY_RSA }, /* 19 */
51 { OID_ECDSA_WITH_SHA1, HASH_SHA1, KEY_ECDSA }, /* 20 */
52 { OID_ECDSA_WITH_SHA256, HASH_SHA256, KEY_ECDSA }, /* 21 */
53 { OID_ECDSA_WITH_SHA384, HASH_SHA384, KEY_ECDSA }, /* 22 */
54 { OID_ECDSA_WITH_SHA512, HASH_SHA512, KEY_ECDSA }, /* 23 */
55 { OID_UNKNOWN, HASH_UNKNOWN, KEY_ECDSA }, /* 24 */
56 { OID_BLISS_WITH_SHA2_256, HASH_SHA256, KEY_BLISS }, /* 25 */
57 { OID_BLISS_WITH_SHA2_384, HASH_SHA384, KEY_BLISS }, /* 26 */
58 { OID_BLISS_WITH_SHA2_512, HASH_SHA512, KEY_BLISS }, /* 27 */
59 { OID_BLISS_WITH_SHA3_256, HASH_SHA3_256, KEY_BLISS }, /* 28 */
60 { OID_BLISS_WITH_SHA3_384, HASH_SHA3_384, KEY_BLISS }, /* 29 */
61 { OID_BLISS_WITH_SHA3_512, HASH_SHA3_512, KEY_BLISS }, /* 30 */
62 { OID_UNKNOWN, HASH_UNKNOWN, KEY_BLISS } /* 31 */
63 };
64
65 START_TEST(test_hasher_from_oid)
66 {
67 ck_assert(hasher_algorithm_from_oid(oids[_i].oid) == oids[_i].alg);
68 }
69 END_TEST
70
71 START_TEST(test_hasher_to_oid)
72 {
73 ck_assert(hasher_algorithm_to_oid(oids[_i].alg) == oids[_i].oid);
74 }
75 END_TEST
76
77 START_TEST(test_hasher_sig_to_oid)
78 {
79 ck_assert(hasher_signature_algorithm_to_oid(oids[_i].alg,
80 oids[_i].key) == oids[_i].oid);
81 }
82 END_TEST
83
84 typedef struct {
85 pseudo_random_function_t prf;
86 hash_algorithm_t alg;
87 }hasher_prf_t;
88
89 static hasher_prf_t prfs[] = {
90 { PRF_HMAC_MD5, HASH_MD5 },
91 { PRF_HMAC_SHA1, HASH_SHA1 },
92 { PRF_FIPS_SHA1_160, HASH_SHA1 },
93 { PRF_KEYED_SHA1, HASH_SHA1 },
94 { PRF_HMAC_SHA2_256, HASH_SHA256 },
95 { PRF_HMAC_SHA2_384, HASH_SHA384 },
96 { PRF_HMAC_SHA2_512, HASH_SHA512 },
97 { PRF_HMAC_TIGER, HASH_UNKNOWN },
98 { PRF_AES128_XCBC, HASH_UNKNOWN },
99 { PRF_AES128_CMAC, HASH_UNKNOWN },
100 { PRF_FIPS_DES, HASH_UNKNOWN },
101 { PRF_CAMELLIA128_XCBC, HASH_UNKNOWN },
102 { PRF_UNDEFINED, HASH_UNKNOWN },
103 { 0, HASH_UNKNOWN }
104 };
105
106 START_TEST(test_hasher_from_prf)
107 {
108 ck_assert(hasher_algorithm_from_prf(prfs[_i].prf) == prfs[_i].alg);
109 }
110 END_TEST
111
112 typedef struct {
113 integrity_algorithm_t auth;
114 hash_algorithm_t alg;
115 size_t length;
116 }hasher_auth_t;
117
118 static hasher_auth_t auths[] = {
119 { AUTH_UNDEFINED, HASH_MD2, 0 },
120 { AUTH_UNDEFINED, HASH_MD4, 0 },
121 { AUTH_UNDEFINED, HASH_SHA224, 0 },
122 { AUTH_UNDEFINED, 9, 0 },
123 { AUTH_UNDEFINED, HASH_UNKNOWN, 0 },
124 { AUTH_HMAC_MD5_96, HASH_MD5, 12 },
125 { AUTH_HMAC_SHA1_96, HASH_SHA1, 12 },
126 { AUTH_HMAC_SHA2_256_96, HASH_SHA256, 12 },
127 { AUTH_HMAC_MD5_128, HASH_MD5, 16 },
128 { AUTH_HMAC_SHA1_128, HASH_SHA1, 16 },
129 { AUTH_HMAC_SHA2_256_128, HASH_SHA256, 16 },
130 { AUTH_HMAC_SHA1_160, HASH_SHA1, 20 },
131 { AUTH_HMAC_SHA2_384_192, HASH_SHA384, 24 },
132 { AUTH_HMAC_SHA2_256_256, HASH_SHA256, 32 },
133 { AUTH_HMAC_SHA2_512_256, HASH_SHA512, 32 },
134 { AUTH_HMAC_SHA2_384_384, HASH_SHA384, 48 },
135 { AUTH_HMAC_SHA2_512_512, HASH_SHA512, 64 },
136 { AUTH_AES_CMAC_96, HASH_UNKNOWN, 0 },
137 { AUTH_AES_128_GMAC, HASH_UNKNOWN, 0 },
138 { AUTH_AES_192_GMAC, HASH_UNKNOWN, 0 },
139 { AUTH_AES_256_GMAC, HASH_UNKNOWN, 0 },
140 { AUTH_AES_XCBC_96, HASH_UNKNOWN, 0 },
141 { AUTH_DES_MAC, HASH_UNKNOWN, 0 },
142 { AUTH_CAMELLIA_XCBC_96, HASH_UNKNOWN, 0 },
143 { 0, HASH_UNKNOWN, 0 }
144 };
145
146 START_TEST(test_hasher_from_integrity)
147 {
148 size_t length;
149
150 length = 0;
151 ck_assert(hasher_algorithm_from_integrity(auths[_i].auth, NULL) ==
152 auths[_i].alg);
153 ck_assert(hasher_algorithm_from_integrity(auths[_i].auth, &length) ==
154 auths[_i].alg);
155 ck_assert(length == auths[_i].length);
156 }
157 END_TEST
158
159 START_TEST(test_hasher_to_integrity)
160 {
161 ck_assert(hasher_algorithm_to_integrity(
162 auths[_i].alg, auths[_i].length) == auths[_i].auth);
163 ck_assert(hasher_algorithm_to_integrity(
164 auths[_i].alg, 0) == AUTH_UNDEFINED);
165 }
166 END_TEST
167
168 Suite *hasher_suite_create()
169 {
170 Suite *s;
171 TCase *tc;
172
173 s = suite_create("hasher");
174
175 tc = tcase_create("from_oid");
176 tcase_add_loop_test(tc, test_hasher_from_oid, 0, 15);
177 suite_add_tcase(s, tc);
178
179 tc = tcase_create("to_oid");
180 tcase_add_loop_test(tc, test_hasher_to_oid, 0, 12);
181 suite_add_tcase(s, tc);
182
183 tc = tcase_create("sig_to_oid");
184 tcase_add_loop_test(tc, test_hasher_sig_to_oid, 11, countof(oids));
185 suite_add_tcase(s, tc);
186
187 tc = tcase_create("from_prf");
188 tcase_add_loop_test(tc, test_hasher_from_prf, 0, countof(prfs));
189 suite_add_tcase(s, tc);
190
191 tc = tcase_create("from_integrity");
192 tcase_add_loop_test(tc, test_hasher_from_integrity, 4, countof(auths));
193 suite_add_tcase(s, tc);
194
195 tc = tcase_create("to_integrity");
196 tcase_add_loop_test(tc, test_hasher_to_integrity, 0, 17);
197 suite_add_tcase(s, tc);
198
199 return s;
200 }