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