Merge remote-tracking branch 'remotes/amit-migration/tags/migration-for-2.7-5' into...
[qemu/kevin.git] / tests / test-crypto-hash.c
blob6e0e89f7d62ef33b5174aee2c2a28eb373350a57
1 /*
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/>.
21 #include "qemu/osdep.h"
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)
61 size_t i;
63 g_assert(qcrypto_init(NULL) == 0);
65 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
66 uint8_t *result = NULL;
67 size_t resultlen = 0;
68 int ret;
69 size_t j;
71 ret = qcrypto_hash_bytes(i,
72 INPUT_TEXT,
73 strlen(INPUT_TEXT),
74 &result,
75 &resultlen,
76 NULL);
77 g_assert(ret == 0);
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]);
84 g_free(result);
88 /* Test with caller preallocating */
89 static void test_hash_prealloc(void)
91 size_t i;
93 g_assert(qcrypto_init(NULL) == 0);
95 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
96 uint8_t *result;
97 size_t resultlen;
98 int ret;
99 size_t j;
101 resultlen = expected_lens[i];
102 result = g_new0(uint8_t, resultlen);
104 ret = qcrypto_hash_bytes(i,
105 INPUT_TEXT,
106 strlen(INPUT_TEXT),
107 &result,
108 &resultlen,
109 NULL);
110 g_assert(ret == 0);
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]);
117 g_free(result);
122 /* Test with dynamic allocation */
123 static void test_hash_iov(void)
125 size_t i;
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;
137 int ret;
138 size_t j;
140 ret = qcrypto_hash_bytesv(i,
141 iov, 3,
142 &result,
143 &resultlen,
144 NULL);
145 g_assert(ret == 0);
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]);
151 g_free(result);
156 /* Test with printable hashing */
157 static void test_hash_digest(void)
159 size_t i;
161 g_assert(qcrypto_init(NULL) == 0);
163 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
164 int ret;
165 char *digest;
166 size_t digestsize;
168 digestsize = qcrypto_hash_digest_len(i);
170 g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[i]));
172 ret = qcrypto_hash_digest(i,
173 INPUT_TEXT,
174 strlen(INPUT_TEXT),
175 &digest,
176 NULL);
177 g_assert(ret == 0);
178 g_assert(g_str_equal(digest, expected_outputs[i]));
179 g_free(digest);
183 /* Test with base64 encoding */
184 static void test_hash_base64(void)
186 size_t i;
188 g_assert(qcrypto_init(NULL) == 0);
190 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
191 int ret;
192 char *digest;
194 ret = qcrypto_hash_base64(i,
195 INPUT_TEXT,
196 strlen(INPUT_TEXT),
197 &digest,
198 NULL);
199 g_assert(ret == 0);
200 g_assert(g_str_equal(digest, expected_outputs_b64[i]));
201 g_free(digest);
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);
213 return g_test_run();