test-crypto-block.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * QEMU Crypto block encryption
  3. *
  4. * Copyright (c) 2016 Red Hat, Inc.
  5. *
  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 of the License, or (at your option) any later version.
  10. *
  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.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "crypto/init.h"
  23. #include "crypto/block.h"
  24. #include "qemu/buffer.h"
  25. #include "qemu/module.h"
  26. #include "crypto/secret.h"
  27. #ifndef _WIN32
  28. #include <sys/resource.h>
  29. #endif
  30. #if (defined(_WIN32) || defined RUSAGE_THREAD) && \
  31. (defined(CONFIG_NETTLE) || defined(CONFIG_GCRYPT))
  32. #define TEST_LUKS
  33. #else
  34. #undef TEST_LUKS
  35. #endif
  36. static QCryptoBlockCreateOptions qcow_create_opts = {
  37. .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
  38. .u.qcow = {
  39. .has_key_secret = true,
  40. .key_secret = (char *)"sec0",
  41. },
  42. };
  43. static QCryptoBlockOpenOptions qcow_open_opts = {
  44. .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
  45. .u.qcow = {
  46. .has_key_secret = true,
  47. .key_secret = (char *)"sec0",
  48. },
  49. };
  50. #ifdef TEST_LUKS
  51. static QCryptoBlockOpenOptions luks_open_opts = {
  52. .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
  53. .u.luks = {
  54. .has_key_secret = true,
  55. .key_secret = (char *)"sec0",
  56. },
  57. };
  58. /* Creation with all default values */
  59. static QCryptoBlockCreateOptions luks_create_opts_default = {
  60. .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
  61. .u.luks = {
  62. .has_key_secret = true,
  63. .key_secret = (char *)"sec0",
  64. },
  65. };
  66. /* ...and with explicit values */
  67. static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_plain64 = {
  68. .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
  69. .u.luks = {
  70. .has_key_secret = true,
  71. .key_secret = (char *)"sec0",
  72. .has_cipher_alg = true,
  73. .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
  74. .has_cipher_mode = true,
  75. .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
  76. .has_ivgen_alg = true,
  77. .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
  78. },
  79. };
  80. static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_essiv = {
  81. .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
  82. .u.luks = {
  83. .has_key_secret = true,
  84. .key_secret = (char *)"sec0",
  85. .has_cipher_alg = true,
  86. .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
  87. .has_cipher_mode = true,
  88. .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
  89. .has_ivgen_alg = true,
  90. .ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
  91. .has_ivgen_hash_alg = true,
  92. .ivgen_hash_alg = QCRYPTO_HASH_ALG_SHA256,
  93. .has_hash_alg = true,
  94. .hash_alg = QCRYPTO_HASH_ALG_SHA1,
  95. },
  96. };
  97. #endif /* TEST_LUKS */
  98. static struct QCryptoBlockTestData {
  99. const char *path;
  100. QCryptoBlockCreateOptions *create_opts;
  101. QCryptoBlockOpenOptions *open_opts;
  102. bool expect_header;
  103. QCryptoCipherAlgorithm cipher_alg;
  104. QCryptoCipherMode cipher_mode;
  105. QCryptoHashAlgorithm hash_alg;
  106. QCryptoIVGenAlgorithm ivgen_alg;
  107. QCryptoHashAlgorithm ivgen_hash;
  108. bool slow;
  109. } test_data[] = {
  110. {
  111. .path = "/crypto/block/qcow",
  112. .create_opts = &qcow_create_opts,
  113. .open_opts = &qcow_open_opts,
  114. .expect_header = false,
  115. .cipher_alg = QCRYPTO_CIPHER_ALG_AES_128,
  116. .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
  117. .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
  118. },
  119. #ifdef TEST_LUKS
  120. {
  121. .path = "/crypto/block/luks/default",
  122. .create_opts = &luks_create_opts_default,
  123. .open_opts = &luks_open_opts,
  124. .expect_header = true,
  125. .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
  126. .cipher_mode = QCRYPTO_CIPHER_MODE_XTS,
  127. .hash_alg = QCRYPTO_HASH_ALG_SHA256,
  128. .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
  129. .slow = true,
  130. },
  131. {
  132. .path = "/crypto/block/luks/aes-256-cbc-plain64",
  133. .create_opts = &luks_create_opts_aes256_cbc_plain64,
  134. .open_opts = &luks_open_opts,
  135. .expect_header = true,
  136. .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
  137. .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
  138. .hash_alg = QCRYPTO_HASH_ALG_SHA256,
  139. .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
  140. .slow = true,
  141. },
  142. {
  143. .path = "/crypto/block/luks/aes-256-cbc-essiv",
  144. .create_opts = &luks_create_opts_aes256_cbc_essiv,
  145. .open_opts = &luks_open_opts,
  146. .expect_header = true,
  147. .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
  148. .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
  149. .hash_alg = QCRYPTO_HASH_ALG_SHA1,
  150. .ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
  151. .ivgen_hash = QCRYPTO_HASH_ALG_SHA256,
  152. .slow = true,
  153. },
  154. #endif
  155. };
  156. static ssize_t test_block_read_func(QCryptoBlock *block,
  157. size_t offset,
  158. uint8_t *buf,
  159. size_t buflen,
  160. void *opaque,
  161. Error **errp)
  162. {
  163. Buffer *header = opaque;
  164. g_assert_cmpint(offset + buflen, <=, header->capacity);
  165. memcpy(buf, header->buffer + offset, buflen);
  166. return buflen;
  167. }
  168. static ssize_t test_block_init_func(QCryptoBlock *block,
  169. size_t headerlen,
  170. void *opaque,
  171. Error **errp)
  172. {
  173. Buffer *header = opaque;
  174. g_assert_cmpint(header->capacity, ==, 0);
  175. buffer_reserve(header, headerlen);
  176. return headerlen;
  177. }
  178. static ssize_t test_block_write_func(QCryptoBlock *block,
  179. size_t offset,
  180. const uint8_t *buf,
  181. size_t buflen,
  182. void *opaque,
  183. Error **errp)
  184. {
  185. Buffer *header = opaque;
  186. g_assert_cmpint(buflen + offset, <=, header->capacity);
  187. memcpy(header->buffer + offset, buf, buflen);
  188. header->offset = offset + buflen;
  189. return buflen;
  190. }
  191. static Object *test_block_secret(void)
  192. {
  193. return object_new_with_props(
  194. TYPE_QCRYPTO_SECRET,
  195. object_get_objects_root(),
  196. "sec0",
  197. &error_abort,
  198. "data", "123456",
  199. NULL);
  200. }
  201. static void test_block_assert_setup(const struct QCryptoBlockTestData *data,
  202. QCryptoBlock *blk)
  203. {
  204. QCryptoIVGen *ivgen;
  205. QCryptoCipher *cipher;
  206. ivgen = qcrypto_block_get_ivgen(blk);
  207. cipher = qcrypto_block_get_cipher(blk);
  208. g_assert(ivgen);
  209. g_assert(cipher);
  210. g_assert_cmpint(data->cipher_alg, ==, cipher->alg);
  211. g_assert_cmpint(data->cipher_mode, ==, cipher->mode);
  212. g_assert_cmpint(data->hash_alg, ==,
  213. qcrypto_block_get_kdf_hash(blk));
  214. g_assert_cmpint(data->ivgen_alg, ==,
  215. qcrypto_ivgen_get_algorithm(ivgen));
  216. g_assert_cmpint(data->ivgen_hash, ==,
  217. qcrypto_ivgen_get_hash(ivgen));
  218. }
  219. static void test_block(gconstpointer opaque)
  220. {
  221. const struct QCryptoBlockTestData *data = opaque;
  222. QCryptoBlock *blk;
  223. Buffer header;
  224. Object *sec = test_block_secret();
  225. memset(&header, 0, sizeof(header));
  226. buffer_init(&header, "header");
  227. blk = qcrypto_block_create(data->create_opts, NULL,
  228. test_block_init_func,
  229. test_block_write_func,
  230. &header,
  231. &error_abort);
  232. g_assert(blk);
  233. if (data->expect_header) {
  234. g_assert_cmpint(header.capacity, >, 0);
  235. } else {
  236. g_assert_cmpint(header.capacity, ==, 0);
  237. }
  238. test_block_assert_setup(data, blk);
  239. qcrypto_block_free(blk);
  240. object_unparent(sec);
  241. /* Ensure we can't open without the secret */
  242. blk = qcrypto_block_open(data->open_opts, NULL,
  243. test_block_read_func,
  244. &header,
  245. 0,
  246. 1,
  247. NULL);
  248. g_assert(blk == NULL);
  249. /* Ensure we can't open without the secret, unless NO_IO */
  250. blk = qcrypto_block_open(data->open_opts, NULL,
  251. test_block_read_func,
  252. &header,
  253. QCRYPTO_BLOCK_OPEN_NO_IO,
  254. 1,
  255. &error_abort);
  256. g_assert(qcrypto_block_get_cipher(blk) == NULL);
  257. g_assert(qcrypto_block_get_ivgen(blk) == NULL);
  258. qcrypto_block_free(blk);
  259. /* Now open for real with secret */
  260. sec = test_block_secret();
  261. blk = qcrypto_block_open(data->open_opts, NULL,
  262. test_block_read_func,
  263. &header,
  264. 0,
  265. 1,
  266. &error_abort);
  267. g_assert(blk);
  268. test_block_assert_setup(data, blk);
  269. qcrypto_block_free(blk);
  270. object_unparent(sec);
  271. buffer_free(&header);
  272. }
  273. int main(int argc, char **argv)
  274. {
  275. gsize i;
  276. module_call_init(MODULE_INIT_QOM);
  277. g_test_init(&argc, &argv, NULL);
  278. g_assert(qcrypto_init(NULL) == 0);
  279. for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
  280. if (test_data[i].open_opts->format == Q_CRYPTO_BLOCK_FORMAT_LUKS &&
  281. !qcrypto_hash_supports(test_data[i].hash_alg)) {
  282. continue;
  283. }
  284. if (!test_data[i].slow ||
  285. g_test_slow()) {
  286. g_test_add_data_func(test_data[i].path, &test_data[i], test_block);
  287. }
  288. }
  289. return g_test_run();
  290. }