backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / vircryptotest.c
blob6841d74901ceb628dd669b9d7bd3242b954c133e
1 /*
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/>.
21 #include <config.h>
23 #include "testutils.h"
25 #include "vircrypto.h"
26 #include "virrandom.h"
28 #define VIR_FROM_THIS VIR_FROM_NONE
30 struct testCryptoHashData {
31 virCryptoHash hash;
32 const char *input;
33 const char *output;
36 static int
37 testCryptoHash(const void *opaque)
39 const struct testCryptoHashData *data = opaque;
40 char *actual = NULL;
41 int ret = -1;
43 if (virCryptoHashString(data->hash, data->input, &actual) < 0) {
44 fprintf(stderr, "Failed to generate crypto hash\n");
45 goto cleanup;
48 if (STRNEQ_NULLABLE(data->output, actual)) {
49 fprintf(stderr, "Expected hash '%s' but got '%s'\n",
50 data->output, NULLSTR(actual));
51 goto cleanup;
54 ret = 0;
55 cleanup:
56 VIR_FREE(actual);
57 return ret;
61 struct testCryptoEncryptData {
62 virCryptoCipher algorithm;
63 uint8_t *input;
64 size_t inputlen;
65 uint8_t *ciphertext;
66 size_t ciphertextlen;
69 static int
70 testCryptoEncrypt(const void *opaque)
72 const struct testCryptoEncryptData *data = opaque;
73 uint8_t *enckey = NULL;
74 size_t enckeylen = 32;
75 uint8_t *iv = NULL;
76 size_t ivlen = 16;
77 uint8_t *ciphertext = NULL;
78 size_t ciphertextlen = 0;
79 int ret = -1;
81 if (!virCryptoHaveCipher(data->algorithm)) {
82 fprintf(stderr, "cipher algorithm=%d unavailable\n", data->algorithm);
83 return EXIT_AM_SKIP;
86 if (VIR_ALLOC_N(enckey, enckeylen) < 0 ||
87 VIR_ALLOC_N(iv, ivlen) < 0)
88 goto cleanup;
90 if (virRandomBytes(enckey, enckeylen) < 0 ||
91 virRandomBytes(iv, ivlen) < 0) {
92 fprintf(stderr, "Failed to generate random bytes\n");
93 goto cleanup;
96 if (virCryptoEncryptData(data->algorithm, enckey, enckeylen, iv, ivlen,
97 data->input, data->inputlen,
98 &ciphertext, &ciphertextlen) < 0)
99 goto cleanup;
101 if (data->ciphertextlen != ciphertextlen) {
102 fprintf(stderr, "Expected ciphertextlen(%zu) doesn't match (%zu)\n",
103 data->ciphertextlen, ciphertextlen);
104 goto cleanup;
107 if (memcmp(data->ciphertext, ciphertext, ciphertextlen)) {
108 fprintf(stderr, "Expected ciphertext doesn't match\n");
109 goto cleanup;
112 ret = 0;
113 cleanup:
114 VIR_FREE(enckey);
115 VIR_FREE(iv);
116 VIR_FREE(ciphertext);
118 return ret;
122 static int
123 mymain(void)
125 int ret = 0;
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) \
133 do { \
134 struct testCryptoHashData data = { \
135 .hash = h, \
136 .input = i, \
137 .output = o, \
138 }; \
139 if (virTestRun("Hash " i, testCryptoHash, &data) < 0) \
140 ret = -1; \
141 } while (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) \
158 do { \
159 struct testCryptoEncryptData data = { \
160 .algorithm = a, \
161 .input = i, \
162 .inputlen = il, \
163 .ciphertext = c, \
164 .ciphertextlen = cl, \
165 }; \
166 if (virTestRun("Encrypt " n, testCryptoEncrypt, &data) < 0) \
167 ret = -1; \
168 } while (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")