l10n: Update translation catalogues
[libisds.git] / test / offline / compute_hash.c
blob2fe4dfd690c764cc824c73429b548341b19555cf
1 #include "../test.h"
2 #include "crypto.h"
3 #include <string.h>
5 static int cmp_hash (const struct isds_hash *h1, const struct isds_hash *h2) {
6 if (h1 == NULL && h2 == NULL) PASS_TEST;
7 if (!(h1 != NULL && h2 != NULL))
8 FAIL_TEST("Hash structure is NULL and shouldn't be or vice versa");
9 if (h1->algorithm != h2->algorithm)
10 FAIL_TEST("Wrong hash algorithm");
12 if (h1->length != h2->length)
13 FAIL_TEST("Wrong hash length");
14 for (size_t i = 0; i < h1->length; i++) {
15 if (((uint8_t *) (h1->value))[i] != ((uint8_t *) (h2->value))[i])
16 FAIL_TEST("Wrong hash value");
18 PASS_TEST;
21 static int test_compute_hash(const isds_error error,
22 const struct isds_hash *correct, struct isds_hash *test, const void *input,
23 const size_t input_length) {
24 isds_error err;
25 int status;
27 if (!correct || !test) return 1;
29 err = _isds_compute_hash(input, input_length, test);
30 if (err != error) {
31 free(test->value); test->value = NULL;
32 FAIL_TEST("Wrong return value");
35 if (!err) {
36 status = cmp_hash(correct, test);
37 } else {
38 status = 0;
41 free(test->value); test->value = NULL;
42 return (status);
45 int main(void) {
47 INIT_TEST("compute_hash");
49 if (_isds_init_crypto())
50 ABORT_UNIT("init_crypto() failed");
52 char input[] = "42";
53 struct isds_hash test = {
54 .algorithm = HASH_ALGORITHM_SHA_1,
55 .length = 0,
56 .value = NULL
58 struct isds_hash correct = {
59 .algorithm = HASH_ALGORITHM_SHA_1,
60 .length = 20,
61 .value = (uint8_t[]) {
62 0x27, 0x45, 0x34, 0x43, 0x27, 0x4a, 0x2d, 0x9c, 0xe2, 0xca,
63 0xfe, 0x6f, 0xe0, 0x16, 0x3a, 0x4a, 0x32, 0x74, 0x62, 0xaa
66 TEST("standard SHA1", test_compute_hash, IE_SUCCESS, &correct, &test,
67 input, sizeof(input));
69 TEST("NULL input", test_compute_hash, IE_INVAL, &correct, &test, NULL, 1);
71 correct.value = (uint8_t[]) {
72 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
73 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09
75 test.algorithm = HASH_ALGORITHM_SHA_1;
76 TEST("zero length with NULL input",
77 test_compute_hash, IE_SUCCESS, &correct, &test, NULL, 0);
79 correct.algorithm = HASH_ALGORITHM_MD5;
80 correct.length = 16;
81 correct.value = (uint8_t[]) {
82 0x67, 0x54, 0x80, 0xc6, 0x5a, 0x65, 0x57, 0x6e,
83 0xdf, 0xf9, 0x26, 0xa7, 0xf0, 0xa8, 0x25, 0x70
85 test.algorithm = HASH_ALGORITHM_MD5;
86 TEST("MD5", test_compute_hash, IE_SUCCESS, &correct, &test, input,
87 sizeof(input));
89 correct.algorithm = HASH_ALGORITHM_SHA_224;
90 correct.length = 28;
91 correct.value = (uint8_t[]) {
92 0x58, 0x8d, 0x39, 0xd2, 0x1a, 0x1f, 0xef, 0x0b,
93 0x89, 0xdf, 0x62, 0x1b, 0x3e, 0xb1, 0x41, 0x62,
94 0x70, 0x22, 0xe5, 0x8f, 0xe9, 0x25, 0x83, 0x06,
95 0x73, 0xcb, 0x2b, 0x03
97 test.algorithm = HASH_ALGORITHM_SHA_224;
98 TEST("SHA-224", test_compute_hash, IE_SUCCESS, &correct, &test, input,
99 sizeof(input));
101 correct.algorithm = HASH_ALGORITHM_SHA_256;
102 correct.length = 32;
103 correct.value = (uint8_t[]) {
104 0xe0, 0x8d, 0xe2, 0x65, 0x65, 0x32, 0xc1, 0x05,
105 0x88, 0xb5, 0xbf, 0x37, 0x06, 0x0f, 0x4c, 0xc6,
106 0xec, 0xff, 0x71, 0x7a, 0xa3, 0x93, 0x54, 0x59,
107 0x4e, 0x92, 0x57, 0xc1, 0x8b, 0x47, 0xb7, 0x5a
109 test.algorithm = HASH_ALGORITHM_SHA_256;
110 TEST("SHA-256", test_compute_hash, IE_SUCCESS, &correct, &test, input,
111 sizeof(input));
113 correct.algorithm = HASH_ALGORITHM_SHA_384;
114 correct.length = 48;
115 correct.value = (uint8_t[]) {
116 0x81, 0x2b, 0x94, 0x0b, 0x9c, 0x58, 0x60, 0x31,
117 0xb8, 0x20, 0x4b, 0xa7, 0xf1, 0x96, 0xdb, 0x5b,
118 0xfe, 0x55, 0xec, 0x87, 0x33, 0x92, 0x78, 0x5a,
119 0x78, 0x0a, 0x38, 0xe4, 0xaa, 0x9e, 0xe0, 0x4d,
120 0x0b, 0xbe, 0x55, 0x43, 0x2c, 0x9a, 0x85, 0x29,
121 0xb4, 0x8d, 0x6b, 0x6f, 0xea, 0xd1, 0x95, 0xa8
123 test.algorithm = HASH_ALGORITHM_SHA_384;
124 TEST("SHA-384", test_compute_hash, IE_SUCCESS, &correct, &test, input,
125 sizeof(input));
127 correct.algorithm = HASH_ALGORITHM_SHA_512;
128 correct.length = 64;
129 correct.value = (uint8_t[]) {
130 0x37, 0x39, 0x43, 0x2c, 0x23, 0x33, 0x2b, 0x88,
131 0x8d, 0x09, 0xfb, 0xab, 0xf5, 0x51, 0x8f, 0xb5,
132 0x0b, 0xf1, 0xc8, 0x7a, 0x7f, 0x28, 0x23, 0xcb,
133 0xf2, 0x64, 0xf3, 0xae, 0x73, 0xa8, 0xf1, 0x75,
134 0x88, 0xc6, 0x3f, 0xc4, 0xfd, 0x25, 0x5b, 0x4c,
135 0xbc, 0xdb, 0x0d, 0x0d, 0xb1, 0x05, 0x15, 0x4f,
136 0x7c, 0x63, 0xd8, 0xb0, 0xe2, 0x6f, 0x31, 0x81,
137 0x41, 0xec, 0xec, 0xd1, 0x02, 0x45, 0x6b, 0xf6
139 test.algorithm = HASH_ALGORITHM_SHA_512;
140 TEST("SHA-512", test_compute_hash, IE_SUCCESS, &correct, &test, input,
141 sizeof(input));
143 SUM_TEST();