summaryrefslogtreecommitdiff
path: root/egg/test-symkey.c
blob: 90211d680a620ee3b7dae8ad88eea088c9d03386 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-crypto.c: Test crypto stuff

   Copyright (C) 2007 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "egg/egg-asn1x.h"
#include "egg/egg-asn1-defs.h"
#include "egg/egg-libgcrypt.h"
#include "egg/egg-secure-memory.h"
#include "egg/egg-symkey.h"
#include "egg/egg-testing.h"

typedef struct _EggAsn1xDef ASN1_ARRAY_TYPE;
typedef struct _EggAsn1xDef asn1_static_node;
#include "test.asn.h"

#include <gcrypt.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

EGG_SECURE_DEFINE_GLIB_GLOBALS ();

static const struct {
	const gchar *password;
	int cipher_algo;
	int hash_algo;
	int iterations;
	const gchar *salt;

	const gchar *result_simple;
	const gchar *result_pkcs12;
	const gchar *result_pbkdf2;
	const gchar *result_pbe;
} all_generation_tests[] = {

	{ /* 24 byte output */
		"booo", GCRY_CIPHER_3DES, GCRY_MD_MD5, 1,
		"\x70\x4C\xFF\xD6\x2F\xBA\x03\xE9",
		"\x84\x12\xBB\x34\x94\x8C\x40\xAD\x97\x57\x96\x74\x5B\x6A\xFB\xF8\xD6\x61\x33\x51\xEA\x8C\xCF\xD8",
		NULL,
		NULL,
		NULL
	},

	{ /* 5 byte output */
		"booo", GCRY_CIPHER_RFC2268_40, GCRY_MD_SHA1, 2048,
		"\x8A\x58\xC2\xE8\x7C\x1D\x80\x11",
		NULL,
		"\xD6\xA6\xF0\x76\x66",
		NULL,
		NULL
	},

	{ /* Null Password, 5 byte output */
		NULL, GCRY_CIPHER_RFC2268_40, GCRY_MD_SHA1, 2000,
		"\x04\xE0\x1C\x3E\xF8\xF2\xE9\xFD",
		NULL,
		"\x98\x7F\x20\x97\x1E",
		NULL,
		NULL
	},

	{ /* 24 byte output */
		"booo", GCRY_CIPHER_3DES, GCRY_MD_SHA1, 2048,
		"\xBD\xEE\x0B\xC6\xCF\x43\xAC\x25",
		NULL,
		"\x3F\x38\x1B\x0E\x87\xEB\x19\xBE\xD1\x39\xDC\x5B\xC2\xD2\xB3\x3C\x35\xA8\xB8\xF9\xEE\x66\x48\x94",
		"\x20\x25\x90\xD8\xD6\x98\x3E\x71\x10\x17\x1F\x51\x49\x87\x27\xCA\x97\x27\xD1\xC9\x72\xF8\x11\xBB",
		NULL
	},

	{ /* Empty password, 24 byte output */
		"", GCRY_CIPHER_3DES, GCRY_MD_SHA1, 2048,
		"\xF7\xCF\xD9\xCF\x1F\xF3\xAD\xF6",
		NULL,
		NULL,
		"\x53\xE3\x35\x9E\x5D\xC1\x85\x1A\x71\x3A\x67\x4E\x80\x56\x13\xD6\x4E\x3E\x89\x43\xB7\x1D\x5F\x7F",
		NULL
	},

	{ /* Empty password, 24 byte output */
		"", GCRY_CIPHER_3DES, GCRY_MD_SHA1, 2048,
		"\xD9\xB3\x2E\xC7\xBA\x1A\x8E\x15",
		NULL,
		"\x39\x70\x75\x7C\xF5\xE2\x13\x0B\x5D\xC2\x9D\x96\x8B\x71\xC7\xFC\x5B\x97\x1F\x79\x9F\x06\xFC\xA2",
		NULL,
		NULL
	},

	{ /* 8 byte output */
		"booo", GCRY_CIPHER_DES, GCRY_MD_MD5, 2048,
		"\x93\x4C\x3D\x29\xA2\x42\xB0\xF5",
		NULL,
		NULL,
		NULL,
		"\x8C\x67\x19\x7F\xB9\x23\xE2\x8D"
	}
};

#define N_GENERATION_TESTS (sizeof (all_generation_tests) / sizeof (all_generation_tests[0]))

static void
test_generate_key_simple (void)
{
	int i;
	gboolean ret;
	guchar *key;

	for (i = 0; i < N_GENERATION_TESTS; ++i) {

		if (!all_generation_tests[i].result_simple)
			continue;

		ret = egg_symkey_generate_simple (all_generation_tests[i].cipher_algo,
		                                  all_generation_tests[i].hash_algo,
		                                  all_generation_tests[i].password, -1,
		                                  (guchar*)all_generation_tests[i].salt, 8,
		                                  all_generation_tests[i].iterations,
		                                  &key, NULL);
		g_assert (ret && "key generation failed");

		ret = (memcmp (key, all_generation_tests[i].result_simple,
		               gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0);

		g_assert (ret && "invalid simple key generated");

		egg_secure_free (key);
	}
}

static void
test_generate_key_pkcs12 (void)
{
	int i;
	gboolean ret;
	guchar *key;

	for (i = 0; i < N_GENERATION_TESTS; ++i) {

		if (!all_generation_tests[i].result_pkcs12)
			continue;

		ret = egg_symkey_generate_pkcs12 (all_generation_tests[i].cipher_algo,
		                                  all_generation_tests[i].hash_algo,
		                                  all_generation_tests[i].password, -1,
		                                  (guchar*)all_generation_tests[i].salt, 8,
		                                  all_generation_tests[i].iterations,
		                                  &key, NULL);
		g_assert ("failed to generate pkcs12 key" && ret);

		ret = (memcmp (key, all_generation_tests[i].result_pkcs12,
			        gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0);

		g_assert ("invalid pkcs12 key generated" && ret);

		egg_secure_free (key);
	}
}

static void
test_generate_key_pbkdf2 (void)
{
	int i;
	gboolean ret;
	guchar *key;

	for (i = 0; i < N_GENERATION_TESTS; ++i) {

		if (!all_generation_tests[i].result_pbkdf2)
			continue;

		ret = egg_symkey_generate_pbkdf2 (all_generation_tests[i].cipher_algo,
		                                  all_generation_tests[i].hash_algo,
		                                  all_generation_tests[i].password, -1,
		                                  (guchar*)all_generation_tests[i].salt, 8,
		                                  all_generation_tests[i].iterations,
		                                  &key, NULL);
		g_assert ("failed to generate pbkdf2 key" && ret);

		ret = (memcmp (key, all_generation_tests[i].result_pbkdf2,
			        gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0);

		g_assert ("invalid pbkdf2 key generated" && ret);

		egg_secure_free (key);
	}
}

static void
test_generate_key_pbe (void)
{
	int i;
	gboolean ret;
	guchar *key;

	for (i = 0; i < N_GENERATION_TESTS; ++i) {

		if (!all_generation_tests[i].result_pbe)
			continue;

		ret = egg_symkey_generate_pbe (all_generation_tests[i].cipher_algo,
		                               all_generation_tests[i].hash_algo,
		                               all_generation_tests[i].password, -1,
		                               (guchar*)all_generation_tests[i].salt, 8,
		                               all_generation_tests[i].iterations,
		                               &key, NULL);
		g_assert ("failed to generate pbe key" && ret);

		ret = (memcmp (key, all_generation_tests[i].result_pbe,
			        gcry_cipher_get_algo_keylen (all_generation_tests[i].cipher_algo)) == 0);

		g_assert ("invalid pbe key generated" && ret);

		egg_secure_free (key);
	}
}

typedef struct {
	const gchar *name;
	const gchar *scheme;

	/* Info to use with cipher */
	const gchar *password;
	const gchar *salt;
	gsize iterations;

	/* DER representation of cipher */
	gsize n_der;
	const gchar *der;

	/* Data to encrypt and test with */
	gsize n_text_length;
	const gchar *plain_text;
	const gchar *cipher_text;
} ReadCipher;

static const ReadCipher cipher_tests[] = {
	{
		"pbe-sha1-des-cbc", "1.2.840.113549.1.5.10",
		"password", "saltsalt", 33,
		15, "\x30\x0D"
			"\x04\x08""saltsalt"
			"\x02\x01\x2A",
		8, "plaintex", "\x69\xe2\x88\x4c\x31\xcf\x0e\x2a"
	},
	{
		"pkcs12-pbe-3des-sha1", "1.2.840.113549.1.12.1.3",
		"password", "saltsalt", 33,
		15, "\x30\x0D"
			"\x04\x08""saltsalt"
			"\x02\x01\x2A",
		8, "plaintex", "\xcf\xfb\x49\x2e\x42\x75\x15\x56"
	},
	{
		"pkcs5-pbes2", "1.2.840.113549.1.5.13",
		"password", "salt", 33,
		48, "\x30\x2e"
			"\x30\x16"
				"\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x05\x0c"
				"\x30\x09"
					"\x04\x04\x73\x61\x6c\x74"
					"\x02\x01\x21"
			"\x30\x14"
				"\x06\x08\x2a\x86\x48\x86\xf7\x0d\x03\x07"
				"\x04\x08\x73\x61\x6c\x74\x73\x61\x6c\x74",
		8, "plaintex", "\x46\x1A\x3A\x39\xD0\xF5\x21\x5C"
	},
	{
		"pkcs5-pbes2-des-cbc", "1.2.840.113549.1.5.13",
		"password", "salt", 33,
		0x2d, "\x30\x2b"
			"\x30\x16"
				"\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x05\x0c"
				"\x30\x09"
					"\x04\x04\x73\x61\x6c\x74"
					"\x02\x01\x21"
			"\x30\x11"
				"\x06\x05\x2b\x0e\x03\x02\x07"
				"\x04\x08\x73\x61\x6c\x74\x73\x61\x6c\x74",
		8, "plaintex", "\xB7\x7B\x54\xBF\x29\x4D\x31\x7D"
	}
};

typedef struct {
	const gchar *name;
	const gchar *scheme;

	/* Info to use with cipher */
	const gchar *password;

	/* DER representation of cipher */
	gsize n_der;
	const gchar *der;
} InvalidCipher;

#if 0
#include "egg/egg-hex.h"

static void
create_pkcs5_pbes2 (void)
{
	GNode *asn;
	GNode *param;
	GBytes *bytes;
	gconstpointer data;
	gsize size;

	asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-5-PBES2-params");

	egg_asn1x_set_oid_as_string (egg_asn1x_node (asn, "keyDerivationFunc", "algorithm", NULL), "1.2.840.113549.1.5.12");
	param = egg_asn1x_create (pkix_asn1_tab, "pkcs-5-PBKDF2-params");
	egg_asn1x_set_integer_as_ulong (egg_asn1x_node (param, "iterationCount", NULL), 33);
#if 1
	egg_asn1x_set_choice (egg_asn1x_node (param, "salt", NULL), egg_asn1x_node (param, "salt", "specified", NULL));
	egg_asn1x_set_string_as_raw (egg_asn1x_node (param, "salt", "specified", NULL), (guchar *)"salt", 4, NULL);
#else
	egg_asn1x_set_choice (egg_asn1x_node (param, "salt", NULL), egg_asn1x_node (param, "salt", "otherSource", NULL)); */
	egg_asn1x_set_oid_as_string (egg_asn1x_node (param, "salt", "otherSource", "algorithm", NULL), "1.2.1"); */
#endif
	egg_asn1x_set_any_from (egg_asn1x_node (asn, "keyDerivationFunc", "parameters", NULL), param);
	egg_asn1x_destroy (param);

	egg_asn1x_set_oid_as_string (egg_asn1x_node (asn, "encryptionScheme", "algorithm", NULL), "1.3.14.3.2.7");
	param = egg_asn1x_create (pkix_asn1_tab, "pkcs-5-des-EDE3-CBC-params");
	egg_asn1x_set_string_as_raw (param, (guchar *)"saltsalt", 8, NULL);
	egg_asn1x_set_any_from (egg_asn1x_node (asn, "encryptionScheme", "parameters", NULL), param);
	egg_asn1x_destroy (param);

	bytes = egg_asn1x_encode (asn, NULL);
	egg_asn1x_assert (bytes != NULL, asn);
	egg_asn1x_destroy (asn);

	data = g_bytes_get_data (bytes, &size);
	g_printerr ("%s: \\x%s\n", __FUNCTION__, egg_hex_encode_full (data, size, FALSE, "\\x", 1));
	g_bytes_unref (bytes);
}
#endif

static void
test_read_cipher (gconstpointer data)
{
	const ReadCipher *test = data;
	gcry_cipher_hd_t cih;
	gcry_error_t gcry;
	GNode *asn;
	gboolean ret;
	GBytes *bytes;
	gpointer block;

	bytes = g_bytes_new_static (test->der, test->n_der);
	asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestAny", bytes);
	g_assert (asn != NULL);
	g_bytes_unref (bytes);

	ret = egg_symkey_read_cipher (g_quark_from_static_string (test->scheme),
	                              test->password, strlen (test->password),
	                              asn, &cih);

	egg_asn1x_destroy (asn);
	g_assert (ret == TRUE);

	block = g_memdup (test->plain_text, test->n_text_length);
	gcry = gcry_cipher_encrypt (cih, block, test->n_text_length, NULL, 0);
	g_assert_cmpint (gcry, ==, 0);

	egg_assert_cmpmem (test->cipher_text, test->n_text_length, ==,
	                   block, test->n_text_length);

	gcry_cipher_close (cih);
	g_free (block);
}

static const InvalidCipher cipher_invalid[] = {
	{
		"pbe-bad-der", "1.2.840.113549.1.12.1.3",
		"password",
		/* Valid DER, but not pkcs-12-PbeParams */
		11, "\x30\x09\x04\x07""invalid"
	},
	{
		"pkcs5-pbe-bad-der", "1.2.840.113549.1.5.10",
		"password",
		/* Valid DER, but not pkcs-5-PBE-params */
		11, "\x30\x09\x04\x07""invalid"
	},
	{
		"pkcs5-pbes2-bad-der", "1.2.840.113549.1.5.13",
		"password",
		/* Valid DER, but not pkcs-5-PBES2-params */
		11, "\x30\x09\x04\x07""invalid"
	},
	{
		"pkcs5-pbes2-missing-key-parameters", "1.2.840.113549.1.5.13",
		"password",
		0x25, "\x30\x23"
			"\x30\x0b"
				"\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x05\x0c"
				/* Missing OPTIONAL parameters here */
			"\x30\x14"
				"\x06\x08\x2a\x86\x48\x86\xf7\x0d\x03\x07"
				"\x04\x08\x73\x61\x6c\x74\x73\x61\x6c\x74",
	},
	{
		"pkcs5-pbes2-missing-scheme-parameters", "1.2.840.113549.1.5.13",
		"password",
		0x26, "\x30\x24"
			"\x30\x16"
				"\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x05\x0c"
				"\x30\x09"
					"\x04\x04\x73\x61\x6c\x74"
					"\x02\x01\x21"
			"\x30\x0a"
				"\x06\x08\x2a\x86\x48\x86\xf7\x0d\x03\x07"
				/* Missing OPTIONAL parameters here */
	},
	{
		"pkcs5-pbes2-bad-key-derivation-algo", "1.2.840.113549.1.5.13",
		"password",
		48, "\x30\x2e"
			"\x30\x16" /* An unsupported keyDerivation algorithm oid */
				"\x06\x09\x2a\x86\x48\x86\xf7\x0c\x01\x04\x0b"
				"\x30\x09"
					"\x04\x04\x73\x61\x6c\x74"
					"\x02\x01\x21"
			"\x30\x14"
				"\x06\x08\x2a\x86\x48\x86\xf7\x0d\x03\x07"
				"\x04\x08\x73\x61\x6c\x74\x73\x61\x6c\x74",
	},
	{
		"pkcs5-pbes2-salt-not-specified", "1.2.840.113549.1.5.13",
		"password",
		0x30, "\x30\x2e"
			"\x30\x16"
				"\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x05\x0c"
				"\x30\x09"
					"\x30\x04"
						"\x06\x02\x2a\x01"
					"\x02\x01\x21"
			"\x30\x14"
				"\x06\x08\x2a\x86\x48\x86\xf7\x0d\x03\x07"
				"\x04\x08\x73\x61\x6c\x74\x73\x61\x6c\x74"
	},
	{
		"pkcs5-pbes2-unsupported-des-rc5-cbc", "1.2.840.113549.1.5.13",
		"password",
		0x30, "\x30\x2e"
			"\x30\x16"
				"\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x05\x0c"
				"\x30\x09"
					"\x04\x04\x73\x61\x6c\x74"
					"\x02\x01\x21"
			"\x30\x14"
				"\x06\x08\x2a\x86\x48\x86\xf7\x0d\x03\x09"
				"\x04\x08\x73\x61\x6c\x74\x73\x61\x6c\x74"
	}
};

static void
test_read_cipher_invalid (gconstpointer data)
{
	const InvalidCipher *test = data;
	gcry_cipher_hd_t cih;
	GNode *asn;
	gboolean ret;
	GBytes *bytes;

	bytes = g_bytes_new_static (test->der, test->n_der);
	asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestAny", bytes);
	g_assert (asn != NULL);
	g_bytes_unref (bytes);

	ret = egg_symkey_read_cipher (g_quark_from_static_string (test->scheme),
	                              test->password, strlen (test->password),
	                              asn, &cih);

	egg_asn1x_destroy (asn);
	g_assert (ret == FALSE);
}

static void
test_read_cipher_unsupported_pbe (void)
{
	gcry_cipher_hd_t cih;
	GNode *asn;
	gboolean ret;
	GBytes *bytes;

	/*
	 * On many test systems RC2 is no longer supported by libgcrypt, but
	 * in case these tests are run elsewhere, double check.
	 */
	if (gcry_cipher_algo_info (GCRY_CIPHER_RFC2268_128, GCRYCTL_TEST_ALGO, NULL, 0) == 0)
		return;

	bytes = g_bytes_new_static ("\x30\x09\x04\x07""invalid", 11);
	asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestAny", bytes);
	g_assert (asn != NULL);
	g_bytes_unref (bytes);

	ret = egg_symkey_read_cipher (g_quark_from_static_string ("1.2.840.113549.1.12.1.5"),
	                              "blah", 4, asn, &cih);

	g_assert (ret == FALSE);

	egg_asn1x_destroy (asn);
}

typedef struct {
	const gchar *name;
	const gchar *scheme;
	gsize digest_len;

	/* Info to use with cipher */
	const gchar *password;
	const gchar *salt;
	gsize iterations;

	/* DER representation of cipher */
	gsize n_der;
	const gchar *der;

	/* Data to encrypt and test with */
	gsize n_plain_length;
	const gchar *plain_text;
	const gchar *digest;
} ReadMac;

static const ReadMac mac_tests[] = {
	{
		"sha1", "1.3.14.3.2.26", 20,
		"password", "saltsalt", 33,
		31, "\x30\x1d"
			"\x30\x12"
				"\x30\x07"
					"\x06\x05\x2b\x0e\x03\x02\x1a"
				"\x04\x07""invalid"
			"\x04\x04""salt"
			"\x02\x01\x21",
		8, "plaintex", "\x8b\x96\x7f\xa2\xf4\x4f\x2d\x70\xcb\x59\x7e\x8f\xad\xf3\x92\x18\x70\x08\x5c\x57"
	}
};

static void
test_read_mac (gconstpointer data)
{
	const ReadMac *test = data;
	gcry_md_hd_t mdh;
	gpointer digest;
	gsize digest_len;
	GNode *asn;
	gboolean ret;
	GBytes *bytes;

	bytes = g_bytes_new_static (test->der, test->n_der);
	asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestAny", bytes);
	g_assert (asn != NULL);
	g_bytes_unref (bytes);

	ret = egg_symkey_read_mac (g_quark_from_static_string (test->scheme),
	                           test->password, strlen (test->password),
	                           asn, &mdh, &digest_len);

	g_assert_cmpint (digest_len, ==, test->digest_len);

	egg_asn1x_destroy (asn);
	g_assert (ret == TRUE);

	gcry_md_write (mdh, test->plain_text, test->n_plain_length);
	digest = gcry_md_read (mdh, 0);

	egg_assert_cmpmem (test->digest, digest_len, ==,
	                   digest, digest_len);

	gcry_md_close (mdh);
}

static void
test_read_mac_invalid (void)
{
	gcry_md_hd_t mdh;
	gsize digest_len;
	GNode *asn;
	gboolean ret;
	GBytes *bytes;

	bytes = g_bytes_new_static ("\x30\x09\x04\x07""invalid", 11);
	asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestAny", bytes);
	g_assert (asn != NULL);
	g_bytes_unref (bytes);

	ret = egg_symkey_read_mac (g_quark_from_static_string ("1.3.14.3.2.26"),
	                           "blah", 4, asn, &mdh, &digest_len);

	g_assert (ret == FALSE);

	egg_asn1x_destroy (asn);
}

static void
null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
                  const gchar *message, gpointer user_data)
{

}

int
main (int argc, char **argv)
{
	gchar *name;
	gint i;

	g_test_init (&argc, &argv, NULL);
	egg_libgcrypt_initialize ();

	/* Suppress these messages in tests */
	g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO,
	                   null_log_handler, NULL);

	g_test_add_func ("/symkey/generate_key_simple", test_generate_key_simple);
	g_test_add_func ("/symkey/generate_key_pkcs12", test_generate_key_pkcs12);
	g_test_add_func ("/symkey/generate_key_pbkdf2", test_generate_key_pbkdf2);
	g_test_add_func ("/symkey/generate_key_pbe", test_generate_key_pbe);

	for (i = 0; i < G_N_ELEMENTS (cipher_tests); i++) {
		name = g_strdup_printf ("/symkey/read-cipher/%s", cipher_tests[i].name);
		g_test_add_data_func (name, cipher_tests + i, test_read_cipher);
		g_free (name);
	}

	for (i = 0; i < G_N_ELEMENTS (cipher_invalid); i++) {
		name = g_strdup_printf ("/symkey/read-cipher-invalid/%s", cipher_invalid[i].name);
		g_test_add_data_func (name, cipher_invalid + i, test_read_cipher_invalid);
		g_free (name);
	}

	g_test_add_func ("/symkey/read-cipher-unsupported/pbe", test_read_cipher_unsupported_pbe);

	for (i = 0; i < G_N_ELEMENTS (mac_tests); i++) {
		name = g_strdup_printf ("/symkey/read-mac/%s", mac_tests[i].name);
		g_test_add_data_func (name, mac_tests + i, test_read_mac);
		g_free (name);
	}

	g_test_add_func ("/symkey/read-mac-invalid", test_read_mac_invalid);

	return g_test_run ();
}