2 * QEMU Crypto hash algorithms
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "crypto/init.h"
24 #include "crypto/hash.h"
26 #define INPUT_TEXT "Hiss hisss Hissss hiss Hiss hisss Hiss hiss"
27 #define INPUT_TEXT1 "Hiss hisss "
28 #define INPUT_TEXT2 "Hissss hiss "
29 #define INPUT_TEXT3 "Hiss hisss Hiss hiss"
31 #define OUTPUT_MD5 "628d206371563035ab8ef62f492bdec9"
32 #define OUTPUT_SHA1 "b2e74f26758a3a421e509cee045244b78753cc02"
33 #define OUTPUT_SHA256 "bc757abb0436586f392b437e5dd24096" \
34 "f7f224de6b74d4d86e2abc6121b160d0"
36 #define OUTPUT_MD5_B64 "Yo0gY3FWMDWrjvYvSSveyQ=="
37 #define OUTPUT_SHA1_B64 "sudPJnWKOkIeUJzuBFJEt4dTzAI="
38 #define OUTPUT_SHA256_B64 "vHV6uwQ2WG85K0N+XdJAlvfyJN5rdNTYbiq8YSGxYNA="
40 static const char *expected_outputs
[] = {
41 [QCRYPTO_HASH_ALG_MD5
] = OUTPUT_MD5
,
42 [QCRYPTO_HASH_ALG_SHA1
] = OUTPUT_SHA1
,
43 [QCRYPTO_HASH_ALG_SHA256
] = OUTPUT_SHA256
,
45 static const char *expected_outputs_b64
[] = {
46 [QCRYPTO_HASH_ALG_MD5
] = OUTPUT_MD5_B64
,
47 [QCRYPTO_HASH_ALG_SHA1
] = OUTPUT_SHA1_B64
,
48 [QCRYPTO_HASH_ALG_SHA256
] = OUTPUT_SHA256_B64
,
50 static const int expected_lens
[] = {
51 [QCRYPTO_HASH_ALG_MD5
] = 16,
52 [QCRYPTO_HASH_ALG_SHA1
] = 20,
53 [QCRYPTO_HASH_ALG_SHA256
] = 32,
56 static const char hex
[] = "0123456789abcdef";
58 /* Test with dynamic allocation */
59 static void test_hash_alloc(void)
63 g_assert(qcrypto_init(NULL
) == 0);
65 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
66 uint8_t *result
= NULL
;
71 ret
= qcrypto_hash_bytes(i
,
78 g_assert(resultlen
== expected_lens
[i
]);
80 for (j
= 0; j
< resultlen
; j
++) {
81 g_assert(expected_outputs
[i
][j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
82 g_assert(expected_outputs
[i
][j
* 2 + 1] == hex
[result
[j
] & 0xf]);
88 /* Test with caller preallocating */
89 static void test_hash_prealloc(void)
93 g_assert(qcrypto_init(NULL
) == 0);
95 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
101 resultlen
= expected_lens
[i
];
102 result
= g_new0(uint8_t, resultlen
);
104 ret
= qcrypto_hash_bytes(i
,
112 g_assert(resultlen
== expected_lens
[i
]);
113 for (j
= 0; j
< resultlen
; j
++) {
114 g_assert(expected_outputs
[i
][j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
115 g_assert(expected_outputs
[i
][j
* 2 + 1] == hex
[result
[j
] & 0xf]);
122 /* Test with dynamic allocation */
123 static void test_hash_iov(void)
127 g_assert(qcrypto_init(NULL
) == 0);
129 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
130 struct iovec iov
[3] = {
131 { .iov_base
= (char *)INPUT_TEXT1
, .iov_len
= strlen(INPUT_TEXT1
) },
132 { .iov_base
= (char *)INPUT_TEXT2
, .iov_len
= strlen(INPUT_TEXT2
) },
133 { .iov_base
= (char *)INPUT_TEXT3
, .iov_len
= strlen(INPUT_TEXT3
) },
135 uint8_t *result
= NULL
;
136 size_t resultlen
= 0;
140 ret
= qcrypto_hash_bytesv(i
,
146 g_assert(resultlen
== expected_lens
[i
]);
147 for (j
= 0; j
< resultlen
; j
++) {
148 g_assert(expected_outputs
[i
][j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
149 g_assert(expected_outputs
[i
][j
* 2 + 1] == hex
[result
[j
] & 0xf]);
156 /* Test with printable hashing */
157 static void test_hash_digest(void)
161 g_assert(qcrypto_init(NULL
) == 0);
163 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
168 digestsize
= qcrypto_hash_digest_len(i
);
170 g_assert_cmpint(digestsize
* 2, ==, strlen(expected_outputs
[i
]));
172 ret
= qcrypto_hash_digest(i
,
178 g_assert(g_str_equal(digest
, expected_outputs
[i
]));
183 /* Test with base64 encoding */
184 static void test_hash_base64(void)
188 g_assert(qcrypto_init(NULL
) == 0);
190 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
194 ret
= qcrypto_hash_base64(i
,
200 g_assert(g_str_equal(digest
, expected_outputs_b64
[i
]));
205 int main(int argc
, char **argv
)
207 g_test_init(&argc
, &argv
, NULL
);
208 g_test_add_func("/crypto/hash/iov", test_hash_iov
);
209 g_test_add_func("/crypto/hash/alloc", test_hash_alloc
);
210 g_test_add_func("/crypto/hash/prealloc", test_hash_prealloc
);
211 g_test_add_func("/crypto/hash/digest", test_hash_digest
);
212 g_test_add_func("/crypto/hash/base64", test_hash_base64
);