summaryrefslogtreecommitdiffstats
path: root/tests/gnutls_hmac_fast.c
blob: 67b10d62837a97eff2040d9548bf1ad5d3b3c632 (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
/*
 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include "utils.h"

static void tls_log_func(int level, const char *str)
{
	fprintf(stderr, "|<%d>| %s", level, str);
}

void doit(void)
{
	unsigned char digest[20];
	int err;

	global_init();
	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	err =
	    gnutls_hmac_fast(GNUTLS_MAC_SHA1, "keykeykey", 9, "abcdefgh",
			     8, digest);
	if (err < 0)
		fail("gnutls_hmac_fast(SHA1) failed: %d\n", err);
	else {
		if (memcmp(digest, "\x58\x93\x7a\x58\xfe\xea\x82\xf8"
			   "\x0e\x64\x62\x01\x40\x2b\x2c\xed\x5d\x54\xc1\xfa",
			   20) == 0) {
			if (debug)
				success("gnutls_hmac_fast(SHA1) OK\n");
		} else {
			hexprint(digest, 20);
			fail("gnutls_hmac_fast(SHA1) failure\n");
		}
	}

	/* enable MD5 usage */
	if (gnutls_fips140_mode_enabled()) {
		gnutls_fips140_set_mode(GNUTLS_FIPS140_LOG, 0);
	}

	err =
	    gnutls_hmac_fast(GNUTLS_MAC_MD5, "keykeykey", 9, "abcdefgh", 8,
			     digest);
	if (err < 0)
		fail("gnutls_hmac_fast(MD5) failed: %d\n", err);
	else {
		if (memcmp(digest, "\x3c\xb0\x9d\x83\x28\x01\xef\xc0"
			   "\x7b\xb3\xaf\x42\x69\xe5\x93\x9a", 16) == 0) {
			if (debug)
				success("gnutls_hmac_fast(MD5) OK\n");
		} else {
			hexprint(digest, 16);
			fail("gnutls_hmac_fast(MD5) failure\n");
		}
	}

	err =
	    gnutls_hmac_fast(GNUTLS_MAC_AES_GMAC_128, "keykeykeykeykeyk", 16, "abcdefghabc", 8,
			     digest);
	if (err >= 0)
		fail("gnutls_hmac_fast(GMAC-128) succeeded unexpectedly: %d\n", err);
	else if (err != GNUTLS_E_INVALID_REQUEST)
		fail("gnutls_hmac_fast(GMAC-128) failure: %d\n", err);
	else if (debug)
		success("gnutls_hmac_fast(GMAC-128) OK\n");

	err =
	    gnutls_hmac_fast(GNUTLS_MAC_AES_GMAC_192, "keykeykeykeykeykeykeykey", 24,
			     "abcdefghabc", 8,
			     digest);
	if (err >= 0)
		fail("gnutls_hmac_fast(GMAC-192) succeeded unexpectedly: %d\n", err);
	else if (err != GNUTLS_E_INVALID_REQUEST)
		fail("gnutls_hmac_fast(GMAC-192) failure: %d\n", err);
	else if (debug)
		success("gnutls_hmac_fast(GMAC-192) OK\n");

	err =
	    gnutls_hmac_fast(GNUTLS_MAC_AES_GMAC_256, "keykeykeykeykeykeykeykeykeykeyke", 32,
			     "abcdefghabc", 8,
			     digest);
	if (err >= 0)
		fail("gnutls_hmac_fast(GMAC-256) succeeded unexpectedly: %d\n", err);
	else if (err != GNUTLS_E_INVALID_REQUEST)
		fail("gnutls_hmac_fast(GMAC-256) failure: %d\n", err);
	else if (debug)
		success("gnutls_hmac_fast(GMAC-256) OK\n");

	err =
	    gnutls_hmac_fast(GNUTLS_MAC_UMAC_96, "keykeykeykeykeyk", 16, "abcdefghabc", 8,
			     digest);
	if (err >= 0)
		fail("gnutls_hmac_fast(UMAC-96) succeeded unexpectedly: %d\n", err);
	else if (err != GNUTLS_E_INVALID_REQUEST)
		fail("gnutls_hmac_fast(UMAC-96) failure: %d\n", err);
	else if (debug)
		success("gnutls_hmac_fast(UMAC-96) OK\n");

	err =
	    gnutls_hmac_fast(GNUTLS_MAC_UMAC_128, "keykeykeykeykeyk", 16, "abcdefghabc", 8,
			     digest);
	if (err >= 0)
		fail("gnutls_hmac_fast(UMAC-128) succeeded unexpectedly: %d\n", err);
	else if (err != GNUTLS_E_INVALID_REQUEST)
		fail("gnutls_hmac_fast(UMAC-128) failure: %d\n", err);
	else if (debug)
		success("gnutls_hmac_fast(UMAC-128) OK\n");

	gnutls_global_deinit();
}