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"
24 #include "crypto/init.h"
25 #include "crypto/hash.h"
27 #define INPUT_TEXT "Hiss hisss Hissss hiss Hiss hisss Hiss hiss"
28 #define INPUT_TEXT1 "Hiss hisss "
29 #define INPUT_TEXT2 "Hissss hiss "
30 #define INPUT_TEXT3 "Hiss hisss Hiss hiss"
32 #define OUTPUT_MD5 "628d206371563035ab8ef62f492bdec9"
33 #define OUTPUT_SHA1 "b2e74f26758a3a421e509cee045244b78753cc02"
34 #define OUTPUT_SHA256 "bc757abb0436586f392b437e5dd24096" \
35 "f7f224de6b74d4d86e2abc6121b160d0"
37 #define OUTPUT_MD5_B64 "Yo0gY3FWMDWrjvYvSSveyQ=="
38 #define OUTPUT_SHA1_B64 "sudPJnWKOkIeUJzuBFJEt4dTzAI="
39 #define OUTPUT_SHA256_B64 "vHV6uwQ2WG85K0N+XdJAlvfyJN5rdNTYbiq8YSGxYNA="
41 static const char *expected_outputs
[] = {
42 [QCRYPTO_HASH_ALG_MD5
] = OUTPUT_MD5
,
43 [QCRYPTO_HASH_ALG_SHA1
] = OUTPUT_SHA1
,
44 [QCRYPTO_HASH_ALG_SHA256
] = OUTPUT_SHA256
,
46 static const char *expected_outputs_b64
[] = {
47 [QCRYPTO_HASH_ALG_MD5
] = OUTPUT_MD5_B64
,
48 [QCRYPTO_HASH_ALG_SHA1
] = OUTPUT_SHA1_B64
,
49 [QCRYPTO_HASH_ALG_SHA256
] = OUTPUT_SHA256_B64
,
51 static const int expected_lens
[] = {
52 [QCRYPTO_HASH_ALG_MD5
] = 16,
53 [QCRYPTO_HASH_ALG_SHA1
] = 20,
54 [QCRYPTO_HASH_ALG_SHA256
] = 32,
57 static const char hex
[] = "0123456789abcdef";
59 /* Test with dynamic allocation */
60 static void test_hash_alloc(void)
64 g_assert(qcrypto_init(NULL
) == 0);
66 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
67 uint8_t *result
= NULL
;
72 ret
= qcrypto_hash_bytes(i
,
79 g_assert(resultlen
== expected_lens
[i
]);
81 for (j
= 0; j
< resultlen
; j
++) {
82 g_assert(expected_outputs
[i
][j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
83 g_assert(expected_outputs
[i
][j
* 2 + 1] == hex
[result
[j
] & 0xf]);
89 /* Test with caller preallocating */
90 static void test_hash_prealloc(void)
94 g_assert(qcrypto_init(NULL
) == 0);
96 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
102 resultlen
= expected_lens
[i
];
103 result
= g_new0(uint8_t, resultlen
);
105 ret
= qcrypto_hash_bytes(i
,
113 g_assert(resultlen
== expected_lens
[i
]);
114 for (j
= 0; j
< resultlen
; j
++) {
115 g_assert(expected_outputs
[i
][j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
116 g_assert(expected_outputs
[i
][j
* 2 + 1] == hex
[result
[j
] & 0xf]);
123 /* Test with dynamic allocation */
124 static void test_hash_iov(void)
128 g_assert(qcrypto_init(NULL
) == 0);
130 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
131 struct iovec iov
[3] = {
132 { .iov_base
= (char *)INPUT_TEXT1
, .iov_len
= strlen(INPUT_TEXT1
) },
133 { .iov_base
= (char *)INPUT_TEXT2
, .iov_len
= strlen(INPUT_TEXT2
) },
134 { .iov_base
= (char *)INPUT_TEXT3
, .iov_len
= strlen(INPUT_TEXT3
) },
136 uint8_t *result
= NULL
;
137 size_t resultlen
= 0;
141 ret
= qcrypto_hash_bytesv(i
,
147 g_assert(resultlen
== expected_lens
[i
]);
148 for (j
= 0; j
< resultlen
; j
++) {
149 g_assert(expected_outputs
[i
][j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
150 g_assert(expected_outputs
[i
][j
* 2 + 1] == hex
[result
[j
] & 0xf]);
157 /* Test with printable hashing */
158 static void test_hash_digest(void)
162 g_assert(qcrypto_init(NULL
) == 0);
164 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
169 digestsize
= qcrypto_hash_digest_len(i
);
171 g_assert_cmpint(digestsize
* 2, ==, strlen(expected_outputs
[i
]));
173 ret
= qcrypto_hash_digest(i
,
179 g_assert(g_str_equal(digest
, expected_outputs
[i
]));
184 /* Test with base64 encoding */
185 static void test_hash_base64(void)
189 g_assert(qcrypto_init(NULL
) == 0);
191 for (i
= 0; i
< G_N_ELEMENTS(expected_outputs
) ; i
++) {
195 ret
= qcrypto_hash_base64(i
,
201 g_assert(g_str_equal(digest
, expected_outputs_b64
[i
]));
206 int main(int argc
, char **argv
)
208 g_test_init(&argc
, &argv
, NULL
);
209 g_test_add_func("/crypto/hash/iov", test_hash_iov
);
210 g_test_add_func("/crypto/hash/alloc", test_hash_alloc
);
211 g_test_add_func("/crypto/hash/prealloc", test_hash_prealloc
);
212 g_test_add_func("/crypto/hash/digest", test_hash_digest
);
213 g_test_add_func("/crypto/hash/base64", test_hash_base64
);