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