summaryrefslogtreecommitdiffstats
path: root/examples/ncr_lib.c
blob: d0f435e33a6d70e117535dbc081debc2e27712a8 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Demo on how to use libcryptodev for HMAC.
 *
 * Placed under public domain.
 *
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
//#include <fcntl.h>
#include <time.h>
//#include <sys/ioctl.h>
#include <sys/types.h>
//#include <sys/stat.h>
#include "../ncr.h"
#include "../userspace/ncrypto.h"
#include <stdlib.h>

#define DATA_SIZE 4096
#define KEY_DATA_SIZE 16
#define WRAPPED_KEY_DATA_SIZE 32
#define DKEY "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"
#define DIAGNOSTIC_CALL(f,p...)							\
	if ((output_size = f(p)) < 0) {						\
		fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);		\
		perror(#f);							\
		return 1;							\
	}
#define DIAGNOSTIC_ERROR(p...)							\
	fprintf(stderr, "Error: %s:%d\n", __func__, __LINE__);			\
	fprintf(stderr, p);
#define DIAGNOSTIC_DUMP(d,l)							\
	{									\
		int out_index;							\
										\
		for (out_index = 0; out_index < l; out_index++)			\
			fprintf(stderr, "%.2x:", (int)d[out_index]);		\
		fprintf(stderr, "\n");						\
	}

static void randomize_data(uint8_t * data, size_t data_size)
{
	int i;
	
	srand(time(0) * getpid());
	for (i = 0; i < data_size; i++) {
		data[i] = rand() & 0xff;
	}
}

static int
test_ncr_key(void)
{
	ncr_key_t key;
	uint8_t data[KEY_DATA_SIZE];
	uint8_t data_bak[KEY_DATA_SIZE];
	ssize_t output_size;
	ncr_key_generate_params_t params;

	fprintf(stdout, "Tests on Keys:\n");

	/* test 1: generate a key in userspace import it
	 * to kernel via data and export it.
	 */

	fprintf(stdout, "\tKey import...\n");
	randomize_data(data, sizeof(data));
	memcpy(data_bak, data, sizeof(data));
	DIAGNOSTIC_CALL(ncr_key_init, &key);
	/* import into a key */
	DIAGNOSTIC_CALL(ncr_key_import, key, data, sizeof(data), "ab", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE);
	/* now try to read it */
	fprintf(stdout, "\tKey export...\n");
	DIAGNOSTIC_CALL(ncr_key_export, key, data, sizeof(data));
	if ((output_size != sizeof(data)) || memcmp(data, data_bak, sizeof(data))) {
		DIAGNOSTIC_ERROR("data returned but differ!\n");
		return 1;
	}
	DIAGNOSTIC_CALL(ncr_key_deinit, key);

	/* finished, we keep data for next test */

	/* test 2: generate a key in kernel space and
	 * export it.
	 */

	fprintf(stdout, "\tKey generation...\n");
	DIAGNOSTIC_CALL(ncr_key_generate_params_init, &params);
	DIAGNOSTIC_CALL(ncr_key_generate_params_set_algorithm, params, NCR_ALG_AES_CBC);
	DIAGNOSTIC_CALL(ncr_key_generate_params_set_keyflags, params, NCR_KEY_FLAG_EXPORTABLE);
	DIAGNOSTIC_CALL(ncr_key_generate_params_set_secret_bits, params, 128); /* 16  bytes */
	DIAGNOSTIC_CALL(ncr_key_init, &key);
	/* generate a key */
	DIAGNOSTIC_CALL(ncr_key_generate, key, params);
	DIAGNOSTIC_CALL(ncr_key_generate_params_deinit, params);
	memset(data, 0, sizeof(data));
	DIAGNOSTIC_CALL(ncr_key_export, key, data, sizeof(data));
	if (output_size == 0 || (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[4] == 0)) {
		DIAGNOSTIC_ERROR("Generated key: ");
		DIAGNOSTIC_DUMP(data, 16);
		return 1;
	}
	DIAGNOSTIC_CALL(ncr_key_deinit, key);
	
	/* test 3: generate an unexportable key in kernel space and
	 * try to export it.
	 */
	fprintf(stdout, "\tKey protection of non-exportable keys...\n");
	DIAGNOSTIC_CALL(ncr_key_generate_params_init, &params);
	DIAGNOSTIC_CALL(ncr_key_generate_params_set_algorithm, params, NCR_ALG_AES_CBC);
	DIAGNOSTIC_CALL(ncr_key_generate_params_set_keyflags, params, 0);
	DIAGNOSTIC_CALL(ncr_key_generate_params_set_secret_bits, params, 128); /* 16  bytes */
	DIAGNOSTIC_CALL(ncr_key_init, &key);
	DIAGNOSTIC_CALL(ncr_key_generate, key, params);
	DIAGNOSTIC_CALL(ncr_key_generate_params_deinit, params);
	memset(data, 0, sizeof(data));
	/* try to get the output data - should fail */
	if (ncr_key_export(key, data, sizeof(data)) >= 0) {
		DIAGNOSTIC_ERROR("Data were exported, but shouldn't be!\n");
		return 1;
	}
	DIAGNOSTIC_CALL(ncr_key_deinit, key);
	return 0;
}

/* Key wrapping */
static int
test_ncr_wrap_key(void)
{
	ncr_key_t key, key2;
	uint8_t data[WRAPPED_KEY_DATA_SIZE];
	ssize_t output_size, data_size;

	fprintf(stdout, "Tests on Keys:\n");

	/* test 1: generate a key in userspace import it
	 * to kernel via data and export it.
	 */

	fprintf(stdout, "\tKey Wrap test...\n");
	DIAGNOSTIC_CALL(ncr_key_init, &key);
	/* import into a key */
	DIAGNOSTIC_CALL(ncr_key_import, key, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", 16, "ab", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE);
	DIAGNOSTIC_CALL(ncr_key_init, &key2);
	/* import into a key2 */
	DIAGNOSTIC_CALL(ncr_key_import, key2, DKEY, 16, "ba", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE);
	/* now try wrapping key2 using key */
	DIAGNOSTIC_CALL(ncr_key_wrap, key, NCR_WALG_AES_RFC3394, NULL, key2, data, sizeof(data));
	data_size = output_size;
	if (output_size != 24 || memcmp(data, "\x1F\xA6\x8B\x0A\x81\x12\xB4\x47\xAE\xF3\x4B\xD8\xFB\x5A\x7B\x82\x9D\x3E\x86\x23\x71\xD2\xCF\xE5", 24) != 0) {
		DIAGNOSTIC_ERROR("Wrapped data do not match.\nData[%d]: ", (int) output_size);
		DIAGNOSTIC_DUMP(data, output_size);
		return 1;
	}
	DIAGNOSTIC_CALL(ncr_key_deinit, key2);
	/* test unwrapping */
	fprintf(stdout, "\tKey Unwrap test...\n");

	/* create empty key2 */
	DIAGNOSTIC_CALL(ncr_key_init, &key2);
	DIAGNOSTIC_CALL(ncr_key_unwrap, key, NCR_WALG_AES_RFC3394, NULL, key2, data, data_size);
	/* now export the unwrapped */
#if 0
	/* this cannot be performed like that, because unwrap
	 * always sets keys as unexportable. Maybe we can implement
	 * a data comparison ioctl().
	 */
#endif
	DIAGNOSTIC_CALL(ncr_key_deinit, key2);
	DIAGNOSTIC_CALL(ncr_key_deinit, key);

	return 0;
}

static int
test_ncr_store_wrap_key(void)
{
	ncr_key_t key2;
	uint8_t data[DATA_SIZE];
	int data_size;
	ssize_t output_size;

	fprintf(stdout, "Tests on Key storage:\n");

	/* test 1: generate a key in userspace import it
	 * to kernel via data and export it.
	 */

	fprintf(stdout, "\tKey Storage wrap test...\n");
	/* create empty key2 */
	DIAGNOSTIC_CALL(ncr_key_init, &key2);
	/* import into a key2 */
	DIAGNOSTIC_CALL(ncr_key_import, key2, DKEY, 16, "ba", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE);
	/* now try wrapping key2 using masterkey */
	DIAGNOSTIC_CALL(ncr_key_storage_wrap, key2, data, sizeof(data));
	/* test unwrapping */
	data_size = output_size;
	fprintf(stdout, "\tKey Storage Unwrap test...\n");
	/* reset key2 */
	DIAGNOSTIC_CALL(ncr_key_deinit, key2);
	DIAGNOSTIC_CALL(ncr_key_init, &key2);
	DIAGNOSTIC_CALL(ncr_key_storage_unwrap, key2, data, data_size);
	/* now export the unwrapped */
	DIAGNOSTIC_CALL(ncr_key_export, key2, data, sizeof(data));
	if (output_size != 16 || memcmp(data, DKEY, 16)) {
		DIAGNOSTIC_ERROR("Unwrapped data do not match.\n");
		DIAGNOSTIC_DUMP(data, output_size);
		return 1;
	}
	DIAGNOSTIC_CALL(ncr_key_deinit, key2);

	return 0;

}

struct aes_vectors_st {
	const uint8_t* key;
	const uint8_t* plaintext;
	const uint8_t* ciphertext;
} aes_vectors[] = {
	{
		.key = (uint8_t*)"\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
		.plaintext = (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
		.ciphertext = (uint8_t*)"\x4b\xc3\xf8\x83\x45\x0c\x11\x3c\x64\xca\x42\xe1\x11\x2a\x9e\x87",
	},
	{
		.key = (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
		.plaintext = (uint8_t*)"\xf3\x44\x81\xec\x3c\xc6\x27\xba\xcd\x5d\xc3\xfb\x08\xf2\x73\xe6",
		.ciphertext = (uint8_t*)"\x03\x36\x76\x3e\x96\x6d\x92\x59\x5a\x56\x7c\xc9\xce\x53\x7f\x5e",
	},
	{
		.key = (uint8_t*)"\x10\xa5\x88\x69\xd7\x4b\xe5\xa3\x74\xcf\x86\x7c\xfb\x47\x38\x59",
		.plaintext = (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
		.ciphertext = (uint8_t*)"\x6d\x25\x1e\x69\x44\xb0\x51\xe0\x4e\xaa\x6f\xb4\xdb\xf7\x84\x65",
	},
	{
		.key = (uint8_t*)"\xca\xea\x65\xcd\xbb\x75\xe9\x16\x9e\xcd\x22\xeb\xe6\xe5\x46\x75",
		.plaintext = (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
		.ciphertext = (uint8_t*)"\x6e\x29\x20\x11\x90\x15\x2d\xf4\xee\x05\x81\x39\xde\xf6\x10\xbb",
	},
	{
		.key = (uint8_t*)"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe",
		.plaintext = (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
		.ciphertext = (uint8_t*)"\x9b\xa4\xa9\x14\x3f\x4e\x5d\x40\x48\x52\x1c\x4f\x88\x77\xd8\x8e",
	},
};

/* AES cipher */
static int
test_ncr_aes(void)
{
	ncr_key_t key;
	uint8_t data[KEY_DATA_SIZE];
	int i;
	ssize_t output_size;

	/* convert it to key */
	DIAGNOSTIC_CALL(ncr_key_init, &key);

	fprintf(stdout, "Tests on AES Encryption\n");
	for (i = 0; i < sizeof(aes_vectors) / sizeof(aes_vectors[0]); i++) {
		DIAGNOSTIC_CALL(ncr_key_import, key, (void*)aes_vectors[i].key, 16, "ab", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE);
		/* encrypt */
		DIAGNOSTIC_CALL(ncr_session_once_direct_data, key, NULL, NCR_OP_ENCRYPT, NCR_ALG_AES_ECB, (void*)aes_vectors[i].plaintext, 16, data, 16);
		/* verify */
		if (output_size != 16 || memcmp(data, aes_vectors[i].ciphertext, 16)) {
			DIAGNOSTIC_ERROR("AES test vector %d failed!\n", i);
			fprintf(stderr, "Cipher[%d]: ", (int)output_size);
			DIAGNOSTIC_DUMP(data, output_size);
			fprintf(stderr, "Expected[%d]: ", 16);
			DIAGNOSTIC_DUMP(aes_vectors[i].ciphertext, 16);
			return 1;
		}
	}

	fprintf(stdout, "Tests on AES Decryption\n");
	for (i = 0; i < sizeof(aes_vectors) / sizeof(aes_vectors[0]); i++) {
		DIAGNOSTIC_CALL(ncr_key_import, key, (void*)aes_vectors[i].key, 16, "ab", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE);
		/* decrypt */
		DIAGNOSTIC_CALL(ncr_session_once_direct_data, key, NULL, NCR_OP_DECRYPT, NCR_ALG_AES_ECB, (void*)aes_vectors[i].ciphertext, 16, data, 16);
		if (output_size != 16 || memcmp(data, aes_vectors[i].plaintext, 16)) {
			DIAGNOSTIC_ERROR("AES test vector %d failed!\n", i);
			fprintf(stderr, "Plain[%d]: ", (int)output_size);
			DIAGNOSTIC_DUMP(data, output_size);
			fprintf(stderr, "Expected[%d]: ", 16);
			DIAGNOSTIC_DUMP(aes_vectors[i].plaintext, 16);
			return 1;
		}
	}

	DIAGNOSTIC_CALL(ncr_key_deinit, key);
	fprintf(stdout, "\n");

	return 0;
}

struct hash_vectors_st {
	const char* name;
	ncr_algorithm_t algorithm;
	const uint8_t* key; /* if hmac */
	int key_size;
	const uint8_t* plaintext;
	int plaintext_size;
	const uint8_t* output;
	int output_size;
	ncr_crypto_op_t op;
} hash_vectors[] = {
	{
		.name = "SHA1",
		.algorithm = NCR_ALG_SHA1,
		.key = NULL,
		.plaintext = (uint8_t*)"what do ya want for nothing?",
		.plaintext_size = sizeof("what do ya want for nothing?")-1,
		.output = (uint8_t*)"\x8f\x82\x03\x94\xf9\x53\x35\x18\x20\x45\xda\x24\xf3\x4d\xe5\x2b\xf8\xbc\x34\x32",
		.output_size = 20,
		.op = NCR_OP_SIGN,
	},
	{
		.name = "HMAC-MD5",
		.algorithm = NCR_ALG_HMAC_MD5,
		.key = (uint8_t*)"Jefe",
		.key_size = 4,
		.plaintext = (uint8_t*)"what do ya want for nothing?",
		.plaintext_size = sizeof("what do ya want for nothing?")-1,
		.output = (uint8_t*)"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38",
		.output_size = 16,
		.op = NCR_OP_SIGN,
	},
	/* from rfc4231 */
	{
		.name = "HMAC-SHA224",
		.algorithm = NCR_ALG_HMAC_SHA2_224,
		.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		.key_size = 20,
		.plaintext = (uint8_t*)"Hi There",
		.plaintext_size = sizeof("Hi There")-1,
		.output = (uint8_t*)"\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22",
		.output_size = 28,
		.op = NCR_OP_SIGN,
	},
	{
		.name = "HMAC-SHA256",
		.algorithm = NCR_ALG_HMAC_SHA2_256,
		.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		.key_size = 20,
		.plaintext = (uint8_t*)"Hi There",
		.plaintext_size = sizeof("Hi There")-1,
		.output = (uint8_t*)"\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7",
		.output_size = 32,
		.op = NCR_OP_SIGN,
	},
	{
		.name = "HMAC-SHA384",
		.algorithm = NCR_ALG_HMAC_SHA2_384,
		.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		.key_size = 20,
		.plaintext = (uint8_t*)"Hi There",
		.plaintext_size = sizeof("Hi There")-1,
		.output = (uint8_t*)"\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2\xfa\x9c\xb6",
		.output_size = 48,
		.op = NCR_OP_SIGN,
	},
	{
		.name = "HMAC-SHA512",
		.algorithm = NCR_ALG_HMAC_SHA2_512,
		.key = (uint8_t*)"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
		.key_size = 20,
		.plaintext = (uint8_t*)"Hi There",
		.plaintext_size = sizeof("Hi There")-1,
		.output = (uint8_t*)"\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54",
		.output_size = 64,
		.op = NCR_OP_SIGN,
	},
};

#define HASH_DATA_SIZE 64

/* SHA1 and other hashes */
static int
test_ncr_hash(void)
{
	ncr_key_t key = 0;
	uint8_t data[HASH_DATA_SIZE];
	int i;
	int output_size;

	/* convert it to key */
	DIAGNOSTIC_CALL(ncr_key_init, &key);
	fprintf(stdout, "Tests on Hashes\n");
	for (i = 0; i < sizeof(hash_vectors) / sizeof(hash_vectors[0]); i++) {
		/* encrypt */
		if (hash_vectors[i].key != NULL) {
			DIAGNOSTIC_CALL(ncr_key_import, key, (void*)hash_vectors[i].key, hash_vectors[i].key_size, "ab", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE);
			DIAGNOSTIC_CALL(ncr_session_once_direct_data, key, NULL, hash_vectors[i].op, hash_vectors[i].algorithm, (void*)hash_vectors[i].plaintext, hash_vectors[i].plaintext_size, data, sizeof(data));
		} else {
			DIAGNOSTIC_CALL(ncr_session_once_direct_data, NCR_KEY_INVALID, NULL, hash_vectors[i].op, hash_vectors[i].algorithm, (void*)hash_vectors[i].plaintext, hash_vectors[i].plaintext_size, data, sizeof(data));
		}
		if (output_size != hash_vectors[i].output_size || memcmp(data, hash_vectors[i].output, hash_vectors[i].output_size)) {
			DIAGNOSTIC_ERROR("HASH test vector %d failed!\n", i);
			fprintf(stderr, "Output[%d]: ", (int)output_size);
			DIAGNOSTIC_DUMP(data, output_size);
			fprintf(stderr, "Expected[%d]: ", hash_vectors[i].output_size);
			DIAGNOSTIC_DUMP(hash_vectors[i].output, hash_vectors[i].output_size);
			return 1;
		}
	}

	DIAGNOSTIC_CALL(ncr_key_deinit, key);
	fprintf(stdout, "\n");

	return 0;

}

static int
test_ncr_hash_key(void)
{
	ncr_key_t key;
	uint8_t data[HASH_DATA_SIZE];
	ncr_session_t session;
	const uint8_t *output = (void*)"\xe2\xd7\x2c\x2e\x14\xad\x97\xc8\xd2\xdb\xce\xd8\xb3\x52\x9f\x1c\xb3\x2c\x5c\xec";
	int output_size;

	/* convert it to key */
	DIAGNOSTIC_CALL(ncr_key_init, &key);

	fprintf(stdout, "Tests on Hashes of Keys\n");
	fprintf(stdout, "\t%s:\n", hash_vectors[0].name);

	/* import key */
	DIAGNOSTIC_CALL(ncr_key_import, key, (void*)hash_vectors[0].plaintext, hash_vectors[0].plaintext_size, "ab", 2, NCR_ALG_AES_CBC, NCR_KEY_TYPE_SECRET, NCR_KEY_FLAG_EXPORTABLE);


	/* encrypt */
	DIAGNOSTIC_CALL(ncr_session_init, &session, NCR_KEY_INVALID, NULL, hash_vectors[0].op, hash_vectors[0].algorithm);
	DIAGNOSTIC_CALL(ncr_session_update_direct_data, session, (void*)hash_vectors[0].plaintext, hash_vectors[0].plaintext_size, NULL, 0);
	DIAGNOSTIC_CALL(ncr_session_update_key_data, session, key, NULL, 0);
	DIAGNOSTIC_CALL(ncr_session_final, session, data, sizeof(data));
	if (output_size != hash_vectors[0].output_size || memcmp(data, output, hash_vectors[0].output_size) != 0) {
			DIAGNOSTIC_ERROR("HASH test vector %d failed!\n", 0);
			fprintf(stderr, "Output[%d]: ", (int)output_size);
			DIAGNOSTIC_DUMP(data, output_size);
			fprintf(stderr, "Expected[%d]: ", hash_vectors[0].output_size);
			DIAGNOSTIC_DUMP(hash_vectors[0].output, hash_vectors[0].output_size);
			return 1;
	}

	fprintf(stdout, "\n");
	return 0;
}

int
main()
{
	/* Open the crypto device */
	ncr_global_init(0);
	if (test_ncr_key())
		return 1;
	if (test_ncr_aes())
		return 1;
	if (test_ncr_hash())
		return 1;
	if (test_ncr_hash_key())
		return 1;
	if (test_ncr_wrap_key())
		return 1;
	if (test_ncr_store_wrap_key())
		return 1;
	/* Close the original descriptor */
	ncr_global_deinit();

	return 0;
}