test-crypto-afsplit.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * QEMU Crypto anti-forensic splitter
  3. *
  4. * Copyright (c) 2015-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/afsplit.h"
  24. typedef struct QCryptoAFSplitTestData QCryptoAFSplitTestData;
  25. struct QCryptoAFSplitTestData {
  26. const char *path;
  27. QCryptoHashAlgorithm hash;
  28. uint32_t stripes;
  29. size_t blocklen;
  30. const uint8_t *key;
  31. const uint8_t *splitkey;
  32. };
  33. static QCryptoAFSplitTestData test_data[] = {
  34. {
  35. .path = "/crypto/afsplit/sha256/5",
  36. .hash = QCRYPTO_HASH_ALG_SHA256,
  37. .stripes = 5,
  38. .blocklen = 32,
  39. .key = (const uint8_t *)
  40. "\x00\x01\x02\x03\x04\x05\x06\x07"
  41. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  42. "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7"
  43. "\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
  44. .splitkey = (const uint8_t *)
  45. "\xfd\xd2\x73\xb1\x7d\x99\x93\x34"
  46. "\x70\xde\xfa\x07\xc5\xac\x58\xd2"
  47. "\x30\x67\x2f\x1a\x35\x43\x60\x7d"
  48. "\x77\x02\xdb\x62\x3c\xcb\x2c\x33"
  49. "\x48\x08\xb6\xf1\x7c\xa3\x20\xa0"
  50. "\xad\x2d\x4c\xf3\xcd\x18\x6f\x53"
  51. "\xf9\xe8\xe7\x59\x27\x3c\xa9\x54"
  52. "\x61\x87\xb3\xaf\xf6\xf7\x7e\x64"
  53. "\x86\xaa\x89\x7f\x1f\x9f\xdb\x86"
  54. "\xf4\xa2\x16\xff\xa3\x4f\x8c\xa1"
  55. "\x59\xc4\x23\x34\x28\xc4\x77\x71"
  56. "\x83\xd4\xcd\x8e\x89\x1b\xc7\xc5"
  57. "\xae\x4d\xa9\xcd\xc9\x72\x85\x70"
  58. "\x13\x68\x52\x83\xfc\xb8\x11\x72"
  59. "\xba\x3d\xc6\x4a\x28\xfa\xe2\x86"
  60. "\x7b\x27\xab\x58\xe1\xa4\xca\xf6"
  61. "\x9e\xbc\xfe\x0c\x92\x79\xb3\xec"
  62. "\x1c\x5f\x79\x3b\x0d\x1e\xaa\x1a"
  63. "\x77\x0f\x70\x19\x4b\xc8\x80\xee"
  64. "\x27\x7c\x6e\x4a\x91\x96\x5c\xf4"
  65. },
  66. {
  67. .path = "/crypto/afsplit/sha256/5000",
  68. .hash = QCRYPTO_HASH_ALG_SHA256,
  69. .stripes = 5000,
  70. .blocklen = 16,
  71. .key = (const uint8_t *)
  72. "\x00\x01\x02\x03\x04\x05\x06\x07"
  73. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
  74. },
  75. {
  76. .path = "/crypto/afsplit/sha1/1000",
  77. .hash = QCRYPTO_HASH_ALG_SHA1,
  78. .stripes = 1000,
  79. .blocklen = 32,
  80. .key = (const uint8_t *)
  81. "\x00\x01\x02\x03\x04\x05\x06\x07"
  82. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  83. "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7"
  84. "\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
  85. },
  86. {
  87. .path = "/crypto/afsplit/sha256/big",
  88. .hash = QCRYPTO_HASH_ALG_SHA256,
  89. .stripes = 1000,
  90. .blocklen = 64,
  91. .key = (const uint8_t *)
  92. "\x00\x01\x02\x03\x04\x05\x06\x07"
  93. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  94. "\x00\x01\x02\x03\x04\x05\x06\x07"
  95. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  96. "\x00\x01\x02\x03\x04\x05\x06\x07"
  97. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  98. "\x00\x01\x02\x03\x04\x05\x06\x07"
  99. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
  100. },
  101. };
  102. static inline char hex(int i)
  103. {
  104. if (i < 10) {
  105. return '0' + i;
  106. }
  107. return 'a' + (i - 10);
  108. }
  109. static char *hex_string(const uint8_t *bytes,
  110. size_t len)
  111. {
  112. char *hexstr = g_new0(char, len * 2 + 1);
  113. size_t i;
  114. for (i = 0; i < len; i++) {
  115. hexstr[i * 2] = hex((bytes[i] >> 4) & 0xf);
  116. hexstr[i * 2 + 1] = hex(bytes[i] & 0xf);
  117. }
  118. hexstr[len * 2] = '\0';
  119. return hexstr;
  120. }
  121. static void test_afsplit(const void *opaque)
  122. {
  123. const QCryptoAFSplitTestData *data = opaque;
  124. size_t splitlen = data->blocklen * data->stripes;
  125. uint8_t *splitkey = g_new0(uint8_t, splitlen);
  126. uint8_t *key = g_new0(uint8_t, data->blocklen);
  127. gchar *expect, *actual;
  128. /* First time we round-trip the key */
  129. qcrypto_afsplit_encode(data->hash,
  130. data->blocklen, data->stripes,
  131. data->key, splitkey,
  132. &error_abort);
  133. qcrypto_afsplit_decode(data->hash,
  134. data->blocklen, data->stripes,
  135. splitkey, key,
  136. &error_abort);
  137. expect = hex_string(data->key, data->blocklen);
  138. actual = hex_string(key, data->blocklen);
  139. g_assert_cmpstr(actual, ==, expect);
  140. g_free(actual);
  141. g_free(expect);
  142. /* Second time we merely try decoding a previous split */
  143. if (data->splitkey) {
  144. memset(key, 0, data->blocklen);
  145. qcrypto_afsplit_decode(data->hash,
  146. data->blocklen, data->stripes,
  147. data->splitkey, key,
  148. &error_abort);
  149. expect = hex_string(data->key, data->blocklen);
  150. actual = hex_string(key, data->blocklen);
  151. g_assert_cmpstr(actual, ==, expect);
  152. g_free(actual);
  153. g_free(expect);
  154. }
  155. g_free(key);
  156. g_free(splitkey);
  157. }
  158. int main(int argc, char **argv)
  159. {
  160. size_t i;
  161. g_test_init(&argc, &argv, NULL);
  162. g_assert(qcrypto_init(NULL) == 0);
  163. for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
  164. if (!qcrypto_hash_supports(test_data[i].hash)) {
  165. continue;
  166. }
  167. g_test_add_data_func(test_data[i].path, &test_data[i], test_afsplit);
  168. }
  169. return g_test_run();
  170. }