esp: check dma length before reading scsi command(CVE-2016-4441)
[qemu/ar7.git] / tests / test-crypto-hash.c
blob735d6d7e0b7510ee8e54adddbcc4a09e730339cc
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"
22 #include <glib.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)
62 size_t i;
64 g_assert(qcrypto_init(NULL) == 0);
66 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
67 uint8_t *result = NULL;
68 size_t resultlen = 0;
69 int ret;
70 size_t j;
72 ret = qcrypto_hash_bytes(i,
73 INPUT_TEXT,
74 strlen(INPUT_TEXT),
75 &result,
76 &resultlen,
77 NULL);
78 g_assert(ret == 0);
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]);
85 g_free(result);
89 /* Test with caller preallocating */
90 static void test_hash_prealloc(void)
92 size_t i;
94 g_assert(qcrypto_init(NULL) == 0);
96 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
97 uint8_t *result;
98 size_t resultlen;
99 int ret;
100 size_t j;
102 resultlen = expected_lens[i];
103 result = g_new0(uint8_t, resultlen);
105 ret = qcrypto_hash_bytes(i,
106 INPUT_TEXT,
107 strlen(INPUT_TEXT),
108 &result,
109 &resultlen,
110 NULL);
111 g_assert(ret == 0);
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]);
118 g_free(result);
123 /* Test with dynamic allocation */
124 static void test_hash_iov(void)
126 size_t i;
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;
138 int ret;
139 size_t j;
141 ret = qcrypto_hash_bytesv(i,
142 iov, 3,
143 &result,
144 &resultlen,
145 NULL);
146 g_assert(ret == 0);
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]);
152 g_free(result);
157 /* Test with printable hashing */
158 static void test_hash_digest(void)
160 size_t i;
162 g_assert(qcrypto_init(NULL) == 0);
164 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
165 int ret;
166 char *digest;
167 size_t digestsize;
169 digestsize = qcrypto_hash_digest_len(i);
171 g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[i]));
173 ret = qcrypto_hash_digest(i,
174 INPUT_TEXT,
175 strlen(INPUT_TEXT),
176 &digest,
177 NULL);
178 g_assert(ret == 0);
179 g_assert(g_str_equal(digest, expected_outputs[i]));
180 g_free(digest);
184 /* Test with base64 encoding */
185 static void test_hash_base64(void)
187 size_t i;
189 g_assert(qcrypto_init(NULL) == 0);
191 for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
192 int ret;
193 char *digest;
195 ret = qcrypto_hash_base64(i,
196 INPUT_TEXT,
197 strlen(INPUT_TEXT),
198 &digest,
199 NULL);
200 g_assert(ret == 0);
201 g_assert(g_str_equal(digest, expected_outputs_b64[i]));
202 g_free(digest);
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);
214 return g_test_run();