summaryrefslogtreecommitdiff
path: root/tests/kdf-api.c
blob: ec74f44ce847a334b1aa2092a68c1eb8cde56412 (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
/*
 * Copyright (C) 2020 Red Hat, Inc.
 *
 * Author: Daiki Ueno
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

#include "config.h"

#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>

#include <assert.h>
#include <stdint.h>

#include "utils.h"

#define MAX_BUF 1024

static void
test_hkdf(gnutls_mac_algorithm_t mac,
	  const char *ikm_hex,
	  const char *salt_hex,
	  const char *info_hex,
	  size_t length,
	  const char *prk_hex,
	  const char *okm_hex)
{
	gnutls_datum_t hex;
	gnutls_datum_t ikm;
	gnutls_datum_t salt;
	gnutls_datum_t info;
	gnutls_datum_t prk;
	gnutls_datum_t okm;
	uint8_t buf[MAX_BUF];

	success("HKDF test with %s\n", gnutls_mac_get_name(mac));

	/* Test HKDF-Extract */
	hex.data = (void *)ikm_hex;
	hex.size = strlen(ikm_hex);
	assert(gnutls_hex_decode2(&hex, &ikm) >= 0);

	hex.data = (void *)salt_hex;
	hex.size = strlen(salt_hex);
	assert(gnutls_hex_decode2(&hex, &salt) >= 0);

	assert(gnutls_hkdf_extract(mac, &ikm, &salt, buf) >= 0);
	gnutls_free(ikm.data);
	gnutls_free(salt.data);

	prk.data = buf;
	prk.size = strlen(prk_hex) / 2;
	assert(gnutls_hex_encode2(&prk, &hex) >= 0);

	if (strcmp((char *)hex.data, prk_hex))
		fail("prk doesn't match: %s != %s\n",
		     (char *)hex.data, prk_hex);

	gnutls_free(hex.data);

	/* Test HKDF-Expand */
	hex.data = (void *)info_hex;
	hex.size = strlen(info_hex);
	assert(gnutls_hex_decode2(&hex, &info) >= 0);

	assert(gnutls_hkdf_expand(mac, &prk, &info, buf, length) >= 0);
	gnutls_free(info.data);

	okm.data = buf;
	okm.size = strlen(okm_hex) / 2;
	assert(gnutls_hex_encode2(&okm, &hex) >= 0);

	if (strcmp((char *)hex.data, okm_hex))
		fail("okm doesn't match: %s != %s\n",
		     (char *)hex.data, okm_hex);

	gnutls_free(hex.data);
}

static void
test_pbkdf2(gnutls_mac_algorithm_t mac,
	    const char *ikm_hex,
	    const char *salt_hex,
	    unsigned iter_count,
	    size_t length,
	    const char *okm_hex)
{
	gnutls_datum_t hex;
	gnutls_datum_t ikm;
	gnutls_datum_t salt;
	gnutls_datum_t okm;
	uint8_t buf[MAX_BUF];

	success("PBKDF2 test with %s\n", gnutls_mac_get_name(mac));

	hex.data = (void *)ikm_hex;
	hex.size = strlen(ikm_hex);
	assert(gnutls_hex_decode2(&hex, &ikm) >= 0);

	hex.data = (void *)salt_hex;
	hex.size = strlen(salt_hex);
	assert(gnutls_hex_decode2(&hex, &salt) >= 0);

	assert(gnutls_pbkdf2(mac, &ikm, &salt, iter_count, buf, length) >= 0);
	gnutls_free(ikm.data);
	gnutls_free(salt.data);

	okm.data = buf;
	okm.size = length;
	assert(gnutls_hex_encode2(&okm, &hex) >= 0);

	if (strcmp((char *)hex.data, okm_hex))
		fail("okm doesn't match: %s != %s\n",
		     (char *)hex.data, okm_hex);

	gnutls_free(hex.data);
}

void
doit(void)
{
	/* Test vector from RFC 5869.  More thorough testing is done
	 * in nettle. */
	test_hkdf(GNUTLS_MAC_SHA256,
		  "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
		  "0b0b0b0b0b0b",
		  "000102030405060708090a0b0c",
		  "f0f1f2f3f4f5f6f7f8f9",
		  42,
		  "077709362c2e32df0ddc3f0dc47bba63"
		  "90b6c73bb50f9c3122ec844ad7c2b3e5",
		  "3cb25f25faacd57a90434f64d0362f2a"
		  "2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
		  "34007208d5b887185865");

	/* Test vector from RFC 6070.  More thorough testing is done
	 * in nettle. */
	test_pbkdf2(GNUTLS_MAC_SHA1,
		    "70617373776f7264", /* "password" */
		    "73616c74",		/* "salt" */
		    4096,
		    20,
		    "4b007901b765489abead49d926f721d065a429c1");
}