2 * QEMU Crypto hmac algorithms tests
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
7 * Longpeng(Mike) <longpeng2@huawei.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
15 #include "qemu/osdep.h"
16 #include "crypto/init.h"
17 #include "crypto/hmac.h"
19 #define INPUT_TEXT1 "ABCDEFGHIJKLMNOPQRSTUVWXY"
20 #define INPUT_TEXT2 "Zabcdefghijklmnopqrstuvwx"
21 #define INPUT_TEXT3 "yz0123456789"
22 #define INPUT_TEXT INPUT_TEXT1 \
26 #define KEY "monkey monkey monkey monkey"
28 typedef struct QCryptoHmacTestData QCryptoHmacTestData
;
29 struct QCryptoHmacTestData
{
30 QCryptoHashAlgorithm alg
;
31 const char *hex_digest
;
34 static QCryptoHmacTestData test_data
[] = {
36 .alg
= QCRYPTO_HASH_ALG_MD5
,
38 "ede9cb83679ba82d88fbeae865b3f8fc",
41 .alg
= QCRYPTO_HASH_ALG_SHA1
,
43 "c7b5a631e3aac975c4ededfcd346e469"
47 .alg
= QCRYPTO_HASH_ALG_SHA224
,
49 "5f768179dbb29ca722875d0f461a2e2f"
50 "597d0210340a84df1a8e9c63",
53 .alg
= QCRYPTO_HASH_ALG_SHA256
,
55 "3798f363c57afa6edaffe39016ca7bad"
56 "efd1e670afb0e3987194307dec3197db",
59 .alg
= QCRYPTO_HASH_ALG_SHA384
,
61 "d218680a6032d33dccd9882d6a6a7164"
62 "64f26623be257a9b2919b185294f4a49"
63 "9e54b190bfd6bc5cedd2cd05c7e65e82",
66 .alg
= QCRYPTO_HASH_ALG_SHA512
,
68 "835a4f5b3750b4c1fccfa88da2f746a4"
69 "900160c9f18964309bb736c13b59491b"
70 "8e32d37b724cc5aebb0f554c6338a3b5"
71 "94c4ba26862b2dadb59b7ede1d08d53e",
74 .alg
= QCRYPTO_HASH_ALG_RIPEMD160
,
76 "94964ed4c1155b62b668c241d67279e5"
81 static const char hex
[] = "0123456789abcdef";
83 static void test_hmac_alloc(void)
87 for (i
= 0; i
< G_N_ELEMENTS(test_data
); i
++) {
88 QCryptoHmacTestData
*data
= &test_data
[i
];
89 QCryptoHmac
*hmac
= NULL
;
90 uint8_t *result
= NULL
;
93 const char *exp_output
= NULL
;
97 if (!qcrypto_hmac_supports(data
->alg
)) {
101 exp_output
= data
->hex_digest
;
103 hmac
= qcrypto_hmac_new(data
->alg
, (const uint8_t *)KEY
,
105 g_assert(err
== NULL
);
106 g_assert(hmac
!= NULL
);
108 ret
= qcrypto_hmac_bytes(hmac
, (const char *)INPUT_TEXT
,
109 strlen(INPUT_TEXT
), &result
,
111 g_assert(err
== NULL
);
114 for (j
= 0; j
< resultlen
; j
++) {
115 g_assert(exp_output
[j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
116 g_assert(exp_output
[j
* 2 + 1] == hex
[result
[j
] & 0xf]);
119 qcrypto_hmac_free(hmac
);
125 static void test_hmac_prealloc(void)
129 for (i
= 0; i
< G_N_ELEMENTS(test_data
); i
++) {
130 QCryptoHmacTestData
*data
= &test_data
[i
];
131 QCryptoHmac
*hmac
= NULL
;
132 uint8_t *result
= NULL
;
133 size_t resultlen
= 0;
135 const char *exp_output
= NULL
;
139 if (!qcrypto_hmac_supports(data
->alg
)) {
143 exp_output
= data
->hex_digest
;
145 resultlen
= strlen(exp_output
) / 2;
146 result
= g_new0(uint8_t, resultlen
);
148 hmac
= qcrypto_hmac_new(data
->alg
, (const uint8_t *)KEY
,
150 g_assert(err
== NULL
);
151 g_assert(hmac
!= NULL
);
153 ret
= qcrypto_hmac_bytes(hmac
, (const char *)INPUT_TEXT
,
154 strlen(INPUT_TEXT
), &result
,
156 g_assert(err
== NULL
);
159 exp_output
= data
->hex_digest
;
160 for (j
= 0; j
< resultlen
; j
++) {
161 g_assert(exp_output
[j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
162 g_assert(exp_output
[j
* 2 + 1] == hex
[result
[j
] & 0xf]);
165 qcrypto_hmac_free(hmac
);
171 static void test_hmac_iov(void)
175 for (i
= 0; i
< G_N_ELEMENTS(test_data
); i
++) {
176 QCryptoHmacTestData
*data
= &test_data
[i
];
177 QCryptoHmac
*hmac
= NULL
;
178 uint8_t *result
= NULL
;
179 size_t resultlen
= 0;
181 const char *exp_output
= NULL
;
184 struct iovec iov
[3] = {
185 { .iov_base
= (char *)INPUT_TEXT1
, .iov_len
= strlen(INPUT_TEXT1
) },
186 { .iov_base
= (char *)INPUT_TEXT2
, .iov_len
= strlen(INPUT_TEXT2
) },
187 { .iov_base
= (char *)INPUT_TEXT3
, .iov_len
= strlen(INPUT_TEXT3
) },
190 if (!qcrypto_hmac_supports(data
->alg
)) {
194 exp_output
= data
->hex_digest
;
196 hmac
= qcrypto_hmac_new(data
->alg
, (const uint8_t *)KEY
,
198 g_assert(err
== NULL
);
199 g_assert(hmac
!= NULL
);
201 ret
= qcrypto_hmac_bytesv(hmac
, iov
, 3, &result
,
203 g_assert(err
== NULL
);
206 for (j
= 0; j
< resultlen
; j
++) {
207 g_assert(exp_output
[j
* 2] == hex
[(result
[j
] >> 4) & 0xf]);
208 g_assert(exp_output
[j
* 2 + 1] == hex
[result
[j
] & 0xf]);
211 qcrypto_hmac_free(hmac
);
217 static void test_hmac_digest(void)
221 for (i
= 0; i
< G_N_ELEMENTS(test_data
); i
++) {
222 QCryptoHmacTestData
*data
= &test_data
[i
];
223 QCryptoHmac
*hmac
= NULL
;
224 uint8_t *result
= NULL
;
226 const char *exp_output
= NULL
;
229 if (!qcrypto_hmac_supports(data
->alg
)) {
233 exp_output
= data
->hex_digest
;
235 hmac
= qcrypto_hmac_new(data
->alg
, (const uint8_t *)KEY
,
237 g_assert(err
== NULL
);
238 g_assert(hmac
!= NULL
);
240 ret
= qcrypto_hmac_digest(hmac
, (const char *)INPUT_TEXT
,
241 strlen(INPUT_TEXT
), (char **)&result
,
243 g_assert(err
== NULL
);
246 g_assert_cmpstr((const char *)result
, ==, exp_output
);
248 qcrypto_hmac_free(hmac
);
254 int main(int argc
, char **argv
)
256 g_test_init(&argc
, &argv
, NULL
);
258 g_assert(qcrypto_init(NULL
) == 0);
260 g_test_add_func("/crypto/hmac/iov", test_hmac_iov
);
261 g_test_add_func("/crypto/hmac/alloc", test_hmac_alloc
);
262 g_test_add_func("/crypto/hmac/prealloc", test_hmac_prealloc
);
263 g_test_add_func("/crypto/hmac/digest", test_hmac_digest
);