1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
* vircryptotest.c: cryptographic helper test suite
*
* Copyright (C) 2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "testutils.h"
#include "vircrypto.h"
#include "virrandom.h"
#define VIR_FROM_THIS VIR_FROM_NONE
struct testCryptoHashData {
virCryptoHash hash;
const char *input;
const char *output;
};
static int
testCryptoHash(const void *opaque)
{
const struct testCryptoHashData *data = opaque;
g_autofree char *actual = NULL;
if (virCryptoHashString(data->hash, data->input, &actual) < 0) {
fprintf(stderr, "Failed to generate crypto hash\n");
return -1;
}
if (STRNEQ_NULLABLE(data->output, actual)) {
fprintf(stderr, "Expected hash '%s' but got '%s'\n",
data->output, NULLSTR(actual));
return -1;
}
return 0;
}
struct testCryptoEncryptData {
virCryptoCipher algorithm;
uint8_t *input;
size_t inputlen;
uint8_t *ciphertext;
size_t ciphertextlen;
};
struct testCryptoDecryptData {
virCryptoCipher algorithm;
uint8_t *input;
size_t inputlen;
uint8_t *plaintext;
size_t plaintextlen;
};
static int
testCryptoEncrypt(const void *opaque)
{
const struct testCryptoEncryptData *data = opaque;
g_autofree uint8_t *enckey = NULL;
size_t enckeylen = 32;
g_autofree uint8_t *iv = NULL;
size_t ivlen = 16;
g_autofree uint8_t *ciphertext = NULL;
size_t ciphertextlen = 0;
enckey = g_new0(uint8_t, enckeylen);
iv = g_new0(uint8_t, ivlen);
if (virRandomBytes(enckey, enckeylen) < 0 ||
virRandomBytes(iv, ivlen) < 0) {
fprintf(stderr, "Failed to generate random bytes\n");
return -1;
}
if (virCryptoEncryptData(data->algorithm, enckey, enckeylen, iv, ivlen,
data->input, data->inputlen,
&ciphertext, &ciphertextlen) < 0)
return -1;
if (data->ciphertextlen != ciphertextlen) {
fprintf(stderr, "Expected ciphertextlen(%zu) doesn't match (%zu)\n",
data->ciphertextlen, ciphertextlen);
return -1;
}
if (memcmp(data->ciphertext, ciphertext, ciphertextlen)) {
fprintf(stderr, "Expected ciphertext doesn't match\n");
return -1;
}
return 0;
}
static int
testCryptoDecrypt(const void *opaque)
{
const struct testCryptoDecryptData *data = opaque;
g_autofree uint8_t *deckey = NULL;
size_t deckeylen = 32;
g_autofree uint8_t *iv = NULL;
size_t ivlen = 16;
g_autofree uint8_t *plaintext = NULL;
size_t plaintextlen = 0;
deckey = g_new0(uint8_t, deckeylen);
iv = g_new0(uint8_t, ivlen);
if (virRandomBytes(deckey, deckeylen) < 0 ||
virRandomBytes(iv, ivlen) < 0) {
fprintf(stderr, "Failed to generate random bytes\n");
return -1;
}
if (virCryptoDecryptData(data->algorithm, deckey, deckeylen, iv, ivlen,
data->input, data->inputlen,
&plaintext, &plaintextlen) < 0)
return -1;
if (data->plaintextlen != plaintextlen) {
fprintf(stderr, "Expected plaintexlen(%zu) doesn't match (%zu)\n",
data->plaintextlen, plaintextlen);
return -1;
}
if (memcmp(data->plaintext, plaintext, plaintextlen)) {
fprintf(stderr, "Expected plaintext doesn't match\n");
return -1;
}
return 0;
}
static int
mymain(void)
{
int ret = 0;
uint8_t secretdata[8] = "letmein";
uint8_t expected_ciphertext[16] = {0x48, 0x8e, 0x9, 0xb9,
0x6a, 0xa6, 0x24, 0x5f,
0x1b, 0x8c, 0x3f, 0x48,
0x27, 0xae, 0xb6, 0x7a};
#define VIR_CRYPTO_HASH(h, i, o) \
do { \
struct testCryptoHashData data = { \
.hash = h, \
.input = i, \
.output = o, \
}; \
if (virTestRun("Hash " i, testCryptoHash, &data) < 0) \
ret = -1; \
} while (0)
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, "", "d41d8cd98f00b204e9800998ecf8427e");
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, " ", "7215ee9c7d9dc229d2921a40e899ec5f");
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, " ", "36a9e7f1c95b82ffb99743e0c5c4ce95d83c9a430aac59f84ef3cbfab6145068");
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, "\n", "68b329da9893e34099c7d8ad5cb9c940");
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, "\n", "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b");
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_MD5, "The quick brown fox", "a2004f37730b9445670a738fa0fc9ee5");
VIR_CRYPTO_HASH(VIR_CRYPTO_HASH_SHA256, "The quick brown fox", "5cac4f980fedc3d3f1f99b4be3472c9b30d56523e632d151237ec9309048bda9");
#undef VIR_CRYPTO_HASH
#define VIR_CRYPTO_ENCRYPT(a, n, i, il, c, cl) \
do { \
struct testCryptoEncryptData data = { \
.algorithm = a, \
.input = i, \
.inputlen = il, \
.ciphertext = c, \
.ciphertextlen = cl, \
}; \
if (virTestRun("Encrypt " n, testCryptoEncrypt, &data) < 0) \
ret = -1; \
} while (0)
VIR_CRYPTO_ENCRYPT(VIR_CRYPTO_CIPHER_AES256CBC, "aes256cbc",
secretdata, 7, expected_ciphertext, 16);
#undef VIR_CRYPTO_ENCRYPT
#define VIR_CRYPTO_DECRYPT(a, n, i, il, c, cl) \
do { \
struct testCryptoDecryptData data = { \
.algorithm = a, \
.input = i, \
.inputlen = il, \
.plaintext = c, \
.plaintextlen = cl, \
}; \
if (virTestRun("Decrypt " n, testCryptoDecrypt, &data) < 0) \
ret = -1; \
} while (0)
VIR_CRYPTO_DECRYPT(VIR_CRYPTO_CIPHER_AES256CBC, "aes256cbc",
expected_ciphertext, 16, secretdata, 7);
#undef VIR_CRYPTO_DECRYPT
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
/* Forces usage of not so random virRandomBytes */
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virrandom"))
|