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
|
/*
* 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 "vircrypto.h"
#include "virrandom.h"
#include "testutils.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;
char *actual = NULL;
int ret = -1;
if (virCryptoHashString(data->hash, data->input, &actual) < 0) {
fprintf(stderr, "Failed to generate crypto hash\n");
goto cleanup;
}
if (STRNEQ_NULLABLE(data->output, actual)) {
fprintf(stderr, "Expected hash '%s' but got '%s'\n",
data->output, NULLSTR(actual));
goto cleanup;
}
ret = 0;
cleanup:
VIR_FREE(actual);
return ret;
}
struct testCryptoEncryptData {
virCryptoCipher algorithm;
uint8_t *input;
size_t inputlen;
uint8_t *ciphertext;
size_t ciphertextlen;
};
static int
testCryptoEncrypt(const void *opaque)
{
const struct testCryptoEncryptData *data = opaque;
uint8_t *enckey = NULL;
size_t enckeylen = 32;
uint8_t *iv = NULL;
size_t ivlen = 16;
uint8_t *ciphertext = NULL;
size_t ciphertextlen = 0;
int ret = -1;
if (!virCryptoHaveCipher(data->algorithm)) {
fprintf(stderr, "cipher algorithm=%d unavailable\n", data->algorithm);
return EXIT_AM_SKIP;
}
if (VIR_ALLOC_N(enckey, enckeylen) < 0 ||
VIR_ALLOC_N(iv, ivlen) < 0)
goto cleanup;
if (virRandomBytes(enckey, enckeylen) ||
virRandomBytes(iv, ivlen)) {
fprintf(stderr, "Failed to generate random bytes\n");
goto cleanup;
}
if (virCryptoEncryptData(data->algorithm, enckey, enckeylen, iv, ivlen,
data->input, data->inputlen,
&ciphertext, &ciphertextlen) < 0)
goto cleanup;
if (data->ciphertextlen != ciphertextlen) {
fprintf(stderr, "Expected ciphertextlen(%zu) doesn't match (%zu)\n",
data->ciphertextlen, ciphertextlen);
goto cleanup;
}
if (memcmp(data->ciphertext, ciphertext, ciphertextlen)) {
fprintf(stderr, "Expected ciphertext doesn't match\n");
goto cleanup;
}
ret = 0;
cleanup:
VIR_FREE(enckey);
VIR_FREE(iv);
VIR_FREE(ciphertext);
return ret;
}
static int
mymain(void)
{
int ret = 0;
uint8_t secretdata[8];
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)
memset(&secretdata, 0, 8);
memcpy(&secretdata, "letmein", 7);
VIR_CRYPTO_ENCRYPT(VIR_CRYPTO_CIPHER_AES256CBC, "aes265cbc",
secretdata, 7, expected_ciphertext, 16);
#undef VIR_CRYPTO_ENCRYPT
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
/* Forces usage of not so random virRandomBytes */
VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virrandommock.so")
|