2 * vircryptotest.c: cryptographic helper test suite
4 * Copyright (C) 2014 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.1 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
18 * <http://www.gnu.org/licenses/>.
23 #include "testutils.h"
25 #include "vircrypto.h"
26 #include "virrandom.h"
28 #define VIR_FROM_THIS VIR_FROM_NONE
30 struct testCryptoHashData
{
37 testCryptoHash(const void *opaque
)
39 const struct testCryptoHashData
*data
= opaque
;
43 if (virCryptoHashString(data
->hash
, data
->input
, &actual
) < 0) {
44 fprintf(stderr
, "Failed to generate crypto hash\n");
48 if (STRNEQ_NULLABLE(data
->output
, actual
)) {
49 fprintf(stderr
, "Expected hash '%s' but got '%s'\n",
50 data
->output
, NULLSTR(actual
));
61 struct testCryptoEncryptData
{
62 virCryptoCipher algorithm
;
70 testCryptoEncrypt(const void *opaque
)
72 const struct testCryptoEncryptData
*data
= opaque
;
73 uint8_t *enckey
= NULL
;
74 size_t enckeylen
= 32;
77 uint8_t *ciphertext
= NULL
;
78 size_t ciphertextlen
= 0;
81 if (!virCryptoHaveCipher(data
->algorithm
)) {
82 fprintf(stderr
, "cipher algorithm=%d unavailable\n", data
->algorithm
);
86 if (VIR_ALLOC_N(enckey
, enckeylen
) < 0 ||
87 VIR_ALLOC_N(iv
, ivlen
) < 0)
90 if (virRandomBytes(enckey
, enckeylen
) < 0 ||
91 virRandomBytes(iv
, ivlen
) < 0) {
92 fprintf(stderr
, "Failed to generate random bytes\n");
96 if (virCryptoEncryptData(data
->algorithm
, enckey
, enckeylen
, iv
, ivlen
,
97 data
->input
, data
->inputlen
,
98 &ciphertext
, &ciphertextlen
) < 0)
101 if (data
->ciphertextlen
!= ciphertextlen
) {
102 fprintf(stderr
, "Expected ciphertextlen(%zu) doesn't match (%zu)\n",
103 data
->ciphertextlen
, ciphertextlen
);
107 if (memcmp(data
->ciphertext
, ciphertext
, ciphertextlen
)) {
108 fprintf(stderr
, "Expected ciphertext doesn't match\n");
116 VIR_FREE(ciphertext
);
126 uint8_t secretdata
[8];
127 uint8_t expected_ciphertext
[16] = {0x48, 0x8e, 0x9, 0xb9,
128 0x6a, 0xa6, 0x24, 0x5f,
129 0x1b, 0x8c, 0x3f, 0x48,
130 0x27, 0xae, 0xb6, 0x7a};
132 #define VIR_CRYPTO_HASH(h, i, o) \
134 struct testCryptoHashData data = { \
139 if (virTestRun("Hash " i, testCryptoHash, &data) < 0) \
143 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5
, "", "d41d8cd98f00b204e9800998ecf8427e");
144 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256
, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
146 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5
, " ", "7215ee9c7d9dc229d2921a40e899ec5f");
147 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256
, " ", "36a9e7f1c95b82ffb99743e0c5c4ce95d83c9a430aac59f84ef3cbfab6145068");
149 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5
, "\n", "68b329da9893e34099c7d8ad5cb9c940");
150 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256
, "\n", "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b");
152 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5
, "The quick brown fox", "a2004f37730b9445670a738fa0fc9ee5");
153 VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256
, "The quick brown fox", "5cac4f980fedc3d3f1f99b4be3472c9b30d56523e632d151237ec9309048bda9");
155 #undef VIR_CRYPTO_HASH
157 #define VIR_CRYPTO_ENCRYPT(a, n, i, il, c, cl) \
159 struct testCryptoEncryptData data = { \
164 .ciphertextlen = cl, \
166 if (virTestRun("Encrypt " n, testCryptoEncrypt, &data) < 0) \
170 memset(&secretdata
, 0, 8);
171 memcpy(&secretdata
, "letmein", 7);
173 VIR_CRYPTO_ENCRYPT(VIR_CRYPTO_CIPHER_AES256CBC
, "aes265cbc",
174 secretdata
, 7, expected_ciphertext
, 16);
176 #undef VIR_CRYPTO_ENCRYPT
178 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
181 /* Forces usage of not so random virRandomBytes */
182 VIR_TEST_MAIN_PRELOAD(mymain
, abs_builddir
"/.libs/virrandommock.so")